ode
On this page
Ordinary differential equations on core's Array: adaptive initial
value solvers (explicit Runge-Kutta, Radau IIA, BDF, automatic stiff
switching) with dense output and events, fixed-step and symplectic
integrators, and boundary value problems by collocation.
Modules
opsFlows: one-step maps phi_h composed the way splitting and composition methods are written (Hairer, Lubich and Wanner, Geometric Numerical Integration, II.4-5), behind theopsfeature: a ∘ b a after b (core's∘) c * a the same map with step c h a ** n n steps of a a ⊳ y a applied to the state y (binds like*) so Strang splitting is(0.5 * a) ∘ b ∘ (0.5 * a)and a step of Yoshida's method(w1 * v) ∘ (w0 * v) ∘ (w1 * v).problemsStandard test problems with their exact Jacobians and reference values: the stiff ones from Hairer and Wanner (Solving ODEs II, IV.10) and the IVP test set of Mazzia and Magherini, plus Lorenz, the Kepler problem and an N-body cluster for the non-stiff and symplectic integrators.
Structs
BvpSource
pub struct BvpThe result of solve_bvp.
sol is the C1 cubic spline (sol.at(x)), x the final mesh, y
and yp the solution and its derivative there ([n, x.len()]), p
the fitted parameters, rms_residuals the relative residual per
interval.
use core::array::{Array, Matrix, Vector};
// y'' = -y, y(0) = 0, y(pi / 2) = 1: y = sin x.
let x: Vector<f64> = [0.0, 0.5, 1.0, 1.5707963267948966];
let y: Matrix<f64> = Array::zeros([2, 4]);
let r = try ode::solve_bvp(|x: f64, y: Vector<f64>| -> Vector<f64> { [y[1], -y[0]] }, |a: Vector<f64>, b: Vector<f64>| -> Vector<f64> { [a[0], b[0] - 1.0] }, x, y, tol: 1e-6);
assert(r.success() && (r.sol.at(1.0)[0] - 1.0f64.sin()).abs() < 1e-6);
The spline solution.
The final mesh.
The solution at the mesh,
[n, m].f(x, y, p)at the mesh,[n, m].The parameters (empty without any).
RMS relative residual per interval.
Newton / refinement rounds.
How it ended.
Evaluations of
f.Whether the tolerances were met (
status.success()).nabiuse core::array::{Array, Vector}; let x: Vector<f64> = [0.0, 1.0]; let r = try ode::solve_bvp(|x: f64, y: Vector<f64>| -> Vector<f64> { [y[1], 0.0] }, |a: Vector<f64>, b: Vector<f64>| -> Vector<f64> { [a[0], b[0] - 2.0] }, x, Array::zeros([2, 2])); assert(r.success() && (r.sol.at(0.25)[0] - 0.5).abs() < 1e-12);
DenseSource
pub struct DenseThe continuous solution of an initial value problem: sol(t) (or
sol.at(t)) reads it anywhere on the span, between steps too, and
sol(ts) at every time of a vector, to the accuracy of the
method's own interpolant (order 3 for RK23, 4 for RK45, 7 for
DOP853, 3 for Radau, the current order for BDF). Outside the span it
extends the first or last step's polynomial. Each lookup is a binary
search over the steps, O(log steps + n).
use core::array::Vector;
let y0: Vector<f64> = [1.0];
let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { -y }, 0.0..2.0, y0, rtol: 1e-8, atol: 1e-10);
assert(((r.sol)(0.7)[0] - (-0.7f64).exp()).abs() < 1e-7);
let sol = r.sol;
assert(sol([0.5, 0.7])[0, 1] == sol(0.7)[0]);
assert(sol.span().start == 0.0 && sol.span().end == 2.0);
The solution at
t.nabiuse core::array::Vector; let y0: Vector<f64> = [0.0, 1.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { [y[1], -y[0]] }, 0.0..3.0, y0, method: ode::Method::Dop853, rtol: 1e-10, atol: 1e-12); let y = r.sol.at(1.0); assert((y[0] - 1.0f64.sin()).abs() < 1e-9 && (y[1] - 1.0f64.cos()).abs() < 1e-9);The solution at every time of
ts, as columns: shape[n, ts.len()], likeSolution.y.nabiuse core::array::Vector; let y0: Vector<f64> = [1.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 0.0..1.0, y0, rtol: 1e-9, atol: 1e-12); let ys = r.sol.eval([0.25, 0.5, 1.0]); assert(ys.shape() == [1, 3] && (ys[0, 1] - 0.5f64.exp()).abs() < 1e-8);Component
ias a function of time, for quadrature or root finding:sol.component(0)(t) == sol.at(t)[0].nabiuse core::array::Vector; let y0: Vector<f64> = [0.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { [2.0 * t] }, 0.0..1.0, y0); let g = r.sol.component(0); assert((g(0.5) - 0.25).abs() < 1e-12);An
ipast the last component gives NaN everywhere.The time span covered, from the start to the last step's end.
nabiuse core::array::Vector; let y0: Vector<f64> = [1.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 0.0..1.0, y0); assert(r.sol.span().start == 0.0 && r.sol.span().end == 1.0);The number of steps (interpolants).
nabiuse core::array::Vector; let y0: Vector<f64> = [1.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 0.0..1.0, y0); assert(r.sol.steps() == r.steps);The step ends, from the start:
steps() + 1times.nabiuse core::array::Vector; let y0: Vector<f64> = [1.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 0.0..1.0, y0); assert(r.sol.breakpoints() == r.t);
Trait implementations 2
impl ops::Call<f64> for Denseimpl ops::Call<Vector<f64>> for Dense
EventSource
pub struct EventA function of (t, y) whose zeros solve_ivp locates. A
terminal event stops the integration at its first zero.
use ode::{Direction, Event};
use core::array::Vector;
// A ball thrown up at 10 m/s lands after 2 v / g.
let ground = Event { f: |t: f64, y: Vector<f64>| -> f64 { y[0] }, terminal: true, direction: Direction::Falling };
let y0: Vector<f64> = [0.0, 10.0];
let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { [y[1], -9.81] }, 0.0..5.0, y0, events: [ground]);
assert(r.status == ode::Status::Event);
assert((r.t_events[0][0] - 20.0 / 9.81).abs() < 1e-10);
The event function; an event is a zero of it.
Stop at the first zero.
Which crossings count.
SolutionSource
pub struct SolutionThe result of solve_ivp.
t and y hold every step end (or the t_eval times), the state as
columns: y[i, j] is component i at t[j]. sol is the dense
output. t_events[k] and y_events[k] hold the zeros of event k
(y_events[k] is hits x n).
use core::array::Vector;
let y0: Vector<f64> = [2.0];
let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y .* -0.5 }, 0.0..10.0, y0, t_eval: [0.0, 5.0, 10.0]);
assert(r.status.success() && r.t.len() == 3 && r.y.shape() == [1, 3]);
assert((r.y[0, 2] - 2.0 * (-5.0f64).exp()).abs() < 1e-3);
assert(r.nfev > 0 && r.njev == 0);
Output times.
The state at
t, shape[n, t.len()].Why the run stopped.
Evaluations of the right-hand side, finite-difference Jacobians included.
Jacobian evaluations.
LU factorizations.
Accepted steps.
The dense output (empty with
dense: false).The times of each event's zeros.
The states at each event's zeros,
hits x n.Method::Autoonly: the times it changed method, the first to the stiff one, alternating after.Whether the run ended as asked (
status.success()).nabiuse core::array::Vector; let y0: Vector<f64> = [1.0]; assert((try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 0.0..1.0, y0)).success());The state at the last output time.
nabiuse core::array::Vector; let y0: Vector<f64> = [1.0, 0.0]; let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { [0.0, 1.0] }, 0.0..2.0, y0); let y = r.last(); assert(y[0] == 1.0 && (y[1] - 2.0).abs() < 1e-14);
OrbitSource
pub struct OrbitA trajectory from symplectic: positions q and momenta p as
columns ([d, t.len()]) at the saved times t.
use core::array::Vector;
let q0: Vector<f64> = [1.0];
let v0: Vector<f64> = [0.0];
let o = try ode::verlet(|q: Vector<f64>| -> Vector<f64> { -q }, q0, v0, 0.0..10.0, 1000);
let e = o.energy(|q: Vector<f64>, p: Vector<f64>| -> f64 { 0.5 * (q[0] * q[0] + p[0] * p[0]) });
assert(o.q.shape() == [1, 1001] && (e[1000] - 0.5).abs() < 1e-5);
Saved times.
Positions,
[d, t.len()].Momenta (velocities for
verletand friends),[d, t.len()].Force evaluations.
h(q, p)at every saved time: the energy for a Hamiltonianh.nabiuse core::array::Vector; let q0: Vector<f64> = [1.0]; let v0: Vector<f64> = [0.0]; let o = try ode::verlet(|q: Vector<f64>| -> Vector<f64> { -q }, q0, v0, 0.0..1.0, 100); let e = o.energy(|q: Vector<f64>, p: Vector<f64>| -> f64 { 0.5 * (q[0] * q[0] + p[0] * p[0]) }); assert(e.len() == 101 && e[0] == 0.5);The final positions.
nabiuse core::array::Vector; let q0: Vector<f64> = [1.0]; let v0: Vector<f64> = [0.0]; let o = try ode::verlet(|q: Vector<f64>| -> Vector<f64> { -q }, q0, v0, 0.0..1.0, 100); assert((o.last_q()[0] - 1.0f64.cos()).abs() < 1e-4);The final momenta.
nabiuse core::array::Vector; let q0: Vector<f64> = [1.0]; let v0: Vector<f64> = [0.0]; let o = try ode::verlet(|q: Vector<f64>| -> Vector<f64> { -q }, q0, v0, 0.0..1.0, 100); assert((o.last_p()[0] + 1.0f64.sin()).abs() < 1e-4);
Enums
BvpStatusSource
pub enum BvpStatusHow solve_bvp ended.
use ode::BvpStatus;
assert(BvpStatus::Converged.success() && !BvpStatus::MaxNodes.success());
assert(f"{BvpStatus::MaxNodes}".contains("max_nodes"));
The residuals and boundary conditions meet their tolerances.
Refining the mesh further would pass
max_nodes.The Newton system was singular.
The residual is small but the boundary conditions miss
bc_tolafter 10 iterations.Whether the tolerances were met.
nabiassert(ode::BvpStatus::Converged.success());
Trait implementations 1
impl Display for BvpStatus
OdeErrorSource
pub enum OdeErrorWhat went wrong setting up a problem. A run that starts but cannot
finish (a step size too small, NaN from the right-hand side) does not
throw: it returns its result with a Status that says why.
use ode::OdeError;
use core::array::Vector;
let y0: Vector<f64> = [1.0];
let e = try {
_ = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { -y }, 0.0..1.0, y0, rtol: -1.0);
OdeError::NotFinite { what: "" }
} catch e: OdeError { e };
assert(e == OdeError::BadArgument { what: "rtol must be positive and finite, got -1; pass a relative tolerance such as `rtol: 1e-6`" });
assert(f"{e}".contains("rtol"));
An argument is out of its range;
whatsays which and what fits.Two inputs that must agree in length or shape do not;
whatnames both.An input, or a value the problem's functions gave at the start, holds NaN or an infinity;
whatnames it.
Trait implementations 2
impl Display for OdeErrorimpl Error for OdeError
FixedSource
pub enum FixedA fixed-step method for fixed.
| method | order | stages |
|---|---|---|
Euler | 1 | 1 |
Midpoint | 2 | 2 |
Heun | 2 | 2 |
Rk4 | 4 | 4 |
use ode::Fixed;
assert(Fixed::Rk4.order() == 4 && Fixed::Euler.order() == 1);
Forward Euler.
The explicit midpoint rule.
Heun's method (explicit trapezoid).
The classical 4th order Runge-Kutta method.
The order of accuracy.
nabiassert(ode::Fixed::Heun.order() == 2);
DirectionSource
pub enum DirectionWhich zero crossings of an event function count.
use ode::Direction;
assert(Direction::Rising != Direction::Falling);
Crossings either way.
From negative to positive only.
From positive to negative only.
JacobianSource
pub enum JacobianWhere the implicit methods get df/dy.
use ode::{Jacobian, Method};
use core::array::{Matrix, Vector};
let k = 50.0;
let y0: Vector<f64> = [1.0];
let jac = Jacobian::Exact(|t: f64, y: Vector<f64>| -> Matrix<f64> { [[-k]] });
let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y .* -k }, 0.0..1.0, y0, method: Method::Radau, jac: jac);
assert(r.success() && r.njev >= 1);
Forward differences,
nevaluations offeach.A function giving the
n x nmatrixJ[i, j] = df_i / dy_j.A constant matrix (a linear problem); never recomputed.
MethodSource
pub enum MethodAn integration method for solve_ivp.
| method | kind | order | use for |
|---|---|---|---|
Rk23 | explicit RK 3(2) | 3 | loose tolerances, cheap f |
Rk45 | explicit RK 5(4) | 5 | the default for non-stiff problems |
Dop853 | explicit RK 8(5,3) | 8 | tight tolerances, smooth non-stiff problems |
Radau | implicit RK (IIA) | 5 | stiff problems, tight tolerances |
Bdf | multistep (NDF) | 1-5 | stiff problems, large systems |
Rodas | Rosenbrock 4(3) | 4 | stiff problems, moderate tolerances, small n |
Auto | RK45 / BDF switch | 5/1-5 | stiffness unknown or changing |
use ode::Method;
use core::array::Vector;
let y0: Vector<f64> = [1.0];
for m in [Method::Rk23, Method::Rk45, Method::Dop853, Method::Radau, Method::Bdf, Method::Rodas, Method::Auto] {
let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { -y }, 0.0..1.0, y0, method: m, rtol: 1e-8, atol: 1e-10);
assert((r.last()[0] - (-1.0f64).exp()).abs() < 1e-6);
}
Bogacki-Shampine 3(2), cubic dense output.
Dormand-Prince 5(4), quartic dense output.
Hairer's DOP853, 8th order with 7th degree dense output.
Radau IIA, 5th order, L-stable; cubic dense output.
Variable-order BDF (NDF form), orders 1 to 5.
The Rosenbrock method RODAS, order 4, L-stable; cubic dense output. A fresh Jacobian each step, no Newton iteration.
RK45 while non-stiff, BDF while stiff, switching on its own.
StatusSource
pub enum StatusWhy a run stopped.
use ode::Status;
assert(Status::Finished.success() && Status::Event.success());
assert(!Status::StepTooSmall.success());
assert(f"{Status::StepTooSmall}".contains("rtol"));
The end of the span was reached.
A terminal event stopped the run.
The step size the error test asked for fell below the spacing of floats at
t.The right-hand side gave NaN or infinity, and no smaller step avoided it.
Whether the run ended as asked (the end of the span or a terminal event).
nabiassert(ode::Status::Finished.success());
Trait implementations 1
impl Display for Status
SymplecticSource
pub enum SymplecticA symplectic method for symplectic.
| method | order | force evaluations per step |
|---|---|---|
Euler | 1 | 1 |
Verlet | 2 | 1 (kick-drift-kick) |
Leapfrog | 2 | 1 (drift-kick-drift) |
Yoshida4 | 4 | 3 |
Yoshida6 | 6 | 7 |
assert(ode::Symplectic::Yoshida4.order() == 4);
Symplectic Euler: kick, then drift.
Velocity Verlet (Stormer-Verlet, kick-drift-kick).
Position Verlet (drift-kick-drift).
Yoshida's 4th order composition (Forest-Ruth).
Yoshida's 6th order composition, solution A.
The order of accuracy.
nabiassert(ode::Symplectic::Leapfrog.order() == 2);
Functions
solve_bvpSource
pub fn solve_bvp<X: AsVector<f64>>(
f: fn(f64, Vector<f64>) -> Vector<f64>,
bc: fn(Vector<f64>, Vector<f64>) -> Vector<f64>,
x: X,
y: Matrix<f64>,
tol: f64 = 1e-3,
max_nodes: usize = 1000,
bc_tol: f64 = 0.0,
) throws (OdeError) -> BvpSolves y' = f(x, y) with bc(y(a), y(b)) = 0 (n conditions) on
the mesh x (rising, at least 2 nodes), from the guess y
([n, x.len()]). tol bounds the RMS relative residual, bc_tol
(tol when left 0) the boundary residuals.
use core::array::{Array, Matrix, Vector};
// Bratu's problem y'' + e^y = 0, y(0) = y(1) = 0 (the lower branch).
let x: Vector<f64> = [0.0, 0.25, 0.5, 0.75, 1.0];
let y: Matrix<f64> = Array::zeros([2, 5]);
let r = try ode::solve_bvp(|x: f64, y: Vector<f64>| -> Vector<f64> { [y[1], -y[0].exp()] }, |a: Vector<f64>, b: Vector<f64>| -> Vector<f64> { [a[0], b[0]] }, x, y, tol: 1e-8);
assert(r.success() && (r.sol.at(0.5)[0] - 0.1405392144).abs() < 1e-8);
solve_bvp_paramsSource
pub fn solve_bvp_params<X: AsVector<f64>, P: AsVector<f64>>(
f: fn(f64, Vector<f64>, Vector<f64>) -> Vector<f64>,
bc: fn(Vector<f64>, Vector<f64>, Vector<f64>) -> Vector<f64>,
x: X,
y: Matrix<f64>,
p: P,
tol: f64 = 1e-3,
max_nodes: usize = 1000,
bc_tol: f64 = 0.0,
) throws (OdeError) -> Bvpsolve_bvp with unknown parameters p (k of them, from the guess
p): f(x, y, p) and n + k boundary conditions bc(ya, yb, p),
as for eigenvalue problems. Always takes the dense Newton path.
use core::array::{Array, Matrix, Vector};
// y'' + k^2 y = 0, y(0) = y(1) = 0, y'(0) = k: the first eigenvalue is k = pi.
let x: Vector<f64> = [0.0, 0.25, 0.5, 0.75, 1.0];
let y: Matrix<f64> = [[0.0, 0.7, 1.0, 0.7, 0.0], [3.0, 2.0, 0.0, -2.0, -3.0]];
let p0: Vector<f64> = [3.0];
let r = try ode::solve_bvp_params(
|x: f64, y: Vector<f64>, p: Vector<f64>| -> Vector<f64> { [y[1], -p[0] * p[0] * y[0]] },
|a: Vector<f64>, b: Vector<f64>, p: Vector<f64>| -> Vector<f64> { [a[0], b[0], a[1] - p[0]] },
x, y, p0, tol: 1e-8);
assert(r.success() && (r.p[0] - 3.141592653589793).abs() < 1e-6);
eulerSource
pub fn euler<Y: AsVector<f64>>(f: fn(f64, Vector<f64>) -> Vector<f64>, span: Range<f64>, y0: Y, steps: usize) throws (OdeError) -> Solution fixed with forward Euler.
use core::array::Vector;
let y0: Vector<f64> = [1.0];
let r = try ode::euler(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 0.0..1.0, y0, 4);
assert(r.last()[0] == 1.25 * 1.25 * 1.25 * 1.25);
fixedSource
pub fn fixed<Y: AsVector<f64>>(f: fn(f64, Vector<f64>) -> Vector<f64>, span: Range<f64>, y0: Y, steps: usize, method: Fixed = Fixed::Rk4) throws (OdeError) -> Solution Integrates y' = f(t, y) over span in steps equal steps of
method (h = (end - start) / steps; the span may run backwards).
The result has steps + 1 columns. A NaN or infinity in the state
stops the run with Status::NotFinite.
use ode::Fixed;
use core::array::Vector;
let y0: Vector<f64> = [1.0];
let f = |t: f64, y: Vector<f64>| -> Vector<f64> { y };
let e1 = (try ode::fixed(f, 0.0..1.0, y0, 100, method: Fixed::Rk4)).last()[0] - 1.0f64.exp();
let e2 = (try ode::fixed(f, 0.0..1.0, y0, 200, method: Fixed::Rk4)).last()[0] - 1.0f64.exp();
assert((e1 / e2 - 16.0).abs() < 0.5); // 4th order: halving h cuts the error 16 times
rk4Source
pub fn rk4<Y: AsVector<f64>>(f: fn(f64, Vector<f64>) -> Vector<f64>, span: Range<f64>, y0: Y, steps: usize) throws (OdeError) -> Solution fixed with the classical Runge-Kutta method.
use core::array::Vector;
let y0: Vector<f64> = [0.0, 1.0];
let r = try ode::rk4(|t: f64, y: Vector<f64>| -> Vector<f64> { [y[1], -y[0]] }, 0.0..6.283185307179586, y0, 200);
assert((r.last()[1] - 1.0).abs() < 1e-7);
solve_ivpSource
pub fn solve_ivp<Y: AsVector<f64>>(
f: fn(f64, Vector<f64>) -> Vector<f64>,
span: Range<f64>,
y0: Y,
method: Method = Method::Rk45,
rtol: f64 = 1e-3,
atol: f64 | Vector<f64> = 1e-6,
t_eval: Vector<f64> | none = none,
dense: bool = true,
events: List<Event> = [],
max_step: f64 = f64::INFINITY,
first_step: f64 = 0.0,
jac: Jacobian = Jacobian::Approx,
) throws (OdeError) -> SolutionSolves y' = f(t, y), y(span.start) = y0 over span, which may
run backwards (10.0..0.0).
The error test is per component, |err_i| <= atol_i + rtol |y_i|,
in the RMS norm: rtol (floored at 100 eps) and atol (one value
or one per component) say how many digits to keep and below what
size a component counts as zero. Options:
t_eval: output only at these times (insidespan, ordered along it), read off the dense output; otherwise at every step end.dense: keep the dense output insol(on by default;falsesaves memory and DOP853's three extra stages per step).events: functions whose zeros to locate (Event).max_step,first_step: bound the step size, fix the first one (0lets the method choose).jac: the Jacobian forRadau,Bdf,RodasandAuto.
A run that cannot finish returns what it has with a failing
status; bad input throws OdeError.
use ode::Method;
use core::array::Vector;
// Logistic growth, y' = y (1 - y): y(t) = 1 / (1 + 9 e^-t) from 0.1.
let r = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y .* (1.0 .- y) }, 0.0..5.0, [0.1], method: Method::Dop853, rtol: 1e-10, atol: 1e-12);
assert((r.last()[0] - 1.0 / (1.0 + 9.0 * (-5.0f64).exp())).abs() < 1e-10);
// Backwards from t = 1.
let y1: Vector<f64> = [1.0f64.exp()];
let b = try ode::solve_ivp(|t: f64, y: Vector<f64>| -> Vector<f64> { y }, 1.0..0.0, y1, rtol: 1e-9, atol: 1e-12);
assert((b.last()[0] - 1.0).abs() < 1e-8);
leapfrogSource
pub fn leapfrog<Q: AsVector<f64>, V: AsVector<f64>>(accel: fn(Vector<f64>) -> Vector<f64>, q0: Q, v0: V, span: Range<f64>, steps: usize, every: usize = 1) throws (OdeError) -> Orbit Leapfrog (drift-kick-drift) for q'' = accel(q), unit mass.
use core::array::Vector;
let q0: Vector<f64> = [0.0];
let v0: Vector<f64> = [0.0];
let o = try ode::leapfrog(|q: Vector<f64>| -> Vector<f64> { [-9.81] }, q0, v0, 0.0..1.0, 4);
assert((o.last_q()[0] + 0.5 * 9.81).abs() < 1e-12); // exact for constant force
symplecticSource
pub fn symplectic(
velocity: fn(Vector<f64>) -> Vector<f64>,
force: fn(Vector<f64>) -> Vector<f64>,
q0: Vector<f64>,
p0: Vector<f64>,
span: Range<f64>,
steps: usize,
method: Symplectic = Symplectic::Verlet,
every: usize = 1,
) throws (OdeError) -> OrbitIntegrates the separable Hamiltonian system q' = velocity(p)
(dT/dp), p' = force(q) (-dV/dq) over span in steps equal
steps, saving every every-th step (and the last).
use ode::Symplectic;
use core::array::Vector;
// A pendulum, H = p^2 / 2 - cos q.
let q0: Vector<f64> = [1.0];
let p0: Vector<f64> = [0.0];
let o = try ode::symplectic(|p: Vector<f64>| -> Vector<f64> { p }, |q: Vector<f64>| -> Vector<f64> { [-q[0].sin()] }, q0, p0, 0.0..100.0, 2000, method: Symplectic::Yoshida4);
let e = o.energy(|q: Vector<f64>, p: Vector<f64>| -> f64 { 0.5 * p[0] * p[0] - q[0].cos() });
assert((e[e.len() - 1] - e[0]).abs() < 1e-6);
verletSource
pub fn verlet<Q: AsVector<f64>, V: AsVector<f64>>(accel: fn(Vector<f64>) -> Vector<f64>, q0: Q, v0: V, span: Range<f64>, steps: usize, every: usize = 1) throws (OdeError) -> Orbit Velocity Verlet for q'' = accel(q): symplectic with unit mass,
p the velocity.
use core::array::Vector;
let q0: Vector<f64> = [0.0];
let v0: Vector<f64> = [1.0];
let o = try ode::verlet(|q: Vector<f64>| -> Vector<f64> { [0.0] }, q0, v0, 0.0..2.0, 10);
assert((o.last_q()[0] - 2.0).abs() < 1e-14);
yoshida4Source
pub fn yoshida4<Q: AsVector<f64>, V: AsVector<f64>>(accel: fn(Vector<f64>) -> Vector<f64>, q0: Q, v0: V, span: Range<f64>, steps: usize, every: usize = 1) throws (OdeError) -> Orbit Yoshida's 4th order method for q'' = accel(q), unit mass.
use core::array::Vector;
let q0: Vector<f64> = [1.0];
let v0: Vector<f64> = [0.0];
let o = try ode::yoshida4(|q: Vector<f64>| -> Vector<f64> { -q }, q0, v0, 0.0..1.0, 50);
assert((o.last_q()[0] - 1.0f64.cos()).abs() < 1e-8);