Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Public Attributes | Private Attributes | List of all members
Utopia::Models::SimpleFlocking::SimpleFlocking Class Reference

The SimpleFlocking Model. More...

#include <SimpleFlocking.hh>

Inheritance diagram for Utopia::Models::SimpleFlocking::SimpleFlocking:
Inheritance graph
[legend]
Collaboration diagram for Utopia::Models::SimpleFlocking::SimpleFlocking:
Collaboration graph
[legend]

Public Types

using Base = Model< SimpleFlocking, ModelTypes >
 The type of the Model base class of this derived class.
 
using AgentManager = Utopia::AgentManager< AgentTraits, SimpleFlocking >
 The type of the agent manager.
 
using Agent = AgentManager::Agent
 Agent type.
 
using AgentPtr = std::shared_ptr< Agent >
 Pointer to agent.
 
using Rule = typename AgentManager::RuleFunc
 Type of the update rules for agents.
 
using VoidRule = typename AgentManager::VoidRuleFunc
 Type of the update rules for agents where no state is to be returned.
 
using SpaceVec = typename AgentManager::SpaceVec
 Type of spatial vectors within the domain.
 
using DataSet = typename Base::DataSet
 Data type for a dataset.
 
- Public Types inherited from Utopia::Model< SimpleFlocking, ModelTypes >
using Config = typename ModelTypes::Config
 Data type that holds the configuration.
 
using DataManager = DataIO::Default::DefaultDataManager< SimpleFlocking >
 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 >
 SimpleFlocking (const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
 Construct the SimpleFlocking model instance.
 
void perform_step ()
 Iterate a single step: adjust agent orientation, then move all agents.
 
void monitor ()
 Monitor the model state.
 
void write_data ()
 Write data.
 
std::size_t num_agents () const
 The number of agents in the system (typically constant)
 
void set_agent_speed (const double new_speed)
 Sets the speed value of all agents.
 
double norm_group_velocity () const
 The normalized absolute group velocity.
 
template<class Adapter , class ValueType = std::invoke_result_t<Adapter, const AgentPtr&>>
std::vector< ValueTypeget_from_agents (const Adapter &adapter) const
 Aggregate agent properties into a container.
 
- Public Member Functions inherited from Utopia::Model< SimpleFlocking, ModelTypes >
 Model (const std::string &name, const ParentModel &parent_model, const Config &custom_cfg={}, std::tuple< WriterArgs... > w_args={}, const DataIO::Default::DefaultDecidermap< SimpleFlocking > &w_deciders=DataIO::Default::default_deciders< SimpleFlocking >, const DataIO::Default::DefaultTriggermap< SimpleFlocking > &w_triggers=DataIO::Default::default_triggers< SimpleFlocking >)
 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.
 
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.
 
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.
 

Public Attributes

const Rule _adjust_orientation
 Rule that sets agent orientation to the mean orientation (in a radius)
 
const VoidRule _move
 Rule that applies the current displacement vector to the agent position.
 

Private Attributes

AgentManager _am
 The agent manager.
 
double _speed
 The global speed value, used as the uniform speed of all agents.
 
double _interaction_radius
 The radius within which agents interact with each other.
 
double _noise_level
 The amplitude of the noise applied to the orientation update.
 
std::uniform_real_distribution< double_noise_distr
 The distribution used for determining the orientation noise.
 
bool _store_agent_data
 Whether to store agent-specific data.
 
std::shared_ptr< DataSet_dset_agent_x
 
std::shared_ptr< DataSet_dset_agent_y
 
std::shared_ptr< DataSet_dset_agent_orientation
 
std::shared_ptr< DataSet_dset_orientation_circmean
 
std::shared_ptr< DataSet_dset_orientation_circstd
 
std::shared_ptr< DataSet_dset_norm_group_velocity
 

Additional Inherited Members

- Protected Member Functions inherited from Utopia::Model< SimpleFlocking, 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.
 
SimpleFlocking & impl ()
 cast to the derived class
 
const SimpleFlocking & impl () const
 const cast to the derived interface
 
- Protected Attributes inherited from Utopia::Model< SimpleFlocking, 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< SimpleFlocking, ModelTypes >
static constexpr WriteMode _write_mode
 Which data-writing mode the base model should use.
 

Detailed Description

The SimpleFlocking Model.

An agent-based model of spatial collective dynamics, akin to those observed in bird flocks or fish schools.

These dynamics arise as a result of agents adjusting their orientation according to that of nearby agents.

Member Typedef Documentation

◆ Agent

Agent type.

◆ AgentManager

The type of the agent manager.

◆ AgentPtr

Pointer to agent.

◆ Base

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

◆ DataSet

Data type for a dataset.

◆ Rule

Type of the update rules for agents.

◆ SpaceVec

Type of spatial vectors within the domain.

◆ VoidRule

Type of the update rules for agents where no state is to be returned.

Constructor & Destructor Documentation

◆ SimpleFlocking()

template<class ParentModel >
Utopia::Models::SimpleFlocking::SimpleFlocking::SimpleFlocking ( const std::string &  name,
ParentModel parent_model,
const DataIO::Config custom_cfg = {} 
)
inline

Construct the SimpleFlocking model instance.

Parameters
nameName of this model instance; is used to extract the configuration from the parent model and set up a HDFGroup for this instance
parent_modelThe parent model this model instance resides in
custom_cfgA custom configuration to use instead of the one extracted from the parent model using the instance name
123 {}
124 )
125 :
127
128 , _am(*this)
129
130 , _speed(get_as<double>("speed", this->_cfg))
131 , _interaction_radius(get_as<double>("interaction_radius", this->_cfg))
132 , _noise_level(get_as<double>("noise_level", this->_cfg))
133
135
136 // .. Output-related ......................................................
137 , _store_agent_data(get_as<bool>("store_agent_data", this->_cfg))
138
139 , _dset_agent_x(this->create_am_dset("agent/x", _am))
140 , _dset_agent_y(this->create_am_dset("agent/y", _am))
141 , _dset_agent_orientation(this->create_am_dset("agent/orientation", _am))
142
144 this->create_dset("orientation_circmean", {})
145 )
147 this->create_dset("orientation_circstd", {})
148 )
150 this->create_dset("norm_group_velocity", {})
151 )
152 {
154
155 this->_log->info("{} all set up.", this->_name);
156 this->_log->info(" Store agent data? {}", _store_agent_data);
157 }
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_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.
Definition model.hh:939
const std::shared_ptr< spdlog::logger > _log
The (model) logger.
Definition model.hh:164
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
double _interaction_radius
The radius within which agents interact with each other.
Definition SimpleFlocking.hh:80
std::shared_ptr< DataSet > _dset_agent_orientation
Definition SimpleFlocking.hh:99
AgentManager _am
The agent manager.
Definition SimpleFlocking.hh:72
std::shared_ptr< DataSet > _dset_agent_x
Definition SimpleFlocking.hh:97
Model< SimpleFlocking, ModelTypes > Base
The type of the Model base class of this derived class.
Definition SimpleFlocking.hh:44
std::shared_ptr< DataSet > _dset_norm_group_velocity
Definition SimpleFlocking.hh:104
double _noise_level
The amplitude of the noise applied to the orientation update.
Definition SimpleFlocking.hh:83
std::uniform_real_distribution< double > _noise_distr
The distribution used for determining the orientation noise.
Definition SimpleFlocking.hh:89
double _speed
The global speed value, used as the uniform speed of all agents.
Definition SimpleFlocking.hh:77
std::shared_ptr< DataSet > _dset_orientation_circmean
Definition SimpleFlocking.hh:102
std::shared_ptr< DataSet > _dset_agent_y
Definition SimpleFlocking.hh:98
void set_agent_speed(const double new_speed)
Sets the speed value of all agents.
Definition SimpleFlocking.hh:244
bool _store_agent_data
Whether to store agent-specific data.
Definition SimpleFlocking.hh:94
std::shared_ptr< DataSet > _dset_orientation_circstd
Definition SimpleFlocking.hh:103
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

Member Function Documentation

◆ get_from_agents()

template<class Adapter , class ValueType = std::invoke_result_t<Adapter, const AgentPtr&>>
std::vector< ValueType > Utopia::Models::SimpleFlocking::SimpleFlocking::get_from_agents ( const Adapter adapter) const
inline

Aggregate agent properties into a container.

278 {
279 std::vector<ValueType> cont;
280 cont.reserve(num_agents());
281
283 this->_am.agents().begin(), this->_am.agents().end(),
284 std::back_inserter(cont), adapter
285 );
286 return cont;
287 }
const AgentContainer< Agent > & agents() const
Return const reference to the managed agents.
Definition agent_manager.hh:184
std::size_t num_agents() const
The number of agents in the system (typically constant)
Definition SimpleFlocking.hh:239
OutputIt transform(const Utopia::ExecPolicy policy, InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op)
Apply a unary operator to a range and store the result in a new range.
Definition parallel.hh:368

◆ monitor()

void Utopia::Models::SimpleFlocking::SimpleFlocking::monitor ( )
inline

Monitor the model state.

This monitor provides information about the current orientation's (circular) mean and standard deviation. In addition, the normalized group velocity is monitored, which represents the system's order parameter.

184 {
185 const auto orientations = get_from_agents([](const auto& agent){
186 return agent->state().get_orientation();
187 });
188
190 this->_monitor.set_entry("orientation_mean", circ_mean);
191 this->_monitor.set_entry("orientation_std", circ_std);
192 this->_monitor.set_entry("norm_group_velocity", norm_group_velocity());
193 }
Monitor _monitor
The monitor.
Definition model.hh:188
std::vector< ValueType > get_from_agents(const Adapter &adapter) const
Aggregate agent properties into a container.
Definition SimpleFlocking.hh:278
double norm_group_velocity() const
The normalized absolute group velocity.
Definition SimpleFlocking.hh:266
auto circular_mean_and_std(const Container &angles)
Computes the circular mean and std from a sample of (constrained) angles.
Definition utils.hh:129

◆ norm_group_velocity()

double Utopia::Models::SimpleFlocking::SimpleFlocking::norm_group_velocity ( ) const
inline

The normalized absolute group velocity.

Computed by dividing the absolute group velocity of all agents by the absolute speed. This represents the order parameter of the system.

Note
The computation assumes that all agents have the same speed (as indicated by the _speed member). If this is not the case, the result will be erroneous! For performance reasons, no check for uniform agent velocities is carried out.
266 {
267 const auto velocities = get_from_agents([](const auto& agent){
268 return agent->state().get_displacement();
269 });
270 return absolute_group_velocity(velocities) / std::fabs(_speed);
271 }
double absolute_group_velocity(const Container &velocities)
Computes the absolute group velocity from a container of velocity vectors.
Definition utils.hh:58

◆ num_agents()

std::size_t Utopia::Models::SimpleFlocking::SimpleFlocking::num_agents ( ) const
inline

The number of agents in the system (typically constant)

239 {
240 return _am.agents().size();
241 }

◆ perform_step()

void Utopia::Models::SimpleFlocking::SimpleFlocking::perform_step ( )
inline

Iterate a single step: adjust agent orientation, then move all agents.

These rules are both applied synchronously to all agents.

172 {
175 }
const VoidRule _move
Rule that applies the current displacement vector to the agent position.
Definition SimpleFlocking.hh:332
const Rule _adjust_orientation
Rule that sets agent orientation to the mean orientation (in a radius)
Definition SimpleFlocking.hh:298
void apply_rule(Rule &&rule, const ContTarget &cont_target, ContArgs &&... cont_args)
Sequential overload.
Definition apply.hh:133

◆ set_agent_speed()

void Utopia::Models::SimpleFlocking::SimpleFlocking::set_agent_speed ( const double  new_speed)
inline

Sets the speed value of all agents.

244 {
245 this->_log->info("Setting all agent's speed to {} ...", new_speed);
247 [speed=new_speed](const auto& agent){
248 auto state = agent->state();
249 state.set_speed(speed);
250 return state;
251 },
252 this->_am.agents()
253 );
254 }

◆ write_data()

void Utopia::Models::SimpleFlocking::SimpleFlocking::write_data ( )
inline

Write data.

197 {
198 using WriteT = float;
199 const auto& agents = _am.agents();
200
201 // -- Global observables
202 const auto orientations = get_from_agents([](const auto& agent){
203 return agent->state().get_orientation();
204 });
205
209
211
212 // -- Agent-specific data
213 // ... only stored optionally
214 if (not _store_agent_data) return;
215
216 _dset_agent_x->write(
217 agents.begin(), agents.end(),
218 [](const auto& agent) {
219 return static_cast<WriteT>(agent->position()[0]);
220 });
221
222 _dset_agent_y->write(
223 agents.begin(), agents.end(),
224 [](const auto& agent) {
225 return static_cast<WriteT>(agent->position()[1]);
226 });
227
229 orientations.begin(), orientations.end(),
230 [](const auto& orientation) {
231 return static_cast<WriteT>(orientation);
232 });
233 }

Member Data Documentation

◆ _adjust_orientation

const Rule Utopia::Models::SimpleFlocking::SimpleFlocking::_adjust_orientation

Rule that sets agent orientation to the mean orientation (in a radius)

The orientation of the current agent is included into the calculation of the mean orientation.

Currently uses a

298 {
299 auto state = agent->state();
300
301 // Find all agents within the interaction radius and compute their
302 // average orientation, including (!) the current agent.
303 // To find an average orientation, we need to separate the x- and y-
304 // components and later combine them back into an angle.
305 auto agg_sin = std::sin(agent->state().get_orientation());
306 auto agg_cos = std::cos(agent->state().get_orientation());
307
308 // NOTE The AgentManager::neighbors_of method finds neighbors with
309 // linear complexity in agent number, leading to an overall
310 // quadratic complexity in agent number for this search.
311 // This can be mitigated (on the level of the AgentManager!) by
312 // using a spatially restricted search or a lookup grid.
313 for (const auto& nb :
314 this->_am.neighbors_of(agent, this->_interaction_radius))
315 {
316 agg_sin += std::sin(nb->state().get_orientation());
317 agg_cos += std::cos(nb->state().get_orientation());
318 }
319
320 // Can now set the orientation, including noise
321 // NOTE Could divide by number of involved agents here, but that is
322 // unnecessary because it cancels out through division anyway.
323 state.set_orientation(
324 std::atan2(agg_sin, agg_cos) +
325 (this->_noise_level > 0. ? _noise_distr(*this->_rng) : 0.)
326 );
327
328 return state;
329 };
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition model.hh:161

◆ _am

AgentManager Utopia::Models::SimpleFlocking::SimpleFlocking::_am
private

The agent manager.

◆ _dset_agent_orientation

std::shared_ptr<DataSet> Utopia::Models::SimpleFlocking::SimpleFlocking::_dset_agent_orientation
private

◆ _dset_agent_x

std::shared_ptr<DataSet> Utopia::Models::SimpleFlocking::SimpleFlocking::_dset_agent_x
private

◆ _dset_agent_y

std::shared_ptr<DataSet> Utopia::Models::SimpleFlocking::SimpleFlocking::_dset_agent_y
private

◆ _dset_norm_group_velocity

std::shared_ptr<DataSet> Utopia::Models::SimpleFlocking::SimpleFlocking::_dset_norm_group_velocity
private

◆ _dset_orientation_circmean

std::shared_ptr<DataSet> Utopia::Models::SimpleFlocking::SimpleFlocking::_dset_orientation_circmean
private

◆ _dset_orientation_circstd

std::shared_ptr<DataSet> Utopia::Models::SimpleFlocking::SimpleFlocking::_dset_orientation_circstd
private

◆ _interaction_radius

double Utopia::Models::SimpleFlocking::SimpleFlocking::_interaction_radius
private

The radius within which agents interact with each other.

◆ _move

const VoidRule Utopia::Models::SimpleFlocking::SimpleFlocking::_move
Initial value:
= [this](const auto& agent){
this->_am.move_by(agent, agent->state().get_displacement());
}
void move_by(const std::shared_ptr< Agent > &agent, const SpaceVec &move_vec) const
Move an agent relative to its current position.
Definition agent_manager.hh:214

Rule that applies the current displacement vector to the agent position.

332 {
333 this->_am.move_by(agent, agent->state().get_displacement());
334 };

◆ _noise_distr

std::uniform_real_distribution<double> Utopia::Models::SimpleFlocking::SimpleFlocking::_noise_distr
private

The distribution used for determining the orientation noise.

◆ _noise_level

double Utopia::Models::SimpleFlocking::SimpleFlocking::_noise_level
private

The amplitude of the noise applied to the orientation update.

◆ _speed

double Utopia::Models::SimpleFlocking::SimpleFlocking::_speed
private

The global speed value, used as the uniform speed of all agents.

◆ _store_agent_data

bool Utopia::Models::SimpleFlocking::SimpleFlocking::_store_agent_data
private

Whether to store agent-specific data.


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