DiffEq - Modern C++ ODE Integration Library 1.0.0
High-performance C++ library for solving ODEs with async signal processing
Loading...
Searching...
No Matches
improved_euler.hpp
1#pragma once
2#include <core/concepts.hpp>
3#include <core/abstract_integrator.hpp>
4#include <core/state_creator.hpp>
5
6namespace diffeq {
7
17template<system_state S>
19public:
21 using state_type = typename base_type::state_type;
22 using time_type = typename base_type::time_type;
23 using value_type = typename base_type::value_type;
24 using system_function = typename base_type::system_function;
25
26 explicit ImprovedEulerIntegrator(system_function sys)
27 : base_type(std::move(sys)) {}
28
29 void step(state_type& state, time_type dt) override {
30 // Create temporary states
31 state_type k1 = StateCreator<state_type>::create(state);
32 state_type k2 = StateCreator<state_type>::create(state);
33 state_type temp_state = StateCreator<state_type>::create(state);
34
35 // k1 = f(t, y)
36 this->sys_(this->current_time_, state, k1);
37
38 // k2 = f(t + dt, y + dt*k1)
39 for (std::size_t i = 0; i < state.size(); ++i) {
40 auto state_it = state.begin();
41 auto k1_it = k1.begin();
42 auto temp_it = temp_state.begin();
43 temp_it[i] = state_it[i] + dt * k1_it[i];
44 }
45 this->sys_(this->current_time_ + dt, temp_state, k2);
46
47 // y_new = y + dt/2 * (k1 + k2)
48 for (std::size_t i = 0; i < state.size(); ++i) {
49 auto state_it = state.begin();
50 auto k1_it = k1.begin();
51 auto k2_it = k2.begin();
52 state_it[i] += dt * (k1_it[i] + k2_it[i]) / static_cast<time_type>(2);
53 }
54
55 this->advance_time(dt);
56 }
57};
58
59} // namespace diffeq
Improved Euler (Heun's method) integrator.