Module ode

Source
Expand description

§Ordinary Differential Equation (ODE) Solvers

This module provides traits and structs for solving ordinary differential equations (ODEs).

§Overview

  • ODEProblem: Trait for defining an ODE problem.
  • ODEIntegrator: Trait for ODE integrators.
  • ODESolver: Trait for ODE solvers.
  • ODEError: Enum for ODE errors.
    • ReachedMaxStepIter: Reached maximum number of steps per step. (internal error)
    • ConstraintViolation(f64, Vec<f64>, Vec<f64>): Constraint violation. (user-defined error)
    • ODE uses anyhow for error handling. So, you can customize your errors.

§Available integrators

  • Explicit
    • Ralston’s 3rd order (RALS3)
    • Runge-Kutta 4th order (RK4)
    • Ralston’s 4th order (RALS4)
    • Runge-Kutta 5th order (RK5)
  • Embedded
    • Bogacki-Shampine 2/3rd order (BS23)
    • Runge-Kutta-Fehlberg 4/5th order (RKF45)
    • Dormand-Prince 4/5th order (DP45)
    • Tsitouras 4/5th order (TSIT45)
    • Runge-Kutta-Fehlberg 7/8th order (RKF78)
  • Implicit
    • Gauss-Legendre 4th order (GL4)

§Available solvers

  • BasicODESolver: A basic ODE solver using a specified integrator.

You can implement your own ODE solver by implementing the ODESolver trait.

§Example

use peroxide::fuga::*;

fn main() -> Result<(), Box<dyn Error>> {
    // Same as : let rkf = RKF45::new(1e-4, 0.9, 1e-6, 1e-1, 100);
    let rkf = RKF45 {
        tol: 1e-6,
        safety_factor: 0.9,
        min_step_size: 1e-6,
        max_step_size: 1e-1,
        max_step_iter: 100,
    };
    let basic_ode_solver = BasicODESolver::new(rkf);
    let initial_conditions = vec![1f64];
    let (t_vec, y_vec) = basic_ode_solver.solve(
        &Test,
        (0f64, 10f64),
        0.01,
        &initial_conditions,
    )?;
    let y_vec: Vec<f64> = y_vec.into_iter().flatten().collect();
    println!("{}", y_vec.len());

    let mut plt = Plot2D::new();
    plt
        .set_domain(t_vec)
        .insert_image(y_vec)
        .set_xlabel(r"$t$")
        .set_ylabel(r"$y$")
        .set_style(PlotStyle::Nature)
        .tight_layout()
        .set_dpi(600)
        .set_path("example_data/rkf45_test.png")
        .savefig()?;
    Ok(())
}

// Extremely customizable struct
struct Test;

impl ODEProblem for Test {
    fn rhs(&self, t: f64, y: &[f64], dy: &mut [f64]) -> anyhow::Result<()> {
        Ok(dy[0] = (5f64 * t.powi(2) - y[0]) / (t + y[0]).exp())
    }
}

Structs§

ArchivedBS23
An archived BS23
ArchivedDP45
An archived DP45
ArchivedGL4
An archived GL4
ArchivedRALS3
An archived RALS3
ArchivedRALS4
An archived RALS4
ArchivedRK4
An archived RK4
ArchivedRK5
An archived RK5
ArchivedRKF45
An archived RKF45
ArchivedRKF78
An archived RKF78
ArchivedTSIT45
An archived TSIT45
BS23
Bogacki-Shampine 3(2) method
BS23Resolver
The resolver for an archived BS23
BasicODESolver
A basic ODE solver using a specified integrator.
DP45
Dormand-Prince 5(4) method
DP45Resolver
The resolver for an archived DP45
GL4
Gauss-Legendre 4th order integrator.
GL4Resolver
The resolver for an archived GL4
RALS3
Ralston’s 3rd order integrator
RALS4
Ralston’s 4th order integrator.
RALS3Resolver
The resolver for an archived RALS3
RALS4Resolver
The resolver for an archived RALS4
RK4
Runge-Kutta 4th order integrator.
RK5
Runge-Kutta 5th order integrator
RK4Resolver
The resolver for an archived RK4
RK5Resolver
The resolver for an archived RK5
RKF45
Runge-Kutta-Fehlberg 4/5th order integrator.
RKF78
Runge-Kutta-Fehlberg 7/8th order integrator.
RKF45Resolver
The resolver for an archived RKF45
RKF78Resolver
The resolver for an archived RKF78
TSIT45
Tsitouras 5(4) method
TSIT45Resolver
The resolver for an archived TSIT45

Enums§

ArchivedImplicitSolver
An archived ImplicitSolver
ImplicitSolver
Enum for implicit solvers.
ImplicitSolverResolver
The resolver for an archived ImplicitSolver
ODEError
Enum for ODE errors.

Traits§

ButcherTableau
Trait for Butcher tableau
ODEIntegrator
Trait for ODE integrators.
ODEProblem
Trait for defining an ODE problem.
ODESolver
Trait for ODE solvers.