simulation.h Source File

CPP API: simulation.h Source File
memilio/compartments/simulation.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Jan Kleinert, Daniel Abele
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_H
21 #define MIO_COMPARTMENTS_SIMULATION_H
22 
28 
29 namespace mio
30 {
31 
33 template <typename FP>
35 
41 template <typename FP, IsCompartmentalModel<FP> M>
42 class Simulation : public details::SimulationBase<FP, M, OdeIntegrator<FP>>
43 {
44 public:
46  using Model = M;
47 
54  Simulation(Model const& model, FP t0 = 0., FP dt = 0.1)
55  : Base(model, std::make_unique<DefaultIntegratorCore<FP>>(), t0, dt)
56  {
57  }
58 
66  Eigen::Ref<Eigen::VectorX<FP>> advance(FP tmax)
67  {
68  return Base::advance(
69  [this](auto&& y, auto&& t, auto&& dydt) {
70  Base::get_model().eval_right_hand_side(y, y, t, dydt);
71  },
72  tmax, Base::get_result());
73  }
74 };
75 
81 template <class Simulation, typename FP>
82 concept IsCompartmentalModelSimulation = requires(Simulation simulation, FP t) {
83  requires IsCompartmentalModel<typename Simulation::Model, FP>;
84  simulation.advance(t);
85 };
86 
99 template <typename FP, class Model, class Sim = mio::Simulation<FP, Model>>
100 TimeSeries<FP> simulate(FP t0, FP tmax, FP dt, Model const& model,
101  std::unique_ptr<OdeIntegratorCore<FP>>&& integrator_core = nullptr)
102 {
103  model.check_constraints();
104  Sim sim(model, t0, dt);
105  if (integrator_core) {
106  sim.set_integrator_core(std::move(integrator_core));
107  }
108  sim.advance(tmax);
109  return sim.get_result();
110 }
111 
112 } // namespace mio
113 
114 #endif // MIO_COMPARTMENTS_SIMULATION_H
This is an adaptive IntegratorCore.
Definition: stepper_wrapper.h:59
Interface class defining the integration step used in a SystemIntegrator.
Definition: integrator.h:48
Eigen::Ref< Eigen::VectorX< FP > > advance(FP tmax)
Run the simulation up to a given time.
Definition: memilio/compartments/simulation.h:66
M Model
Definition: memilio/compartments/simulation.h:46
Simulation(Model const &model, FP t0=0., FP dt=0.1)
Setup the simulation with an ODE solver.
Definition: memilio/compartments/simulation.h:54
stores vectors of values at time points (or some other abstract variable) the value at each time poin...
Definition: time_series.h:58
The Model of the Simulation.
Definition: abm/model.h:54
Run the Simulation in discrete steps, evolve the Model and report results.
Definition: models/abm/simulation.h:37
void advance(TimePoint tmax, History &... history)
Run the Simulation from the current time to tmax.
Definition: models/abm/simulation.h:70
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
const Model & get_model() const
Get a reference to the model owned and used by the simulation.
Definition: simulation_base.h:131
TimeSeries< FP > & get_result()
Returns the simulation result describing the model population in each time step.
Definition: simulation_base.h:116
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
requires(!std::is_trivial_v< T >) void BinarySerializerObject
Definition: binary_serializer.h:333
concept IsCompartmentalModelSimulation
Concept to check if a type is a simulation for a compartmental model.
Definition: memilio/compartments/simulation.h:82
TimeSeries< FP > simulate(FP t0, FP tmax, FP dt, Model const &model, std::unique_ptr< OdeIntegratorCore< FP >> &&integrator_core=nullptr)
Run a Simulation of a CompartmentalModel.
Definition: memilio/compartments/simulation.h:100
Definition: io.h:94