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 expressions.resize(string_expressions.size());
29 for (
unsigned i = 0; i < string_expressions.size(); ++i)
31 expressions[i].register_symbol_table(symbol_table);
32 if (!parser.compile(string_expressions[i], expressions[i]))
34 OGS_FATAL(
"Error: {:s}\tExpression: {:s}\n",
36 string_expressions[i]);
43 std::vector<std::pair<int, double*>>
const& symbol_values,
46 for (
auto& index_value_ptr_pair : symbol_values)
48 auto const index = index_value_ptr_pair.first;
50 double* value_ptr = index_value_ptr_pair.second;
52 [&value_ptr, &index](
auto&& v)
54 using T = std::decay_t<decltype(v)>;
55 if constexpr (std::is_same_v<T, std::monostate>)
58 "Function property: variable {:s} value needed for "
59 "evaluation of the expression was not set by the "
63 else if constexpr (std::is_same_v<T, double>)
70 "Function property: not implemented handling for a "
71 "type {:s} of variable {:s}.",
75 variable_array[index]);
80 std::vector<std::pair<int, double*>>
const& symbol_values,
82 std::vector<exprtk::expression<double>>
const& expressions)
86 std::vector<double> result(expressions.size());
87 std::transform(begin(expressions), end(expressions), begin(result),
88 [](
auto const& e) {
return e.value(); });
90 switch (result.size())
98 return Eigen::Vector2d{result[0], result[1]};
102 return Eigen::Vector3d{result[0], result[1], result[2]};
106 Eigen::Matrix<double, 2, 2> m;
107 m = Eigen::Map<Eigen::Matrix<double, 2, 2>
const>(result.data(), 2,
113 Eigen::Matrix<double, 3, 3> m;
114 m = Eigen::Map<Eigen::Matrix<double, 3, 3>
const>(result.data(), 3,
119 OGS_FATAL(
"Cannot convert a vector of size {} to a PropertyDataType",
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 std::vector<std::string> variables;
130 auto collect_variables = [&](
auto string_expressions)
132 for (
auto const& string_expression : string_expressions)
134 if (!exprtk::collect_variables(string_expression, variables))
136 OGS_FATAL(
"Collecting variables didn't work.");
141 collect_variables(value_string_expressions);
142 for (
auto const& var_name_string_expression : dvalue_string_expressions)
144 collect_variables(var_name_string_expression.second);
153 std::vector<std::string>
const& value_string_expressions,
154 std::vector<std::pair<std::string, std::vector<std::string>>>
const&
155 dvalue_string_expressions)
160 exprtk::symbol_table<double> symbol_table;
165 symbol_table.create_variable(v);
168 int const variable_array_index =
171 &symbol_table.get_variable(v)->ref());
179 for (
auto const& [variable_name, string_expressions] :
180 dvalue_string_expressions)
190 double const ,
double const )
const
199 double const ,
double const )
const
203 [&primary_variable](
auto const& v)
204 {
return v.first == primary_variable; });
209 "Requested derivative with respect to the variable {:s} not "
210 "provided for Function-type property {:s}.",
PropertyDataType dValue(VariableArray const &variable_array, Variable const primary_variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const override
std::vector< std::pair< int, double * > > symbol_values_
Mapping from variable array index to symbol table values.
std::vector< Expression > value_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)
std::vector< std::pair< Variable, std::vector< Expression > > > dvalue_expressions_
virtual PropertyDataType value() const
void makeVectorUnique(std::vector< T > &v)
static PropertyDataType evaluateExpressions(std::vector< std::pair< int, double * >> const &symbol_values, VariableArray const &variable_array, std::vector< exprtk::expression< double >> const &expressions)
static const std::array< std::string, static_cast< int >Variable::number_of_variables)> variable_enum_to_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 > > PropertyDataType
static std::vector< exprtk::expression< T > > compileExpressions(exprtk::symbol_table< T > &symbol_table, std::vector< std::string > const &string_expressions)
std::array< VariableType, static_cast< int >(Variable::number_of_variables)> VariableArray
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)
Variable convertStringToVariable(std::string const &string)
static void updateVariableValues(std::vector< std::pair< int, double * >> const &symbol_values, VariableArray const &variable_array)