OGS
Property.h
Go to the documentation of this file.
1 
12 #pragma once
13 
14 #include <Eigen/Dense>
15 #include <array>
16 #include <string>
17 #include <typeinfo>
18 #include <variant>
19 
20 #include "BaseLib/Error.h"
22 #include "PropertyType.h"
23 #include "VariableType.h"
24 
25 namespace MaterialPropertyLib
26 {
27 class Medium;
28 class Phase;
29 class Component;
30 
32  std::variant<double, Eigen::Matrix<double, 2, 1>,
33  Eigen::Matrix<double, 3, 1>, Eigen::Matrix<double, 2, 2>,
34  Eigen::Matrix<double, 3, 3>, Eigen::Matrix<double, 4, 1>,
35  Eigen::Matrix<double, 6, 1>>;
36 
40 PropertyDataType fromVector(std::vector<double> const& values);
41 
45 class Property
46 {
47 public:
48 #ifndef NDEBUG
49  virtual ~Property()
50  {
51  if(property_used)
52  {
53  DBUG("Property is used: '{:s}'", description());
54  }
55  else
56  {
57  WARN("Property is not used: '{:s}'", description());
58  }
59  }
60 #else
61  virtual ~Property() = default;
62 #endif
63 
67  ParameterLib::SpatialPosition const& pos, double const t) const;
68 
71  virtual PropertyDataType value() const;
75  virtual PropertyDataType value(VariableArray const& variable_array,
76  VariableArray const& variable_array_prev,
78  double const t, double const dt) const;
82  virtual PropertyDataType value(VariableArray const& variable_array,
84  double const t, double const dt) const;
88  virtual PropertyDataType dValue(VariableArray const& variable_array,
89  VariableArray const& variable_array_prev,
90  Variable const variable,
92  double const t, double const dt) const;
96  virtual PropertyDataType dValue(VariableArray const& variable_array,
97  Variable const variable,
99  double const t, double const dt) const;
102  virtual PropertyDataType d2Value(VariableArray const& variable_array,
103  Variable const variable1,
104  Variable const variable2,
106  double const t, double const dt) const;
107 
108  void setScale(std::variant<Medium*, Phase*, Component*> scale)
109  {
110  scale_ = scale;
111  checkScale();
112  };
113 
114  template <typename T>
116  double const t) const
117  {
118  try
119  {
120  return std::get<T>(initialValue(pos, t));
121  }
122  catch (std::bad_variant_access const&)
123  {
124  OGS_FATAL(
125  "The initial value of {:s} does not hold requested type '{:s}' "
126  "but a {:s}.",
127  description(),
128  typeid(T).name(),
129  property_data_type_names_[initialValue(pos, t).index()]);
130  }
131  }
132 
133  template <typename T>
134  T value() const
135  {
136  try
137  {
138 #ifndef NDEBUG
139  property_used = true;
140 #endif
141  return std::get<T>(value());
142  }
143  catch (std::bad_variant_access const&)
144  {
145  OGS_FATAL(
146  "The value of {:s} does not hold requested type '{:s}' but a "
147  "{:s}.",
148  description(),
149  typeid(T).name(),
150  property_data_type_names_[value().index()]);
151  }
152  }
153 
154  template <typename T>
155  T value(VariableArray const& variable_array,
156  VariableArray const& variable_array_prev,
157  ParameterLib::SpatialPosition const& pos, double const t,
158  double const dt) const
159  {
160  try
161  {
162 #ifndef NDEBUG
163  property_used = true;
164 #endif
165  return std::get<T>(
166  value(variable_array, variable_array_prev, pos, t, dt));
167  }
168  catch (std::bad_variant_access const&)
169  {
170  OGS_FATAL(
171  "The value of {:s} is not of the requested type '{:s}' but a "
172  "{:s}.",
173  description(),
174  typeid(T).name(),
175  property_data_type_names_[value(variable_array,
176  variable_array_prev, pos, t, dt)
177  .index()]);
178  }
179  }
180  template <typename T>
181  T value(VariableArray const& variable_array,
182  ParameterLib::SpatialPosition const& pos, double const t,
183  double const dt) const
184  {
185  try
186  {
187 #ifndef NDEBUG
188  property_used = true;
189 #endif
190  return std::get<T>(value(variable_array, pos, t, dt));
191  }
192  catch (std::bad_variant_access const&)
193  {
194  OGS_FATAL(
195  "The value of {:s} is not of the requested type '{:s}' but a "
196  "{:s}.",
197  description(),
198  typeid(T).name(),
199  property_data_type_names_[value(variable_array, pos, t, dt)
200  .index()]);
201  }
202  }
203 
204  template <typename T>
205  T dValue(VariableArray const& variable_array,
206  VariableArray const& variable_array_prev, Variable const variable,
207  ParameterLib::SpatialPosition const& pos, double const t,
208  double const dt) const
209  {
210  try
211  {
212 #ifndef NDEBUG
213  property_used = true;
214 #endif
215  return std::get<T>(dValue(variable_array, variable_array_prev,
216  variable, pos, t, dt));
217  }
218  catch (std::bad_variant_access const&)
219  {
220  OGS_FATAL(
221  "The first derivative value of {:s} is not of the requested "
222  "type '{:s}' but a {:s}.",
223  description(),
224  typeid(T).name(),
226  [dValue(variable_array, variable, pos, t, dt).index()]);
227  }
228  }
229  template <typename T>
230  T dValue(VariableArray const& variable_array, Variable const variable,
231  ParameterLib::SpatialPosition const& pos, double const t,
232  double const dt) const
233  {
234  try
235  {
236 #ifndef NDEBUG
237  property_used = true;
238 #endif
239  return std::get<T>(dValue(variable_array, variable, pos, t, dt));
240  }
241  catch (std::bad_variant_access const&)
242  {
243  OGS_FATAL(
244  "The first derivative value of {:s} is not of the requested "
245  "type '{:s}' but a {:s}.",
246  description(),
247  typeid(T).name(),
249  [dValue(variable_array, variable, pos, t, dt).index()]);
250  }
251  }
252  template <typename T>
253  T d2Value(VariableArray const& variable_array, Variable const& variable1,
254  Variable const& variable2,
255  ParameterLib::SpatialPosition const& pos, double const t,
256  double const dt) const
257  {
258  try
259  {
260 #ifndef NDEBUG
261  property_used = true;
262 #endif
263  return std::get<T>(
264  d2Value(variable_array, variable1, variable2, pos, t, dt));
265  }
266  catch (std::bad_variant_access const&)
267  {
268  OGS_FATAL(
269  "The second derivative value of {:s} is not of the requested "
270  "type '{:s}' but a {:s}.",
271  description(),
272  typeid(T).name(),
273  property_data_type_names_[d2Value(variable_array, variable1,
274  variable2, pos, t, dt)
275  .index()]);
276  }
277  }
278 
279 protected:
280  std::string name_;
287  std::variant<Medium*, Phase*, Component*> scale_;
288 
289 private:
290  virtual void checkScale() const
291  {
292  // Empty check for properties which can be defined on every scale,
293  // medium, phase or component
294  }
295  std::string description() const;
296 #ifndef NDEBUG
297  mutable bool property_used = false;
298 #endif
299 
300 private:
302  static constexpr std::array property_data_type_names_ = {
303  "scalar", "2-vector", "3-vector", "2x2-matrix",
304  "3x3-matrix", "2D-Kelvin vector", "3D-Kelvin vector"};
305  static_assert(property_data_type_names_.size() ==
306  std::variant_size_v<PropertyDataType>,
307  "The array of property data type names has different size "
308  "than the PropertyDataType variant type.");
309 };
310 
312  PropertyArray& properties,
313  PropertyArray& new_properties,
314  std::variant<Medium*, Phase*, Component*>
315  scale_pointer)
316 {
317  for (std::size_t i = 0; i < properties.size(); ++i)
318  {
319  if (new_properties[i] != nullptr)
320  {
321  properties[i] = std::move(new_properties[i]);
322  properties[i]->setScale(scale_pointer);
323  }
324  }
325 }
326 
327 } // namespace MaterialPropertyLib
#define OGS_FATAL(...)
Definition: Error.h:26
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
virtual void checkScale() const
Definition: Property.h:290
virtual PropertyDataType d2Value(VariableArray const &variable_array, Variable const variable1, Variable const variable2, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Default implementation: 2nd derivative of any constant property is zero.
Definition: Property.cpp:127
std::string description() const
Definition: Property.cpp:140
static constexpr std::array property_data_type_names_
Corresponds to the PropertyDataType.
Definition: Property.h:302
T dValue(VariableArray const &variable_array, VariableArray const &variable_array_prev, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition: Property.h:205
T value(VariableArray const &variable_array, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition: Property.h:181
T d2Value(VariableArray const &variable_array, Variable const &variable1, Variable const &variable2, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition: Property.h:253
PropertyDataType value_
The single value of a property.
Definition: Property.h:282
T dValue(VariableArray const &variable_array, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition: Property.h:230
PropertyDataType dvalue_
Definition: Property.h:283
T initialValue(ParameterLib::SpatialPosition const &pos, double const t) const
Definition: Property.h:115
virtual PropertyDataType value() const
Definition: Property.cpp:72
void setScale(std::variant< Medium *, Phase *, Component * > scale)
Definition: Property.h:108
T value(VariableArray const &variable_array, VariableArray const &variable_array_prev, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition: Property.h:155
std::variant< Medium *, Phase *, Component * > scale_
Definition: Property.h:287
virtual PropertyDataType initialValue(ParameterLib::SpatialPosition const &pos, double const t) const
Definition: Property.cpp:65
virtual PropertyDataType dValue(VariableArray const &variable_array, VariableArray const &variable_array_prev, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition: Property.cpp:101
std::array< std::unique_ptr< Property >, PropertyType::number_of_properties > PropertyArray
void overwriteExistingProperties(PropertyArray &properties, PropertyArray &new_properties, std::variant< Medium *, Phase *, Component * > scale_pointer)
Definition: Property.h:311
PropertyDataType fromVector(std::vector< double > const &values)
Definition: Property.cpp:23
std::variant< double, Eigen::Matrix< double, 2, 1 >, Eigen::Matrix< double, 3, 1 >, Eigen::Matrix< double, 2, 2 >, Eigen::Matrix< double, 3, 3 >, Eigen::Matrix< double, 4, 1 >, Eigen::Matrix< double, 6, 1 > > PropertyDataType
Definition: Property.h:35
std::array< VariableType, static_cast< int >(Variable::number_of_variables)> VariableArray
Definition: VariableType.h:108
void scale(PETScVector &x, double const a)
Definition: LinAlg.cpp:44