Skip to content

symplectic

cosmic_warp.core.integrators.symplectic

Symplectic integrator implementation for Hamiltonian systems.

This integrator preserves the symplectic structure of Hamiltonian mechanics, making it ideal for long-term orbital simulations and energy conservation.

SymplecticIntegrator

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

Bases: Integrator

Symplectic Integrator (second-order, kick-drift-kick).

More accurate and stable for long-term simulations of mechanical systems. Suitable for: - Long-duration orbital simulations - Systems with conserved quantities (e.g., energy, momentum) - Scenarios where stability over many time steps is critical

Accuracy notes: - The scheme is symplectic only for constant mass. While thrusters fire, propellant consumption makes the system non-Hamiltonian and energy is (correctly) not conserved. Each kick solves the constant-force variable-mass flow exactly, so burns stay second-order. - The rotational drift is the explicit midpoint rule on the rotation group: omega is taken at the half-step attitude. Total angular momentum is conserved exactly in the inertial frame; the attitude error is second-order in dt. - Velocity-dependent forces (drag) are evaluated at the start of each kick, which is first-order in their (usually tiny) contribution.

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

Second-order method.

name property

name: str

Method name.

gravity_vector

gravity_vector(
    state: State, flags: SimulationFlags
) -> vec3d

Gravitational force [N], zero when gravity is disabled.

Source code in cosmic_warp/core/integrators/symplectic.py
@wp.func
def gravity_vector(state: State, flags: SimulationFlags) -> wp.vec3d:
    """Gravitational force [N], zero when gravity is disabled."""
    F = wp.vec3d()
    if flags.gravity:
        F = gravity_force(state, flags)
    return F

kick_momentum

kick_momentum(
    mom: vec3d,
    m_start: float64,
    m_end: float64,
    F_grav: vec3d,
    F_other: vec3d,
    h: float64,
) -> vec3d

Exact momentum kick over time h with forces held fixed and mass falling linearly from m_start to m_end (the burn fraction keeps the flow on for the whole kick, so the mass never sits at a dry clamp within it).

With expelled propellant leaving at the vehicle velocity (Meshchersky), m dv/dt = F. Gravity is proportional to mass (constant acceleration); thrust and drag are not, so they act through the integral of 1/m(t): v1 = v0 + h F_grav/m_start + F_other * int_0^h dt/m(t)

Source code in cosmic_warp/core/integrators/symplectic.py
@wp.func
def kick_momentum(
    mom: wp.vec3d,
    m_start: wp.float64,
    m_end: wp.float64,
    F_grav: wp.vec3d,
    F_other: wp.vec3d,
    h: wp.float64,
) -> wp.vec3d:
    """
    Exact momentum kick over time h with forces held fixed and mass falling
    linearly from m_start to m_end (the burn fraction keeps the flow on for
    the whole kick, so the mass never sits at a dry clamp within it).

    With expelled propellant leaving at the vehicle velocity (Meshchersky),
    m dv/dt = F. Gravity is proportional to mass (constant acceleration);
    thrust and drag are not, so they act through the integral of 1/m(t):
        v1 = v0 + h F_grav/m_start + F_other * int_0^h dt/m(t)
    """
    v0 = mom / m_start
    # int_0^h dt/m(t) = h/m_start * (-ln(1-x)/x), x = burned fraction of
    # m_start; series for small x (including x = 0) avoids cancellation
    x = (m_start - m_end) / m_start
    log_ratio = wp.float64(1.0) + x * (wp.float64(0.5) + x * (
        wp.float64(1.0) / wp.float64(3.0) + x * wp.float64(0.25)))
    if x > wp.float64(1.0e-4):
        log_ratio = -wp.log(wp.float64(1.0) - x) / x
    v1 = v0 + h * F_grav / m_start + h / m_start * log_ratio * F_other
    return m_end * v1