1 #ifndef UTOPIA_MODELS_CONTDISEASE_HH
2 #define UTOPIA_MODELS_CONTDISEASE_HH
51 public Model<ContDisease, CDTypes>
129 template<
class ParentModel>
131 const std::string& name,
132 ParentModel& parent_model,
137 Base(name, parent_model, custom_cfg),
166 _densities.fill(std::numeric_limits<double>::quiet_NaN());
173 if (
_cfg[
"stones"] and get_as<bool>(
"enabled",
_cfg[
"stones"])) {
174 this->
_log->info(
"Setting cells to be stones ...");
180 apply_rule<Update::async, Shuffle::off>(
181 [](
const auto& cell){
182 auto& state = cell->state;
189 this->
_log->info(
"Set {} cells to be stones using selection mode "
190 "'{}'.", to_turn_to_stone.size(),
191 get_as<std::string>(
"mode",
_cfg[
"stones"]));
195 if (
_cfg[
"infection_source"]
196 and get_as<bool>(
"enabled",
_cfg[
"infection_source"]))
198 this->
_log->info(
"Setting cells to be infection sources ...");
201 apply_rule<Update::async, Shuffle::off>(
202 [](
const auto& cell){
203 auto& state = cell->state;
210 this->
_log->info(
"Set {} cells to be infection sources using "
211 "selection mode '{}'.", source_cells.size(),
212 get_as<std::string>(
"mode",
_cfg[
"infection_source"]));
219 std::vector<std::string>{
220 "empty",
"tree",
"infected",
"source",
"stone"
222 this->
_log->debug(
"Added coordinates to densities dataset.");
225 this->
_log->debug(
"{} model fully set up.", this->
_name);
244 for (
const auto& cell : this->_cm.
cells()) {
246 ++
_densities[
static_cast<char>(cell->state.kind)];
253 d /=
static_cast<double>(this->_cm.
cells().size());
289 const auto cells_pool =
291 [&](
const auto& cell){
299 cells_pool.begin(), cells_pool.end(),
300 std::back_inserter(
sample),
306 for (
const auto& cell :
sample){
318 const auto change_p_infect =
321 if (this->
_time == change_p_infect.first) {
341 auto state = cell->state;
342 state.cluster_id = 0;
370 auto nb_state = nb->state;
401 if (cell->state.cluster_id != 0 or cell->state.kind !=
Kind::tree) {
414 cluster.push_back(cell);
417 for (
unsigned int i = 0; i < cluster.size(); ++i) {
420 for (
const auto& nb : this->_cm.
neighbors_of(cluster[i])) {
422 if ( nb->state.cluster_id == 0
426 cluster.push_back(nb);
465 this->
_monitor.set_entry(
"densities", _densities);
488 [](
const auto& cell) {
489 return static_cast<char>(cell->state.kind);
495 [](
const auto& cell) {
496 return static_cast<unsigned short int>(cell->state.age);
503 [](
const auto& cell) {
504 return cell->state.cluster_id;
const CellContainer< Cell > & cells() const
Return const reference to the managed CA cells.
Definition: cell_manager.hh:219
typename std::function< CellState(const std::shared_ptr< Cell > &)> RuleFunc
The type of a rule function acting on cells of this cell manager.
Definition: cell_manager.hh:84
CellContainer< Cell > select_cells(Args &&... args) const
Select cells using the Utopia::select_entities interface.
Definition: cell_manager.hh:342
CellContainer< Cell > neighbors_of(const Cell &cell) const
Retrieve the given cell's neighbors.
Definition: cell_manager.hh:458
Utopia::Cell< CDCellTraits > Cell
Type of the managed cells.
Definition: cell_manager.hh:47
Base class interface for Models using the CRT Pattern.
Definition: model.hh:112
Monitor _monitor
The monitor.
Definition: model.hh:188
std::shared_ptr< DataSet > create_cm_dset(const std::string name, const CellManager &cm, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
Create a dataset storing data from a CellManager.
Definition: model.hh:849
typename ModelTypes::DataSet DataSet
Data type that is used for storing data.
Definition: model.hh:125
const Config _cfg
Config node belonging to this model instance.
Definition: model.hh:158
Time _time
Model-internal current time stamp.
Definition: model.hh:170
const std::string _name
Name of the model instance.
Definition: model.hh:149
std::shared_ptr< DataSet > create_dset(const std::string name, const std::shared_ptr< DataGroup > &hdfgrp, std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
Create a new dataset within the given group.
Definition: model.hh:752
typename ModelTypes::DataGroup DataGroup
Data type that is used for storing datasets.
Definition: model.hh:122
const std::shared_ptr< spdlog::logger > _log
The (model) logger.
Definition: model.hh:164
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition: model.hh:161
Contagious disease model on a grid.
Definition: ContDisease.hh:52
std::array< double, 5 > _densities
Densities for all states.
Definition: ContDisease.hh:101
void update_densities()
Update the densities array.
Definition: ContDisease.hh:238
RuleFunc _update
Define the update rule.
Definition: ContDisease.hh:339
void identify_clusters()
Identify clusters.
Definition: ContDisease.hh:263
void write_data()
Write data.
Definition: ContDisease.hh:473
typename Base::DataGroup DataGroup
Data type for a data group.
Definition: ContDisease.hh:58
void perform_step()
Iterate a single time step.
Definition: ContDisease.hh:447
std::shared_ptr< DataSet > _dset_cluster_id
The dataset for storing the cluster ID associated with each cell.
Definition: ContDisease.hh:116
RuleFunc _identify_cluster
Identify each cluster of trees.
Definition: ContDisease.hh:400
void infection_control()
Apply infection control.
Definition: ContDisease.hh:281
typename CellManager::RuleFunc RuleFunc
Rule function type.
Definition: ContDisease.hh:73
Utopia::CellContainer< Cell > CellContainer
Type of a container of shared pointers to cells.
Definition: ContDisease.hh:70
Model< ContDisease, CDTypes > Base
The base model type.
Definition: ContDisease.hh:55
CellManager _cm
The cell manager.
Definition: ContDisease.hh:81
unsigned int _cluster_id_cnt
The incremental cluster tag.
Definition: ContDisease.hh:90
typename Base::DataSet DataSet
Data type for a dataset.
Definition: ContDisease.hh:61
std::shared_ptr< DataSet > _dset_densities
2D dataset (densities array and time) of density values
Definition: ContDisease.hh:107
void monitor()
Monitor model information.
Definition: ContDisease.hh:463
const Params _params
Model parameters.
Definition: ContDisease.hh:84
std::vector< std::shared_ptr< CellManager::Cell > > _cluster_members
A temporary container for use in cluster identification.
Definition: ContDisease.hh:93
typename CellManager::Cell Cell
Type of a cell.
Definition: ContDisease.hh:67
std::uniform_real_distribution< double > _prob_distr
The range [0, 1] distribution to use for probability checks.
Definition: ContDisease.hh:87
ContDisease(const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
Construct the ContDisease model.
Definition: ContDisease.hh:130
std::shared_ptr< DataSet > _dset_kind
2D dataset (cell ID and time) of cell kinds
Definition: ContDisease.hh:110
bool _write_only_densities
Definition: ContDisease.hh:104
std::shared_ptr< DataSet > _dset_age
2D dataset (tree age and time) of cells
Definition: ContDisease.hh:113
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
@ condition
Select if a condition is fulfilled.
@ sample
Select a random sample of entities with a known sample size.
Definition: ContDisease.hh:18
@ stone
Cell cannot be infected.
@ source
Cell is an infection source: constantly infected, spreading infection.
@ infected
Cell is infected.
@ tree
Cell represents a tree.
EntityContainer< CellType > CellContainer
Type of the variably sized container for cells.
Definition: types.hh:26
The entity traits struct gathers types to be used for specializing an entity.
Definition: entity.hh:49
Wrapper struct for defining model class data types.
Definition: model.hh:92
TimesValuesQueue change_p_infect
Change p_infect to new value at given times.
Definition: params.hh:37
const std::size_t num_additional_infections
The number of infections added to the default p_infect.
Definition: params.hh:27
TimesQueue at_times
Add additional infections at these time steps.
Definition: params.hh:30
const bool enabled
Whether infection control is enabled.
Definition: params.hh:24
Parameters of the ContDisease.
Definition: params.hh:133
const double p_growth
Probability per site and time step to go from state empty to tree.
Definition: params.hh:135
const double p_immunity
Definition: params.hh:139
double p_infect
Definition: params.hh:143
const InfectionContParams infection_control
Infection control parameters.
Definition: params.hh:146