Skip to content

rk4

cosmic_warp.core.integrators.rk4

Runge-Kutta 4th order (RK4) integrator implementation.

RK4Integrator

RK4Integrator(
    num_envs: int, device: str = "cuda", num_bodies: int = 1
)

Bases: Integrator

Classic 4th-order Runge-Kutta integrator (RK4).

Good balance of accuracy and computational cost. Uses 4 function evaluations per step.

The method computes

k1 = f(t, y) k2 = f(t + dt/2, y + dt/2 * k1) k3 = f(t + dt/2, y + dt/2 * k2) k4 = f(t + dt, y + dt * k3) y(t+dt) = y(t) + dt/6 * (k1 + 2k2 + 2k3 + k4)

Reaction wheel momenta h(t) follow a closed-form solution within the step, so each stage evaluates them at its own time (0, dt/2, dt). The RK stages advance the body-frame total angular momentum L + h, whose rate tau_ext - omega x (L + h) needs no wheel torque; L is recovered by subtracting h at the stage time. This keeps the wheel/body coupling fourth-order.

Source code in cosmic_warp/core/integrators/base.py
def __init__(self, num_envs: int, device: str = "cuda", num_bodies: int = 1):
    """
    Initialize integrator.

    Args:
        num_envs: Number of parallel environments
        device: Device to run on
        num_bodies: Number of bodies per environment
    """
    self.num_envs = num_envs
    self.num_bodies = num_bodies
    self.num_entities = num_envs * num_bodies
    self.device = device

order property

order: int

Fourth-order method.

name property

name: str

Method name.