|
1 | 1 | /* |
2 | 2 | * Copyright (C) 2020-2025 MEmilio |
3 | 3 | * |
4 | | -* Authors: Anna Wendler, Lena Ploetzke |
| 4 | +* Authors: Anna Wendler, Lena Ploetzke, Hannah Tritzschak |
5 | 5 | * |
6 | 6 | * Contact: Martin J. Kuehn <[email protected]> |
7 | 7 | * |
|
17 | 17 | * See the License for the specific language governing permissions and |
18 | 18 | * limitations under the License. |
19 | 19 | */ |
20 | | - |
21 | 20 | #include "ide_secir/model.h" |
22 | 21 | #include "ide_secir/infection_state.h" |
23 | 22 | #include "ide_secir/simulation.h" |
|
32 | 31 |
|
33 | 32 | int main() |
34 | 33 | { |
| 34 | + // This is a simple example to demonstrate how use the IDE-SECIR model. |
| 35 | + |
35 | 36 | using Vec = Eigen::VectorX<ScalarType>; |
36 | 37 |
|
37 | | - size_t num_agegroups = 1; |
| 38 | + // Define simulation parameters. |
| 39 | + ScalarType t0 = 0.; |
| 40 | + ScalarType tmax = 5.; |
| 41 | + ScalarType dt = 0.01; // The step size will stay constant throughout the simulation. |
38 | 42 |
|
39 | | - ScalarType tmax = 10; |
40 | | - mio::CustomIndexArray<ScalarType, mio::AgeGroup> N = |
41 | | - mio::CustomIndexArray<ScalarType, mio::AgeGroup>(mio::AgeGroup(num_agegroups), 10000.); |
42 | | - mio::CustomIndexArray<ScalarType, mio::AgeGroup> deaths = |
43 | | - mio::CustomIndexArray<ScalarType, mio::AgeGroup>(mio::AgeGroup(num_agegroups), 13.10462213); |
44 | | - ScalarType dt = 1.; |
| 43 | + // Define number of age groups. |
| 44 | + size_t num_agegroups = 2; |
45 | 45 |
|
46 | | - int num_transitions = (int)mio::isecir::InfectionTransition::Count; |
| 46 | + // Define initial values for the total population and number of deaths per age group. |
| 47 | + mio::CustomIndexArray<ScalarType, mio::AgeGroup> total_population_init = |
| 48 | + mio::CustomIndexArray<ScalarType, mio::AgeGroup>(mio::AgeGroup(num_agegroups), 1000.); |
| 49 | + mio::CustomIndexArray<ScalarType, mio::AgeGroup> deaths_init = |
| 50 | + mio::CustomIndexArray<ScalarType, mio::AgeGroup>(mio::AgeGroup(num_agegroups), 6.); |
47 | 51 |
|
48 | | - // Create TimeSeries with num_transitions * num_agegroups elements where transitions needed for simulation will be |
49 | | - // stored. |
50 | | - mio::TimeSeries<ScalarType> init(num_transitions * num_agegroups); |
| 52 | + // Create TimeSeries with num_transitions * num_agegroups elements where initial transitions needed for simulation |
| 53 | + // will be stored. We require values for the transitions for a sufficient number of time points before the start of |
| 54 | + // the simulation to initialize our model. |
| 55 | + size_t num_transitions = (size_t)mio::isecir::InfectionTransition::Count; |
| 56 | + mio::TimeSeries<ScalarType> transitions_init(num_transitions * num_agegroups); |
51 | 57 |
|
52 | | - // Add time points for initialization of transitions. |
| 58 | + // Define vector of transitions that will be added as values to the time points of the TimeSeries transitions_init. |
53 | 59 | Vec vec_init(num_transitions * num_agegroups); |
54 | | - vec_init[(int)mio::isecir::InfectionTransition::SusceptibleToExposed] = 25.0; |
55 | | - vec_init[(int)mio::isecir::InfectionTransition::ExposedToInfectedNoSymptoms] = 15.0; |
56 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedNoSymptomsToInfectedSymptoms] = 8.0; |
57 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedNoSymptomsToRecovered] = 4.0; |
58 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedSymptomsToInfectedSevere] = 1.0; |
59 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedSymptomsToRecovered] = 4.0; |
60 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedSevereToInfectedCritical] = 1.0; |
61 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedSevereToRecovered] = 1.0; |
62 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedCriticalToDead] = 1.0; |
63 | | - vec_init[(int)mio::isecir::InfectionTransition::InfectedCriticalToRecovered] = 1.0; |
64 | | - |
| 60 | + for (size_t group = 0; group < num_agegroups; ++group) { |
| 61 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::SusceptibleToExposed] = 25.0; |
| 62 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::ExposedToInfectedNoSymptoms] = |
| 63 | + 15.0; |
| 64 | + vec_init[group * num_transitions + |
| 65 | + (size_t)mio::isecir::InfectionTransition::InfectedNoSymptomsToInfectedSymptoms] = 8.0; |
| 66 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedNoSymptomsToRecovered] = |
| 67 | + 4.0; |
| 68 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedSymptomsToInfectedSevere] = |
| 69 | + 1.0; |
| 70 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedSymptomsToRecovered] = 4.0; |
| 71 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedSevereToInfectedCritical] = |
| 72 | + 1.0; |
| 73 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedSevereToRecovered] = 1.0; |
| 74 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedCriticalToDead] = 1.0; |
| 75 | + vec_init[group * num_transitions + (size_t)mio::isecir::InfectionTransition::InfectedCriticalToRecovered] = 1.0; |
| 76 | + } |
| 77 | + // Multiply vec_init with dt so that within a time interval of length 1, always the above number of |
| 78 | + // individuals are transitioning from one compartment to another, irrespective of the chosen time step size. |
65 | 79 | vec_init = vec_init * dt; |
66 | | - // Add initial time point to time series. |
67 | | - init.add_time_point(-10, vec_init); |
68 | | - // Add further time points until time 0. |
69 | | - while (init.get_last_time() < -dt / 2) { |
70 | | - init.add_time_point(init.get_last_time() + dt, vec_init); |
| 80 | + |
| 81 | + // In this example, we will set the TransitionDistributions below. For these distributions, setting the initial time |
| 82 | + // point of the TimeSeries transitions_init at time -10 will give us a sufficient number of time points before t0=0. |
| 83 | + // For more information on this, we refer to the documentation of TransitionDistributions in |
| 84 | + // models/ide_secir/parameters.h. |
| 85 | + transitions_init.add_time_point(-10, vec_init); |
| 86 | + // Add further time points with distance dt until time t0. |
| 87 | + while (transitions_init.get_last_time() < t0 - dt / 2) { |
| 88 | + transitions_init.add_time_point(transitions_init.get_last_time() + dt, vec_init); |
71 | 89 | } |
72 | 90 |
|
73 | 91 | // Initialize model. |
74 | | - mio::isecir::Model model(std::move(init), N, deaths, num_agegroups); |
| 92 | + mio::isecir::Model model(std::move(transitions_init), total_population_init, deaths_init, num_agegroups); |
| 93 | + |
| 94 | + // Uncomment one of the code blocks below to use a different method to initialize the model, based on a |
| 95 | + // given number of either Susceptibles or Recovered instead of using the TimeSeries transitions_init from above. |
| 96 | + |
| 97 | + // Initialization method with given Susceptibles. |
| 98 | + // size_t num_infstates = (size_t)mio::isecir::InfectionState::Count; |
| 99 | + // for (size_t group = 0; group < num_agegroups; ++group) { |
| 100 | + // model.populations.get_last_value()[group * num_infstates + (size_t)mio::isecir::InfectionState::Susceptible] = |
| 101 | + // 900; |
| 102 | + // } |
75 | 103 |
|
76 | | - // Uncomment one of the two lines to use a different method to initialize the model using the TimeSeries init. |
77 | | - // Initialization method with Susceptibles. |
78 | | - // model.populations.get_last_value()[(Eigen::Index)mio::isecir::InfectionState::Susceptible] = 1000; |
79 | | - // Initialization method with Recovered. |
80 | | - // model.populations.get_last_value()[(Eigen::Index)mio::isecir::InfectionState::Recovered] = 0; |
| 104 | + // Initialization method with given Recovered. |
| 105 | + // size_t num_infstates = (size_t)mio::isecir::InfectionState::Count; |
| 106 | + // for (size_t group = 0; group < num_agegroups; ++group) { |
| 107 | + // model.populations.get_last_value()[group * num_infstates + (size_t)mio::isecir::InfectionState::Recovered] = 10; |
| 108 | + // } |
81 | 109 |
|
82 | 110 | // Set working parameters. |
83 | | - mio::SmootherCosine<ScalarType> smoothcos(2.0); |
84 | | - mio::StateAgeFunctionWrapper<ScalarType> delaydistribution(smoothcos); |
85 | | - std::vector<mio::StateAgeFunctionWrapper<ScalarType>> vec_delaydistrib(num_transitions, delaydistribution); |
86 | | - // TransitionDistribution is not used for SusceptibleToExposed. Therefore, the parameter can be set to any value. |
87 | | - vec_delaydistrib[(int)mio::isecir::InfectionTransition::SusceptibleToExposed].set_distribution_parameter(-1.); |
88 | | - vec_delaydistrib[(int)mio::isecir::InfectionTransition::InfectedNoSymptomsToInfectedSymptoms] |
89 | | - .set_distribution_parameter(4.0); |
90 | 111 |
|
91 | | - model.parameters.get<mio::isecir::TransitionDistributions>()[mio::AgeGroup(0)] = vec_delaydistrib; |
| 112 | + // TransitionDistributions |
| 113 | + // In the following, we explicitly set the TransitionDistributions for the first age group. If the model contains |
| 114 | + // more age groups, the default distributions are used for these age groups. |
| 115 | + mio::SmootherCosine<ScalarType> smoothcos1(3.0); |
| 116 | + mio::StateAgeFunctionWrapper<ScalarType> delaydistribution1(smoothcos1); |
| 117 | + std::vector<mio::StateAgeFunctionWrapper<ScalarType>> vec_delaydistrib1(num_transitions, delaydistribution1); |
| 118 | + // TransitionDistribution is not used for SusceptibleToExposed. Therefore, the parameter can be set to any value. |
| 119 | + vec_delaydistrib1[(size_t)mio::isecir::InfectionTransition::SusceptibleToExposed].set_distribution_parameter(-1.); |
| 120 | + model.parameters.get<mio::isecir::TransitionDistributions>()[mio::AgeGroup(0)] = vec_delaydistrib1; |
92 | 121 |
|
| 122 | + // TransitionProbabilities |
93 | 123 | std::vector<ScalarType> vec_prob(num_transitions, 0.5); |
94 | 124 | // The following probabilities must be 1, as there is no other way to go. |
95 | | - vec_prob[Eigen::Index(mio::isecir::InfectionTransition::SusceptibleToExposed)] = 1; |
96 | | - vec_prob[Eigen::Index(mio::isecir::InfectionTransition::ExposedToInfectedNoSymptoms)] = 1; |
97 | | - model.parameters.get<mio::isecir::TransitionProbabilities>()[mio::AgeGroup(0)] = vec_prob; |
| 125 | + vec_prob[(size_t)mio::isecir::InfectionTransition::SusceptibleToExposed] = 1; |
| 126 | + vec_prob[(size_t)mio::isecir::InfectionTransition::ExposedToInfectedNoSymptoms] = 1; |
| 127 | + for (mio::AgeGroup group = mio::AgeGroup(0); group < mio::AgeGroup(num_agegroups); ++group) { |
| 128 | + model.parameters.get<mio::isecir::TransitionProbabilities>()[group] = vec_prob; |
| 129 | + } |
98 | 130 |
|
| 131 | + // Contact patterns |
99 | 132 | mio::ContactMatrixGroup<ScalarType> contact_matrix = mio::ContactMatrixGroup<ScalarType>(1, num_agegroups); |
100 | 133 | contact_matrix[0] = |
101 | 134 | mio::ContactMatrix<ScalarType>(Eigen::MatrixX<ScalarType>::Constant(num_agegroups, num_agegroups, 10.)); |
102 | | - model.parameters.get<mio::isecir::ContactPatterns>() = mio::UncertainContactMatrix<ScalarType>(contact_matrix); |
| 135 | + model.parameters.get<mio::isecir::ContactPatterns>() = mio::UncertainContactMatrix(contact_matrix); |
103 | 136 |
|
| 137 | + // Furhter epidemiological parameters |
104 | 138 | mio::ExponentialSurvivalFunction<ScalarType> exponential(0.5); |
105 | 139 | mio::StateAgeFunctionWrapper<ScalarType> prob(exponential); |
106 | | - |
107 | | - model.parameters.get<mio::isecir::TransmissionProbabilityOnContact>()[mio::AgeGroup(0)] = prob; |
108 | | - model.parameters.get<mio::isecir::RelativeTransmissionNoSymptoms>()[mio::AgeGroup(0)] = prob; |
109 | | - model.parameters.get<mio::isecir::RiskOfInfectionFromSymptomatic>()[mio::AgeGroup(0)] = prob; |
110 | | - |
| 140 | + for (mio::AgeGroup group = mio::AgeGroup(0); group < mio::AgeGroup(num_agegroups); ++group) { |
| 141 | + model.parameters.get<mio::isecir::TransmissionProbabilityOnContact>()[group] = prob; |
| 142 | + model.parameters.get<mio::isecir::RelativeTransmissionNoSymptoms>()[group] = prob; |
| 143 | + model.parameters.get<mio::isecir::RiskOfInfectionFromSymptomatic>()[group] = prob; |
| 144 | + } |
111 | 145 | model.parameters.set<mio::isecir::Seasonality>(0.1); |
112 | | - // Start the simulation on the 40th day of a year (i.e. in February). |
113 | | - model.parameters.set<mio::isecir::StartDay>(40); |
| 146 | + model.parameters.set<mio::isecir::StartDay>( |
| 147 | + 40); // Start the simulation on the 40th day of a year (i.e. in February). |
114 | 148 |
|
| 149 | + // Check if all model constraints regarding initial values and parameters are satisfied before simulating. |
115 | 150 | model.check_constraints(dt); |
116 | 151 |
|
117 | 152 | // Carry out simulation. |
118 | 153 | mio::isecir::Simulation sim(model, dt); |
119 | 154 | sim.advance(tmax); |
120 | 155 |
|
| 156 | + // Interpolate results to days. |
121 | 157 | auto interpolated_results = mio::interpolate_simulation_result(sim.get_result(), dt / 2.); |
122 | 158 |
|
123 | | - interpolated_results.print_table({"S", "E", "C", "I", "H", "U", "R", "D "}, 16, 8); |
| 159 | + // Print results. Note that the column labels are suitable for a simulation with two age groups and may need to be |
| 160 | + // adapted when the number of age groups is changed. |
| 161 | + // interpolated_results.print_table( |
| 162 | + // {"S1", "E1", "C1", "I1", "H1", "U1", "R1", "D1 ", "S2", "E2", "C2", "I2", "H2", "U2", "R2", "D2 "}, 16, 8); |
124 | 163 | // Uncomment this line to print the transitions. |
125 | | - // sim.get_transitions().print_table( |
126 | | - // {"S->E 1", "E->C 1", "C->I 1", "C->R 1", "I->H 1", "I->R 1", "H->U 1", "H->R 1", "U->D 1", "U->R 1"}, 16, 8); |
| 164 | + // sim.get_transitions().print_table({"S->E 1", "E->C 1", "C->I 1", "C->R 1", "I->H 1", "I->R 1", "H->U 1", |
| 165 | + // "H->R 1", "U->D 1", "U->R 1", "S->E 2", "E->C 2", "C->I 2", "C->R 2", |
| 166 | + // "I->H 2", "I->R 2", "H->U 2", "H->R 2", "U->D 2", "U->R 2"}, |
| 167 | + // 16, 8); |
127 | 168 | } |
0 commit comments