https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Conversion.h
Go to the documentation of this file.
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
24class MultiMooseEnum;
25namespace libMesh
26{
27class Point;
28class FEType;
29}
30
31namespace Moose
32{
33// Scalar conversions
34template <typename T>
35T stringToEnum(const std::string & s);
36
37template <>
39
40template <>
42
43template <>
45
46template <>
47SolveType stringToEnum<SolveType>(const std::string & s);
48
49template <>
50LineSearchType stringToEnum<LineSearchType>(const std::string & s);
51
52template <>
54
55template <>
57
58// Vector conversions
59template <typename T>
60std::vector<T> vectorStringsToEnum(const MultiMooseEnum & v);
61
63template <typename T>
64std::string
65stringify(const T & t)
66{
67 std::ostringstream os;
68 os << t;
69 return os.str();
70}
71
72// overload for boolean type instead of simply printing 0 or 1
73inline std::string
74stringify(bool v)
75{
76 return v ? "true" : "false";
77}
78
79// overloads for integer types where std::to_string gives the same result and is faster
80inline std::string
82{
83 return std::to_string(v);
84}
85inline std::string
86stringify(long v)
87{
88 return std::to_string(v);
89}
90inline std::string
91stringify(long long v)
92{
93 return std::to_string(v);
94}
95inline std::string
96stringify(unsigned int v)
97{
98 return std::to_string(v);
99}
100inline std::string
101stringify(unsigned long v)
102{
103 return std::to_string(v);
104}
105inline std::string
106stringify(unsigned long long v)
107{
108 return std::to_string(v);
109}
110
111namespace internal
112{
113template <typename T, typename V>
114inline std::string
115stringify_variant(const V & value)
116{
117 return std::holds_alternative<T>(value) ? stringify(std::get<T>(value)) : "";
118}
119}
120
121template <typename... T>
122inline std::string
123stringify(std::variant<T...> v)
124{
125 return (internal::stringify_variant<T>(v) + ...);
126}
127
129std::string stringify(const SolveType & t);
130
132std::string stringify(const EigenSolveType & t);
133
135std::string stringify(const VarFieldType & t);
136
138std::string stringify(const std::string & s);
139
141std::string stringify(libMesh::FEFamily f);
142
145std::string stringify(const libMesh::FEType & t);
146
148std::string stringify(SolutionIterationType t);
149
151std::string stringify(ElementType t);
152
154std::string stringify(libMesh::ElemType t);
155
157template <typename T, typename U>
158std::string
159stringify(const std::pair<T, U> & p, const std::string & delim = ":")
160{
161 return stringify(p.first) + delim + stringify(p.second);
162}
163
165template <typename... Args>
166std::string
167stringify(const std::tuple<Args...> & t, const std::string & delim = ":")
168{
169 if constexpr (sizeof...(Args) == 0)
170 {
171 return "";
172 }
173
174 return std::apply(
175 [&delim](const auto &... args)
176 {
177 std::size_t n{0};
178 std::string result;
179 ((result += (n++ == 0 ? "" : delim) + stringify(args)), ...);
180 return result;
181 },
182 t);
183}
184
198template <template <typename...> class T, typename... U>
199std::string
200stringify(const T<U...> & c,
201 const std::string & delim = ", ",
202 const std::string & elem_encl = "",
203 bool enclose_list_in_curly_braces = false)
204{
205 std::string str;
206 if (enclose_list_in_curly_braces)
207 str += "{";
208 const auto begin = c.begin();
209 const auto end = c.end();
210 for (auto i = begin; i != end; ++i)
211 str += (i != begin ? delim : "") + elem_encl + stringify(*i) + elem_encl;
212 if (enclose_list_in_curly_braces)
213 str += "}";
214 return str;
215}
216
221std::string stringifyExact(Real);
222}
223
224template <typename T1, typename T2>
225std::vector<T2>
226vectorCast(std::vector<T2> in)
227{
228 std::vector<T2> out(in.begin(), in.end());
229 return out;
230}
231
237Point toPoint(const std::vector<Real> & pos);
Point toPoint(const std::vector< Real > &pos)
Convert point represented as std::vector into Point.
std::vector< T2 > vectorCast(std::vector< T2 > in)
Definition Conversion.h:226
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
std::string stringify_variant(const V &value)
Definition Conversion.h:115
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
SolveType stringToEnum< SolveType >(const std::string &s)
Definition Conversion.C:202
LineSearchType stringToEnum< LineSearchType >(const std::string &s)
Definition Conversion.C:262
std::vector< T > vectorStringsToEnum(const MultiMooseEnum &v)
T stringToEnum(const std::string &s)
SolveType
Type of the solve.
Definition MooseTypes.h:897
libMesh::Order stringToEnum< libMesh::Order >(const std::string &s)
TimeIntegratorType
Time integrators.
Definition MooseTypes.h:957
RelationshipManagerType stringToEnum< RelationshipManagerType >(const std::string &s)
Definition Conversion.C:307
LineSearchType
Type of the line search.
Definition MooseTypes.h:980
TimeIntegratorType stringToEnum< TimeIntegratorType >(const std::string &s)
Definition Conversion.C:277
CoordinateSystemType stringToEnum< CoordinateSystemType >(const std::string &s)
Definition Conversion.C:187
libMesh::QuadratureType stringToEnum< libMesh::QuadratureType >(const std::string &s)
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:65
RelationshipManagerType
Main types of Relationship Managers.
CoordinateSystemType
Definition MooseTypes.h:864
SolutionIterationType
Definition MooseTypes.h:270
std::string stringifyExact(Real)
Stringify Reals with enough precision to guarantee lossless Real -> string -> Real roundtrips.
Definition Conversion.C:504
EigenSolveType
Type of the eigen solve.
Definition MooseTypes.h:909
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...