1#ifndef UTOPIA_MODELS_FORESTFIRE_HH
2#define UTOPIA_MODELS_FORESTFIRE_HH
12#include "../ContDisease/state.hh"
54 throw std::invalid_argument(
"p_tree needs to be in interval "
55 "[0., 1.], but was not!");
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,
238 this->_log->info(
"Setting cells to be stones ...");
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 "
259 if ( _cfg[
"ignite_permanently"]
262 this->_log->info(
"Setting cells to be permanently ignited ...");
263 auto to_be_ignited = _cm.select_cells(_cfg[
"ignite_permanently"]);
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 "
279 this->_log->debug(
"{} model fully set up.", this->_name);
294 std::accumulate(_cm.cells().begin(), _cm.cells().end(),
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...");
316 _identify_cluster, _cm.cells()
319 this->_log->debug(
"Identified {} clusters.", _cluster_id_cnt);
321 return _cluster_id_cnt;
343 auto& state =
cell->state;
344 state.cluster_id = 0;
347 if (state.kind == Kind::empty) {
348 if (this->_prob_distr(*this->_rng) < _param.p_growth) {
349 state.kind = Kind::tree;
354 else if (state.kind == Kind::tree) {
356 if (this->_prob_distr(*this->_rng) < _param.p_lightning) {
357 state = _burn_cluster(
cell);
366 else if (state.kind == Kind::source) {
367 state = _burn_cluster(
cell);
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;
395 auto&
cluster = _cluster_members;
400 for (
unsigned int i = 0;
i <
cluster.size(); ++
i) {
406 if (
c->state.kind == Kind::tree) {
408 if (this->_param.p_immunity > 0.) {
410 if ( this->_prob_distr(*this->_rng)
411 < _param.p_immunity) {
417 c->state.kind = Kind::empty;
438 if (
cell->state.cluster_id != 0
439 or cell->state.kind != Kind::tree)
447 cell->state.cluster_id = _cluster_id_cnt;
450 auto&
cluster = _cluster_members;
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)
463 c->state.cluster_id = _cluster_id_cnt;
482 _update, _cm.cells(), *
this->_rng
488 this->_monitor.set_entry(
"tree_density", calculate_tree_density());
494 _dset_tree_density->write(calculate_tree_density());
496 if (_write_only_tree_density) {
502 _dset_kind->write(_cm.cells().begin(), _cm.cells().end(),
503 [](
const auto&
cell) {
504 return static_cast<char>(cell->state.kind);
508 _dset_age->write(_cm.cells().begin(), _cm.cells().end(),
509 [](
const auto&
cell) {
510 return cell->state.age;
515 _dset_cluster_id->write(_cm.cells().begin(), _cm.cells().end(),
516 [](
const auto&
cell) {
517 return cell->state.cluster_id;
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
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
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
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
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
double calculate_tree_density() const
Calculate and return the density of tree cells.
Definition ForestFire.hh:288
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
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
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
Kind
The kind of the cell: empty, tree, infected, source, stone.
Definition state.hh:9
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.