parameters.h Source File

CPP API: parameters.h Source File
ode_mseirs4/parameters.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Henrik Zunker
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef ODE_MSEIRS4_PARAMETERS_H
21 #define ODE_MSEIRS4_PARAMETERS_H
22 
25 #include "memilio/utils/logging.h"
26 #include <string>
27 
28 namespace mio
29 {
30 namespace omseirs4
31 {
32 template <typename FP = ScalarType>
33 struct BaseTransmissionRate { // b0
35  static Type get_default()
36  {
37  return Type(1.0);
38  }
39  static std::string name()
40  {
41  return "BaseTransmissionRate";
42  }
43 };
44 
45 template <typename FP = ScalarType>
46 struct SeasonalAmplitude { // b1 in [0,1]
48  static Type get_default()
49  {
50  return Type(0.0);
51  }
52  static std::string name()
53  {
54  return "SeasonalAmplitude";
55  }
56 };
57 
58 template <typename FP = ScalarType>
59 struct SeasonalPhase { // phi in radians
61  static Type get_default()
62  {
63  return Type(0.0);
64  }
65  static std::string name()
66  {
67  return "SeasonalPhase";
68  }
69 };
70 
71 template <typename FP = ScalarType>
72 struct NaturalBirthDeathRate { // mu
74  static Type get_default()
75  {
76  return Type(0.0);
77  }
78  static std::string name()
79  {
80  return "NaturalBirthDeathRate";
81  }
82 };
83 
84 template <typename FP = ScalarType>
87  static Type get_default()
88  {
89  return Type(0.0);
90  }
91  static std::string name()
92  {
93  return "LossMaternalImmunityRate";
94  }
95 };
96 
97 template <typename FP = ScalarType>
98 struct ProgressionRate { // sigma, E->I
100  static Type get_default()
101  {
102  return Type(1.0);
103  }
104  static std::string name()
105  {
106  return "ProgressionRate";
107  }
108 };
109 
110 template <typename FP = ScalarType>
111 struct RecoveryRate { // nu, I->R
113  static Type get_default()
114  {
115  return Type(1.0);
116  }
117  static std::string name()
118  {
119  return "RecoveryRate";
120  }
121 };
122 
123 template <typename FP = ScalarType>
124 struct ImmunityWaningRate { // gamma, R -> S stages
126  static Type get_default()
127  {
128  return Type(0.0);
129  }
130  static std::string name()
131  {
132  return "ImmunityWaningRate";
133  }
134 };
135 
136 template <typename FP = ScalarType>
137 struct Beta2Factor { // f2 multiplier for beta1
139  static Type get_default()
140  {
141  return Type(0.5);
142  }
143  static std::string name()
144  {
145  return "Beta2Factor";
146  }
147 };
148 
149 template <typename FP = ScalarType>
150 struct Beta3Factor { // f3 multiplier for beta1
152  static Type get_default()
153  {
154  return Type(0.35);
155  }
156  static std::string name()
157  {
158  return "Beta3Factor";
159  }
160 };
161 
162 template <typename FP = ScalarType>
163 struct Beta4Factor { // f4 multiplier for beta1
165  static Type get_default()
166  {
167  return Type(0.25);
168  }
169  static std::string name()
170  {
171  return "Beta4Factor";
172  }
173 };
174 
175 template <typename FP = ScalarType>
180 
181 template <typename FP = ScalarType>
182 class Parameters : public ParametersBase<FP>
183 {
184 public:
186  : ParametersBase<FP>()
187  {
188  }
189 
191  {
192  bool corrected = false;
193  auto clamp_nonneg = [&](auto& v) {
194  if (v < 0) {
195  if (v > -1e-10) {
196  log_warning("Constraint check: Parameter was found negative. Changed from {} to {} ", v, 0);
197  }
198  else {
199  log_error("Constraint check: Parameter was found negative. Changed from {} to {} ", v, 0);
200  }
201  v = 0;
202  corrected = true;
203  }
204  };
205  clamp_nonneg(this->template get<BaseTransmissionRate<FP>>());
206  clamp_nonneg(this->template get<SeasonalAmplitude<FP>>());
207  if (this->template get<SeasonalAmplitude<FP>>() > 1) {
208  this->template get<SeasonalAmplitude<FP>>() = 1;
209  corrected = true;
210  }
211  clamp_nonneg(this->template get<NaturalBirthDeathRate<FP>>());
212  clamp_nonneg(this->template get<LossMaternalImmunityRate<FP>>());
213  clamp_nonneg(this->template get<ProgressionRate<FP>>());
214  clamp_nonneg(this->template get<RecoveryRate<FP>>());
215  clamp_nonneg(this->template get<ImmunityWaningRate<FP>>());
216  clamp_nonneg(this->template get<Beta2Factor<FP>>());
217  clamp_nonneg(this->template get<Beta3Factor<FP>>());
218  clamp_nonneg(this->template get<Beta4Factor<FP>>());
219  return corrected;
220  }
221 
222  bool check_constraints() const
223  {
224  bool violated = false;
225 
226  auto check_nonneg = [&](auto& v) {
227  if (v < 0) {
228  log_error("Constraint check: Parameter was found to be {} and should not be smaller than {}.", v, 0);
229  violated = true;
230  }
231  };
232  check_nonneg(this->template get<BaseTransmissionRate<FP>>());
233  check_nonneg(this->template get<SeasonalAmplitude<FP>>());
234  check_nonneg(this->template get<NaturalBirthDeathRate<FP>>());
235  check_nonneg(this->template get<LossMaternalImmunityRate<FP>>());
236  check_nonneg(this->template get<ProgressionRate<FP>>());
237  check_nonneg(this->template get<RecoveryRate<FP>>());
238  check_nonneg(this->template get<ImmunityWaningRate<FP>>());
239  check_nonneg(this->template get<Beta2Factor<FP>>());
240  check_nonneg(this->template get<Beta3Factor<FP>>());
241  check_nonneg(this->template get<Beta4Factor<FP>>());
242  if (this->template get<SeasonalAmplitude<FP>>() < 0 || this->template get<SeasonalAmplitude<FP>>() > 1) {
243  log_error("Constraint check: Parameter SeasonalAmplitude was found to be out of [0, 1].");
244  violated = true;
245  }
246  return violated;
247  }
248 
249 private:
251  : ParametersBase<FP>(std::move(base))
252  {
253  }
254 
255 public:
260  template <class IOContext>
261  static IOResult<Parameters> deserialize(IOContext& io)
262  {
263  BOOST_OUTCOME_TRY(auto&& base, ParametersBase<FP>::deserialize(io));
264  return success(Parameters(std::move(base)));
265  }
266 };
267 
268 } // namespace omseirs4
269 } // namespace mio
270 
271 #endif // ODE_MSEIRS4_PARAMETERS_H
a set of parameters defined at compile time
Definition: parameter_set.h:205
const ParameterTagTraits< Tag >::Type & get() const
get value of a parameter
Definition: parameter_set.h:262
Definition: ode_mseirs4/parameters.h:183
Parameters(ParametersBase< FP > &&base)
Definition: ode_mseirs4/parameters.h:250
static IOResult< Parameters > deserialize(IOContext &io)
deserialize an object of this class.
Definition: ode_mseirs4/parameters.h:261
bool apply_constraints()
Definition: ode_mseirs4/parameters.h:190
bool check_constraints() const
Definition: ode_mseirs4/parameters.h:222
Parameters()
Definition: ode_mseirs4/parameters.h:185
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
void log_warning(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:112
auto success()
Create an object that is implicitly convertible to a succesful IOResult<void>.
Definition: io.h:359
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353
Definition: io.h:94
Definition: ode_mseirs4/parameters.h:33
static std::string name()
Definition: ode_mseirs4/parameters.h:39
static Type get_default()
Definition: ode_mseirs4/parameters.h:35
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:34
Definition: ode_mseirs4/parameters.h:137
static Type get_default()
Definition: ode_mseirs4/parameters.h:139
static std::string name()
Definition: ode_mseirs4/parameters.h:143
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:138
Definition: ode_mseirs4/parameters.h:150
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:151
static std::string name()
Definition: ode_mseirs4/parameters.h:156
static Type get_default()
Definition: ode_mseirs4/parameters.h:152
Definition: ode_mseirs4/parameters.h:163
static Type get_default()
Definition: ode_mseirs4/parameters.h:165
static std::string name()
Definition: ode_mseirs4/parameters.h:169
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:164
Definition: ode_mseirs4/parameters.h:124
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:125
static std::string name()
Definition: ode_mseirs4/parameters.h:130
static Type get_default()
Definition: ode_mseirs4/parameters.h:126
Definition: ode_mseirs4/parameters.h:85
static std::string name()
Definition: ode_mseirs4/parameters.h:91
static Type get_default()
Definition: ode_mseirs4/parameters.h:87
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:86
Definition: ode_mseirs4/parameters.h:72
static Type get_default()
Definition: ode_mseirs4/parameters.h:74
static std::string name()
Definition: ode_mseirs4/parameters.h:78
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:73
Definition: ode_mseirs4/parameters.h:98
static Type get_default()
Definition: ode_mseirs4/parameters.h:100
static std::string name()
Definition: ode_mseirs4/parameters.h:104
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:99
Definition: ode_mseirs4/parameters.h:111
static std::string name()
Definition: ode_mseirs4/parameters.h:117
static Type get_default()
Definition: ode_mseirs4/parameters.h:113
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:112
Definition: ode_mseirs4/parameters.h:46
static std::string name()
Definition: ode_mseirs4/parameters.h:52
static Type get_default()
Definition: ode_mseirs4/parameters.h:48
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:47
Definition: ode_mseirs4/parameters.h:59
UncertainValue< FP > Type
Definition: ode_mseirs4/parameters.h:60
static std::string name()
Definition: ode_mseirs4/parameters.h:65
static Type get_default()
Definition: ode_mseirs4/parameters.h:61