1919*/
2020
2121#include " lct_secir/model.h"
22- #include " lct_secir/infection_state.h"
23- #include " lct_secir/simulation.h"
2422#include " memilio/config.h"
2523#include " memilio/utils/time_series.h"
2624#include " memilio/epidemiology/uncertain_matrix.h"
2725#include " memilio/math/eigen.h"
2826#include " memilio/utils/logging.h"
27+ #include " memilio/compartments/simulation.h"
28+ #include " memilio/data/analyze_result.h"
2929
3030#include < vector>
3131
3232int main ()
3333{
3434 // Simple example to demonstrate how to run a simulation using an LCT SECIR model.
3535 // Parameters, initial values and the number of subcompartments are not meant to represent a realistic scenario.
36+ constexpr size_t NumExposed = 2 , NumInfectedNoSymptoms = 3 , NumInfectedSymptoms = 1 , NumInfectedSevere = 1 ,
37+ NumInfectedCritical = 5 ;
38+ using Model = mio::lsecir::Model<NumExposed, NumInfectedNoSymptoms, NumInfectedSymptoms, NumInfectedSevere,
39+ NumInfectedCritical>;
40+ using LctState = Model::LctState;
41+ using InfectionState = LctState::InfectionState;
3642
37- using Model = mio::lsecir::Model<2 , 3 , 1 , 1 , 5 >;
38- using LctState = Model::LctState;
43+ Model model;
3944
4045 ScalarType tmax = 20 ;
4146
42- // Define the initial value vector init with the distribution of the population into subcompartments.
43- // This method of defining the vector using a vector of vectors is a bit of overhead , but should remind you how
44- // the entries of the initial value vector relate to the defined template parameters of the model or the number of subcompartments.
45- // It is also possible to define the initial value vector directly.
47+ // Define the initial values with the distribution of the population into subcompartments.
48+ // This method of defining the initial values using a vector of vectors is not necessary , but should remind you
49+ // how the entries of the initial value vector relate to the defined template parameters of the model or the number
50+ // of subcompartments. It is also possible to define the initial values directly.
4651 std::vector<std::vector<ScalarType>> initial_populations = {{750 }, {30 , 20 }, {20 , 10 , 10 }, {50 },
4752 {50 }, {10 , 10 , 5 , 3 , 2 }, {20 }, {10 }};
4853
4954 // Assert that initial_populations has the right shape.
50- if (initial_populations.size () != (size_t )LctState:: InfectionState::Count) {
55+ if (initial_populations.size () != (size_t )InfectionState::Count) {
5156 mio::log_error (" The number of vectors in initial_populations does not match the number of InfectionStates." );
5257 return 1 ;
5358 }
54- if ((initial_populations[(int )LctState::InfectionState::Susceptible].size () !=
55- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::Susceptible>()) ||
56- (initial_populations[(int )LctState::InfectionState::Exposed].size () !=
57- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::Exposed>()) ||
58- (initial_populations[(int )LctState::InfectionState::InfectedNoSymptoms].size () !=
59- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::InfectedNoSymptoms>()) ||
60- (initial_populations[(int )LctState::InfectionState::InfectedSymptoms].size () !=
61- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::InfectedSymptoms>()) ||
62- (initial_populations[(int )LctState::InfectionState::InfectedSevere].size () !=
63- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::InfectedSevere>()) ||
64- (initial_populations[(int )LctState::InfectionState::InfectedCritical].size () !=
65- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::InfectedCritical>()) ||
66- (initial_populations[(int )LctState::InfectionState::Recovered].size () !=
67- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::Recovered>()) ||
68- (initial_populations[(int )LctState::InfectionState::Dead].size () !=
69- (size_t )LctState::get_num_subcompartments<LctState::InfectionState::Dead>())) {
59+ if ((initial_populations[(size_t )InfectionState::Susceptible].size () !=
60+ LctState::get_num_subcompartments<InfectionState::Susceptible>()) ||
61+ (initial_populations[(size_t )InfectionState::Exposed].size () != NumExposed) ||
62+ (initial_populations[(size_t )InfectionState::InfectedNoSymptoms].size () != NumInfectedNoSymptoms) ||
63+ (initial_populations[(size_t )InfectionState::InfectedSymptoms].size () != NumInfectedSymptoms) ||
64+ (initial_populations[(size_t )InfectionState::InfectedSevere].size () != NumInfectedSevere) ||
65+ (initial_populations[(size_t )InfectionState::InfectedCritical].size () != NumInfectedCritical) ||
66+ (initial_populations[(size_t )InfectionState::Recovered].size () !=
67+ LctState::get_num_subcompartments<InfectionState::Recovered>()) ||
68+ (initial_populations[(size_t )InfectionState::Dead].size () !=
69+ LctState::get_num_subcompartments<InfectionState::Dead>())) {
7070 mio::log_error (" The length of at least one vector in initial_populations does not match the related number of "
7171 " subcompartments." );
7272 return 1 ;
7373 }
7474
75- // Transfer the initial values in initial_populations to the vector init.
76- Eigen::VectorXd init = Eigen::VectorXd::Zero (LctState::Count);
77- init[LctState::get_first_index<LctState::InfectionState::Susceptible>()] =
78- initial_populations[(int )LctState::InfectionState::Susceptible][0 ];
79- for (int i = 0 ; i < LctState::get_num_subcompartments<LctState::InfectionState::Exposed>(); i++) {
80- init[LctState::get_first_index<LctState::InfectionState::Exposed>() + i] =
81- initial_populations[(int )LctState::InfectionState::Exposed][i];
75+ // Transfer the initial values in initial_populations to the model.
76+ std::vector<ScalarType> flat_initial_populations;
77+ for (auto && vec : initial_populations) {
78+ flat_initial_populations.insert (flat_initial_populations.end (), vec.begin (), vec.end ());
8279 }
83- for (int i = 0 ; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedNoSymptoms>(); i++) {
84- init[LctState::get_first_index<LctState::InfectionState::InfectedNoSymptoms>() + i] =
85- initial_populations[(int )LctState::InfectionState::InfectedNoSymptoms][i];
80+ for (size_t i = 0 ; i < LctState::Count; i++) {
81+ model.populations [mio::Index<LctState>(i)] = flat_initial_populations[i];
8682 }
87- for (int i = 0 ; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedSymptoms>(); i++) {
88- init[LctState::get_first_index<LctState::InfectionState::InfectedSymptoms>() + i] =
89- initial_populations[(int )LctState::InfectionState::InfectedSymptoms][i];
90- }
91- for (int i = 0 ; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedSevere>(); i++) {
92- init[LctState::get_first_index<LctState::InfectionState::InfectedSevere>() + i] =
93- initial_populations[(int )LctState::InfectionState::InfectedSevere][i];
94- }
95- for (int i = 0 ; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedCritical>(); i++) {
96- init[LctState::get_first_index<LctState::InfectionState::InfectedCritical>() + i] =
97- initial_populations[(int )LctState::InfectionState::InfectedCritical][i];
98- }
99- init[LctState::get_first_index<LctState::InfectionState::Recovered>()] =
100- initial_populations[(int )LctState::InfectionState::Recovered][0 ];
101- init[LctState::get_first_index<LctState::InfectionState::Dead>()] =
102- initial_populations[(int )LctState::InfectionState::Dead][0 ];
103-
104- // Initialize model.
105- Model model (std::move (init));
10683
10784 // Set Parameters.
10885 model.parameters .get <mio::lsecir::TimeExposed>() = 3.2 ;
@@ -127,8 +104,10 @@ int main()
127104 model.parameters .set <mio::lsecir::DeathsPerCritical>(0.3 );
128105
129106 // Perform a simulation.
130- mio::TimeSeries<ScalarType> result = mio::lsecir::simulate (0 , tmax, 0.5 , model);
131- // Calculate the distribution in the InfectionState%s without subcompartments of the result and print it.
132- mio::TimeSeries<ScalarType> population_no_subcompartments = model.calculate_populations (result);
133- population_no_subcompartments.print_table ({" S" , " E" , " C" , " I" , " H" , " U" , " R" , " D " }, 16 , 8 );
107+ mio::TimeSeries<ScalarType> result = mio::simulate<ScalarType, Model>(0 , tmax, 0.5 , model);
108+ // The simulation result is divided by subcompartments.
109+ // We call the function calculate_comparttments to get a result according to the InfectionStates.
110+ mio::TimeSeries<ScalarType> population_no_subcompartments = model.calculate_compartments (result);
111+ auto interpolated_results = mio::interpolate_simulation_result (population_no_subcompartments);
112+ interpolated_results.print_table ({" S" , " E" , " C" , " I" , " H" , " U" , " R" , " D " }, 16 , 8 );
134113}
0 commit comments