OGS
FunctionEvaluation.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <cassert>
7
8#ifdef _OPENMP
9#include <omp.h>
10#endif
11
12#include "BaseLib/Error.h"
13#include "ExprtkUtils.h"
14
15namespace ParameterLib
16{
17
19 int num_threads,
20 std::vector<std::string> const& variables,
21 std::vector<std::string> const& expression_strings,
22 std::map<std::string,
23 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
24 curves)
25{
26 auto const expression_symbol_names = collectVariables(expression_strings);
27 auto const spatial_position_is_required =
28 hasSpatialVariables(expression_symbol_names);
29 auto const used_curve_names =
30 collectUsedCurveNames(expression_symbol_names, curves);
31
32 per_thread_data_.reserve(num_threads);
33 for (int thread_id = 0; thread_id < num_threads; ++thread_id)
34 {
35 per_thread_data_.emplace_back(variables, spatial_position_is_required,
36 used_curve_names, curves,
37 expression_strings);
38 }
39}
40
42 std::vector<std::string> const& variables,
43 bool const spatial_position_is_required,
44 std::vector<std::string> const& used_curve_names,
45 std::map<std::string,
46 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
47 curves,
48 std::map<std::string, CurveWrapper>& curve_wrappers)
49{
50 auto symbol_table = createBaseSymbolTable(spatial_position_is_required);
51
52 // Create additional (non-standard) variables.
53 for (auto const& v : variables)
54 {
55 if (!isBuiltinSymbol(v))
56 {
57 symbol_table.create_variable(v);
58 }
59 }
60
61 registerCurveWrappers(symbol_table, used_curve_names, curves,
62 curve_wrappers);
63
64 return symbol_table;
65}
66
68 std::vector<std::string> const& variables,
69 bool const spatial_position_is_required,
70 std::vector<std::string> const& used_curve_names,
71 std::map<std::string,
72 std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
73 curves,
74 std::vector<std::string> const& expression_strings)
75 : symbol_table(createSymbolTable(variables, spatial_position_is_required,
76 used_curve_names, curves, curve_wrappers)),
78 symbol_table_cache(symbol_table, spatial_position_is_required)
79{
80}
81
83{
85 "Function parameter: The internal PerThreadData is not "
86 "move-constructible.");
87}
88
89void FunctionEvaluation::evaluate(SpatialPosition const& pos, double const t,
90 std::span<double> result) const
91{
92#ifdef _OPENMP
93 int const thread_id = omp_get_thread_num();
94#else
95 int const thread_id = 0;
96#endif
97
98 if (thread_id >= static_cast<int>(per_thread_data_.size()))
99 {
100 OGS_FATAL("Thread id {:d} exceeds allocated threads {:d}.", thread_id,
101 per_thread_data_.size());
102 }
103
104 auto const& thread_data = per_thread_data_[thread_id];
105 auto const& expressions = thread_data.value_expressions;
106 assert(result.size() == expressions.size());
107
108 auto const& cache = thread_data.symbol_table_cache;
109
110 // Update time and spatial position.
111 cache.setTimeAndPosition(t, pos);
112
113 // Evaluate expressions.
114 for (unsigned i = 0; i < expressions.size(); ++i)
115 {
116 result[i] = expressions[i].value();
117 }
118}
119
120std::vector<double> FunctionEvaluation::evaluate(SpatialPosition const& pos,
121 double const t) const
122{
123 std::vector<double> result(
124 per_thread_data_.front().value_expressions.size());
125 evaluate(pos, t, result);
126 return result;
127}
128
130{
131 return static_cast<int>(per_thread_data_.front().value_expressions.size());
132}
133
134} // namespace ParameterLib
#define OGS_FATAL(...)
Definition Error.h:10
std::vector< double > evaluate(SpatialPosition const &pos, double t) const
exprtk::symbol_table< double > SymbolTable
FunctionEvaluation(int num_threads, std::vector< std::string > const &variables, std::vector< std::string > const &expression_strings, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
int getNumberOfComponents() const
Number of components (number of compiled value expressions).
std::vector< PerThreadData > per_thread_data_
Per-thread evaluation context; indexed by omp_get_thread_num().
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)
static FunctionEvaluation::SymbolTable createSymbolTable(std::vector< std::string > const &variables, bool const spatial_position_is_required, std::vector< std::string > const &used_curve_names, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves, std::map< std::string, CurveWrapper > &curve_wrappers)
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)
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)
std::map< std::string, CurveWrapper > curve_wrappers
Curve wrappers owned by this thread; must outlive symbol_table.
PerThreadData(std::vector< std::string > const &variables, bool spatial_position_is_required, std::vector< std::string > const &used_curve_names, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves, std::vector< std::string > const &expression_strings)
SymbolTableCache symbol_table_cache
Cached pointers to t, x, y, z inside symbol_table.
std::vector< Expression > value_expressions
Compiled value expressions. Must be destroyed before symbol_table.