numerics 0.1.0
Loading...
Searching...
No Matches
ODE Examples

ODE routines cover fixed-step explicit schemes, adaptive RK45, symplectic second-order schemes, and implicit field updates.

Adaptive RK45

#include <numerics.hpp>
auto rhs = [](double, const num::Vector& y, num::Vector& dy) {
dy[0] = y[1];
dy[1] = -y[0];
};
auto result = num::solve(
num::ODEProblem{rhs, {1.0, 0.0}, 0.0, 20.0},
num::RK45{.h = 1e-2, .rtol = 1e-8, .atol = 1e-10});
ODEResult solve(const P &prob, const RK45 &alg, ObserverFn obs=nullptr)
Definition solve.hpp:32
constexpr real e
Definition math.hpp:44
Umbrella include for the numerics library.

result.u is the final state, result.t is the final time, and result.steps is the accepted step count.

Fixed-Step RK4 With Samples

auto steps = num::rk4(rhs, {1.0, 0.0}, {.t0 = 0.0, .tf = 10.0, .h = 1e-2});
for (const auto& s : steps) {
const double energy = 0.5 * (s.u[0] * s.u[0] + s.u[1] * s.u[1]);
(void)energy;
}
RK4Steps rk4(ODERhsFn f, Vector y0, ODEParams p={})
Definition ode.cpp:375

The range API is useful when every accepted step is part of the output.

Symplectic Verlet

auto accel = [](const num::Vector& q, num::Vector& a) {
a[0] = -q[0];
a[1] = -q[1];
};
num::Vector q0{1.0, 0.0};
num::Vector v0{0.0, 1.0};
auto orbit = num::ode_verlet(accel, q0, v0, {.t0 = 0.0, .tf = 100.0, .h = 1e-2});
SymplecticResult ode_verlet(AccelFn accel, Vector q0, Vector v0, ODEParams p={}, SympObserverFn obs=nullptr)
Velocity Verlet, 2nd-order symplectic, 1 force evaluation per step.
Definition ode.cpp:413

Use ode_yoshida4 when a fourth-order symplectic update is required.

Backward Euler Field Step

num::LinearSolver solver = [&](const num::Vector& rhs, num::Vector& x) {
return num::cg(num::operators::assume_spd(Aop), rhs, x, 1e-8, 1000).converged;
};
num::ode::advance(u, solver, {.nstep = nsteps, .dt = dt});
void advance(Field &u, const LinearSolver &solver, ImplicitParams p, Observer &&obs)
Definition implicit.hpp:31
SPDOp< Op > assume_spd(Op op)
std::function< SolverResult(const Vector &rhs, Vector &x)> LinearSolver
Callable that solves .
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Definition cg.cpp:8
bool converged
Whether tolerance was met.
Adapt a SparseMatrix to the operator protocol.
Definition sparse_op.hpp:15

This form separates the implicit update from the linear solver used at each step.