Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 7 additions & 6 deletions cpp/examples/d_abm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "d_abm/quad_well.h"
#include "d_abm/simulation.h"
#include "d_abm/parameters.h"
#include "memilio/config.h"
#include "memilio/utils/random_number_generator.h"
#include "memilio/data/analyze_result.h"
#include "memilio/epidemiology/adoption_rate.h"
Expand All @@ -44,10 +45,10 @@ int main()
using Model = mio::dabm::Model<QuadWell<InfectionState>>;
std::vector<Model::Agent> agents(1000);
//Random variables for initialization of agents' position and infection state
auto& pos_rng = mio::UniformDistribution<double>::get_instance();
auto& pos_rng = mio::UniformDistribution<ScalarType>::get_instance();
auto& sta_rng = mio::DiscreteDistribution<size_t>::get_instance();
//Infection state distribution
std::vector<double> pop_dist{0.98, 0.01, 0.005, 0.005, 0., 0.};
std::vector<ScalarType> pop_dist{0.98, 0.01, 0.005, 0.005, 0., 0.};
for (auto& a : agents) {
//Agents are equally distributed in [-2,2]x[-2,2] at the beginning
a.position =
Expand All @@ -71,11 +72,11 @@ int main()
}

//Set interaction radius and noise term of the diffusion process
double interaction_radius = 0.5;
double noise = 0.4;
ScalarType interaction_radius = 0.5;
ScalarType noise = 0.4;

double dt = 0.1;
double tmax = 30.;
ScalarType dt = 0.1;
ScalarType tmax = 30.;

Model model(agents, adoption_rates, interaction_radius, noise, {InfectionState::D});
auto sim = mio::dabm::Simulation(model, 0.0, dt);
Expand Down
65 changes: 34 additions & 31 deletions cpp/examples/ode_secir_feedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,33 @@
#include "memilio/compartments/feedback_simulation.h"
#include "memilio/utils/logging.h"

void initialize_model(mio::osecir::Model<double>& model, int total_population, double cont_freq)
void initialize_model(mio::osecir::Model<ScalarType>& model, int total_population, ScalarType cont_freq)
{
model.parameters.set<mio::osecir::StartDay<double>>(60);
model.parameters.set<mio::osecir::Seasonality<double>>(0.2);
model.parameters.set<mio::osecir::StartDay<ScalarType>>(60);
model.parameters.set<mio::osecir::Seasonality<ScalarType>>(0.2);

// time-related parameters
model.parameters.get<mio::osecir::TimeExposed<double>>() = 3.2;
model.parameters.get<mio::osecir::TimeInfectedNoSymptoms<double>>() = 2.0;
model.parameters.get<mio::osecir::TimeInfectedSymptoms<double>>() = 5.8;
model.parameters.get<mio::osecir::TimeInfectedSevere<double>>() = 9.5;
model.parameters.get<mio::osecir::TimeInfectedCritical<double>>() = 7.1;
model.parameters.get<mio::osecir::TimeExposed<ScalarType>>() = 3.2;
model.parameters.get<mio::osecir::TimeInfectedNoSymptoms<ScalarType>>() = 2.0;
model.parameters.get<mio::osecir::TimeInfectedSymptoms<ScalarType>>() = 5.8;
model.parameters.get<mio::osecir::TimeInfectedSevere<ScalarType>>() = 9.5;
model.parameters.get<mio::osecir::TimeInfectedCritical<ScalarType>>() = 7.1;

// Set transmission and isolation parameters
model.parameters.get<mio::osecir::TransmissionProbabilityOnContact<double>>() = 0.05;
model.parameters.get<mio::osecir::RelativeTransmissionNoSymptoms<double>>() = 0.7;
model.parameters.get<mio::osecir::RecoveredPerInfectedNoSymptoms<double>>() = 0.09;
model.parameters.get<mio::osecir::RiskOfInfectionFromSymptomatic<double>>() = 0.25;
model.parameters.get<mio::osecir::MaxRiskOfInfectionFromSymptomatic<double>>() = 0.45;
model.parameters.get<mio::osecir::TestAndTraceCapacity<double>>() = 35;
model.parameters.get<mio::osecir::SeverePerInfectedSymptoms<double>>() = 0.2;
model.parameters.get<mio::osecir::CriticalPerSevere<double>>() = 0.25;
model.parameters.get<mio::osecir::DeathsPerCritical<double>>() = 0.3;
model.parameters.get<mio::osecir::TransmissionProbabilityOnContact<ScalarType>>() = 0.05;
model.parameters.get<mio::osecir::RelativeTransmissionNoSymptoms<ScalarType>>() = 0.7;
model.parameters.get<mio::osecir::RecoveredPerInfectedNoSymptoms<ScalarType>>() = 0.09;
model.parameters.get<mio::osecir::RiskOfInfectionFromSymptomatic<ScalarType>>() = 0.25;
model.parameters.get<mio::osecir::MaxRiskOfInfectionFromSymptomatic<ScalarType>>() = 0.45;
model.parameters.get<mio::osecir::TestAndTraceCapacity<ScalarType>>() = 35;
model.parameters.get<mio::osecir::SeverePerInfectedSymptoms<ScalarType>>() = 0.2;
model.parameters.get<mio::osecir::CriticalPerSevere<ScalarType>>() = 0.25;
model.parameters.get<mio::osecir::DeathsPerCritical<ScalarType>>() = 0.3;

// contact matrix
mio::ContactMatrixGroup<double>& contact_matrix = model.parameters.get<mio::osecir::ContactPatterns<double>>();
contact_matrix[0] = mio::ContactMatrix<double>(Eigen::MatrixXd::Constant(1, 1, cont_freq));
mio::ContactMatrixGroup<ScalarType>& contact_matrix =
model.parameters.get<mio::osecir::ContactPatterns<ScalarType>>();
contact_matrix[0] = mio::ContactMatrix<ScalarType>(Eigen::MatrixXd::Constant(1, 1, cont_freq));

// initial population
model.populations[{mio::AgeGroup(0), mio::osecir::InfectionState::Exposed}] = 40;
Expand All @@ -65,23 +66,24 @@ void initialize_model(mio::osecir::Model<double>& model, int total_population, d
model.apply_constraints();
}

void initialize_feedback(mio::FeedbackSimulation<double, mio::Simulation<double, mio::osecir::Model<double>>,
mio::osecir::ContactPatterns<double>>& feedback_simulation)
void initialize_feedback(
mio::FeedbackSimulation<ScalarType, mio::Simulation<ScalarType, mio::osecir::Model<ScalarType>>,
mio::osecir::ContactPatterns<ScalarType>>& feedback_simulation)
{
// nominal ICU capacity
feedback_simulation.get_parameters().template get<mio::NominalICUCapacity<double>>() = 10;
feedback_simulation.get_parameters().template get<mio::NominalICUCapacity<ScalarType>>() = 10;

// ICU occupancy in the past for memory kernel
auto& icu_occupancy = feedback_simulation.get_parameters().template get<mio::ICUOccupancyHistory<double>>();
auto& icu_occupancy = feedback_simulation.get_parameters().template get<mio::ICUOccupancyHistory<ScalarType>>();
Eigen::VectorXd icu_day = Eigen::VectorXd::Constant(1, 1);
const auto cutoff = static_cast<int>(feedback_simulation.get_parameters().template get<mio::GammaCutOff>());
for (int t = -cutoff; t <= 0; ++t) {
icu_occupancy.add_time_point(t, icu_day);
}

// bounds for contact reduction measures
feedback_simulation.get_parameters().template get<mio::ContactReductionMin<double>>() = {0.1};
feedback_simulation.get_parameters().template get<mio::ContactReductionMax<double>>() = {0.8};
feedback_simulation.get_parameters().template get<mio::ContactReductionMin<ScalarType>>() = {0.1};
feedback_simulation.get_parameters().template get<mio::ContactReductionMax<ScalarType>>() = {0.8};
}

int main()
Expand All @@ -92,23 +94,24 @@ int main()
// risk which is calculated from the ICU occupancy using a memory kernel.
mio::set_log_level(mio::LogLevel::warn);

const double tmax = 35;
const ScalarType tmax = 35;
const int total_population = 1000;
const double cont_freq = 10;
const ScalarType cont_freq = 10;

// create and initialize ODE model for a single age group
mio::osecir::Model<double> model(1);
mio::osecir::Model<ScalarType> model(1);
initialize_model(model, total_population, cont_freq);

// determine the index for the ICU state (InfectedCritical) for feedback mechanism
auto icu_index = std::vector<size_t>{
model.populations.get_flat_index({mio::AgeGroup(0), mio::osecir::InfectionState::InfectedCritical})};

// create simulation objects: first a secir simulation, then a feedback simulation
auto simulation = mio::osecir::Simulation<double, mio::Simulation<double, mio::osecir::Model<double>>>(model);
auto simulation =
mio::osecir::Simulation<ScalarType, mio::Simulation<ScalarType, mio::osecir::Model<ScalarType>>>(model);
auto feedback_simulation =
mio::FeedbackSimulation<double, mio::Simulation<double, mio::osecir::Model<double>>,
mio::osecir::ContactPatterns<double>>(std::move(simulation), icu_index);
mio::FeedbackSimulation<ScalarType, mio::Simulation<ScalarType, mio::osecir::Model<ScalarType>>,
mio::osecir::ContactPatterns<ScalarType>>(std::move(simulation), icu_index);

// set up the parameters for the feedback simulation
initialize_feedback(feedback_simulation);
Expand Down
69 changes: 35 additions & 34 deletions cpp/examples/ode_secir_feedback_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,72 @@
#include <iostream>

// alias for the type of the simulation with feedback
using FeedbackSim = mio::FeedbackSimulation<double, mio::Simulation<double, mio::osecir::Model<double>>,
mio::osecir::ContactPatterns<double>>;
using FeedbackSim = mio::FeedbackSimulation<ScalarType, mio::Simulation<ScalarType, mio::osecir::Model<ScalarType>>,
mio::osecir::ContactPatterns<ScalarType>>;

// helper function to initialize the model with population and parameters
void initialize_model(mio::osecir::Model<double>& model, double cont_freq)
void initialize_model(mio::osecir::Model<ScalarType>& model, ScalarType cont_freq)
{
model.parameters.set<mio::osecir::StartDay<double>>(60);
model.parameters.set<mio::osecir::Seasonality<double>>(0.2);
model.parameters.set<mio::osecir::StartDay<ScalarType>>(60);
model.parameters.set<mio::osecir::Seasonality<ScalarType>>(0.2);

// Mean stay times per compartment
model.parameters.get<mio::osecir::TimeExposed<double>>() = 3.2;
model.parameters.get<mio::osecir::TimeInfectedNoSymptoms<double>>() = 2.0;
model.parameters.get<mio::osecir::TimeInfectedSymptoms<double>>() = 5.8;
model.parameters.get<mio::osecir::TimeInfectedSevere<double>>() = 9.5;
model.parameters.get<mio::osecir::TimeInfectedCritical<double>>() = 7.1;
model.parameters.get<mio::osecir::TimeExposed<ScalarType>>() = 3.2;
model.parameters.get<mio::osecir::TimeInfectedNoSymptoms<ScalarType>>() = 2.0;
model.parameters.get<mio::osecir::TimeInfectedSymptoms<ScalarType>>() = 5.8;
model.parameters.get<mio::osecir::TimeInfectedSevere<ScalarType>>() = 9.5;
model.parameters.get<mio::osecir::TimeInfectedCritical<ScalarType>>() = 7.1;

// Set transmission and isolation parameters
model.parameters.get<mio::osecir::TransmissionProbabilityOnContact<double>>() = 0.05;
model.parameters.get<mio::osecir::RelativeTransmissionNoSymptoms<double>>() = 0.7;
model.parameters.get<mio::osecir::RecoveredPerInfectedNoSymptoms<double>>() = 0.09;
model.parameters.get<mio::osecir::RiskOfInfectionFromSymptomatic<double>>() = 0.25;
model.parameters.get<mio::osecir::MaxRiskOfInfectionFromSymptomatic<double>>() = 0.45;
model.parameters.get<mio::osecir::TestAndTraceCapacity<double>>() = 35;
model.parameters.get<mio::osecir::SeverePerInfectedSymptoms<double>>() = 0.2;
model.parameters.get<mio::osecir::CriticalPerSevere<double>>() = 0.25;
model.parameters.get<mio::osecir::DeathsPerCritical<double>>() = 0.3;
model.parameters.get<mio::osecir::TransmissionProbabilityOnContact<ScalarType>>() = 0.05;
model.parameters.get<mio::osecir::RelativeTransmissionNoSymptoms<ScalarType>>() = 0.7;
model.parameters.get<mio::osecir::RecoveredPerInfectedNoSymptoms<ScalarType>>() = 0.09;
model.parameters.get<mio::osecir::RiskOfInfectionFromSymptomatic<ScalarType>>() = 0.25;
model.parameters.get<mio::osecir::MaxRiskOfInfectionFromSymptomatic<ScalarType>>() = 0.45;
model.parameters.get<mio::osecir::TestAndTraceCapacity<ScalarType>>() = 35;
model.parameters.get<mio::osecir::SeverePerInfectedSymptoms<ScalarType>>() = 0.2;
model.parameters.get<mio::osecir::CriticalPerSevere<ScalarType>>() = 0.25;
model.parameters.get<mio::osecir::DeathsPerCritical<ScalarType>>() = 0.3;

// contact matrix
mio::ContactMatrixGroup<double>& contact_matrix = model.parameters.get<mio::osecir::ContactPatterns<double>>();
contact_matrix[0] = mio::ContactMatrix<double>(Eigen::MatrixXd::Constant(1, 1, cont_freq));
mio::ContactMatrixGroup<ScalarType>& contact_matrix =
model.parameters.get<mio::osecir::ContactPatterns<ScalarType>>();
contact_matrix[0] = mio::ContactMatrix<ScalarType>(Eigen::MatrixXd::Constant(1, 1, cont_freq));
}

// helper function to initialize the feedback mechanism parameters for a simulation
void initialize_feedback(FeedbackSim& feedback_simulation)
{
// nominal ICU capacity
feedback_simulation.get_parameters().template get<mio::NominalICUCapacity<double>>() = 10;
feedback_simulation.get_parameters().template get<mio::NominalICUCapacity<ScalarType>>() = 10;

// ICU occupancy in the past for memory kernel
auto& icu_occupancy = feedback_simulation.get_parameters().template get<mio::ICUOccupancyHistory<double>>();
auto& icu_occupancy = feedback_simulation.get_parameters().template get<mio::ICUOccupancyHistory<ScalarType>>();
Eigen::VectorXd icu_day = Eigen::VectorXd::Constant(1, 1);
const auto cutoff = static_cast<int>(feedback_simulation.get_parameters().template get<mio::GammaCutOff>());
for (int t = -cutoff; t <= 0; ++t) {
icu_occupancy.add_time_point(t, icu_day);
}

// bounds for contact reduction measures
feedback_simulation.get_parameters().template get<mio::ContactReductionMin<double>>() = {0.1};
feedback_simulation.get_parameters().template get<mio::ContactReductionMax<double>>() = {0.8};
feedback_simulation.get_parameters().template get<mio::ContactReductionMin<ScalarType>>() = {0.1};
feedback_simulation.get_parameters().template get<mio::ContactReductionMax<ScalarType>>() = {0.8};

// Set blending factors. The global blending factor is implicitly defined as 1 - local - regional.
feedback_simulation.get_parameters().template get<mio::BlendingFactorLocal<double>>() = 0.5;
feedback_simulation.get_parameters().template get<mio::BlendingFactorRegional<double>>() = 0.3;
feedback_simulation.get_parameters().template get<mio::BlendingFactorLocal<ScalarType>>() = 0.5;
feedback_simulation.get_parameters().template get<mio::BlendingFactorRegional<ScalarType>>() = 0.3;
}

// helper function to create the graph with nodes and edges
mio::Graph<mio::SimulationNode<double, FeedbackSim>, mio::MobilityEdge<double>>
create_graph(int num_nodes, int total_population, double cont_freq)
mio::Graph<mio::SimulationNode<ScalarType, FeedbackSim>, mio::MobilityEdge<ScalarType>>
create_graph(int num_nodes, int total_population, ScalarType cont_freq)
{
// Create a graph for the metapopulation simulation
mio::Graph<mio::SimulationNode<double, FeedbackSim>, mio::MobilityEdge<double>> g;
mio::Graph<mio::SimulationNode<ScalarType, FeedbackSim>, mio::MobilityEdge<ScalarType>> g;

// Create models and add nodes to the graph
for (int i = 0; i < num_nodes; ++i) {
mio::osecir::Model<double> model(1);
mio::osecir::Model<ScalarType> model(1);
initialize_model(model, cont_freq);

// Set initial populations (infection starts in the first node)
Expand Down Expand Up @@ -120,7 +121,7 @@ create_graph(int num_nodes, int total_population, double cont_freq)
model.populations.get_flat_index({mio::AgeGroup(0), mio::osecir::InfectionState::InfectedCritical})};

// Create feedback simulation
auto feedback_sim = FeedbackSim(mio::Simulation<double, mio::osecir::Model<double>>(model), icu_index);
auto feedback_sim = FeedbackSim(mio::Simulation<ScalarType, mio::osecir::Model<ScalarType>>(model), icu_index);
initialize_feedback(feedback_sim);

// Node-ID-Logic: 1001-1005, 2001-2005, ...
Expand Down Expand Up @@ -153,15 +154,15 @@ int main()
const auto tmax = 10.;
const auto dt = 0.5;
const int total_population = 1000;
const double cont_freq = 2.7;
const ScalarType cont_freq = 2.7;
const int num_nodes = 10;

// Create the graph
auto g = create_graph(num_nodes, total_population, cont_freq);

// Create and run the simulation
using Graph = decltype(g);
auto sim = mio::FeedbackGraphSimulation<double, Graph>(std::move(g), t0, dt);
auto sim = mio::FeedbackGraphSimulation<ScalarType, Graph>(std::move(g), t0, dt);
sim.advance(tmax);

// The output shows the compartments sizes for a node without any initial infections.
Expand Down
Loading
Loading