euler_maruyama.h Source File

CPP API: euler_maruyama.h Source File
euler_maruyama.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Martin J. Kuehn, 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_MATH_EULER_MARUYAMA_H
21 #define MIO_MATH_EULER_MARUYAMA_H
22 
23 #include "memilio/math/eigen.h" // IWYU pragma: keep
26 #include "memilio/utils/logging.h"
27 
28 namespace mio
29 {
30 
35 template <typename FP>
37 {
38 public:
40  : SdeIntegratorCore<FP>(FP{}, FP{})
41  {
42  }
43 
44  std::unique_ptr<SdeIntegratorCore<FP>> clone() const override
45  {
46  return std::make_unique<EulerMaruyamaIntegratorCore>(*this);
47  }
48 
59  bool step(const DerivFunction<FP>& f, const DerivFunction<FP>& noise_f, Eigen::Ref<const Eigen::VectorX<FP>> yt,
60  FP& t, FP& dt, Eigen::Ref<Eigen::VectorX<FP>> ytp1) const override
61  {
62  using std::sqrt;
63 
64  f(yt, t, ytp1);
65 
66  Eigen::VectorX<FP> noise = Eigen::VectorX<FP>::Zero(yt.size());
67  noise_f(yt, t, noise);
68 
69  ytp1 = yt + dt * ytp1 + sqrt(dt) * noise;
70 
71  if (!map_to_nonnegative(ytp1)) [[unlikely]] {
72  mio::log_error("Failed to rescale values in Euler Maruyama step, total population is negative. "
73  "Redistributing absolute values evenly.");
74  }
75 
76  t += dt;
77  return true;
78  }
79 };
80 
81 } // namespace mio
82 
83 #endif // MIO_MATH_EULER_MARUYAMA_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
std::unique_ptr< SdeIntegratorCore< FP > > clone() const override
Definition: euler_maruyama.h:44
bool step(const DerivFunction< FP > &f, const DerivFunction< FP > &noise_f, Eigen::Ref< const Eigen::VectorX< FP >> yt, FP &t, FP &dt, Eigen::Ref< Eigen::VectorX< FP >> ytp1) const override
Fixed step width integrator for stochastic models.
Definition: euler_maruyama.h:59
EulerMaruyamaIntegratorCore()
Definition: euler_maruyama.h:39
Interface class defining the integration step used in a SystemIntegrator.
Definition: integrator.h:48
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_sqrt< AD_TAPE_REAL > > sqrt(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:1023
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100
IOResult< void > map_to_nonnegative(Eigen::Ref< Eigen::VectorX< FP >> x, const FP tolerance=Limits< FP >::zero_tolerance())
Map a vector onto nonnegative values while preserving its nonnegative sum.
Definition: math_utils.h:39
std::function< void(Eigen::Ref< const Eigen::VectorX< FP > > y, FP t, Eigen::Ref< Eigen::VectorX< FP > > dydt)> DerivFunction
Function template to be integrated.
Definition: integrator.h:39