Runge–Kutta Methods
Runge–Kutta methods are a family of implicit and explicit iterative algorithms used to approximate the solutions of ordinary differential equations. They achieve high-order accuracy by evaluating the derivative of the state vector at multiple intermediate stages within a single time step.
I. Mathematical Formulation
1. General Runge–Kutta Steps
For an ordinary differential equation defined by state vector \(\mathbf{y}\) and derivative function \(\mathbf{f}\) as
a general \(s\)-stage Runge–Kutta method advances the state from time \(t_n\) to \(t_{n+1}=t_n+h\) using step size \(h\) as
where \(\mathbf{k}_i\) represents the stage derivative vector for stage \(i\), computed recursively as
with coefficients \(a_{ij}\), \(b_i\), and \(c_i\) defining the specific integration method. For explicit methods, the stage summation runs only up to \(j=1,\dots,i-1\), meaning the current stage derivative depends only on previously computed derivatives.
2. Butcher Tableaus
The algebraic coefficients of a Runge–Kutta method are compactly organized in a structured table called a Butcher tableau. The tableau represents the coefficients as
where the matrix element \(a_{ij}\) represents the weight of stage \(j\) in calculating stage \(i\), \(b_i\) represents the weight of stage \(i\) in calculating the final step update, and \(c_i\) represents the fractional time step for stage \(i\).
II. Implementation and Methods
3. Compile-Time Optimization and Allocation Tricks
In the codebase, Runge–Kutta steppers are implemented as generic types parameterized by a compile-time ButcherTableau structure. This allows the compiler to unroll the stage summation loops and optimize away terms where \(a_{ij} = 0\) or \(b_i = 0\). To minimize memory fragmentation and allocation overhead during dynamics simulations, the stage derivative vectors \(\mathbf{k}_i\) are allocated in a single, contiguous block of memory and accessed as slices. During a step, the temporary state is accumulated in a pre-allocated vector tmp before invoking the user-provided derivative evaluation callback.
4. First-Order Euler Method
The first-order Euler method (RK1) is the simplest Runge–Kutta scheme, containing a single stage. Its Butcher tableau is given by
which yields a first-order accurate update step.
5. Classical Fourth-Order Runge–Kutta Method
The classical fourth-order Runge–Kutta method (RK4) is a widely used four-stage integration scheme. Its Butcher tableau is constructed as
which yields fourth-order global accuracy, providing a robust balance between computational cost and numerical precision.