3 #ifndef __ode_runge4_hpp
4 #define __ode_runge4_hpp
23 {
return step(t, dt, f, x, dx);}
33 {
return step(t, dt, f, x, dx, theta);}
40 virtual bool step(
const T &t,
58 template <
typename T,
typename X,
typename F>
class Runge4 :
public Solver<T, X, F>
63 virtual bool step(
const T &t,
65 const std::vector<F> &f,
77 template <
typename T,
typename X,
typename F>
87 dx = x + k1*dt*(T)0.5;
88 X k2 = f(t + dt*(T)0.5, dx);
90 dx = x + k2*dt*(T)0.5;
91 X k3 = f(t + dt*(T)0.5, dx);
96 dx = (k1 + (T)2*k2 + (T)2*k3 + k4)*dt/(T)6.;
103 template <
typename T,
typename X,
typename F>
114 dx = x + k1*dt*(T)0.5;
115 X k2 = f(t + dt*(T)0.5, dx);
117 dx = x + k2*dt*(T)0.5;
118 X k3 = f(t + dt*(T)0.5, dx);
121 X k4 = f(t + dt, dx);
123 theta = (k2 - k3)/(k1 - k2);
125 dx = (k1 + (T)2*k2 + (T)2*k3 + k4)*dt/(T)6.;
131 template <
typename T,
typename X,
typename F>
134 const std::vector<F> &f,
140 if (f.size() != x.size() || x.size() != dx.size())
142 printf (
"ode::Runge4::solve: error, different sizes of f(%zu), x(%zu), dx(%zu)\n",
143 f.size(), x.size(), dx.size());
147 std::vector<X> k1(f.size(), 0.);
148 for (
size_t i=0; i<f.size(); i++)
152 std::vector<X> s(f.size(), 0.);
153 for (
size_t i=0; i<f.size(); i++)
154 s[i] = x[i] + k1[i]*dt*(T)0.5;
157 std::vector<X> k2(f.size(), 0.);
158 for (
size_t i=0; i<f.size(); i++)
159 k2[i] = f[i](t + dt*(T)0.5, s);
162 for (
size_t i=0; i<f.size(); i++)
163 s[i] = x[i] + k2[i]*dt*(T)0.5;
166 std::vector<X> k3(f.size(), 0.);
167 for (
size_t i=0; i<f.size(); i++)
168 k3[i] = f[i](t + dt*(T)0.5, s);
171 for (
size_t i=0; i<f.size(); i++)
172 s[i] = x[i] + k3[i]*dt;
175 std::vector<X> k4(f.size(), 0.);
176 for (
size_t i=0; i<f.size(); i++)
177 k4[i] = f[i](t + dt, s);
180 for (
size_t i=0; i<f.size(); i++)
181 dx[i] = (k1[i] + (T)2*k2[i] + (T)2*k3[i] + k4[i])*dt/(T)6;
bool operator()(const T &t, const T &dt, F &f, X &x, X &dx, X &theta) const
Definition: ode_runge4.hpp:26
virtual bool step(const T &t, const T &dt, const std::vector< F > &f, std::vector< X > &x, std::vector< X > &dx) const
Definition: ode_runge4.hpp:132
Definition: ode_runge4.hpp:12
bool operator()(const T &t, const T &dt, F &f, X &x, X &dx) const
Definition: ode_runge4.hpp:17
Definition: ode_runge4.hpp:58
virtual bool step(const T &t, const T &dt, F &f, X &x, X &dx) const
Definition: ode_runge4.hpp:78