Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone > Class Template Reference

The Environment model provides a non-uniform, dynamic parameter background. More...

#include <Environment.hh>

Inheritance diagram for Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >:
Inheritance graph
[legend]
Collaboration diagram for Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >:
Collaboration graph
[legend]

Classes

struct  DummyCellManager
 A dummy CellManager type that is used in standalone mode. More...
 

Public Types

using Self = Environment< EnvParam, EnvCellState, standalone >
 Type of this class.
 
using Base = Model< Self, ModelTypes >
 The type of the Model base class of this derived class.
 
using DataGroup = typename Base::DataGroup
 Data type of the group to write model data to, holding datasets.
 
using DataSet = typename Base::DataSet
 Data type for a dataset.
 
using CellTraits = Utopia::CellTraits< EnvCellState, Update::manual >
 Cell traits specialization using the state type.
 
using CellManager = Utopia::CellManager< CellTraits, Self >
 The type of the cell manager.
 
using EnvCellContainer = CellContainer< typename CellManager::Cell >
 The type of the EnvCellContainer.
 
using Time = typename Base::Time
 The type of the model time.
 
using EnvParamFunc = typename std::function< double()>
 The type of the environment parameter functions.
 
using EnvParamFuncBundle = FuncBundle::ParamFuncBundle< EnvParamFunc, Time >
 The type wrapping EnvParamFunc with metadata.
 
using EnvStateFunc = typename CellManager::RuleFunc
 The type of the environment state functions; basically a rule function.
 
using EnvStateFuncBundle = FuncBundle::RuleFuncBundle< EnvStateFunc, Time, EnvCellContainer >
 The type wrapping EnvStateFunc with metadata.
 
using Config = DataIO::Config
 Configuration node type alias.
 
- Public Types inherited from Utopia::Model< Derived, ModelTypes >
using Config = typename ModelTypes::Config
 Data type that holds the configuration.
 
using DataManager = DataIO::Default::DefaultDataManager< Derived >
 The data manager to use, specialized with the derived model.
 
using DataGroup = typename ModelTypes::DataGroup
 Data type that is used for storing datasets.
 
using DataSet = typename ModelTypes::DataSet
 Data type that is used for storing data.
 
using RNG = typename ModelTypes::RNG
 Data type of the shared RNG.
 
using Space = typename ModelTypes::Space
 Data type of the space this model resides in.
 
using Time = typename ModelTypes::Time
 Data type for the model time.
 
using Monitor = typename ModelTypes::Monitor
 Data type for the monitor.
 
using MonitorManager = typename ModelTypes::MonitorManager
 Data type for the monitor manager.
 
using Level = typename ModelTypes::Level
 Data type for the hierarchical level.
 

Public Member Functions

template<class ParentModel , class CellManager >
 Environment (const std::string &name, ParentModel &parent_model, CellManager &&associate_cm, const DataIO::Config &custom_cfg={})
 Construct the Environment model associated to a CellManager.
 
template<typename ParentModel >
 Environment (const std::string name, ParentModel &parent)
 Construct Environment without associated CellManager.
 
void perform_step ()
 Iterate a single step.
 
void monitor ()
 Monitor model information.
 
void write_data ()
 Write data.
 
double get_parameter (const std::string &param_name) const
 Return the current value of the parameter with param_name.
 
void set_parameter (const std::string &param_name, double value)
 Change the current value of the parameter with param_name.
 
const autocm () const
 Return a const reference to the cell manager.
 
template<class EPFB >
void add_env_param_func (EPFB &&epfb)
 Add a rule function at the end of the sequence of parameter functions.
 
void add_env_param_func (const std::string &name, const EnvParamFunc &epf, const std::string &param_name, std::tuple< bool, bool, std::set< Time > > times_tuple)
 Add a param function at the end of the sequence of env functions.
 
void add_env_param_func_from_cfg (const std::string &name, const EnvParamFunc &epf, const std::string &param_name, const Config &cfg={})
 Add a param function at the end of the sequence of env functions.
 
template<bool add_to_initial = false, class ESFB >
void add_env_state_func (ESFB &&esfb)
 Add a rule function at the end of the sequence of state functions.
 
template<bool add_to_initial = false>
void add_env_state_func (const std::string &name, const EnvStateFunc &esf, const Update &update=Update::sync, std::pair< bool, std::set< Time > > times_pair={true, {}}, Config select_cfg={})
 Add a rule function at the end of the sequence of environment functions.
 
template<bool add_to_initial = false>
void add_env_state_func_from_cfg (const std::string &name, const EnvStateFunc &esf, const Update &update=Update::sync, const Config &cfg={})
 Add a rule function at the end of the sequence of environment functions.
 
void track_parameter (const std::string &key)
 Mark a parameter as being tracked, i.e. store its data in write_data.
 
void track_parameters (const std::vector< std::string > &keys)
 Track multiple parameters.
 
void track_state (const std::string &key)
 Mark a state as being tracked, i.e. store its data in write_data.
 
void track_states (const std::vector< std::string > &keys)
 Track multiple states.
 
- Public Member Functions inherited from Utopia::Model< Derived, ModelTypes >
template<class ParentModel , class... WriterArgs>
 Model (const std::string &name, const ParentModel &parent_model, const Config &custom_cfg={}, std::tuple< WriterArgs... > w_args={}, const DataIO::Default::DefaultDecidermap< Derived > &w_deciders=DataIO::Default::default_deciders< Derived >, const DataIO::Default::DefaultTriggermap< Derived > &w_triggers=DataIO::Default::default_triggers< Derived >)
 Constructs a Model instance.
 
const std::shared_ptr< Space > & get_space () const
 Return the space this model resides in.
 
Time get_time () const
 Return the current time of this model.
 
Time get_time_max () const
 Return the maximum time possible for this model.
 
Config get_cfg () const
 Return the config node of this model.
 
std::string get_name () const
 Return the name of this model instance.
 
std::string get_full_name () const
 Return the full name of this model within the model hierarchy.
 
std::shared_ptr< DataGroupget_hdfgrp () const
 Return a pointer to the HDF group this model stores data in.
 
Time get_write_start () const
 Return the parameter that controls when write_data is called first.
 
Time get_write_every () const
 Return the parameter that controls how often write_data is called.
 
DataManager get_datamanager () const
 return the datamanager
 
hsize_t get_remaining_num_writes () const
 Return the number of remaining write_data calls this model will make.
 
std::shared_ptr< RNGget_rng () const
 Return a pointer to the shared RNG.
 
std::shared_ptr< spdlog::logger > get_logger () const
 Return a pointer to the logger of this model.
 
Monitor get_monitor () const
 Return the monitor of this model.
 
std::shared_ptr< MonitorManagerget_monitor_manager () const
 Get the monitor manager of the root model.
 
Level get_level () const
 Return the hierarchical level within the model hierarchy.
 
virtual void prolog ()
 A function that is called before starting model iteration.
 
virtual void epilog ()
 A function that is called after the last iteration of a model.
 
void iterate ()
 Iterate one (time) step of this model.
 
void run ()
 Run the model from the current time to the maximum time.
 
std::shared_ptr< DataSetcreate_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.
 
std::shared_ptr< DataSetcreate_dset (const std::string name, const 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 model's base data group.
 
template<class CellManager >
std::shared_ptr< DataSetcreate_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.
 
template<class AgentManager >
std::shared_ptr< DataSetcreate_am_dset (const std::string name, const AgentManager &am, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a dataset storing data from a AgentManager.
 

Private Member Functions

void setup_env_param_funcs (const Config &cfg)
 Construct the rule funcs sequencefor EnvParam from cfg.
 
template<bool add_to_initial = false>
void setup_env_state_funcs (const Config &cfg)
 Construct the rule funcs sequence from cfg.
 
template<class EPFB >
void apply_env_param_func (EPFB &&epfb, bool initialization=false)
 Apply a given environment parameter function.
 
template<class ESFB >
void apply_env_state_func (ESFB &&esfb)
 Apply a given environment state function.
 

Private Attributes

CellManager _cm
 The cell manager.
 
EnvParam _params
 The environment parameters.
 
std::vector< EnvParamFuncBundle_env_param_funcs
 Container of functions that are invoked every time step.
 
std::vector< EnvStateFuncBundle_init_env_state_funcs
 Container of rule functions that are invoked once at initialisation.
 
std::vector< EnvStateFuncBundle_env_state_funcs
 Container of rule functions that are invoked every time step.
 
std::unordered_map< std::string, std::shared_ptr< DataSet > > _dsets_state
 Dynamically generated map of datasets of cell states.
 
std::unordered_map< std::string, std::shared_ptr< DataSet > > _dsets_param
 Dynamically generated map of datasets of parameters.
 

Additional Inherited Members

- Protected Member Functions inherited from Utopia::Model< Derived, ModelTypes >
void __perform_step ()
 Perform the computation of a step.
 
void __monitor ()
 Monitor information in the terminal.
 
void __write_data ()
 Write data; calls the implementation's write_data method.
 
void __write_initial_state ()
 Write the initial state.
 
void increment_time (const Time dt=1)
 Increment time.
 
void __prolog ()
 The default prolog of a model.
 
void __epilog ()
 The default epilog of a model.
 
Derivedimpl ()
 cast to the derived class
 
const Derivedimpl () const
 const cast to the derived interface
 
- Protected Attributes inherited from Utopia::Model< Derived, ModelTypes >
const std::string _name
 Name of the model instance.
 
const std::string _full_name
 The full name within the model hierarchy.
 
const Level _level
 The level within the model hierarchy.
 
const Config _cfg
 Config node belonging to this model instance.
 
const std::shared_ptr< RNG_rng
 The RNG shared between models.
 
const std::shared_ptr< spdlog::logger > _log
 The (model) logger.
 
std::shared_ptr< Space_space
 The space this model resides in.
 
Time _time
 Model-internal current time stamp.
 
const Time _time_max
 Model-internal maximum time stamp.
 
const std::shared_ptr< DataGroup_hdfgrp
 The HDF group this model instance should write its data to.
 
const Time _write_start
 First time at which write_data is called.
 
const Time _write_every
 How often to call write_data from iterate.
 
Monitor _monitor
 The monitor.
 
DataManager _datamanager
 Manager object for handling data output; see DataManager.
 
- Static Protected Attributes inherited from Utopia::Model< Derived, ModelTypes >
static constexpr WriteMode _write_mode = ModelTypes::write_mode
 Which data-writing mode the base model should use.
 

Detailed Description

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
class Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >

The Environment model provides a non-uniform, dynamic parameter background.

This happens by coupling to another model's CellManager. Additionally, global parameters can also be changed.

Template Parameters
EnvParamThe parameter type of the environment
EnvCellStateThe cell state type of the environment cells
standaloneWhether to have the model as a standalone model

Member Typedef Documentation

◆ Base

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::Base = Model<Self, ModelTypes>

The type of the Model base class of this derived class.

◆ CellManager

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::CellManager = Utopia::CellManager<CellTraits, Self>

The type of the cell manager.

◆ CellTraits

Cell traits specialization using the state type.

◆ Config

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::Config = DataIO::Config

Configuration node type alias.

◆ DataGroup

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::DataGroup = typename Base::DataGroup

Data type of the group to write model data to, holding datasets.

◆ DataSet

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::DataSet = typename Base::DataSet

Data type for a dataset.

◆ EnvCellContainer

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::EnvCellContainer = CellContainer<typename CellManager::Cell>

The type of the EnvCellContainer.

◆ EnvParamFunc

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::EnvParamFunc = typename std::function<double()>

The type of the environment parameter functions.

◆ EnvParamFuncBundle

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::EnvParamFuncBundle = FuncBundle::ParamFuncBundle<EnvParamFunc, Time>

The type wrapping EnvParamFunc with metadata.

◆ EnvStateFunc

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::EnvStateFunc = typename CellManager::RuleFunc

The type of the environment state functions; basically a rule function.

◆ EnvStateFuncBundle

The type wrapping EnvStateFunc with metadata.

◆ Self

Type of this class.

◆ Time

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
using Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::Time = typename Base::Time

The type of the model time.

Constructor & Destructor Documentation

◆ Environment() [1/2]

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::Environment ( const std::string &  name,
ParentModel parent_model,
CellManager &&  associate_cm,
const DataIO::Config custom_cfg = {} 
)
inline

Construct the Environment model associated to a CellManager.

Parameters
nameName of this model instance
parent_modelThe parent model this model instance resides in
associate_cmThe CellManager of the associate (i.e. parent) model. The Environment model's CellManager will use the configuration of that CellManager.
custom_cfgA custom configuration to use instead of the one extracted from the parent model using the instance name
237 {}
238 )
239 :
240 // Initialize first via base model
242
243 // Set up the internal CellManager
244 _cm(*this, associate_cm.cfg()),
245 // NOTE If called from the standalone constructor, the .cfg() call
246 // always returns an empty configuration which leads to this
247 // internal CellManager being set up from _cfg["cell_manager"]
248
249 //set up the environment params
250 _params(this->_cfg),
251
252 // Set up environment function bundle containers empty
256
257 // Map of datasets; empty until states are set to be tracked
258 _dsets_state{},
260 {
261 // Associate the CellManager's cells with each other
262 if constexpr (not std::is_same<CellManager, DummyCellManager>()) {
263 for (std::size_t i=0; i < _cm.cells().size(); i++) {
264 associate_cm.cells()[i]->custom_links().env = _cm.cells()[i];
265 }
266
267 this->_log->info(
268 "Associated '{}' cells with those of the parent model '{}'.",
269 this->_name, parent_model.get_name()
270 );
271 }
272 else if constexpr (not standalone) {
273 // Only allow coupled models without coupled cellmanagers when
274 // a EnvCellState is the dummy type
275 static_assert(std::is_same<EnvCellState, DummyEnvCellState>(),
276 "The cm for association of environment cells cannot "
277 "be the DummyCellManager! \n"
278 "Setup the model with type DummyEnvCellState for "
279 "standalone model or pass a cell manager for "
280 "associated model.");
281
282 this->_log->info("Setting up '{}' as coupled model without "
283 "associated cell manager.", this->_name);
284 }
285 else {
286 this->_log->info("Setting up '{}' as standalone model ...",
287 this->_name);
288 }
289
290 // Check inheritance of EnvCellState; needed for position cache
291 static_assert(std::is_base_of<BaseEnvCellState, EnvCellState>::value,
292 "The model's EnvCellState must derive from "
293 "Utopia::Models::Environment::BaseEnvCellState!");
294
295 // Check inheritance of EnvParam; needed for getters and setters
296 static_assert(std::is_base_of<BaseEnvParam, EnvParam>::value,
297 "The model's EnvParam must derive from "
298 "Utopia::Models::Environment::BaseEnvParam!");
299
300 // Store positions
302 [this](const auto& cell){
303 auto& state = cell->state;
304 state.position = this->_cm.barycenter_of(cell);
305 return state;
306 },
307 this->_cm.cells()
308 );
309 this->_log->debug("Cell barycenters cached.");
310
311
312 // Now set up the actual environment parameter functions
313 if (this->_cfg["env_param_funcs"]) {
314 setup_env_param_funcs(this->_cfg["env_param_funcs"]);
315 }
316
317 // Apply the env_param_funcs for initialization
318 this->_log->debug("Applying {} initial environment param function{} ...",
319 _env_param_funcs.size(),
320 _env_param_funcs.size() != 1 ? "s" : "");
321 for (auto& epfb : _env_param_funcs) {
322 if (epfb.invoke_at_initialization) {
324 }
325 }
326
327 // Now set up the actual environment state functions
328 if (this->_cfg["init_env_state_funcs"]) {
329 setup_env_state_funcs<true>(this->_cfg["init_env_state_funcs"]);
330 }
331 if (this->_cfg["env_state_funcs"]) {
332 setup_env_state_funcs<false>(this->_cfg["env_state_funcs"]);
333 }
334
335 // Apply the env_state_funcs for initialization
336 this->_log->info("Applying {} initial environment state function{} ...",
338 _init_env_state_funcs.size() != 1 ? "s" : "");
339 for (auto& esfb : _init_env_state_funcs) {
341 }
342
343 this->_log->info("{} set up.", this->_name);
344 }
SpaceVec barycenter_of(const Cell &cell) const
Returns the barycenter of the given cell.
Definition cell_manager.hh:249
const CellContainer< Cell > & cells() const
Return const reference to the managed CA cells.
Definition cell_manager.hh:219
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
const std::shared_ptr< spdlog::logger > _log
The (model) logger.
Definition model.hh:164
std::vector< EnvParamFuncBundle > _env_param_funcs
Container of functions that are invoked every time step.
Definition Environment.hh:202
CellManager _cm
The cell manager.
Definition Environment.hh:195
std::vector< EnvStateFuncBundle > _init_env_state_funcs
Container of rule functions that are invoked once at initialisation.
Definition Environment.hh:206
std::unordered_map< std::string, std::shared_ptr< DataSet > > _dsets_state
Dynamically generated map of datasets of cell states.
Definition Environment.hh:214
void setup_env_param_funcs(const Config &cfg)
Construct the rule funcs sequencefor EnvParam from cfg.
Definition Environment.hh:590
Model< Self, ModelTypes > Base
The type of the Model base class of this derived class.
Definition Environment.hh:146
EnvParam _params
The environment parameters.
Definition Environment.hh:198
void apply_env_state_func(ESFB &&esfb)
Apply a given environment state function.
Definition Environment.hh:796
std::vector< EnvStateFuncBundle > _env_state_funcs
Container of rule functions that are invoked every time step.
Definition Environment.hh:210
void apply_env_param_func(EPFB &&epfb, bool initialization=false)
Apply a given environment parameter function.
Definition Environment.hh:759
std::unordered_map< std::string, std::shared_ptr< DataSet > > _dsets_param
Dynamically generated map of datasets of parameters.
Definition Environment.hh:217
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213

◆ Environment() [2/2]

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<typename ParentModel >
Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::Environment ( const std::string  name,
ParentModel parent 
)
inline

Construct Environment without associated CellManager.

Note
This constructor can be used to set up a Environment as a standalone model or with only the EnvParam. In the case of standalone the standalone template parameter needs to be set to true. In the the other case the EnvCellState needs to be the default dummy type DummyEnvCellState and the standalone template parameter needs to be false (default).
357 :
358 // Use existing constructor, passing a _DummyCellManager instance that
359 // ensures that no configuration is carried over.
360 Environment(name, parent, DummyCellManager())
361 { }
Environment(const std::string &name, ParentModel &parent_model, CellManager &&associate_cm, const DataIO::Config &custom_cfg={})
Construct the Environment model associated to a CellManager.
Definition Environment.hh:233

Member Function Documentation

◆ add_env_param_func() [1/2]

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::add_env_param_func ( const std::string &  name,
const EnvParamFunc epf,
const std::string &  param_name,
std::tuple< bool, bool, std::set< Time > >  times_tuple 
)
inline

Add a param function at the end of the sequence of env functions.

Parameters
epfEnvParamFunc that is applied to all cm.cells()
times_tupleinvoke_at_initialization, invoke_always, set of times when to invoke the function
445 {
447 EnvParamFuncBundle(name, epf, param_name, times_tuple)
448 );
449 }
void add_env_param_func(EPFB &&epfb)
Add a rule function at the end of the sequence of parameter functions.
Definition Environment.hh:432
FuncBundle::ParamFuncBundle< EnvParamFunc, Time > EnvParamFuncBundle
The type wrapping EnvParamFunc with metadata.
Definition Environment.hh:170

◆ add_env_param_func() [2/2]

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<class EPFB >
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::add_env_param_func ( EPFB &&  epfb)
inline

Add a rule function at the end of the sequence of parameter functions.

Parameters
epfbThe EnvParamFuncBundle of environment parameter function that is to be added and its metadata.
432 {
433 _env_param_funcs.push_back(epfb);
434 this->_log->debug("Added environment param function '{}'.", epfb.name);
435 }

◆ add_env_param_func_from_cfg()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::add_env_param_func_from_cfg ( const std::string &  name,
const EnvParamFunc epf,
const std::string &  param_name,
const Config cfg = {} 
)
inline

Add a param function at the end of the sequence of env functions.

Parameters
epfEnvParamFunc that is applied to all cm.cells()
cfgFrom which invoke_at_initialization and times to invoke the function is extracted
458 {})
459 {
461 add_env_param_func(name, epf, param_name, invoke_times_tuple);
462 }

◆ add_env_state_func() [1/2]

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<bool add_to_initial = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::add_env_state_func ( const std::string &  name,
const EnvStateFunc esf,
const Update update = Update::sync,
std::pair< bool, std::set< Time > >  times_pair = {true, {}},
Config  select_cfg = {} 
)
inline

Add a rule function at the end of the sequence of environment functions.

Parameters
esfEnvStateFunc that is applied to all cm.cells()
updateThe Update mode to use with apply_rule(esf, cm.cells)
times_pairinvoke_always, Set of times at which to invoke function.
select_cfgConfig passed to _cm.select_cells
490 {true, {}},
491 Config select_cfg = {})
492 {
493 // resolve select option
494 bool fix_selection;
495 EnvCellContainer cell_selection = {};
496 if (not select_cfg.IsNull()) {
497 std::string generate = get_as<std::string>("generate", select_cfg);
498 if (generate == "once") {
499 fix_selection = true;
500 }
501 else if (generate == "always") {
502 fix_selection = false;
503 }
504 else {
505 throw std::invalid_argument("Key 'generate' in 'select' "
506 "feature of environment state function '"
507 + name + "' must be 'once' to fix selection or "
508 "'always' to generate it on the fly, but was '"
509 + generate + "'!");
510 }
511
512 if (fix_selection) {
513 this->_log->debug("Generating a selection of cells and fixing "
514 "it for the rule '{}'.", name);
515 cell_selection = _cm.select_cells(select_cfg);
516 }
517 }
518 else {
519 fix_selection = false;
520 }
521
524 {fix_selection, cell_selection, select_cfg})
525 );
526 }
CellContainer< Cell > select_cells(Args &&... args) const
Select cells using the Utopia::select_entities interface.
Definition cell_manager.hh:342
FuncBundle::RuleFuncBundle< EnvStateFunc, Time, EnvCellContainer > EnvStateFuncBundle
The type wrapping EnvStateFunc with metadata.
Definition Environment.hh:177
DataIO::Config Config
Configuration node type alias.
Definition Environment.hh:180
CellContainer< typename CellManager::Cell > EnvCellContainer
The type of the EnvCellContainer.
Definition Environment.hh:161

◆ add_env_state_func() [2/2]

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<bool add_to_initial = false, class ESFB >
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::add_env_state_func ( ESFB &&  esfb)
inline

Add a rule function at the end of the sequence of state functions.

Parameters
esfbThe EnvStateFuncBundle of environment state function that is to be added and its metadata.
469 {
470 if constexpr (add_to_initial) {
471 _init_env_state_funcs.push_back(esfb);
472 }
473 else {
474 _env_state_funcs.push_back(esfb);
475 }
476 this->_log->debug("Added {}environment function '{}'.",
477 add_to_initial ? "initial " : "", esfb.name);
478 }

◆ add_env_state_func_from_cfg()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<bool add_to_initial = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::add_env_state_func_from_cfg ( const std::string &  name,
const EnvStateFunc esf,
const Update update = Update::sync,
const Config cfg = {} 
)
inline

Add a rule function at the end of the sequence of environment functions.

Parameters
esfEnvStateFunc that is applied to all cm.cells()
updateThe Update mode to use with apply_rule(esf, cm.cells)
cfgThe config of the state_func. Here, times and select are extracted; All other entries ignored. Defaults are invoke always and select all cells.
540 {})
541 {
543 Config select_cfg = {};
544 if (cfg.IsMap()) {
545 select_cfg = get_as<Config>("select", cfg, {});
546 }
547
549 select_cfg);
550 }

◆ apply_env_param_func()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<class EPFB >
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::apply_env_param_func ( EPFB &&  epfb,
bool  initialization = false 
)
inlineprivate

Apply a given environment parameter function.

Parameters
esfEnvParamFunc that is applied
759 {
760 // Check whether to invoke
761 if (initialization) {
762 if (not epfb.invoke_at_initialization) {
763 this->_log->trace("Not invoking environment function '{}' at "
764 "initialization.", epfb.name);
765 return;
766 }
767 }
768 else if (not epfb.invoke_always) {
769 // Compare to first element of the times set
770 // NOTE This approach has a low and constant complexity as no tree
771 // traversal in the set takes place. This, however, relies on
772 // the ordering of the set and that the first element is
773 // never smaller than (current time + 1), which would lead to
774 // clogging of the erasure ...
775 if ( epfb.times.size()
776 and *epfb.times.begin() == (this->_time + 1))
777 {
778 // Invoke at this time; pop element corresponding to this time
779 epfb.times.erase(epfb.times.begin());
780 }
781 else {
782 this->_log->trace("Not invoking environment function '{}' in "
783 "this iteration.", epfb.name);
784 return;
785 }
786 }
787
788 this->_log->debug("Applying environment parameter function '{}' ...",
789 epfb.name);
790 this->_params.set_env(epfb.param_name, epfb.func());
791 }
void set_env(const std::string &key, const double &value) override
Setter.
Definition Environment.cc:32

◆ apply_env_state_func()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<class ESFB >
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::apply_env_state_func ( ESFB &&  esfb)
inlineprivate

Apply a given environment state function.

Parameters
esfEnvStateFunc that is applied to _cm.cells()
796 {
797 // Check whether to invoke
798 if (not esfb.invoke_always) {
799 // Compare to first element of the times set
800 // NOTE This approach has a low and constant complexity as no tree
801 // traversal in the set takes place. This, however, relies on
802 // the ordering of the set and that the first element is
803 // never smaller than (current time + 1), which would lead to
804 // clogging of the erasure ...
805 if ( esfb.times.size()
806 and *esfb.times.begin() == (this->_time + 1))
807 {
808 // Invoke at this time; pop element corresponding to this time
809 esfb.times.erase(esfb.times.begin());
810 }
811 else {
812 this->_log->trace("Not invoking environment function '{}' in "
813 "this iteration.", esfb.name);
814 return;
815 }
816 }
817
818 this->_log->debug("Applying environment state function '{}' ...",
819 esfb.name);
820
821 // get EnvCellContainer over which to apply the function
822 EnvCellContainer cell_selection = {};
823 if (esfb.fix_selection) {
824 cell_selection = esfb.cell_selection;
825 }
826 else if (not esfb.select_cfg.IsNull()) {
827 this->_log->debug("Generating a new selection of cells for "
828 "environment state function '{}' ...", esfb.name);
829 cell_selection = _cm.select_cells(esfb.select_cfg);
830 }
831 else {
832 cell_selection = _cm.cells();
833 }
834
835 this->_log->debug("Applying to {} cells ...",
836 (cell_selection.size() == _cm.cells().size() ?
837 "all" : std::to_string(cell_selection.size())));
838
839 // Need to distinguish by update mode
840 if (esfb.update == Update::sync) {
841 apply_rule<Update::sync>(esfb.func, cell_selection);
842 }
843 else if (esfb.update == Update::async) {
844 apply_rule<Update::async>(esfb.func, cell_selection, *this->_rng);
845 }
846 else {
847 // Throw in case the Update enum gets extended and an unexpected
848 // value is passed ...
849 throw std::invalid_argument("Unsupported `update` argument!");
850 }
851 }
std::string to_string(const Config &node)
Given a config node, returns a string representation of it.
Definition cfg_utils.hh:110
@ async
Asynchronous update.
@ sync
Synchronous update.
Definition parallel.hh:235

◆ cm()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
const auto & Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::cm ( ) const
inline

Return a const reference to the cell manager.

422 {
423 return this->_cm;
424 }

◆ get_parameter()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
double Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::get_parameter ( const std::string &  param_name) const
inline

Return the current value of the parameter with param_name.

412 {
413 return this->_params.get_env(param_name);
414 }
double get_env(const std::string &key) const override
Getter.
Definition Environment.cc:23

◆ monitor()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::monitor ( )
inline

Monitor model information.

379 {
380 }

◆ perform_step()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::perform_step ( )
inline

Iterate a single step.

368 {
369 for (auto& epfb : _env_param_funcs) {
371 }
372 for (auto& esfb : _env_state_funcs) {
374 }
375 }

◆ set_parameter()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::set_parameter ( const std::string &  param_name,
double  value 
)
inline

Change the current value of the parameter with param_name.

417 {
418 this->_params.set_env(param_name, value);
419 }

◆ setup_env_param_funcs()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::setup_env_param_funcs ( const Config cfg)
inlineprivate

Construct the rule funcs sequencefor EnvParam from cfg.

590 {
591 this->_log->info("Setting up environment param function sequence "
592 "from {} configuration entr{} ...",
593 cfg.size(), cfg.size() != 1 ? "ies" : "y");
594
595 // For zombie or empty configurations, return empty container
596 if (not cfg or not cfg.size()) {
597 return;
598 }
599 // Otherwise, require a sequence
600 if (not cfg.IsSequence()) {
601 throw std::invalid_argument("The config for initializing the "
602 "environment functions must be a sequence!");
603 }
604
605 // Iterate over the sequence of mappings
606 for (const auto& epfs : cfg) {
607 // epfs.IsMap() == true
608 // The top `epfs` keys are now the names of the desired environment
609 // functions. Iterate over those ...
610 for (const auto& epf_pair : epfs) {
611 // epf_pair is a pair of (key node, value node)
612 // Find out the name of the rule function
613 const auto epf_name = epf_pair.first.as<std::string>();
614
615 this->_log->trace(" Function name: {}", epf_name);
616
617 // Now iterate over the (param name, param cfg) pairs
618 for (const auto& kv_pair : epf_pair.second) {
619 // Get the parameter name and configuration
620 const auto param_name = kv_pair.first.as<std::string>();
621 const auto& epf_cfg = kv_pair.second;
622
623 // Distinguish by name of rule function
624 if (epf_name == "increment") {
625 auto epf = epf_increment(*this, param_name, epf_cfg);
627 epf_name+"."+param_name, epf, param_name, epf_cfg
628 );
629 }
630 else if (epf_name == "random") {
631 auto epf = epf_random(*this, param_name, epf_cfg);
633 epf_name+"."+param_name, epf, param_name, epf_cfg
634 );
635 }
636 else if (epf_name == "rectangular") {
637 auto epf = epf_rectangular(*this, epf_cfg);
639 epf_name+"."+param_name, epf, param_name, epf_cfg
640 );
641 }
642 else if (epf_name == "set") {
643 auto epf = epf_set(*this, epf_cfg);
645 epf_name+"."+param_name, epf, param_name, epf_cfg
646 );
647 }
648 else if (epf_name == "sinusoidal") {
649 auto epf = epf_sinusoidal(*this, epf_cfg);
651 epf_name+"."+param_name, epf, param_name, epf_cfg
652 );
653 }
654 // .. can add more rule functions here (alphabetic order).
655 // Add also in invalid_argument message below ..
656 else if (epf_name != "void") {
657 throw std::invalid_argument("No environment parameter "
658 "function '" + epf_name + "' available to "
659 "construct! Choose from: increment, random, "
660 "rectangular, set, sinusoidal.");
661 }
662
663 this->_log->debug("Added '{}' environment parameter "
664 "function for parameter '{}'.", epf_name, param_name);
665 }
666 }
667 }
668 };
void add_env_param_func_from_cfg(const std::string &name, const EnvParamFunc &epf, const std::string &param_name, const Config &cfg={})
Add a param function at the end of the sequence of env functions.
Definition Environment.hh:456
EnvParamFunc epf_sinusoidal(const EnvModel &model, const Config &cfg)
Creates a rule function for sinusoidal parameter values.
Definition env_param_func_collection.hh:270
EnvParamFunc epf_random(const EnvModel &model, const std::string &param_name, const Config &cfg)
Creates a rule function for random parameter values.
Definition env_param_func_collection.hh:106
EnvParamFunc epf_increment(const EnvModel &model, const std::string param_name, const Config &cfg)
Creates a rule function for incrementing parameter values.
Definition env_param_func_collection.hh:73
EnvParamFunc epf_rectangular(const EnvModel &model, const Config &cfg)
Creates a rule function for rectangular function like parameter values.
Definition env_param_func_collection.hh:181
EnvParamFunc epf_set(const EnvModel &, const Config &cfg)
Creates a rule function for setting a parameter value.
Definition env_param_func_collection.hh:236

◆ setup_env_state_funcs()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
template<bool add_to_initial = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::setup_env_state_funcs ( const Config cfg)
inlineprivate

Construct the rule funcs sequence from cfg.

672 {
673 this->_log->info("Setting up {}environment state function sequence "
674 "from {} configuration entr{} ...",
675 add_to_initial ? "initial " : "",
676 cfg.size(), cfg.size() != 1 ? "ies" : "y");
677
678 // For zombie or empty configurations, return empty container
679 if (not cfg or not cfg.size()) {
680 return;
681 }
682 // Otherwise, require a sequence
683 if (not cfg.IsSequence()) {
684 throw std::invalid_argument("The config for initializing the "
685 "environment state functions must be a sequence!");
686 }
687
688 // Iterate over the sequence of mappings
689 for (const auto& esfs : cfg) {
690 // esfs.IsMap() == true
691 // The top `esfs` keys are now the names of the desired environment
692 // functions. Iterate over those ...
693 for (const auto& esf_pair : esfs) {
694 // esf_pair is a pair of (key node, value node)
695 // Find out the name of the rule function
696 const auto esf_name = esf_pair.first.as<std::string>();
697
698 this->_log->trace(" Function name: {}", esf_name);
699
700 // Now iterate over the (param name, param cfg) pairs
701 for (const auto& kv_pair : esf_pair.second) {
702 // Get the parameter name and configuration
703 const auto param_name = kv_pair.first.as<std::string>();
704 const auto& esf_cfg = kv_pair.second;
705
706 // Distinguish by name of rule function
707 if (esf_name == "noise") {
708 auto esf_update_pair = esf_noise(*this, param_name,
709 esf_cfg);
711 "noise."+param_name, esf_update_pair.first,
713 );
714 }
715 else if (esf_name == "slope") {
716 auto esf_update_pair = esf_slope(*this, param_name,
717 esf_cfg,
718 _cm.space()->extent);
720 "slope."+param_name, esf_update_pair.first,
722 );
723 }
724 else if (esf_name == "steps") {
725 auto esf_update_pair = esf_steps(*this, param_name,
726 esf_cfg);
728 "steps."+param_name, esf_update_pair.first,
730 );
731 }
732 else if (esf_name == "uniform") {
733 auto esf_update_pair = esf_uniform(*this, param_name,
734 esf_cfg);
736 "uniform."+param_name, esf_update_pair.first,
738 );
739 }
740 // .. can add more rule functions here ..
741 else if (esf_name != "void") {
742 throw std::invalid_argument("No environment state "
743 "function '" + esf_name + "' available to "
744 "construct! Choose from: noise, slope, steps, "
745 "uniform.");
746 }
747
748 this->_log->trace("Added '{}' environment state"
749 "function for parameter '{}'.", esf_name, param_name);
750 }
751 }
752 }
753 };
const std::shared_ptr< Space > & space() const
Return pointer to the space, for convenience.
Definition cell_manager.hh:209
std::pair< EnvStateFunc, Update > esf_uniform(const EnvModel &, const std::string &param_name, const Config &cfg)
Creates a rule function for spatially uniform parameter values.
Definition env_state_func_collection.hh:260
std::pair< EnvStateFunc, Update > esf_noise(const EnvModel &model, const std::string &param_name, const Config &cfg)
Creates a rule function for noisy parameter values.
Definition env_state_func_collection.hh:77
std::pair< EnvStateFunc, Update > esf_steps(const EnvModel &, const std::string &param_name, const Config &cfg)
Creates a rule function for spatial steps in the parameter values.
Definition env_state_func_collection.hh:201
std::pair< EnvStateFunc, Update > esf_slope(const EnvModel &, const std::string &param_name, const Config &cfg, const Extent &extent)
Creates a rule function for spatially linearly parameter values.
Definition env_state_func_collection.hh:151

◆ track_parameter()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::track_parameter ( const std::string &  key)
inline

Mark a parameter as being tracked, i.e. store its data in write_data.

553 {
554 if (_dsets_param.find(key) != _dsets_param.end()) {
555 throw std::invalid_argument("Parameter '" + key + "' is already "
556 "being tracked!");
557 }
558 _dsets_param.insert({key, this->create_dset(key, {})});
559 }
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

◆ track_parameters()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::track_parameters ( const std::vector< std::string > &  keys)
inline

Track multiple parameters.

Invokes track_state for each entry

564 {
565 for (const auto& key : keys) {
567 }
568 }
void track_parameter(const std::string &key)
Mark a parameter as being tracked, i.e. store its data in write_data.
Definition Environment.hh:553

◆ track_state()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::track_state ( const std::string &  key)
inline

Mark a state as being tracked, i.e. store its data in write_data.

571 {
572 if (_dsets_state.find(key) != _dsets_state.end()) {
573 throw std::invalid_argument("State '" + key + "' is already "
574 "being tracked!");
575 }
576 _dsets_state.insert({key, this->create_cm_dset(key, _cm)});
577 }
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

◆ track_states()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::track_states ( const std::vector< std::string > &  keys)
inline

Track multiple states.

Invokes track_state for each entry

582 {
583 for (const auto& key : keys) {
585 }
586 }
void track_state(const std::string &key)
Mark a state as being tracked, i.e. store its data in write_data.
Definition Environment.hh:571

◆ write_data()

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
void Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::write_data ( )
inline

Write data.

For all parameters and states registered for writing, writes the values to the corresponding dataset.

Note
To register keys, use track_parameter and track_state method, respectively
390 {
391 // write parameters
392 for (auto& [key, dset] : _dsets_param) {
393 dset->write(_params.get_env(key));
394 }
395
396 // write states
397 for (auto& param_dset_pair : _dsets_state) {
398 const auto key = std::get<0>(param_dset_pair);
399 auto& dset = std::get<1>(param_dset_pair);
400
401 dset->write(_cm.cells().begin(), _cm.cells().end(),
402 [key](const auto& cell) {
403 return cell->state.get_env(key);
404 }
405 );
406 }
407 }

Member Data Documentation

◆ _cm

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
CellManager Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_cm
private

The cell manager.

◆ _dsets_param

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
std::unordered_map<std::string, std::shared_ptr<DataSet> > Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_dsets_param
private

Dynamically generated map of datasets of parameters.

◆ _dsets_state

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
std::unordered_map<std::string, std::shared_ptr<DataSet> > Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_dsets_state
private

Dynamically generated map of datasets of cell states.

◆ _env_param_funcs

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
std::vector<EnvParamFuncBundle> Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_env_param_funcs
private

Container of functions that are invoked every time step.

Functions changing a parameter

◆ _env_state_funcs

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
std::vector<EnvStateFuncBundle> Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_env_state_funcs
private

Container of rule functions that are invoked every time step.

Functions changing a state

◆ _init_env_state_funcs

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
std::vector<EnvStateFuncBundle> Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_init_env_state_funcs
private

Container of rule functions that are invoked once at initialisation.

Functions changing a state

◆ _params

template<typename EnvParam = DummyEnvParam, typename EnvCellState = DummyEnvCellState, bool standalone = false>
EnvParam Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >::_params
private

The environment parameters.


The documentation for this class was generated from the following file: