model.h Source File

CPP API: model.h Source File
ide_secir/model.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Anna Wendler, Lena Ploetzke, Martin J. Kuehn
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 
21 #ifndef IDESECIR_MODEL_H
22 #define IDESECIR_MODEL_H
23 
24 #include "ide_secir/parameters.h"
26 #include "memilio/config.h"
29 #include "memilio/utils/date.h"
31 
32 #include "vector"
33 
34 namespace mio
35 {
36 namespace isecir
37 {
38 // Forward declaration of friend classes/functions of Model.
39 class Model;
40 class Simulation;
41 template <typename EntryType>
42 IOResult<void> set_initial_flows(Model& model, const ScalarType dt, const std::vector<EntryType> rki_data,
43  const Date date, const CustomIndexArray<ScalarType, AgeGroup> scale_confirmed_cases);
44 
45 class Model
46 {
48 
49 public:
67  CustomIndexArray<ScalarType, AgeGroup> deaths_init, size_t num_agegroups,
69 
70  // ---- Additional functionality such as constraint checking, setters and getters, etc. ----
75  bool check_constraints(ScalarType dt) const;
76 
88  int get_transition_flat_index(Eigen::Index transition_idx, AgeGroup agegroup) const
89  {
90  return (static_cast<int>(size_t(agegroup)) * int(InfectionTransition::Count) + int(transition_idx));
91  }
92 
103  int get_state_flat_index(Eigen::Index state_idx, AgeGroup agegroup) const
104  {
105  return (static_cast<int>(size_t(agegroup)) * int(InfectionState::Count) + int(state_idx));
106  }
107 
120 
137  {
139  }
140 
146  size_t get_num_agegroups() const
147  {
148  return m_num_agegroups;
149  }
150 
157  {
158  m_tol = new_tol;
159  }
160 
161  // ---- Public parameters. ----
163  // Attention: populations and transitions do not necessarily have the same number of time points due to the
164  // initialization part.
167  // one #InfectionState to another as defined in #InfectionTransition%s for every AgeGroup.
169  // people in defined #InfectionState%s for every AgeGroup.
172 
173 private:
174  // ---- Functionality to calculate the sizes of the compartments for time t0. ----
197  void compute_compartment_from_flows(ScalarType dt, Eigen::Index idx_InfectionState, AgeGroup group,
198  Eigen::Index idx_IncomingFlow, int idx_TransitionDistribution1,
199  int idx_TransitionDistribution2 = 0);
200 
211 
232 
233  // ---- Functionality for the iterations of a simulation. ----
242 
257  void compute_flow(Eigen::Index idx_InfectionTransitions, Eigen::Index idx_IncomingFlow, ScalarType dt,
258  Eigen::Index current_time_index, AgeGroup group);
259 
273  void compute_flow(Eigen::Index idx_InfectionTransitions, Eigen::Index idx_IncomingFlow, ScalarType dt,
274  AgeGroup group);
275 
284 
292  void update_compartment_from_flow(InfectionState infectionState,
293  std::vector<InfectionTransition> const& IncomingFlows,
294  std::vector<InfectionTransition> const& OutgoingFlows, AgeGroup group);
295 
304  void update_compartments();
305 
316  void compute_forceofinfection(ScalarType dt, bool initialization = false);
317 
318  // ---- Functionality to set vectors with necessary information regarding TransitionDistributions. ----
328 
340 
353 
354  // ---- Private parameters. ----
357  m_N;
358  ScalarType m_tol{1e-10};
361  // See also get_initialization_method_compartments() for the number code.
363  // of Type AgeGroup containing a Vector containing the support_max for all TransitionDistributions.
366  //of Type AgeGroup containing a Vector containing the approximated derivative for all TransitionDistributions for
367  // all necessary time points.
370  // of Type AgeGroup containing a Vector containing the weighted TransitionDistributions for all necessary time
371  // points in the force of infection term.
372 
373  // ---- Friend classes/functions. ----
374  // In the Simulation class, the actual simulation is performed which is why it needs access to the here
375  // defined (and private) functions to solve the model equations.
376  friend class Simulation;
377  // In set_initial_flows(), we compute initial flows based on RKI data using the (private) compute_flow() function
378  // which is why it is defined as a friend function.
379  template <typename EntryType>
380  friend IOResult<void> set_initial_flows(Model& model, const ScalarType dt, const std::vector<EntryType> rki_data,
381  const Date date,
382  const CustomIndexArray<ScalarType, AgeGroup> scale_confirmed_cases);
383 };
384 
385 } // namespace isecir
386 } // namespace mio
387 
388 #endif // IDESECIR_MODEL_H
A class template for an array with custom indices.
Definition: custom_index_array.h:136
Definition: ide_secir/model.h:46
void initial_compute_compartments_infection(ScalarType dt)
Computes the values of the infection compartments subset at initialization.
Definition: ide_secir/model.cpp:232
void flows_current_timestep(ScalarType dt)
Sets all required flows for the current last timestep in transitions.
Definition: ide_secir/model.cpp:474
void set_transitiondistributions_derivative(ScalarType dt)
Setter for the vector m_transitiondistributions_derivative that contains the approximated derivative ...
Definition: ide_secir/model.cpp:677
ScalarType m_tol
Tolerance used to calculate the maximum support of the TransitionDistributions.
Definition: ide_secir/model.h:358
void compute_flow(Eigen::Index idx_InfectionTransitions, Eigen::Index idx_IncomingFlow, ScalarType dt, Eigen::Index current_time_index, AgeGroup group)
Computes size of a flow.
Definition: ide_secir/model.cpp:438
void set_transitiondistributions_support_max(ScalarType dt)
Setter for the vector m_transitiondistributions_support_max that contains the support_max for all Tra...
Definition: ide_secir/model.cpp:664
void compute_forceofinfection(ScalarType dt, bool initialization=false)
Computes force of infection for the current last time in transitions.
Definition: ide_secir/model.cpp:582
bool check_constraints(ScalarType dt) const
Checks constraints on model parameters and initial data.
Definition: ide_secir/model.cpp:75
int get_state_flat_index(Eigen::Index state_idx, AgeGroup agegroup) const
Returns a flat index for the TimeSeries populations which contains values for the InfectionStates.
Definition: ide_secir/model.h:103
void set_transitiondistributions_in_forceofinfection(ScalarType dt)
Setter for the vector m_transitiondistributions_in_forceofinfection.
Definition: ide_secir/model.cpp:705
int get_transition_flat_index(Eigen::Index transition_idx, AgeGroup agegroup) const
Returns a flat index for the TimeSeries transitions which contains values for the InfectionTransition...
Definition: ide_secir/model.h:88
void initial_compute_compartments(ScalarType dt)
Computes the values of compartments at initialization.
Definition: ide_secir/model.cpp:263
CustomIndexArray< ScalarType, AgeGroup > m_N
Vector containing the total population size of the considered region for every AgeGroup.
Definition: ide_secir/model.h:357
void set_tol_for_support_max(ScalarType new_tol)
Setter for the tolerance used to calculate the maximum support of the TransitionDistributions.
Definition: ide_secir/model.h:156
int get_initialization_method_compartments() const
Returns the index of the automatically selected initialization method.
Definition: ide_secir/model.h:136
CustomIndexArray< ScalarType, AgeGroup > m_forceofinfection
Force of infection term needed for numerical scheme.
Definition: ide_secir/model.h:355
TimeSeries< ScalarType > transitions
TimeSeries containing points of time and the corresponding number of individuals transitioning from.
Definition: ide_secir/model.h:166
Model(TimeSeries< ScalarType > &&transitions_init, CustomIndexArray< ScalarType, AgeGroup > N_init, CustomIndexArray< ScalarType, AgeGroup > deaths_init, size_t num_agegroups, CustomIndexArray< ScalarType, AgeGroup > total_confirmed_cases_init=CustomIndexArray< ScalarType, AgeGroup >())
Constructor to create an IDE-SECIR model.
Definition: ide_secir/model.cpp:39
TimeSeries< ScalarType > populations
TimeSeries containing points of time and the corresponding number of.
Definition: ide_secir/model.h:168
CustomIndexArray< ScalarType, AgeGroup > total_confirmed_cases
CustomIndexArray that contains the total number of confirmed cases at time t0 for every AgeGroup.
Definition: ide_secir/model.h:171
CustomIndexArray< std::vector< ScalarType >, AgeGroup > m_transitiondistributions_support_max
CustomIndexArray.
Definition: ide_secir/model.h:362
friend IOResult< void > set_initial_flows(Model &model, const ScalarType dt, const std::vector< EntryType > rki_data, const Date date, const CustomIndexArray< ScalarType, AgeGroup > scale_confirmed_cases)
ParameterSet parameters
ParameterSet of Model Parameters.
Definition: ide_secir/model.h:162
void update_compartment_from_flow(InfectionState infectionState, std::vector< InfectionTransition > const &IncomingFlows, std::vector< InfectionTransition > const &OutgoingFlows, AgeGroup group)
Updates the values of one compartment using flows.
Definition: ide_secir/model.cpp:515
CustomIndexArray< std::vector< std::vector< ScalarType > >, AgeGroup > m_transitiondistributions_derivative
CustomIndexArray.
Definition: ide_secir/model.h:365
void compute_compartment_from_flows(ScalarType dt, Eigen::Index idx_InfectionState, AgeGroup group, Eigen::Index idx_IncomingFlow, int idx_TransitionDistribution1, int idx_TransitionDistribution2=0)
Compute the compartment specified in idx_InfectionState at the current time – only using historic flo...
Definition: ide_secir/model.cpp:193
ScalarType get_global_support_max(ScalarType dt) const
Getter for the global support_max, i.e.
Definition: ide_secir/model.cpp:150
void compute_susceptibles(ScalarType dt)
Computes number of Susceptibles for the current last time in populations.
Definition: ide_secir/model.cpp:426
size_t get_num_agegroups() const
Getter for number of age groups.
Definition: ide_secir/model.h:146
void update_compartments()
Updates the values of all compartments except Susceptible at initialization.
Definition: ide_secir/model.cpp:534
CustomIndexArray< std::vector< std::vector< ScalarType > >, AgeGroup > m_transitiondistributions_in_forceofinfection
CustomIndexArray.
Definition: ide_secir/model.h:369
size_t m_num_agegroups
Number of Age Groups.
Definition: ide_secir/model.h:359
int m_initialization_method
Gives the index of the method used for the initialization of the model.
Definition: ide_secir/model.h:360
Parameters of an age-resolved SECIR/SECIHURD model.
Definition: ide_secir/parameters.h:212
run the simulation in discrete steps and report results.
Definition: models/ide_secir/simulation.h:38
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
InfectionState
The InfectionState enum describes the possible categories for the infectious state of persons.
Definition: ide_secir/infection_state.h:36
IOResult< void > set_initial_flows(Model &model, const ScalarType dt, const std::vector< EntryType > rki_data, const Date date, const CustomIndexArray< ScalarType, AgeGroup > scale_confirmed_cases)
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353
The AgeGroup struct is used as a dynamically sized tag for all age dependent categories.
Definition: age_group.h:32
Simple date representation as year, month, and day.
Definition: date.h:50