simulation_base.h Source File

CPP API: simulation_base.h Source File
simulation_base.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Jan Kleinert, Daniel Abele, Rene Schmieding
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_COMPARTMENTS_SIMULATION_BASE_H
21 #define MIO_COMPARTMENTS_SIMULATION_BASE_H
22 
26 
27 namespace mio
28 {
29 namespace details
30 {
31 
40 template <typename FP, IsCompartmentalModel<FP> M, class... Integrands>
42 {
43 
44 public:
45  using Model = M;
46  using Core = IntegratorCore<FP, Integrands...>;
47 
55  SimulationBase(Model const& model, std::unique_ptr<Core>&& integrator_core, FP t0, FP dt)
56  : m_model(std::make_unique<Model>(model))
57  , m_integrator(std::move(integrator_core))
58  , m_result(t0, m_model->get_initial_values())
59  , m_dt(dt)
60  {
61  }
62 
64  : m_model(std::make_unique<Model>(*other.m_model))
65  , m_integrator(other.m_integrator)
66  , m_result(other.m_result)
67  , m_dt(other.m_dt)
68  {
69  }
70 
72  {
73  if (this != &other) {
74  m_model = std::make_unique<Model>(*other.m_model);
75  m_integrator = other.m_integrator;
76  m_result = other.m_result;
77  m_dt = other.m_dt;
78  }
79  return *this;
80  }
81 
86  void set_integrator_core(std::unique_ptr<Core>&& integrator_core)
87  {
88  m_integrator.set_integrator_core(std::move(integrator_core));
89  }
90 
97  {
99  }
100  const Core& get_integrator_core() const
101  {
103  }
117  {
118  return m_result;
119  }
120  const TimeSeries<FP>& get_result() const
121  {
122  return m_result;
123  }
131  const Model& get_model() const
132  {
133  return *m_model;
134  }
136  {
137  return *m_model;
138  }
147  FP& get_dt()
148  {
149  return m_dt;
150  }
151  const FP& get_dt() const
152  {
153  return m_dt;
154  }
157 protected:
165  Eigen::Ref<Eigen::VectorX<FP>> advance(const Integrands&... fs, FP tmax, TimeSeries<FP>& results)
166  {
167  return m_integrator.advance(fs..., tmax, m_dt, results);
168  }
169 
170 private:
171  std::unique_ptr<Model> m_model;
172  SystemIntegrator<FP, Integrands...>
175  FP m_dt;
176 };
177 
179 template <typename FP, class M, class... Integrands>
180 class SimulationBase<FP, M, SystemIntegrator<FP, Integrands...>> : public SimulationBase<FP, M, Integrands...>
181 {
182  using SimulationBase<FP, M, Integrands...>::SimulationBase;
183 };
184 
185 } // namespace details
186 } // namespace mio
187 
188 #endif // MIO_COMPARTMENTS_SIMULATION_BASE_H
Interface class defining the integration step used in a SystemIntegrator.
Definition: integrator.h:48
Integrate a system of equations over time.
Definition: integrator.h:147
Eigen::Ref< Eigen::VectorX< FP > > advance(const Integrands &... fs, const FP tmax, FP &dt, TimeSeries< FP > &results)
Advance the integrator.
Definition: integrator.h:184
Core & get_integrator_core()
Access the IntegratorCore used for integration.
Definition: integrator.h:266
void set_integrator_core(std::unique_ptr< Core > &&core)
Change the IntegratorCore used for integration.
Definition: integrator.h:255
stores vectors of values at time points (or some other abstract variable) the value at each time poin...
Definition: time_series.h:58
Base class to define a Simulation.
Definition: simulation_base.h:42
Eigen::Ref< Eigen::VectorX< FP > > advance(const Integrands &... fs, FP tmax, TimeSeries< FP > &results)
Run the simulation up to a given time.
Definition: simulation_base.h:165
FP & get_dt()
Returns the step size used by the integrator.
Definition: simulation_base.h:147
SimulationBase(const SimulationBase &other)
Definition: simulation_base.h:63
const Model & get_model() const
Get a reference to the model owned and used by the simulation.
Definition: simulation_base.h:131
void set_integrator_core(std::unique_ptr< Core > &&integrator_core)
Set the IntegratorCore used in the simulation.
Definition: simulation_base.h:86
TimeSeries< FP > m_result
The simulation results.
Definition: simulation_base.h:174
Model & get_model()
Get a reference to the model owned and used by the simulation.
Definition: simulation_base.h:135
FP m_dt
The time step used (and possibly set) by m_integrator::m_core::step.
Definition: simulation_base.h:175
std::unique_ptr< Model > m_model
The model defining the ODE system and initial conditions.
Definition: simulation_base.h:171
const TimeSeries< FP > & get_result() const
Returns the simulation result describing the model population in each time step.
Definition: simulation_base.h:120
M Model
Definition: simulation_base.h:45
TimeSeries< FP > & get_result()
Returns the simulation result describing the model population in each time step.
Definition: simulation_base.h:116
const FP & get_dt() const
Returns the step size used by the integrator.
Definition: simulation_base.h:151
Core & get_integrator_core()
Access the IntegratorCore used in the simulation.
Definition: simulation_base.h:96
SimulationBase(Model const &model, std::unique_ptr< Core > &&integrator_core, FP t0, FP dt)
Create a SimulationBase.
Definition: simulation_base.h:55
SystemIntegrator< FP, Integrands... > m_integrator
Integrates the DerivFunction (see advance) and stores resutls in m_result.
Definition: simulation_base.h:173
SimulationBase & operator=(const SimulationBase &other)
Definition: simulation_base.h:71
const Core & get_integrator_core() const
Access the IntegratorCore used in the simulation.
Definition: simulation_base.h:100
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
Definition: io.h:94