Skip to main content

Rk45

Struct Rk45 

Source
pub struct Rk45<T: Numeric = f64> { /* private fields */ }
Expand description

Adaptive Dormand–Prince 5(4) integrator for y' = f(t, y) with state Vector<N, T>.

Build with Rk45::default and adjust with the with_* methods. Solve with solve, stream accepted steps with for_each_step, or sample a time grid with solve_on_grid.

Implementations§

Source§

impl<T: Numeric> Rk45<T>

Source

pub fn with_rtol(self, rtol: T) -> Self

Sets the relative tolerance (default 1e-6).

Source

pub fn with_atol(self, atol: T) -> Self

Sets the absolute tolerance (default 1e-9).

Source

pub fn with_first_step(self, h: T) -> Self

Sets the first step size; 0 (the default) auto-selects it.

Source

pub fn with_min_step(self, h: T) -> Self

Sets the minimum step magnitude; falling below it returns IntegrateError::StepSizeTooSmall. 0 (the default) disables the floor.

Source

pub fn with_max_step(self, h: T) -> Self

Sets the maximum step magnitude (default unbounded).

Source

pub fn with_max_steps(self, n: usize) -> Self

Sets the maximum number of step attempts before IntegrateError::DidNotConverge (default 100_000).

Source§

impl<T: Numeric> Rk45<T>

Source

pub fn for_each_step<const N: usize, F, O>( &self, f: &F, t0: T, y0: &Vector<N, T>, tf: T, obs: O, ) -> Result<Vector<N, T>, IntegrateError>
where F: Fn(T, &Vector<N, T>) -> Vector<N, T>, O: FnMut(&Step<N, T>),

Integrates from t0 to tf, invoking obs with each accepted Step, and returns the final state.

§Errors

LimitsIllDefined for a NaN or zero-length span; NonFinite if f or the state goes non-finite; StepSizeTooSmall if the step drops below min_step; DidNotConverge if max_steps is exhausted.

use multicalc::ode::Rk45;
use multicalc::linear_algebra::Vector;
// y' = -y over [0, 2]; y(2) = e^{-2}.
let yf = Rk45::default()
    .solve(&|_t, y: &Vector<1, f64>| -*y, 0.0, &Vector::new([1.0]), 2.0)
    .unwrap();
assert!((yf[0] - (-2.0_f64).exp()).abs() < 1e-6);
Source

pub fn solve<const N: usize, F>( &self, f: &F, t0: T, y0: &Vector<N, T>, tf: T, ) -> Result<Vector<N, T>, IntegrateError>
where F: Fn(T, &Vector<N, T>) -> Vector<N, T>,

Integrates from t0 to tf and returns the final state (no per-step callback).

Source

pub fn solve_on_grid<const N: usize, F>( &self, f: &F, t0: T, y0: &Vector<N, T>, times: &[T], out: &mut [Vector<N, T>], ) -> Result<(), IntegrateError>
where F: Fn(T, &Vector<N, T>) -> Vector<N, T>,

Samples the solution at each time in times (sorted in the integration direction and lying within [t0, tf]), writing to out via cubic-Hermite dense output. No allocation.

§Errors

LimitsIllDefined if times.len() != out.len() or a time is out of range / out of order; otherwise as for_each_step.

use multicalc::ode::Rk45;
use multicalc::linear_algebra::Vector;
// Sample y' = -y at t = 0.5 and t = 1 by cubic-Hermite dense output.
let times = [0.5, 1.0];
let mut out = [Vector::<1, f64>::zeros(); 2];
Rk45::default()
    .solve_on_grid(&|_t, y: &Vector<1, f64>| -*y, 0.0, &Vector::new([1.0]), &times, &mut out)
    .unwrap();
assert!((out[0][0] - (-0.5_f64).exp()).abs() < 1e-6);
assert!((out[1][0] - (-1.0_f64).exp()).abs() < 1e-6);

Trait Implementations§

Source§

impl<T: Numeric> Default for Rk45<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Rk45<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Rk45<T>
where T: RefUnwindSafe,

§

impl<T> Send for Rk45<T>
where T: Send,

§

impl<T> Sync for Rk45<T>
where T: Sync,

§

impl<T> Unpin for Rk45<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Rk45<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Rk45<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.