OGS
Linear.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <numeric>
7
9{
11 VariableArray const& variable_array,
13 double const t) const
14{
15 double x = valueUnclamped(variable_array, pos, t);
16
17 if (min)
18 {
19 x = std::max(x, *min);
20 }
21 if (max)
22 {
23 x = std::min(x, *max);
24 }
25
26 return x;
27}
28
30 VariableArray const& variable_array,
32 double const t) const
33{
34 if (auto* var_ptr = std::get_if<Variable>(&type))
35 {
36 return std::get<double>(variable_array[*var_ptr]);
37 }
38
39 if (auto* str_ptr = std::get_if<std::string>(&type))
40 {
41 if (*str_ptr == "t")
42 {
43 return t;
44 }
45
46 if (*str_ptr == "x")
47 {
48 return pos.getCoordinates().value()[0];
49 }
50
51 if (*str_ptr == "y")
52 {
53 return pos.getCoordinates().value()[1];
54 }
55
56 if (*str_ptr == "z")
57 {
58 return pos.getCoordinates().value()[2];
59 }
60
61 OGS_FATAL("Unknown independent variable {:s} for a linear property.",
62 *str_ptr)
63 }
64
66 "Could not convert independent_variable neither to a Variable nor "
67 "to a std::string.");
68}
69
70Linear::Linear(std::string name,
71 PropertyDataType const& property_reference_value,
72 std::vector<IndependentVariable> const& vs)
74{
75 name_ = std::move(name);
76 value_ = property_reference_value;
77}
78
81 double const t, double const /*dt*/) const
82{
83 auto calculate_linearized_ratio =
84 [&variable_array, pos, t](double const initial_linearized_ratio,
85 auto const& iv)
86 {
87 double const x = iv.valueClamped(variable_array, pos, t);
88
89 return initial_linearized_ratio +
90 std::get<double>(iv.slope) *
91 (x - std::get<double>(iv.reference_condition));
92 };
93
94 double const linearized_ratio_to_reference_value =
95 std::accumulate(independent_variables_.begin(),
97 1.0,
98 calculate_linearized_ratio);
99
100 return std::get<double>(value_) * linearized_ratio_to_reference_value;
101}
102
104 Variable const variable,
106 double const t, double const /*dt*/) const
107{
108 auto const independent_variable = std::find_if(
111 [&variable](auto const& iv) -> bool
112 {
113 if (auto const* var_ptr = std::get_if<Variable>(&iv.type))
114 {
115 return *var_ptr == variable;
116 }
117 return false;
118 });
119
120 auto const zero = decltype(value_){};
121 if (independent_variable == independent_variables_.end())
122 {
123 return zero;
124 }
125
126 auto const& iv = *independent_variable;
127
128 double const x = iv.valueUnclamped(variable_array, pos, t);
129
130 if (iv.min && x < *iv.min)
131 {
132 return zero;
133 }
134 if (iv.max && x > *iv.max)
135 {
136 return zero;
137 }
138
139 return std::get<double>(value_) * std::get<double>(iv.slope);
140}
141
143 Variable const /*pv1*/, Variable const /*pv2*/,
144 ParameterLib::SpatialPosition const& /*pos*/,
145 double const /*t*/, double const /*dt*/) const
146{
147 return decltype(value_){};
148}
149
150} // namespace MaterialPropertyLib
#define OGS_FATAL(...)
Definition Error.h:19
std::vector< IndependentVariable > const independent_variables_
Definition Linear.h:66
PropertyDataType d2Value(VariableArray const &variable_array, Variable const pv1, Variable const pv2, ParameterLib::SpatialPosition const &, double const, double const) const override
Definition Linear.cpp:142
PropertyDataType dValue(VariableArray const &variable_array, Variable const variable, ParameterLib::SpatialPosition const &, double const, double const) const override
Definition Linear.cpp:103
Linear(std::string name, PropertyDataType const &property_reference_value, std::vector< IndependentVariable > const &vs)
Definition Linear.cpp:70
PropertyDataType value_
The single value of a property.
virtual PropertyDataType value() const
std::optional< MathLib::Point3d > const getCoordinates() const
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
double valueClamped(VariableArray const &variable_array, ParameterLib::SpatialPosition const &pos, double const t) const
Definition Linear.cpp:10
std::optional< double > max
Definition Linear.h:18
std::optional< double > min
Definition Linear.h:17
double valueUnclamped(VariableArray const &variable_array, ParameterLib::SpatialPosition const &pos, double const t) const
Definition Linear.cpp:29