parameters.h Source File

CPP API: parameters.h Source File
ode_seir_metapop/parameters.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Carlotta Gerstein
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 ODESEIRMETAPOP_PARAMETERS_H
21 #define ODESEIRMETAPOP_PARAMETERS_H
22 
29 
30 #include <vector>
31 
32 namespace mio
33 {
34 namespace oseirmetapop
35 {
36 
38 
39 /****************************************************
40 * Define Parameters of the SEIR model with mobility *
41 ****************************************************/
42 
46 template <typename FP = ScalarType>
49  static Type get_default(Region size_region, AgeGroup size_age)
50  {
51  return Type({size_region, size_age}, 1.0);
52  }
53  static std::string name()
54  {
55  return "TransmissionProbabilityOnContact";
56  }
57 };
58 
62 template <typename FP = ScalarType>
63 struct TimeExposed {
65  static Type get_default(Region size_region, AgeGroup size_age)
66  {
67  return Type({size_region, size_age}, 5.2);
68  }
69  static std::string name()
70  {
71  return "TimeExposed";
72  }
73 };
74 
78 template <typename FP = ScalarType>
79 struct TimeInfected {
81  static Type get_default(Region size_region, AgeGroup size_age)
82  {
83  return Type({size_region, size_age}, 6.0);
84  }
85  static std::string name()
86  {
87  return "TimeInfected";
88  }
89 };
90 
95 template <typename FP = ScalarType>
99  {
100  return Type(1, static_cast<Eigen::Index>((size_t)size));
101  }
102  static std::string name()
103  {
104  return "ContactPatterns";
105  }
106 };
107 
113 template <typename FP = ScalarType>
117  {
118  return Type(1, static_cast<Eigen::Index>((size_t)size));
119  }
120  static std::string name()
121  {
122  return "CommutingStrengths";
123  }
124 };
125 
131 template <typename FP = ScalarType>
134  static Type get_default(Region size_regions, AgeGroup size_agegroups)
135  {
136  return Type({size_regions, size_agegroups}, 0.);
137  }
138  static std::string name()
139  {
140  return "PopulationAfterCommuting";
141  }
142 };
143 
144 template <typename FP = ScalarType>
147 
151 template <typename FP = ScalarType>
152 class Parameters : public ParametersBase<FP>
153 {
154 public:
155  Parameters(Region num_regions, AgeGroup num_agegroups)
156  : ParametersBase<FP>(num_regions, num_agegroups)
157  , m_num_regions{num_regions}
158  , m_num_agegroups(num_agegroups)
159  {
160  }
161 
163  {
164  return m_num_regions;
165  }
166 
168  {
169  return m_num_agegroups;
170  }
171 
185  {
186  const FP tol_times = 1e-1;
187  bool corrected = false;
188 
189  for (auto i = AgeGroup(0); i < AgeGroup(m_num_agegroups); i++) {
190  for (auto j = Region(0); j < Region(m_num_regions); j++) {
191  if (this->template get<TimeExposed<FP>>()[{j, i}] < tol_times) {
192  log_warning(
193  "Constraint check: Parameter TimeExposed changed from {} to {}. Please note that "
194  "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
195  "and reset parameters.",
196  this->template get<TimeExposed<FP>>()[{j, i}], tol_times);
197  this->template get<TimeExposed<FP>>()[{j, i}] = tol_times;
198  corrected = true;
199  }
200  if (this->template get<TimeInfected<FP>>()[{j, i}] < tol_times) {
201  log_warning(
202  "Constraint check: Parameter TimeInfected changed from {} to {}. Please note that "
203  "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
204  "and reset parameters.",
205  this->template get<TimeInfected<FP>>()[{j, i}], tol_times);
206  this->template get<TimeInfected<FP>>()[{j, i}] = tol_times;
207  corrected = true;
208  }
209  if (this->template get<TransmissionProbabilityOnContact<FP>>()[{j, i}] < 0.0 ||
210  this->template get<TransmissionProbabilityOnContact<FP>>()[{j, i}] > 1.0) {
211  log_warning("Constraint check: Parameter TransmissionProbabilityOnContact changed from {} to {} ",
212  this->template get<TransmissionProbabilityOnContact<FP>>()[{j, i}], 0.0);
213  this->template get<TransmissionProbabilityOnContact<FP>>() = 0.0;
214  corrected = true;
215  }
216 
217  if (this->template get<PopulationAfterCommuting<FP>>()[{j, i}] <= 0.0) {
218  log_warning(
219  "Constraint check: Parameter PopulationAfterCommuting changed from {} to {}. Please "
220  "note that this only prevents division by zero. Consider to cancel and reset parameters.",
221  this->template get<PopulationAfterCommuting<FP>>()[{j, i}], 1.0);
222  this->template get<PopulationAfterCommuting<FP>>()[{j, i}] = 1.0;
223  corrected = true;
224  }
225  }
226  }
227  if ((this->template get<CommutingStrengths<FP>>()
228  .get_cont_freq_mat()
229  .get_matrix_at(SimulationTime<FP>(0))
230  .rowwise()
231  .sum() -
232  Eigen::VectorXd::Ones((size_t)this->get_num_regions()))
233  .cwiseAbs()
234  .maxCoeff() > 1e-10 ||
235  this->template get<CommutingStrengths<FP>>()
236  .get_cont_freq_mat()
237  .get_matrix_at(SimulationTime<FP>(0))
238  .minCoeff() < 0.0 ||
239  this->template get<CommutingStrengths<FP>>()
240  .get_cont_freq_mat()
241  .get_matrix_at(SimulationTime<FP>(0))
242  .maxCoeff() > 1.0) {
243  log_warning("Constraint check: Parameter CommutingStrengths does not ensure that the number of people "
244  "staying equals the complement of those leaving. Running without commuting.");
245  this->template get<CommutingStrengths<FP>>().get_cont_freq_mat()[0].get_baseline() =
246  Eigen::MatrixXd::Identity((size_t)this->get_num_regions(), (size_t)this->get_num_regions());
247  corrected = true;
248  }
249  return corrected;
250  }
251 
257  bool check_constraints() const
258  {
259  const double tol_times = 1e-1;
260  for (auto i = AgeGroup(0); i < AgeGroup(m_num_agegroups); i++) {
261  for (auto j = Region(0); j < Region(m_num_regions); j++) {
262  if (this->template get<TimeExposed<FP>>()[{j, i}] < tol_times) {
263  log_error(
264  "Constraint check: Parameter TimeExposed {} smaller or equal {}. Please note that "
265  "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
266  "and reset parameters.",
267  this->template get<TimeExposed<FP>>()[{j, i}], 0.0);
268  return true;
269  }
270  if (this->template get<TimeInfected<FP>>()[{j, i}] < tol_times) {
271  log_error(
272  "Constraint check: Parameter TimeInfected {} smaller or equal {}. Please note that "
273  "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
274  "and reset parameters.",
275  this->template get<TimeInfected<FP>>()[{j, i}], 0.0);
276  return true;
277  }
278  if (this->template get<TransmissionProbabilityOnContact<FP>>()[{j, i}] < 0.0 ||
279  this->template get<TransmissionProbabilityOnContact<FP>>()[{j, i}] > 1.0) {
280  log_error("Constraint check: Parameter TransmissionProbabilityOnContact {} smaller {} or "
281  "greater {:.4f}",
282  this->template get<TransmissionProbabilityOnContact<FP>>()[{j, i}], 0.0, 1.0);
283  return true;
284  }
285 
286  if (this->template get<PopulationAfterCommuting<FP>>()[{j, i}] <= 0.0) {
287  log_error("Constraint check: Parameter PopulationAfterCommuting {} smaller or equal {}",
288  this->template get<PopulationAfterCommuting<FP>>()[{j, i}], 0.0);
289  return true;
290  }
291  }
292  }
293  if ((this->template get<CommutingStrengths<FP>>()
294  .get_cont_freq_mat()
295  .get_matrix_at(SimulationTime<FP>(0))
296  .rowwise()
297  .sum() -
298  Eigen::VectorXd::Ones((size_t)this->get_num_regions()))
299  .cwiseAbs()
300  .maxCoeff() > 1e-10 ||
301  this->template get<CommutingStrengths<FP>>()
302  .get_cont_freq_mat()
303  .get_matrix_at(SimulationTime<FP>(0))
304  .minCoeff() < 0.0 ||
305  this->template get<CommutingStrengths<FP>>()
306  .get_cont_freq_mat()
307  .get_matrix_at(SimulationTime<FP>(0))
308  .maxCoeff() > 1.0) {
309  log_error("Constraint check: Parameter CommutingStrengths does not ensure that the number of people "
310  "staying equals the complement of those leaving.");
311  return true;
312  }
313 
314  return false;
315  }
316 
317 private:
319  : ParametersBase<FP>(std::move(base))
322  {
323  }
324 
325 public:
330  template <class IOContext>
331  static IOResult<Parameters> deserialize(IOContext& io)
332  {
333  BOOST_OUTCOME_TRY(auto&& base, ParametersBase<FP>::deserialize(io));
334  return success(Parameters(std::move(base)));
335  }
336 
337 private:
340 };
341 
342 } // namespace oseirmetapop
343 } // namespace mio
344 
345 #endif // SEIRMETAPOP_PARAMETERS_H
A class template for an array with custom indices.
Definition: custom_index_array.h:136
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
A class template for compartment populations.
Definition: populations.h:55
Typesafe wrapper for a floating-point simulation time value (in days).
Definition: damping.h:78
The UncertainContactMatrix class consists of a ContactMatrix with fixed baseline and uncertain Dampin...
Definition: uncertain_matrix.h:43
Parameters of the SEIR metapopulation model.
Definition: ode_seir_metapop/parameters.h:153
AgeGroup m_num_agegroups
Definition: ode_seir_metapop/parameters.h:339
Region get_num_regions() const
Definition: ode_seir_metapop/parameters.h:162
Parameters(Region num_regions, AgeGroup num_agegroups)
Definition: ode_seir_metapop/parameters.h:155
bool check_constraints() const
Checks whether all Parameters satisfy their corresponding constraints and logs an error if constraint...
Definition: ode_seir_metapop/parameters.h:257
bool apply_constraints()
Checks whether all Parameters satisfy their corresponding constraints and applies them,...
Definition: ode_seir_metapop/parameters.h:184
Region m_num_regions
Definition: ode_seir_metapop/parameters.h:338
static IOResult< Parameters > deserialize(IOContext &io)
Deserialize an object of this class.
Definition: ode_seir_metapop/parameters.h:331
AgeGroup get_num_agegroups() const
Definition: ode_seir_metapop/parameters.h:167
Parameters(ParametersBase< FP > &&base)
Definition: ode_seir_metapop/parameters.h:318
int size(Comm comm)
Return the size of the given communicator.
Definition: miompi.cpp:75
mio::regions::Region Region
Definition: ode_seir_metapop/parameters.h:37
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
auto i
Definition: io.h:810
void log_warning(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:126
auto success()
Create an object that is implicitly convertible to a succesful IOResult<void>.
Definition: io.h:360
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:114
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:354
Definition: io.h:95
Typesafe index representing an age group.
Definition: age_group.h:40
The commuting patterns between different Regions are modelled using a ContactMatrix of size n_regions...
Definition: ode_seir_metapop/parameters.h:114
static Type get_default(Region size, AgeGroup)
Definition: ode_seir_metapop/parameters.h:116
UncertainContactMatrix< FP > Type
Definition: ode_seir_metapop/parameters.h:115
static std::string name()
Definition: ode_seir_metapop/parameters.h:120
The contact patterns within the society are modelled using a ContactMatrix.
Definition: ode_seir_metapop/parameters.h:96
UncertainContactMatrix< FP > Type
Definition: ode_seir_metapop/parameters.h:97
static std::string name()
Definition: ode_seir_metapop/parameters.h:102
static Type get_default(Region, AgeGroup size)
Definition: ode_seir_metapop/parameters.h:98
The number of individuals in each Region and AgeGroup if commuting was applied.
Definition: ode_seir_metapop/parameters.h:132
Populations< FP, Region, AgeGroup > Type
Definition: ode_seir_metapop/parameters.h:133
static std::string name()
Definition: ode_seir_metapop/parameters.h:138
static Type get_default(Region size_regions, AgeGroup size_agegroups)
Definition: ode_seir_metapop/parameters.h:134
The latent time in day unit.
Definition: ode_seir_metapop/parameters.h:63
static Type get_default(Region size_region, AgeGroup size_age)
Definition: ode_seir_metapop/parameters.h:65
CustomIndexArray< UncertainValue< FP >, Region, AgeGroup > Type
Definition: ode_seir_metapop/parameters.h:64
static std::string name()
Definition: ode_seir_metapop/parameters.h:69
The infectious time in day unit.
Definition: ode_seir_metapop/parameters.h:79
CustomIndexArray< UncertainValue< FP >, Region, AgeGroup > Type
Definition: ode_seir_metapop/parameters.h:80
static std::string name()
Definition: ode_seir_metapop/parameters.h:85
static Type get_default(Region size_region, AgeGroup size_age)
Definition: ode_seir_metapop/parameters.h:81
Probability of getting infected from a contact.
Definition: ode_seir_metapop/parameters.h:47
static Type get_default(Region size_region, AgeGroup size_age)
Definition: ode_seir_metapop/parameters.h:49
static std::string name()
Definition: ode_seir_metapop/parameters.h:53
CustomIndexArray< UncertainValue< FP >, Region, AgeGroup > Type
Definition: ode_seir_metapop/parameters.h:48
Index for enumerating subregions (cities, counties, etc.) of the modelled area.
Definition: regions.h:37