OGS
ParameterLib::FunctionEvaluation Class Reference

Detailed Description

Shared implementation for mathematical function evaluation using exprtk. Provides per-thread storage for thread-safe evaluation under OpenMP.

Definition at line 20 of file FunctionEvaluation.h.

#include <FunctionEvaluation.h>

Classes

struct  PerThreadData

Public Types

using Expression = exprtk::expression<double>
using SymbolTable = exprtk::symbol_table<double>

Public Member Functions

 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)
std::vector< double > evaluate (SpatialPosition const &pos, double t) const
void evaluate (SpatialPosition const &pos, double t, std::span< double > result) const
int getNumberOfComponents () const
 Number of components (number of compiled value expressions).
bool isTimeDependent () const

Private Attributes

std::vector< PerThreadDataper_thread_data_
 Per-thread evaluation context; indexed by omp_get_thread_num().

Member Typedef Documentation

◆ Expression

using ParameterLib::FunctionEvaluation::Expression = exprtk::expression<double>

Definition at line 23 of file FunctionEvaluation.h.

◆ SymbolTable

using ParameterLib::FunctionEvaluation::SymbolTable = exprtk::symbol_table<double>

Definition at line 24 of file FunctionEvaluation.h.

Constructor & Destructor Documentation

◆ FunctionEvaluation()

ParameterLib::FunctionEvaluation::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 )
Parameters
num_threadsNumber of threads for per-thread storage.
variablesVariable names to register in the symbol table. Spatial variables (x, y, z) are created only when they appear here.
expression_stringsExpression strings to compile.
curvesNamed piecewise-linear curves available to expressions.

Definition at line 18 of file FunctionEvaluation.cpp.

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}
std::vector< PerThreadData > per_thread_data_
Per-thread evaluation context; indexed by omp_get_thread_num().
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)

References ParameterLib::collectUsedCurveNames(), ParameterLib::collectVariables(), ParameterLib::hasSpatialVariables(), and per_thread_data_.

Member Function Documentation

◆ evaluate() [1/2]

std::vector< double > ParameterLib::FunctionEvaluation::evaluate ( SpatialPosition const & pos,
double t ) const

Evaluate all value expressions at the given spatial position and time.

Although marked const, this method writes to per-thread symbol-table slots through cached pointers. Each OpenMP thread writes exclusively to its own slot, so concurrent calls from different threads do not race. The mathematical function itself is immutable after construction.

Definition at line 120 of file FunctionEvaluation.cpp.

122{
123 std::vector<double> result(
124 per_thread_data_.front().value_expressions.size());
125 evaluate(pos, t, result);
126 return result;
127}
std::vector< double > evaluate(SpatialPosition const &pos, double t) const

References evaluate(), and per_thread_data_.

Referenced by evaluate().

◆ evaluate() [2/2]

void ParameterLib::FunctionEvaluation::evaluate ( SpatialPosition const & pos,
double t,
std::span< double > result ) const

Evaluate all value expressions into a caller-provided output buffer.

No heap allocation is performed. Thread-safety is identical to the vector-returning overload.

Parameters
posSpatial position for evaluation.
tTime for evaluation.
resultMust have size equal to getNumberOfComponents().

Definition at line 89 of file FunctionEvaluation.cpp.

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}
#define OGS_FATAL(...)
Definition Error.h:10

References OGS_FATAL, and per_thread_data_.

◆ getNumberOfComponents()

int ParameterLib::FunctionEvaluation::getNumberOfComponents ( ) const

Number of components (number of compiled value expressions).

Definition at line 129 of file FunctionEvaluation.cpp.

130{
131 return static_cast<int>(per_thread_data_.front().value_expressions.size());
132}

References per_thread_data_.

◆ isTimeDependent()

bool ParameterLib::FunctionEvaluation::isTimeDependent ( ) const
inline

Definition at line 62 of file FunctionEvaluation.h.

62{ return true; }

Member Data Documentation

◆ per_thread_data_

std::vector<PerThreadData> ParameterLib::FunctionEvaluation::per_thread_data_
private

Per-thread evaluation context; indexed by omp_get_thread_num().

Definition at line 97 of file FunctionEvaluation.h.

Referenced by FunctionEvaluation(), evaluate(), evaluate(), and getNumberOfComponents().


The documentation for this class was generated from the following files: