OGS 6.1.0-1699-ge946d4c5f
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"
22 
23 namespace ProcessLib
24 {
25 
31 template <typename T>
32 struct FunctionParameter final : public Parameter<T>
33 {
34  typedef exprtk::symbol_table<T> symbol_table_t;
35  typedef exprtk::expression<T> expression_t;
36  typedef exprtk::parser<T> parser_t;
37  typedef exprtk::parser_error::type error_t;
38 
47  FunctionParameter(std::string const& name_,
48  MeshLib::Mesh const& mesh_,
49  std::vector<std::string> const& vec_expression_str_)
50  : Parameter<T>(name_), _mesh(mesh_), _vec_expression_str(vec_expression_str_)
51  {
52  _symbol_table.add_constants();
53  _symbol_table.create_variable("x");
54  _symbol_table.create_variable("y");
55  _symbol_table.create_variable("z");
56 
57  _vec_expression.resize(_vec_expression_str.size());
58  for (unsigned i=0; i<_vec_expression_str.size(); i++)
59  {
60  _vec_expression[i].register_symbol_table(_symbol_table);
61  parser_t parser;
62  if (!parser.compile(_vec_expression_str[i], _vec_expression[i]))
63  {
64  OGS_FATAL("Error: %s\tExpression: %s\n", parser.error().c_str(),
65  _vec_expression_str[i].c_str());
66  }
67  }
68  }
69 
70  bool isTimeDependent() const override { return false; }
71 
72  int getNumberOfComponents() const override
73  {
74  return _vec_expression.size();
75  }
76 
77  std::vector<T> operator()(double const /*t*/,
78  SpatialPosition const& pos) const override
79  {
80  std::vector<T> cache(getNumberOfComponents());
81  auto& x = _symbol_table.get_variable("x")->ref();
82  auto& y = _symbol_table.get_variable("y")->ref();
83  auto& z = _symbol_table.get_variable("z")->ref();
84  if (pos.getCoordinates())
85  {
86  auto const coords = pos.getCoordinates().get();
87  x = coords[0];
88  y = coords[1];
89  z = coords[2];
90  }
91  else if (pos.getNodeID())
92  {
93  auto const& node = *_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  cache[i] = _vec_expression[i].value();
101 
102  return cache;
103  }
104 
105 private:
107  std::vector<std::string> const _vec_expression_str;
108  symbol_table_t _symbol_table;
109  std::vector<expression_t> _vec_expression;
110 };
111 
112 std::unique_ptr<ParameterBase> createFunctionParameter(
113  std::string const& name, BaseLib::ConfigTree const& config,
114  MeshLib::Mesh const& mesh);
115 
116 } // ProcessLib
std::vector< std::string > const _vec_expression_str
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition: Mesh.h:84
FunctionParameter(std::string const &name_, MeshLib::Mesh const &mesh_, std::vector< std::string > const &vec_expression_str_)
Definition of the Node class.
boost::optional< std::size_t > getNodeID() const
exprtk::parser_error::type error_t
std::vector< T > operator()(double const, SpatialPosition const &pos) const override
Returns the parameter value at the given time and position.
boost::optional< MathLib::TemplatePoint< double, 3 > > const & getCoordinates() const
int getNumberOfComponents() const override
std::string const name
Definition: Parameter.h:60
bool isTimeDependent() const override
exprtk::symbol_table< T > symbol_table_t
std::vector< expression_t > _vec_expression
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
exprtk::expression< T > expression_t
std::unique_ptr< ParameterBase > createFunctionParameter(std::string const &name, BaseLib::ConfigTree const &config, MeshLib::Mesh const &mesh)
Definition of the Element class.