DiffEq - Modern C++ ODE Integration Library 1.0.0
High-performance C++ library for solving ODEs with async signal processing
Loading...
Searching...
No Matches
euler_maruyama.hpp
1#pragma once
2
3#include <sde/sde_base.hpp>
4#include <core/state_creator.hpp>
5#include <cmath>
6
7namespace diffeq {
8
18template<system_state StateType>
20public:
22 using state_type = typename base_type::state_type;
23 using time_type = typename base_type::time_type;
24 using value_type = typename base_type::value_type;
25
26 explicit EulerMaruyamaIntegrator(std::shared_ptr<typename base_type::sde_problem_type> problem,
27 std::shared_ptr<typename base_type::wiener_process_type> wiener = nullptr)
28 : base_type(problem, wiener) {}
29
30 void step(state_type& state, time_type dt) override {
31 // Create temporary states
32 state_type drift_term = StateCreator<state_type>::create(state);
33 state_type diffusion_term = StateCreator<state_type>::create(state);
34 state_type dW = StateCreator<state_type>::create(state);
35
36 // Generate Wiener increments
37 this->wiener_->generate_increment(dW, dt);
38
39 // Compute drift: f(t, X)
40 this->problem_->drift(this->current_time_, state, drift_term);
41
42 // Compute diffusion: g(t, X)
43 this->problem_->diffusion(this->current_time_, state, diffusion_term);
44
45 // Apply noise to diffusion term
46 this->problem_->apply_noise(this->current_time_, state, diffusion_term, dW);
47
48 // Update state: X_{n+1} = X_n + f*dt + g*dW
49 for (size_t i = 0; i < state.size(); ++i) {
50 auto state_it = state.begin();
51 auto drift_it = drift_term.begin();
52 auto diffusion_it = diffusion_term.begin();
53
54 state_it[i] += drift_it[i] * dt + diffusion_it[i];
55 }
56
57 this->advance_time(dt);
58 }
59
60 std::string name() const override {
61 return "Euler-Maruyama";
62 }
63};
64
65} // namespace diffeq
Euler-Maruyama method for SDEs.
Abstract base class for SDE integrators.
Definition sde_base.hpp:147