Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 "MooseTypes.h" 14 : 15 : // libMesh 16 : #include "libmesh/enum_order.h" 17 : #include "libmesh/enum_quadrature_type.h" 18 : #include "libmesh/enum_fe_family.h" 19 : #include "libmesh/enum_elem_type.h" 20 : 21 : #include <variant> 22 : 23 : // Forward declarations 24 : class MultiMooseEnum; 25 : namespace libMesh 26 : { 27 : class Point; 28 : } 29 : 30 : namespace Moose 31 : { 32 : // Scalar conversions 33 : template <typename T> 34 : T stringToEnum(const std::string & s); 35 : 36 : template <> 37 : libMesh::QuadratureType stringToEnum<libMesh::QuadratureType>(const std::string & s); 38 : 39 : template <> 40 : libMesh::Order stringToEnum<libMesh::Order>(const std::string & s); 41 : 42 : template <> 43 : CoordinateSystemType stringToEnum<CoordinateSystemType>(const std::string & s); 44 : 45 : template <> 46 : SolveType stringToEnum<SolveType>(const std::string & s); 47 : 48 : template <> 49 : LineSearchType stringToEnum<LineSearchType>(const std::string & s); 50 : 51 : template <> 52 : TimeIntegratorType stringToEnum<TimeIntegratorType>(const std::string & s); 53 : 54 : template <> 55 : RelationshipManagerType stringToEnum<RelationshipManagerType>(const std::string & s); 56 : 57 : // Vector conversions 58 : template <typename T> 59 : std::vector<T> vectorStringsToEnum(const MultiMooseEnum & v); 60 : 61 : /// conversion to string 62 : template <typename T> 63 : std::string 64 2256373 : stringify(const T & t) 65 : { 66 2256373 : std::ostringstream os; 67 2256373 : os << t; 68 4512746 : return os.str(); 69 2256373 : } 70 : 71 : // overload for boolean type instead of simply printing 0 or 1 72 : inline std::string 73 : stringify(bool v) 74 : { 75 : return v ? "true" : "false"; 76 : } 77 : 78 : // overloads for integer types where std::to_string gives the same result and is faster 79 : inline std::string 80 54411 : stringify(int v) 81 : { 82 54411 : return std::to_string(v); 83 : } 84 : inline std::string 85 : stringify(long v) 86 : { 87 : return std::to_string(v); 88 : } 89 : inline std::string 90 : stringify(long long v) 91 : { 92 : return std::to_string(v); 93 : } 94 : inline std::string 95 13570 : stringify(unsigned int v) 96 : { 97 13570 : return std::to_string(v); 98 : } 99 : inline std::string 100 65250 : stringify(unsigned long v) 101 : { 102 65250 : return std::to_string(v); 103 : } 104 : inline std::string 105 : stringify(unsigned long long v) 106 : { 107 : return std::to_string(v); 108 : } 109 : 110 : namespace internal 111 : { 112 : template <typename T, typename V> 113 : inline std::string 114 : stringify_variant(const V & value) 115 : { 116 : return std::holds_alternative<T>(value) ? stringify(std::get<T>(value)) : ""; 117 : } 118 : } 119 : 120 : template <typename... T> 121 : inline std::string 122 : stringify(std::variant<T...> v) 123 : { 124 : return (internal::stringify_variant<T>(v) + ...); 125 : } 126 : 127 : /// Convert solve type into human readable string 128 : std::string stringify(const SolveType & t); 129 : 130 : /// Convert eigen solve type into human readable string 131 : std::string stringify(const EigenSolveType & t); 132 : 133 : /// Convert variable field type into human readable string 134 : std::string stringify(const VarFieldType & t); 135 : 136 : /// Add no-op stringify if the argument already is a string (must use overloading) 137 : std::string stringify(const std::string & s); 138 : 139 : /// Convert FEType from libMesh into string 140 : std::string stringify(libMesh::FEFamily f); 141 : 142 : /// Convert SolutionIterationType into string 143 : std::string stringify(SolutionIterationType t); 144 : 145 : /// Convert ElementType into string 146 : std::string stringify(ElementType t); 147 : 148 : /// Convert the libmesh ElemType into string 149 : std::string stringify(libMesh::ElemType t); 150 : 151 : /// Add pair stringify to support maps 152 : template <typename T, typename U> 153 : std::string 154 278 : stringify(const std::pair<T, U> & p, const std::string & delim = ":") 155 : { 156 278 : return stringify(p.first) + delim + stringify(p.second); 157 : } 158 : 159 : /// Add tuple stringify 160 : template <typename... Args> 161 : std::string 162 0 : stringify(const std::tuple<Args...> & t, const std::string & delim = ":") 163 : { 164 : if constexpr (sizeof...(Args) == 0) 165 : { 166 : return ""; 167 : } 168 : 169 : return std::apply( 170 0 : [&delim](const auto &... args) 171 : { 172 0 : std::size_t n{0}; 173 0 : std::string result; 174 0 : ((result += (n++ == 0 ? "" : delim) + stringify(args)), ...); 175 0 : return result; 176 0 : }, 177 0 : t); 178 : } 179 : 180 : /** 181 : * Convert a container to a string with elements separated by delimiter of user's choice 182 : * 183 : * Optionally, the container elements can be enclosed by curly braces and can 184 : * enclose elements in quotations (or other characters) to make the separation 185 : * of elements more clear. 186 : * 187 : * @param[in] c Container to stringify 188 : * @param[in] delim String to print between elements 189 : * @param[in] elem_encl String to use at the beginning and end of each element, 190 : * typically quotation marks 191 : * @param[in] enclose_list_in_curly_braces Enclose the list string in curly braces? 192 : */ 193 : template <template <typename...> class T, typename... U> 194 : std::string 195 906784 : stringify(const T<U...> & c, 196 : const std::string & delim = ", ", 197 : const std::string & elem_encl = "", 198 : bool enclose_list_in_curly_braces = false) 199 : { 200 906784 : std::string str; 201 906784 : if (enclose_list_in_curly_braces) 202 761 : str += "{"; 203 906784 : const auto begin = c.begin(); 204 906784 : const auto end = c.end(); 205 4131064 : for (auto i = begin; i != end; ++i) 206 5037700 : str += (i != begin ? delim : "") + elem_encl + stringify(*i) + elem_encl; 207 906784 : if (enclose_list_in_curly_braces) 208 761 : str += "}"; 209 1813568 : return str; 210 0 : } 211 : 212 : /** 213 : * Stringify Reals with enough precision to guarantee lossless 214 : * Real -> string -> Real roundtrips. 215 : */ 216 : std::string stringifyExact(Real); 217 : } 218 : 219 : template <typename T1, typename T2> 220 : std::vector<T2> 221 : vectorCast(std::vector<T2> in) 222 : { 223 : std::vector<T2> out(in.begin(), in.end()); 224 : return out; 225 : } 226 : 227 : /** 228 : * Convert point represented as std::vector into Point 229 : * @param pos Point represented as a vector 230 : * @return Converted point 231 : */ 232 : Point toPoint(const std::vector<Real> & pos);