www.mooseframework.org
MooseUtils.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 #ifndef MOOSEUTILS_H
15 #define MOOSEUTILS_H
16 
17 // MOOSE includes
18 #include "HashMap.h"
19 #include "MaterialProperty.h" // MaterialProperties
20 #include "InfixIterator.h"
21 
22 // C++ includes
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <list>
27 #include <iterator>
28 
29 // Forward Declarations
30 namespace libMesh
31 {
32 class Elem;
33 namespace Parallel
34 {
35 class Communicator;
36 }
37 }
38 
39 namespace MooseUtils
40 {
41 
43 int levenshteinDist(const std::string & s1, const std::string & s2);
44 
50 void escape(std::string & str);
51 
55 std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
56 
60 bool pathContains(const std::string & expression,
61  const std::string & string_to_find,
62  const std::string & delims = "/");
63 
71 bool checkFileReadable(const std::string & filename,
72  bool check_line_endings = false,
73  bool throw_on_unreadable = true);
74 
81 bool checkFileWriteable(const std::string & filename, bool throw_on_unwritable = true);
82 
87 void parallelBarrierNotify(const libMesh::Parallel::Communicator & comm);
88 
95 void serialBegin(const libMesh::Parallel::Communicator & comm);
96 
101 void serialEnd(const libMesh::Parallel::Communicator & comm);
102 
110 bool hasExtension(const std::string & filename, std::string ext, bool strip_exodus_ext = false);
111 
119 std::pair<std::string, std::string> splitFileName(std::string full_file);
120 
126 std::string camelCaseToUnderscore(const std::string & camel_case_name);
127 
133 std::string underscoreToCamelCase(const std::string & underscore_name, bool leading_upper_case);
134 
138 std::string shortName(const std::string & name);
139 
143 std::string baseName(const std::string & name);
144 
148 template <typename T1, typename T2>
149 bool
150 doesMapContainValue(const std::map<T1, T2> & the_map, const T2 & value)
151 {
152  for (typename std::map<T1, T2>::const_iterator iter = the_map.begin(); iter != the_map.end();
153  ++iter)
154  if (iter->second == value)
155  return true;
156  return false;
157 }
158 
166 bool absoluteFuzzyEqual(const libMesh::Real & var1,
167  const libMesh::Real & var2,
168  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
169 
178 bool absoluteFuzzyGreaterEqual(const libMesh::Real & var1,
179  const libMesh::Real & var2,
180  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
181 
190 bool absoluteFuzzyGreaterThan(const libMesh::Real & var1,
191  const libMesh::Real & var2,
192  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
193 
202 bool absoluteFuzzyLessEqual(const libMesh::Real & var1,
203  const libMesh::Real & var2,
204  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
205 
213 bool absoluteFuzzyLessThan(const libMesh::Real & var1,
214  const libMesh::Real & var2,
215  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
216 
224 bool relativeFuzzyEqual(const libMesh::Real & var1,
225  const libMesh::Real & var2,
226  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
227 
236 bool relativeFuzzyGreaterEqual(const libMesh::Real & var1,
237  const libMesh::Real & var2,
238  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
239 
247 bool relativeFuzzyGreaterThan(const libMesh::Real & var1,
248  const libMesh::Real & var2,
249  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
250 
259 bool relativeFuzzyLessEqual(const libMesh::Real & var1,
260  const libMesh::Real & var2,
261  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
262 
270 bool relativeFuzzyLessThan(const libMesh::Real & var1,
271  const libMesh::Real & var2,
272  const libMesh::Real & tol = libMesh::TOLERANCE * libMesh::TOLERANCE);
273 
284  const HashMap<const libMesh::Elem *, HashMap<unsigned int, MaterialProperties>> & props);
285 
310 void
311 indentMessage(const std::string & prefix, std::string & message, const char * color = COLOR_CYAN);
312 
316 std::string & removeColor(std::string & msg);
317 
324 std::list<std::string> getFilesInDirs(const std::list<std::string> & directory_list);
325 
331 std::string getLatestMeshCheckpointFile(const std::list<std::string> & checkpoint_files);
332 
333 std::string getLatestAppCheckpointFileBase(const std::list<std::string> & checkpoint_files);
334 
335 /*
336  * Checks to see if a string matches a search string
337  * @param name The name to check
338  * @param search_string The search string to check name against
339  */
340 bool wildCardMatch(std::string name, std::string search_string);
341 
348 template <typename T>
349 void
350 tokenize(const std::string & str,
351  std::vector<T> & elements,
352  unsigned int min_len = 1,
353  const std::string & delims = "/")
354 {
355  elements.clear();
356 
357  std::string::size_type last_pos = str.find_first_not_of(delims, 0);
358  std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
359 
360  while (last_pos != std::string::npos)
361  {
362  elements.push_back(str.substr(last_pos, pos - last_pos));
363  // skip delims between tokens
364  last_pos = str.find_first_not_of(delims, pos);
365  if (last_pos == std::string::npos)
366  break;
367  pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
368  }
369 }
370 
375 template <typename T>
376 bool
377 tokenizeAndConvert(const std::string & str,
378  std::vector<T> & tokenized_vector,
379  const std::string & delimiter = " \t\n\v\f\r")
380 {
381  std::vector<std::string> tokens;
382  MooseUtils::tokenize(str, tokens, 1, delimiter);
383  tokenized_vector.resize(tokens.size());
384  for (unsigned int j = 0; j < tokens.size(); ++j)
385  {
386  std::stringstream ss(trim(tokens[j]));
387  // we have to make sure that the conversion succeeded _and_ that the string
388  // was fully read to avoid situations like [conversion to Real] 3.0abc to work
389  if ((ss >> tokenized_vector[j]).fail() || !ss.eof())
390  return false;
391  }
392  return true;
393 }
394 
399 std::string toUpper(const std::string & name);
400 
405 template <typename T>
406 T
407 concatenate(T c1, const T & c2)
408 {
409  c1.insert(c2.begin(), c2.end());
410  return c1;
411 }
412 
416 template <typename T>
417 std::vector<T>
418 concatenate(std::vector<T> c1, const std::vector<T> & c2)
419 {
420  c1.insert(c1.end(), c2.begin(), c2.end());
421  return c1;
422 }
423 
427 template <typename T>
428 std::vector<T>
429 concatenate(std::vector<T> c1, const T & item)
430 {
431  c1.push_back(item);
432  return c1;
433 }
434 
443 template <typename T>
444 int
445 numDigits(const T & num)
446 {
447  return num > 9 ? static_cast<int>(std::log10(static_cast<double>(num))) + 1 : 1;
448 }
449 }
450 
451 #endif // MOOSEUTILS_H
bool relativeFuzzyEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
bool absoluteFuzzyGreaterEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than or equal to another variable within an absolute ...
std::vector< T > concatenate(std::vector< T > c1, const T &item)
Returns the passed in vector with the item appended to it.
Definition: MooseUtils.h:429
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:350
HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts...
Definition: HashMap.h:24
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \t\n\v\f\r")
tokenizeAndConvert splits a string using delimiter and then converts to type T.
Definition: MooseUtils.h:377
void serialBegin(const libMesh::Parallel::Communicator &comm)
This function marks the begin of a section of code that is executed in serial rank by rank...
Definition: MooseUtils.C:197
void MaterialPropertyStorageDump(const HashMap< const libMesh::Elem *, HashMap< unsigned int, MaterialProperties >> &props)
Function to dump the contents of MaterialPropertyStorage for debugging purposes.
Definition: MooseUtils.C:397
bool doesMapContainValue(const std::map< T1, T2 > &the_map, const T2 &value)
This routine is a simple helper function for searching a map by values instead of keys...
Definition: MooseUtils.h:150
void indentMessage(const std::string &prefix, std::string &message, const char *color=COLOR_CYAN)
Indents the supplied message given the prefix and color.
Definition: MooseUtils.C:438
std::list< std::string > getFilesInDirs(const std::list< std::string > &directory_list)
Retrieves the names of all of the files contained within the list of directories passed into the rout...
Definition: MooseUtils.C:466
bool absoluteFuzzyLessThan(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than another variable within an absolute tolerance...
bool relativeFuzzyLessThan(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than another variable within a relative tolerance...
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
std::string camelCaseToUnderscore(const std::string &camel_case_name)
Function for converting a camel case name to a name containing underscores.
Definition: MooseUtils.C:281
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:325
std::string getLatestAppCheckpointFileBase(const std::list< std::string > &checkpoint_files)
Definition: MooseUtils.C:503
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true)
Checks to see if a file is readable (exists and permissions)
Definition: MooseUtils.C:121
std::pair< std::string, std::string > splitFileName(std::string full_file)
Function for splitting path and filename.
Definition: MooseUtils.C:251
bool relativeFuzzyLessEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than or equal to another variable within a relative tole...
std::string underscoreToCamelCase(const std::string &underscore_name, bool leading_upper_case)
Function for converting an underscore name to a camel case name.
Definition: MooseUtils.C:293
bool hasExtension(const std::string &filename, std::string ext, bool strip_exodus_ext=false)
Function tests if the supplied filename as the desired extension.
Definition: MooseUtils.C:227
std::string & removeColor(std::string &msg)
remove ANSI escape sequences for teminal color from msg
Definition: MooseUtils.C:430
std::string getLatestMeshCheckpointFile(const std::list< std::string > &checkpoint_files)
Returns the most recent checkpoint or mesh file given a list of files.
Definition: MooseUtils.C:495
bool checkFileWriteable(const std::string &filename, bool throw_on_unwritable=true)
Check if the file is writable (path exists and permissions)
Definition: MooseUtils.C:151
std::string baseName(const std::string &name)
Function for string the information before the final / in a parser block.
Definition: MooseUtils.C:331
int numDigits(const T &num)
Return the number of digits for a number.
Definition: MooseUtils.h:445
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:511
bool absoluteFuzzyGreaterThan(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than another variable within an absolute tolerance...
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Definition: MooseUtils.C:95
MPI_Comm comm
int levenshteinDist(const std::string &s1, const std::string &s2)
Computes and returns the Levenshtein distance between strings s1 and s2.
Definition: MooseUtils.C:46
bool absoluteFuzzyEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
bool relativeFuzzyGreaterEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than or equal to another variable within a relative t...
bool absoluteFuzzyLessEqual(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than or equal to another variable within an absolute tol...
bool pathContains(const std::string &expression, const std::string &string_to_find, const std::string &delims="/")
This function tokenizes a path and checks to see if it contains the string to look for...
Definition: MooseUtils.C:105
void parallelBarrierNotify(const libMesh::Parallel::Communicator &comm)
This function implements a parallel barrier function but writes progress to stdout.
void escape(std::string &str)
This function will escape all of the standard C++ escape characters so that they can be printed...
Definition: MooseUtils.C:77
void serialEnd(const libMesh::Parallel::Communicator &comm)
Closes a section of code that is executed in serial rank by rank, and that was opened with a call to ...
Definition: MooseUtils.C:211
bool relativeFuzzyGreaterThan(const libMesh::Real &var1, const libMesh::Real &var2, const libMesh::Real &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than another variable within a relative tolerance...