Index
cosmic_warp
COSMIC WARP - Spacecraft Simulator
A GPU-accelerated spacecraft dynamics simulator built on NVIDIA WARP.
SpacecraftSimulator
SpacecraftSimulator(
*,
num_envs: int = 1,
integrator: str = "rk4",
dt: float = 0.01,
device: str = "cuda",
num_bodies: int = 1,
gravity: bool = True,
j2_perturbation: Optional[bool] = None,
gravity_gradient: bool = False,
magnetic_torque: bool = False,
drag: bool = False,
check_bounds: bool = True,
check_planet_impact: bool = False,
pos_limit: float = 100000000.0,
mom_limit: float = inf,
ang_mom_limit: float = inf,
mass_limit: float = 0.0,
planet_radius: float = RE_EARTH,
reset_config: Optional[Dict[str, Any]] = None,
differentiable: bool = False,
)
Main simulation pipeline for parallel spacecraft dynamics.
This class orchestrates all simulation components including: - State management - Force/torque computation - Numerical integration - Termination checking - Environment resets
Initialize spacecraft simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_envs
|
int
|
Number of parallel environments |
1
|
integrator
|
str
|
Integration method ("euler", "rk4", "symplectic"); default "rk4" |
'rk4'
|
dt
|
float
|
Time step [s] |
0.01
|
device
|
str
|
Device to run on ("cuda" or "cpu") |
'cuda'
|
num_bodies
|
int
|
Number of bodies per environment |
1
|
gravity
|
bool
|
Enable gravitational forces |
True
|
j2_perturbation
|
Optional[bool]
|
Enable J2 perturbation; requires gravity=True. Default (None): follows gravity |
None
|
gravity_gradient
|
bool
|
Enable gravity gradient torque |
False
|
magnetic_torque
|
bool
|
Enable magnetic torque (set vehicle.magnetic_dipole [A*m^2]) |
False
|
drag
|
bool
|
Enable atmospheric drag (uses vehicle.area_drag [m^2] and vehicle.cd) |
False
|
check_bounds
|
bool
|
Terminate on the position, momentum, angular-momentum and mass limits below |
True
|
check_planet_impact
|
bool
|
Enable impact detection with the central body |
False
|
pos_limit
|
float
|
Maximum position bound [m]; math.inf disables |
100000000.0
|
mom_limit
|
float
|
Maximum momentum bound [kg*m/s]; no limit by default (momentum scales with the vehicle's mass) |
inf
|
ang_mom_limit
|
float
|
Maximum angular momentum bound [kg*m^2/s]; no limit by default (it scales with the vehicle's inertia) |
inf
|
mass_limit
|
float
|
Terminate once mass <= mass_limit [kg]; 0 disables. mass_limit = mass_dry terminates when propellant runs out |
0.0
|
planet_radius
|
float
|
Central body impact radius [m], used by check_planet_impact; default Earth's equatorial radius (RE_EARTH) |
RE_EARTH
|
reset_config
|
Optional[Dict[str, Any]]
|
Configuration for environment resets |
None
|
differentiable
|
bool
|
Compile adjoint kernels for the chosen integrator so differentiable_rollout() can backpropagate through the dynamics (roughly doubles that module's one-off JIT cost; no effect on stepping performance) |
False
|
Source code in cosmic_warp/core/pipeline.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
num_envs
class-attribute
instance-attribute
Number of parallel environments.
num_bodies
class-attribute
instance-attribute
Number of bodies per environment.
num_entities
class-attribute
instance-attribute
num_envs * num_bodies.
device
class-attribute
instance-attribute
The device the simulator was built on, as given.
differentiable
class-attribute
instance-attribute
Built with adjoint kernels for differentiable_rollout().
states
class-attribute
instance-attribute
The State array, one row per entity: assign to it or write it from a kernel, never rebind it.
thruster_manager
class-attribute
instance-attribute
The ThrusterManager.
rw_manager
class-attribute
instance-attribute
The ReactionWheelManager.
reset_manager
class-attribute
instance-attribute
The ResetManager.
randomizer
class-attribute
instance-attribute
The DomainRandomizer.
termination_checker
class-attribute
instance-attribute
The TerminationChecker.
actuators
class-attribute
instance-attribute
The ActuatorManager: its commands and states arrays, one row per entity.
metadata
Provenance snapshot of the software and the constructor settings.
Returns a JSON-serializable dict with the package, warp-lang and Python versions, the device, and the constructor settings (dimensions, integrator, dt, physics flags, termination switches). It does not cover termination limits or anything configured after construction: vehicle and actuator settings, reset and randomization bounds and seeds, initial conditions and commands. Save those too (e.g. the YAML config) to reproduce a run.
Source code in cosmic_warp/core/pipeline.py
reset
reset(
*,
env_ids: Optional[list] = None,
body_ids: Optional[list] = None,
entity_ids: Optional[list] = None,
) -> None
Reset environments to initial conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_ids
|
Optional[list]
|
List of environment indices to reset (None for all) |
None
|
body_ids
|
Optional[list]
|
List of body indices to reset (None for all bodies) |
None
|
entity_ids
|
Optional[list]
|
Optional flat entity indices (env_id * num_bodies + body_id) |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if the selection covers only some bodies of an environment that is terminated. Termination is environment wide, so such an environment must be reset whole. |
Source code in cosmic_warp/core/pipeline.py
reset_to_conditions
reset_to_conditions(
*,
positions: array = None,
momenta: array = None,
angular_momenta: array = None,
quaternions: array = None,
masses: array = None,
env_ids: Optional[list] = None,
body_ids: Optional[list] = None,
entity_ids: Optional[list] = None,
) -> None
Reset environments to explicit initial conditions.
Like reset(), this also clears termination status and actuator commands/states for the selected entities; unlike reset(), the dynamic state is set to the given values instead of being sampled (and domain randomization is not applied).
The condition arrays are always indexed by flat entity id, so they keep the full [num_entities] length even when resetting a subset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
array
|
WARP array of positions [num_entities] (vec3d), None uses ResetConfig center |
None
|
momenta
|
array
|
WARP array of momenta [num_entities] (vec3d), None uses ResetConfig center |
None
|
angular_momenta
|
array
|
WARP array of angular momenta [num_entities] (vec3d), None uses ResetConfig center |
None
|
quaternions
|
array
|
WARP array of quaternions [num_entities] (quatd), normalized on write; None uses identity |
None
|
masses
|
array
|
WARP array of masses [num_entities] (float64), None uses ResetConfig center |
None
|
env_ids
|
Optional[list]
|
List of environment indices to reset (None for all) |
None
|
body_ids
|
Optional[list]
|
List of body indices to reset (None for all bodies) |
None
|
entity_ids
|
Optional[list]
|
Optional flat entity indices (env_id * num_bodies + body_id) |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if an array is not a Warp array of the listed dtype, of length num_entities, on the simulator's device; if the selection covers only some bodies of a terminated environment (as reset()), if masses is omitted and the mass_bounds center is below a body's dry mass, or if a selected mass is below its dry mass or a selected quaternion is zero or non-finite. |
Source code in cosmic_warp/core/pipeline.py
reset_terminated
Reset environments that have met termination conditions entirely on the GPU.
Source code in cosmic_warp/core/pipeline.py
step
Advance simulation by one time step.
Force/torque computation, numerical integration, per-entity termination checking and episode step counters are fused into one kernel. Multi-body status is then reduced so all bodies complete the terminating step.
Source code in cosmic_warp/core/pipeline.py
set_thrust_commands
Set thruster commands for all entities (broadcast same command).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thrust_cmd
|
list
|
Up to 8 throttles in [0, 1], one per thruster slot; omitted trailing slots are zero |
required |
Source code in cosmic_warp/core/pipeline.py
set_thrust_commands_per_entity
Set thruster commands per entity (different command for each body).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thrust_cmds
|
array
|
float64 Warp array of shape (num_envs * num_bodies, 8) on the simulator's device: one throttle in [0, 1] per thruster slot (columns of unused slots have no effect) |
required |
Source code in cosmic_warp/core/pipeline.py
set_rw_torque_commands
Set reaction wheel torque commands for all entities (broadcast same command).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rw_torque_cmds
|
list
|
Up to 4 RW torques [N*m]; omitted trailing wheels are zero |
required |
Source code in cosmic_warp/core/pipeline.py
set_rw_torque_commands_per_entity
Set reaction wheel torque commands per entity (different command for each body).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rw_torque_cmds
|
array
|
float64 Warp array of shape (num_envs * num_bodies, 4) on the simulator's device, RW torques [N*m] |
required |
Source code in cosmic_warp/core/pipeline.py
set_rw_angular_momentum_state
Set reaction wheel angular momentum state for all entities (broadcast).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rw_ang_mom
|
list
|
Up to 4 RW angular momenta [kg*m^2/s]; omitted trailing wheels are zero. Each must be finite and within the wheel's max_momentum. |
required |
Source code in cosmic_warp/core/pipeline.py
set_rw_angular_momentum_state_per_entity
Set reaction wheel angular momentum state per entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rw_ang_mom
|
array
|
float64 Warp array of shape (num_envs * num_bodies, 4) on the simulator's device [kg*m^2/s]. Each entry must be finite and within the wheel's max_momentum. |
required |
Source code in cosmic_warp/core/pipeline.py
get_positions
Get positions of all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of positions [num_envs * num_bodies, 3]. |
array
|
The returned array is a preallocated buffer reused between calls. |
Source code in cosmic_warp/core/pipeline.py
get_velocities
Get velocities of all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of velocities [num_envs * num_bodies, 3]. |
array
|
The returned array is a preallocated buffer reused between calls. |
Source code in cosmic_warp/core/pipeline.py
get_quaternions
Get attitude quaternions of all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of quaternions [num_envs * num_bodies, 4]. |
array
|
The returned array is a preallocated buffer reused between calls. |
Source code in cosmic_warp/core/pipeline.py
get_angular_velocities
Get angular velocities of all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of angular velocities [num_envs * num_bodies, 3]. |
array
|
The returned array is a preallocated buffer reused between calls. |
Source code in cosmic_warp/core/pipeline.py
get_masses
Get masses of all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of masses [num_envs * num_bodies]. |
array
|
The returned array is a preallocated buffer reused between calls. |
Source code in cosmic_warp/core/pipeline.py
get_rw_angular_momenta
Get reaction wheel angular momenta of all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of vec4d [num_envs * num_bodies]: the momentum of |
array
|
wheels 0-3 about their spin axes [kg*m^2/s]. |
array
|
The returned array is a preallocated buffer reused between calls. |
Source code in cosmic_warp/core/pipeline.py
get_terminated
Get termination status for all entities.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of TerminationStatus structs [num_envs * num_bodies] |
get_episode_steps
Get per-entity episode step counters.
Counts steps since each entity's last reset (every reset path zeroes the counter of the entities it resets), so per-environment episode lengths stay correct across partial resets and reset_terminated. Counts attempted steps even while a terminated entity's state is frozen.
Returns:
| Type | Description |
|---|---|
array
|
WARP array of int32 [num_envs * num_bodies] |
Source code in cosmic_warp/core/pipeline.py
simulate
simulate(
num_steps: int,
*,
save_states: bool = False,
save_rw_states: bool = False,
auto_reset_terminated: bool = False,
) -> Dict[str, array]
Run simulation for specified number of steps.
On CUDA devices the whole per-step kernel sequence is captured once into a CUDA graph and replayed, removing per-kernel launch overhead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_steps
|
int
|
Number of time steps to simulate |
required |
save_states
|
bool
|
Whether to save the state trajectory (positions, velocities, etc.) at every step. Off by default: it allocates num_steps x num_entities device memory, so enable it only when the time history is actually needed (e.g. plotting) |
False
|
save_rw_states
|
bool
|
Whether to save reaction wheel state trajectory |
False
|
auto_reset_terminated
|
bool
|
Whether to automatically reset terminated environments or keep them terminated |
False
|
Returns: Dictionary of WARP arrays: the recorded trajectories, and with them "time", time[k] = (k + 1) * dt, the epoch of row k (each row holds the state after its step). Empty without save_states or save_rw_states.
Source code in cosmic_warp/core/pipeline.py
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 | |
differentiable_rollout
differentiable_rollout(
num_steps: int, *, commands: list = None
) -> DifferentiableRollout
Create a differentiable rollout starting from the current state.
Gradients flow to the initial state, the vehicle/actuator parameters
(system identification) and, if given, the per-step control sequence.
Requires the simulator to be constructed with differentiable=True
(and termination checks disabled). See
:class:cosmic_warp.core.autodiff.DifferentiableRollout for the
taping/backward workflow and memory footprint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_steps
|
int
|
Number of time steps to roll out |
required |
commands
|
list
|
Optional per-step control sequence from
:func: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DifferentiableRollout |
DifferentiableRollout
|
rollout bound to this simulator |
Source code in cosmic_warp/core/pipeline.py
simulate_monte_carlo
Run simulation and return only final states (for Monte Carlo).
Memory efficient - doesn't store trajectory, only final state and statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_steps
|
int
|
Number of time steps to simulate |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, array]
|
Dictionary with final state and termination info |