Reference documentation for deal.II version 8.4.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 #include <deal.II/base/mpi.h>
22 
23 #include <vector>
24 #include <utility>
25 #include <functional>
26 #include <string>
27 
28 #ifdef DEAL_II_WITH_TRILINOS
29 # include <Epetra_Comm.h>
30 # include <Epetra_Map.h>
31 # ifdef DEAL_II_WITH_MPI
32 # include <Epetra_MpiComm.h>
33 # else
34 # include <Epetra_SerialComm.h>
35 # endif
36 #endif
37 
38 DEAL_II_NAMESPACE_OPEN
39 
40 
49 namespace Utilities
50 {
51 
68  std::string
69  int_to_string (const unsigned int value,
70  const unsigned int digits = numbers::invalid_unsigned_int);
71 
82  template <typename number>
83  std::string
84  to_string (const number value,
85  const unsigned int digits = numbers::invalid_unsigned_int);
86 
91  unsigned int
92  needed_digits (const unsigned int max_number);
93 
98  int
99  string_to_int (const std::string &s);
100 
112  std::string dim_string(const int dim, const int spacedim);
113 
118  std::vector<int>
119  string_to_int (const std::vector<std::string> &s);
120 
125  double
126  string_to_double (const std::string &s);
127 
128 
133  std::vector<double>
134  string_to_double (const std::vector<std::string> &s);
135 
178  std::vector<std::string>
179  split_string_list (const std::string &s,
180  const char delimiter = ',');
181 
191  std::vector<std::string>
192  break_text_into_lines (const std::string &original_text,
193  const unsigned int width,
194  const char delimiter = ' ');
195 
200  bool
201  match_at_string_start (const std::string &name,
202  const std::string &pattern);
203 
212  std::pair<int, unsigned int>
213  get_integer_at_position (const std::string &name,
214  const unsigned int position);
215 
220  std::string replace_in_string(const std::string &input,
221  const std::string &from,
222  const std::string &to);
223 
229  std::string
230  trim(const std::string &input);
231 
256  double
257  generate_normal_random_number (const double a,
258  const double sigma);
259 
260 
269  template <int N, typename T>
270  T
271  fixed_power (const T t);
272 
282  template <int a, int N>
284  {
285  static const int value = a *fixed_int_power<a,N-1>::value;
286  };
287 
292  template <int a>
293  struct fixed_int_power<a,0>
294  {
295  static const int value = 1;
296  };
297 
319  template<typename Iterator, typename T>
320  Iterator
321  lower_bound (Iterator first,
322  Iterator last,
323  const T &val);
324 
325 
331  template<typename Iterator, typename T, typename Comp>
332  Iterator
333  lower_bound (Iterator first,
334  Iterator last,
335  const T &val,
336  const Comp comp);
337 
343  std::vector<unsigned int>
344  reverse_permutation (const std::vector<unsigned int> &permutation);
345 
351  std::vector<unsigned int>
352  invert_permutation (const std::vector<unsigned int> &permutation);
353 
359  std::vector<unsigned long long int>
360  reverse_permutation (const std::vector<unsigned long long int> &permutation);
361 
367  std::vector<unsigned long long int>
368  invert_permutation (const std::vector<unsigned long long int> &permutation);
369 
375  namespace System
376  {
377 
385  double get_cpu_load ();
386 
391  struct MemoryStats
392  {
393  unsigned long int VmPeak;
394  unsigned long int VmSize;
395  unsigned long int VmHWM;
396  unsigned long int VmRSS;
397  };
398 
399 
404  void get_memory_stats (MemoryStats &stats);
405 
406 
410  std::string get_hostname ();
411 
412 
416  std::string get_time ();
417 
422  std::string get_date ();
423 
438  void posix_memalign (void **memptr, size_t alignment, size_t size);
439 
444  }
445 
446 
447 #ifdef DEAL_II_WITH_TRILINOS
448 
453  namespace Trilinos
454  {
464  const Epetra_Comm &comm_world();
465 
475  const Epetra_Comm &comm_self();
476 
509  Epetra_Comm *
510  duplicate_communicator (const Epetra_Comm &communicator);
511 
534  void
535  destroy_communicator (Epetra_Comm &communicator);
536 
541  unsigned int get_n_mpi_processes (const Epetra_Comm &mpi_communicator);
542 
549  unsigned int get_this_mpi_process (const Epetra_Comm &mpi_communicator);
550 
561  Epetra_Map
562  duplicate_map (const Epetra_BlockMap &map,
563  const Epetra_Comm &comm);
564  }
565 
566 #endif
567 
568 
569 }
570 
571 
572 // --------------------- inline functions
573 
574 namespace Utilities
575 {
576  template <int N, typename T>
577  inline
578  T fixed_power (const T n)
579  {
580  Assert (N>0, ExcNotImplemented());
581  switch (N)
582  {
583  case 1:
584  return n;
585  case 2:
586  return n*n;
587  case 3:
588  return n*n*n;
589  case 4:
590  return n*n*n*n;
591  default:
592  T result = n;
593  for (int d=1; d<N; ++d)
594  result *= n;
595  return result;
596  }
597  }
598 
599 
600 
601  template<typename Iterator, typename T>
602  inline
603  Iterator
604  lower_bound (Iterator first,
605  Iterator last,
606  const T &val)
607  {
608  return Utilities::lower_bound (first, last, val,
609  std::less<T>());
610  }
611 
612 
613 
614  template<typename Iterator, typename T, typename Comp>
615  inline
616  Iterator
617  lower_bound (Iterator first,
618  Iterator last,
619  const T &val,
620  const Comp comp)
621  {
622  // verify that the two iterators are properly ordered. since
623  // we need operator- for the iterator type anyway, do the
624  // test as follows, rather than via 'last >= first'
625  Assert (last - first >= 0,
626  ExcMessage ("The given iterators do not satisfy the proper ordering."));
627 
628  unsigned int len = static_cast<unsigned int>(last-first);
629 
630  if (len==0)
631  return first;
632 
633  while (true)
634  {
635  // if length equals 8 or less,
636  // then do a rolled out
637  // search. use a switch without
638  // breaks for that and roll-out
639  // the loop somehow
640  if (len < 8)
641  {
642  switch (len)
643  {
644  case 7:
645  if (!comp(*first, val))
646  return first;
647  ++first;
648  case 6:
649  if (!comp(*first, val))
650  return first;
651  ++first;
652  case 5:
653  if (!comp(*first, val))
654  return first;
655  ++first;
656  case 4:
657  if (!comp(*first, val))
658  return first;
659  ++first;
660  case 3:
661  if (!comp(*first, val))
662  return first;
663  ++first;
664  case 2:
665  if (!comp(*first, val))
666  return first;
667  ++first;
668  case 1:
669  if (!comp(*first, val))
670  return first;
671  return first+1;
672  default:
673  // indices seem
674  // to not be
675  // sorted
676  // correctly!? or
677  // did len
678  // become==0
679  // somehow? that
680  // shouldn't have
681  // happened
682  Assert (false, ExcInternalError());
683  }
684  }
685 
686 
687 
688  const unsigned int half = len >> 1;
689  const Iterator middle = first + half;
690 
691  // if the value is larger than
692  // that pointed to by the
693  // middle pointer, then the
694  // insertion point must be
695  // right of it
696  if (comp(*middle, val))
697  {
698  first = middle + 1;
699  len -= half + 1;
700  }
701  else
702  len = half;
703  }
704  }
705 }
706 
707 
708 DEAL_II_NAMESPACE_CLOSE
709 
710 #endif
Iterator lower_bound(Iterator first, Iterator last, const T &val)
Definition: utilities.h:604
static const unsigned int invalid_unsigned_int
Definition: types.h:164
unsigned int get_n_mpi_processes(const Epetra_Comm &mpi_communicator)
Definition: utilities.cc:793
::ExceptionBase & ExcMessage(std::string arg1)
std::string trim(const std::string &input)
Definition: utilities.cc:131
std::pair< int, unsigned int > get_integer_at_position(const std::string &name, const unsigned int position)
Definition: utilities.cc:406
const Epetra_Comm & comm_self()
Definition: utilities.cc:733
bool job_supports_mpi() DEAL_II_DEPRECATED
Definition: utilities.cc:705
std::string get_date()
Definition: utilities.cc:672
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:89
void get_memory_stats(MemoryStats &stats)
Definition: utilities.cc:610
double string_to_double(const std::string &s)
Definition: utilities.cc:227
double get_cpu_load()
Definition: utilities.cc:601
T fixed_power(const T t)
Definition: utilities.h:578
BlockDynamicSparsityPattern BlockCompressedSparsityPattern DEAL_II_DEPRECATED
double generate_normal_random_number(const double a, const double sigma)
Definition: utilities.cc:450
#define Assert(cond, exc)
Definition: exceptions.h:294
unsigned long int VmSize
Definition: utilities.h:394
bool match_at_string_start(const std::string &name, const std::string &pattern)
Definition: utilities.cc:390
void posix_memalign(void **memptr, size_t alignment, size_t size)
Definition: utilities.cc:687
std::vector< unsigned int > invert_permutation(const std::vector< unsigned int > &permutation)
Definition: utilities.cc:483
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:82
std::string replace_in_string(const std::string &input, const std::string &from, const std::string &to)
Definition: utilities.cc:112
std::string dim_string(const int dim, const int spacedim)
Definition: utilities.cc:158
void destroy_communicator(Epetra_Comm &communicator)
Definition: utilities.cc:775
Definition: mpi.h:55
std::vector< std::string > break_text_into_lines(const std::string &original_text, const unsigned int width, const char delimiter= ' ')
Definition: utilities.cc:313
std::string get_hostname()
Definition: utilities.cc:643
unsigned long int VmHWM
Definition: utilities.h:395
const Epetra_Comm & comm_world()
Definition: utilities.cc:717
std::vector< std::string > split_string_list(const std::string &s, const char delimiter= ',')
Definition: utilities.cc:265
int string_to_int(const std::string &s)
Definition: utilities.cc:189
std::vector< unsigned int > reverse_permutation(const std::vector< unsigned int > &permutation)
Definition: utilities.cc:469
unsigned long int VmRSS
Definition: utilities.h:396
unsigned int get_this_mpi_process(const Epetra_Comm &mpi_communicator)
Definition: utilities.cc:799
Epetra_Map duplicate_map(const Epetra_BlockMap &map, const Epetra_Comm &comm)
Definition: utilities.cc:807
std::string get_time()
Definition: utilities.cc:657
unsigned int needed_digits(const unsigned int max_number)
Definition: utilities.cc:168