Macro runge_kutta_method

Source
macro_rules! runge_kutta_method {
    (
        $(#[$attr:meta])*
        name: $name:ident,
        a: $a:expr,
        b: $b:expr,
        c: $c:expr,
        order: $order:expr,
        stages: $stages:expr
        $(,)? // Optional trailing comma
    ) => { ... };
}
Expand description

Macro to create a Runge-Kutta solver from a Butcher tableau with fixed-size arrays

§Arguments

  • name: Name of the solver struct to create
  • doc: Documentation string for the solver
  • a: Matrix of coefficients for intermediate stages
  • b: Weights for final summation
  • c: Time offsets for each stage
  • order: Order of accuracy of the method
  • stages: Number of stages in the method

§Example

use differential_equations::runge_kutta_method;

// Define classical RK4 method
runge_kutta_method!(
    /// Classical 4th Order Runge-Kutta Method
    name: RK4,
    a: [[0.0, 0.0, 0.0, 0.0],
        [0.5, 0.0, 0.0, 0.0],
        [0.0, 0.5, 0.0, 0.0],
        [0.0, 0.0, 1.0, 0.0]],
    b: [1.0/6.0, 2.0/6.0, 2.0/6.0, 1.0/6.0],
    c: [0.0, 0.5, 0.5, 1.0],
    order: 4,
    stages: 4
);

§Note on Butcher Tableaus

The a matrix is typically a lower triangular matrix with zeros on the diagonal. when creating the a matrix for implementation simplicity it is generated as a 2D array with zeros in the upper triangular portion of the matrix. The array size is known at compile time and it is a O(1) operation to access the desired elements. When computing the Runge-Kutta stages only the elements in the lower triangular portion of the matrix and unnessary multiplication by zero is avoided. The Rust compiler is also likely to optimize the array out instead of memory addresses directly.