libMesh::Utility Namespace Reference

Classes

struct  do_pow
 
struct  do_pow< 0, T >
 
struct  do_pow< 1, T >
 
struct  do_pow< 6, T >
 
class  ReverseBytes
 

Functions

std::string get_timestamp ()
 
uint32_t hashword (const uint32_t *k, size_t length, uint32_t initval=0)
 
uint32_t hashword (const std::vector< uint32_t > &keys, uint32_t initval=0)
 
uint32_t hashword2 (const uint32_t &first, const uint32_t &second, uint32_t initval=0)
 
uint64_t hashword2 (const uint64_t first, const uint64_t second)
 
uint16_t hashword2 (const uint16_t first, const uint16_t second)
 
uint64_t hashword (const uint64_t *k, size_t length)
 
uint16_t hashword (const uint16_t *k, size_t length)
 
template<typename Container >
Container::value_type hashword (const Container &keys)
 
template<typename T >
string_to_enum (const std::string &s)
 
template<typename T >
std::string enum_to_string (const T e)
 
void print_timestamp (std::ostream &target=std::cout)
 
std::string system_info ()
 
template<typename ForwardIter , typename T >
void iota (ForwardIter first, ForwardIter last, T value)
 
template<class InputIterator >
bool is_sorted (InputIterator first, InputIterator last)
 
template<class ForwardIterator , class T >
ForwardIterator binary_find (ForwardIterator first, ForwardIterator last, const T &value)
 
template<class ForwardIterator , class T , class Compare >
ForwardIterator binary_find (ForwardIterator first, ForwardIterator last, const T &value, Compare comp)
 
template<int N, typename T >
pow (const T &x)
 
unsigned int factorial (unsigned int n)
 
template<typename T >
binomial (T n, T k)
 
template<typename T >
void deallocate (std::vector< T > &vec)
 
std::string complex_filename (const std::string &basename, unsigned int r_o_c=0)
 
void prepare_complex_data (const std::vector< Complex > &source, std::vector< Real > &real_part, std::vector< Real > &imag_part)
 
int mkdir (const char *pathname)
 

Function Documentation

◆ binary_find() [1/2]

template<class ForwardIterator , class T >
ForwardIterator libMesh::Utility::binary_find ( ForwardIterator  first,
ForwardIterator  last,
const T &  value 
)

The STL provides std::binary_search() which returns true or false depending on whether the searched-for value is found. In contrast, Utility::binary_find() uses a std::lower_bound() based search on a sorted range to find the required value.

Returns
An iterator to the searched-for element, or "last" if the element is not found.

Definition at line 135 of file utility.h.

References value.

Referenced by libMesh::ReplicatedMesh::stitching_helper(), and libMesh::TetGenMeshInterface::triangulate_conformingDelaunayMesh_carvehole().

136 {
137  ForwardIterator it = std::lower_bound(first, last, value);
138  return (it == last || value < *it) ? last : it;
139 }
static const bool value
Definition: xdr_io.C:109

◆ binary_find() [2/2]

template<class ForwardIterator , class T , class Compare >
ForwardIterator libMesh::Utility::binary_find ( ForwardIterator  first,
ForwardIterator  last,
const T &  value,
Compare  comp 
)

As above, but takes a custom comparison object.

Definition at line 145 of file utility.h.

References value.

146 {
147  ForwardIterator it = std::lower_bound(first, last, value, comp);
148  return (it == last || comp(value,*it)) ? last : it;
149 }
static const bool value
Definition: xdr_io.C:109

◆ binomial()

template<typename T >
T libMesh::Utility::binomial ( n,
k 
)

Definition at line 224 of file utility.h.

Referenced by libMesh::FE< Dim, LAGRANGE_VEC >::shape(), and libMesh::FE< Dim, LAGRANGE_VEC >::shape_deriv().

225 {
226  T ret = 1;
227 
228  // Binomial function is "symmetric" in k, C(n, k) = C(n, n-k).
229  if (k > n - k)
230  k = n - k;
231 
232  // Compute n * (n-1) * ... * (n-k+1) / (k * (k-1) * ... * 1)
233  for (T i = 0; i < k; ++i)
234  {
235  ret *= (n - i);
236  ret /= (i + 1);
237  }
238 
239  return ret;
240 }

◆ complex_filename()

std::string libMesh::Utility::complex_filename ( const std::string &  basename,
unsigned int  r_o_c = 0 
)
Returns
For r_o_c = 0 the filename for output of the real part of complex data, and for r_o_c = 1 the filename for the imaginary part.

Definition at line 105 of file utility.C.

References libMesh::Quality::name().

107 {
108  std::string name(basename);
109 
110  if (r_o_c == 0)
111  name.append(".real");
112 
113  else
114  name.append(".imag");
115 
116  return name;
117 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42

◆ deallocate()

template<typename T >
void libMesh::Utility::deallocate ( std::vector< T > &  vec)

A convenient method to truly empty a vector using the "swap trick"

Definition at line 247 of file utility.h.

References swap().

Referenced by libMesh::Nemesis_IO::read().

248 {
249  std::vector<T>().swap(vec);
250 }
void swap(Iterator &lhs, Iterator &rhs)

◆ enum_to_string()

◆ factorial()

unsigned int libMesh::Utility::factorial ( unsigned int  n)
inline

A simple implementation of the factorial.

Definition at line 207 of file utility.h.

208 {
209 
210  unsigned int factorial_n = 1;
211 
212  if (n==0)
213  return factorial_n;
214 
215  for (unsigned int i=1; i<n; i++)
216  factorial_n *= i+1;
217 
218  return factorial_n;
219 }

◆ get_timestamp()

std::string libMesh::Utility::get_timestamp ( )

Definition at line 37 of file timestamp.C.

References libMesh::out.

Referenced by libMesh::PerfLog::get_info_header(), print_timestamp(), and system_info().

38 {
39 #ifdef LIBMESH_HAVE_LOCALE
40  // Create time_put "facet"
41  std::locale loc;
42  const std::time_put<char> & tp = std::use_facet <std::time_put<char>> (loc);
43 
44  // Call C-style time getting functions
45  time_t now = time(nullptr);
46  tm * tm_struct = localtime(&now);
47 
48  // Date will eventually be stored in this ostringstream's string
49  std::ostringstream date_stream;
50 
51  // See below for documentation on the use of the
52  // std::time_put::put() function
53  tp.put(date_stream, /*s*/
54  date_stream, /*str*/
55  date_stream.fill(), /*fill*/
56  tm_struct, /*tm*/
57  'c'); /*format*/
58 
59  // Another way to use it is to totally customize the format...
60  // char pattern[]="%d %B %Y %I:%M:%S %p";
61  // tp.put(date_stream, /*s*/
62  // date_stream, /*str*/
63  // date_stream.fill(), /*fill*/
64  // tm_struct, /*tm*/
65  // pattern, /*format begin*/
66  // pattern+sizeof(pattern)-1); /*format end */
67 
68  return date_stream.str();
69 #else
70  // C-style code originally found here:
71  // http://people.sc.fsu.edu/~burkardt/cpp_src/timestamp/timestamp.C
72  // Author: John Burkardt, 24 September 2003
73  const unsigned int time_size = 40;
74  char time_buffer[time_size];
75 
76  time_t now = time (nullptr);
77  tm * tm_struct = localtime (&now);
78 
79  // No more than time_size characters will be placed into the array. If the
80  // total number of resulting characters, including the terminating
81  // NUL character, is not more than time_size, strftime() returns the
82  // number of characters in the array, not counting the terminating
83  // NUL. Otherwise, zero is returned and the buffer contents are
84  // indeterminate.
85  size_t len = strftime ( time_buffer, time_size, "%c", tm_struct );
86 
87  if (len != 0)
88  return std::string(time_buffer);
89  else
90  {
91  libMesh::out << "Error formatting time buffer, returning empty string!" << std::endl;
92  return std::string("");
93  }
94 
95 #endif // LIBMESH_HAVE_LOCALE
96 }
OStreamProxy out(std::cout)

◆ hashword() [1/5]

uint32_t libMesh::Utility::hashword ( const uint32_t *  k,
size_t  length,
uint32_t  initval = 0 
)
inline

The hashword function takes an array of uint32_t's of length 'length' and computes a single key from it.

Author
Bob Jenkins
Date
May 2006

Definition at line 153 of file hashword.h.

Referenced by libMesh::Elem::compute_key(), hashword(), and libMesh::Elem::key().

154 {
155  uint32_t a,b,c;
156 
157  // Set up the internal state
158  a = b = c = 0xdeadbeef + ((static_cast<uint32_t>(length))<<2) + initval;
159 
160  //------------------------------------------------- handle most of the key
161  while (length > 3)
162  {
163  a += k[0];
164  b += k[1];
165  c += k[2];
166  mix(a,b,c);
167  length -= 3;
168  k += 3;
169  }
170 
171  //------------------------------------------- handle the last 3 uint32_t's
172  switch(length) // all the case statements fall through
173  {
174  case 3 : c+=k[2];
175  libmesh_fallthrough();
176  case 2 : b+=k[1];
177  libmesh_fallthrough();
178  case 1 : a+=k[0];
179  final(a,b,c);
180  libmesh_fallthrough();
181  default: // case 0: nothing left to add
182  break;
183  }
184 
185  //------------------------------------------------------ report the result
186  return c;
187 }

◆ hashword() [2/5]

uint32_t libMesh::Utility::hashword ( const std::vector< uint32_t > &  keys,
uint32_t  initval = 0 
)
inline

Calls function above with slightly more convenient std::vector interface.

Definition at line 195 of file hashword.h.

References hashword().

196 {
197  return hashword(keys.data(), keys.size(), initval);
198 }
Container::value_type hashword(const Container &keys)
Definition: hashword.h:304

◆ hashword() [3/5]

uint64_t libMesh::Utility::hashword ( const uint64_t *  k,
size_t  length 
)
inline

Call the 64-bit FNV hash function.

Definition at line 249 of file hashword.h.

250 {
251  return fnv_64_buf(k, 8*length);
252 }

◆ hashword() [4/5]

uint16_t libMesh::Utility::hashword ( const uint16_t *  k,
size_t  length 
)
inline

In a personal communication from Bob Jenkins, he recommended using "Probably final() from lookup3.c... You could hash up to 6 16-bit integers that way. The output is c, or the top or bottom 16 bits of c if you only need 16 bit hash values." [JWP]

Definition at line 263 of file hashword.h.

264 {
265  // Three values that will be passed to final() after they are initialized.
266  uint32_t a = 0;
267  uint32_t b = 0;
268  uint32_t c = 0;
269 
270  switch (length)
271  {
272  case 3:
273  {
274  // Cast the inputs to 32 bit integers and call final().
275  a = k[0];
276  b = k[1];
277  c = k[2];
278  break;
279  }
280  case 4:
281  {
282  // Combine the 4 16-bit inputs, "w, x, y, z" into two 32-bit
283  // inputs "wx" and "yz" using bit operations and call final.
284  a = ((k[0]<<16) | (k[1] & 0xffff)); // wx
285  b = ((k[2]<<16) | (k[3] & 0xffff)); // yz
286  break;
287  }
288  default:
289  libmesh_error_msg("Unsupported length: " << length);
290  }
291 
292  // Result is returned in c
293  final(a,b,c);
294  return static_cast<uint16_t>(c);
295 }

◆ hashword() [5/5]

template<typename Container >
Container::value_type libMesh::Utility::hashword ( const Container &  keys)
inline

Calls functions above with slightly more convenient std::vector/array compatible interface.

Definition at line 304 of file hashword.h.

References hashword().

305 {
306  return hashword(keys.data(), keys.size());
307 }
Container::value_type hashword(const Container &keys)
Definition: hashword.h:304

◆ hashword2() [1/3]

uint32_t libMesh::Utility::hashword2 ( const uint32_t &  first,
const uint32_t &  second,
uint32_t  initval = 0 
)
inline

This is a hard-coded version of hashword for hashing exactly 2 numbers.

Author
Bob Jenkins
Date
May 2006

Definition at line 210 of file hashword.h.

Referenced by libMesh::Elem::compute_key().

211 {
212  uint32_t a,b,c;
213 
214  // Set up the internal state
215  a = b = c = 0xdeadbeef + 8 + initval;
216 
217  b+=second;
218  a+=first;
219  final(a,b,c);
220 
221  return c;
222 }

◆ hashword2() [2/3]

uint64_t libMesh::Utility::hashword2 ( const uint64_t  first,
const uint64_t  second 
)
inline

Call the 64-bit FNV hash function.

Definition at line 228 of file hashword.h.

229 {
230  // This isn't optimal (it would be nice to avoid this packing step)
231  // but we are going to go ahead and conform to the 32-bit
232  // hashword2() interface.
233  uint64_t k[2] = {first, second};
234 
235  // len is the total number of bytes in two 64-bit ints
236  return fnv_64_buf(k, /*len=*/8*2);
237 }

◆ hashword2() [3/3]

uint16_t libMesh::Utility::hashword2 ( const uint16_t  first,
const uint16_t  second 
)
inline

Definition at line 240 of file hashword.h.

241 {
242  return static_cast<uint16_t>(first%65449 + (second<<5)%65449);
243 }

◆ iota()

template<typename ForwardIter , typename T >
void libMesh::Utility::iota ( ForwardIter  first,
ForwardIter  last,
value 
)

Utility::iota is a duplication of the SGI STL extension std::iota. It simply assigns sequentially increasing values to a range. That is, it assigns value to *first, value + 1 to *(first + 1) and so on. In general, each iterator i in the range [first, last) is assigned value + (i - first).

Definition at line 57 of file utility.h.

References value.

Referenced by libMesh::PetscVector< T >::create_subvector(), libMesh::PetscVector< T >::localize(), libMesh::System::project_vector(), and libMesh::System::projection_matrix().

58 {
59  // Use std::iota instead!
60  libmesh_deprecated();
61 
62  while (first != last)
63  {
64  *first = value++;
65  ++first;
66  }
67 }
static const bool value
Definition: xdr_io.C:109

◆ is_sorted()

template<class InputIterator >
bool libMesh::Utility::is_sorted ( InputIterator  first,
InputIterator  last 
)

Utility::is_sorted mimics the behavior of the SGI STL extension std::is_sorted. Checks to see if the range [first,last) is sorted in non-decreasing order, ie. for each "i" in [first,last) *i <= *(i+1).

Definition at line 77 of file utility.h.

References libMesh::MeshTools::Subdivision::prev.

78 {
79  if (first == last)
80  return true;
81 
82  // "prev" always points to the entry just to the left of "first"
83  // [- - - - - -]
84  // ^ ^
85  // prev first
86  //
87  // [- - - - - -]
88  // ^ ^
89  // prev first
90  //
91  // [- - - - - -]
92  // ^ ^
93  // prev first
94  InputIterator prev( first );
95  for (++first; first != last; ++prev, ++first)
96  if (*first < *prev) // Note: this is the same as *prev > *first,
97  return false; // but we only require op< to be defined.
98 
99  // If we haven't returned yet, it's sorted!
100  return true;
101 
102 
103  // A one-liner version using adjacent_find. This doesn't work for
104  // C-style arrays, since their pointers do not have a value_type.
105  //
106  // Works by checking to see if adjacent entries satisfy *i >
107  // *(i+1) and returns the first one which does. If "last" is
108  // returned, no such pair was found, and therefore the range must
109  // be in non-decreasing order.
110  //
111  // return (last ==
112  // std::adjacent_find(first, last,
113  // std::greater<typename InputIterator::value_type >()));
114 
115  // A second one-linear attempt. This one checks for a **strictly
116  // increasing** (no duplicate entries) range. Also doesn't work
117  // with C-style arrays.
118  //
119  // return (last ==
120  // std::adjacent_find(first, last,
121  // std::not2(std::less<typename InputIterator::value_type>())));
122 }
static const unsigned int prev[3]

◆ mkdir()

int libMesh::Utility::mkdir ( const char *  pathname)

Create a directory.

Definition at line 140 of file utility.C.

140  {
141 #if defined(LIBMESH_HAVE_MKDIR)
142  return ::mkdir(pathname, 0755);
143 #elif LIBMESH_HAVE_DECL__MKDIR
144  return _mkdir(pathname);
145 #else
146  libmesh_error_msg("Function mkdir not available on this system.");
147 #endif
148 
149 }
int mkdir(const char *pathname)
Definition: utility.C:140

◆ pow()

template<int N, typename T >
T libMesh::Utility::pow ( const T &  x)
inline

◆ prepare_complex_data()

void libMesh::Utility::prepare_complex_data ( const std::vector< Complex > &  source,
std::vector< Real > &  real_part,
std::vector< Real > &  imag_part 
)

Prepare complex data for writing.

Definition at line 121 of file utility.C.

124 {
125  const unsigned int len = source.size();
126 
127  real_part.resize(len);
128  imag_part.resize(len);
129 
130  for (unsigned int i=0; i<len; i++)
131  {
132  real_part[i] = source[i].real();
133  imag_part[i] = source[i].imag();
134  }
135 }

◆ print_timestamp()

void libMesh::Utility::print_timestamp ( std::ostream &  target = std::cout)
inline

Definition at line 37 of file timestamp.h.

References get_timestamp().

38 {
39  target << get_timestamp() << std::endl;
40 }
std::string get_timestamp()
Definition: timestamp.C:37

◆ string_to_enum()

template<typename T >
T libMesh::Utility::string_to_enum ( const std::string &  s)
Returns
the enumeration of type T which matches the string s.

◆ system_info()

std::string libMesh::Utility::system_info ( )
Returns
A string containing information about the system you are running on.

Definition at line 57 of file utility.C.

References get_timestamp().

58 {
59  std::ostringstream oss;
60 
61  std::string date = Utility::get_timestamp();
62 
63 #ifdef LIBMESH_HAVE_SYS_UTSNAME_H
64  // Get system information
65  struct utsname sysInfo;
66  uname(&sysInfo);
67 #endif
68 
69  // Get user information
70 #ifdef LIBMESH_HAVE_GETPWUID
71  struct passwd * p = getpwuid(getuid());
72 #endif
73 
74 
75  oss << '\n'
76  << " ---------------------------------------------------------------------\n"
77  << "| Time: " << date << '\n'
78 #ifdef LIBMESH_HAVE_SYS_UTSNAME_H
79  << "| OS: " << sysInfo.sysname << '\n'
80  << "| HostName: " << sysInfo.nodename << '\n'
81  << "| OS Release " << sysInfo.release << '\n'
82  << "| OS Version: " << sysInfo.version << '\n'
83  << "| Machine: " << sysInfo.machine << '\n'
84 #else
85  << "| OS: " << "Unknown" << '\n'
86  << "| HostName: " << "Unknown" << '\n'
87  << "| OS Release " << "Unknown" << '\n'
88  << "| OS Version: " << "Unknown" << '\n'
89  << "| Machine: " << "Unknown" << '\n'
90 #endif
91 #ifdef LIBMESH_HAVE_GETPWUID
92  << "| Username: " << p->pw_name << '\n'
93 #else
94  << "| Username: " << "Unknown" << '\n'
95 #endif
96  << " ---------------------------------------------------------------------\n";
97 
98  return oss.str();
99 }
std::string get_timestamp()
Definition: timestamp.C:37