OGS
ExprtkUtils.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <exprtk.hpp>
7#include <map>
8#include <memory>
9#include <string>
10#include <string_view>
11#include <vector>
12
13#include "BaseLib/Error.h"
15#include "SpatialPosition.h"
16
17namespace ParameterLib
18{
19
23struct CurveWrapper : public exprtk::ifunction<double>
24{
26 : exprtk::ifunction<double>(1), curve_(curve)
27 {
28 exprtk::disable_has_side_effects(*this);
29 }
30
31 double operator()(double const& t) override { return curve_.getValue(t); }
32
33private:
35};
36
40{
41 SymbolTableCache() = default;
42
46 SymbolTableCache(exprtk::symbol_table<double>& symbol_table,
47 bool spatial_position_is_required);
48
50 void reinitialize(exprtk::symbol_table<double>& symbol_table);
51
54 void setTimeAndPosition(double t, SpatialPosition const& pos) const;
55
56private:
58 double* t_ptr = nullptr;
59 double* x_ptr = nullptr;
60 double* y_ptr = nullptr;
61 double* z_ptr = nullptr;
62};
63
73template <typename T>
74std::vector<exprtk::expression<T>> compileExpressions(
75 exprtk::symbol_table<T>& symbol_table,
76 std::vector<std::string> const& string_expressions)
77{
78 using settings_t = typename exprtk::parser<T>::settings_store;
79 exprtk::parser<T> parser(settings_t::default_compile_all_opts +
80 settings_t::e_collect_assings);
81
82 std::vector<exprtk::expression<T>> expressions(string_expressions.size());
83 for (unsigned i = 0; i < string_expressions.size(); ++i)
84 {
85 expressions[i].register_symbol_table(symbol_table);
86 if (!parser.compile(string_expressions[i], expressions[i]))
87 {
88 OGS_FATAL("Error: {:s}\tExpression: {:s}\n", parser.error(),
89 string_expressions[i]);
90 }
91
92 std::vector<
93 typename exprtk::parser<T>::dependent_entity_collector::symbol_t>
94 assignments;
95 parser.dec().assignment_symbols(assignments);
96 if (!assignments.empty())
97 {
98 std::string names;
99 for (auto const& [name, type] : assignments)
100 {
101 if (!names.empty())
102 {
103 names += ", ";
104 }
105 names += name;
106 }
107 OGS_FATAL(
108 "Expression '{:s}' assigns to the already-defined symbol(s) "
109 "'{:s}'. Assignment to the built-in variables (t, x, y, z), to "
110 "MPL variables, curves or constants is not allowed because it "
111 "would corrupt the shared evaluation state. Assignment to "
112 "user-defined local 'var's is permitted.",
113 string_expressions[i], names);
114 }
115 }
116 return expressions;
117}
118
121bool isBuiltinSymbol(std::string_view name);
122
125bool hasSpatialVariables(std::vector<std::string> const& variables);
126
129exprtk::symbol_table<double> createBaseSymbolTable(
130 bool spatial_position_is_required);
131
140 exprtk::symbol_table<double>& symbol_table,
141 std::vector<std::string> const& curve_names,
142 std::map<std::string,
143 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
144 curves,
145 std::map<std::string, CurveWrapper>& curve_wrappers);
146
147extern template std::vector<exprtk::expression<double>>
148compileExpressions<double>(exprtk::symbol_table<double>&,
149 std::vector<std::string> const&);
150
153std::vector<std::string> collectVariables(
154 std::vector<std::string> const& expression_strings);
155
157std::vector<std::string> collectUsedCurveNames(
158 std::vector<std::string> const& expression_symbol_names,
159 std::map<std::string,
160 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
161 curves);
162
163} // namespace ParameterLib
#define OGS_FATAL(...)
Definition Error.h:10
std::vector< exprtk::expression< T > > compileExpressions(exprtk::symbol_table< T > &symbol_table, std::vector< std::string > const &string_expressions)
Definition ExprtkUtils.h:74
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)
MathLib::PiecewiseLinearInterpolation const & curve_
Definition ExprtkUtils.h:34
double operator()(double const &t) override
Definition ExprtkUtils.h:31
CurveWrapper(MathLib::PiecewiseLinearInterpolation const &curve)
Definition ExprtkUtils.h:25
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.