libMesh
Loading...
Searching...
No Matches
Classes | Functions
libMesh::Utility Namespace Reference

Classes

struct  CompareUnderlying
 Struct which defines a custom comparison object that can be used with std::sets of std::unique_ptrs. More...
 
struct  do_pow
 An efficient template instantiation for raising to an arbitrary integer power. More...
 
struct  do_pow< 0, T >
 
struct  do_pow< 1, T >
 
struct  do_pow< 6, T >
 
class  is_streamable
 Helper struct for enabling template metaprogramming/SFINAE. More...
 
class  ReverseBytes
 This Functor simply takes an object and reverses its byte representation. More...
 

Functions

template<typename T >
std::string enum_to_string (const T e)
 
uint32_t hashword (const uint32_t *k, size_t length, uint32_t initval=0)
 The hashword function takes an array of uint32_t's of length 'length' and computes a single key from it.
 
uint32_t hashword (const std::vector< uint32_t > &keys, uint32_t initval=0)
 Calls function above with slightly more convenient std::vector interface.
 
uint32_t hashword2 (const uint32_t &first, const uint32_t &second, uint32_t initval=0)
 This is a hard-coded version of hashword for hashing exactly 2 numbers.
 
uint64_t hashword2 (const uint64_t first, const uint64_t second)
 Computes the same hash as calling fnv_64_buf() with exactly two entries.
 
uint16_t hashword2 (const uint16_t first, const uint16_t second)
 
uint64_t hashword (const uint64_t *k, size_t length)
 Call the 64-bit FNV hash function.
 
uint16_t hashword (const uint16_t *k, size_t length)
 In a personal communication from Bob Jenkins, he recommended using "Probably final_mix() from lookup3.c... You could hash up to 6 16-bit integers that way.
 
template<typename Container >
Container::value_type hashword (const Container &keys)
 Calls functions above with slightly more convenient std::vector/array compatible interface.
 
template<typename T >
string_to_enum (const std::string &s)
 
template<typename T >
string_to_enum (std::string_view s)
 
template<typename T >
string_to_enum (const char *s)
 
std::string get_timestamp ()
 
void print_timestamp (std::ostream &target=std::cout)
 
std::string system_info ()
 
template<typename Map , typename Key , typename std::enable_if<!is_streamable< Key >::value, Key >::type * = nullptr>
Map::mapped_type & map_find (Map &map, const Key &key, const char *filename, int line_number)
 -Wdangling-reference was nowhere near ready to add to -Wall in gcc 13.
 
template<typename Map , typename Key , typename std::enable_if<!is_streamable< Key >::value, Key >::type * = nullptr>
const Map::mapped_type & map_find (const Map &map, const Key &key, const char *filename, int line_number)
 A version of the function above that works for const objects.
 
template<typename Vector >
Vector::reference & vector_at (Vector &vec, typename Vector::size_type i, const char *filename, int line_number)
 A replacement for std::vector::at(i) which is meant to be used with a macro, and, unlike at(), gives a proper line number and useful error message when the index is past the end.
 
template<typename Vector >
Vector::const_reference & vector_at (const Vector &vec, typename Vector::size_type i, const char *filename, int line_number)
 Same as above, but for const inputs.
 
template<class ForwardIterator , class T >
ForwardIterator 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.
 
template<class ForwardIterator , class T , class Compare >
ForwardIterator binary_find (ForwardIterator first, ForwardIterator last, const T &value, Compare comp)
 As above, but takes a custom comparison object.
 
template<int N, typename T >
pow (const T &x)
 
unsigned int factorial (unsigned int n)
 A simple implementation of the factorial.
 
template<typename T >
binomial (T n, T k)
 
template<typename T >
void deallocate (std::vector< T > &vec)
 A convenient method to truly empty a vector using the "swap trick".
 
std::string_view basename_of (const std::string &fullname)
 
bool contains (std::string_view superstring, std::string_view substring)
 Look for a substring within a string.
 
bool ends_with (std::string_view superstring, std::string_view suffix)
 Look for a substring at the very end of a string.
 
std::string complex_filename (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)
 Prepare complex data for writing.
 
int mkdir (const char *pathname)
 Create a directory.
 
std::string unzip_file (std::string_view name)
 Create an unzipped copy of a bz2 or xz file, returning the name of the now-unzipped file that can be directly opened.
 

Function Documentation

◆ basename_of()

std::string_view libMesh::Utility::basename_of ( const std::string &  fullname)

Definition at line 108 of file utility.C.

109{
110 std::size_t first_char = fullname.find_last_of("/\\");
111 if (first_char == std::string::npos)
112 first_char = 0;
113 return std::string_view(fullname).substr(first_char);
114}

Referenced by libMesh::SparseMatrix< T >::print(), libMesh::SparseMatrix< T >::read(), libMesh::NameBasedIO::read(), libMesh::NameBasedIO::write(), and libMesh::NameBasedIO::write_equation_systems().

◆ 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 233 of file utility.h.

234{
235 ForwardIterator it = std::lower_bound(first, last, value);
236 return (it == last || value < *it) ? last : it;
237}
static const bool value
Definition xdr_io.C:55

References value.

Referenced by libMesh::BoundaryInfo::add_elements(), libMesh::UnstructuredMesh::stitching_helper(), libMesh::TetGenMeshInterface::triangulate_conformingDelaunayMesh_carvehole(), and NonManifoldGhostingFunctorTest::verify_send_list_entries_helper().

◆ 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 243 of file utility.h.

244{
245 ForwardIterator it = std::lower_bound(first, last, value, comp);
246 return (it == last || comp(value,*it)) ? last : it;
247}

References value.

◆ binomial()

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

Definition at line 322 of file utility.h.

323{
324 T ret = 1;
325
326 // Binomial function is "symmetric" in k, C(n, k) = C(n, n-k).
327 if (k > n - k)
328 k = n - k;
329
330 // Compute n * (n-1) * ... * (n-k+1) / (k * (k-1) * ... * 1)
331 for (T i = 0; i < k; ++i)
332 {
333 ret *= (n - i);
334 ret /= (i + 1);
335 }
336
337 return ret;
338}

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

◆ complex_filename()

std::string libMesh::Utility::complex_filename ( 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 119 of file utility.C.

121{
122 if (r_o_c == 0)
123 basename.append(".real");
124
125 else
126 basename.append(".imag");
127
128 return basename;
129}

◆ contains()

bool libMesh::Utility::contains ( std::string_view  superstring,
std::string_view  substring 
)

◆ 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 345 of file utility.h.

346{
347 std::vector<T>().swap(vec);
348}

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

◆ ends_with()

bool libMesh::Utility::ends_with ( std::string_view  superstring,
std::string_view  suffix 
)

Look for a substring at the very end of a string.

Definition at line 213 of file utility.C.

215{
216 // This can just be C++20 ends_with() someday
217 const auto sufsize = suffix.size();
218 const auto supsize = superstring.size();
219 if (sufsize > supsize)
220 return false;
221
222 return superstring.compare(supsize - sufsize, std::string::npos,
223 suffix) == 0;
224}

Referenced by libMesh::NameBasedIO::is_parallel_file_format(), libMesh::Xdr::open(), libMesh::STLIO::open_file(), libMesh::SparseMatrix< T >::print(), libMesh::SparseMatrix< T >::read(), libMesh::NameBasedIO::read(), libMesh::AbaqusIO::read(), libMesh::DynaIO::read(), libMesh::UnstructuredMesh::read(), libMesh::NumericVector< T >::read_matlab(), libMesh::SparseMatrix< T >::read_matlab(), unzip_file(), libMesh::NameBasedIO::write(), libMesh::NameBasedIO::write_nodal_data(), and libMesh::VTKIO::write_nodal_data().

◆ enum_to_string()

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

Referenced by libMesh::LaspackLinearSolver< T >::adjoint_solve(), libMesh::AbaqusIO::assign_sideset_ids(), libMesh::AbaqusIO::assign_subdomain_ids(), libMesh::Elem::build(), libMesh::FETransformationBase< OutputShape >::build(), libMesh::Partitioner::build(), libMesh::FEGenericBase< OutputType >::build(), libMesh::FEAbstract::build(), libMesh::MeshTools::Generation::build_cube(), libMesh::FEGenericBase< OutputType >::build_InfFE(), libMesh::Elem::complete_order_equivalent_type(), libMesh::InfFE< Dim, T_radial, T_map >::compute_node_indices(), libMesh::InfFE< Dim, T_radial, T_map >::compute_node_indices_fast(), libMesh::FEGenericBase< FEOutputType< T >::type >::compute_periodic_constraints(), libMesh::InfFE< Dim, T_radial, T_map >::compute_shape_indices(), libMesh::InfFE< Dim, T_radial, T_map >::compute_shape_indices(), SideVertexAverageNormalTest::construct_elem(), VolumeTest::construct_elem(), libMesh::TriangleWrapper::copy_tri_to_mesh(), libMesh::FEGenericBase< FEOutputType< T >::type >::determine_calculations(), libMesh::UNVIO::elements_out(), libMesh::ReferenceElem::get(), libMesh::FEInterface::get_continuity(), libMesh::InfFEBase::get_elem_type(), libMesh::Elem::get_info(), libMesh::MeshBase::get_info(), libMesh::FEAbstract::get_refspace_nodes(), libMesh::VariationalSmootherSystem::get_target_elem(), libMesh::MeshTools::Generation::Private::idx(), libMesh::MeshTools::Generation::Private::idx(), libMesh::FEInterface::ifem_inverse_map(), libMesh::FEInterface::ifem_inverse_map(), libMesh::FEInterface::ifem_map(), libMesh::FEInterface::ifem_nodal_soln(), libMesh::PetscVector< T >::init(), libMesh::QNodal::init_1D(), libMesh::QClough::init_2D(), libMesh::QConical::init_2D(), libMesh::QGauss::init_2D(), libMesh::QGaussLobatto::init_2D(), libMesh::QGrundmann_Moller::init_2D(), libMesh::QGrid::init_2D(), libMesh::QNodal::init_2D(), libMesh::QSimpson::init_2D(), libMesh::QTrap::init_2D(), libMesh::QClough::init_3D(), libMesh::QConical::init_3D(), libMesh::QGauss::init_3D(), libMesh::QGaussLobatto::init_3D(), libMesh::QGrundmann_Moller::init_3D(), libMesh::QGrid::init_3D(), libMesh::QNodal::init_3D(), libMesh::QSimpson::init_3D(), libMesh::QTrap::init_3D(), libMesh::AdvectionSystem::init_data(), libMesh::FEInterface::is_hierarchic(), libMesh::LIBMESH_DEFAULT_VECTORIZED_FE(), libMesh::LIBMESH_DEFAULT_VECTORIZED_FE(), libMesh::LIBMESH_DEFAULT_VECTORIZED_FE(), libMesh::monomial_n_dofs(), libMesh::FEAbstract::on_reference_element(), libMesh::PatchRecoveryErrorEstimator::EstimateError::operator()(), libMesh::WeightedPatchRecoveryErrorEstimator::EstimateError::operator()(), libMesh::LinearSolver< T >::print_converged_reason(), libMesh::Elem::quality(), libMesh::ExodusII_IO::read(), libMesh::CheckpointIO::read_connectivity(), libMesh::AbaqusIO::read_elements(), libMesh::GmshIO::read_mesh(), libMesh::Elem::second_order_equivalent_type(), libMesh::PetscPreconditioner< T >::set_petsc_preconditioner_type(), libMesh::PetscLinearSolver< T >::set_petsc_solver_type(), libMesh::SlepcEigenSolver< T >::set_slepc_solver_type(), libMesh::AztecLinearSolver< T >::set_solver_type(), libMesh::FE< Dim, T >::shape(), libMesh::FE< Dim, T >::shape(), libMesh::FE< Dim, T >::shape_deriv(), libMesh::FE< Dim, T >::shape_deriv(), libMesh::FE< Dim, T >::shape_deriv(), libMesh::FE< Dim, T >::shape_second_deriv(), libMesh::FE< Dim, T >::shape_second_deriv(), libMesh::FE< Dim, T >::shape_second_deriv(), libMesh::FE< Dim, T >::shape_second_deriv(), libMesh::EigenSparseLinearSolver< T >::solve(), libMesh::LaspackLinearSolver< T >::solve(), ExodusTest< elem_type >::test_read_gold(), XdrIOTest< elem_type >::test_read_gold(), ExodusTest< elem_type >::test_write(), XdrIOTest< elem_type >::test_write(), QuadratureTest::testBuild(), MeshSmootherTest::testVariationalSmoother(), libMesh::TriangleInterface::triangulate(), libMesh::GMVIO::write_ascii_old_impl(), libMesh::CheckpointIO::write_connectivity(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::ExodusII_IO_Helper::write_elements(), and libMesh::GmshIO::write_post().

◆ factorial()

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

A simple implementation of the factorial.

Definition at line 305 of file utility.h.

306{
307
308 unsigned int factorial_n = 1;
309
310 if (n==0)
311 return factorial_n;
312
313 for (unsigned int i=1; i<n; i++)
314 factorial_n *= i+1;
315
316 return factorial_n;
317}

◆ get_timestamp()

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

Definition at line 37 of file timestamp.C.

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

References libMesh::out.

Referenced by libMesh::RBConstruction::compute_Fq_representor_innerprods(), libMesh::RBConstruction::compute_output_dual_innerprods(), libMesh::PerfLog::get_info_header(), print_timestamp(), system_info(), libMesh::TransientRBConstruction::update_residual_terms(), and libMesh::RBConstruction::update_residual_terms().

◆ hashword() [1/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 330 of file hashword.h.

331{
332 return hashword(keys.data(), keys.size());
333}
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval=0)
The hashword function takes an array of uint32_t's of length 'length' and computes a single key from ...
Definition hashword.h:158

References hashword().

◆ 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 200 of file hashword.h.

201{
202 return hashword(keys.data(), keys.size(), initval);
203}

References hashword().

◆ hashword() [3/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_mix() 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 289 of file hashword.h.

290{
291 // Three values that will be passed to final_mix() after they are initialized.
292 uint32_t a = 0;
293 uint32_t b = 0;
294 uint32_t c = 0;
295
296 switch (length)
297 {
298 case 3:
299 {
300 // Cast the inputs to 32 bit integers and call final_mix().
301 a = k[0];
302 b = k[1];
303 c = k[2];
304 break;
305 }
306 case 4:
307 {
308 // Combine the 4 16-bit inputs, "w, x, y, z" into two 32-bit
309 // inputs "wx" and "yz" using bit operations and call final_mix.
310 a = ((k[0]<<16) | (k[1] & 0xffff)); // wx
311 b = ((k[2]<<16) | (k[3] & 0xffff)); // yz
312 break;
313 }
314 default:
315 libmesh_error_msg("Unsupported length: " << length);
316 }
317
318 // Result is returned in c
319 final_mix(a,b,c);
320 return static_cast<uint16_t>(c);
321}
static const Real b

References b.

◆ hashword() [4/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 158 of file hashword.h.

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

References b.

Referenced by libMesh::Elem::compute_key(), libMesh::Elem::compute_key(), libMesh::SparsityPattern::Build::handle_vi_vj(), hashword(), hashword(), libMesh::Elem::key(), libMesh::Polyhedron::key(), libMesh::Polygon::key(), libMesh::Polyhedron::low_order_key(), and libMesh::MeshTools::SidesToElemMap::HashFunction::operator()().

◆ hashword() [5/5]

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

Call the 64-bit FNV hash function.

Definition at line 275 of file hashword.h.

276{
277 return fnv_64_buf(k, 8*length);
278}

◆ hashword2() [1/3]

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

Definition at line 266 of file hashword.h.

267{
268 return static_cast<uint16_t>(first%65449 + (second<<5)%65449);
269}

◆ hashword2() [2/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 215 of file hashword.h.

216{
217 uint32_t a,b,c;
218
219 // Set up the internal state
220 a = b = c = 0xdeadbeef + 8 + initval;
221
222 b+=second;
223 a+=first;
224 final_mix(a,b,c);
225
226 return c;
227}

References b.

Referenced by libMesh::Elem::compute_key(), and libMesh::SparsityPattern::Build::handle_vi_vj().

◆ hashword2() [3/3]

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

Computes the same hash as calling fnv_64_buf() with exactly two entries.

This function allows the compiler to optimize by unrolling loops whose number of iterations are known at compile time. By inspecting the assembly generated for different optimization levels, we observed that the compiler sometimes chooses to unroll only the outer loop, but may also choose to unroll both the outer and inner loops.

Definition at line 238 of file hashword.h.

239{
240 // Initializing hval with this value corresponds to the FNV-1 hash algorithm.
241 uint64_t hval = static_cast<uint64_t>(0xcbf29ce484222325ULL);
242
243 for (int i=0; i!=2; ++i)
244 {
245 // char pointers to (start, end) of either "first" or "second". We interpret
246 // the 64 bits of each input 64-bit integer as 8 8-byte characters.
247 auto beg = reinterpret_cast<const unsigned char *>(i==0 ? &first : &second);
248 auto end = beg + sizeof(uint64_t)/sizeof(unsigned char); // beg+8
249
250 // FNV-1 hash each octet of the buffer
251 while (beg < end)
252 {
253 hval +=
254 (hval << 1) + (hval << 4) + (hval << 5) +
255 (hval << 7) + (hval << 8) + (hval << 40);
256
257 // xor the bottom with the current octet
258 hval ^= static_cast<uint64_t>(*beg++);
259 }
260 }
261
262 return hval;
263}

◆ map_find() [1/2]

template<typename Map , typename Key , typename std::enable_if<!is_streamable< Key >::value, Key >::type * = nullptr>
const Map::mapped_type & libMesh::Utility::map_find ( const Map &  map,
const Key &  key,
const char *  filename,
int  line_number 
)
inline

A version of the function above that works for const objects.

Definition at line 124 of file utility.h.

128{
129 auto it = map.find(key);
130 libmesh_error_msg_if(it == map.end(),
131 "map_find() error: key not found in file "
132 << filename << " on line " << line_number);
133 return it->second;
134}

◆ map_find() [2/2]

template<typename Map , typename Key , typename std::enable_if<!is_streamable< Key >::value, Key >::type * = nullptr>
Map::mapped_type & libMesh::Utility::map_find ( Map &  map,
const Key &  key,
const char *  filename,
int  line_number 
)
inline

-Wdangling-reference was nowhere near ready to add to -Wall in gcc 13.

A version of the map_find() utility which can only be used if the map key is printable via std::stream.

It's been moved to -Wextra, but we use that too. :-)

See e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109642

Our map_find functions trigger it. This function should not be called directly (although it can be), instead see the libmesh_map_find() macro.

Calls find(key), and checks the result against end(). Returns the corresponding value if found, throws an error otherwise. Templated on the type of map, so this will work with both std::map and std::unordered_map.

Definition at line 105 of file utility.h.

109{
110 auto it = map.find(key);
111 libmesh_error_msg_if(it == map.end(),
112 "map_find() error: key not found in file "
113 << filename << " on line " << line_number);
114 return it->second;
115}

◆ mkdir()

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

Create a directory.

Definition at line 152 of file utility.C.

152 {
153#if defined(LIBMESH_HAVE_MKDIR)
154 return ::mkdir(pathname, 0755);
155#elif LIBMESH_HAVE_DECL__MKDIR
156 return _mkdir(pathname);
157#else
158 libmesh_error_msg("Function mkdir not available on this system.");
159#endif
160
161}

Referenced by libMesh::RBEvaluation::legacy_write_offline_data_to_files(), libMesh::RBEIMEvaluation::write_out_interior_basis_functions(), libMesh::RBEIMEvaluation::write_out_node_basis_functions(), libMesh::RBEIMEvaluation::write_out_side_basis_functions(), libMesh::RBEvaluation::write_out_vectors(), and libMesh::RBConstruction::write_riesz_representors_to_files().

◆ pow()

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

Definition at line 296 of file utility.h.

297{
298 return do_pow<N,T>::apply(x);
299}
An efficient template instantiation for raising to an arbitrary integer power.
Definition utility.h:255

References libMesh::Utility::do_pow< N, T >::apply(), and pow().

Referenced by libMesh::LIBMESH_DEFAULT_VECTORIZED_FE(), pow(), libMesh::FE< Dim, T >::shape_deriv(), and libMesh::FE< Dim, T >::shape_second_deriv().

◆ 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 133 of file utility.C.

136{
137 const unsigned int len = source.size();
138
139 real_part.resize(len);
140 imag_part.resize(len);
141
142 for (unsigned int i=0; i<len; i++)
143 {
144 real_part[i] = source[i].real();
145 imag_part[i] = source[i].imag();
146 }
147}

◆ print_timestamp()

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

Definition at line 37 of file timestamp.h.

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

References get_timestamp().

◆ string_to_enum() [1/3]

template<typename T >
T libMesh::Utility::string_to_enum ( const char *  s)
Returns
the enumeration of type T which matches the C-style string s.

◆ string_to_enum() [2/3]

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

◆ string_to_enum() [3/3]

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

◆ system_info()

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

Definition at line 63 of file utility.C.

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

References get_timestamp().

◆ unzip_file()

std::string libMesh::Utility::unzip_file ( std::string_view  name)

Create an unzipped copy of a bz2 or xz file, returning the name of the now-unzipped file that can be directly opened.

This is a hack because we don't have a neat bz2/xz equivalent to gzstreams.

Definition at line 164 of file utility.C.

165{
166 std::ostringstream pid_suffix;
167 pid_suffix << '_' << getpid();
168
169 std::string new_name { name };
170 if (Utility::ends_with(name, ".bz2"))
171 {
172#ifdef LIBMESH_HAVE_BZIP
173 new_name.erase(new_name.end() - 4, new_name.end());
174 new_name += pid_suffix.str();
175 LOG_SCOPE("system(bunzip2)", "Utility");
176 std::string system_string = "bunzip2 -f -k -c ";
177 system_string.append(name);
178 system_string += " > " + new_name;
179 if (std::system(system_string.c_str()))
180 libmesh_file_error(system_string);
181#else
182 libmesh_error_msg("ERROR: need bzip2/bunzip2 to open .bz2 file " << name);
183#endif
184 }
185 else if (Utility::ends_with(name, ".xz"))
186 {
187#ifdef LIBMESH_HAVE_XZ
188 new_name.erase(new_name.end() - 3, new_name.end());
189 new_name += pid_suffix.str();
190 LOG_SCOPE("system(xz -d)", "Utility");
191 std::string system_string = "xz -f -d -k -c ";
192 system_string.append(name);
193 system_string += " > " + new_name;
194 if (std::system(system_string.c_str()))
195 libmesh_file_error(system_string);
196#else
197 libmesh_error_msg("ERROR: need xz to open .xz file " << name);
198#endif
199 }
200 return new_name;
201}

References ends_with().

Referenced by libMesh::Xdr::open(), libMesh::STLIO::open_file(), libMesh::AbaqusIO::read(), libMesh::DynaIO::read(), and libMesh::SparseMatrix< T >::read_matlab().

◆ vector_at() [1/2]

template<typename Vector >
Vector::const_reference & libMesh::Utility::vector_at ( const Vector &  vec,
typename Vector::size_type  i,
const char *  filename,
int  line_number 
)
inline

Same as above, but for const inputs.

Definition at line 210 of file utility.h.

214{
215 libmesh_error_msg_if(i >= vec.size(),
216 "vec_at() error: Index " << i <<
217 " past end of vector in file " << filename <<
218 " on line " << line_number);
219 return vec[i];
220}

◆ vector_at() [2/2]

template<typename Vector >
Vector::reference & libMesh::Utility::vector_at ( Vector &  vec,
typename Vector::size_type  i,
const char *  filename,
int  line_number 
)
inline

A replacement for std::vector::at(i) which is meant to be used with a macro, and, unlike at(), gives a proper line number and useful error message when the index is past the end.

Definition at line 192 of file utility.h.

196{
197 libmesh_error_msg_if(i >= vec.size(),
198 "vec_at() error: Index " << i <<
199 " past end of vector in file " << filename <<
200 " on line " << line_number);
201 return vec[i];
202}