14#include <range/v3/range/conversion.hpp>
15#include <range/v3/view/transform.hpp>
28 exprtk::symbol_table<T>& symbol_table,
29 std::vector<std::string>
const& string_expressions)
31 exprtk::parser<T> parser;
33 std::vector<exprtk::expression<T>> expressions(string_expressions.size());
34 for (
unsigned i = 0; i < string_expressions.size(); ++i)
36 expressions[i].register_symbol_table(symbol_table);
37 if (!parser.compile(string_expressions[i], expressions[i]))
39 OGS_FATAL(
"Error: {:s}\tExpression: {:s}\n",
41 string_expressions[i]);
48class Function::Implementation
55 std::vector<std::string>
const& variables,
56 std::vector<std::string>
const& value_string_expressions,
57 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
58 dvalue_string_expressions);
64 std::vector<std::string>
const& variables);
73 std::vector<std::pair<Variable, std::vector<Expression>>>
83 std::vector<std::string>
const& variables)
85 exprtk::symbol_table<double> symbol_table;
87 for (
auto const& v : variables)
89 auto add_scalar = [&v, &symbol_table](
double& value)
90 { symbol_table.add_variable(v, value); };
93 [&v, &symbol_table](
double* ptr, std::size_t
const size)
94 { symbol_table.add_vector(v, ptr, size); };
98 { add_scalar(*address); },
101 auto constexpr size =
104 address->template emplace<Eigen::Matrix<double, size, 1>>();
105 add_vector(result.data(), size);
111 address->template emplace<Eigen::Matrix<double, size, 1>>();
112 add_vector(result.data(), size);
116 variable_array.visitVariable(add_any_variable, variable);
123 std::vector<std::string>
const& variables,
124 std::vector<std::string>
const& value_string_expressions,
125 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
126 dvalue_string_expressions)
128 auto symbol_table = createSymbolTable(variables);
135 for (
auto const& [variable_name, string_expressions] :
136 dvalue_string_expressions)
138 dvalue_expressions.emplace_back(
148 for (
auto const& variable : variables)
153 double const value = *std::get<VariableArray::Scalar const*>(
156 if (std::isnan(value))
159 "Function property: Scalar variable '{:s}' is not "
166 auto assign_kelvin_vector = [&variable, &new_variable_array](
169 auto assign_value = [&destination = *address,
170 &variable]<
typename S>(S
const& source)
172 if constexpr (std::is_same_v<S, std::monostate>)
175 "Function property: Kelvin vector variable '{:s}' is "
181 if (std::holds_alternative<S>(destination))
189 "Function property: Mismatch of Kelvin vector "
190 "sizes for variable {:s}.",
196 std::visit(assign_value,
197 *std::get<VariableArray::KelvinVector const*>(
200 auto assign_deformation_gradient =
204 auto assign_value = [&destination = *address,
205 &variable]<
typename S>(S
const& source)
207 if constexpr (std::is_same_v<S, std::monostate>)
210 "Function property: Vectorized tensor variable '{:s}' "
211 "is not initialized.",
216 if (std::holds_alternative<S>(destination))
218 std::get<S>(destination) = source;
223 "Function property: Mismatch of vectorized tensor "
224 "sizes for variable {:s}.",
230 std::visit(assign_value,
231 *std::get<VariableArray::DeformationGradient const*>(
237 assign_deformation_gradient},
243 std::vector<Variable>
const& variables,
245 std::vector<exprtk::expression<double>>
const& expressions,
249 std::vector<double> result(expressions.size());
252 std::lock_guard lock_guard(mutex);
256 std::transform(begin(expressions), end(expressions), begin(result),
257 [](
auto const& e) {
return e.value(); });
260 switch (result.size())
268 return Eigen::Vector2d{result[0], result[1]};
272 return Eigen::Vector3d{result[0], result[1], result[2]};
276 Eigen::Matrix<double, 2, 2> m;
277 m = Eigen::Map<Eigen::Matrix<double, 2, 2>
const>(result.data(), 2,
283 Eigen::Matrix<double, 3, 3> m;
284 m = Eigen::Map<Eigen::Matrix<double, 3, 3>
const>(result.data(), 3,
289 OGS_FATAL(
"Cannot convert a vector of size {} to a PropertyDataType",
294 std::vector<std::string>
const& value_string_expressions,
295 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
296 dvalue_string_expressions)
298 std::vector<std::string> variables;
300 auto collect_variables = [&](
auto string_expressions)
302 for (
auto const& string_expression : string_expressions)
304 if (!exprtk::collect_variables(string_expression, variables))
307 "Collecting variables from expression '{}' didn't work.",
313 collect_variables(value_string_expressions);
314 for (
auto const& var_name_string_expression : dvalue_string_expressions)
316 collect_variables(var_name_string_expression.second);
325 std::vector<std::string>
const& value_string_expressions,
326 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
327 dvalue_string_expressions)
331 auto const variables =
335 ranges::views::transform([](std::string
const& s)
337 ranges::to<std::vector>;
339 impl2_ = std::make_unique<Implementation<2>>(
340 variables, value_string_expressions, dvalue_string_expressions);
341 impl3_ = std::make_unique<Implementation<3>>(
342 variables, value_string_expressions, dvalue_string_expressions);
349 if (variable_array.
is2D())
353 if (variable_array.
is3D())
359 "Variable array has vectors for 2 and 3 dimensions simultaneously. "
360 "Mixed dimensions cannot be dealt within Function evaluation.");
365 double const ,
double const )
const
371 impl_ptr->value_expressions,
372 impl_ptr->variable_array,
mutex_);
380 double const ,
double const )
const
385 auto const it = std::find_if(begin(impl_ptr->dvalue_expressions),
386 end(impl_ptr->dvalue_expressions),
387 [&variable](
auto const& v)
388 { return v.first == variable; });
390 if (it == end(impl_ptr->dvalue_expressions))
393 "Requested derivative with respect to the variable {:s} "
395 "provided for Function-type property {:s}.",
400 impl_ptr->variable_array,
mutex_);
std::vector< Expression > value_expressions
VariableArray variable_array
Implementation(std::vector< std::string > const &variables, std::vector< std::string > const &value_string_expressions, std::vector< std::pair< std::string, std::vector< std::string > > > const &dvalue_string_expressions)
exprtk::symbol_table< double > createSymbolTable(std::vector< std::string > const &variables)
std::vector< std::pair< Variable, std::vector< Expression > > > dvalue_expressions
exprtk::expression< double > Expression
std::unique_ptr< Implementation< 3 > > impl3_
std::vector< Variable > variables_
Variables used in the exprtk expressions.
std::variant< Function::Implementation< 2 > *, Function::Implementation< 3 > * > getImplementationForDimensionOfVariableArray(VariableArray const &variable_array) const
Function(std::string name, std::vector< std::string > const &value_string_expressions, std::vector< std::pair< std::string, std::vector< std::string > > > const &dvalue_string_expressions)
PropertyDataType dValue(VariableArray const &variable_array, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const override
std::unique_ptr< Implementation< 2 > > impl2_
virtual PropertyDataType value() const
std::variant< std::monostate, Eigen::Vector< double, 5 >, Eigen::Vector< double, 9 > > DeformationGradient
std::variant< std::monostate, Eigen::Vector< double, 4 >, Eigen::Vector< double, 6 > > KelvinVector
VariablePointerConst address_of(Variable const v) const
auto visitVariable(Visitor &&visitor, Variable const variable)
void makeVectorUnique(std::vector< T > &v)
static std::vector< exprtk::expression< T > > compileExpressions(exprtk::symbol_table< T > &symbol_table, std::vector< std::string > const &string_expressions)
static std::vector< std::string > collectVariables(std::vector< std::string > const &value_string_expressions, std::vector< std::pair< std::string, std::vector< std::string > > > const &dvalue_string_expressions)
static PropertyDataType evaluateExpressions(std::vector< Variable > const &variables, VariableArray const &new_variable_array, std::vector< exprtk::expression< double > > const &expressions, VariableArray &variable_array, std::mutex &mutex)
static void updateVariableArrayValues(std::vector< Variable > const &variables, VariableArray const &new_variable_array, VariableArray &variable_array)
static const std::array< std::string, static_cast< int >(Variable::number_of_variables)> variable_enum_to_string
Variable convertStringToVariable(std::string const &string)
std::variant< double, Eigen::Matrix< double, 2, 1 >, Eigen::Matrix< double, 3, 1 >, Eigen::Matrix< double, 2, 2 >, Eigen::Matrix< double, 3, 3 >, Eigen::Matrix< double, 4, 1 >, Eigen::Matrix< double, 6, 1 >, Eigen::MatrixXd > PropertyDataType
Eigen::Matrix< double, 4, 1 > kelvinVectorToSymmetricTensor(Eigen::Matrix< double, 4, 1, Eigen::ColMajor, 4, 1 > const &v)
constexpr int kelvin_vector_dimensions(int const displacement_dim)
Kelvin vector dimensions for given displacement dimension.
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.