Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1003 implement model in which the mobility is integrated into the ODEs #1128

Draft
wants to merge 61 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
2d4aec1
add model with Populations with Regions
charlie0614 Apr 11, 2024
9416256
working SIR model without infections during commuting
charlie0614 Apr 24, 2024
12c9c09
add infections during commuting and age groups
charlie0614 Apr 26, 2024
2b883b6
read in mobility data for new model
charlie0614 Apr 26, 2024
d49a048
only add edges if weight is big enough
charlie0614 Apr 26, 2024
56c4dd4
correct age resolution
charlie0614 May 8, 2024
0efa8db
add tests
charlie0614 Jun 10, 2024
05e3717
add seir version of mobility model
charlie0614 Jun 17, 2024
f475e3c
small adjustments sir version
charlie0614 Jun 17, 2024
68632c5
Merge branch 'main' into 1003-implement-model-in-which-the-mobility-i…
charlie0614 Jun 17, 2024
f19bcf6
fixes after merge
charlie0614 Jun 18, 2024
6ace8cf
Renaming
charlie0614 Jul 25, 2024
232f705
Renaming
charlie0614 Jul 25, 2024
6045e1f
Set up examples for comparison
charlie0614 Jul 25, 2024
dfdac2d
changes for plots and corrections
charlie0614 Jul 29, 2024
16a07da
add improved model
charlie0614 Sep 19, 2024
23196bc
changes for comparing simulations
charlie0614 Sep 19, 2024
4193cd6
Merge branch 'main' into 1003-implement-model-in-which-the-mobility-i…
charlie0614 Sep 26, 2024
bfc2562
adjust plot file
charlie0614 Sep 26, 2024
c6b56e9
reformat py file
charlie0614 Sep 26, 2024
5e51d69
py file
charlie0614 Sep 26, 2024
0eff4d1
py file
charlie0614 Sep 26, 2024
3d3b007
change commuting strengths to contact matrix and implement indicator …
charlie0614 Sep 30, 2024
9f0fba7
return to factor 0.5
charlie0614 Oct 7, 2024
8908d01
read in data and time measurement 400 counties
charlie0614 Oct 8, 2024
06950d9
small corrections model
charlie0614 Oct 8, 2024
aad78a9
corrections again
charlie0614 Oct 10, 2024
2bdb791
change integrator in graph model
charlie0614 Oct 10, 2024
3c6f630
fix bug with age groups and discard transmissions during commuting
charlie0614 Nov 7, 2024
aa6b393
adapt structure of old model
charlie0614 Nov 7, 2024
e6e6d70
add computation of basis reproduction number for old model
charlie0614 Nov 8, 2024
649467c
add computation of basis reproduction numbers and changes for countin…
charlie0614 Nov 20, 2024
4239fa7
small cleanup
charlie0614 Nov 28, 2024
2c4de48
read in population data
charlie0614 Dec 1, 2024
ce86fb6
debugging artefacts
charlie0614 Dec 1, 2024
d378118
more cleanup and provide demo population for a number of agegroups di…
charlie0614 Dec 2, 2024
33a2dd5
read in contact matrices, set age resolved parameters, restructure
charlie0614 Dec 2, 2024
c1bd747
add bool for using population from data
charlie0614 Dec 3, 2024
df22f54
read in data for graph model
charlie0614 Dec 3, 2024
7b30779
simulation for nrw, plots and a bug fix
charlie0614 Dec 12, 2024
f49a497
add timing example
charlie0614 Dec 13, 2024
64744ab
cmake changes for timing
charlie0614 Dec 13, 2024
a262104
fix bug for measuring runtimes and graph timing example
charlie0614 Dec 13, 2024
565141c
fix bug for graph timing
charlie0614 Dec 13, 2024
9f36912
fix things for runtime measurements
charlie0614 Dec 16, 2024
ee57c8f
add plotfiles, small correction in the model and maybe optimizations
charlie0614 Dec 18, 2024
d8b321f
mini optimizations and small changes for timing runs
charlie0614 Dec 19, 2024
7dc7e44
add simulations for paper model and comparison of basic reproduction …
charlie0614 Dec 19, 2024
90a8d3e
small changes in plots
charlie0614 Dec 19, 2024
c78c996
fix runtime maybe
charlie0614 Dec 20, 2024
cd056b5
adjust timing examples for single age group
charlie0614 Dec 31, 2024
fba237a
remove prints from simulations
charlie0614 Jan 9, 2025
363bf85
add examples for basic reproduction numbers, and measurements of steps
charlie0614 Jan 9, 2025
fd2bfdd
remove synthetic population for simulation
charlie0614 Jan 9, 2025
33bf7a9
small improvement model c
charlie0614 Jan 9, 2025
24883f3
remove steps from timing example
charlie0614 Jan 9, 2025
4db243d
changes in plots
charlie0614 Jan 9, 2025
8084deb
fix bug in model b
charlie0614 Jan 12, 2025
9b87803
fix bug in computation of basic reproduction number as well
charlie0614 Jan 12, 2025
f7f3eca
add likwid test example
Jan 14, 2025
c74474f
new likwid example
Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ if(MEMILIO_BUILD_MODELS)
add_subdirectory(models/ode_seir)
add_subdirectory(models/ode_seair)
add_subdirectory(models/ode_sir)
add_subdirectory(models/ode_sir_mobility)
# add_subdirectory(models/ode_seir_mobility_massaction)
add_subdirectory(models/ode_seir_mobility_improved)
add_subdirectory(models/ode_seir_mobility)
add_subdirectory(models/sde_sir)
add_subdirectory(models/sde_sirs)
add_subdirectory(models/sde_seirvv)
Expand Down
40 changes: 40 additions & 0 deletions cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ add_executable(sde_sir_example sde_sir.cpp)
target_link_libraries(sde_sir_example PRIVATE memilio sde_sir)
target_compile_options(sde_sir_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(ode_sir_mobility_example ode_sir_mobility.cpp)
target_link_libraries(ode_sir_mobility_example PRIVATE memilio ode_sir_mobility)
target_compile_options(ode_sir_mobility_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(ode_seir_mobility_example ode_seir_mobility.cpp)
target_link_libraries(ode_seir_mobility_example PRIVATE memilio ode_seir_mobility)
target_compile_options(ode_seir_mobility_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

if (MEMILIO_ENABLE_OPENMP)
add_executable(ode_seir_mobility_timing ode_seir_mobility_timing.cpp)
target_link_libraries(ode_seir_mobility_timing PRIVATE memilio ode_seir_mobility_improved)
target_compile_options(ode_seir_mobility_timing PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})
endif()

add_executable(ode_seir_mobility_steps ode_seir_mobility_steps.cpp)
target_link_libraries(ode_seir_mobility_steps PRIVATE memilio ode_seir_mobility_improved)
target_compile_options(ode_seir_mobility_steps PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(basic_reproduction_numbers basic_reproduction_numbers.cpp)
target_link_libraries(basic_reproduction_numbers PRIVATE memilio ode_seir)
target_compile_options(basic_reproduction_numbers PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(ode_seir_mobility_example_improved ode_seir_mobility_improved.cpp)
target_link_libraries(ode_seir_mobility_example_improved PRIVATE memilio ode_seir_mobility_improved)
target_compile_options(ode_seir_mobility_example_improved PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(sde_sirs_example sde_sirs.cpp)
target_link_libraries(sde_sirs_example PRIVATE memilio sde_sirs)
target_compile_options(sde_sirs_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})
Expand Down Expand Up @@ -84,6 +110,20 @@ add_executable(graph_example graph.cpp)
target_link_libraries(graph_example PRIVATE memilio ode_seir)
target_compile_options(graph_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(graph_example_extended graph_extended.cpp)
target_link_libraries(graph_example_extended PRIVATE memilio ode_seir)
target_compile_options(graph_example_extended PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

add_executable(graph_steps graph_steps.cpp)
target_link_libraries(graph_steps PRIVATE memilio ode_seir)
target_compile_options(graph_steps PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})

if (MEMILIO_ENABLE_OPENMP)
add_executable(graph_timing graph_timing.cpp)
target_link_libraries(graph_timing PRIVATE memilio ode_seir)
target_compile_options(graph_timing PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})
endif()

add_executable(graph_stochastic_mobility_example graph_stochastic_mobility.cpp)
target_link_libraries(graph_stochastic_mobility_example PRIVATE memilio ode_secir)
target_compile_options(graph_stochastic_mobility_example PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS})
Expand Down
196 changes: 196 additions & 0 deletions cpp/examples/basic_reproduction_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@

#include "models/ode_seir/model.h"
#include "models/ode_seir_mobility/model.h"
// #include "models/ode_seir_mobility_improved/model.h"

#include "memilio/math/euler.h"
#include "memilio/compartments/simulation.h"
#include "memilio/utils/custom_index_array.h"

Eigen::MatrixXd get_contact_matrix()
{
Eigen::MatrixXd contact_matrix_eigen(6, 6);
contact_matrix_eigen << 3.9547, 1.1002, 2.9472, 2.05, 0.3733, 0.0445, 0.3327, 3.5892, 1.236, 1.9208, 0.2681, 0.0161,
0.246, 0.7124, 5.6518, 3.2939, 0.2043, 0.0109, 0.1742, 0.8897, 3.3124, 4.5406, 0.4262, 0.0214, 0.0458, 0.1939,
0.5782, 1.3825, 1.473, 0.0704, 0.1083, 0.1448, 0.4728, 0.9767, 0.6266, 0.1724;

return contact_matrix_eigen;
}

const ScalarType TimeExposed[] = {3.335, 3.335, 3.335, 3.335, 3.335, 3.335};
const ScalarType TimeInfected[] = {8.0096875, 8.0096875, 8.2182, 8.1158, 8.033, 7.985};
const ScalarType TransmissionProbabilityOnContact[] = {0.03, 0.06, 0.06, 0.06, 0.09, 0.175};

void seir(size_t number_regions, ScalarType tmax)
{
mio::set_log_level(mio::LogLevel::off);
ScalarType t0 = 0.;
ScalarType dt = 0.1;
ScalarType number_age_groups = 6;

mio::oseir::Model<ScalarType> model(number_age_groups);
auto& population = model.populations;

for (size_t j = 0; j < number_age_groups; j++) {

population[{mio::AgeGroup(j), mio::oseir::InfectionState::Susceptible}] = number_regions * 10000;
}
population[{mio::AgeGroup(0), mio::oseir::InfectionState::Exposed}] += 100;
population[{mio::AgeGroup(0), mio::oseir::InfectionState::Susceptible}] -= 100;

mio::ContactMatrixGroup& contact_matrix =
model.parameters.template get<mio::oseir::ContactPatterns<>>().get_cont_freq_mat();
contact_matrix[0].get_baseline() = get_contact_matrix();

for (size_t j = 0; j < number_age_groups; j++) {
model.parameters.template get<mio::oseir::TimeExposed<>>()[mio::AgeGroup(j)] = TimeExposed[j];
model.parameters.template get<mio::oseir::TimeInfected<>>()[mio::AgeGroup(j)] = TimeInfected[j];
model.parameters.template get<mio::oseir::TransmissionProbabilityOnContact<>>()[mio::AgeGroup(j)] =
TransmissionProbabilityOnContact[j];
}

std::shared_ptr<mio::IntegratorCore<ScalarType>> integrator = std::make_shared<mio::EulerIntegratorCore<>>();

auto result = simulate(t0, tmax, dt, model, integrator);

auto basic_reproduction_number = model.get_reproduction_number(t0, result).value();
std::cout << "\"SEIR\": " << basic_reproduction_number << ", " << std::endl;
}

void wang(size_t number_regions, ScalarType tmax)
{
mio::set_log_level(mio::LogLevel::off);
ScalarType t0 = 0.;
ScalarType dt = 0.1;
ScalarType number_age_groups = 6;

mio::oseirmobility::Model<ScalarType> model(number_regions, number_age_groups);
auto& population = model.populations;

for (size_t j = 0; j < number_age_groups; j++) {
for (size_t i = 0; i < number_regions; i++) {
population[{mio::oseirmobility::Region(i), mio::AgeGroup(j),
mio::oseirmobility::InfectionState::Susceptible}] = 10000;
}
}
population[{mio::oseirmobility::Region(0), mio::AgeGroup(0), mio::oseirmobility::InfectionState::Exposed}] += 100;
population[{mio::oseirmobility::Region(0), mio::AgeGroup(0), mio::oseirmobility::InfectionState::Susceptible}] -=
100;

double fraction_commuter = 1. / (2 * number_regions);
Eigen::MatrixXd mobility_data_commuter =
Eigen::MatrixXd::Constant(number_regions, number_regions, fraction_commuter) -
fraction_commuter *
Eigen::MatrixXd::Identity(number_regions, number_regions); // Ensure that the diagonal is zero
for (size_t county_idx_i = 0; county_idx_i < number_regions; ++county_idx_i) {
mobility_data_commuter(county_idx_i, county_idx_i) = 1 - mobility_data_commuter.rowwise().sum()(county_idx_i);
}
model.parameters.template get<mio::oseirmobility::CommutingStrengths<>>().get_cont_freq_mat()[0].get_baseline() =
mobility_data_commuter;

mio::ContactMatrixGroup& contact_matrix =
model.parameters.template get<mio::oseirmobility::ContactPatterns<>>().get_cont_freq_mat();
contact_matrix[0].get_baseline() = get_contact_matrix();

for (size_t j = 0; j < number_age_groups; j++) {
model.parameters.template get<mio::oseirmobility::TimeExposed<>>()[mio::AgeGroup(j)] = TimeExposed[j];
model.parameters.template get<mio::oseirmobility::TimeInfected<>>()[mio::AgeGroup(j)] = TimeInfected[j];
model.parameters.template get<mio::oseirmobility::TransmissionProbabilityOnContact<>>()[mio::AgeGroup(j)] =
TransmissionProbabilityOnContact[j];
}

std::shared_ptr<mio::IntegratorCore<ScalarType>> integrator = std::make_shared<mio::EulerIntegratorCore<>>();

auto result = simulate(t0, tmax, dt, model, integrator);

auto basic_reproduction_number = model.get_reproduction_number(t0, result).value();
std::cout << "\"Wang\": " << basic_reproduction_number << "}" << std::endl;
}

// void metapopulation(size_t number_regions, ScalarType tmax)
// {
// mio::set_log_level(mio::LogLevel::off);
// ScalarType t0 = 0.;
// ScalarType dt = 0.1;
// ScalarType number_age_groups = 6;

// mio::oseirmobilityimproved::Model<ScalarType> model(number_regions, number_age_groups);
// auto& population = model.populations;

// for (size_t j = 0; j < number_age_groups; j++) {
// for (size_t i = 0; i < number_regions; i++) {
// population[{mio::oseirmobilityimproved::Region(i), mio::AgeGroup(j),
// mio::oseirmobilityimproved::InfectionState::Susceptible}] = 10000;
// }
// }
// population[{mio::oseirmobilityimproved::Region(0), mio::AgeGroup(0),
// mio::oseirmobilityimproved::InfectionState::Exposed}] += 100;
// population[{mio::oseirmobilityimproved::Region(0), mio::AgeGroup(0),
// mio::oseirmobilityimproved::InfectionState::Susceptible}] -= 100;

// double fraction_commuter = 1. / (2 * number_regions);
// Eigen::MatrixXd mobility_data_commuter =
// Eigen::MatrixXd::Constant(number_regions, number_regions, fraction_commuter) -
// fraction_commuter *
// Eigen::MatrixXd::Identity(number_regions, number_regions); // Ensure that the diagonal is zero
// for (size_t county_idx_i = 0; county_idx_i < number_regions; ++county_idx_i) {
// mobility_data_commuter(county_idx_i, county_idx_i) = 1 - mobility_data_commuter.rowwise().sum()(county_idx_i);
// }
// model.parameters.template get<mio::oseirmobilityimproved::CommutingStrengths<>>()
// .get_cont_freq_mat()[0]
// .get_baseline() = mobility_data_commuter;

// mio::ContactMatrixGroup& contact_matrix =
// model.parameters.template get<mio::oseirmobilityimproved::ContactPatterns<>>().get_cont_freq_mat();
// contact_matrix[0].get_baseline() = get_contact_matrix();

// for (size_t j = 0; j < number_age_groups; j++) {
// model.parameters.template get<mio::oseirmobilityimproved::TimeExposed<>>()[mio::AgeGroup(j)] = TimeExposed[j];
// model.parameters.template get<mio::oseirmobilityimproved::TimeInfected<>>()[mio::AgeGroup(j)] = TimeInfected[j];
// model.parameters
// .template get<mio::oseirmobilityimproved::TransmissionProbabilityOnContact<>>()[mio::AgeGroup(j)] =
// TransmissionProbabilityOnContact[j];
// }

// mio::ContactMatrixGroup& commuting_strengths =
// model.parameters.template get<mio::oseirmobilityimproved::CommutingStrengths<>>().get_cont_freq_mat();

// auto& population_after_commuting = model.m_population_after_commuting;
// for (size_t region_n = 0; region_n < number_regions; ++region_n) {
// for (size_t age = 0; age < number_age_groups; ++age) {
// double population_n = 0;
// for (size_t state = 0; state < (size_t)mio::oseirmobilityimproved::InfectionState::Count; state++) {
// population_n += population[{mio::oseirmobilityimproved::Region(region_n), mio::AgeGroup(age),
// mio::oseirmobilityimproved::InfectionState(state)}];
// }
// population_after_commuting[{mio::oseirmobilityimproved::Region(region_n), mio::AgeGroup(age)}] +=
// population_n;
// for (size_t region_m = 0; region_m < number_regions; ++region_m) {
// population_after_commuting[{mio::oseirmobilityimproved::Region(region_n), mio::AgeGroup(age)}] -=
// commuting_strengths[0].get_baseline()(region_n, region_m) * population_n;
// population_after_commuting[{mio::oseirmobilityimproved::Region(region_m), mio::AgeGroup(age)}] +=
// commuting_strengths[0].get_baseline()(region_n, region_m) * population_n;
// }
// }
// }

// std::shared_ptr<mio::IntegratorCore<ScalarType>> integrator = std::make_shared<mio::EulerIntegratorCore<>>();

// auto result = simulate(t0, tmax, dt, model, integrator);

// auto basic_reproduction_number = model.get_reproduction_number(t0, result).value();
// std::cout << "\"Metapopulation\": " << basic_reproduction_number << "}" << std::endl;
// }

int main()
{
const ScalarType tmax = 1.;
size_t num_regions = 150;

std::cout << "{ \"Regions\": " << num_regions << ", " << std::endl;

seir(num_regions, tmax);
wang(num_regions, tmax);
// metapopulation(num_regions, tmax);
return 0;
}
34 changes: 27 additions & 7 deletions cpp/examples/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@
#include "ode_seir/parameters.h"
#include "memilio/mobility/metapopulation_mobility_instant.h"
#include "memilio/compartments/simulation.h"
#include "memilio/io/result_io.h"

int main()
{
const auto t0 = 0.;
const auto tmax = 10.;
const auto tmax = 15.;
const auto dt = 0.5; //time step of mobility, daily mobility every second step

mio::oseir::Model<> model(1);

// set population
model.populations[{mio::AgeGroup(0), mio::oseir::InfectionState::Susceptible}] = 10000;

model.parameters.set<mio::oseir::TransmissionProbabilityOnContact<>>(1.);

// set transition times
model.parameters.set<mio::oseir::TimeExposed<>>(1);
model.parameters.set<mio::oseir::TimeInfected<>>(1);
model.parameters.set<mio::oseir::TimeExposed<>>(3.);
model.parameters.set<mio::oseir::TimeInfected<>>(5.);

// set contact matrix
mio::ContactMatrixGroup& contact_matrix = model.parameters.get<mio::oseir::ContactPatterns<>>().get_cont_freq_mat();
Expand All @@ -47,9 +50,9 @@ int main()
auto model_group2 = model;

//some contact restrictions in group 1
mio::ContactMatrixGroup& contact_matrix1 =
model_group1.parameters.get<mio::oseir::ContactPatterns<>>().get_cont_freq_mat();
contact_matrix1[0].add_damping(0.5, mio::SimulationTime(5));
// mio::ContactMatrixGroup& contact_matrix1 =
// model_group1.parameters.get<mio::oseir::ContactPatterns<>>().get_cont_freq_mat();
// contact_matrix1[0].add_damping(0.5, mio::SimulationTime(5));

//infection starts in group 1
model_group1.populations[{mio::AgeGroup(0), mio::oseir::InfectionState::Susceptible}] = 9990;
Expand All @@ -58,12 +61,29 @@ int main()
mio::Graph<mio::SimulationNode<mio::Simulation<ScalarType, mio::oseir::Model<>>>, mio::MobilityEdge<>> g;
g.add_node(1001, model_group1, t0);
g.add_node(1002, model_group2, t0);
g.add_edge(0, 1, Eigen::VectorXd::Constant((size_t)mio::oseir::InfectionState::Count, 0.01));
for (auto& node : g.nodes()) {
node.property.get_simulation().set_integrator(std::make_shared<mio::EulerIntegratorCore<ScalarType>>());
}
g.add_edge(0, 1, Eigen::VectorXd::Constant((size_t)mio::oseir::InfectionState::Count, 0.05));
g.add_edge(1, 0, Eigen::VectorXd::Constant((size_t)mio::oseir::InfectionState::Count, 0.01));

auto sim = mio::make_mobility_sim(t0, dt, std::move(g));

sim.advance(tmax);

auto result_graph = std::move(sim).get_graph();
auto result = mio::interpolate_simulation_result(result_graph);

std::vector<int> county_ids(result_graph.nodes().size());
std::transform(result_graph.nodes().begin(), result_graph.nodes().end(), county_ids.begin(), [](auto& n) {
return n.id;
});

auto save_result_status = save_result(result, county_ids, 1, "graph_result.h5");

for (auto&& node : result_graph.nodes()) {
node.property.get_result().print_table();
}

return 0;
}
Loading