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. More...
 
- 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. More...
 
 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
 

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 49 of file FunctionParameter.h.

◆ parser_t

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

Definition at line 50 of file FunctionParameter.h.

◆ symbol_table_t

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

Definition at line 48 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 60 of file FunctionParameter.h.

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

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 104 of file FunctionParameter.h.

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

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 102 of file FunctionParameter.h.

102 { 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 109 of file FunctionParameter.h.

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

References ParameterLib::ParameterBase::_coordinate_system, 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

◆ _symbol_table

◆ _vec_expression


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