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