-
Notifications
You must be signed in to change notification settings - Fork 16
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
1159 add diffusive abm and smm #1162
Open
jubicker
wants to merge
9
commits into
main
Choose a base branch
from
1159-add-diffusive-abm-and-smm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b8cedb
add diffusive ABM and tests
jubicker 294b601
read me diffusive abm
jubicker 9977cc8
add smm
jubicker 0b0ebe3
add tests for smm
jubicker 2b95423
smm readme
jubicker 8807c55
msvc is a special snowflake
jubicker 26f7258
examples smm and dabm
jubicker 428cffa
advance tests for smm and d_abm simulations
jubicker 985bd06
Merge branch 'main' into 1159-add-diffusive-abm-and-smm
jubicker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2020-2024 German Aerospace Center (DLR-SC) | ||
* | ||
* Authors: Julia Bicker, René Schmieding | ||
* | ||
* Contact: Martin J. Kuehn <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "d_abm/quad_well.h" | ||
#include "d_abm/simulation.h" | ||
#include "d_abm/parameters.h" | ||
#include "memilio/utils/random_number_generator.h" | ||
#include "memilio/data/analyze_result.h" | ||
#include <vector> | ||
|
||
enum class InfectionState | ||
{ | ||
S, | ||
E, | ||
C, | ||
I, | ||
R, | ||
D, | ||
Count | ||
|
||
}; | ||
|
||
int main() | ||
{ | ||
//Example how to run a simulation of the diffusive ABM using the quadwell potential | ||
using Model = mio::dabm::Model<QuadWellModel<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& 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.}; | ||
for (auto& a : agents) { | ||
//Agents are equally distributed in [-2,2]x[-2,2] at the beginning | ||
a.position = | ||
Eigen::Vector2d{pos_rng(mio::thread_local_rng(), -2., 2.), pos_rng(mio::thread_local_rng(), -2., 2.)}; | ||
a.status = static_cast<InfectionState>(sta_rng(mio::thread_local_rng(), pop_dist)); | ||
} | ||
|
||
//Set adoption rates | ||
std::vector<mio::dabm::AdoptionRate<InfectionState>> adoption_rates; | ||
for (size_t region = 0; region < 4; ++region) { | ||
adoption_rates.push_back({InfectionState::S, | ||
InfectionState::E, | ||
mio::dabm::Region(region), | ||
0.1, | ||
{InfectionState::C, InfectionState::I}, | ||
{1, 0.5}}); | ||
adoption_rates.push_back({InfectionState::E, InfectionState::C, mio::dabm::Region(region), 1.0 / 5., {}, {}}); | ||
adoption_rates.push_back({InfectionState::C, InfectionState::R, mio::dabm::Region(region), 0.2 / 3., {}, {}}); | ||
adoption_rates.push_back({InfectionState::C, InfectionState::I, mio::dabm::Region(region), 0.8 / 3., {}, {}}); | ||
adoption_rates.push_back({InfectionState::I, InfectionState::R, mio::dabm::Region(region), 0.99 / 5., {}, {}}); | ||
adoption_rates.push_back({InfectionState::I, InfectionState::D, mio::dabm::Region(region), 0.01 / 5., {}, {}}); | ||
} | ||
|
||
//Set interaction radius and noise term of the diffusion process | ||
double interaction_radius = 0.5; | ||
double noise = 0.4; | ||
|
||
double dt = 0.1; | ||
double tmax = 30.; | ||
|
||
Model model(agents, adoption_rates, interaction_radius, noise, {InfectionState::D}); | ||
auto sim = mio::Simulation<double, Model>(model, 0.0, dt); | ||
sim.advance(tmax); | ||
|
||
auto interpolated_results = mio::interpolate_simulation_result(sim.get_result()); | ||
interpolated_results.print_table({"S", "E", "C", "I", "R", "D "}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2020-2024 German Aerospace Center (DLR-SC) | ||
* | ||
* Authors: Julia Bicker, René Schmieding | ||
* | ||
* Contact: Martin J. Kuehn <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "smm/simulation.h" | ||
#include "smm/parameters.h" | ||
#include "memilio/data/analyze_result.h" | ||
|
||
enum class InfectionState | ||
{ | ||
S, | ||
E, | ||
C, | ||
I, | ||
R, | ||
D, | ||
Count | ||
|
||
}; | ||
|
||
int main() | ||
{ | ||
|
||
//Example how to run the stochastic metapopulation models with four regions | ||
const size_t num_regions = 4; | ||
using Model = mio::smm::Model<num_regions, InfectionState>; | ||
|
||
double numE = 12, numC = 4, numI = 12, numR = 0, numD = 0; | ||
|
||
Model model; | ||
//Population are distributed uniformly to the four regions | ||
for (size_t r = 0; r < num_regions; ++r) { | ||
model.populations[{mio::smm::Region(r), InfectionState::S}] = | ||
(1000 - numE - numC - numI - numR - numD) / num_regions; | ||
model.populations[{mio::smm::Region(r), InfectionState::E}] = numE / num_regions; | ||
model.populations[{mio::smm::Region(r), InfectionState::C}] = numC / num_regions; | ||
model.populations[{mio::smm::Region(r), InfectionState::I}] = numI / num_regions; | ||
model.populations[{mio::smm::Region(r), InfectionState::R}] = numR / num_regions; | ||
model.populations[{mio::smm::Region(r), InfectionState::D}] = numD / num_regions; | ||
} | ||
|
||
//Set infection state adoption and spatial transition rates | ||
std::vector<mio::smm::AdoptionRate<InfectionState>> adoption_rates; | ||
std::vector<mio::smm::TransitionRate<InfectionState>> transition_rates; | ||
for (size_t r = 0; r < num_regions; ++r) { | ||
adoption_rates.push_back({InfectionState::S, | ||
InfectionState::E, | ||
mio::smm::Region(r), | ||
0.1, | ||
{InfectionState::C, InfectionState::I}, | ||
{1, 0.5}}); | ||
adoption_rates.push_back({InfectionState::E, InfectionState::C, mio::smm::Region(r), 1.0 / 5., {}, {}}); | ||
adoption_rates.push_back({InfectionState::C, InfectionState::R, mio::smm::Region(r), 0.2 / 3., {}, {}}); | ||
adoption_rates.push_back({InfectionState::C, InfectionState::I, mio::smm::Region(r), 0.8 / 3., {}, {}}); | ||
adoption_rates.push_back({InfectionState::I, InfectionState::R, mio::smm::Region(r), 0.99 / 5., {}, {}}); | ||
adoption_rates.push_back({InfectionState::I, InfectionState::D, mio::smm::Region(r), 0.01 / 5., {}, {}}); | ||
} | ||
|
||
//Agents in infection state D do not transition | ||
for (size_t s = 0; s < static_cast<size_t>(InfectionState::D); ++s) { | ||
for (size_t i = 0; i < num_regions; ++i) { | ||
for (size_t j = 0; j < num_regions; ++j) | ||
if (i != j) { | ||
transition_rates.push_back({InfectionState(s), mio::smm::Region(i), mio::smm::Region(j), 0.01}); | ||
transition_rates.push_back({InfectionState(s), mio::smm::Region(j), mio::smm::Region(i), 0.01}); | ||
} | ||
} | ||
} | ||
|
||
model.parameters.get<mio::smm::AdoptionRates<InfectionState>>() = adoption_rates; | ||
model.parameters.get<mio::smm::TransitionRates<InfectionState>>() = transition_rates; | ||
|
||
double dt = 0.1; | ||
double tmax = 30.; | ||
|
||
auto sim = mio::Simulation<double, Model>(model, 0.0, dt); | ||
sim.advance(tmax); | ||
|
||
auto interpolated_results = mio::interpolate_simulation_result(sim.get_result()); | ||
interpolated_results.print_table({"S", "E", "C", "I", "R", "D "}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
# MEmilio Models # | ||
|
||
Contains different concrete models that are built using the MEmilio C++ library. See the corresponding directory for details about each model. | ||
Contains different concrete models that are built using the MEmilio C++ library. Some models contain a spatial resolution and some do not contain spatial resolution, hence cannot capture spatially heterogenous dynamics. | ||
The models with spatial resolution are: | ||
- abm | ||
- d_abm | ||
|
||
The models without spatial resolution are: | ||
- ode_* | ||
- ide_* | ||
- lct_* | ||
- glct_* | ||
- sde_* | ||
|
||
See the corresponding directory for details about each model. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_library(dabm | ||
model.h | ||
model.cpp | ||
simulation.h | ||
simulation.cpp | ||
parameters.h | ||
) | ||
target_link_libraries(dabm PUBLIC memilio) | ||
target_include_directories(dabm PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..> | ||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> | ||
) | ||
target_compile_options(dabm PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,14 @@ | ||||||||||||||
# Diffusive Agent-Based Model | ||||||||||||||
|
||||||||||||||
This agent-based model uses a Markov process to simulate disease dynamics. The features of an agent are position and infection state. The evolution of the system state is determined by the following master equation | ||||||||||||||
|
||||||||||||||
```math | ||||||||||||||
\partial_t p(X,Z;t) = G p(X,Z;t) + L p(X,Z;t) | ||||||||||||||
``` | ||||||||||||||
The operator $G$ defines the infection state adoptions and only acts on $Z$, while $L$ defines location changes, only acting on $X$. Infection state adoptions are modeled with independent Poisson processes given by adoption rate functions. Movement is modeled with independent diffusion processes. A temporal Gillespie algorithm is used for simulation. | ||||||||||||||
|
||||||||||||||
The Model class needs an Implementation class as template argument which prvides the domain agents move and interact in. We here implemented a quadwell potential given in the class QuadWellModel, but any other suitable potential can be used as implementation. | ||||||||||||||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would point out how L and G can be found in the ABM implementation. Maybe:
Suggested change
(also, a typo) |
||||||||||||||
|
||||||||||||||
## Simulation | ||||||||||||||
|
||||||||||||||
The simulation runs in discrete time steps. In every step we advance the model until the next infection state adoption event, then adopt the corresponding agent's infection state and draw a new waiting time until the next adoption event. If the waiting time until the next adoption event is bigger than the remaining time in the time step, we advance the model until the end of the time step. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (C) 2020-2024 German Aerospace Center (DLR-SC) | ||
* | ||
* Authors: René Schmieding | ||
* | ||
* Contact: Martin J. Kuehn <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "d_abm/model.h" | ||
|
||
namespace mio | ||
{ | ||
namespace dabm | ||
{ | ||
|
||
} // namespace dabm | ||
} // namespace mio |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep the underscore in the name, we keep it in other models as well.