1 #ifndef UTOPIA_MODELS_FORESTFIRE_HH
2 #define UTOPIA_MODELS_FORESTFIRE_HH
12 #include "../ContDisease/state.hh"
50 const auto p_tree = get_as<double>(
"p_tree", cfg);
53 if (p_tree < 0. or p_tree > 1.) {
54 throw std::invalid_argument(
"p_tree needs to be in interval "
55 "[0., 1.], but was not!");
57 else if (p_tree == 0.) {
60 else if (p_tree == 1.) {
66 if (std::uniform_real_distribution<double>(0., 1.)(*
rng) < p_tree) {
107 throw std::invalid_argument(
"Invalid p_growth! Need be a value "
108 "in range [0, 1] and specify the probability per time step "
109 "and cell with which an empty cell turns into a tree.");
112 throw std::invalid_argument(
"Invalid p_lightning! Need be "
113 "in range [0, 1] and specify the probability per cell and "
114 "time step for lightning to strike.");
117 throw std::invalid_argument(
"Invalid p_immunity! "
118 "Need be a value in range [0, 1] and specify the probability "
119 "per neighbor with which that neighbor is immune to fire.");
137 public Model<ForestFire, ModelTypes>
204 template<
class ParentModel>
206 const std::string& name,
207 ParentModel& parent_model,
212 Base(name, parent_model, custom_cfg),
237 if (
_cfg[
"stones"] and get_as<bool>(
"enabled",
_cfg[
"stones"])) {
238 this->
_log->info(
"Setting cells to be stones ...");
244 apply_rule<Update::async, Shuffle::off>(
245 [](
const auto& cell){
246 auto& state = cell->state;
247 state.kind = Kind::stone;
253 this->
_log->info(
"Set {} cells to be stones using selection mode "
254 "'{}'.", to_turn_to_stone.size(),
255 get_as<std::string>(
"mode",
_cfg[
"stones"]));
259 if (
_cfg[
"ignite_permanently"]
260 and get_as<bool>(
"enabled",
_cfg[
"ignite_permanently"]))
262 this->
_log->info(
"Setting cells to be permanently ignited ...");
265 apply_rule<Update::async, Shuffle::off>(
266 [](
const auto& cell){
267 auto& state = cell->state;
268 state.kind = Kind::source;
274 this->
_log->info(
"Set {} cells to be permanently ignited using "
275 "selection mode '{}'.", to_be_ignited.size(),
276 get_as<std::string>(
"mode",
_cfg[
"ignite_permanently"]));
279 this->
_log->debug(
"{} model fully set up.", this->
_name);
296 [&](std::size_t sum,
const std::shared_ptr<Cell>& cell) {
297 return sum + (cell->state.kind == Kind::tree);
300 /
static_cast<double>(
_cm.
cells().size());
310 this->
_log->debug(
"Identifying clusters...");
315 apply_rule<Update::async, Shuffle::off>(
319 this->
_log->debug(
"Identified {} clusters.", _cluster_id_cnt);
343 auto& state = cell->state;
344 state.cluster_id = 0;
347 if (state.kind == Kind::empty) {
349 state.kind = Kind::tree;
354 else if (state.kind == Kind::tree) {
366 else if (state.kind == Kind::source) {
371 else if (state.kind == Kind::stone) {
377 throw std::invalid_argument(
"Invalid cell state!");
389 if (cell->state.kind == Kind::tree) {
390 cell->state.kind = Kind::empty;
397 cluster.push_back(cell);
400 for (
unsigned int i = 0; i < cluster.size(); ++i) {
401 const auto& cluster_member = cluster[i];
404 for (
const auto& c : this->_cm.
neighbors_of(cluster_member)) {
406 if (c->state.kind == Kind::tree) {
417 c->state.kind = Kind::empty;
419 cluster.push_back(c);
438 if ( cell->state.cluster_id != 0
439 or cell->state.kind != Kind::tree)
452 cluster.push_back(cell);
455 for (
unsigned int i = 0; i < cluster.size(); ++i) {
458 for (
const auto& c : this->_cm.
neighbors_of(cluster[i])) {
460 if ( c->state.cluster_id == 0
461 and c->state.kind == Kind::tree)
464 cluster.push_back(c);
481 apply_rule<Update::async, Shuffle::on>(
503 [](
const auto& cell) {
504 return static_cast<char>(cell->state.kind);
509 [](
const auto& cell) {
510 return cell->state.age;
516 [](
const auto& cell) {
517 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< CellTraits > 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
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
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
The ForestFire model.
Definition: ForestFire.hh:138
void perform_step()
Perform step.
Definition: ForestFire.hh:479
typename Base::DataSet DataSet
Data type for a dataset.
Definition: ForestFire.hh:144
CellManager _cm
The cell manager for the forest fire model.
Definition: ForestFire.hh:162
unsigned int identify_clusters()
Identifies clusters in the cells and labels them with corresponding IDs.
Definition: ForestFire.hh:309
void monitor()
Provide monitoring information to the frontend: tree_density
Definition: ForestFire.hh:487
Model< ForestFire, ModelTypes > Base
The base model type.
Definition: ForestFire.hh:141
RuleFunc _identify_cluster
Get the identity of each cluster of trees.
Definition: ForestFire.hh:436
double calculate_tree_density() const
Calculate and return the density of tree cells.
Definition: ForestFire.hh:288
RuleFunc _update
Update rule, called every step.
Definition: ForestFire.hh:341
std::uniform_real_distribution< double > _prob_distr
A [0,1]-range uniform distribution used for evaluating probabilities.
Definition: ForestFire.hh:168
const std::shared_ptr< DataSet > _dset_age
2D dataset (tree age and time) of cells
Definition: ForestFire.hh:184
unsigned int _cluster_id_cnt
The incremental cluster tag caching variable.
Definition: ForestFire.hh:171
std::vector< std::shared_ptr< CellManager::Cell > > _cluster_members
A temporary container for use in cluster identification.
Definition: ForestFire.hh:174
RuleFunc _burn_cluster
Rule to burn a cluster of trees around the given cell.
Definition: ForestFire.hh:387
typename CellManager::Cell Cell
The type of a cell.
Definition: ForestFire.hh:150
const std::shared_ptr< DataSet > _dset_tree_density
The dataset that stores the mean density.
Definition: ForestFire.hh:190
const std::shared_ptr< DataSet > _dset_kind
The dataset that stores the kind for each cell, e.g. Kind::tree.
Definition: ForestFire.hh:181
void write_data()
Write data.
Definition: ForestFire.hh:492
const bool _write_only_tree_density
Whether to only write the tree density.
Definition: ForestFire.hh:178
ForestFire(const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
Construct the ForestFire model.
Definition: ForestFire.hh:205
typename CellManager::RuleFunc RuleFunc
Rule function type, extracted from CellManager.
Definition: ForestFire.hh:153
const Param _param
Model parameters.
Definition: ForestFire.hh:165
const std::shared_ptr< DataSet > _dset_cluster_id
The dataset that stores the cluster id.
Definition: ForestFire.hh:187
@ empty
Every entity is utterly alone in the world.
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
ReturnType get_as(const std::string &key, const DataIO::Config &node)
This function is a wrapper around the yaml-cpp YAML::Node::as function.
Definition: cfg_utils.hh:158
Kind
The kind of the cell: empty, tree, infected, source, stone.
Definition: state.hh:9
std::mt19937 rng
– Type definitions ----------------------------------------------------—
Definition: test_revision.cc:17
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
ForestFire model parameter struct.
Definition: ForestFire.hh:89
const double p_lightning
Frequency of lightning occurring per cell.
Definition: ForestFire.hh:94
Param(const DataIO::Config &cfg)
Construct the parameters from the given configuration node.
Definition: ForestFire.hh:100
const double p_growth
Rate of growth per cell.
Definition: ForestFire.hh:91
const double p_immunity
The probability (per neighbor) to be immune to a spreading fire.
Definition: ForestFire.hh:97
The full cell struct for the ForestFire model.
Definition: ForestFire.hh:28
State(const DataIO::Config &cfg, const std::shared_ptr< RNG > &rng)
Construct a cell from a configuration node and an RNG.
Definition: ForestFire.hh:43
unsigned int cluster_id
An ID denoting to which cluster this cell belongs (if it is a tree)
Definition: ForestFire.hh:36
unsigned short age
The age of the tree on this cell.
Definition: ForestFire.hh:33
Kind kind
The kind of object that populates this cell, e.g. a tree.
Definition: ForestFire.hh:30
State()=delete
Remove default constructor, for safety.