|
| 1 | +/* |
| 2 | +* Copyright (C) 2020-2023 German Aerospace Center (DLR-SC) |
| 3 | +* |
| 4 | +* Authors: Daniel Abele, Jan Kleinert, 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 | + |
| 21 | +#include "ode_sir/model.h" |
| 22 | +#include "ode_sir/infection_state.h" |
| 23 | +#include "ode_sir/parameters.h" |
| 24 | +#include "memilio/compartments/simulation.h" |
| 25 | +#include "memilio/utils/logging.h" |
| 26 | + |
| 27 | +int main() |
| 28 | +{ |
| 29 | + mio::set_log_level(mio::LogLevel::debug); |
| 30 | + |
| 31 | + double t0 = 0.; |
| 32 | + double tmax = 50.; |
| 33 | + double dt = 0.1002004008016032; |
| 34 | + |
| 35 | + double total_population = 1061000; |
| 36 | + |
| 37 | + mio::log_info("Simulating SIR; t={} ... {} with dt = {}.", t0, tmax, dt); |
| 38 | + |
| 39 | + mio::osir::Model model; |
| 40 | + |
| 41 | + model.populations[{mio::Index<mio::osir::InfectionState>(mio::osir::InfectionState::Infected)}] = 1000; |
| 42 | + model.populations[{mio::Index<mio::osir::InfectionState>(mio::osir::InfectionState::Recovered)}] = 1000; |
| 43 | + model.populations[{mio::Index<mio::osir::InfectionState>(mio::osir::InfectionState::Susceptible)}] = |
| 44 | + total_population - |
| 45 | + model.populations[{mio::Index<mio::osir::InfectionState>(mio::osir::InfectionState::Infected)}] - |
| 46 | + model.populations[{mio::Index<mio::osir::InfectionState>(mio::osir::InfectionState::Recovered)}]; |
| 47 | + model.parameters.set<mio::osir::TimeInfected>(2); |
| 48 | + model.parameters.set<mio::osir::TransmissionProbabilityOnContact>(1); |
| 49 | + model.parameters.get<mio::osir::ContactPatterns>().get_baseline()(0, 0) = 2.7; |
| 50 | + model.parameters.get<mio::osir::ContactPatterns>().add_damping(0.6, mio::SimulationTime(12.5)); |
| 51 | + |
| 52 | + auto integrator = std::make_shared<mio::EulerIntegratorCore>(); |
| 53 | + |
| 54 | + model.check_constraints(); |
| 55 | + |
| 56 | + auto sir = simulate(t0, tmax, dt, model, integrator); |
| 57 | + |
| 58 | + bool print_to_terminal = true; |
| 59 | + |
| 60 | + if (print_to_terminal) { |
| 61 | + std::vector<std::string> vars = {"S", "I", "R"}; |
| 62 | + printf("\n # t"); |
| 63 | + for (size_t k = 0; k < (size_t)mio::osir::InfectionState::Count; k++) { |
| 64 | + printf(" %s", vars[k].c_str()); |
| 65 | + } |
| 66 | + |
| 67 | + auto num_points = static_cast<size_t>(sir.get_num_time_points()); |
| 68 | + for (size_t i = 0; i < num_points; i++) { |
| 69 | + printf("\n%.14f ", sir.get_time(i)); |
| 70 | + Eigen::VectorXd res_j = sir.get_value(i); |
| 71 | + for (size_t j = 0; j < (size_t)mio::osir::InfectionState::Count; j++) { |
| 72 | + printf(" %.14f", res_j[j]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Eigen::VectorXd res_j = sir.get_last_value(); |
| 77 | + printf("number total: %f", res_j[0] + res_j[1] + res_j[2]); |
| 78 | + } |
| 79 | +} |
0 commit comments