OGS
Property.h
Go to the documentation of this file.
1
12#pragma once
13
14#include <Eigen/Core>
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
25namespace MaterialPropertyLib
26{
27class Medium;
28class Phase;
29class 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>, Eigen::MatrixXd>;
36
45PropertyDataType fromVector(std::vector<double> const& values);
46
51{
52public:
53#ifndef NDEBUG
54 virtual ~Property()
55 {
56 if (property_used)
57 {
58 DBUG("Property is used: '{:s}'", description());
59 }
60 else
61 {
62 WARN("Property is not used: '{:s}'", description());
63 }
64 }
65#else
66 virtual ~Property() = default;
67#endif
68
72 ParameterLib::SpatialPosition const& pos, double const t) const;
73
76 virtual PropertyDataType value() const;
80 virtual PropertyDataType value(VariableArray const& variable_array,
81 VariableArray const& variable_array_prev,
83 double const t, double const dt) const;
87 virtual PropertyDataType value(VariableArray const& variable_array,
89 double const t, double const dt) const;
93 virtual PropertyDataType dValue(VariableArray const& variable_array,
94 VariableArray const& variable_array_prev,
95 Variable const variable,
97 double const t, double const dt) const;
101 virtual PropertyDataType dValue(VariableArray const& variable_array,
102 Variable const variable,
104 double const t, double const dt) const;
107 virtual PropertyDataType d2Value(VariableArray const& variable_array,
108 Variable const variable1,
109 Variable const variable2,
111 double const t, double const dt) const;
112
115 virtual void setProperties(
116 std::vector<std::unique_ptr<Phase>> const& phases);
117
118 void setScale(std::variant<Medium*, Phase*, Component*> scale)
119 {
120 scale_ = scale;
121 checkScale();
122 };
123
124 template <typename T>
126 double const t) const
127 {
128 try
129 {
130 return std::get<T>(initialValue(pos, t));
131 }
132 catch (std::bad_variant_access const&)
133 {
134 OGS_FATAL(
135 "The initial value of {:s} does not hold requested type '{:s}' "
136 "but a {:s}.",
137 description(),
138 typeid(T).name(),
139 property_data_type_names_[initialValue(pos, t).index()]);
140 }
141 }
142
143 template <typename T>
144 T value() const
145 {
146 try
147 {
148#ifndef NDEBUG
149 property_used = true;
150#endif
151 return std::get<T>(value());
152 }
153 catch (std::bad_variant_access const&)
154 {
155 OGS_FATAL(
156 "The value of {:s} does not hold requested type '{:s}' but a "
157 "{:s}.",
158 description(),
159 typeid(T).name(),
161 }
162 }
163
164 template <typename T>
165 T value(VariableArray const& variable_array,
166 VariableArray const& variable_array_prev,
167 ParameterLib::SpatialPosition const& pos, double const t,
168 double const dt) const
169 {
170 try
171 {
172#ifndef NDEBUG
173 property_used = true;
174#endif
175 return std::get<T>(
176 value(variable_array, variable_array_prev, pos, t, dt));
177 }
178 catch (std::bad_variant_access const&)
179 {
180 OGS_FATAL(
181 "The value of {:s} is not of the requested type '{:s}' but a "
182 "{:s}.",
183 description(),
184 typeid(T).name(),
185 property_data_type_names_[value(variable_array,
186 variable_array_prev, pos, t, dt)
187 .index()]);
188 }
189 }
190 template <typename T>
191 T value(VariableArray const& variable_array,
192 ParameterLib::SpatialPosition const& pos, double const t,
193 double const dt) const
194 {
195 try
196 {
197#ifndef NDEBUG
198 property_used = true;
199#endif
200 return std::get<T>(value(variable_array, pos, t, dt));
201 }
202 catch (std::bad_variant_access const&)
203 {
204 OGS_FATAL(
205 "The value of {:s} is not of the requested type '{:s}' but a "
206 "{:s}.",
207 description(),
208 typeid(T).name(),
209 property_data_type_names_[value(variable_array, pos, t, dt)
210 .index()]);
211 }
212 }
213
214 template <typename T>
215 T dValue(VariableArray const& variable_array,
216 VariableArray const& variable_array_prev, Variable const variable,
217 ParameterLib::SpatialPosition const& pos, double const t,
218 double const dt) const
219 {
220 try
221 {
222#ifndef NDEBUG
223 property_used = true;
224#endif
225 return std::get<T>(dValue(variable_array, variable_array_prev,
226 variable, pos, t, dt));
227 }
228 catch (std::bad_variant_access const&)
229 {
230 OGS_FATAL(
231 "The first derivative value of {:s} is not of the requested "
232 "type '{:s}' but a {:s}.",
233 description(),
234 typeid(T).name(),
236 [dValue(variable_array, variable, pos, t, dt).index()]);
237 }
238 }
239 template <typename T>
240 T dValue(VariableArray const& variable_array, Variable const variable,
241 ParameterLib::SpatialPosition const& pos, double const t,
242 double const dt) const
243 {
244 try
245 {
246#ifndef NDEBUG
247 property_used = true;
248#endif
249 return std::get<T>(dValue(variable_array, variable, pos, t, dt));
250 }
251 catch (std::bad_variant_access const&)
252 {
253 OGS_FATAL(
254 "The first derivative value of {:s} is not of the requested "
255 "type '{:s}' but a {:s}.",
256 description(),
257 typeid(T).name(),
259 [dValue(variable_array, variable, pos, t, dt).index()]);
260 }
261 }
262 template <typename T>
263 T d2Value(VariableArray const& variable_array, Variable const& variable1,
264 Variable const& variable2,
265 ParameterLib::SpatialPosition const& pos, double const t,
266 double const dt) const
267 {
268 try
269 {
270#ifndef NDEBUG
271 property_used = true;
272#endif
273 return std::get<T>(
274 d2Value(variable_array, variable1, variable2, pos, t, dt));
275 }
276 catch (std::bad_variant_access const&)
277 {
278 OGS_FATAL(
279 "The second derivative value of {:s} is not of the requested "
280 "type '{:s}' but a {:s}.",
281 description(),
282 typeid(T).name(),
283 property_data_type_names_[d2Value(variable_array, variable1,
284 variable2, pos, t, dt)
285 .index()]);
286 }
287 }
288
289protected:
290 std::string name_;
297 std::variant<Medium*, Phase*, Component*> scale_;
298
299private:
300 virtual void checkScale() const
301 {
302 // Empty check for properties which can be defined on every scale,
303 // medium, phase or component
304 }
305 std::string description() const;
306#ifndef NDEBUG
307 mutable bool property_used = false;
308#endif
309
310private:
312 static constexpr std::array property_data_type_names_ = {
313 "scalar", "2-vector", "3-vector",
314 "2x2-matrix", "3x3-matrix", "2D-Kelvin vector",
315 "3D-Kelvin vector", "dynamic matrix type"};
316 static_assert(property_data_type_names_.size() ==
317 std::variant_size_v<PropertyDataType>,
318 "The array of property data type names has different size "
319 "than the PropertyDataType variant type.");
320};
321
323 PropertyArray& properties,
324 PropertyArray& new_properties,
325 std::variant<Medium*, Phase*, Component*>
326 scale_pointer)
327{
328 for (std::size_t i = 0; i < properties.size(); ++i)
329 {
330 if (new_properties[i] != nullptr)
331 {
332 properties[i] = std::move(new_properties[i]);
333 properties[i]->setScale(scale_pointer);
334 }
335 }
336}
337
339 PropertyArray& properties,
340 std::vector<std::unique_ptr<Phase>> const& phases)
341{
342 for (auto& p : properties)
343 {
344 if (p != nullptr)
345 {
346 p->setProperties(phases);
347 }
348 }
349}
350
351} // namespace MaterialPropertyLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
virtual void checkScale() const
Definition Property.h:300
virtual void setProperties(std::vector< std::unique_ptr< Phase > > const &phases)
Default implementation:
Definition Property.cpp:145
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:131
std::string description() const
Definition Property.cpp:151
static constexpr std::array property_data_type_names_
Corresponds to the PropertyDataType.
Definition Property.h:312
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:215
T value(VariableArray const &variable_array, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition Property.h:191
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:263
PropertyDataType value_
The single value of a property.
Definition Property.h:292
T dValue(VariableArray const &variable_array, Variable const variable, ParameterLib::SpatialPosition const &pos, double const t, double const dt) const
Definition Property.h:240
PropertyDataType dvalue_
Definition Property.h:293
T initialValue(ParameterLib::SpatialPosition const &pos, double const t) const
Definition Property.h:125
virtual PropertyDataType value() const
Definition Property.cpp:76
void setScale(std::variant< Medium *, Phase *, Component * > scale)
Definition Property.h:118
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:165
std::variant< Medium *, Phase *, Component * > scale_
Definition Property.h:297
virtual PropertyDataType initialValue(ParameterLib::SpatialPosition const &pos, double const t) const
Definition Property.cpp:69
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:105
void overwriteExistingProperties(PropertyArray &properties, PropertyArray &new_properties, std::variant< Medium *, Phase *, Component * > scale_pointer)
Definition Property.h:322
PropertyDataType fromVector(std::vector< double > const &values)
Definition Property.cpp:23
std::array< std::unique_ptr< Property >, PropertyType::number_of_properties > PropertyArray
void updatePropertiesForAllPhases(PropertyArray &properties, std::vector< std::unique_ptr< Phase > > const &phases)
Definition Property.h:338
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 >, Eigen::MatrixXd > PropertyDataType
Definition Property.h:31