22 exprtk::symbol_table<T>& symbol_table,
23 std::vector<std::string>
const& string_expressions)
25 exprtk::parser<T> parser;
27 std::vector<exprtk::expression<T>> expressions(string_expressions.size());
28 for (
unsigned i = 0; i < string_expressions.size(); ++i)
30 expressions[i].register_symbol_table(symbol_table);
31 if (!parser.compile(string_expressions[i], expressions[i]))
33 OGS_FATAL(
"Error: {:s}\tExpression: {:s}\n",
35 string_expressions[i]);
42 std::vector<std::pair<Variable, double*>>
const& symbol_values,
45 for (
auto const& [variable, value_ptr] : symbol_values)
48 [&variable = variable, &value_ptr = value_ptr](
auto&& v)
50 using T = std::decay_t<
decltype(v)>;
51 if constexpr (std::is_same_v<T, std::monostate>)
54 "Function property: variable {:s} value needed for "
55 "evaluation of the expression was not set by the "
59 else if constexpr (std::is_same_v<T, double>)
66 "Function property: not implemented handling for a "
67 "type {:s} of variable {:s}.",
72 variable_array[variable]);
77 std::vector<std::pair<Variable, double*>>
const& symbol_values,
79 std::vector<exprtk::expression<double>>
const& expressions,
82 std::vector<double> result(expressions.size());
85 std::lock_guard lock_guard(mutex);
88 std::transform(begin(expressions), end(expressions), begin(result),
89 [](
auto const& e) {
return e.value(); });
92 switch (result.size())
100 return Eigen::Vector2d{result[0], result[1]};
104 return Eigen::Vector3d{result[0], result[1], result[2]};
108 Eigen::Matrix<double, 2, 2> m;
109 m = Eigen::Map<Eigen::Matrix<double, 2, 2>
const>(result.data(), 2,
115 Eigen::Matrix<double, 3, 3> m;
116 m = Eigen::Map<Eigen::Matrix<double, 3, 3>
const>(result.data(), 3,
121 OGS_FATAL(
"Cannot convert a vector of size {} to a PropertyDataType",
126 std::vector<std::string>
const& value_string_expressions,
127 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
128 dvalue_string_expressions)
130 std::vector<std::string> variables;
132 auto collect_variables = [&](
auto string_expressions)
134 for (
auto const& string_expression : string_expressions)
136 if (!exprtk::collect_variables(string_expression, variables))
138 OGS_FATAL(
"Collecting variables didn't work.");
143 collect_variables(value_string_expressions);
144 for (
auto const& var_name_string_expression : dvalue_string_expressions)
146 collect_variables(var_name_string_expression.second);
155 std::vector<std::string>
const& value_string_expressions,
156 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
157 dvalue_string_expressions)
162 exprtk::symbol_table<double> symbol_table;
167 symbol_table.create_variable(v);
172 &symbol_table.get_variable(v)->ref());
180 for (
auto const& [variable_name, string_expressions] :
181 dvalue_string_expressions)
191 double const ,
double const )
const
200 double const ,
double const )
const
204 [&variable](
auto const& v)
205 {
return v.first == variable; });
210 "Requested derivative with respect to the variable {:s} not "
211 "provided for Function-type property {:s}.",
std::vector< Expression > value_expressions_
std::vector< std::pair< Variable, double * > > symbol_values_
Mapping from variable array index to symbol table values.
std::vector< std::pair< Variable, std::vector< Expression > > > dvalue_expressions_
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
virtual PropertyDataType value() const
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 void updateVariableValues(std::vector< std::pair< Variable, double * > > const &symbol_values, VariableArray const &variable_array)
static PropertyDataType evaluateExpressions(std::vector< std::pair< Variable, double * > > const &symbol_values, VariableArray const &variable_array, std::vector< exprtk::expression< double > > const &expressions, std::mutex &mutex)
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