OGS
TimeDependentHeterogeneousParameter.cpp
Go to the documentation of this file.
1
12
13#include "BaseLib/ConfigTree.h"
14#include "BaseLib/Error.h"
15#include "Utils.h"
16
17namespace ParameterLib
18{
20 std::string name,
21 std::vector<PairTimeParameterName>
22 time_parameter_name_mapping)
23 : Parameter<double>(std::move(name), nullptr),
24 _time_parameter_name_mapping(std::move(time_parameter_name_mapping))
25{
26}
27
29{
30 return _time_parameter_mapping[0].second->getNumberOfGlobalComponents();
31}
32
34{
35 return true;
36}
37
39 double const t, SpatialPosition const& pos) const
40{
41 // No local coordinate transformation here, which might happen twice
42 // otherwise.
43 assert(!this->_coordinate_system ||
44 "Coordinate system not expected to be set for curve scaled "
45 "parameters.");
46 if (t < _time_parameter_mapping[0].first)
47 {
48 return _time_parameter_mapping[0].second->operator()(t, pos);
49 }
50 if (_time_parameter_mapping.back().first <= t)
51 {
52 return _time_parameter_mapping.back().second->operator()(t, pos);
53 }
54 std::size_t k(1);
55 for (; k < _time_parameter_mapping.size(); ++k)
56 {
57 if (_time_parameter_mapping[k - 1].first <= t &&
58 t < _time_parameter_mapping[k].first)
59 {
60 break;
61 }
62 }
63 auto const t0 = _time_parameter_mapping[k - 1].first;
64 auto const t1 = _time_parameter_mapping[k].first;
65 auto const alpha = (t - t0) / (t1 - t0);
66
67 auto r0 = _time_parameter_mapping[k - 1].second->operator()(t, pos);
68 std::transform(r0.begin(), r0.end(), r0.begin(),
69 [alpha](auto const& v) { return (1 - alpha) * v; });
70 auto r1 = _time_parameter_mapping[k].second->operator()(t, pos);
71 std::transform(r1.begin(), r1.end(), r1.begin(),
72 [alpha](auto const& v) { return alpha * v; });
73 std::transform(r0.begin(), r0.end(), r1.begin(), r0.begin(),
74 [](auto const& v0, auto const& v1) { return v0 + v1; });
75 return r0;
76}
77
79 std::vector<std::unique_ptr<ParameterBase>> const& parameters)
80{
81 DBUG("TimeDependentHeterogeneousParameter init {:d} time series entries.",
83 for (auto const& time_parameter_map : _time_parameter_name_mapping)
84 {
85 auto parameter =
86 &findParameter<double>(time_parameter_map.second, parameters, 0);
87 _time_parameter_mapping.emplace_back(time_parameter_map.first,
88 parameter);
89 }
90
91 // check that all parameters have the same number of components
92 auto const n =
93 _time_parameter_mapping[0].second->getNumberOfGlobalComponents();
94 if (!std::all_of(_time_parameter_mapping.begin(),
96 [n](auto const p)
97 { return n == p.second->getNumberOfGlobalComponents(); }))
98 {
100 "All referenced parameters in time dependent heterogeneous "
101 "parameter '{:s}' have to have the same number of components.",
102 name);
103 }
104}
105
106std::unique_ptr<ParameterBase> createTimeDependentHeterogeneousParameter(
107 std::string const& name, BaseLib::ConfigTree const& config)
108{
110 config.checkConfigParameter("type", "TimeDependentHeterogeneousParameter");
111 auto const time_series_config =
113 config.getConfigSubtree("time_series");
114
115 std::vector<TimeDependentHeterogeneousParameter::PairTimeParameterName>
116 time_series;
118 for (auto const p : time_series_config.getConfigSubtreeList("pair"))
119 {
121 auto time = p.getConfigParameter<double>("time");
122 auto parameter_name =
124 p.getConfigParameter<std::string>("parameter_name");
125 time_series.emplace_back(time, parameter_name);
126 }
127
128 if (time_series.empty())
129 {
130 OGS_FATAL(
131 "Time dependent heterogeneous parameter '{:s}' doesn't contain "
132 "necessary time series data.",
133 name);
134 }
135
136 if (!std::is_sorted(
137 time_series.begin(), time_series.end(),
139 p0,
141 p1) { return p0.first < p1.first; }))
142 {
143 OGS_FATAL(
144 "The points in time in the time series '{:s}' aren't in ascending "
145 "order.",
146 name);
147 }
148
149 return std::make_unique<TimeDependentHeterogeneousParameter>(
150 name, std::move(time_series));
151}
152
153} // namespace ParameterLib
#define OGS_FATAL(...)
Definition: Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition: Logging.h:30
ConfigTree getConfigSubtree(std::string const &root) const
Definition: ConfigTree.cpp:162
void checkConfigParameter(std::string const &param, std::string_view const value) const
Definition: ConfigTree.cpp:151
std::vector< double > operator()(double const t, SpatialPosition const &pos) const override
Returns the parameter value at the given time and position.
TimeDependentHeterogeneousParameter(std::string name, std::vector< PairTimeParameterName > time_parameter_name_mapping)
void initialize(std::vector< std::unique_ptr< ParameterBase > > const &parameters) override
std::unique_ptr< ParameterBase > createTimeDependentHeterogeneousParameter(std::string const &name, BaseLib::ConfigTree const &config)
std::string const name
Definition: Parameter.h:73
std::optional< CoordinateSystem > _coordinate_system
Definition: Parameter.h:126