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)
 
uint64_t hashword (const std::vector< uint64_t > &keys)
 
uint16_t hashword (const uint16_t *k, size_t length)
 
uint16_t hashword (const std::vector< uint16_t > &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)
 

Function Documentation

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

The STL provides binary_search() which returns true/false depending on whether the searched-for value is found. Utility::binary_find() uses a binary search on a sorted range to return an iterator to the searched-for element, or "last" if the element is not found.

Definition at line 130 of file utility.h.

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

131 {
132  ForwardIterator it = std::lower_bound(first, last, value);
133  return (it == last || value < *it) ? last : it;
134 }
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 140 of file utility.h.

141 {
142  ForwardIterator it = std::lower_bound(first, last, value, comp);
143  return (it == last || comp(value,*it)) ? last : it;
144 }
template<typename T >
T libMesh::Utility::binomial ( n,
k 
)

Definition at line 221 of file utility.h.

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

222 {
223  T ret = 1;
224 
225  // Binomial function is "symmetric" in k, C(n, k) = C(n, n-k).
226  if (k > n - k)
227  k = n - k;
228 
229  // Compute n * (n-1) * ... * (n-k+1) / (k * (k-1) * ... * 1)
230  for (T i = 0; i < k; ++i)
231  {
232  ret *= (n - i);
233  ret /= (i + 1);
234  }
235 
236  return ret;
237 }
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 83 of file utility.C.

References libMesh::Quality::name().

Referenced by deallocate().

85 {
86  std::string name(basename);
87 
88  if (r_o_c == 0)
89  name.append(".real");
90 
91  else
92  name.append(".imag");
93 
94  return name;
95 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:39
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 245 of file utility.h.

References complex_filename(), prepare_complex_data(), and libMesh::swap().

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

246 {
247  std::vector<T>().swap(vec);
248 }
void swap(Iterator &lhs, Iterator &rhs)
unsigned int libMesh::Utility::factorial ( unsigned int  n)
inline

A simple implementation of the factorial.

Definition at line 204 of file utility.h.

205 {
206 
207  unsigned int factorial_n = 1;
208 
209  if (n==0)
210  return factorial_n;
211 
212  for (unsigned int i=1; i<n; i++)
213  factorial_n *= i+1;
214 
215  return factorial_n;
216 }
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(NULL);
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-stye 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 (NULL);
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)
uint32_t libMesh::Utility::hashword ( const uint32_t *  k,
size_t  length,
uint32_t  initval = 0 
)
inline

Definition at line 141 of file hashword.h.

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

142 {
143  uint32_t a,b,c;
144 
145  // Set up the internal state
146  a = b = c = 0xdeadbeef + ((static_cast<uint32_t>(length))<<2) + initval;
147 
148  //------------------------------------------------- handle most of the key
149  while (length > 3)
150  {
151  a += k[0];
152  b += k[1];
153  c += k[2];
154  mix(a,b,c);
155  length -= 3;
156  k += 3;
157  }
158 
159  //------------------------------------------- handle the last 3 uint32_t's
160  switch(length) // all the case statements fall through
161  {
162  case 3 : c+=k[2];
163  case 2 : b+=k[1];
164  case 1 : a+=k[0];
165  final(a,b,c);
166  default: // case 0: nothing left to add
167  break;
168  }
169 
170  //------------------------------------------------------ report the result
171  return c;
172 }
uint32_t libMesh::Utility::hashword ( const std::vector< uint32_t > &  keys,
uint32_t  initval = 0 
)
inline

Definition at line 178 of file hashword.h.

References hashword().

179 {
180  return hashword(&keys[0], keys.size(), initval);
181 }
uint16_t hashword(const std::vector< uint16_t > &keys)
Definition: hashword.h:280
uint64_t libMesh::Utility::hashword ( const uint64_t *  k,
size_t  length 
)
inline

Definition at line 224 of file hashword.h.

225 {
226  return fnv_64_buf(k, 8*length);
227 }
uint64_t libMesh::Utility::hashword ( const std::vector< uint64_t > &  keys)
inline

Definition at line 233 of file hashword.h.

References hashword().

234 {
235  return hashword(&keys[0], keys.size());
236 }
uint16_t hashword(const std::vector< uint16_t > &keys)
Definition: hashword.h:280
uint16_t libMesh::Utility::hashword ( const uint16_t *  k,
size_t  length 
)
inline

Definition at line 243 of file hashword.h.

244 {
245  // Three values that will be passed to final() after they are initialized.
246  uint32_t a = 0;
247  uint32_t b = 0;
248  uint32_t c = 0;
249 
250  switch (length)
251  {
252  case 3:
253  {
254  // Cast the inputs to 32 bit integers and call final().
255  a = k[0];
256  b = k[1];
257  c = k[2];
258  break;
259  }
260  case 4:
261  {
262  // Combine the 4 16-bit inputs, "w, x, y, z" into two 32-bit
263  // inputs "wx" and "yz" using bit operations and call final.
264  a = ((k[0]<<16) | (k[1] & 0xffff)); // wx
265  b = ((k[2]<<16) | (k[3] & 0xffff)); // yz
266  break;
267  }
268  default:
269  libmesh_error_msg("Unsupported length: " << length);
270  }
271 
272  // Result is returned in c
273  final(a,b,c);
274  return static_cast<uint16_t>(c);
275 }
uint16_t libMesh::Utility::hashword ( const std::vector< uint16_t > &  keys)
inline

Definition at line 280 of file hashword.h.

References hashword().

281 {
282  return hashword(&keys[0], keys.size());
283 }
uint16_t hashword(const std::vector< uint16_t > &keys)
Definition: hashword.h:280
uint32_t libMesh::Utility::hashword2 ( const uint32_t &  first,
const uint32_t &  second,
uint32_t  initval = 0 
)
inline

Definition at line 189 of file hashword.h.

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

190 {
191  uint32_t a,b,c;
192 
193  // Set up the internal state
194  a = b = c = 0xdeadbeef + 8 + initval;
195 
196  b+=second;
197  a+=first;
198  final(a,b,c);
199 
200  return c;
201 }
uint64_t libMesh::Utility::hashword2 ( const uint64_t  first,
const uint64_t  second 
)
inline

Definition at line 205 of file hashword.h.

206 {
207  // This isn't optimal (it would be nice to avoid this packing step)
208  // but we are going to go ahead and conform to the 32-bit
209  // hashword2() interface.
210  uint64_t k[2] = {first, second};
211 
212  // len is the total number of bytes in two 64-bit ints
213  return fnv_64_buf(k, /*len=*/8*2);
214 }
uint16_t libMesh::Utility::hashword2 ( const uint16_t  first,
const uint16_t  second 
)
inline

Definition at line 217 of file hashword.h.

218 {
219  return static_cast<uint16_t>(first%65449 + (second<<5)%65449);
220 }
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 58 of file utility.h.

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

59 {
60  while (first != last)
61  {
62  *first = value++;
63  ++first;
64  }
65 }
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 75 of file utility.h.

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

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

Referenced by deallocate().

102 {
103  const unsigned int len = source.size();
104 
105  real_part.resize(len);
106  imag_part.resize(len);
107 
108  for (unsigned int i=0; i<len; i++)
109  {
110  real_part[i] = source[i].real();
111  imag_part[i] = source[i].imag();
112  }
113 }
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
template<typename T >
T libMesh::Utility::string_to_enum ( const std::string &  s)

Takes the string s and returns the matching enumeration of type T.

std::string libMesh::Utility::system_info ( )

The system_info function returns information about the system you are running on.

Definition at line 45 of file utility.C.

References get_timestamp().

46 {
47  std::ostringstream oss;
48 
49  std::string date = Utility::get_timestamp();
50 
51  // Get system information
52  struct utsname sysInfo;
53  uname(&sysInfo);
54 
55  // Get user information
56 #ifdef LIBMESH_HAVE_GETPWUID
57  struct passwd * p = getpwuid(getuid());
58 #endif
59 
60 
61  oss << '\n'
62  << " ---------------------------------------------------------------------\n"
63  << "| Time: " << date << '\n'
64  << "| OS: " << sysInfo.sysname << '\n'
65  << "| HostName: " << sysInfo.nodename << '\n'
66  << "| OS Release " << sysInfo.release << '\n'
67  << "| OS Version: " << sysInfo.version << '\n'
68  << "| Machine: " << sysInfo.machine << '\n'
69 #ifdef LIBMESH_HAVE_GETPWUID
70  << "| Username: " << p->pw_name << '\n'
71 #else
72  << "| Username: " << "Unknown" << '\n'
73 #endif
74  << " ---------------------------------------------------------------------\n";
75 
76  return oss.str();
77 }
std::string get_timestamp()
Definition: timestamp.C:37