[][src]Module peroxide::numerical::ode

Solver for ordinary differential equations

Introduce ODE Trait & Structure

ODE Trait

State<T> structure

For example,

$$ \frac{dy_n}{dt} = f(t, y_n) $$

Methods for State<T> are as follows.

ExplicitODE struct

ExplicitODE is given as follow :

extern crate peroxide;
use std::collections::HashMap;
use peroxide::{State, ExMethod, BoundaryCondition, ODEOptions};

#[derive(Clone)]
pub struct ExplicitODE {
    state: State<f64>,
    func: fn(&mut State<f64>),
    step_size: f64,
    method: ExMethod,
    init_cond: State<f64>,
    bound_cond1: (State<f64>, BoundaryCondition),
    bound_cond2: (State<f64>, BoundaryCondition),
    stop_cond: fn(&Self) -> bool,
    times: usize,
    to_use: HashMap<ODEOptions, bool>,
}

Example

Lorenz Butterfly

extern crate peroxide;
use peroxide::*;

fn main() {
    // =========================================
    //  Declare ODE
    // =========================================
    let mut ex_test = ExplicitODE::new(f);

    let init_state: State<f64> = State::new(
        0.0,
        vec![10.0, 1.0, 1.0],
        vec![0.0, 0.0, 0.0],
    );

    ex_test
        .set_initial_condition(init_state)
        .set_method(ExMethod::Euler)
        .set_step_size(0.01f64)
        .set_times(10000);

    let mut ex_test2 = ex_test.clone();
    ex_test2.set_method(ExMethod::RK4);

    // =========================================
    //  Save results
    // =========================================
    let results = ex_test.integrate();
    let results2 = ex_test2.integrate();

    // Plot or extract
}

fn f(st: &mut State<f64>) {
    let x = &st.value;
    let dx = &mut st.deriv;
    dx[0] = 10f64 * (x[1] - x[0]);
    dx[1] = 28f64 * x[0] - x[1] - x[0] * x[2];
    dx[2] = -8f64/3f64 * x[2] + x[0] * x[1];
}

If plotting pickle data with python, then

Lorenz with Euler

Lorenz with RK4

Simple 1D Runge-Kutta

$$\begin{gathered} \frac{dy}{dx} = \frac{5x^2 - y}{e^{x+y}} \\ y(0) = 1 \end{gathered}$$

extern crate peroxide;
use peroxide::*;

fn main() {
    let init_state = State::<f64>::new(0f64, c!(1), c!(0));

    let mut ode_solver = ExplicitODE::new(test_fn);

    ode_solver
        .set_method(ExMethod::RK4)
        .set_initial_condition(init_state)
        .set_step_size(0.01)
        .set_times(1000);

    let result = ode_solver.integrate();

    // Plot or Extract..
}

fn test_fn(st: &mut State<f64>) {
    let x = st.param;
    let y = &st.value;
    let dy = &mut st.deriv;
    dy[0] = (5f64*x.powi(2) - y[0]) / (x + y[0]).exp();
}

Structs

ExplicitODE
ImplicitODE
State

State for ODE

Enums

BoundaryCondition

Kinds of Boundary Conditions

ExMethod

Explicit ODE Methods

ImMethod
ODEOptions

Options for ODE

Traits

ODE

ODE solver

Type Definitions

ExUpdater
ImUpdater