OGS 6.2.0-97-g4a610c866
FunctionParameter.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <utility>
13 #include <vector>
14 
15 #include <exprtk.hpp>
16 
18 #include "MeshLib/Node.h"
19 
20 #include "Parameter.h"
21 #include "Utils.h"
22 
23 namespace ParameterLib
24 {
30 template <typename T>
31 struct FunctionParameter final : public Parameter<T>
32 {
33  using symbol_table_t = exprtk::symbol_table<T>;
34  using expression_t = exprtk::expression<T>;
35  using parser_t = exprtk::parser<T>;
36  using error_t = exprtk::parser_error::type;
37 
46  FunctionParameter(std::string const& name,
47  MeshLib::Mesh const& mesh,
48  std::vector<std::string> const& vec_expression_str)
49  : Parameter<T>(name, &mesh), _vec_expression_str(vec_expression_str)
50  {
51  _symbol_table.add_constants();
52  _symbol_table.create_variable("x");
53  _symbol_table.create_variable("y");
54  _symbol_table.create_variable("z");
55 
56  _vec_expression.resize(_vec_expression_str.size());
57  for (unsigned i = 0; i < _vec_expression_str.size(); i++)
58  {
59  _vec_expression[i].register_symbol_table(_symbol_table);
60  parser_t parser;
61  if (!parser.compile(_vec_expression_str[i], _vec_expression[i]))
62  {
63  OGS_FATAL("Error: %s\tExpression: %s\n", parser.error().c_str(),
64  _vec_expression_str[i].c_str());
65  }
66  }
67  }
68 
69  bool isTimeDependent() const override { return false; }
70 
71  int getNumberOfComponents() const override
72  {
73  return _vec_expression.size();
74  }
75 
76  std::vector<T> operator()(double const /*t*/,
77  SpatialPosition const& pos) const override
78  {
79  std::vector<T> cache(getNumberOfComponents());
80  auto& x = _symbol_table.get_variable("x")->ref();
81  auto& y = _symbol_table.get_variable("y")->ref();
82  auto& z = _symbol_table.get_variable("z")->ref();
83  if (pos.getCoordinates())
84  {
85  auto const coords = pos.getCoordinates().get();
86  x = coords[0];
87  y = coords[1];
88  z = coords[2];
89  }
90  else if (pos.getNodeID())
91  {
92  auto const& node =
93  *ParameterBase::_mesh->getNode(pos.getNodeID().get());
94  x = node[0];
95  y = node[1];
96  z = node[2];
97  }
98 
99  for (unsigned i = 0; i < _vec_expression.size(); i++)
100  {
101  cache[i] = _vec_expression[i].value();
102  }
103 
104  if (!this->_coordinate_system)
105  {
106  return cache;
107  }
108 
109  return this->rotateWithCoordinateSystem(cache, pos);
110  }
111 
112 private:
113  std::vector<std::string> const _vec_expression_str;
115  std::vector<expression_t> _vec_expression;
116 };
117 
118 std::unique_ptr<ParameterBase> createFunctionParameter(
119  std::string const& name, BaseLib::ConfigTree const& config,
120  MeshLib::Mesh const& mesh);
121 
122 } // namespace ParameterLib
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition: Mesh.h:84
boost::optional< CoordinateSystem > _coordinate_system
Definition: Parameter.h:126
std::unique_ptr< ParameterBase > createFunctionParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
Definition of the Node class.
int getNumberOfComponents() const override
exprtk::symbol_table< T > symbol_table_t
std::vector< expression_t > _vec_expression
FunctionParameter(std::string const &name, MeshLib::Mesh const &mesh, std::vector< std::string > const &vec_expression_str)
boost::optional< MathLib::TemplatePoint< double, 3 > > const & getCoordinates() const
boost::optional< std::size_t > getNodeID() const
exprtk::parser_error::type error_t
std::vector< double > rotateWithCoordinateSystem(std::vector< double > const &values, SpatialPosition const &pos) const
Definition: Parameter.h:76
std::string const name
Definition: Parameter.h:73
std::vector< T > operator()(double const, SpatialPosition const &pos) const override
Returns the parameter value at the given time and position.
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
exprtk::expression< T > expression_t
Definition of the Element class.
MeshLib::Mesh const * mesh() const
Definition: Parameter.h:71
std::vector< std::string > const _vec_expression_str
MeshLib::Mesh const * _mesh
Definition: Parameter.h:130
bool isTimeDependent() const override