OGS
ParameterLib::FunctionParameter< T > Struct Template Referencefinal

Detailed Description

template<typename T>
struct ParameterLib::FunctionParameter< T >

A parameter class evaluating functions defined by user-provided mathematical expressions.

Currently, x, y, z, and t are supported as variables of the functions.

Definition at line 29 of file FunctionParameter.h.

#include <FunctionParameter.h>

Inheritance diagram for ParameterLib::FunctionParameter< T >:
[legend]
Collaboration diagram for ParameterLib::FunctionParameter< T >:
[legend]

Classes

class  CurveWrapper
 

Public Types

using symbol_table_t = exprtk::symbol_table<T>
 
using expression_t = exprtk::expression<T>
 
using parser_t = exprtk::parser<T>
 

Public Member Functions

 FunctionParameter (std::string const &name, std::vector< std::string > const &vec_expression_str, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const &curves)
 
bool isTimeDependent () const override
 
int getNumberOfGlobalComponents () const override
 
std::vector< T > operator() (double const t, SpatialPosition const &pos) const override
 Returns the parameter value at the given time and position.
 
- Public Member Functions inherited from ParameterLib::Parameter< T >
 ~Parameter () override=default
 
virtual Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > getNodalValuesOnElement (MeshLib::Element const &element, double const t) const
 Returns a matrix of values for all nodes of the given element.
 
 ParameterBase (std::string name_, MeshLib::Mesh const *mesh=nullptr)
 
- Public Member Functions inherited from ParameterLib::ParameterBase
 ParameterBase (std::string name_, MeshLib::Mesh const *mesh=nullptr)
 
virtual ~ParameterBase ()=default
 
void setCoordinateSystem (CoordinateSystem const &coordinate_system)
 
virtual void initialize (std::vector< std::unique_ptr< ParameterBase > > const &)
 
MeshLib::Mesh const * mesh () const
 

Private Attributes

symbol_table_t _symbol_table
 
std::vector< expression_t_vec_expression
 
std::vector< std::pair< std::string, CurveWrapper > > _curves
 
std::mutex _mutex
 

Additional Inherited Members

- Public Attributes inherited from ParameterLib::ParameterBase
std::string const name
 
- Protected Member Functions inherited from ParameterLib::ParameterBase
std::vector< double > rotateWithCoordinateSystem (std::vector< double > const &values, SpatialPosition const &pos) const
 
- Protected Attributes inherited from ParameterLib::ParameterBase
std::optional< CoordinateSystem_coordinate_system
 
MeshLib::Mesh const * _mesh
 

Member Typedef Documentation

◆ expression_t

template<typename T >
using ParameterLib::FunctionParameter< T >::expression_t = exprtk::expression<T>

Definition at line 50 of file FunctionParameter.h.

◆ parser_t

template<typename T >
using ParameterLib::FunctionParameter< T >::parser_t = exprtk::parser<T>

Definition at line 51 of file FunctionParameter.h.

◆ symbol_table_t

template<typename T >
using ParameterLib::FunctionParameter< T >::symbol_table_t = exprtk::symbol_table<T>

Definition at line 49 of file FunctionParameter.h.

Constructor & Destructor Documentation

◆ FunctionParameter()

template<typename T >
ParameterLib::FunctionParameter< T >::FunctionParameter ( std::string const & name,
std::vector< std::string > const & vec_expression_str,
std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation > > const & curves )
inline

Constructing from a vector of expressions

Parameters
namethe parameter's name
vec_expression_stra vector of mathematical expressions
curvesnamed list of curves used by expressions. The vector size specifies the number of components of the parameter.

Definition at line 61 of file FunctionParameter.h.

67 : Parameter<T>(name, nullptr)
68 {
69 // Convert curves to function objects callable by the exprtk.
70 _curves.reserve(curves.size());
71 std::transform(
72 begin(curves), end(curves), std::back_inserter(_curves),
73 [](auto const& curve) -> std::pair<std::string, CurveWrapper> {
74 return {curve.first, CurveWrapper(*curve.second)};
75 });
76
77 // Create symbol table for variables and functions.
78 _symbol_table.add_constants();
79 _symbol_table.create_variable("x");
80 _symbol_table.create_variable("y");
81 _symbol_table.create_variable("z");
82 _symbol_table.create_variable("t");
83 for (auto& curve : _curves)
84 {
85 _symbol_table.add_function(curve.first, curve.second);
86 }
87
88 // Compile expressions.
89 _vec_expression.resize(vec_expression_str.size());
90 for (unsigned i = 0; i < vec_expression_str.size(); i++)
91 {
92 _vec_expression[i].register_symbol_table(_symbol_table);
93 parser_t parser;
94 if (!parser.compile(vec_expression_str[i], _vec_expression[i]))
95 {
96 OGS_FATAL("Error: {:s}\tExpression: {:s}\n",
97 parser.error(),
98 vec_expression_str[i]);
99 }
100 }
101 }
#define OGS_FATAL(...)
Definition Error.h:26
std::vector< std::pair< std::string, CurveWrapper > > _curves
std::vector< expression_t > _vec_expression
std::string const name
Definition Parameter.h:73

References ParameterLib::FunctionParameter< T >::_curves, ParameterLib::FunctionParameter< T >::_symbol_table, ParameterLib::FunctionParameter< T >::_vec_expression, and OGS_FATAL.

Member Function Documentation

◆ getNumberOfGlobalComponents()

template<typename T >
int ParameterLib::FunctionParameter< T >::getNumberOfGlobalComponents ( ) const
inlineoverridevirtual

Returns the number of components this Parameter has at every position and point in time.

Implements ParameterLib::Parameter< T >.

Definition at line 105 of file FunctionParameter.h.

106 {
107 return _vec_expression.size();
108 }

References ParameterLib::FunctionParameter< T >::_vec_expression.

Referenced by ParameterLib::FunctionParameter< T >::operator()().

◆ isTimeDependent()

template<typename T >
bool ParameterLib::FunctionParameter< T >::isTimeDependent ( ) const
inlineoverridevirtual

Implements ParameterLib::ParameterBase.

Definition at line 103 of file FunctionParameter.h.

103{ return true; }

◆ operator()()

template<typename T >
std::vector< T > ParameterLib::FunctionParameter< T >::operator() ( double const t,
SpatialPosition const & pos ) const
inlineoverridevirtual

Returns the parameter value at the given time and position.

Implements ParameterLib::Parameter< T >.

Definition at line 110 of file FunctionParameter.h.

112 {
113 std::vector<T> cache(getNumberOfGlobalComponents());
114 auto& x = _symbol_table.get_variable("x")->ref();
115 auto& y = _symbol_table.get_variable("y")->ref();
116 auto& z = _symbol_table.get_variable("z")->ref();
117 auto& time = _symbol_table.get_variable("t")->ref();
118 if (!pos.getCoordinates())
119 {
120 OGS_FATAL(
121 "FunctionParameter: The spatial position has to be set by "
122 "coordinates.");
123 }
124 auto const coords = pos.getCoordinates().value();
125
126 {
127 std::lock_guard lock_guard(_mutex);
128 x = coords[0];
129 y = coords[1];
130 z = coords[2];
131 time = t;
132
133 {
134 for (unsigned i = 0; i < _vec_expression.size(); i++)
135 {
136 cache[i] = _vec_expression[i].value();
137 }
138 }
139 }
140
141 if (!this->_coordinate_system)
142 {
143 return cache;
144 }
145
146 return this->rotateWithCoordinateSystem(cache, pos);
147 }
constexpr ranges::views::view_closure coords
Definition Mesh.h:232
int getNumberOfGlobalComponents() const override
std::vector< double > rotateWithCoordinateSystem(std::vector< double > const &values, SpatialPosition const &pos) const
Definition Parameter.h:76
std::optional< CoordinateSystem > _coordinate_system
Definition Parameter.h:126

References ParameterLib::ParameterBase::_coordinate_system, ParameterLib::FunctionParameter< T >::_mutex, ParameterLib::FunctionParameter< T >::_symbol_table, ParameterLib::FunctionParameter< T >::_vec_expression, ParameterLib::SpatialPosition::getCoordinates(), ParameterLib::FunctionParameter< T >::getNumberOfGlobalComponents(), OGS_FATAL, and ParameterLib::ParameterBase::rotateWithCoordinateSystem().

Member Data Documentation

◆ _curves

template<typename T >
std::vector<std::pair<std::string, CurveWrapper> > ParameterLib::FunctionParameter< T >::_curves
private

◆ _mutex

template<typename T >
std::mutex ParameterLib::FunctionParameter< T >::_mutex
mutableprivate

◆ _symbol_table

◆ _vec_expression


The documentation for this struct was generated from the following file: