Reference documentation for deal.II version 8.5.1
utilities.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2005 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__utilities_h
17 #define dealii__utilities_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 
22 #include <vector>
23 #include <utility>
24 #include <functional>
25 #include <string>
26 
27 #ifdef DEAL_II_WITH_TRILINOS
28 # include <Epetra_Comm.h>
29 # include <Epetra_Map.h>
30 # ifdef DEAL_II_WITH_MPI
31 # include <Epetra_MpiComm.h>
32 # else
33 # include <Epetra_SerialComm.h>
34 # endif
35 #endif
36 
37 DEAL_II_NAMESPACE_OPEN
38 
39 
48 namespace Utilities
49 {
50 
67  std::string
68  int_to_string (const unsigned int value,
69  const unsigned int digits = numbers::invalid_unsigned_int);
70 
81  template <typename number>
82  std::string
83  to_string (const number value,
84  const unsigned int digits = numbers::invalid_unsigned_int);
85 
90  unsigned int
91  needed_digits (const unsigned int max_number);
92 
97  int
98  string_to_int (const std::string &s);
99 
111  std::string dim_string(const int dim, const int spacedim);
112 
117  std::vector<int>
118  string_to_int (const std::vector<std::string> &s);
119 
124  double
125  string_to_double (const std::string &s);
126 
127 
132  std::vector<double>
133  string_to_double (const std::vector<std::string> &s);
134 
177  std::vector<std::string>
178  split_string_list (const std::string &s,
179  const char delimiter = ',');
180 
190  std::vector<std::string>
191  break_text_into_lines (const std::string &original_text,
192  const unsigned int width,
193  const char delimiter = ' ');
194 
199  bool
200  match_at_string_start (const std::string &name,
201  const std::string &pattern);
202 
211  std::pair<int, unsigned int>
212  get_integer_at_position (const std::string &name,
213  const unsigned int position);
214 
219  std::string replace_in_string(const std::string &input,
220  const std::string &from,
221  const std::string &to);
222 
228  std::string
229  trim(const std::string &input);
230 
255  double
256  generate_normal_random_number (const double a,
257  const double sigma);
258 
259 
268  template <int N, typename T>
269  T
270  fixed_power (const T t);
271 
281  template <int a, int N>
283  {
284  static const int value = a *fixed_int_power<a,N-1>::value;
285  };
286 
291  template <int a>
292  struct fixed_int_power<a,0>
293  {
294  static const int value = 1;
295  };
296 
318  template<typename Iterator, typename T>
319  Iterator
320  lower_bound (Iterator first,
321  Iterator last,
322  const T &val);
323 
324 
330  template<typename Iterator, typename T, typename Comp>
331  Iterator
332  lower_bound (Iterator first,
333  Iterator last,
334  const T &val,
335  const Comp comp);
336 
342  std::vector<unsigned int>
343  reverse_permutation (const std::vector<unsigned int> &permutation);
344 
350  std::vector<unsigned int>
351  invert_permutation (const std::vector<unsigned int> &permutation);
352 
358  std::vector<unsigned long long int>
359  reverse_permutation (const std::vector<unsigned long long int> &permutation);
360 
366  std::vector<unsigned long long int>
367  invert_permutation (const std::vector<unsigned long long int> &permutation);
368 
374  namespace System
375  {
376 
384  double get_cpu_load ();
385 
390  struct MemoryStats
391  {
395  unsigned long int VmPeak;
396 
400  unsigned long int VmSize;
401 
405  unsigned long int VmHWM;
406 
410  unsigned long int VmRSS;
411  };
412 
413 
418  void get_memory_stats (MemoryStats &stats);
419 
420 
424  std::string get_hostname ();
425 
426 
430  std::string get_time ();
431 
436  std::string get_date ();
437 
452  void posix_memalign (void **memptr, size_t alignment, size_t size);
453 
457  bool job_supports_mpi () DEAL_II_DEPRECATED;
458  }
459 
460 
461 #ifdef DEAL_II_WITH_TRILINOS
462 
467  namespace Trilinos
468  {
478  const Epetra_Comm &comm_world();
479 
489  const Epetra_Comm &comm_self();
490 
523  Epetra_Comm *
524  duplicate_communicator (const Epetra_Comm &communicator);
525 
548  void
549  destroy_communicator (Epetra_Comm &communicator);
550 
555  unsigned int get_n_mpi_processes (const Epetra_Comm &mpi_communicator);
556 
563  unsigned int get_this_mpi_process (const Epetra_Comm &mpi_communicator);
564 
575  Epetra_Map
576  duplicate_map (const Epetra_BlockMap &map,
577  const Epetra_Comm &comm);
578  }
579 
580 #endif
581 
582 
583 }
584 
585 
586 // --------------------- inline functions
587 
588 namespace Utilities
589 {
590  template <int N, typename T>
591  inline
592  T fixed_power (const T n)
593  {
594  Assert (N>0, ExcNotImplemented());
595  switch (N)
596  {
597  case 1:
598  return n;
599  case 2:
600  return n*n;
601  case 3:
602  return n*n*n;
603  case 4:
604  return n*n*n*n;
605  default:
606  T result = n;
607  for (int d=1; d<N; ++d)
608  result *= n;
609  return result;
610  }
611  }
612 
613 
614 
615  template<typename Iterator, typename T>
616  inline
617  Iterator
618  lower_bound (Iterator first,
619  Iterator last,
620  const T &val)
621  {
622  return Utilities::lower_bound (first, last, val,
623  std::less<T>());
624  }
625 
626 
627 
628  template<typename Iterator, typename T, typename Comp>
629  inline
630  Iterator
631  lower_bound (Iterator first,
632  Iterator last,
633  const T &val,
634  const Comp comp)
635  {
636  // verify that the two iterators are properly ordered. since
637  // we need operator- for the iterator type anyway, do the
638  // test as follows, rather than via 'last >= first'
639  Assert (last - first >= 0,
640  ExcMessage ("The given iterators do not satisfy the proper ordering."));
641 
642  unsigned int len = static_cast<unsigned int>(last-first);
643 
644  if (len==0)
645  return first;
646 
647  while (true)
648  {
649  // if length equals 8 or less,
650  // then do a rolled out
651  // search. use a switch without
652  // breaks for that and roll-out
653  // the loop somehow
654  if (len < 8)
655  {
656  switch (len)
657  {
658  case 7:
659  if (!comp(*first, val))
660  return first;
661  ++first;
662  case 6:
663  if (!comp(*first, val))
664  return first;
665  ++first;
666  case 5:
667  if (!comp(*first, val))
668  return first;
669  ++first;
670  case 4:
671  if (!comp(*first, val))
672  return first;
673  ++first;
674  case 3:
675  if (!comp(*first, val))
676  return first;
677  ++first;
678  case 2:
679  if (!comp(*first, val))
680  return first;
681  ++first;
682  case 1:
683  if (!comp(*first, val))
684  return first;
685  return first+1;
686  default:
687  // indices seem
688  // to not be
689  // sorted
690  // correctly!? or
691  // did len
692  // become==0
693  // somehow? that
694  // shouldn't have
695  // happened
696  Assert (false, ExcInternalError());
697  }
698  }
699 
700 
701 
702  const unsigned int half = len >> 1;
703  const Iterator middle = first + half;
704 
705  // if the value is larger than
706  // that pointed to by the
707  // middle pointer, then the
708  // insertion point must be
709  // right of it
710  if (comp(*middle, val))
711  {
712  first = middle + 1;
713  len -= half + 1;
714  }
715  else
716  len = half;
717  }
718  }
719 }
720 
721 
722 DEAL_II_NAMESPACE_CLOSE
723 
724 #endif
Iterator lower_bound(Iterator first, Iterator last, const T &val)
Definition: utilities.h:618
static const unsigned int invalid_unsigned_int
Definition: types.h:170
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
Definition: utilities.cc:797
std::string trim(const std::string &input)
Definition: utilities.cc:134
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
Definition: utilities.cc:409
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter=' ')
Definition: utilities.cc:316
const Epetra_Comm & comm_self()
Definition: utilities.cc:736
std::string get_date()
Definition: utilities.cc:675
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:92
void get_memory_stats(MemoryStats &stats)
Definition: utilities.cc:613
double string_to_double(const std::string &s)
Definition: utilities.cc:230
double get_cpu_load()
Definition: utilities.cc:604
T fixed_power(const T t)
Definition: utilities.h:592
static ::ExceptionBase & ExcMessage(std::string arg1)
double generate_normal_random_number(const double a, const double sigma)
Definition: utilities.cc:453
#define Assert(cond, exc)
Definition: exceptions.h:313
std::vector< std::string > split_string_list(const std::string &s, const char delimiter=',')
Definition: utilities.cc:268
unsigned long int VmSize
Definition: utilities.h:400
bool match_at_string_start(const std::string &name, const std::string &pattern)
Definition: utilities.cc:393
void posix_memalign(void **memptr, size_t alignment, size_t size)
Definition: utilities.cc:690
std::vector< unsigned int > invert_permutation(const std::vector< unsigned int > &permutation)
Definition: utilities.cc:486
bool job_supports_mpi() 1
Definition: utilities.cc:708
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:85
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
Definition: utilities.cc:115
std::string dim_string(const int dim, const int spacedim)
Definition: utilities.cc:161
void destroy_communicator(Epetra_Comm &communicator)
Definition: utilities.cc:778
Definition: mpi.h:48
std::string get_hostname()
Definition: utilities.cc:646
unsigned long int VmHWM
Definition: utilities.h:405
const Epetra_Comm & comm_world()
Definition: utilities.cc:720
int string_to_int(const std::string &s)
Definition: utilities.cc:192
std::vector< unsigned int > reverse_permutation(const std::vector< unsigned int > &permutation)
Definition: utilities.cc:472
static ::ExceptionBase & ExcNotImplemented()
unsigned long int VmRSS
Definition: utilities.h:410
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
Definition: utilities.cc:803
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
Definition: utilities.cc:811
std::string get_time()
Definition: utilities.cc:660
unsigned long int VmPeak
Definition: utilities.h:395
unsigned int needed_digits(const unsigned int max_number)
Definition: utilities.cc:171
static ::ExceptionBase & ExcInternalError()