|
| 1 | +/* |
| 2 | +* Copyright (C) 2020-2024 MEmilio |
| 3 | +* |
| 4 | +* Authors: Daniel Abele, Martin J. Kuehn |
| 5 | +* |
| 6 | +* Contact: Martin J. Kuehn <[email protected]> |
| 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 | +#include "ode_secir/model.h" |
| 21 | +#include "memilio/compartments/simulation.h" |
| 22 | +#include "memilio/utils/logging.h" |
| 23 | +#include "memilio/math/euler.h" |
| 24 | + |
| 25 | +/* |
| 26 | + * This example demonstrates how to realize contact behavior changes with any of our ordinary differential |
| 27 | + * equation-based models. This example can thus easily be adapted for other models like osecirvvs, oseir, osir etc. |
| 28 | + * We print out the flows (i.e., new transmissions, infections, hospitalizations etc. per time point.) As we use an |
| 29 | + * fixed-time step Explicit Euler, we can compare them. |
| 30 | +*/ |
| 31 | +int main() |
| 32 | +{ |
| 33 | + mio::set_log_level(mio::LogLevel::warn); |
| 34 | + |
| 35 | + double t0 = 0; |
| 36 | + double dt = 0.1; |
| 37 | + double tmax = 1; |
| 38 | + |
| 39 | + double cont_freq = 10; |
| 40 | + |
| 41 | + double nb_total_t0 = 1000, nb_inf_t0 = 10; |
| 42 | + |
| 43 | + auto integrator = std::make_shared<mio::EulerIntegratorCore>(); |
| 44 | + |
| 45 | + // default model run to be compared against |
| 46 | + mio::osecir::Model model_a(1); |
| 47 | + const auto indx_flow_SE = |
| 48 | + model_a.get_flat_flow_index<mio::osecir::InfectionState::Susceptible, mio::osecir::InfectionState::Exposed>( |
| 49 | + {mio::AgeGroup(0)}); |
| 50 | + |
| 51 | + model_a.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedSymptoms}] = nb_inf_t0; |
| 52 | + model_a.populations.set_difference_from_total({mio::AgeGroup(0), mio::osecir::InfectionState::Susceptible}, |
| 53 | + nb_total_t0); |
| 54 | + mio::ContactMatrixGroup& contact_matrix_a = model_a.parameters.get<mio::osecir::ContactPatterns>(); |
| 55 | + contact_matrix_a[0] = mio::ContactMatrix(Eigen::MatrixXd::Constant(1, 1, cont_freq)); |
| 56 | + // set probability of transmission and risk of infection to 1. |
| 57 | + model_a.parameters.get<mio::osecir::TransmissionProbabilityOnContact>() = 1.0; |
| 58 | + model_a.parameters.get<mio::osecir::RiskOfInfectionFromSymptomatic>() = 1.0; |
| 59 | + auto result_a = simulate_flows(t0, tmax, dt, model_a, integrator); |
| 60 | + result_a[1].print_table({"S->E", "E->I_NS", "I_NS->I_Sy", "I_NS->R", "I_NSC->I_SyC", "I_NSC->R", "I_Sy->I_Sev", |
| 61 | + "I_Sy->R", "I_SyC->I_Sev", "I_SyC->R", "I_Sev->I_Crit", "I_Sev->R", "I_Sev->D", |
| 62 | + "I_Crit->D", "I_Crit->R"}, |
| 63 | + 4, 4); |
| 64 | + std::cout << "With default contacts, the number of new transmissions (flow from S->E) in first time step is: " |
| 65 | + << result_a[1].get_value(1)[indx_flow_SE] << ".\n"; |
| 66 | + |
| 67 | + // The contacts are halfed: reduced transmission through damping with value 0.5 |
| 68 | + mio::osecir::Model model_b{model_a}; |
| 69 | + model_b.populations.set_total(nb_total_t0); |
| 70 | + model_b.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedSymptoms}] = nb_inf_t0; |
| 71 | + model_b.populations.set_difference_from_total({mio::AgeGroup(0), mio::osecir::InfectionState::Susceptible}, |
| 72 | + nb_total_t0); |
| 73 | + mio::ContactMatrixGroup& contact_matrix_b = model_b.parameters.get<mio::osecir::ContactPatterns>(); |
| 74 | + contact_matrix_b[0] = mio::ContactMatrix(Eigen::MatrixXd::Constant(1, 1, cont_freq)); |
| 75 | + contact_matrix_b[0].add_damping(0.5, mio::SimulationTime(0.)); // contact reduction happens here! |
| 76 | + auto result_b = simulate_flows(t0, tmax, dt, model_b, integrator); |
| 77 | + result_b[1].print_table({"S->E", "E->I_NS", "I_NS->I_Sy", "I_NS->R", "I_NSC->I_SyC", "I_NSC->R", "I_Sy->I_Sev", |
| 78 | + "I_Sy->R", "I_SyC->I_Sev", "I_SyC->R", "I_Sev->I_Crit", "I_Sev->R", "I_Sev->D", |
| 79 | + "I_Crit->D", "I_Crit->R"}, |
| 80 | + 4, 4); |
| 81 | + std::cout << "With contacts reduced to a half of the original example, the number of new transmissions (flow from " |
| 82 | + "S->E) in first time step is: " |
| 83 | + << result_b[1].get_value(1)[indx_flow_SE] << ".\n"; |
| 84 | + |
| 85 | + // No contacts at all: no transmission through damping with value 1. |
| 86 | + mio::osecir::Model model_c{model_a}; |
| 87 | + model_c.populations.set_total(nb_total_t0); |
| 88 | + model_c.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedSymptoms}] = nb_inf_t0; |
| 89 | + model_c.populations.set_difference_from_total({mio::AgeGroup(0), mio::osecir::InfectionState::Susceptible}, |
| 90 | + nb_total_t0); |
| 91 | + mio::ContactMatrixGroup& contact_matrix_c = model_c.parameters.get<mio::osecir::ContactPatterns>(); |
| 92 | + contact_matrix_c[0] = mio::ContactMatrix(Eigen::MatrixXd::Constant(1, 1, cont_freq)); |
| 93 | + contact_matrix_c[0].add_damping(1., mio::SimulationTime(0.)); // contact reduction happens here! |
| 94 | + auto result_c = simulate_flows(t0, tmax, dt, model_c, integrator); |
| 95 | + result_c[1].print_table({"S->E", "E->I_NS", "I_NS->I_Sy", "I_NS->R", "I_NSC->I_SyC", "I_NSC->R", "I_Sy->I_Sev", |
| 96 | + "I_Sy->R", "I_SyC->I_Sev", "I_SyC->R", "I_Sev->I_Crit", "I_Sev->R", "I_Sev->D", |
| 97 | + "I_Crit->D", "I_Crit->R"}, |
| 98 | + 4, 4); |
| 99 | + std::cout |
| 100 | + << "With contacts reduced to zero, the number of new transmissions (flow from S->E) in first time step is: " |
| 101 | + << result_c[1].get_value(1)[indx_flow_SE] << ".\n"; |
| 102 | + |
| 103 | + // The contacts are doubled: increased transmission through damping with value -1. |
| 104 | + mio::osecir::Model model_d{model_a}; |
| 105 | + model_d.populations.set_total(nb_total_t0); |
| 106 | + model_d.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::InfectedSymptoms}] = nb_inf_t0; |
| 107 | + model_d.populations.set_difference_from_total({mio::AgeGroup(0), mio::osecir::InfectionState::Susceptible}, |
| 108 | + nb_total_t0); |
| 109 | + mio::ContactMatrixGroup& contact_matrix_d = model_d.parameters.get<mio::osecir::ContactPatterns>(); |
| 110 | + contact_matrix_d[0] = mio::ContactMatrix(Eigen::MatrixXd::Constant(1, 1, cont_freq)); |
| 111 | + contact_matrix_d[0].add_damping(-1., mio::SimulationTime(0.)); // contact increase happens here! |
| 112 | + auto result_d = simulate_flows(t0, tmax, dt, model_d, integrator); |
| 113 | + result_d[1].print_table({"S->E", "E->I_NS", "I_NS->I_Sy", "I_NS->R", "I_NSC->I_SyC", "I_NSC->R", "I_Sy->I_Sev", |
| 114 | + "I_Sy->R", "I_SyC->I_Sev", "I_SyC->R", "I_Sev->I_Crit", "I_Sev->R", "I_Sev->D", |
| 115 | + "I_Crit->D", "I_Crit->R"}, |
| 116 | + 4, 4); |
| 117 | + std::cout << "With contacts doubled, the number of new transmissions (flow from S->E) in first time step is: " |
| 118 | + << result_d[1].get_value(1)[indx_flow_SE] << "\n"; |
| 119 | +} |
0 commit comments