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
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 if (k == _time_parameter_mapping.size())
64 {
66 "Could not find parameter for time {}. There are {:d} "
67 "time-parameter pairs.",
68 t, _time_parameter_mapping.size());
69 }
70 auto const t0 = _time_parameter_mapping[k - 1].first;
71 auto const t1 = _time_parameter_mapping[k].first;
72 auto const alpha = (t - t0) / (t1 - t0);
73
74 auto r0 = _time_parameter_mapping[k - 1].second->operator()(t, pos);
75 std::transform(r0.begin(), r0.end(), r0.begin(),
76 [alpha](auto const& v) { return (1 - alpha) * v; });
77 auto r1 = _time_parameter_mapping[k].second->operator()(t, pos);
78 std::transform(r1.begin(), r1.end(), r1.begin(),
79 [alpha](auto const& v) { return alpha * v; });
80 std::transform(r0.begin(), r0.end(), r1.begin(), r0.begin(),
81 [](auto const& v0, auto const& v1) { return v0 + v1; });
82 return r0;
83}
84
86 std::vector<std::unique_ptr<ParameterBase>> const& parameters)
87{
88 DBUG("TimeDependentHeterogeneousParameter init {:d} time series entries.",
90 for (auto const& time_parameter_map : _time_parameter_name_mapping)
91 {
92 auto parameter =
93 &findParameter<double>(time_parameter_map.second, parameters, 0);
94 _time_parameter_mapping.emplace_back(time_parameter_map.first,
95 parameter);
96 }
97
98 // check that all parameters have the same number of components
99 auto const n =
100 _time_parameter_mapping[0].second->getNumberOfGlobalComponents();
101 if (!std::all_of(_time_parameter_mapping.begin(),
103 [n](auto const p)
104 { return n == p.second->getNumberOfGlobalComponents(); }))
105 {
106 OGS_FATAL(
107 "All referenced parameters in time dependent heterogeneous "
108 "parameter '{:s}' have to have the same number of components.",
109 name);
110 }
111}
112
113std::unique_ptr<ParameterBase> createTimeDependentHeterogeneousParameter(
114 std::string const& name, BaseLib::ConfigTree const& config)
115{
117 config.checkConfigParameter("type", "TimeDependentHeterogeneousParameter");
118 auto const time_series_config =
120 config.getConfigSubtree("time_series");
121
122 std::vector<TimeDependentHeterogeneousParameter::PairTimeParameterName>
123 time_series;
125 for (auto const p : time_series_config.getConfigSubtreeList("pair"))
126 {
128 auto time = p.getConfigParameter<double>("time");
129 auto parameter_name =
131 p.getConfigParameter<std::string>("parameter_name");
132 time_series.emplace_back(time, parameter_name);
133 }
134
135 if (time_series.empty())
136 {
137 OGS_FATAL(
138 "Time dependent heterogeneous parameter '{:s}' doesn't contain "
139 "necessary time series data.",
140 name);
141 }
142
143 if (!std::is_sorted(
144 time_series.begin(), time_series.end(),
146 p0,
148 p1) { return p0.first < p1.first; }))
149 {
150 OGS_FATAL(
151 "The points in time in the time series '{:s}' aren't in ascending "
152 "order.",
153 name);
154 }
155
156 return std::make_unique<TimeDependentHeterogeneousParameter>(
157 name, std::move(time_series));
158}
159
160} // 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
void checkConfigParameter(std::string const &param, std::string_view const value) const
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)
OGS_NO_DANGLING Parameter< ParameterDataType > & findParameter(std::string const &parameter_name, std::vector< std::unique_ptr< ParameterBase > > const &parameters, int const num_components, MeshLib::Mesh const *const mesh=nullptr)
Definition Utils.h:102
std::string const name
Definition Parameter.h:73
std::optional< CoordinateSystem > _coordinate_system
Definition Parameter.h:126
virtual int getNumberOfGlobalComponents() const =0