Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Classes | Typedefs | Enumerations | Functions
Environment

The environment model provides a collection of parameter functions designed for coupling to other models. More...

Collaboration diagram for Environment:

Classes

struct  Utopia::Models::Environment::BaseEnvParam
 Base type for environment parameter. More...
 
struct  Utopia::Models::Environment::DummyEnvParam
 Dummy type for environment parameter. More...
 
struct  Utopia::Models::Environment::BaseEnvCellState
 Base type for environment cell states. More...
 
struct  Utopia::Models::Environment::DummyEnvCellState
 Dummy type for environment cell states. More...
 
class  Utopia::Models::Environment::Environment< EnvParam, EnvCellState, standalone >
 The Environment model provides a non-uniform, dynamic parameter background. More...
 
struct  Utopia::Models::Environment::FuncBundle::FuncBundle< Func, Time >
 A bundle wrapping a (iterative) function with metadata. More...
 
struct  Utopia::Models::Environment::FuncBundle::ParamFuncBundle< Func, Time >
 A bundle wrapping a (iterative) function with metadata. More...
 
struct  Utopia::Models::Environment::FuncBundle::RuleFuncBundle< RuleFunc, Time, CellContainer >
 A bundle wrapping a (rule-)function with metadata. More...
 

Typedefs

using Utopia::Models::Environment::ModelTypes = Utopia::ModelTypes<>
 Type helper to define types used by the model.
 

Enumerations

enum class  Utopia::Models::Environment::ValMode { Utopia::Models::Environment::ValMode::Set , Utopia::Models::Environment::ValMode::Add }
 Value calculation mode. More...
 

Functions

template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_increment (const EnvModel &model, const std::string param_name, const Config &cfg)
 Creates a rule function for incrementing parameter values.
 
template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_random (const EnvModel &model, const std::string &param_name, const Config &cfg)
 Creates a rule function for random parameter values.
 
template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_rectangular (const EnvModel &model, const Config &cfg)
 Creates a rule function for rectangular function like parameter values.
 
template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_set (const EnvModel &, const Config &cfg)
 Creates a rule function for setting a parameter value.
 
template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_sinusoidal (const EnvModel &model, const Config &cfg)
 Creates a rule function for sinusoidal parameter values.
 
template<typename EnvModel , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, UpdateUtopia::Models::Environment::StateFunctionCollection::esf_noise (const EnvModel &model, const std::string &param_name, const Config &cfg)
 Creates a rule function for noisy parameter values.
 
template<typename EnvModel , typename Extent , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, UpdateUtopia::Models::Environment::StateFunctionCollection::esf_slope (const EnvModel &, const std::string &param_name, const Config &cfg, const Extent &extent)
 Creates a rule function for spatially linearly parameter values.
 
template<typename EnvModel , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, UpdateUtopia::Models::Environment::StateFunctionCollection::esf_steps (const EnvModel &, const std::string &param_name, const Config &cfg)
 Creates a rule function for spatial steps in the parameter values.
 
template<typename EnvModel , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, UpdateUtopia::Models::Environment::StateFunctionCollection::esf_uniform (const EnvModel &, const std::string &param_name, const Config &cfg)
 Creates a rule function for spatially uniform parameter values.
 
ValMode Utopia::Models::Environment::extract_val_mode (const DataIO::Config &cfg, const std::string &context)
 Given a configuration node, extract the value mode.
 
template<typename Time >
std::pair< bool, std::set< Time > > Utopia::Models::Environment::extract_times (const DataIO::Config &cfg)
 
template<typename Time >
std::tuple< bool, bool, std::set< Time > > Utopia::Models::Environment::extract_times_and_initialization (const DataIO::Config &cfg)
 
template<typename Time >
Time Utopia::Models::Environment::extract_time_start (const DataIO::Config &cfg)
 Given a configuration, extracts the time of first function invocation.
 

Detailed Description

The environment model provides a collection of parameter functions designed for coupling to other models.

For details on how this is to be used, consult the actual model documentation. The doxygen documentation here provides merely the API reference and information on the available parameters for each of the environment functions.

Typedef Documentation

◆ ModelTypes

Type helper to define types used by the model.

Enumeration Type Documentation

◆ ValMode

Value calculation mode.

Enumerator
Set 

Set a value, discarding the current state.

Add 

Add the new value to the existing value.

10 {
12 Set,
13
15 Add
16};
@ Set
Set a value, discarding the current state.
@ Add
Add the new value to the existing value.

Function Documentation

◆ epf_increment()

template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_increment ( const EnvModel model,
const std::string  param_name,
const Config cfg 
)

Creates a rule function for incrementing parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • times: Sequence of time points at which to invoke this. if None, always incrementing
  • value: The incrementor
  • invoke_at_initialization: Whether to invoke at initialization. Default is False
75{
76 const auto value = get_as<double>("value", cfg);
77 EnvParamFunc epf = [&model, param_name, value] () mutable
78 {
79 return model.get_parameter(param_name) + value;
80 };
81
82 return epf;
83}

◆ epf_random()

template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_random ( const EnvModel model,
const std::string &  param_name,
const Config cfg 
)

Creates a rule function for random parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • mode: set (default) or add
  • times: Sequence of time points at which to invoke this
  • distribution: The distribution type. For each value below, the corresponding additional parameters are required in cfg:
    • normal: mean and stddev
    • poisson: mean
    • exponential: lambda
    • uniform: interval (length 2 array)
    • uniform_int: interval (length 2 array)
108{
109 const auto mode = extract_val_mode(cfg, "random");
110 const auto distribution = get_as<std::string>("distribution", cfg);
111
112 // Depending on chosen distribution, construct it and build a rule
113 // function using a reference to the newly created one...
114 if (distribution == "normal") {
115 const auto mean = get_as<double>("mean", cfg);
116 const auto stddev = get_as<double>("stddev", cfg);
117 std::normal_distribution<> dist(mean, stddev);
118
119 return build_rng_env_param_func(model, std::move(dist), param_name,
120 mode);
121 }
122 else if (distribution == "poisson") {
123 const auto mean = get_as<double>("mean", cfg);
124 std::poisson_distribution<> dist(mean);
125
126 return build_rng_env_param_func(model, std::move(dist), param_name,
127 mode);
128 }
129 else if (distribution == "exponential") {
130 const auto lambda = get_as<double>("lambda", cfg);
131 std::exponential_distribution<> dist(lambda);
132
133 return build_rng_env_param_func(model, std::move(dist), param_name,
134 mode);
135 }
136 else if (distribution == "uniform_int") {
137 auto interval = get_as<std::array<int, 2>>("interval", cfg);
138 std::uniform_int_distribution<> dist(interval[0], interval[1]);
139
140 return build_rng_env_param_func(model, std::move(dist), param_name,
141 mode);
142 }
143 else if (distribution == "uniform_real" or distribution == "uniform") {
144 auto interval = get_as<std::array<double, 2>>("interval", cfg);
145 std::uniform_real_distribution<> dist(interval[0], interval[1]);
146
147 return build_rng_env_param_func(model, std::move(dist), param_name,
148 mode);
149 }
150 else {
151 throw std::invalid_argument("No method implemented to resolve "
152 "noise distribution '" + distribution + "'! Valid options: "
153 "normal, poisson, uniform_int, uniform_real.");
154 }
155};
ValMode extract_val_mode(const DataIO::Config &cfg, const std::string &context)
Given a configuration node, extract the value mode.
Definition tools.hh:19
EnvParamFunc build_rng_env_param_func(const EnvModel &model, DistType &&dist, const std::string &param_name, const ValMode &mode)
Create a rule function that uses a random number distribution.
Definition env_param_func_collection.hh:24

◆ epf_rectangular()

template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_rectangular ( const EnvModel model,
const Config cfg 
)

Creates a rule function for rectangular function like parameter values.

Rectangular shaped parameters.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • times: Sequence of time points at which to invoke this. if None, always setting
  • offset: The minimum value. Optional, default 0.
  • amplitude: The amplitude above minimum value
  • period: The period
  • time_in_max: The number of steps in max value. Optional, default half period.
  • phase: The phase at first invocation in fraction of period length. E.g.: 0.5 for initialization at half period. Optional, default 0 equiv. in high value.

182{
183 using Time = typename EnvModel::Time;
184
185 double min_value = get_as<double>("offset", cfg, 0.);
186 double max_value = get_as<double>("amplitude", cfg) + min_value;
187 Time period = get_as<Time>("period", cfg);
188 Time time_in_max = get_as<Time>("time_in_max", cfg, period / 2.);
189 double phase = get_as<double>("phase", cfg, 0) * period;
190
191 // Check parameter
192 if (time_in_max > period) {
193 throw std::invalid_argument("The `time_in_max` argument cannot be "
194 "larger than the `period` argument in rectangular "
195 "environment parameter function!");
196 }
197 if (phase < 0 or phase > period) {
198 throw std::invalid_argument("The `phase` argument was not in interval "
199 "[0., 1.]!");
200 }
201
202 // get starting time
203 Time time_start = extract_time_start<Time>(cfg);
204
205 // Build function
206 EnvParamFunc epf = [&model, max_value, min_value,
207 period, time_in_max, phase, time_start] () mutable
208 {
209 auto time = (model.get_time()+1 - time_start) % period;
210 if (time >= phase and time < time_in_max + phase) {
211 return max_value;
212 }
213 return min_value;
214 };
215
216 return epf;
217}

◆ epf_set()

template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_set ( const EnvModel ,
const Config cfg 
)

Creates a rule function for setting a parameter value.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • times: Sequence of time points at which to invoke this. if None, always setting
  • value: The new value
  • invoke_at_initialization: Whether to invoke at initialization. Default is False
237{
238 const auto value = get_as<double>("value", cfg);
239 EnvParamFunc epf = [value] () mutable
240 {
241 return value;
242 };
243
244 return epf;
245}

◆ epf_sinusoidal()

template<typename EnvModel , class EnvParamFunc = typename EnvModel::EnvParamFunc>
EnvParamFunc Utopia::Models::Environment::ParameterFunctionCollection::epf_sinusoidal ( const EnvModel model,
const Config cfg 
)

Creates a rule function for sinusoidal parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • times: Sequence of time points at which to invoke this. if None, always setting
  • period: The period of the sinus
  • amplitude: The amplitude
  • phase: The phase shift at the point of first invokation. In multiples of pi. 1 = pi or 180 degree shift. Optional, default 0.
  • offset: Offset of the mean value over the full period. Optional, default 0.
Note
Cannot be invoked at initialization.
271{
272 using Time = typename EnvModel::Time;
273
274 const auto period = get_as<double>("period", cfg);
275 const auto amplitude = get_as<double>("amplitude", cfg);
276 double phase = 0;
277 if (cfg["phase"]) {
278 phase = get_as<double>("phase", cfg);
279 }
280 double offset = 0.;
281 if (cfg["offset"]) {
282 offset = get_as<double>("offset", cfg);
283 }
284
285 // get starting time
286 Time time_start = extract_time_start<Time>(cfg);
287
288 EnvParamFunc epf = [&model, period, amplitude,
289 phase, offset, time_start] () mutable
290 {
291 double x = (model.get_time() + 1 - time_start)/period * 2*M_PI;
292 return offset + amplitude * sin(x + phase * M_PI);
293 };
294
295 return epf;
296}

◆ esf_noise()

template<typename EnvModel , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, Update > Utopia::Models::Environment::StateFunctionCollection::esf_noise ( const EnvModel model,
const std::string &  param_name,
const Config cfg 
)

Creates a rule function for noisy parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • mode: set (default) or add
  • times: Sequence of time points at which to invoke this
  • distribution: The distribution type. For each value below, the corresponding additional parameters are required in cfg:
    • normal: mean and stddev
    • poisson: mean
    • exponential: lambda
    • uniform: interval (length 2 array)
    • uniform_int: interval (length 2 array)
80{
81 const auto mode = extract_val_mode(cfg, "noise");
82 const auto distribution = get_as<std::string>("distribution", cfg);
83
84 // Depending on chosen distribution, construct it and build a rule
85 // function using a reference to the newly created one...
86 if (distribution == "normal") {
87 const auto mean = get_as<double>("mean", cfg);
88 const auto stddev = get_as<double>("stddev", cfg);
89 std::normal_distribution<> dist(mean, stddev);
90
91 auto esf = build_rng_env_state_func(model, std::move(dist), param_name,
92 mode);
93 return {esf, Update::sync};
94 }
95 else if (distribution == "poisson") {
96 const auto mean = get_as<double>("mean", cfg);
97 std::poisson_distribution<> dist(mean);
98
99 auto esf = build_rng_env_state_func(model, std::move(dist), param_name,
100 mode);
101 return {esf, Update::sync};
102 }
103 else if (distribution == "exponential") {
104 const auto lambda = get_as<double>("lambda", cfg);
105 std::exponential_distribution<> dist(lambda);
106
107 auto esf = build_rng_env_state_func(model, std::move(dist), param_name,
108 mode);
109 return {esf, Update::sync};
110 }
111 else if (distribution == "uniform_int") {
112 auto interval = get_as<std::array<int, 2>>("interval", cfg);
113 std::uniform_int_distribution<> dist(interval[0], interval[1]);
114
115 auto esf = build_rng_env_state_func(model, std::move(dist), param_name,
116 mode);
117 return {esf, Update::sync};
118 }
119 else if (distribution == "uniform_real" or distribution == "uniform") {
120 auto interval = get_as<std::array<double, 2>>("interval", cfg);
121 std::uniform_real_distribution<> dist(interval[0], interval[1]);
122
123 auto esf = build_rng_env_state_func(model, std::move(dist), param_name,
124 mode);
125 return {esf, Update::sync};
126 }
127 else {
128 throw std::invalid_argument("No method implemented to resolve "
129 "noise distribution '" + distribution + "'! Valid options: "
130 "normal, poisson, uniform_int, uniform_real.");
131 }
132};
EnvStateFunc build_rng_env_state_func(EnvModel &model, DistType &&dist, const std::string &param_name, const ValMode &mode)
Create a rule function that uses a random number distribution.
Definition env_state_func_collection.hh:21

◆ esf_slope()

template<typename EnvModel , typename Extent , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, Update > Utopia::Models::Environment::StateFunctionCollection::esf_slope ( const EnvModel ,
const std::string &  param_name,
const Config cfg,
const Extent extent 
)

Creates a rule function for spatially linearly parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • mode: set (default) or add
  • times: Sequence of time points
  • values_north_south: Values at northern and souther boundary; uses linear interpolation in between.
extentThe extent of space
155{
156 const auto mode = extract_val_mode(cfg, "slope");
157
158 const auto values_north_south =
159 get_as<std::array<double, 2>>("values_north_south", cfg);
160
161 EnvStateFunc esf =
162 [param_name, mode, values_north_south, extent]
163 (const auto& env_cell) mutable
164 {
165 auto& env_state = env_cell->state;
166
167 // Use the relative position along y-dimension
168 const double pos = ( env_state.position[1] / extent[1]);
169 const double slope = values_north_south[0] - values_north_south[1];
170 const double value = values_north_south[1] + pos * slope;
171
172 double current_value = 0.;
173 if (mode == ValMode::Add) {
174 current_value = env_state.get_env(param_name);
175 }
176 env_state.set_env(param_name, current_value + value);
177 return env_state;
178 };
179
180 return {esf, Update::sync};
181};

◆ esf_steps()

template<typename EnvModel , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, Update > Utopia::Models::Environment::StateFunctionCollection::esf_steps ( const EnvModel ,
const std::string &  param_name,
const Config cfg 
)

Creates a rule function for spatial steps in the parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • mode: set (default) or add
  • times: Sequence of time points
  • values_north_south: Sequence of parameter values for the step heights, from north to south.
  • latitudes: Sequence of latitudes of separation, from north to south
204{
205 const auto mode = extract_val_mode(cfg, "steps");
206
207 const auto latitudes =
208 get_as<std::vector<double>>("latitudes", cfg, {0.5});
209 const auto values_north_south =
210 get_as<std::vector<double>>("values_north_south", cfg);
211
212 if (latitudes.size() != values_north_south.size() - 1) {
213 throw std::invalid_argument("The list of 'latitudes' and"
214 " 'values_north_south' don't match in size. Sizes were "
215 + std::to_string(latitudes.size()) + " and "
216 + std::to_string(values_north_south.size()) +
217 ". Values_north_south must have one element more that"
218 " latitudes.");
219 }
220
221 EnvStateFunc esf =
222 [param_name, mode, latitudes, values_north_south]
223 (const auto& env_cell) mutable
224 {
225 auto& env_state = env_cell->state;
226 double value = values_north_south[0];
227 for (unsigned int i = 0; i < latitudes.size(); ++i) {
228 if (env_state.position[1] > latitudes[i]) {
229 break;
230 }
231 value = values_north_south[i+1];
232 }
233
234 double current_value = 0.;
235 if (mode == ValMode::Add) {
236 current_value = env_state.get_env(param_name);
237 }
238 env_state.set_env(param_name, current_value + value);
239 return env_state;
240 };
241
242 return {esf, Update::sync};
243};

◆ esf_uniform()

template<typename EnvModel , class EnvStateFunc = typename EnvModel::EnvStateFunc>
std::pair< EnvStateFunc, Update > Utopia::Models::Environment::StateFunctionCollection::esf_uniform ( const EnvModel ,
const std::string &  param_name,
const Config cfg 
)

Creates a rule function for spatially uniform parameter values.

Parameters
param_nameThe parameter to attach this environment function to
cfg

Configuration for this environment function. Allows the following arguments:

  • mode: set (default) or add
  • times: Sequence of time points
  • value: The scalar value to use
263{
264 ValMode mode;
265 double value;
266
267 // Extract configuration depending on whether cfg is scalar or mapping
268 if (cfg.IsScalar()) {
269 // Interpret as desiring to set to the given scalar value
270 mode = ValMode::Set;
271 value = cfg.as<double>();
272 }
273 else if (cfg.IsMap()) {
274 mode = extract_val_mode(cfg, "uniform");
275 value = get_as<double>("value", cfg);
276 }
277 else {
278 throw std::invalid_argument("The configuration for environment "
279 "function 'uniform' must be a scalar or a mapping!");
280 }
281
282 EnvStateFunc esf =
283 [param_name, mode, value]
284 (const auto& env_cell) mutable
285 {
286 auto& env_state = env_cell->state;
287
288 double current_value = 0.;
289 if (mode == ValMode::Add) {
290 current_value = env_state.get_env(param_name);
291 }
292
293 env_state.set_env(param_name, current_value + value);
294 return env_state;
295 };
296
297 return {esf, Update::sync};
298}
ValMode
Value calculation mode.
Definition tools.hh:10

◆ extract_time_start()

template<typename Time >
Time Utopia::Models::Environment::extract_time_start ( const DataIO::Config cfg)

Given a configuration, extracts the time of first function invocation.

86 {
87 // get starting time
88 auto times_list = get_as<std::vector<Time>>("times", cfg, {0});
89 auto time_start = times_list.front();
90 for (auto t : times_list) {
91 if (t < time_start and t >= 0)
92 time_start = t;
93 }
94
95 return time_start;
96}

◆ extract_times()

template<typename Time >
std::pair< bool, std::set< Time > > Utopia::Models::Environment::extract_times ( const DataIO::Config cfg)

Given a configuration, extracts the set of times at which to invoke environment functions

39 {
40 bool invoke_always = true;
41 std::set<Time> times;
42
43 if (not cfg.IsMap()) {
44 // Already return here
45 return {invoke_always, times};
46 }
47
48 // Extract information from configuration
49 if (cfg["times"]) {
50 invoke_always = false;
51 auto times_list = get_as<std::vector<Time>>("times", cfg);
52 // TODO Consider wrapping negative values around
53
54 // Make sure negative times or 0 is not included
55 // NOTE 0 may not be included because the environment state functions
56 // are invoked separately for this time.
57 times_list.erase(
58 std::remove_if(times_list.begin(), times_list.end(),
59 [](auto& t){ return (t <= 0); }),
60 times_list.end()
61 );
62
63 // Populate the set; this will impose ordering
64 times.insert(times_list.begin(), times_list.end());
65 }
66
67 return {invoke_always, times};
68}

◆ extract_times_and_initialization()

template<typename Time >
std::tuple< bool, bool, std::set< Time > > Utopia::Models::Environment::extract_times_and_initialization ( const DataIO::Config cfg)

Given a configuration, extracts the set of times at which to invoke environment functions and whether to invoke them at initialization

75{
76 bool invoke_at_initialization = get_as<bool>("invoke_at_initialization",
77 cfg);
78
79 auto [invoke_always, times] = extract_times<Time>(cfg);
80
81 return {invoke_at_initialization, invoke_always, times};
82}

◆ extract_val_mode()

ValMode Utopia::Models::Environment::extract_val_mode ( const DataIO::Config cfg,
const std::string &  context 
)

Given a configuration node, extract the value mode.

21{
22 const auto mode_key = get_as<std::string>("mode", cfg);
23
24 if (mode_key == "add") {
25 return ValMode::Add;
26 }
27 else if (mode_key == "set") {
28 return ValMode::Set;
29 }
30
31 throw std::invalid_argument("The `mode` argument for configuration of "
32 "environment function " + context + " can be 'add' or 'set', but "
33 "was '" + mode_key + "'!");
34}