stochastic_simulation.h Source File

CPP API: stochastic_simulation.h Source File
stochastic_simulation.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: 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_STOCHASTIC_SIMULATION_H
21 #define MIO_COMPARTMENTS_STOCHASTIC_SIMULATION_H
22 
28 
29 namespace mio
30 {
31 
37 template <typename FP, IsStochasticModel<FP> M>
38 class StochasticSimulation : public details::SimulationBase<FP, M, SdeIntegrator<FP>>
39 {
40 public:
42  using Model = M;
43 
50  StochasticSimulation(Model const& model, FP t0 = 0., FP dt = 0.1)
51  : Base(model, std::make_unique<EulerMaruyamaIntegratorCore<FP>>(), t0, dt)
52  {
53  }
54 
62  Eigen::Ref<Eigen::VectorX<FP>> advance(FP tmax)
63  {
64  return Base::advance(
65  [this](auto&& y, auto&& t, auto&& dydt) {
66  Base::get_model().eval_right_hand_side(y, y, t, dydt);
67  },
68  [this](auto&& y, auto&& t, auto&& noise) {
69  noise.setZero();
70  Base::get_model().get_noise(y, y, t, noise);
71  },
72  tmax, Base::get_result());
73  }
74 };
75 
76 template <typename FP, class Model, class Sim = StochasticSimulation<FP, Model>>
77 TimeSeries<FP> simulate_stochastic(FP t0, FP tmax, FP dt, Model const& model,
78  std::unique_ptr<SdeIntegratorCore<FP>>&& integrator_core = nullptr)
79 {
80  model.check_constraints();
81  Sim sim(model, t0, dt);
82  if (integrator_core) {
83  sim.set_integrator_core(std::move(integrator_core));
84  }
85  sim.advance(tmax);
86  return sim.get_result();
87 }
88 
89 } // namespace mio
90 
91 #endif // MIO_COMPARTMENTS_STOCHASTIC_SIMULATION_H
Simple explicit integration y(t+1) = y(t) + h*f(t,y) + sqrt(h) * noise_f(t,y) for SDE systems.
Definition: euler_maruyama.h:37
Interface class defining the integration step used in a SystemIntegrator.
Definition: integrator.h:48
A class for simulating a StochasticModel.
Definition: stochastic_simulation.h:39
StochasticSimulation(Model const &model, FP t0=0., FP dt=0.1)
Setup the simulation with an SDE solver.
Definition: stochastic_simulation.h:50
Eigen::Ref< Eigen::VectorX< FP > > advance(FP tmax)
Run the simulation up to a given time.
Definition: stochastic_simulation.h:62
M Model
Definition: stochastic_simulation.h:42
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
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
TimeSeries< FP > simulate_stochastic(FP t0, FP tmax, FP dt, Model const &model, std::unique_ptr< SdeIntegratorCore< FP >> &&integrator_core=nullptr)
Definition: stochastic_simulation.h:77
Definition: io.h:94