16 #include <deal.II/base/config.h> 22 # include <winsock2.h> 25 #include <deal.II/base/exceptions.h> 26 #include <deal.II/base/mpi.h> 27 #include <deal.II/base/thread_local_storage.h> 28 #include <deal.II/base/utilities.h> 30 #include <boost/lexical_cast.hpp> 31 #include <boost/math/special_functions/erf.hpp> 32 #include <boost/random.hpp> 47 #if defined(DEAL_II_HAVE_UNISTD_H) && defined(DEAL_II_HAVE_GETHOSTNAME) 56 #ifdef DEAL_II_WITH_TRILINOS 57 # ifdef DEAL_II_WITH_MPI 58 # include <deal.II/lac/trilinos_parallel_block_vector.h> 59 # include <deal.II/lac/trilinos_vector.h> 60 # include <deal.II/lac/vector_memory.h> 62 # include <Epetra_MpiComm.h> 64 # include <Epetra_SerialComm.h> 65 # include <Teuchos_RCP.hpp> 70 DEAL_II_NAMESPACE_OPEN
78 <<
"When trying to convert " << arg1 <<
" to a string with " 79 << arg2 <<
" digits");
83 <<
"Can't convert the string " << arg1
84 <<
" to the desired type");
90 return DEAL_II_PACKAGE_NAME
" version " DEAL_II_PACKAGE_VERSION;
103 template <
typename number>
105 to_string(
const number value,
const unsigned int digits)
107 std::string lc_string = boost::lexical_cast<std::string>(value);
111 else if (lc_string.size() < digits)
114 const unsigned int padding_position = (lc_string[0] ==
'-') ? 1 : 0;
116 const std::string padding(digits - lc_string.size(),
'0');
117 lc_string.insert(padding_position, padding);
125 const std::string &from,
126 const std::string &to)
131 std::string out = input;
132 std::string::size_type pos = out.find(from);
134 while (pos != std::string::npos)
136 out.replace(pos, from.size(), to);
137 pos = out.find(from, pos + to.size());
145 std::string::size_type left = 0;
146 std::string::size_type right = input.size() > 0 ? input.size() - 1 : 0;
148 for (; left < input.size(); ++left)
150 if (!std::isspace(input[left]))
156 for (; right >= left; --right)
158 if (!std::isspace(input[right]))
164 return std::string(input, left, right - left + 1);
184 if (max_number < 100)
186 if (max_number < 1000)
188 if (max_number < 10000)
190 if (max_number < 100000)
192 if (max_number < 1000000)
205 while ((s.size() > 0) && (s[0] ==
' '))
207 while ((s.size() > 0) && (s[s.size() - 1] ==
' '))
208 s.erase(s.end() - 1);
218 const int i = std::strtol(s.c_str(), &p, 10);
220 ((s.size() > 0) && (*p !=
'\0'))),
221 ExcMessage(
"Can't convert <" + s +
"> to an integer."));
231 std::vector<int> tmp(s.size());
232 for (
unsigned int i = 0; i < s.size(); ++i)
244 while ((s.size() > 0) && (s[0] ==
' '))
246 while ((s.size() > 0) && (s[s.size() - 1] ==
' '))
247 s.erase(s.end() - 1);
257 const double d = std::strtod(s.c_str(), &p);
259 ((s.size() > 0) && (*p !=
'\0'))),
260 ExcMessage(
"Can't convert <" + s +
"> to a double."));
270 std::vector<double> tmp(s.size());
271 for (
unsigned int i = 0; i < s.size(); ++i)
278 std::vector<std::string>
287 while (tmp.length() != 0 && tmp[tmp.length() - 1] ==
' ')
288 tmp.erase(tmp.length() - 1, 1);
296 std::vector<std::string> split_list;
297 while (tmp.length() != 0)
302 if (name.find(delimiter) != std::string::npos)
304 name.erase(name.find(delimiter), std::string::npos);
305 tmp.erase(0, tmp.find(delimiter) + delimiter.size());
311 while ((name.length() != 0) && (name[0] ==
' '))
313 while (name.length() != 0 && name[name.length() - 1] ==
' ')
314 name.erase(name.length() - 1, 1);
316 split_list.push_back(name);
323 std::vector<std::string>
332 std::vector<std::string>
334 const unsigned int width,
335 const char delimiter)
337 std::string text = original_text;
338 std::vector<std::string> lines;
341 while ((text.length() != 0) && (text[text.length() - 1] == delimiter))
342 text.erase(text.length() - 1, 1);
345 while (text.length() != 0)
349 while ((text.length() != 0) && (text[0] == delimiter))
352 std::size_t pos_newline = text.find_first_of(
'\n', 0);
353 if (pos_newline != std::string::npos && pos_newline <= width)
355 std::string line(text, 0, pos_newline);
356 while ((line.length() != 0) &&
357 (line[line.length() - 1] == delimiter))
358 line.erase(line.length() - 1, 1);
359 lines.push_back(line);
360 text.erase(0, pos_newline + 1);
367 if (text.length() < width)
370 while ((text.length() != 0) &&
371 (text[text.length() - 1] == delimiter))
372 text.erase(text.length() - 1, 1);
373 lines.push_back(text);
381 int location = std::min<int>(width, text.length() - 1);
382 for (; location > 0; --location)
383 if (text[location] == delimiter)
389 for (location = std::min<int>(width, text.length() - 1);
390 location < static_cast<int>(text.length());
392 if (text[location] == delimiter)
398 std::string line(text, 0, location);
399 while ((line.length() != 0) &&
400 (line[line.length() - 1] == delimiter))
401 line.erase(line.length() - 1, 1);
402 lines.push_back(line);
403 text.erase(0, location);
415 if (pattern.size() > name.size())
418 for (
unsigned int i = 0; i < pattern.size(); ++i)
419 if (pattern[i] != name[i])
427 std::pair<int, unsigned int>
432 const std::string test_string(name.begin() + position, name.end());
434 std::istringstream str(test_string);
444 return std::make_pair(i, 1U);
446 return std::make_pair(i, 2U);
448 return std::make_pair(i, 3U);
450 return std::make_pair(i, 4U);
452 return std::make_pair(i, 5U);
453 else if (i < 1000000)
454 return std::make_pair(i, 6U);
455 else if (i < 10000000)
456 return std::make_pair(i, 7U);
482 return boost::normal_distribution<>(a,
483 sigma)(random_number_generator.
get());
488 std::vector<unsigned int>
491 const unsigned int n = permutation.size();
493 std::vector<unsigned int> out(n);
494 for (
unsigned int i = 0; i < n; ++i)
495 out[i] = n - 1 - permutation[i];
502 std::vector<unsigned int>
505 const unsigned int n = permutation.size();
509 for (
unsigned int i = 0; i < n; ++i)
512 out[permutation[i]] = i;
517 for (
unsigned int i = 0; i < n; ++i)
519 ExcMessage(
"The given input permutation had duplicate entries!"));
524 std::vector<unsigned long long int>
527 const unsigned long long int n = permutation.size();
529 std::vector<unsigned long long int> out(n);
530 for (
unsigned long long int i = 0; i < n; ++i)
531 out[i] = n - 1 - permutation[i];
538 std::vector<unsigned long long int>
541 const unsigned long long int n = permutation.size();
545 for (
unsigned long long int i = 0; i < n; ++i)
548 out[permutation[i]] = i;
553 for (
unsigned long long int i = 0; i < n; ++i)
555 ExcMessage(
"The given input permutation had duplicate entries!"));
561 template <
typename Integer>
565 const unsigned int n = permutation.size();
567 std::vector<Integer> out(n);
568 for (
unsigned int i = 0; i < n; ++i)
569 out[i] = n - 1 - permutation[i];
576 template <
typename Integer>
580 const unsigned int n = permutation.size();
584 for (
unsigned int i = 0; i < n; ++i)
587 out[permutation[i]] = i;
592 for (
unsigned int i = 0; i < n; ++i)
594 ExcMessage(
"The given input permutation had duplicate entries!"));
603 #if defined(__linux__) 608 std::ifstream cpuinfo;
609 cpuinfo.open(
"/proc/loadavg");
632 switch (DEAL_II_COMPILER_VECTORIZATION_LEVEL)
645 "Invalid DEAL_II_COMPILER_VECTORIZATION_LEVEL."));
659 #if defined(__linux__) 660 std::ifstream file(
"/proc/self/status");
666 if (name ==
"VmPeak:")
668 else if (name ==
"VmSize:")
670 else if (name ==
"VmHWM:")
672 else if (name ==
"VmRSS:")
688 #if defined(DEAL_II_HAVE_UNISTD_H) && defined(DEAL_II_HAVE_GETHOSTNAME) 689 const unsigned int N = 1024;
691 gethostname(&(hostname[0]), N - 1);
693 std::string hostname(
"unknown");
703 std::time_t time1 = std::time(
nullptr);
704 std::tm * time = std::localtime(&time1);
706 std::ostringstream o;
707 o << time->tm_hour <<
":" << (time->tm_min < 10 ?
"0" :
"")
708 << time->tm_min <<
":" << (time->tm_sec < 10 ?
"0" :
"")
719 std::time_t time1 = std::time(
nullptr);
720 std::tm * time = std::localtime(&time1);
722 std::ostringstream o;
723 o << time->tm_year + 1900 <<
"/" << time->tm_mon + 1 <<
"/" 742 *memptr = malloc(size);
758 #ifdef DEAL_II_WITH_TRILINOS 765 # ifdef DEAL_II_WITH_MPI 766 static Teuchos::RCP<Epetra_MpiComm> communicator =
767 Teuchos::rcp(
new Epetra_MpiComm(MPI_COMM_WORLD),
true);
769 static Teuchos::RCP<Epetra_SerialComm> communicator =
770 Teuchos::rcp(
new Epetra_SerialComm(),
true);
773 return *communicator;
781 # ifdef DEAL_II_WITH_MPI 782 static Teuchos::RCP<Epetra_MpiComm> communicator =
783 Teuchos::rcp(
new Epetra_MpiComm(MPI_COMM_SELF),
true);
785 static Teuchos::RCP<Epetra_SerialComm> communicator =
786 Teuchos::rcp(
new Epetra_SerialComm(),
true);
789 return *communicator;
795 duplicate_communicator(
const Epetra_Comm &communicator)
797 # ifdef DEAL_II_WITH_MPI 802 const Epetra_MpiComm *mpi_comm =
803 dynamic_cast<const Epetra_MpiComm *
>(&communicator);
804 if (mpi_comm !=
nullptr)
805 return new Epetra_MpiComm(
813 Assert(dynamic_cast<const Epetra_SerialComm *>(&communicator) !=
nullptr,
815 return new Epetra_SerialComm(
816 dynamic_cast<const Epetra_SerialComm &>(communicator));
826 # ifdef DEAL_II_WITH_MPI 827 Epetra_MpiComm *mpi_comm =
dynamic_cast<Epetra_MpiComm *
>(&communicator);
828 if (mpi_comm !=
nullptr)
830 MPI_Comm comm = mpi_comm->GetMpiComm();
831 *mpi_comm = Epetra_MpiComm(MPI_COMM_SELF);
832 const int ierr = MPI_Comm_free(&comm);
843 return mpi_communicator.NumProc();
850 return (
unsigned int)mpi_communicator.MyPID();
858 if (map.LinearMap() ==
true)
865 return Epetra_Map(map.NumGlobalElements(),
874 return Epetra_Map(map.NumGlobalElements(),
876 map.MyGlobalElements(),
886 to_string<int>(int,
unsigned int);
888 to_string<long int>(
long int,
unsigned int);
890 to_string<long long int>(
long long int,
unsigned int);
892 to_string<unsigned int>(
unsigned int,
unsigned int);
894 to_string<unsigned long int>(
unsigned long int,
unsigned int);
896 to_string<unsigned long long int>(
unsigned long long int,
unsigned int);
898 to_string<float>(float,
unsigned int);
900 to_string<double>(double,
unsigned int);
902 to_string<long double>(
long double,
unsigned int);
906 DEAL_II_NAMESPACE_CLOSE
std::vector< std::string > split_string_list(const std::string &s, const std::string &delimiter=",")
static const unsigned int invalid_unsigned_int
#define DeclException2(Exception2, type1, type2, outsequence)
A class that provides a separate storage location on each thread that accesses the object...
static::ExceptionBase & ExcIO()
std::string dealii_version_string()
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
std::string trim(const std::string &input)
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
#define AssertThrow(cond, exc)
static::ExceptionBase & ExcOutOfMemory()
const Epetra_Comm & comm_self()
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
void get_memory_stats(MemoryStats &stats)
double string_to_double(const std::string &s)
static::ExceptionBase & ExcMessage(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
double generate_normal_random_number(const double a, const double sigma)
#define Assert(cond, exc)
static::ExceptionBase & ExcCantConvertString(std::string arg1)
static::ExceptionBase & ExcInvalidNumber(unsigned int arg1)
bool match_at_string_start(const std::string &name, const std::string &pattern)
void posix_memalign(void **memptr, size_t alignment, size_t size)
std::vector< unsigned int > invert_permutation(const std::vector< unsigned int > &permutation)
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
std::string dim_string(const int dim, const int spacedim)
void destroy_communicator(Epetra_Comm &communicator)
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter= ' ')
std::string get_hostname()
#define AssertThrowMPI(error_code)
MPI_Comm duplicate_communicator(const MPI_Comm &mpi_communicator)
const Epetra_Comm & comm_world()
int string_to_int(const std::string &s)
std::vector< unsigned int > reverse_permutation(const std::vector< unsigned int > &permutation)
static::ExceptionBase & ExcNotImplemented()
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
const std::string get_current_vectorization_level()
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
unsigned int needed_digits(const unsigned int max_number)
static::ExceptionBase & ExcInternalError()
static::ExceptionBase & ExcInvalidNumber2StringConversersion(unsigned int arg1, unsigned int arg2)