Utopia  2
Framework for studying models of complex & adaptive systems.
Vegetation.hh
Go to the documentation of this file.
1 #ifndef VEGETATION_HH
2 #define VEGETATION_HH
3 
4 #include <random>
5 #include <memory>
6 #include <algorithm>
7 #include <string>
8 
9 #include <utopia/core/model.hh>
10 #include <utopia/core/apply.hh>
11 #include <utopia/core/types.hh>
13 
17 
18 
19 namespace Utopia {
20 namespace Models {
21 namespace Vegetation {
22 
24 struct CellState {
25  double plant_mass;
26 };
27 
30 
33 
34 
36 class Vegetation:
37  public Model<Vegetation, VegetationTypes>
38 {
39 public:
42 
44  using Config = typename Base::Config;
45 
47  using DataSet = typename Base::DataSet;
48 
51 
53  using RuleFunc = typename CellManager::RuleFunc;
54 
55 private:
58 
59  // -- The parameters of the model -- //
60  // TODO Consider making these public or implementing setters & getters
61 
63  std::normal_distribution<double> _rain_dist;
64 
66  double _growth_rate;
67 
69  double _seeding_rate;
70 
71 
72  // Datasets -- //
74  std::shared_ptr<DataSet> _dset_plant_mass;
75 
76 
77  // -- Rule functions -- //
79 
87  RuleFunc _growth_seeding = [this](const auto& cell){
88  auto state = cell->state();
89  auto rain = _rain_dist(*(this->_rng));
90  auto mass = state.plant_mass;
91 
92  if (rain < 1e-16) {
93  rain = 0.;
94  }
95 
96  // Distinguish by mass whether to grow or to seed
97  // If negative or smaller than machine epsilon at 1.0 for doubles,
98  // consider invalid and reseed
99  if (not (mass < 1e-16)) {
100  // Logistic Growth
101  /* Use Beverton-Holt to approximate discretized logistic growth.
102  * Be careful that when using the Wikipedia version
103  * [https://en.wikipedia.org/wiki/Beverton–Holt_model], the R0
104  * parameter therein is >= 1 -> proliferation rate!
105  * This means that, as given therein, we have:
106  * n_{t+1} = (r*n_t)/(1 + (n_t*(r-1)/K))
107  * with r >= 1, which has to be turned into
108  * n_{t+1} =((r+1.)*n_t)/(1 +(n_t*r)/K)
109  * when r is given as a growth rate proper.
110  */
111  state.plant_mass = ((_growth_rate + 1.) * mass)/(1. + (mass * (_growth_rate))/rain);
112  }
113  else {
114  // Seeding
115  state.plant_mass = _seeding_rate * rain;
116  }
117 
118  return state;
119  };
120 
122  double calc_mean_mass () const {
123  return std::accumulate(_cm.cells().begin(),
124  _cm.cells().end(),
125  0.,
126  [](double sum, const auto& cell) {
127  return sum + cell->state().plant_mass;
128  }) / _cm.cells().size();
129  }
130 
131 public:
132 
134 
142  template<class ParentModel>
144  const std::string& name,
145  const ParentModel& parent_model,
146  const DataIO::Config& custom_cfg = {}
147  )
148  :
149  // Construct the base class
150  Base(name, parent_model, custom_cfg),
151 
152  // Initialize the manager, setting the initial state
153  _cm(*this, CellState({0.0})),
154 
155  // Initialize the rain distribution
156  _rain_dist{get_as<double>("rain_mean", this->_cfg),
157  get_as<double>("rain_std", this->_cfg)},
158 
159  // Initialize model parameters from config file
160  _growth_rate(get_as<double>("growth_rate", this->_cfg)),
161  _seeding_rate(get_as<double>("seeding_rate", this->_cfg)),
162 
163  // Open dataset for output of cell states
164  _dset_plant_mass(this->create_cm_dset("plant_mass", _cm))
165  {
166  this->_log->info("'{}' model fully set up.", name);
167  }
168 
170  void perform_step ()
171  {
173  }
174 
176  void write_data ()
177  {
178  _dset_plant_mass->write(_cm.cells().begin(),
179  _cm.cells().end(),
180  [](auto& cell) {
181  return cell->state().plant_mass; }
182  );
183  }
184 
186  void monitor () {
187  this->_monitor.set_entry("mean_mass", calc_mean_mass());
188  }
189 
190 };
191 
192 
193 } // namespace Vegetation
194 } // namespace Models
195 } // namespace Utopia
196 
197 #endif // VEGETATION_HH
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
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
typename ModelTypes::Config Config
Data type that holds the configuration.
Definition: model.hh:116
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
A very simple vegetation model.
Definition: Vegetation.hh:38
typename CellManager::RuleFunc RuleFunc
Type of the update rule.
Definition: Vegetation.hh:53
double _seeding_rate
The seeding rate.
Definition: Vegetation.hh:69
std::normal_distribution< double > _rain_dist
Normal distribution for drawing random rain values.
Definition: Vegetation.hh:63
RuleFunc _growth_seeding
Apply logistic growth and seeding.
Definition: Vegetation.hh:87
std::shared_ptr< DataSet > _dset_plant_mass
Plant mass dataset.
Definition: Vegetation.hh:74
Vegetation(const std::string &name, const ParentModel &parent_model, const DataIO::Config &custom_cfg={})
Construct the Vegetation model.
Definition: Vegetation.hh:143
double calc_mean_mass() const
Calculate the mean plant mass.
Definition: Vegetation.hh:122
typename Base::Config Config
Type of the config tree.
Definition: Vegetation.hh:44
void monitor()
Monitor the current model state; supplies the mean plant mass.
Definition: Vegetation.hh:186
void write_data()
Write the cell states (aka plant bio-mass)
Definition: Vegetation.hh:176
Model< Vegetation, VegetationTypes > Base
Type of the base class.
Definition: Vegetation.hh:41
typename Base::DataSet DataSet
Type of a data set.
Definition: Vegetation.hh:47
double _growth_rate
The growth rate (logistic growth model)
Definition: Vegetation.hh:66
void perform_step()
Iterate a single step.
Definition: Vegetation.hh:170
CellManager _cm
The grid manager.
Definition: Vegetation.hh:57
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
void apply_rule(Rule &&rule, const ContTarget &cont_target, ContArgs &&... cont_args)
Sequential overload.
Definition: apply.hh:133
This is the central file of the HDF5 dataIO module of Utopia and provides a class for writing to,...
This file provides a class for creating and managing groups in a HDF5 file, which then can create oth...
Definition: agent.hh:11
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
State of a cell in the Vegetation model, consisting only of plant mass.
Definition: Vegetation.hh:24
double plant_mass
Definition: Vegetation.hh:25