base
cosmic_warp.core.integrators.base
Base integrator interface and common utilities.
This module provides the abstract base class for all integrators and re-exports common dependencies to maintain clean separation of concerns.
Integrator
Bases: ABC
Abstract base class for numerical integrators.
Initialize integrator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_envs
|
int
|
Number of parallel environments |
required |
device
|
str
|
Device to run on |
'cuda'
|
num_bodies
|
int
|
Number of bodies per environment |
1
|
Source code in cosmic_warp/core/integrators/base.py
launch_step
abstractmethod
launch_step(
ctx: SimContext,
dt: float,
states_in: array,
states_out: array,
act_states_in: array,
act_states_out: array,
commands: array,
) -> None
Launch one integration step reading from the *_in buffers and writing the *_out buffers (single fused kernel). The kernel also increments ctx.episode_steps and checks termination for each entity independently. Use step() for in-place stepping with whole-environment termination; it reduces multi-body status after this kernel completes.
The in/out buffers may alias (states_in is states_out): each thread fully reads its own element before writing it. Differentiable rollouts pass distinct per-step buffers so the tape never sees an overwrite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
SimContext
|
Simulation context (vehicle/actuator configs, flags, status) |
required |
dt
|
float
|
Time step |
required |
states_in
|
array
|
State structs read [num_entities] |
required |
states_out
|
array
|
State structs written [num_entities] |
required |
act_states_in
|
array
|
ActuatorState structs read [num_entities] |
required |
act_states_out
|
array
|
ActuatorState structs written [num_entities] |
required |
commands
|
array
|
ActuatorCommand structs for this step [num_entities] |
required |
Source code in cosmic_warp/core/integrators/base.py
step
step(ctx: SimContext, dt: float) -> None
Perform one in-place integration step (updates ctx.states and ctx.actuator_states, flags newly terminated entities in ctx.termination_status, increments ctx.episode_steps).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
SimContext
|
Simulation context (state, config and status device arrays) |
required |
dt
|
float
|
Time step |
required |
Source code in cosmic_warp/core/integrators/base.py
IntegratorFactory
Factory for creating integrators.
create
staticmethod
create(
method: str,
num_envs: int,
device: str = "cuda",
num_bodies: int = 1,
) -> Integrator
Create an integrator instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Integration method ("euler", "rk4", "symplectic") |
required |
num_envs
|
int
|
Number of parallel environments |
required |
device
|
str
|
Device to run on |
'cuda'
|
num_bodies
|
int
|
Number of bodies per environment |
1
|
Returns:
| Type | Description |
|---|---|
Integrator
|
Integrator instance |
Source code in cosmic_warp/core/integrators/base.py
compute_rw_momentum_vec
compute_rw_momentum_vec(
act_state: ActuatorState, rw_config: ReactionWheelConfig
) -> vec3d
Total reaction wheel angular momentum vector in the body frame (sum of all 4 wheels; unused wheels have zero spin axes).
Source code in cosmic_warp/core/integrators/base.py
compute_state_derivative
compute_state_derivative(
state: State,
vehicle: VehicleConfig,
force: vec3d,
torque: vec3d,
mdot: float64,
act_state: ActuatorState,
rw_config: ReactionWheelConfig,
) -> StateDerivative
Compute state derivatives given current state and forces/torques.
Frame conventions
- position, linear momentum: inertial
- angular momentum, rw momentum, omega: body
- forces: inertial
- torques: body
- mdot: total propellant mass flow rate [kg/s] (>= 0, from thruster_wrench for the current step)
Adjoint-safe: single-assignment locals, struct built once via its constructor (field-wise struct mutation breaks Warp's autodiff).
Source code in cosmic_warp/core/integrators/base.py
state_add_scaled
state_add_scaled(
state: State,
vehicle: VehicleConfig,
mom_dot: vec3d,
pos_dot: vec3d,
ang_mom_dot: vec3d,
quat_dot: quatd,
mass_dot: float64,
scale: float64,
) -> State
Add scaled derivatives to state (for RK methods).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Base state |
required |
vehicle
|
VehicleConfig
|
Vehicle configuration (its dry mass floors the new mass) |
required |
mom_dot
|
vec3d
|
Momentum derivative |
required |
pos_dot
|
vec3d
|
Position derivative |
required |
ang_mom_dot
|
vec3d
|
Angular momentum derivative |
required |
quat_dot
|
quatd
|
Quaternion derivative |
required |
mass_dot
|
float64
|
Mass rate-of-change [kg/s] (negative = consuming propellant) |
required |
scale
|
float64
|
Scaling factor (usually dt or fraction of dt) |
required |
Returns:
| Type | Description |
|---|---|
State
|
New state with scaled derivatives added (constructed once — adjoint-safe) |
Source code in cosmic_warp/core/integrators/base.py
store_state
Write a state into the output array field-by-field.
Whole-struct stores of locally built structs break Warp's adjoint generation (silently zeroed gradients); per-field stores are the autodiff-safe pattern shared by every integrator kernel.
Source code in cosmic_warp/core/integrators/base.py
integrate_reaction_wheels
integrate_reaction_wheels(
rw_config: ReactionWheelConfig,
act_cmd: ActuatorCommand,
act_state: ActuatorState,
dt: float64,
) -> vec4d
Update all 4 reaction wheels and return their new angular momenta.
This centralizes the repeated pattern used across integrators so kernels stay concise and maintainable.
Source code in cosmic_warp/core/integrators/base.py
compute_rw_momentum_delta
compute_rw_momentum_delta(
L_new: vec4d,
act_state: ActuatorState,
rw_config: ReactionWheelConfig,
) -> vec3d
Compute the total delta angular-momentum vector transferred from reaction wheels to the body (sum of wheel deltas projected on spin axes).