parameters.h Source File

CPP API: parameters.h Source File
ide_secir/parameters.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Anna Wendler, Lena Ploetzke
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 IDE_SECIR_PARAMS_H
21 #define IDE_SECIR_PARAMS_H
22 
23 #include "memilio/config.h"
28 #include "memilio/math/eigen.h"
29 #include "memilio/math/smoother.h"
33 
34 #include <memory>
35 #include <cstddef>
36 #include <vector>
37 
38 namespace mio
39 {
40 namespace isecir
41 {
42 
43 /**********************************************
44 * Define Parameters of the IDE-SECIHURD model *
45 **********************************************/
46 
57 
60  {
61  SmootherCosine smoothcos(2.0);
62  StateAgeFunctionWrapper<ScalarType> delaydistribution(smoothcos);
63  std::vector<StateAgeFunctionWrapper<ScalarType>> state_age_function_vector((int)InfectionTransition::Count,
64  delaydistribution);
65  return Type(size, state_age_function_vector);
66  }
67 
68  static std::string name()
69  {
70  return "TransitionDistributions";
71  }
72 };
73 
78  /*For consistency, also define TransitionProbabilities for each transition in #InfectionTransition.
79  Transition Probabilities should be set to 1 if there is no possible other flow from starting compartment.*/
82  {
83  std::vector<ScalarType> probs((int)InfectionTransition::Count, 0.5);
84  // Set the following probablities to 1 as there is no other option to go anywhere else.
85  probs[Eigen::Index(InfectionTransition::SusceptibleToExposed)] = 1;
86  probs[Eigen::Index(InfectionTransition::ExposedToInfectedNoSymptoms)] = 1;
87  // Set default to 0.
88  probs[Eigen::Index(InfectionTransition::InfectedSevereToDead)] = 0;
89  return Type(size, probs);
90  }
91 
92  static std::string name()
93  {
94  return "TransitionProbabilities";
95  }
96 };
97 
103 
105  {
106  ContactMatrixGroup<ScalarType> contact_matrix =
107  ContactMatrixGroup<ScalarType>(1, static_cast<Eigen::Index>((size_t)size));
108  contact_matrix[0] = mio::ContactMatrix<ScalarType>(Eigen::MatrixX<ScalarType>::Constant(
109  static_cast<Eigen::Index>((size_t)size), static_cast<Eigen::Index>((size_t)size), 10.));
110  return Type(contact_matrix);
111  }
112  static std::string name()
113  {
114  return "ContactPatterns";
115  }
116 };
117 
124  {
125  ConstantFunction constfunc(1.0);
126  return Type(size, constfunc);
127  }
128  static std::string name()
129  {
130  return "TransmissionProbabilityOnContact";
131  }
132 };
133 
138 
141  {
142  ConstantFunction constfunc(1.0);
143  return Type(size, constfunc);
144  }
145  static std::string name()
146  {
147  return "RelativeTransmissionNoSymptoms";
148  }
149 };
150 
157  {
158  ConstantFunction constfunc(1.0);
159  return Type(size, constfunc);
160  }
161  static std::string name()
162  {
163  return "RiskOfInfectionFromSymptomatic";
164  }
165 };
166 
176 struct StartDay {
177  using Type = ScalarType;
179  {
180  return 0.;
181  }
182  static std::string name()
183  {
184  return "StartDay";
185  }
186 };
187 
193 struct Seasonality {
194  using Type = ScalarType;
196  {
197  return Type(0.);
198  }
199  static std::string name()
200  {
201  return "Seasonality";
202  }
203 };
204 
205 // Define Parameterset for IDE-SECIR model.
209 
214 {
215 public:
216  Parameters(AgeGroup num_agegroups)
217  : ParametersBase(num_agegroups)
218  , m_num_groups{num_agegroups}
219  {
220  }
221 
227  bool check_constraints() const
228  {
229  for (AgeGroup group = AgeGroup(0); group < m_num_groups; ++group) {
230  // For parameters potentially depending on the infectious age, values are checked
231  // equidistantly on a realistic maximum window.
232  // Please note that this is an incomplete check on correctness.
233  size_t infectious_window_check = 50; // parameter defining minimal window on x-axis
234  for (size_t i = 0; i < infectious_window_check; i++) {
235  if (this->get<TransmissionProbabilityOnContact>()[group].eval((ScalarType)i) < 0.0 ||
236  this->get<TransmissionProbabilityOnContact>()[group].eval((ScalarType)i) > 1.0) {
237  log_error("Constraint check: TransmissionProbabilityOnContact smaller {} or larger {} at some "
238  "time {}",
239  0, 1, i);
240  return true;
241  }
242  }
243 
244  for (size_t i = 0; i < infectious_window_check; i++) {
245  if (this->get<RelativeTransmissionNoSymptoms>()[group].eval((ScalarType)i) < 0.0 ||
246  this->get<RelativeTransmissionNoSymptoms>()[group].eval((ScalarType)i) > 1.0) {
247  log_error("Constraint check: RelativeTransmissionNoSymptoms smaller {} or larger {} at some "
248  "time {}",
249  0, 1, i);
250  return true;
251  }
252  }
253 
254  for (size_t i = 0; i < infectious_window_check; i++) {
255  if (this->get<RiskOfInfectionFromSymptomatic>()[group].eval((ScalarType)i) < 0.0 ||
256  this->get<RiskOfInfectionFromSymptomatic>()[group].eval((ScalarType)i) > 1.0) {
257  log_error("Constraint check: RiskOfInfectionFromSymptomatic smaller {} or larger {} at some "
258  "time {}",
259  0, 1, i);
260  return true;
261  }
262  }
263 
264  for (size_t i = 0; i < (int)InfectionTransition::Count; i++) {
265  if (this->get<TransitionProbabilities>()[group][i] < 0.0 ||
266  this->get<TransitionProbabilities>()[group][i] > 1.0) {
267  log_error("Constraint check: One parameter in TransitionProbabilities smaller {} or larger {}", 0,
268  1);
269  return true;
270  }
271  }
272 
274  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::SusceptibleToExposed], 1.0,
275  1e-14)) {
276  log_error("Constraint check: Parameter transition probability for SusceptibleToExposed unequal to {}",
277  1);
278  return true;
279  }
280 
282  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::ExposedToInfectedNoSymptoms],
283  1.0, 1e-14)) {
284  log_error("Constraint check: Parameter transition probability for ExposedToInfectedNoSymptoms unequal "
285  "to {}",
286  1);
287  return true;
288  }
289 
290  if (!floating_point_equal(this->get<TransitionProbabilities>()[group][(
292  this->get<TransitionProbabilities>()[group][(
294  1.0, 1e-14)) {
295  log_error(
296  "Constraint check: Sum of transition probability for InfectedNoSymptomsToInfectedSymptoms and "
297  "InfectedNoSymptomsToRecovered not equal to {}",
298  1);
299  return true;
300  }
301 
302  if (!floating_point_equal(this->get<TransitionProbabilities>()[group][(
304  this->get<TransitionProbabilities>()[group][(
306  1.0, 1e-14)) {
307  log_error("Constraint check: Sum of transition probability for InfectedSymptomsToInfectedSevere and "
308  "InfectedSymptomsToRecovered not equal to {}",
309  1);
310  return true;
311  }
312 
314  this->get<TransitionProbabilities>()[group]
316  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::InfectedSevereToDead] +
317  this->get<TransitionProbabilities>()[group]
319  1.0, 1e-14)) {
320  log_error("Constraint check: Sum of transition probability for InfectedSevereToInfectedCritical, "
321  "InfectedSevereToDead and InfectedSevereToRecovered not equal to {}",
322  1);
323  return true;
324  }
325 
327  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::InfectedCriticalToDead] +
328  this->get<TransitionProbabilities>()[group]
330  1.0, 1e-14)) {
331  log_error("Constraint check: Sum of transition probability for InfectedCriticalToDead and "
332  "InfectedCriticalToRecovered not equal to {}",
333  1);
334  return true;
335  }
336 
337  /* The first entry of TransitionDistributions is not checked because the distribution S->E is never used
338  (and it makes no sense to use the distribution). The support does not need to be valid.*/
339  for (size_t i = 1; i < (int)InfectionTransition::Count; i++) {
340  if (floating_point_less(this->get<TransitionDistributions>()[group][i].get_support_max(10), 0.0,
341  1e-14)) {
342  log_error("Constraint check: One function in TransitionDistributions has invalid support.");
343  return true;
344  }
345  }
346  }
347 
348  if (this->get<Seasonality>() < 0.0 || this->get<Seasonality>() > 0.5) {
349  log_warning("Constraint check: Parameter Seasonality should lie between {} and {}", 0.0, 0.5);
350  return true;
351  }
352 
353  return false;
354  }
355 
360  template <class IOContext>
361  static IOResult<Parameters> deserialize(IOContext& io)
362  {
363  BOOST_OUTCOME_TRY(auto&& base, ParametersBase::deserialize(io));
364  return success(Parameters(std::move(base)));
365  }
366 
367 private:
369  : ParametersBase(std::move(base))
370  , m_num_groups(get<ContactPatterns>().get_cont_freq_mat().get_num_groups())
371  {
372  }
373 
374 private:
376 };
377 } // namespace isecir
378 
379 } // namespace mio
380 
381 #endif // IDE_SECIR_PARAMS_H
represents a collection of contact frequency matrices that whose sum is the total number of contacts.
Definition: contact_matrix.h:536
represents time dependent contact frequencies between groups.
Definition: contact_matrix.h:505
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
static IOResult< ParameterSet > deserialize(IOContext &io)
deserialize an object of this class.
Definition: parameter_set.h:393
const ParameterTagTraits< Tag >::Type & get() const
get value of a parameter
Definition: parameter_set.h:262
The UncertainContactMatrix class consists of a ContactMatrix with fixed baseline and uncertain Dampin...
Definition: uncertain_matrix.h:43
Parameters of an age-resolved SECIR/SECIHURD model.
Definition: ide_secir/parameters.h:214
static IOResult< Parameters > deserialize(IOContext &io)
deserialize an object of this class.
Definition: ide_secir/parameters.h:361
bool check_constraints() const
Checks whether all Parameters satisfy their corresponding constraints and logs an error.
Definition: ide_secir/parameters.h:227
Parameters(ParametersBase &&base)
Definition: ide_secir/parameters.h:368
AgeGroup m_num_groups
Definition: ide_secir/parameters.h:375
Parameters(AgeGroup num_agegroups)
Definition: ide_secir/parameters.h:216
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
ApplyResultT< F, T... > eval(F f, const IOResult< T > &... rs)
Evaluates a function f using values of the given IOResults as arguments, assumes all IOResults are su...
Definition: io.h:449
int size(Comm comm)
Return the size of the given communicator.
Definition: miompi.cpp:75
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
bool floating_point_equal(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values for equality with tolerances.
Definition: floating_point.h:57
auto success()
Create an object that is implicitly convertible to a succesful IOResult<void>.
Definition: io.h:360
bool floating_point_less(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values with tolerances.
Definition: floating_point.h:78
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
Class that defines a constant function.
Definition: state_age_function.h:631
Class that defines an smoother_cosine function depending on the state age.
Definition: state_age_function.h:421
Wrapper around StateAgeFunction so that one can work with an arbitrary StateAgeFunction.
Definition: state_age_function.h:831
The contact patterns within the society are modelled using an UncertainContactMatrix.
Definition: ide_secir/parameters.h:101
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:104
UncertainContactMatrix< ScalarType > Type
Definition: ide_secir/parameters.h:102
static std::string name()
Definition: ide_secir/parameters.h:112
The relative InfectedNoSymptoms infectability.
Definition: ide_secir/parameters.h:137
CustomIndexArray< StateAgeFunctionWrapper< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:139
static std::string name()
Definition: ide_secir/parameters.h:145
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:140
The risk of infection from symptomatic cases in the SECIR model.
Definition: ide_secir/parameters.h:154
CustomIndexArray< StateAgeFunctionWrapper< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:155
static std::string name()
Definition: ide_secir/parameters.h:161
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:156
The seasonality parameter k in the IDE-SECIR model.
Definition: ide_secir/parameters.h:193
static std::string name()
Definition: ide_secir/parameters.h:199
ScalarType Type
Definition: ide_secir/parameters.h:194
static Type get_default(AgeGroup)
Definition: ide_secir/parameters.h:195
Sets the day in a year at which a simulation with an IDE-SECIR model is started.
Definition: ide_secir/parameters.h:176
static Type get_default(AgeGroup)
Definition: ide_secir/parameters.h:178
static std::string name()
Definition: ide_secir/parameters.h:182
ScalarType Type
Definition: ide_secir/parameters.h:177
Transition distribution for each transition in InfectionTransition.
Definition: ide_secir/parameters.h:56
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:59
CustomIndexArray< std::vector< StateAgeFunctionWrapper< ScalarType > >, AgeGroup > Type
Definition: ide_secir/parameters.h:58
static std::string name()
Definition: ide_secir/parameters.h:68
Defines the probability for each possible transition to take this flow/transition.
Definition: ide_secir/parameters.h:77
CustomIndexArray< std::vector< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:80
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:81
static std::string name()
Definition: ide_secir/parameters.h:92
Probability of getting infected from a contact.
Definition: ide_secir/parameters.h:121
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:123
static std::string name()
Definition: ide_secir/parameters.h:128
CustomIndexArray< StateAgeFunctionWrapper< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:122