Utopia  2
Framework for studying models of complex & adaptive systems.
dummy.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_MODELS_DUMMY_HH
2 #define UTOPIA_MODELS_DUMMY_HH
3 
4 #include <utopia/core/model.hh>
5 
6 
7 namespace Utopia {
8 namespace Models {
9 namespace Dummy {
10 
13 
15 
18 class Dummy : public Model<Dummy, DummyTypes>
19 {
20 public:
23 
25  using Data = std::vector<double>;
26 
27  // Type shortcut for dataset
29 
30 private:
33 
36 
38  std::shared_ptr<DataSet> _dset_state;
39 
40 public:
42 
49  template <class ParentModel>
51  const std::string& name,
52  const ParentModel& parent_model,
53  const Data& initial_state,
54  const DataIO::Config& custom_cfg = {}
55  )
56  :
57  // Use the base constructor for the main parts
58  Base(name, parent_model, custom_cfg),
59 
60  // Initialise state and boundary condition members
61  _state(initial_state),
62  _bc(_state.size(), 1.0),
63  _dset_state(this->create_dset("state", {_state.size()}))
64  {}
65 
66 
68 
73  void perform_step()
74  {
75  // Write some random numbers into the state vector
76  auto gen = std::bind(std::uniform_real_distribution<>(), *this->_rng);
77  std::generate(_bc.begin(), _bc.end(), gen);
78  std::transform(_state.begin(), _state.end(),
79  _bc.begin(), _state.begin(),
80  [](const auto a, const auto b) { return a + b; });
81  }
82 
83 
85 
88  void monitor ()
89  {
90  // Supply the state mean to the monitor
91  _monitor.set_by_func("state_mean", [this](){
92  const double sum = std::accumulate(this->_state.begin(),
93  this->_state.end(),
94  0);
95  return sum / this->_state.size();
96  });
97  }
98 
99 
101  void write_data()
102  {
103  _dset_state->write(_state.begin(), _state.end(),
104  [](auto& value) { return value; });
105  }
106 
107 
108  // -- Getters and Setters -- //
109 
110 };
111 
112 } // namespace Dummy
113 } // namespace Models
114 } // namespace Utopia
115 
116 #endif // UTOPIA_MODELS_DUMMY_HH
Base class interface for Models using the CRT Pattern.
Definition: model.hh:112
Monitor _monitor
The monitor.
Definition: model.hh:188
typename ModelTypes::DataSet DataSet
Data type that is used for storing data.
Definition: model.hh:125
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
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition: model.hh:161
Dummy model with simple update rule.
Definition: dummy.hh:19
std::vector< double > Data
The data type to use for _state and _bc members.
Definition: dummy.hh:25
Data _state
The current state of the model.
Definition: dummy.hh:32
std::shared_ptr< DataSet > _dset_state
Dataset to write the state to.
Definition: dummy.hh:38
void perform_step()
Iterate by one time step.
Definition: dummy.hh:73
void monitor()
Monitor model information.
Definition: dummy.hh:88
Base::DataSet DataSet
Definition: dummy.hh:28
Data _bc
The boundary conditions of the model.
Definition: dummy.hh:35
void write_data()
Write data into a dataset that corresponds to the current step.
Definition: dummy.hh:101
Model< Dummy, DummyTypes > Base
The base model class.
Definition: dummy.hh:22
Dummy(const std::string &name, const ParentModel &parent_model, const Data &initial_state, const DataIO::Config &custom_cfg={})
Construct the dummy model with an initial state.
Definition: dummy.hh:50
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
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
Definition: agent.hh:11
Wrapper struct for defining model class data types.
Definition: model.hh:92