OGS
Parameter.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <Eigen/Dense>
14 #include <map>
15 #include <memory>
16 #include <optional>
17 #include <utility>
18 #include <vector>
19 
20 #include "BaseLib/Error.h"
21 #include "CoordinateSystem.h"
23 #include "MeshLib/Node.h"
24 #include "SpatialPosition.h"
25 
26 namespace BaseLib
27 {
28 class ConfigTree;
29 } // namespace BaseLib
30 
31 namespace MathLib
32 {
33 class PiecewiseLinearInterpolation;
34 } // namespace MathLib
35 
36 namespace MeshLib
37 {
38 class Mesh;
39 } // namespace MeshLib
40 
41 namespace ParameterLib
42 {
47 {
48  explicit ParameterBase(std::string name_,
49  MeshLib::Mesh const* mesh = nullptr)
50  : name(std::move(name_)), _mesh(mesh)
51  {
52  }
53 
54  virtual ~ParameterBase() = default;
55 
56  virtual bool isTimeDependent() const = 0;
57 
58  void setCoordinateSystem(CoordinateSystem const& coordinate_system)
59  {
60  _coordinate_system = coordinate_system;
61  }
62 
65  virtual void initialize(
66  std::vector<std::unique_ptr<ParameterBase>> const& /*parameters*/)
67  {
68  }
69 
70  MeshLib::Mesh const* mesh() const { return _mesh; }
71 
72  std::string const name;
73 
74 protected:
75  std::vector<double> rotateWithCoordinateSystem(
76  std::vector<double> const& values, SpatialPosition const& pos) const
77  {
78  assert(!!_coordinate_system); // It is checked before calling this
79  // function.
80 
81  // Don't rotate isotropic/scalar values.
82  if (values.size() == 1)
83  {
84  return values;
85  }
86  if (values.size() == 2)
87  {
88  auto const result =
89  _coordinate_system->rotateDiagonalTensor<2>(values, pos);
90  return {result(0, 0), result(0, 1), result(1, 0), result(1, 1)};
91  }
92  if (values.size() == 3)
93  {
94  auto const result =
95  _coordinate_system->rotateDiagonalTensor<3>(values, pos);
96  return {
97  result(0, 0), result(0, 1), result(0, 2),
98  result(1, 0), result(1, 1), result(1, 2),
99  result(2, 0), result(2, 1), result(2, 2),
100  };
101  }
102  if (values.size() == 4)
103  {
104  auto const result =
105  _coordinate_system->rotateTensor<2>(values, pos);
106  return {result(0, 0), result(0, 1), result(1, 0), result(1, 1)};
107  }
108  if (values.size() == 9)
109  {
110  auto const result =
111  _coordinate_system->rotateTensor<3>(values, pos);
112  return {
113  result(0, 0), result(0, 1), result(0, 2),
114  result(1, 0), result(1, 1), result(1, 2),
115  result(2, 0), result(2, 1), result(2, 2),
116  };
117  }
118  OGS_FATAL(
119  "Coordinate transformation for a {:d}-component parameter is not "
120  "implemented.",
121  values.size());
122  }
123 
124 protected:
125  std::optional<CoordinateSystem> _coordinate_system;
126 
130 };
131 
138 template <typename T>
139 struct Parameter : public ParameterBase
140 {
142 
143  ~Parameter() override = default;
144 
147  virtual int getNumberOfGlobalComponents() const = 0;
148 
150  virtual std::vector<T> operator()(double const t,
151  SpatialPosition const& pos) const = 0;
152 
154  //
155  // The matrix is of the shape NxC, where N is the number of nodes and C is
156  // the number of components, such that subsequent multiplication with shape
157  // functions matrix from left (which is a row vector) results in a row
158  // vector of length C.
159  //
160  // The default implementation covers all cases, but the derived classes may
161  // provide faster implementations.
162  virtual Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
164  double const t) const
165  {
166  auto const n_nodes = static_cast<int>(element.getNumberOfNodes());
167  auto const n_components = getNumberOfGlobalComponents();
168  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> result(n_nodes,
169  n_components);
170 
171  // Column vector of values, copied for each node.
172  SpatialPosition x_position;
173  auto const nodes = element.getNodes();
174  for (int i = 0; i < n_nodes; ++i)
175  {
176  x_position.setAll(
177  nodes[i]->getID(), element.getID(), std::nullopt, *nodes[i]);
178  auto const& values = this->operator()(t, x_position);
179  auto const row_values =
180  Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, 1> const>(
181  values.data(), values.size());
182  result.row(i) = row_values;
183  }
184 
185  return result;
186  }
187 };
188 
192 std::unique_ptr<ParameterBase> createParameter(
193  BaseLib::ConfigTree const& config,
194  const std::vector<std::unique_ptr<MeshLib::Mesh>>& meshes,
195  std::map<std::string,
196  std::unique_ptr<MathLib::PiecewiseLinearInterpolation>> const&
197  curves);
198 
205 std::optional<std::string> isDefinedOnSameMesh(ParameterBase const& parameter,
206  MeshLib::Mesh const& mesh);
207 
208 } // namespace ParameterLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition: Error.h:26
Definition of the Node class.
virtual Node *const * getNodes() const =0
Get array of element nodes.
virtual unsigned getNumberOfNodes() const =0
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:82
void setAll(std::optional< std::size_t > const &node_id, std::optional< std::size_t > const &element_id, std::optional< unsigned > const &integration_point, std::optional< MathLib::TemplatePoint< double, 3 >> const &coordinates)
std::optional< std::string > isDefinedOnSameMesh(ParameterBase const &parameter, MeshLib::Mesh const &mesh)
Definition: Parameter.cpp:98
std::unique_ptr< ParameterBase > createParameter(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< MeshLib::Mesh >> const &meshes, std::map< std::string, std::unique_ptr< MathLib::PiecewiseLinearInterpolation >> const &curves)
Definition: Parameter.cpp:26
virtual void initialize(std::vector< std::unique_ptr< ParameterBase >> const &)
Definition: Parameter.h:65
MeshLib::Mesh const * _mesh
Definition: Parameter.h:129
std::string const name
Definition: Parameter.h:72
virtual bool isTimeDependent() const =0
ParameterBase(std::string name_, MeshLib::Mesh const *mesh=nullptr)
Definition: Parameter.h:48
std::optional< CoordinateSystem > _coordinate_system
Definition: Parameter.h:125
void setCoordinateSystem(CoordinateSystem const &coordinate_system)
Definition: Parameter.h:58
MeshLib::Mesh const * mesh() const
Definition: Parameter.h:70
virtual ~ParameterBase()=default
std::vector< double > rotateWithCoordinateSystem(std::vector< double > const &values, SpatialPosition const &pos) const
Definition: Parameter.h:75
virtual std::vector< T > operator()(double const t, SpatialPosition const &pos) const =0
Returns the parameter value at the given time and position.
~Parameter() override=default
virtual int getNumberOfGlobalComponents() const =0
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.
Definition: Parameter.h:163