PRISMS-PF Manual
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#pragma once
5
6#include <deal.II/base/exceptions.h>
7#include <deal.II/base/point.h>
8#include <deal.II/base/tensor.h>
9#include <deal.II/base/vectorization.h>
10#include <deal.II/matrix_free/evaluation_flags.h>
11
12#include <boost/range/algorithm.hpp>
13#include <boost/range/algorithm_ext.hpp>
14
15#include <prismspf/config.h>
16
17#include <array>
18#include <string>
19#include <string_view>
20#include <unordered_map>
21
23
27template <typename Type>
28Type
29string_to_type(const std::string &string,
30 const std::unordered_map<std::string, Type> &table)
31{
32 auto iterator = table.find(string);
33 AssertThrow(iterator != table.end(),
34 dealii::ExcMessage("Unknown table entry: " + string));
35 return iterator->second;
36}
37
55template <typename Type, typename OtherType>
56std::pair<OtherType, Type>
58 const std::string &string,
59 const std::unordered_map<std::string, Type> &table,
60 const std::unordered_map<std::string, OtherType> &other_table,
61 const std::pair<char, char> &delimiters = {'(', ')'})
62{
63 // Check to see if the string has any delimiters
64 const auto opening_delimiter_position = string.find(delimiters.first);
65 const auto closing_delimiter_position = string.find(delimiters.second);
66
67 bool has_opening_delimiter = opening_delimiter_position != std::string_view::npos;
68 bool has_closing_delimiter = closing_delimiter_position != std::string_view::npos;
69
70 // Some checks for malformed input
72 dealii::ExcMessage(
73 "Opening delimiter must precede the closing delimiter. You had " +
74 string));
76 dealii::ExcMessage("You must either have a delimiter pair or not. You seem "
77 "to only have one delimiter. You had " +
78 string));
79
80 // Case 2 - no delimiters
82 {
83 const auto second_type = string_to_type("", table);
84 const auto first_type = string_to_type(string, other_table);
85 return {first_type, second_type};
86 }
87
88 // More checks for malformed input
89 AssertThrow(closing_delimiter_position == string.size() - 1,
90 dealii::ExcMessage(
91 "Closing delimiter must be at the end of the string. You had " + string));
92
93 // Case 1 - delimiters
94 std::string_view input_view {string};
95 std::string_view outer_string = input_view.substr(0, opening_delimiter_position);
96 std::string_view inner_string =
99
100 AssertThrow(!outer_string.empty() && !inner_string.empty(),
101 dealii::ExcMessage(
102 "Inner and outer map entries must not be empty. You had " + string));
103 // NOTE: This will allocate the string views to strings on the heap. Hopefully this
104 // isn't so bad in performance, but I didn't wanna support heterogeneous lookup tables.
105 // I can't imagine this becoming a performance problem right now since we only deal with
106 // strings in the initialization of simulations; however, if it does, it shouldn't be
107 // two hard to support heterogeneous lookup or a table that uses std::string_view.
108 const auto second_type = string_to_type(std::string(outer_string), table);
109 const auto first_type = string_to_type(std::string(inner_string), other_table);
110 return {first_type, second_type};
111}
112
117template <typename Real, typename OtherReal>
118inline Real
119pmod(const Real &value, const OtherReal &modulus)
120{
121 using std::fmod;
122 return fmod(fmod(value, modulus) + modulus, modulus);
123}
124
128template <unsigned int dim>
129constexpr unsigned int voigt_tensor_size = (2 * dim) - 1 + (dim / 3);
130
135template <unsigned int dim, typename T>
136inline void
138 const dealii::Tensor<1, voigt_tensor_size<dim>, T> &strain,
139 dealii::Tensor<1, voigt_tensor_size<dim>, T> &stress)
140{
142}
143
148template <unsigned int dim, typename T>
149inline void
151 const dealii::Tensor<2, dim, T> &strain,
152 dealii::Tensor<2, dim, T> &stress)
153{
154 dealii::Tensor<1, voigt_tensor_size<dim>, T> sigma;
155 dealii::Tensor<1, voigt_tensor_size<dim>, T> epsilon;
156
157 if constexpr (dim == 3)
158 {
159 const int xx_dir = 0;
160 const int yy_dir = 1;
161 const int zz_dir = 2;
162 const int yz_dir = 3;
163 const int xz_dir = 4;
164 const int xy_dir = 5;
165
169 // In Voigt notation: epsilonngineering shear strain=zz_dir*strain
173
174 // Multiply elasticity_tensor and epsilon to get sigma
176
180
183
186
189 }
190 else if constexpr (dim == 2)
191 {
192 const int xx_dir = 0;
193 const int yy_dir = 1;
194 const int xy_dir = 2;
195
198 // In Voigt notation: epsilonngineering shear strain=xy_dir*strain
200
201 // Multiply elasticity_tensor and epsilon to get sigma
203
208 }
209 else
210 {
211 const int xx_dir = 0;
212
214 }
215}
216
220inline std::string
221strip_whitespace(const std::string &_text)
222{
223 std::string text = _text;
224 boost::range::remove_erase_if(text, ::isspace);
225 return text;
226}
227
231inline const char *
232bool_to_string(bool boolean)
233{
234 return boolean ? "true" : "false";
235}
236
240inline std::string
241eval_flags_to_string(dealii::EvaluationFlags::EvaluationFlags flag)
242{
243 std::string result;
244
245 if ((flag & dealii::EvaluationFlags::EvaluationFlags::values) != 0U)
246 {
247 result += "values";
248 }
249 if ((flag & dealii::EvaluationFlags::EvaluationFlags::gradients) != 0U)
250 {
251 if (!result.empty())
252 {
253 result += " | ";
254 }
255 result += "gradients";
256 }
257 if ((flag & dealii::EvaluationFlags::EvaluationFlags::hessians) != 0U)
258 {
259 if (!result.empty())
260 {
261 result += " | ";
262 }
263 result += "hessians";
264 }
265
266 if (result.empty())
267 {
268 return "nothing";
269 }
270
271 return result;
272}
273
274template <unsigned int dim, typename number>
275inline std::vector<number>
276dealii_point_to_vector(const dealii::Point<dim, number> &point)
277{
278 static_assert(dim < 4, "We only allow 3 space dimensions");
279
280 std::vector<number> vec(3, 0.0);
281
282 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-constant-array-index)
283
284 for (unsigned int i = 0; i < dim; ++i)
285 {
286 vec[i] = point[i];
287 }
288
289 // NOLINTEND(cppcoreguidelines-pro-bounds-constant-array-index)
290
291 return vec;
292}
293
295
@ Value
Use value of the variable as a criterion for refinement.
Definition grid_refiner_criterion.h:31
Definition conditional_ostreams.cc:20
inline ::dealii::VectorizedArray< Number, width > fmod(const ::dealii::VectorizedArray< Number, width > &numer, const Number denom)
Definition vectorized_operations.h:44
Type
Factory function to create appropriate reader based on input file type not a member of ReadFieldBase ...
Definition read_field_factory.h:30
std::string strip_whitespace(const std::string &_text)
Remove whitespace from strings.
Definition utilities.h:221
std::pair< OtherType, Type > string_to_type_pair_with_delimiters(const std::string &string, const std::unordered_map< std::string, Type > &table, const std::unordered_map< std::string, OtherType > &other_table, const std::pair< char, char > &delimiters={'(', ')'})
Helper function that converts a string to some type pair, given two mappings and a set of delimiters.
Definition utilities.h:57
std::string eval_flags_to_string(dealii::EvaluationFlags::EvaluationFlags flag)
Convert evaluation flags to string.
Definition utilities.h:241
Real pmod(const Real &value, const OtherReal &modulus)
Positive moldulo (remainder) returns the normal remainder. (c++ fmod is defined abnormally for negati...
Definition utilities.h:119
std::vector< number > dealii_point_to_vector(const dealii::Point< dim, number > &point)
Definition utilities.h:276
constexpr unsigned int voigt_tensor_size
Voigt notation index range.
Definition utilities.h:129
void compute_stress(const dealii::Tensor< 2, voigt_tensor_size< dim >, T > &elasticity_tensor, const dealii::Tensor< 1, voigt_tensor_size< dim >, T > &strain, dealii::Tensor< 1, voigt_tensor_size< dim >, T > &stress)
Compute the stress with a given displacement and elasticity tensor. This assumes that the provided pa...
Definition utilities.h:137
PRISMS_PF_BEGIN_NAMESPACE Type string_to_type(const std::string &string, const std::unordered_map< std::string, Type > &table)
Helper function that converts a string to some type, given a mapping.
Definition utilities.h:29
const char * bool_to_string(bool boolean)
Convert bool to string.
Definition utilities.h:232