simulation.h Source File

CPP API: simulation.h Source File
models/abm/simulation.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele, Khoa Nguyen
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 #ifndef MIO_ABM_SIMULATION_H
21 #define MIO_ABM_SIMULATION_H
22 
23 #include "abm/model.h"
24 #include "abm/time.h"
25 #include "memilio/io/history.h"
26 
27 namespace mio
28 {
29 namespace abm
30 {
31 
35 template <class M = Model>
37 {
38 public:
39  using Model = M;
40 
46  Simulation(TimePoint t0, Model&& model)
47  : m_model(std::move(model))
48  , m_t(t0)
49  , m_dt(hours(1))
50  {
51  }
52 
59  Simulation(TimePoint t0, size_t num_agegroups)
60  : Simulation(t0, M(num_agegroups))
61  {
62  }
63 
69  template <typename... History>
70  void advance(TimePoint tmax, History&... history)
71  {
72  //log initial system state
73  (history.log(*this), ...);
74  while (m_t < tmax) {
75  evolve_model(tmax);
76  (history.log(*this), ...);
77  }
78  }
79 
84  {
85  return m_t;
86  }
87 
92  {
93  return m_model;
94  }
95  const Model& get_model() const
96  {
97  return m_model;
98  }
99 
100 private:
103  {
104  auto dt = std::min(m_dt, tmax - m_t);
105  m_model.evolve(m_t, dt);
106  m_t += m_dt;
107  }
108 
112 };
113 
114 } // namespace abm
115 } // namespace mio
116 
117 #endif
History class that handles writers and loggers.
Definition: history.h:70
void log(const T &t)
Logs new records according to the Writer and Loggers.
Definition: history.h:96
Run the Simulation in discrete steps, evolve the Model and report results.
Definition: models/abm/simulation.h:37
M Model
Definition: models/abm/simulation.h:39
Model & get_model()
Get the Model that this Simulation evolves.
Definition: models/abm/simulation.h:91
const Model & get_model() const
Definition: models/abm/simulation.h:95
TimePoint m_t
The current TimePoint of the Simulation.
Definition: models/abm/simulation.h:110
TimeSpan m_dt
The length of the time steps.
Definition: models/abm/simulation.h:111
void store_result_at(TimePoint t)
void evolve_model(TimePoint tmax)
Definition: models/abm/simulation.h:102
Simulation(TimePoint t0, Model &&model)
Create a simulation.
Definition: models/abm/simulation.h:46
Simulation(TimePoint t0, size_t num_agegroups)
Create a Simulation with an empty Model.
Definition: models/abm/simulation.h:59
TimePoint get_time() const
Get the current time of the Simulation.
Definition: models/abm/simulation.h:83
void advance(TimePoint tmax, History &... history)
Run the Simulation from the current time to tmax.
Definition: models/abm/simulation.h:70
Model m_model
The Model to simulate.
Definition: models/abm/simulation.h:109
Represents a point in time.
Definition: time.h:175
A duration of time.
Definition: time.h:36
static min_max_return_type< ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > >::type min(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &a, const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &b)
Definition: ad.hpp:2599
TimeSpan hours(int hours)
Create a TimeSpan of a specified number of hours.
Definition: time.h:339
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Definition: io.h:94