www.mooseframework.org
MooseUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "HashMap.h"
14 #include "InfixIterator.h"
15 #include "MooseEnumItem.h"
16 #include "MooseError.h"
17 #include "MooseADWrapper.h"
18 #include "Moose.h"
19 #include "ADReal.h"
20 #include "ExecutablePath.h"
21 #include "ConsoleUtils.h"
22 
23 #include "libmesh/compare_types.h"
24 #include "libmesh/bounding_box.h"
25 #include "libmesh/int_range.h"
26 #include "libmesh/tensor_tools.h"
27 #include "metaphysicl/raw_type.h"
28 #include "metaphysicl/metaphysicl_version.h"
29 #include "metaphysicl/dualnumber_decl.h"
30 #include "metaphysicl/dynamic_std_array_wrapper.h"
31 #include "timpi/standard_type.h"
32 
33 // C++ includes
34 #include <string>
35 #include <vector>
36 #include <map>
37 #include <list>
38 #include <filesystem>
39 #include <deque>
40 
41 // Forward Declarations
42 class InputParameters;
43 class ExecFlagEnum;
44 class MaterialProperties;
45 class MaterialBase;
46 
47 namespace libMesh
48 {
49 class Elem;
50 namespace Parallel
51 {
52 class Communicator;
53 }
54 }
55 class MultiMooseEnum;
56 
57 namespace MooseUtils
58 {
59 
60 std::filesystem::path pathjoin(const std::filesystem::path & p);
61 
62 template <typename... Args>
63 std::filesystem::path
64 pathjoin(const std::filesystem::path & p, Args... args)
65 {
66  return p / pathjoin(args...);
67 }
68 
70 bool parsesToReal(const std::string & input);
71 
74 std::string runTestsExecutable();
75 
79 std::string findTestRoot();
80 
82 std::string installedInputsDir(const std::string & app_name,
83  const std::string & dir_name,
84  const std::string & extra_error_msg = "");
85 
87 std::string docsDir(const std::string & app_name);
88 
90 std::string replaceAll(std::string str, const std::string & from, const std::string & to);
91 
95 std::string convertLatestCheckpoint(std::string orig);
96 
98 int levenshteinDist(const std::string & s1, const std::string & s2);
99 
105 void escape(std::string & str);
106 
110 std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
111 
118 std::vector<std::string> split(const std::string & str,
119  const std::string & delimiter,
120  std::size_t max_count = std::numeric_limits<std::size_t>::max());
121 std::vector<std::string> rsplit(const std::string & str,
122  const std::string & delimiter,
123  std::size_t max_count = std::numeric_limits<std::size_t>::max());
124 
128 template <typename T>
129 std::string
130 join(const T & strings, const std::string & delimiter)
131 {
132  std::ostringstream oss;
133  std::copy(
134  strings.begin(), strings.end(), infix_ostream_iterator<std::string>(oss, delimiter.c_str()));
135  return oss.str();
136 }
137 
141 std::size_t fileSize(const std::string & filename);
142 
146 bool pathContains(const std::string & expression,
147  const std::string & string_to_find,
148  const std::string & delims = "/");
149 
159 bool checkFileReadable(const std::string & filename,
160  bool check_line_endings = false,
161  bool throw_on_unreadable = true,
162  bool check_for_git_lfs_pointer = true);
163 
170 bool checkFileWriteable(const std::string & filename, bool throw_on_unwritable = true);
171 
181 bool checkForGitLFSPointer(std::ifstream & file);
182 
187 void parallelBarrierNotify(const libMesh::Parallel::Communicator & comm, bool messaging = true);
188 
197 void serialBegin(const libMesh::Parallel::Communicator & comm, bool warn = true);
198 
205 void serialEnd(const libMesh::Parallel::Communicator & comm, bool warn = true);
206 
214 bool hasExtension(const std::string & filename, std::string ext, bool strip_exodus_ext = false);
215 
220 std::string stripExtension(const std::string & s);
221 
229 template <typename T>
230 std::pair<std::filesystem::path, std::filesystem::path>
231 splitFileName(const T & full_file)
232 {
233  const auto p = std::filesystem::path(std::string(full_file));
234  // Error if path ends with /
235  if (!p.has_filename())
236  mooseError("Invalid full file name: ", p);
237 
238  const auto d = p.parent_path();
239  return {d.empty() ? "." : d, p.filename()};
240 }
241 
247 std::string getCurrentWorkingDir();
248 
257 void makedirs(const std::string & dir_name, bool throw_on_failure = false);
258 
267 void removedirs(const std::string & dir_name, bool throw_on_failure = false);
268 
274 std::string camelCaseToUnderscore(const std::string & camel_case_name);
275 
281 std::string underscoreToCamelCase(const std::string & underscore_name, bool leading_upper_case);
282 
286 std::string shortName(const std::string & name);
287 
291 std::string baseName(const std::string & name);
292 
296 std::string hostname();
297 
301 unsigned short getTermWidth(bool use_environment);
302 
306 std::string prettyCppType(const std::string & cpp_type);
307 
311 template <typename T>
312 std::string
313 prettyCppType(const T * = nullptr)
314 {
315  return prettyCppType(demangle(typeid(T).name()));
316 }
317 
321 template <typename T1, typename T2>
322 bool
323 doesMapContainValue(const std::map<T1, T2> & the_map, const T2 & value)
324 {
325  for (typename std::map<T1, T2>::const_iterator iter = the_map.begin(); iter != the_map.end();
326  ++iter)
327  if (iter->second == value)
328  return true;
329  return false;
330 }
331 
339 template <typename T,
340  typename T2,
341  typename T3 = T,
342  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
343  ScalarTraits<T3>::value,
344  int>::type = 0>
345 bool
346 absoluteFuzzyEqual(const T & var1,
347  const T2 & var2,
348  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
349 {
350  return (std::abs(MetaPhysicL::raw_value(var1) - MetaPhysicL::raw_value(var2)) <=
352 }
353 
362 template <typename T,
363  typename T2,
364  typename T3 = T,
365  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
366  ScalarTraits<T3>::value,
367  int>::type = 0>
368 bool
370  const T2 & var2,
371  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
372 {
373  return (MetaPhysicL::raw_value(var1) >=
375 }
376 
385 template <typename T,
386  typename T2,
387  typename T3 = T,
388  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
389  ScalarTraits<T3>::value,
390  int>::type = 0>
391 bool
393  const T2 & var2,
394  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
395 {
396  return (MetaPhysicL::raw_value(var1) >
398 }
399 
408 template <typename T,
409  typename T2,
410  typename T3 = T,
411  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
412  ScalarTraits<T3>::value,
413  int>::type = 0>
414 bool
415 absoluteFuzzyLessEqual(const T & var1,
416  const T2 & var2,
417  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
418 {
419  return (MetaPhysicL::raw_value(var1) <=
421 }
422 
430 template <typename T,
431  typename T2,
432  typename T3 = T,
433  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
434  ScalarTraits<T3>::value,
435  int>::type = 0>
436 bool
437 absoluteFuzzyLessThan(const T & var1,
438  const T2 & var2,
439  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
440 {
441  return (MetaPhysicL::raw_value(var1) <
443 }
444 
452 template <typename T, typename T2, typename T3 = Real>
453 bool
454 relativeFuzzyEqual(const T & var1,
455  const T2 & var2,
456  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
457 {
458  if constexpr (libMesh::ScalarTraits<T>::value ||
460  {
463  "Mathematical types must be same for arguments to relativelyFuzzEqual");
465  return absoluteFuzzyEqual(
466  var1,
467  var2,
469  else if constexpr (libMesh::TensorTools::TensorTraits<T>::rank == 1)
470  {
471  for (const auto i : make_range(Moose::dim))
472  if (!relativeFuzzyEqual(var1(i), var2(i), tol))
473  return false;
474 
475  return true;
476  }
477  else if constexpr (libMesh::TensorTools::TensorTraits<T>::rank == 2)
478  {
479  for (const auto i : make_range(Moose::dim))
480  for (const auto j : make_range(Moose::dim))
481  if (!relativeFuzzyEqual(var1(i, j), var2(i, j), tol))
482  return false;
483 
484  return true;
485  }
486  }
487  else
488  {
489  // We dare to dream
490  mooseAssert(var1.size() == var2.size(), "These must be the same size");
491  for (const auto i : index_range(var1))
492  if (!relativeFuzzyEqual(var1(i), var2(i), tol))
493  return false;
494 
495  return true;
496  }
497 }
498 
507 template <typename T,
508  typename T2,
509  typename T3 = T,
510  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
511  ScalarTraits<T3>::value,
512  int>::type = 0>
513 bool
515  const T2 & var2,
516  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
517 {
519  var1,
520  var2,
522 }
523 
531 template <typename T,
532  typename T2,
533  typename T3 = T,
534  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
535  ScalarTraits<T3>::value,
536  int>::type = 0>
537 bool
539  const T2 & var2,
540  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
541 {
542  return (absoluteFuzzyGreaterThan(
543  var1,
544  var2,
546 }
547 
556 template <typename T,
557  typename T2,
558  typename T3 = T,
559  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
560  ScalarTraits<T3>::value,
561  int>::type = 0>
562 bool
563 relativeFuzzyLessEqual(const T & var1,
564  const T2 & var2,
565  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
566 {
567  return (absoluteFuzzyLessEqual(
568  var1,
569  var2,
571 }
572 
580 template <typename T,
581  typename T2,
582  typename T3 = T,
583  typename std::enable_if<ScalarTraits<T>::value && ScalarTraits<T2>::value &&
584  ScalarTraits<T3>::value,
585  int>::type = 0>
586 bool
587 relativeFuzzyLessThan(const T & var1,
588  const T2 & var2,
589  const T3 & tol = libMesh::TOLERANCE * libMesh::TOLERANCE)
590 {
591  return (absoluteFuzzyLessThan(
592  var1,
593  var2,
595 }
596 
602 template <typename T>
603 class Has_size
604 {
605  using Yes = char;
606  struct No
607  {
608  char x[2];
609  };
610 
611  template <typename C>
612  static Yes test(decltype(&C::size));
613  template <typename C>
614  static No test(...);
615 
616 public:
617  static constexpr bool value = sizeof(test<T>(0)) == sizeof(Yes);
618 };
619 
626 template <typename T>
627 bool
628 isZero(const T & value, const Real tolerance = TOLERANCE * TOLERANCE * TOLERANCE)
629 {
630  if constexpr (Has_size<T>::value)
631  {
632  for (const auto & element : value)
633  if (!isZero(element, tolerance))
634  return false;
635 
636  return true;
637  }
638  else if constexpr (libMesh::TensorTools::TensorTraits<T>::rank == 0)
640  else if constexpr (libMesh::TensorTools::TensorTraits<T>::rank == 1)
641  {
642  for (const auto i : make_range(Moose::dim))
644  return false;
645 
646  return true;
647  }
648  else if constexpr (libMesh::TensorTools::TensorTraits<T>::rank == 2)
649  {
650  for (const auto i : make_range(Moose::dim))
651  for (const auto j : make_range(Moose::dim))
653  return false;
654 
655  return true;
656  }
657 }
658 
670 
700 void indentMessage(const std::string & prefix,
701  std::string & message,
702  const char * color = COLOR_CYAN,
703  bool dont_indent_first_line = true,
704  const std::string & post_prefix = ": ");
705 
709 std::string & removeColor(std::string & msg);
710 
711 std::list<std::string> listDir(const std::string path, bool files_only = false);
712 
713 bool pathExists(const std::string & path);
714 bool pathIsDirectory(const std::string & path);
715 
723 std::list<std::string> getFilesInDirs(const std::list<std::string> & directory_list,
724  const bool files_only = true);
725 
731 std::string getLatestCheckpointFilePrefix(const std::list<std::string> & checkpoint_files);
732 
733 /*
734  * Checks to see if a string matches a search string
735  * @param name The name to check
736  * @param search_string The search string to check name against
737  */
738 bool wildCardMatch(std::string name, std::string search_string);
739 
740 /*
741  * Checks to see if a candidate string matches a pattern string, permitting glob
742  * wildcards (* and ?) anywhere in the pattern.
743  * @param candidate The name to check
744  * @param pattern The search string to check name candidate
745  */
746 bool globCompare(const std::string & candidate,
747  const std::string & pattern,
748  std::size_t c = 0,
749  std::size_t p = 0);
750 
751 template <typename T>
752 void
753 expandAllMatches(const std::vector<T> & candidates, std::vector<T> & patterns)
754 {
755  std::set<T> expanded;
756  for (const auto & p : patterns)
757  {
758  unsigned int found = 0;
759  for (const auto & c : candidates)
760  if (globCompare(c, p))
761  {
762  expanded.insert(c);
763  found++;
764  }
765  if (!found)
766  throw std::invalid_argument(p);
767  }
768  patterns.assign(expanded.begin(), expanded.end());
769 }
770 
777 template <typename T>
778 void
779 tokenize(const std::string & str,
780  std::vector<T> & elements,
781  unsigned int min_len = 1,
782  const std::string & delims = "/")
783 {
784  elements.clear();
785 
786  std::string::size_type last_pos = str.find_first_not_of(delims, 0);
787  std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
788 
789  while (last_pos != std::string::npos)
790  {
791  elements.push_back(str.substr(last_pos, pos - last_pos));
792  // skip delims between tokens
793  last_pos = str.find_first_not_of(delims, pos);
794  if (last_pos == std::string::npos)
795  break;
796  pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
797  }
798 }
799 
804 template <typename T>
805 bool
806 tokenizeAndConvert(const std::string & str,
807  std::vector<T> & tokenized_vector,
808  const std::string & delimiter = " \t\n\v\f\r")
809 {
810  std::vector<std::string> tokens;
811  MooseUtils::tokenize(str, tokens, 1, delimiter);
812  tokenized_vector.resize(tokens.size());
813  for (unsigned int j = 0; j < tokens.size(); ++j)
814  {
815  std::stringstream ss(trim(tokens[j]));
816  // we have to make sure that the conversion succeeded _and_ that the string
817  // was fully read to avoid situations like [conversion to Real] 3.0abc to work
818  if ((ss >> tokenized_vector[j]).fail() || !ss.eof())
819  return false;
820  }
821  return true;
822 }
823 
829 template <typename T>
830 T
831 convert(const std::string & str, bool throw_on_failure = false)
832 {
833  std::stringstream ss(str);
834  T val;
835  if ((ss >> val).fail() || !ss.eof())
836  {
837  std::string msg =
838  std::string("Unable to convert '") + str + "' to type " + demangle(typeid(T).name());
839 
840  if (throw_on_failure)
841  throw std::invalid_argument(msg);
842  else
843  mooseError(msg);
844  }
845 
846  return val;
847 }
848 
849 template <>
850 short int convert<short int>(const std::string & str, bool throw_on_failure);
851 
852 template <>
853 unsigned short int convert<unsigned short int>(const std::string & str, bool throw_on_failure);
854 
855 template <>
856 int convert<int>(const std::string & str, bool throw_on_failure);
857 
858 template <>
859 unsigned int convert<unsigned int>(const std::string & str, bool throw_on_failure);
860 
861 template <>
862 long int convert<long int>(const std::string & str, bool throw_on_failure);
863 
864 template <>
865 unsigned long int convert<unsigned long int>(const std::string & str, bool throw_on_failure);
866 
867 template <>
868 long long int convert<long long int>(const std::string & str, bool throw_on_failure);
869 
870 template <>
871 unsigned long long int convert<unsigned long long int>(const std::string & str,
872  bool throw_on_failure);
873 
877 void createSymlink(const std::string & target, const std::string & link);
878 
882 void clearSymlink(const std::string & link);
883 
888 std::string toUpper(const std::string & name);
889 
894 std::string toLower(const std::string & name);
895 
900 template <typename T>
901 T
902 concatenate(T c1, const T & c2)
903 {
904  c1.insert(c2.begin(), c2.end());
905  return c1;
906 }
907 
911 template <typename T>
912 std::vector<T>
913 concatenate(std::vector<T> c1, const std::vector<T> & c2)
914 {
915  c1.insert(c1.end(), c2.begin(), c2.end());
916  return c1;
917 }
918 
922 template <typename T>
923 std::vector<T>
924 concatenate(std::vector<T> c1, const T & item)
925 {
926  c1.push_back(item);
927  return c1;
928 }
929 
938 template <typename T>
939 int
940 numDigits(const T & num)
941 {
942  return num > 9 ? static_cast<int>(std::log10(static_cast<double>(num))) + 1 : 1;
943 }
944 
949 
955 int stringToInteger(const std::string & input, bool throw_on_failure = false);
956 
968 void linearPartitionItems(dof_id_type num_items,
969  dof_id_type num_chunks,
970  dof_id_type chunk_id,
971  dof_id_type & num_local_items,
972  dof_id_type & local_items_begin,
973  dof_id_type & local_items_end);
974 
984 linearPartitionChunk(dof_id_type num_items, dof_id_type num_chunks, dof_id_type item_id);
985 
989 std::string realpath(const std::string & path);
990 
996 template <typename T>
998 {
999  static constexpr bool value = false;
1000 };
1001 template <>
1003 {
1004  static constexpr bool value = true;
1005 };
1006 template <>
1008 {
1009  static constexpr bool value = true;
1010 };
1011 
1015 template <typename T>
1017 {
1018  static constexpr bool value = std::is_base_of<TIMPI::DataType, TIMPI::StandardType<T>>::value ||
1019  TIMPI::Has_buffer_type<TIMPI::Packing<T>>::value;
1020 };
1021 
1023 const static struct AnyType
1024 {
1025 } Any;
1026 
1027 template <typename T1, typename T2>
1028 bool
1029 wildcardEqual(const T1 & a, const T2 & b)
1030 {
1031  return a == b;
1032 }
1033 
1034 template <typename T>
1035 bool
1037 {
1038  return true;
1039 }
1040 template <typename T>
1041 bool
1043 {
1044  return true;
1045 }
1047 
1051 template <typename C, typename M1, typename M2>
1052 typename C::iterator
1053 findPair(C & container, const M1 & first, const M2 & second)
1054 {
1055  return std::find_if(container.begin(),
1056  container.end(),
1057  [&](auto & item) {
1058  return wildcardEqual(first, item.first) &&
1059  wildcardEqual(second, item.second);
1060  });
1061 }
1062 
1078 BoundingBox buildBoundingBox(const Point & p1, const Point & p2);
1079 
1087 template <typename T, std::size_t N, bool value_init = true>
1088 class SemidynamicVector : public MetaPhysicL::DynamicStdArrayWrapper<T, MetaPhysicL::NWrapper<N>>
1089 {
1090  typedef MetaPhysicL::DynamicStdArrayWrapper<T, MetaPhysicL::NWrapper<N>> Parent;
1091 
1092 public:
1093  SemidynamicVector(std::size_t size) : Parent()
1094  {
1095  Parent::resize(size);
1096  if constexpr (value_init)
1097  for (const auto i : make_range(size))
1098  _data[i] = T{};
1099  }
1100 
1101  void resize(std::size_t new_size)
1102  {
1103  [[maybe_unused]] const auto old_dynamic_n = Parent::size();
1104 
1105  Parent::resize(new_size);
1106 
1107  if constexpr (value_init)
1108  for (const auto i : make_range(old_dynamic_n, _dynamic_n))
1109  _data[i] = T{};
1110  }
1111 
1112  void push_back(const T & v)
1113  {
1114  const auto old_dynamic_n = Parent::size();
1115  Parent::resize(old_dynamic_n + 1);
1116  _data[old_dynamic_n] = v;
1117  }
1118 
1119  template <typename... Args>
1120  void emplace_back(Args &&... args)
1121  {
1122  const auto old_dynamic_n = Parent::size();
1123  Parent::resize(old_dynamic_n + 1);
1124  (::new (&_data[old_dynamic_n]) T(std::forward<Args>(args)...));
1125  }
1126 
1127  std::size_t max_size() const { return N; }
1128 
1129  using Parent::_data;
1130  using Parent::_dynamic_n;
1131 };
1132 
1145 template <typename T>
1146 T *
1147 get(const std::unique_ptr<T> & u)
1148 {
1149  return u.get();
1150 }
1151 
1152 template <typename T>
1153 T *
1154 get(T * p)
1155 {
1156  return p;
1157 }
1158 
1159 template <typename T>
1160 T *
1161 get(const std::shared_ptr<T> & s)
1162 {
1163  return s.get();
1164 }
1165 
1170 template <class InputIterator>
1171 bool
1172 setsIntersect(InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2)
1173 {
1174  while (first1 != last1 && first2 != last2)
1175  {
1176  if (*first1 == *first2)
1177  return true;
1178 
1179  if (*first1 < *first2)
1180  ++first1;
1181  else if (*first1 > *first2)
1182  ++first2;
1183  }
1184  return false;
1185 }
1186 
1187 template <class T>
1188 bool
1189 setsIntersect(const T & s1, const T & s2)
1190 {
1191  return setsIntersect(s1.begin(), s1.end(), s2.begin(), s2.end());
1192 }
1193 
1199 inline bool
1200 isDigits(const std::string & str)
1201 {
1202  return std::all_of(str.begin(), str.end(), [](unsigned char c) { return std::isdigit(c); });
1203 }
1204 } // MooseUtils namespace
1205 
1209 void removeSubstring(std::string & main, const std::string & sub);
1210 
1214 std::string removeSubstring(const std::string & main, const std::string & sub);
std::string name(const ElemQuality q)
bool parsesToReal(const std::string &input)
Check if the input string can be parsed into a Real.
Definition: MooseUtils.C:88
std::string docsDir(const std::string &app_name)
Returns the directory of any installed docs/site.
Definition: MooseUtils.C:120
bool globCompare(const std::string &candidate, const std::string &pattern, std::size_t c=0, std::size_t p=0)
Definition: MooseUtils.C:918
T convert(const std::string &str, bool throw_on_failure=false)
convert takes a string representation of a number type and converts it to the number.
Definition: MooseUtils.h:831
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
Taken from https://stackoverflow.com/a/257382 Evaluating constexpr (Has_size<T>::value) in a template...
Definition: MooseUtils.h:603
void serialEnd(const libMesh::Parallel::Communicator &comm, bool warn=true)
Closes a section of code that is executed in serial rank by rank, and that was opened with a call to ...
Definition: MooseUtils.C:373
std::string toLower(const std::string &name)
Convert supplied string to lower case.
Definition: MooseUtils.C:1048
static Yes test(decltype(&C::size))
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:779
bool absoluteFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
Definition: MooseUtils.h:346
HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts...
Definition: HashMap.h:18
SemidynamicVector(std::size_t size)
Definition: MooseUtils.h:1093
int stringToInteger(const std::string &input, bool throw_on_failure=false)
Robust string to integer conversion that fails for cases such at "1foo".
Definition: MooseUtils.C:1062
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:1040
bool relativeFuzzyGreaterThan(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than another variable within a relative tolerance...
Definition: MooseUtils.h:538
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
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:674
static constexpr Real TOLERANCE
std::string installedInputsDir(const std::string &app_name, const std::string &dir_name, const std::string &extra_error_msg="")
Returns the directory of any installed inputs or the empty string if none are found.
Definition: MooseUtils.C:98
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \\\)
tokenizeAndConvert splits a string using delimiter and then converts to type T.
Definition: MooseUtils.h:806
bool isZero(const T &value, const Real tolerance=TOLERANCE *TOLERANCE *TOLERANCE)
Definition: MooseUtils.h:628
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:323
DualNumber< Real, DNDerivativeType, true > DualReal
Definition: DualReal.h:49
void push_back(const T &v)
Definition: MooseUtils.h:1112
std::size_t max_size() const
Definition: MooseUtils.h:1127
C::iterator findPair(C &container, const M1 &first, const M2 &second)
Find a specific pair in a container matching on first, second or both pair components.
Definition: MooseUtils.h:1053
bool pathIsDirectory(const std::string &path)
Definition: MooseUtils.C:247
bool setsIntersect(InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2)
This method detects whether two sets intersect without building a result set.
Definition: MooseUtils.h:1172
std::pair< std::filesystem::path, std::filesystem::path > splitFileName(const T &full_file)
Function for splitting path and filename.
Definition: MooseUtils.h:231
auto raw_value(const Eigen::Map< T > &in)
Definition: ADReal.h:73
static constexpr bool value
Definition: MooseUtils.h:1018
std::list< std::string > getFilesInDirs(const std::list< std::string > &directory_list, const bool files_only=true)
Retrieves the names of all of the files contained within the list of directories passed into the rout...
Definition: MooseUtils.C:794
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:148
std::string stripExtension(const std::string &s)
Removes any file extension from the given string s (i.e.
Definition: MooseUtils.C:413
void removedirs(const std::string &dir_name, bool throw_on_failure=false)
Recursively remove directories from inner-most when the directories are empty.
Definition: MooseUtils.C:491
void main(int argc, char *argv[])
Initialize, create and run a MooseApp.
Definition: MooseMain.h:37
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
void linearPartitionItems(dof_id_type num_items, dof_id_type num_chunks, dof_id_type chunk_id, dof_id_type &num_local_items, dof_id_type &local_items_begin, dof_id_type &local_items_end)
Linearly partition a number of items.
Definition: MooseUtils.C:1068
bool checkForGitLFSPointer(std::ifstream &file)
Check if the file is a Git-LFS pointer.
Definition: MooseUtils.C:292
bool relativeFuzzyLessEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than or equal to another variable within a relative tole...
Definition: MooseUtils.h:563
std::string realpath(const std::string &path)
Wrapper around PetscGetRealPath, which is a cross-platform replacement for realpath.
Definition: MooseUtils.C:1220
T concatenate(T c1, const T &c2)
Returns a container that contains the content of second passed in container inserted into the first p...
Definition: MooseUtils.h:902
bool relativeFuzzyGreaterEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than or equal to another variable within a relative t...
Definition: MooseUtils.h:514
std::string convertLatestCheckpoint(std::string orig)
Replaces "LATEST" placeholders with the latest checkpoint file name.
Definition: MooseUtils.C:146
std::string hostname()
Get the hostname the current process is running on.
Definition: MooseUtils.C:610
auto max(const L &left, const R &right)
std::string camelCaseToUnderscore(const std::string &camel_case_name)
Function for converting a camel case name to a name containing underscores.
Definition: MooseUtils.C:554
std::string shortName(const std::string &name)
Function for stripping name after the file / in parser block.
Definition: MooseUtils.C:598
std::vector< std::string > split(const std::string &str, const std::string &delimiter, std::size_t max_count=std::numeric_limits< std::size_t >::max())
Python like split functions for strings.
Definition: MooseUtils.C:1115
Custom type trait that has a value of true for types that can be broadcasted.
Definition: MooseUtils.h:1016
ExecFlagEnum getDefaultExecFlagEnum()
Return the default ExecFlagEnum for MOOSE.
Definition: MooseUtils.C:1056
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
std::vector< std::string > rsplit(const std::string &str, const std::string &delimiter, std::size_t max_count=std::numeric_limits< std::size_t >::max())
Definition: MooseUtils.C:1135
uint8_t processor_id_type
Custom type trait that has a value of true for types that cam be use interchangably with Real...
Definition: MooseUtils.h:997
void resize(std::size_t new_size)
Definition: MooseUtils.h:1101
int convert< int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:999
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
void indentMessage(const std::string &prefix, std::string &message, const char *color=COLOR_CYAN, bool dont_indent_first_line=true, const std::string &post_prefix=": ")
Indents the supplied message given the prefix and color.
Definition: MooseUtils.C:724
void parallelBarrierNotify(const libMesh::Parallel::Communicator &comm, bool messaging=true)
This function implements a parallel barrier function but writes progress to stdout.
std::string getLatestCheckpointFilePrefix(const std::list< std::string > &checkpoint_files)
Returns the most recent checkpoint prefix (the four numbers at the begining) If a suitable file isn&#39;t...
Definition: MooseUtils.C:805
void serialBegin(const libMesh::Parallel::Communicator &comm, bool warn=true)
This function marks the begin of a section of code that is executed in serial rank by rank...
Definition: MooseUtils.C:359
bool checkFileReadable(const std::string &filename, bool check_line_endings=false, bool throw_on_unreadable=true, bool check_for_git_lfs_pointer=true)
Checks to see if a file is readable (exists and permissions)
Definition: MooseUtils.C:256
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:214
std::string runTestsExecutable()
Returns the location of either a local repo run_tests script - or an installed test executor script i...
Definition: MooseUtils.C:64
bool absoluteFuzzyLessThan(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than another variable within an absolute tolerance...
Definition: MooseUtils.h:437
bool wildcardEqual(const T1 &a, const T2 &b)
Definition: MooseUtils.h:1029
void expandAllMatches(const std::vector< T > &candidates, std::vector< T > &patterns)
Definition: MooseUtils.h:753
std::size_t fileSize(const std::string &filename)
Check the file size.
Definition: MooseUtils.C:1190
unsigned int convert< unsigned int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:1006
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:566
MetaPhysicL::DynamicStdArrayWrapper< T, MetaPhysicL::NWrapper< N > > Parent
Definition: MooseUtils.h:1090
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:389
std::string & removeColor(std::string &msg)
remove ANSI escape sequences for terminal color from msg
Definition: MooseUtils.C:708
std::string demangle(const char *name)
bool checkFileWriteable(const std::string &filename, bool throw_on_unwritable=true)
Check if the file is writable (path exists and permissions)
Definition: MooseUtils.C:309
charT const * delimiter
Definition: InfixIterator.h:34
static constexpr bool value
Definition: MooseUtils.h:617
void clearSymlink(const std::string &link)
Remove a symbolic link, if the given filename is a link.
Definition: MooseUtils.C:1168
long long int convert< long long int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:1027
processor_id_type linearPartitionChunk(dof_id_type num_items, dof_id_type num_chunks, dof_id_type item_id)
Return the chunk_id that is assigned to handle item_id.
Definition: MooseUtils.C:1094
void createSymlink(const std::string &target, const std::string &link)
Create a symbolic link, if the link already exists it is replaced.
Definition: MooseUtils.C:1155
short int convert< short int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:985
std::filesystem::path pathjoin(const std::filesystem::path &p)
Definition: MooseUtils.C:58
void removeSubstring(std::string &main, const std::string &sub)
find, erase, length algorithm for removing a substring from a string
Definition: MooseUtils.C:1256
unsigned long int convert< unsigned long int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:1020
std::string baseName(const std::string &name)
Function for string the information before the final / in a parser block.
Definition: MooseUtils.C:604
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
int numDigits(const T &num)
Return the number of digits for a number.
Definition: MooseUtils.h:940
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:874
bool relativeFuzzyEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
Definition: MooseUtils.h:454
Comparison helpers that support the MooseUtils::Any wildcard which will match any value...
Definition: MooseUtils.h:1023
bool absoluteFuzzyLessEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than or equal to another variable within an absolute tol...
Definition: MooseUtils.h:415
IntRange< T > make_range(T beg, T end)
std::list< std::string > listDir(const std::string path, bool files_only=false)
Definition: MooseUtils.C:768
void emplace_back(Args &&... args)
Definition: MooseUtils.h:1120
BoundingBox buildBoundingBox(const Point &p1, const Point &p2)
Construct a valid bounding box from 2 arbitrary points.
Definition: MooseUtils.C:1226
bool absoluteFuzzyGreaterEqual(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than or equal to another variable within an absolute ...
Definition: MooseUtils.h:369
int levenshteinDist(const std::string &s1, const std::string &s2)
Computes and returns the Levenshtein distance between strings s1 and s2.
Definition: MooseUtils.C:165
std::string getCurrentWorkingDir()
Returns the current working directory as a string.
Definition: MooseUtils.C:422
void makedirs(const std::string &dir_name, bool throw_on_failure=false)
Recursively make directories.
Definition: MooseUtils.C:435
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
static constexpr bool value
Definition: MooseUtils.h:999
static const struct MooseUtils::AnyType Any
long int convert< long int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:1013
unsigned short int convert< unsigned short int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:992
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:224
bool isDigits(const std::string &str)
Courtesy https://stackoverflow.com/a/8889045 and https://en.cppreference.com/w/cpp/string/byte/isdigi...
Definition: MooseUtils.h:1200
bool pathExists(const std::string &path)
Definition: MooseUtils.C:240
std::string replaceAll(std::string str, const std::string &from, const std::string &to)
Replaces all occurences of from in str with to and returns the result.
Definition: MooseUtils.C:134
bool relativeFuzzyLessThan(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is less than another variable within a relative tolerance...
Definition: MooseUtils.h:587
auto min(const L &left, const R &right)
unsigned long long int convert< unsigned long long int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:1034
MaterialBases compute MaterialProperties.
Definition: MaterialBase.h:60
auto index_range(const T &sizable)
std::string join(const T &strings, const std::string &delimiter)
Python like join function for strings.
Definition: MooseUtils.h:130
bool absoluteFuzzyGreaterThan(const T &var1, const T2 &var2, const T3 &tol=libMesh::TOLERANCE *libMesh::TOLERANCE)
Function to check whether a variable is greater than another variable within an absolute tolerance...
Definition: MooseUtils.h:392
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:196
std::string prettyCppType(const std::string &cpp_type)
Definition: MooseUtils.C:1235
uint8_t dof_id_type
std::string findTestRoot()
Searches in the current working directory and then recursively up in each parent directory looking fo...
Definition: MooseUtils.C:74
unsigned short getTermWidth(bool use_environment)
Returns the width of the terminal using sys/ioctl.
Definition: MooseUtils.C:626