17 #include <deal.II/base/utilities.h> 18 #include <deal.II/base/exceptions.h> 19 #include <deal.II/base/thread_local_storage.h> 21 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
22 #include <boost/math/special_functions/erf.hpp> 23 #include <boost/lexical_cast.hpp> 24 #include <boost/random.hpp> 45 # include <winsock2.h> 49 #ifdef DEAL_II_WITH_TRILINOS 50 # ifdef DEAL_II_WITH_MPI 51 # include <Epetra_MpiComm.h> 52 # include <deal.II/lac/vector_memory.h> 53 # include <deal.II/lac/trilinos_vector.h> 54 # include <deal.II/lac/trilinos_block_vector.h> 56 # include "Teuchos_RCP.hpp" 57 # include "Epetra_SerialComm.h" 62 DEAL_II_NAMESPACE_OPEN
70 unsigned int,
unsigned int,
71 <<
"When trying to convert " << arg1
72 <<
" to a string with " << arg2 <<
" digits");
75 <<
"Invalid number " << arg1);
78 <<
"Can't convert the string " << arg1
79 <<
" to the desired type");
87 template <
typename number>
89 to_string (
const number value,
const unsigned int digits)
91 std::string lc_string = boost::lexical_cast<std::string>(value);
95 else if (lc_string.size() < digits)
98 const unsigned int padding_position = (lc_string[0] ==
'-')
104 const std::string padding(digits - lc_string.size(),
'0');
105 lc_string.insert(padding_position, padding);
113 const std::string &from,
114 const std::string &to)
119 std::string out = input;
120 std::string::size_type pos = out.find(from);
122 while (pos != std::string::npos)
124 out.replace(pos, from.size(), to);
125 pos = out.find(from, pos + to.size());
133 std::string::size_type left = 0;
134 std::string::size_type right = input.size() - 1;
136 for (; left < input.size(); ++left)
138 if (!std::isspace(input[left]))
144 for (; right >= left; --right)
146 if (!std::isspace(input[right]))
152 return std::string(input, left, right - left + 1);
172 if (max_number < 100)
174 if (max_number < 1000)
176 if (max_number < 10000)
178 if (max_number < 100000)
180 if (max_number < 1000000)
193 while ((s.size() > 0) && (s[0] ==
' '))
195 while ((s.size() > 0) && (s[s.size()-1] ==
' '))
206 const int i = std::strtol(s.c_str(), &p, 10);
207 AssertThrow ( !((errno != 0) || (s.size() == 0) || ((s.size()>0) && (*p !=
'\0'))),
208 ExcMessage (
"Can't convert <" + s +
"> to an integer."));
218 std::vector<int> tmp (s.size());
219 for (
unsigned int i=0; i<s.size(); ++i)
231 while ((s.size() > 0) && (s[0] ==
' '))
233 while ((s.size() > 0) && (s[s.size()-1] ==
' '))
244 const double d = std::strtod(s.c_str(), &p);
245 AssertThrow ( !((errno != 0) || (s.size() == 0) || ((s.size()>0) && (*p !=
'\0'))),
246 ExcMessage (
"Can't convert <" + s +
"> to an integer."));
256 std::vector<double> tmp (s.size());
257 for (
unsigned int i=0; i<s.size(); ++i)
264 std::vector<std::string>
266 const char delimiter)
274 while (tmp.length() != 0 && tmp[tmp.length()-1] ==
' ')
275 tmp.erase (tmp.length()-1, 1);
283 std::vector<std::string> split_list;
284 split_list.reserve (std::count (tmp.begin(), tmp.end(), delimiter)+1);
285 while (tmp.length() != 0)
290 if (name.find(delimiter) != std::string::npos)
292 name.erase (name.find(delimiter), std::string::npos);
293 tmp.erase (0, tmp.find(delimiter)+1);
299 while ((name.length() != 0) && (name[0] ==
' '))
301 while (name.length() != 0 && name[name.length()-1] ==
' ')
302 name.erase (name.length()-1, 1);
304 split_list.push_back (name);
312 std::vector<std::string>
314 const unsigned int width,
315 const char delimiter)
317 std::string text = original_text;
318 std::vector<std::string> lines;
321 while ((text.length() != 0) && (text[text.length()-1] == delimiter))
322 text.erase(text.length()-1,1);
325 while (text.length() != 0)
329 while ((text.length() != 0) && (text[0] == delimiter))
332 std::size_t pos_newline = text.find_first_of(
"\n", 0);
333 if (pos_newline != std::string::npos && pos_newline <= width)
335 std::string line (text, 0, pos_newline);
336 while ((line.length() != 0) && (line[line.length()-1] == delimiter))
337 line.erase(line.length()-1,1);
338 lines.push_back (line);
339 text.erase (0, pos_newline+1);
346 if (text.length() < width)
349 while ((text.length() != 0) && (text[text.length()-1] == delimiter))
350 text.erase(text.length()-1,1);
351 lines.push_back (text);
359 int location = std::min<int>(width,text.length()-1);
360 for (; location>0; --location)
361 if (text[location] == delimiter)
367 for (location = std::min<int>(width,text.length()-1);
368 location<static_cast<int>(text.length());
370 if (text[location] == delimiter)
376 std::string line (text, 0, location);
377 while ((line.length() != 0) && (line[line.length()-1] == delimiter))
378 line.erase(line.length()-1,1);
379 lines.push_back (line);
380 text.erase (0, location);
391 const std::string &pattern)
393 if (pattern.size() > name.size())
396 for (
unsigned int i=0; i<pattern.size(); ++i)
397 if (pattern[i] != name[i])
405 std::pair<int, unsigned int>
407 const unsigned int position)
409 Assert (position < name.size(), ExcInternalError());
411 const std::string test_string (name.begin()+position,
414 std::istringstream str(test_string);
424 return std::make_pair (i, 1U);
426 return std::make_pair (i, 2U);
428 return std::make_pair (i, 3U);
430 return std::make_pair (i, 4U);
432 return std::make_pair (i, 5U);
434 return std::make_pair (i, 6U);
436 return std::make_pair (i, 7U);
439 Assert (
false, ExcNotImplemented());
463 return boost::normal_distribution<>(a,sigma)(random_number_generator.
get());
468 std::vector<unsigned int>
471 const unsigned int n = permutation.size();
473 std::vector<unsigned int> out (n);
474 for (
unsigned int i=0; i<n; ++i)
475 out[i] = n - 1 - permutation[i];
482 std::vector<unsigned int>
485 const unsigned int n = permutation.size();
489 for (
unsigned int i=0; i<n; ++i)
491 Assert (permutation[i] < n, ExcIndexRange (permutation[i], 0, n));
492 out[permutation[i]] = i;
497 for (
unsigned int i=0; i<n; ++i)
499 ExcMessage (
"The given input permutation had duplicate entries!"));
504 std::vector<unsigned long long int>
507 const unsigned long long int n = permutation.size();
509 std::vector<unsigned long long int> out (n);
510 for (
unsigned long long int i=0; i<n; ++i)
511 out[i] = n - 1 - permutation[i];
518 std::vector<unsigned long long int>
521 const unsigned long long int n = permutation.size();
525 for (
unsigned long long int i=0; i<n; ++i)
527 Assert (permutation[i] < n, ExcIndexRange (permutation[i], 0, n));
528 out[permutation[i]] = i;
533 for (
unsigned long long int i=0; i<n; ++i)
535 ExcMessage (
"The given input permutation had duplicate entries!"));
541 template <
typename Integer>
545 const unsigned int n = permutation.size();
547 std::vector<Integer> out (n);
548 for (
unsigned int i=0; i<n; ++i)
549 out[i] = n - 1 - permutation[i];
556 template <
typename Integer>
560 const unsigned int n = permutation.size();
564 for (
unsigned int i=0; i<n; ++i)
566 Assert (permutation[i] < n, ExcIndexRange (permutation[i], 0, n));
567 out[permutation[i]] = i;
572 for (
unsigned int i=0; i<n; ++i)
574 ExcMessage (
"The given input permutation had duplicate entries!"));
584 #if defined(__linux__) 588 std::ifstream cpuinfo;
589 cpuinfo.open(
"/proc/loadavg");
617 #if defined(__linux__) 618 std::ifstream file(
"/proc/self/status");
624 if (name ==
"VmPeak:")
625 file >> stats.VmPeak;
626 else if (name ==
"VmSize:")
628 else if (name ==
"VmHWM:")
630 else if (name ==
"VmRSS:")
645 #if defined(DEAL_II_HAVE_UNISTD_H) && defined(DEAL_II_HAVE_GETHOSTNAME) 646 const unsigned int N=1024;
648 gethostname (&(hostname[0]), N-1);
650 std::string hostname(
"unknown");
659 std::time_t time1= std::time (0);
660 std::tm *time = std::localtime(&time1);
662 std::ostringstream o;
663 o << time->tm_hour <<
":" 664 << (time->tm_min < 10 ?
"0" :
"") << time->tm_min <<
":" 665 << (time->tm_sec < 10 ?
"0" :
"") << time->tm_sec;
674 std::time_t time1= std::time (0);
675 std::tm *time = std::localtime(&time1);
677 std::ostringstream o;
678 o << time->tm_year + 1900 <<
"/" 679 << time->tm_mon + 1 <<
"/" 697 *memptr = malloc (size);
712 #ifdef DEAL_II_WITH_TRILINOS 719 #ifdef DEAL_II_WITH_MPI 720 static Teuchos::RCP<Epetra_MpiComm>
721 communicator = Teuchos::rcp (
new Epetra_MpiComm (MPI_COMM_WORLD),
true);
723 static Teuchos::RCP<Epetra_SerialComm>
724 communicator = Teuchos::rcp (
new Epetra_SerialComm (),
true);
727 return *communicator;
735 #ifdef DEAL_II_WITH_MPI 736 static Teuchos::RCP<Epetra_MpiComm>
737 communicator = Teuchos::rcp (
new Epetra_MpiComm (MPI_COMM_SELF),
true);
739 static Teuchos::RCP<Epetra_SerialComm>
740 communicator = Teuchos::rcp (
new Epetra_SerialComm (),
true);
743 return *communicator;
749 duplicate_communicator (
const Epetra_Comm &communicator)
751 #ifdef DEAL_II_WITH_MPI 757 *mpi_comm =
dynamic_cast<const Epetra_MpiComm *
>(&communicator);
760 duplicate_communicator(mpi_comm->GetMpiComm()));
767 Assert (dynamic_cast<const Epetra_SerialComm *>(&communicator)
770 return new Epetra_SerialComm(dynamic_cast<const Epetra_SerialComm &>(communicator));
779 #ifdef DEAL_II_WITH_MPI 781 *mpi_comm =
dynamic_cast<Epetra_MpiComm *
>(&communicator);
784 MPI_Comm comm = mpi_comm->GetMpiComm();
785 *mpi_comm = Epetra_MpiComm(MPI_COMM_SELF);
786 MPI_Comm_free (&comm);
795 return mpi_communicator.NumProc();
801 return (
unsigned int)mpi_communicator.MyPID();
808 const Epetra_Comm &comm)
810 if (map.LinearMap() ==
true)
817 return Epetra_Map (map.NumGlobalElements(),
826 return Epetra_Map (map.NumGlobalElements(),
828 map.MyGlobalElements (),
837 template std::string to_string<int> (int,
unsigned int);
838 template std::string to_string<long int> (
long int,
unsigned int);
839 template std::string to_string<long long int> (
long long int,
unsigned int);
840 template std::string to_string<unsigned int> (
unsigned int,
unsigned int);
841 template std::string to_string<unsigned long int> (
unsigned long int,
unsigned int);
842 template std::string to_string<unsigned long long int> (
unsigned long long int,
unsigned int);
843 template std::string to_string<float> (float,
unsigned int);
844 template std::string to_string<double> (double,
unsigned int);
845 template std::string to_string<long double> (
long double,
unsigned int);
849 DEAL_II_NAMESPACE_CLOSE
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...
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
::ExceptionBase & ExcMessage(std::string arg1)
std::string trim(const std::string &input)
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter=' ')
#define AssertThrow(cond, exc)
const Epetra_Comm & comm_self()
bool job_supports_mpi() DEAL_II_DEPRECATED
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)
#define DeclException1(Exception1, type1, outsequence)
double generate_normal_random_number(const double a, const double sigma)
#define Assert(cond, exc)
std::vector< std::string > split_string_list(const std::string &s, const char delimiter=',')
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::string get_hostname()
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)
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
unsigned int needed_digits(const unsigned int max_number)