19 assert(
t_ptr !=
nullptr);
30 "An expression requires spatial position (x, y, z), "
31 "but no coordinates have been set.");
42 return name ==
"t" || name ==
"x" || name ==
"y" || name ==
"z";
47 return std::ranges::any_of(variables, [](std::string
const& v)
48 {
return v ==
"x" || v ==
"y" || v ==
"z"; });
52 bool spatial_position_is_required)
54 exprtk::symbol_table<double> symbol_table;
55 symbol_table.add_constants();
56 symbol_table.create_variable(
"t");
57 if (spatial_position_is_required)
59 symbol_table.create_variable(
"x");
60 symbol_table.create_variable(
"y");
61 symbol_table.create_variable(
"z");
67 exprtk::symbol_table<double>& symbol_table,
68 std::vector<std::string>
const& curve_names,
70 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>>
const&
72 std::map<std::string, CurveWrapper>& curve_wrappers)
74 for (
auto const& name : curve_names)
76 if (exprtk::details::is_reserved_symbol(name))
79 "Curve name '{:s}' collides with an exprtk reserved symbol "
80 "(built-in function or keyword). Such a curve cannot be "
81 "registered: the built-in would silently be used instead. "
82 "Please rename the curve.",
85 curve_wrappers.emplace(name,
CurveWrapper(*curves.at(name)));
87 for (
auto& [name, wrapper] : curve_wrappers)
89 if (!symbol_table.add_function(name, wrapper))
92 "Failed to register curve '{:s}' as an exprtk function. The "
93 "name is likely already in use (reserved symbol or duplicate).",
100 bool const spatial_position_is_required)
108 t_ptr = &(symbol_table.get_variable(
"t")->ref());
111 x_ptr = &(symbol_table.get_variable(
"x")->ref());
112 y_ptr = &(symbol_table.get_variable(
"y")->ref());
113 z_ptr = &(symbol_table.get_variable(
"z")->ref());
124 exprtk::symbol_table<double>&, std::vector<std::string>
const&);
127 std::vector<std::string>
const& expression_strings)
129 std::vector<std::string> expression_symbol_names;
130 for (
auto const& expr : expression_strings)
132 if (!exprtk::collect_variables(expr, expression_symbol_names))
134 OGS_FATAL(
"Collecting variables from expression '{}' didn't work.",
146 std::erase_if(expression_symbol_names,
147 [&constants](std::string
const& name)
148 {
return constants.is_constant_node(name); });
150 return expression_symbol_names;
154 std::vector<std::string>
const& expression_symbol_names,
155 std::map<std::string,
156 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>>
const&
159 std::vector<std::string> used;
160 for (
auto const& v : expression_symbol_names)
162 if (curves.contains(v))
std::optional< MathLib::Point3d > const getCoordinates() const
void makeVectorUnique(std::vector< T > &v)
bool isBuiltinSymbol(std::string_view const name)
exprtk::symbol_table< double > createBaseSymbolTable(bool spatial_position_is_required)
void registerCurveWrappers(exprtk::symbol_table< double > &symbol_table, std::vector< std::string > const &curve_names, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves, std::map< std::string, CurveWrapper > &curve_wrappers)
std::vector< std::string > collectVariables(std::vector< std::string > const &expression_strings)
template std::vector< exprtk::expression< double > > compileExpressions< double >(exprtk::symbol_table< double > &, std::vector< std::string > const &)
std::vector< std::string > collectUsedCurveNames(std::vector< std::string > const &expression_symbol_names, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
Returns the subset of expression_symbol_names that are keys in curves.
bool hasSpatialVariables(std::vector< std::string > const &variables)
SymbolTableCache()=default
void setTimeAndPosition(double t, SpatialPosition const &pos) const
void reinitialize(exprtk::symbol_table< double > &symbol_table)
Re-caches pointers from a (possibly moved) symbol_table.
bool spatial_position_is_required_