05_differential_equations.cpp¶
Integrate an ordinary differential equation, and compare how well methods conserve energy.
ode_rk4 and ode_rk45 integrate y' = f(t, y). ode_verlet and ode_yoshida4 integrate q'' = a(q), and because they are symplectic their energy error stays bounded over long times instead of drifting. The oscillator q'' = -q has energy (q^2 + v^2)/2 = 1/2 and solution q(t) = cos t.
#include <cmath>
#include <io/json.hpp>
#include <numerics.hpp>
#include <string_view>
#include <vector>
using namespace num;
int main(int argc, char **argv) {
const bool plot = argc > 1 && std::string_view(argv[1]) == "--plot";
constexpr real tf = 200.0;
auto energy = [](real q, real v) { return 0.5 * ((q * q) + (v * v)); };
const auto rhs = [](real, const vec<real> &y, vec<real> &dy) {
dy[0] = y[1];
dy[1] = -y[0];
};
const auto acceleration = [](const vec<real> &q, vec<real> &a) { a[0] = -q[0]; };
const ode_params fixed{.t0 = 0.0, .tf = tf, .h = 0.1};
const ode_result rk4 = ode_rk4(rhs, vec<real>{1.0, 0.0}, fixed);
const ode_result rk45 = ode_rk45(rhs, vec<real>{1.0, 0.0},
{.t0 = 0.0, .tf = tf, .h = 0.1, .rtol = 1e-8, .atol = 1e-10});
const symplectic_result verlet = ode_verlet(acceleration, vec<real>{1.0}, vec<real>{0.0}, fixed);
const symplectic_result yoshida =
ode_yoshida4(acceleration, vec<real>{1.0}, vec<real>{0.0}, fixed);
io::write_json({
{"rk4", {{"position", rk4.u[0]}, {"position_error", std::abs(rk4.u[0] - std::cos(tf))},
{"energy_error", energy(rk4.u[0], rk4.u[1]) - 0.5}, {"steps", rk4.steps}}},
{"rk45", {{"position", rk45.u[0]}, {"position_error", std::abs(rk45.u[0] - std::cos(tf))},
{"energy_error", energy(rk45.u[0], rk45.u[1]) - 0.5}, {"steps", rk45.steps}}},
{"verlet", {{"position", verlet.q[0]}, {"position_error", std::abs(verlet.q[0] - std::cos(tf))},
{"energy_error", energy(verlet.q[0], verlet.v[0]) - 0.5}, {"steps", verlet.steps}}},
{"yoshida4", {{"position", yoshida.q[0]}, {"position_error", std::abs(yoshida.q[0] - std::cos(tf))},
{"energy_error", energy(yoshida.q[0], yoshida.v[0]) - 0.5}, {"steps", yoshida.steps}}}
}, "result.json");
if (plot) {
std::vector<double> t, q, v;
for (const auto &step : yoshida4(acceleration, vec<real>{1.0}, vec<real>{0.0},
ode_params{.t0 = 0.0, .tf = 20.0, .h = 0.05})) {
t.push_back(step.t);
q.push_back(step.q[0]);
v.push_back(step.v[0]);
}
plt::plot(t, q, "q(t)", "lines");
plt::plot(t, v, "v(t)", "lines");
plt::title("05 Yoshida 4th-order oscillator");
plt::legend();
plt::savefig("plot.png");
}
}
Saved result:
{
"rk4": {
"energy_error": -1.3871335457438239e-05,
"position": 0.48703588224934546,
"position_error": 0.00015179275766041833,
"steps": 2001
},
"rk45": {
"energy_error": -1.974661095749397e-07,
"position": 0.4871876041294068,
"position_error": 7.087759906765356e-08,
"steps": 2631
},
"verlet": {
"energy_error": -0.0008604245351273576,
"position": 0.5582655030590732,
"position_error": 0.0710778280520673,
"steps": 2001
},
"yoshida4": {
"energy_error": -2.9267364665686024e-06,
"position": 0.4860300688916958,
"position_error": 0.0011576061153100725,
"steps": 2001
}
}
