-
Notifications
You must be signed in to change notification settings - Fork 19
1159 add diffusive abm and smm #1162
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 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 4313cf6
add smm tests
jubicker 6f0b6fd
add documentation for smm tests
jubicker 136be89
enhance tests
jubicker b747a13
review
jubicker bd40f9f
Apply suggestions from code review
jubicker 88407a6
Review
jubicker da23351
CI
jubicker eab5ddd
CI
jubicker 72277d6
CI
jubicker e265e84
CI
jubicker 63d3601
CI
jubicker 37f062b
CI
jubicker e6a3516
ci smm
jubicker 67a939f
smm stops at tmax test
jubicker 0d33105
CI
jubicker dcf6c81
ci
jubicker 648efa4
sim advance
jubicker 568adec
fix sanitize bug
jubicker 12dd3e4
Review
jubicker 5f43aa0
Review and documentation
jubicker 8cc5a22
Update cpp/memilio/epidemiology/adoption_rate.h
jubicker 75fc32e
merge main
jubicker 3740a60
Review
jubicker 99905fe
add comment to dabm parameters.h
jubicker c04573b
test num transitions dabm
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 hidden or 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 hidden or 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 hidden or 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 <Martin.Kuehn@DLR.de> | ||
| * | ||
| * 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 "memilio/epidemiology/adoption_rate.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::AdoptionRate<InfectionState>> adoption_rates; | ||
| for (size_t region = 0; region < 4; ++region) { | ||
| adoption_rates.push_back({InfectionState::S, | ||
| InfectionState::E, | ||
| mio::regions::Region(region), | ||
| 0.1, | ||
| {{InfectionState::C, 1}, {InfectionState::I, 0.5}}}); | ||
| adoption_rates.push_back({InfectionState::E, InfectionState::C, mio::regions::Region(region), 1.0 / 5., {}}); | ||
| adoption_rates.push_back({InfectionState::C, InfectionState::R, mio::regions::Region(region), 0.2 / 3., {}}); | ||
| adoption_rates.push_back({InfectionState::C, InfectionState::I, mio::regions::Region(region), 0.8 / 3., {}}); | ||
| adoption_rates.push_back({InfectionState::I, InfectionState::R, mio::regions::Region(region), 0.99 / 5., {}}); | ||
| adoption_rates.push_back({InfectionState::I, InfectionState::D, mio::regions::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::dabm::Simulation(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 hidden or 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,99 @@ | ||
| /* | ||
| * Copyright (C) 2020-2024 German Aerospace Center (DLR-SC) | ||
| * | ||
| * Authors: Julia Bicker, René Schmieding | ||
| * | ||
| * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||
| * | ||
| * 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" | ||
| #include "memilio/epidemiology/adoption_rate.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::regions::Region(r), InfectionState::S}] = | ||
| (1000 - numE - numC - numI - numR - numD) / num_regions; | ||
| model.populations[{mio::regions::Region(r), InfectionState::E}] = numE / num_regions; | ||
| model.populations[{mio::regions::Region(r), InfectionState::C}] = numC / num_regions; | ||
| model.populations[{mio::regions::Region(r), InfectionState::I}] = numI / num_regions; | ||
| model.populations[{mio::regions::Region(r), InfectionState::R}] = numR / num_regions; | ||
| model.populations[{mio::regions::Region(r), InfectionState::D}] = numD / num_regions; | ||
| } | ||
|
|
||
| //Set infection state adoption and spatial transition rates | ||
| std::vector<mio::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::regions::Region(r), | ||
| 0.1, | ||
| {{InfectionState::C, 1}, {InfectionState::I, 0.5}}}); | ||
| adoption_rates.push_back({InfectionState::E, InfectionState::C, mio::regions::Region(r), 1.0 / 5., {}}); | ||
| adoption_rates.push_back({InfectionState::C, InfectionState::R, mio::regions::Region(r), 0.2 / 3., {}}); | ||
| adoption_rates.push_back({InfectionState::C, InfectionState::I, mio::regions::Region(r), 0.8 / 3., {}}); | ||
| adoption_rates.push_back({InfectionState::I, InfectionState::R, mio::regions::Region(r), 0.99 / 5., {}}); | ||
| adoption_rates.push_back({InfectionState::I, InfectionState::D, mio::regions::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::regions::Region(i), mio::regions::Region(j), 0.01}); | ||
| transition_rates.push_back( | ||
| {InfectionState(s), mio::regions::Region(j), mio::regions::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::smm::Simulation(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 hidden or 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 hidden or 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,59 @@ | ||
| /* | ||
| * Copyright (C) 2020-2025 MEmilio | ||
| * | ||
| * Authors: René Schmieding, Julia Bicker | ||
| * | ||
| * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> | ||
| * | ||
| * 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. | ||
| */ | ||
| #ifndef MIO_EPI_ADOPTIONRATE_H | ||
| #define MIO_EPI_ADOPTIONRATE_H | ||
|
|
||
| #include "memilio/utils/index.h" | ||
| #include "memilio/config.h" | ||
| #include "memilio/geography/regions.h" | ||
|
|
||
| namespace mio | ||
| { | ||
|
|
||
| /** | ||
| * @brief Struct defining an influence for a second-order adoption. | ||
| * The population having "status" is multiplied with "factor." | ||
| * @tparam Status An infection state enum. | ||
| */ | ||
| template <class Status> | ||
| struct Influence { | ||
| Status status; | ||
| ScalarType factor; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Struct defining a possible status adoption in a Model based on Poisson Processes. | ||
| * The AdoptionRate is considered to be of second-order if there are any "influences". | ||
| * In the d_abm and smm simulations, "from" is implicitly an influence, scaled by "factor". This is multiplied by | ||
| * the sum over all "influences", which scale their "status" with the respective "factor". | ||
| * @tparam Status An infection state enum. | ||
| */ | ||
| template <class Status> | ||
| struct AdoptionRate { | ||
| Status from; // i | ||
| Status to; // j | ||
| mio::regions::Region region; // k | ||
| ScalarType factor; // gammahat_{ij}^k | ||
| std::vector<Influence<Status>> influences; // influences[tau] = ( Psi_{i,j,tau} , gamma_{i,j,tau} ) | ||
| }; | ||
|
|
||
| } // namespace mio | ||
|
|
||
| #endif |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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(d_abm | ||
| model.h | ||
| model.cpp | ||
| simulation.h | ||
| simulation.cpp | ||
| parameters.h | ||
| ) | ||
| target_link_libraries(d_abm PUBLIC memilio) | ||
| target_include_directories(d_abm PUBLIC | ||
| $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..> | ||
| $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> | ||
| ) | ||
| target_compile_options(d_abm PRIVATE ${MEMILIO_CXX_FLAGS_ENABLE_WARNING_ERRORS}) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.