Loading [MathJax]/extensions/tex2jax.js
https://mooseframework.inl.gov
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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
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 <>
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 <>
53 
54 template <>
56 
57 // Vector conversions
58 template <typename T>
59 std::vector<T> vectorStringsToEnum(const MultiMooseEnum & v);
60 
62 template <typename T>
63 std::string
64 stringify(const T & t)
65 {
66  std::ostringstream os;
67  os << t;
68  return os.str();
69 }
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 stringify(int v)
81 {
82  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 stringify(unsigned int v)
96 {
97  return std::to_string(v);
98 }
99 inline std::string
100 stringify(unsigned long v)
101 {
102  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 
128 std::string stringify(const SolveType & t);
129 
131 std::string stringify(const EigenSolveType & t);
132 
134 std::string stringify(const VarFieldType & t);
135 
137 std::string stringify(const std::string & s);
138 
140 std::string stringify(libMesh::FEFamily f);
141 
143 std::string stringify(SolutionIterationType t);
144 
146 std::string stringify(ElementType t);
147 
149 std::string stringify(libMesh::ElemType t);
150 
152 template <typename T, typename U>
153 std::string
154 stringify(const std::pair<T, U> & p, const std::string & delim = ":")
155 {
156  return stringify(p.first) + delim + stringify(p.second);
157 }
158 
172 template <template <typename...> class T, typename... U>
173 std::string
174 stringify(const T<U...> & c,
175  const std::string & delim = ", ",
176  const std::string & elem_encl = "",
177  bool enclose_list_in_curly_braces = false)
178 {
179  std::string str;
180  if (enclose_list_in_curly_braces)
181  str += "{";
182  const auto begin = c.begin();
183  const auto end = c.end();
184  for (auto i = begin; i != end; ++i)
185  str += (i != begin ? delim : "") + elem_encl + stringify(*i) + elem_encl;
186  if (enclose_list_in_curly_braces)
187  str += "}";
188  return str;
189 }
190 
195 std::string stringifyExact(Real);
196 }
197 
198 template <typename T1, typename T2>
199 std::vector<T2>
200 vectorCast(std::vector<T2> in)
201 {
202  std::vector<T2> out(in.begin(), in.end());
203  return out;
204 }
205 
211 Point toPoint(const std::vector<Real> & pos);
VarFieldType
Definition: MooseTypes.h:721
std::string stringify_variant(const V &value)
Definition: Conversion.h:114
EigenSolveType
Type of the eigen solve.
Definition: MooseTypes.h:854
RelationshipManagerType
Main types of Relationship Managers.
Definition: MooseTypes.h:963
T stringToEnum(const std::string &s)
CoordinateSystemType stringToEnum< CoordinateSystemType >(const std::string &s)
Definition: Conversion.C:188
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
ElementType
Definition: MooseTypes.h:763
std::basic_ostream< charT, traits > * os
Definition: InfixIterator.h:33
TimeIntegratorType stringToEnum< TimeIntegratorType >(const std::string &s)
Definition: Conversion.C:278
std::string stringifyExact(Real)
Stringify Reals with enough precision to guarantee lossless Real -> string -> Real roundtrips...
Definition: Conversion.C:466
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
LineSearchType
Type of the line search.
Definition: MooseTypes.h:925
Point toPoint(const std::vector< Real > &pos)
Convert point represented as std::vector into Point.
Definition: Conversion.C:478
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
LineSearchType stringToEnum< LineSearchType >(const std::string &s)
Definition: Conversion.C:263
SolveType stringToEnum< SolveType >(const std::string &s)
Definition: Conversion.C:203
std::vector< T2 > vectorCast(std::vector< T2 > in)
Definition: Conversion.h:200
std::vector< T > vectorStringsToEnum(const MultiMooseEnum &v)
CoordinateSystemType
Definition: MooseTypes.h:809
OStreamProxy out
TimeIntegratorType
Time integrators.
Definition: MooseTypes.h:902
RelationshipManagerType stringToEnum< RelationshipManagerType >(const std::string &s)
Definition: Conversion.C:308
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
SolveType
Type of the solve.
Definition: MooseTypes.h:842
SolutionIterationType
Definition: MooseTypes.h:241