common_abm_loggers.h Source File

CPP API: common_abm_loggers.h Source File
common_abm_loggers.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Sascha Korf
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 
21 #ifndef ABM_COMMON_LOGGERS_H
22 #define ABM_COMMON_LOGGERS_H
23 
24 #include "abm/infection_state.h"
25 #include "abm/person_id.h"
26 #include "abm/simulation.h"
27 #include "memilio/io/history.h"
30 #include "abm/mobility_data.h"
31 #include "memilio/utils/mioomp.h"
32 
33 namespace mio
34 {
35 namespace abm
36 {
37 
43 struct mobility_data {
44  uint32_t agent_id;
45  uint32_t from_id;
46  uint32_t to_id;
52 };
53 
55 {
56  switch (current_location) {
73  default:
75  }
76 }
77 
82  using Type = std::vector<
83  std::tuple<mio::abm::LocationId, mio::abm::LocationType, mio::geo::GeographicalLocation, size_t, int>>;
94  static Type log(const mio::abm::Simulation<>& sim)
95  {
96  Type location_information{};
97  for (auto& location : sim.get_model().get_locations()) {
98  auto n_cells = location.get_cells().size();
99  int loc_capacity = 0;
100  for (int i = 0; i < (int)n_cells; i++) {
101  loc_capacity += location.get_capacity(i).persons;
102  }
103  location_information.push_back(std::make_tuple(
104  location.get_id(), location.get_type(), location.get_geographical_location(), n_cells, loc_capacity));
105  }
106  return location_information;
107  }
108 };
109 
114  using Type = std::vector<std::tuple<mio::abm::PersonId, mio::abm::LocationId, mio::AgeGroup>>;
123  static Type log(const mio::abm::Simulation<>& sim)
124  {
125  Type person_information{};
126  person_information.reserve(sim.get_model().get_persons().size());
127  for (auto& person : sim.get_model().get_persons()) {
128  person_information.push_back(std::make_tuple(
129  person.get_id(), sim.get_model().find_location(mio::abm::LocationType::Home, person.get_id()),
130  person.get_age()));
131  }
132  return person_information;
133  }
134 };
135 
153  static Type log(const mio::abm::Simulation<>& sim)
154  {
156  for (Person p : sim.get_model().get_persons()) {
157  mobility_data.push_back(
158  std::make_tuple(p.get_id(), p.get_location(), sim.get_time(), p.get_last_transport_mode(),
159  guess_activity_type(p.get_location_type()), p.get_infection_state(sim.get_time())));
160  }
161  return mobility_data;
162  }
163 };
164 
169  using Type = std::pair<mio::abm::TimePoint, Eigen::VectorX<ScalarType>>;
175  static Type log(const mio::abm::Simulation<>& sim)
176  {
177 
178  Eigen::VectorX<ScalarType> sum =
179  Eigen::VectorX<ScalarType>::Zero(Eigen::Index(mio::abm::InfectionState::Count));
180  auto curr_time = sim.get_time();
181  PRAGMA_OMP(for)
182  for (auto& location : sim.get_model().get_locations()) {
183  for (uint32_t inf_state = 0; inf_state < (int)mio::abm::InfectionState::Count; inf_state++) {
184  sum[inf_state] += sim.get_model().get_subpopulation(location.get_id(), curr_time,
185  mio::abm::InfectionState(inf_state));
186  }
187  }
188  return std::make_pair(curr_time, sum);
189  }
190 };
191 
196 template <class... Loggers>
198  using Data = std::tuple<mio::TimeSeries<ScalarType>>;
199  template <class Logger>
205  static void add_record(const typename Logger::Type& t, Data& data)
206  {
207  std::get<index_of_type_v<Logger, Loggers...>>(data).add_time_point(t.first.days(), t.second);
208  }
209 };
210 
216 template <class... Loggers>
218  using Data = std::tuple<std::vector<typename Loggers::Type>...>;
219  template <class Logger>
226  static void add_record(const typename Logger::Type& t, Data& data)
227  {
228 
229  if (std::get<index_of_type_v<Logger, Loggers...>>(data).size() > 0) {
230  typename Logger::Type diff_vector{};
231  auto& current_state_vec = std::get<index_of_type_v<Logger, Loggers...>>(data).front();
232  for (auto i = 0; i < (int)current_state_vec.size(); i++) {
233  if (std::get<1>(t[i]) != std::get<1>(current_state_vec[i])) {
234  std::get<1>(current_state_vec[i]) = std::get<1>(t[i]);
235  diff_vector.push_back(t[i]);
236  }
237  }
238  std::get<index_of_type_v<Logger, Loggers...>>(data).push_back(diff_vector);
239  }
240  else {
241  std::get<index_of_type_v<Logger, Loggers...>>(data).push_back(
242  t); // We use the first entry as a reference for the current position.
243  std::get<index_of_type_v<Logger, Loggers...>>(data).push_back(t);
244  }
245  }
246 };
247 
248 } // namespace abm
249 } // namespace mio
250 #endif //ABM_COMMON_LOGGERS_H
Agents in the simulated Model that can carry and spread the Infection.
Definition: person.h:51
Run the Simulation in discrete steps, evolve the Model and report results.
Definition: models/abm/simulation.h:37
Model & get_model()
Get the Model that this Simulation evolves.
Definition: models/abm/simulation.h:91
TimePoint get_time() const
Get the current time of the Simulation.
Definition: models/abm/simulation.h:83
Represents a point in time.
Definition: time.h:175
#define PRAGMA_OMP(x)
Macro for OpenMP directives.
Definition: mioomp.h:43
TransportMode
Mode of Transport.
Definition: mobility_data.h:35
LocationType
Type of a Location.
Definition: location_type.h:34
ActivityType
Type of the activity.
Definition: mobility_data.h:50
InfectionState
InfectionState in ABM.
Definition: abm/infection_state.h:35
constexpr mio::abm::ActivityType guess_activity_type(mio::abm::LocationType current_location)
Definition: common_abm_loggers.h:54
int size(Comm comm)
Return the size of the given communicator.
Definition: miompi.cpp:75
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
constexpr std::size_t index_of_type_v
The index of Type in the list Types.
Definition: metaprogramming.h:191
auto i
Definition: io.h:809
constexpr std::tuple_element< I, std::tuple< Index< CategoryTags >... > >::type & get(Index< CategoryTags... > &i) noexcept
Retrieves the Index (by reference) at the Ith position of a MultiIndex.
Definition: index.h:294
Definition: history.h:154
LogOnce and LogAlways can be used as a base class to write a logger for History.
Definition: history.h:143
This class writes data retrieved from loggers to memory.
Definition: common_abm_loggers.h:217
static void add_record(const typename Logger::Type &t, Data &data)
This function adds an entry to the data vector.
Definition: common_abm_loggers.h:226
std::tuple< std::vector< typename Loggers::Type >... > Data
Definition: common_abm_loggers.h:218
Unique identifier for a Location within a Model.
Definition: location_id.h:34
Logger to log mobility data of the agents in the simulation.
Definition: common_abm_loggers.h:139
std::vector< std::tuple< mio::abm::PersonId, mio::abm::LocationId, mio::abm::TimePoint, mio::abm::TransportMode, mio::abm::ActivityType, mio::abm::InfectionState > > Type
Definition: common_abm_loggers.h:141
static Type log(const mio::abm::Simulation<> &sim)
Log the mobility data of the agents in the simulation.
Definition: common_abm_loggers.h:153
Logger to log the TimeSeries of the number of Persons in an InfectionState.
Definition: common_abm_loggers.h:168
std::pair< mio::abm::TimePoint, Eigen::VectorX< ScalarType > > Type
Definition: common_abm_loggers.h:169
static Type log(const mio::abm::Simulation<> &sim)
Log the TimeSeries of the number of Persons in an InfectionState.
Definition: common_abm_loggers.h:175
Logger to log the LocationInformation of the simulation.
Definition: common_abm_loggers.h:81
std::vector< std::tuple< mio::abm::LocationId, mio::abm::LocationType, mio::geo::GeographicalLocation, size_t, int > > Type
Definition: common_abm_loggers.h:83
static Type log(const mio::abm::Simulation<> &sim)
Log the LocationInformation of the simulation.
Definition: common_abm_loggers.h:94
Logger to log the Persons Information in the simulation.
Definition: common_abm_loggers.h:113
std::vector< std::tuple< mio::abm::PersonId, mio::abm::LocationId, mio::AgeGroup > > Type
Definition: common_abm_loggers.h:114
static Type log(const mio::abm::Simulation<> &sim)
Log the LocationInformation of the simulation.
Definition: common_abm_loggers.h:123
Unique ID for a Person within a Model.
Definition: person_id.h:33
This is like the DataWriterToMemory, but it only logs time series data.
Definition: common_abm_loggers.h:197
static void add_record(const typename Logger::Type &t, Data &data)
This function adds an entry to the TimeSeries consisting of the TimePoint and the value.
Definition: common_abm_loggers.h:205
std::tuple< mio::TimeSeries< ScalarType > > Data
Definition: common_abm_loggers.h:198
Struct to save specific mobility data of an agent.
Definition: common_abm_loggers.h:43
mio::abm::TimePoint end_time
Definition: common_abm_loggers.h:48
mio::abm::TimePoint start_time
Definition: common_abm_loggers.h:47
uint32_t agent_id
Definition: common_abm_loggers.h:44
uint32_t to_id
Definition: common_abm_loggers.h:46
mio::abm::InfectionState infection_state
Definition: common_abm_loggers.h:51
uint32_t from_id
Definition: common_abm_loggers.h:45
mio::abm::TransportMode transport_mode
Definition: common_abm_loggers.h:49
mio::abm::ActivityType activity_type
Definition: common_abm_loggers.h:50