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  return Type(size, probs);
88  }
89 
90  static std::string name()
91  {
92  return "TransitionProbabilities";
93  }
94 };
95 
101 
103  {
104  ContactMatrixGroup<ScalarType> contact_matrix =
105  ContactMatrixGroup<ScalarType>(1, static_cast<Eigen::Index>((size_t)size));
106  contact_matrix[0] = mio::ContactMatrix<ScalarType>(Eigen::MatrixX<ScalarType>::Constant(
107  static_cast<Eigen::Index>((size_t)size), static_cast<Eigen::Index>((size_t)size), 10.));
108  return Type(contact_matrix);
109  }
110  static std::string name()
111  {
112  return "ContactPatterns";
113  }
114 };
115 
122  {
123  ConstantFunction constfunc(1.0);
124  return Type(size, constfunc);
125  }
126  static std::string name()
127  {
128  return "TransmissionProbabilityOnContact";
129  }
130 };
131 
136 
139  {
140  ConstantFunction constfunc(1.0);
141  return Type(size, constfunc);
142  }
143  static std::string name()
144  {
145  return "RelativeTransmissionNoSymptoms";
146  }
147 };
148 
155  {
156  ConstantFunction constfunc(1.0);
157  return Type(size, constfunc);
158  }
159  static std::string name()
160  {
161  return "RiskOfInfectionFromSymptomatic";
162  }
163 };
164 
174 struct StartDay {
175  using Type = ScalarType;
177  {
178  return 0.;
179  }
180  static std::string name()
181  {
182  return "StartDay";
183  }
184 };
185 
191 struct Seasonality {
192  using Type = ScalarType;
194  {
195  return Type(0.);
196  }
197  static std::string name()
198  {
199  return "Seasonality";
200  }
201 };
202 
203 // Define Parameterset for IDE-SECIR model.
207 
212 {
213 public:
214  Parameters(AgeGroup num_agegroups)
215  : ParametersBase(num_agegroups)
216  , m_num_groups{num_agegroups}
217  {
218  }
219 
225  bool check_constraints() const
226  {
227  for (AgeGroup group = AgeGroup(0); group < m_num_groups; ++group) {
228  // For parameters potentially depending on the infectious age, values are checked
229  // equidistantly on a realistic maximum window.
230  // Please note that this is an incomplete check on correctness.
231  size_t infectious_window_check = 50; // parameter defining minimal window on x-axis
232  for (size_t i = 0; i < infectious_window_check; i++) {
233  if (this->get<TransmissionProbabilityOnContact>()[group].eval((ScalarType)i) < 0.0 ||
234  this->get<TransmissionProbabilityOnContact>()[group].eval((ScalarType)i) > 1.0) {
235  log_error("Constraint check: TransmissionProbabilityOnContact smaller {} or larger {} at some "
236  "time {}",
237  0, 1, i);
238  return true;
239  }
240  }
241 
242  for (size_t i = 0; i < infectious_window_check; i++) {
243  if (this->get<RelativeTransmissionNoSymptoms>()[group].eval((ScalarType)i) < 0.0 ||
244  this->get<RelativeTransmissionNoSymptoms>()[group].eval((ScalarType)i) > 1.0) {
245  log_error("Constraint check: RelativeTransmissionNoSymptoms smaller {} or larger {} at some "
246  "time {}",
247  0, 1, i);
248  return true;
249  }
250  }
251 
252  for (size_t i = 0; i < infectious_window_check; i++) {
253  if (this->get<RiskOfInfectionFromSymptomatic>()[group].eval((ScalarType)i) < 0.0 ||
254  this->get<RiskOfInfectionFromSymptomatic>()[group].eval((ScalarType)i) > 1.0) {
255  log_error("Constraint check: RiskOfInfectionFromSymptomatic smaller {} or larger {} at some "
256  "time {}",
257  0, 1, i);
258  return true;
259  }
260  }
261 
262  for (size_t i = 0; i < (int)InfectionTransition::Count; i++) {
263  if (this->get<TransitionProbabilities>()[group][i] < 0.0 ||
264  this->get<TransitionProbabilities>()[group][i] > 1.0) {
265  log_error("Constraint check: One parameter in TransitionProbabilities smaller {} or larger {}", 0,
266  1);
267  return true;
268  }
269  }
270 
272  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::SusceptibleToExposed], 1.0,
273  1e-14)) {
274  log_error("Constraint check: Parameter transition probability for SusceptibleToExposed unequal to {}",
275  1);
276  return true;
277  }
278 
280  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::ExposedToInfectedNoSymptoms],
281  1.0, 1e-14)) {
282  log_error("Constraint check: Parameter transition probability for ExposedToInfectedNoSymptoms unequal "
283  "to {}",
284  1);
285  return true;
286  }
287 
288  if (!floating_point_equal(this->get<TransitionProbabilities>()[group][(
290  this->get<TransitionProbabilities>()[group][(
292  1.0, 1e-14)) {
293  log_error(
294  "Constraint check: Sum of transition probability for InfectedNoSymptomsToInfectedSymptoms and "
295  "InfectedNoSymptomsToRecovered not equal to {}",
296  1);
297  return true;
298  }
299 
300  if (!floating_point_equal(this->get<TransitionProbabilities>()[group][(
302  this->get<TransitionProbabilities>()[group][(
304  1.0, 1e-14)) {
305  log_error("Constraint check: Sum of transition probability for InfectedSymptomsToInfectedSevere and "
306  "InfectedSymptomsToRecovered not equal to {}",
307  1);
308  return true;
309  }
310 
311  if (!floating_point_equal(this->get<TransitionProbabilities>()[group][(
313  this->get<TransitionProbabilities>()[group][(
315  1.0, 1e-14)) {
316  log_error("Constraint check: Sum of transition probability for InfectedSevereToInfectedCritical and "
317  "InfectedSevereToRecovered not equal to {}",
318  1);
319  return true;
320  }
321 
323  this->get<TransitionProbabilities>()[group][(int)InfectionTransition::InfectedCriticalToDead] +
324  this->get<TransitionProbabilities>()[group]
326  1.0, 1e-14)) {
327  log_error("Constraint check: Sum of transition probability for InfectedCriticalToDead and "
328  "InfectedCriticalToRecovered not equal to {}",
329  1);
330  return true;
331  }
332 
333  /* The first entry of TransitionDistributions is not checked because the distribution S->E is never used
334  (and it makes no sense to use the distribution). The support does not need to be valid.*/
335  for (size_t i = 1; i < (int)InfectionTransition::Count; i++) {
336  if (floating_point_less(this->get<TransitionDistributions>()[group][i].get_support_max(10), 0.0,
337  1e-14)) {
338  log_error("Constraint check: One function in TransitionDistributions has invalid support.");
339  return true;
340  }
341  }
342  }
343 
344  if (this->get<Seasonality>() < 0.0 || this->get<Seasonality>() > 0.5) {
345  log_warning("Constraint check: Parameter Seasonality should lie between {} and {}", 0.0, 0.5);
346  return true;
347  }
348 
349  return false;
350  }
351 
356  template <class IOContext>
357  static IOResult<Parameters> deserialize(IOContext& io)
358  {
359  BOOST_OUTCOME_TRY(auto&& base, ParametersBase::deserialize(io));
360  return success(Parameters(std::move(base)));
361  }
362 
363 private:
365  : ParametersBase(std::move(base))
366  , m_num_groups(get<ContactPatterns>().get_cont_freq_mat().get_num_groups())
367  {
368  }
369 
370 private:
372 };
373 } // namespace isecir
374 
375 } // namespace mio
376 
377 #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:212
static IOResult< Parameters > deserialize(IOContext &io)
deserialize an object of this class.
Definition: ide_secir/parameters.h:357
bool check_constraints() const
Checks whether all Parameters satisfy their corresponding constraints and logs an error.
Definition: ide_secir/parameters.h:225
Parameters(ParametersBase &&base)
Definition: ide_secir/parameters.h:364
AgeGroup m_num_groups
Definition: ide_secir/parameters.h:371
Parameters(AgeGroup num_agegroups)
Definition: ide_secir/parameters.h:214
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:448
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:809
void log_warning(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:112
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:359
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: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
The AgeGroup struct is used as a dynamically sized tag for all age dependent categories.
Definition: age_group.h:32
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:99
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:102
UncertainContactMatrix< ScalarType > Type
Definition: ide_secir/parameters.h:100
static std::string name()
Definition: ide_secir/parameters.h:110
The relative InfectedNoSymptoms infectability.
Definition: ide_secir/parameters.h:135
CustomIndexArray< StateAgeFunctionWrapper< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:137
static std::string name()
Definition: ide_secir/parameters.h:143
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:138
The risk of infection from symptomatic cases in the SECIR model.
Definition: ide_secir/parameters.h:152
CustomIndexArray< StateAgeFunctionWrapper< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:153
static std::string name()
Definition: ide_secir/parameters.h:159
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:154
The seasonality parameter k in the IDE-SECIR model.
Definition: ide_secir/parameters.h:191
static std::string name()
Definition: ide_secir/parameters.h:197
ScalarType Type
Definition: ide_secir/parameters.h:192
static Type get_default(AgeGroup)
Definition: ide_secir/parameters.h:193
Sets the day in a year at which a simulation with an IDE-SECIR model is started.
Definition: ide_secir/parameters.h:174
static Type get_default(AgeGroup)
Definition: ide_secir/parameters.h:176
static std::string name()
Definition: ide_secir/parameters.h:180
ScalarType Type
Definition: ide_secir/parameters.h:175
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:90
Probability of getting infected from a contact.
Definition: ide_secir/parameters.h:119
static Type get_default(AgeGroup size)
Definition: ide_secir/parameters.h:121
static std::string name()
Definition: ide_secir/parameters.h:126
CustomIndexArray< StateAgeFunctionWrapper< ScalarType >, AgeGroup > Type
Definition: ide_secir/parameters.h:120