Skip to content

thrusters

cosmic_warp.actuators.thrusters

Thruster models for spacecraft propulsion.

Supports configurable thruster layouts with position and direction vectors. Computes both force and torque contributions from thrust commands.

ThrusterConfig

Configuration for a thruster array (up to 8 thrusters).

Each thruster has: - Position relative to spacecraft CoM (row of positions) - Direction (unit vector in body frame, row of directions) - Maximum thrust magnitude

ThrusterWrench

Combined thruster output over one step.

force/torque are in the body frame; mdot is the total propellant mass flow rate (>= 0), zero when isp <= 0 (ideal thruster, no consumption). All three are already scaled by the step's burn fraction. mass_end is the mass at the step end, the exact solution of the constant-flow mass equation that every integrator stores: exactly the dry mass on the burnout step.

ThrusterManager

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

Manages thruster configurations for parallel entities (envs * bodies).

A new manager holds the default 6-thruster layout; set_config (or the body.actuators.thrusters YAML section) changes it.

Initialize thruster manager.

Parameters:

Name Type Description Default
num_envs int

Number of parallel environments

required
device str

Compute device

'cuda'
num_bodies int

Number of bodies per environment

1
Source code in cosmic_warp/actuators/thrusters.py
def __init__(self, num_envs: int, device: str = "cuda", num_bodies: int = 1):
    """
    Initialize thruster manager.

    Args:
        num_envs: Number of parallel environments
        device: Compute device
        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
    self.configs = wp.zeros(self.num_entities, dtype=ThrusterConfig, device=device)
    self.set_config(max_thrust=10.0, isp=0.0,
                    positions=DEFAULT_POSITIONS, directions=DEFAULT_DIRECTIONS)

set_config

set_config(
    *,
    max_thrust: float = None,
    isp: float = None,
    positions: list = None,
    directions: list = None,
    env_ids: list = None,
    body_ids: list = None,
    entity_ids: list = None,
) -> None

Set thruster configuration for all entities or a subset.

Only the fields given are checked and written; None leaves a field unchanged for the selected entities. positions and directions make up the layout and are given together (or both omitted). A new simulator starts with max_thrust 10 N, isp 0 (no propellant use) and the 6-thruster DEFAULT_POSITIONS / DEFAULT_DIRECTIONS layout.

Parameters:

Name Type Description Default
max_thrust float

Maximum thrust per thruster [N], >= 0

None
isp float

Specific impulse [s], >= 0 (0 = ideal, no propellant use)

None
positions list

Up to 8 position tuples (x, y, z) in metres

None
directions list

Direction tuples [(dx,dy,dz), ...], one per position, normalized here; (0,0,0) leaves the slot unused

None
env_ids list

List of environment indices to configure (None = all)

None
body_ids list

List of body indices to configure (None = all bodies)

None
entity_ids list

Optional flat entity indices (env_id * num_bodies + body_id)

None
Source code in cosmic_warp/actuators/thrusters.py
def set_config(
    self,
    *,
    max_thrust: float = None,
    isp: float = None,
    positions: list = None,
    directions: list = None,
    env_ids: list = None,
    body_ids: list = None,
    entity_ids: list = None
) -> None:
    """
    Set thruster configuration for all entities or a subset.

    Only the fields given are checked and written; None leaves a field
    unchanged for the selected entities. positions and directions make up
    the layout and are given together (or both omitted). A new simulator
    starts with max_thrust 10 N, isp 0 (no propellant use) and the
    6-thruster DEFAULT_POSITIONS / DEFAULT_DIRECTIONS layout.

    Args:
        max_thrust: Maximum thrust per thruster [N], >= 0
        isp: Specific impulse [s], >= 0 (0 = ideal, no propellant use)
        positions: Up to 8 position tuples (x, y, z) in metres
        directions: Direction tuples [(dx,dy,dz), ...], one per position,
            normalized here; (0,0,0) leaves the slot unused
        env_ids: List of environment indices to configure (None = all)
        body_ids: List of body indices to configure (None = all bodies)
        entity_ids: Optional flat entity indices (env_id * num_bodies + body_id)
    """
    if max_thrust is not None:
        max_thrust = validation.scalar("max_thrust", max_thrust, minimum=0.0)
    if isp is not None:
        isp = validation.scalar("isp", isp, minimum=0.0)
    if (positions is None) != (directions is None):
        raise ValueError(
            "positions and directions must be given together (the thruster "
            "layout), or both omitted to keep the current layout"
        )
    pos_mat, dir_mat, active_vec = mat83d(), mat83d(), vec8i()
    if positions is not None:
        pos_mat, dir_mat, active_vec = self._layout(positions, directions)

    entity_ids = expand_entity_ids(self.num_envs, self.num_bodies,
                                   env_ids, body_ids, entity_ids)
    if entity_ids is None:
        entity_ids = list(range(self.num_entities))
    if len(entity_ids) == 0:
        return

    entity_ids_device = wp.array(entity_ids, dtype=wp.int32, device=self.device)
    wp.launch(
        kernel=_set_thruster_config_kernel,
        dim=len(entity_ids),
        inputs=[
            self.configs,
            entity_ids_device,
            int(max_thrust is not None),
            float(max_thrust or 0.0),
            int(isp is not None),
            float(isp or 0.0),
            int(positions is not None),
            pos_mat,
            dir_mat,
            active_vec
        ],
        device=self.device
    )

thruster_wrench

thruster_wrench(
    config: ThrusterConfig,
    cmd: ActuatorCommand,
    state: State,
    vehicle: VehicleConfig,
    dt: float64,
) -> ThrusterWrench

Compute the total thruster force, torque and mass flow over one step.

In the body frame these depend only on the commands, the configuration and the propellant left at the step start, so integrators evaluate them once per step. Struct built once via its constructor (adjoint-safe).

On the step where the propellant runs out, all three are scaled by the burn fraction (the share of the step the propellant lasts at full flow), so the mass reaches the dry mass exactly at the step end. mass_end is then mass_dry itself, not a computed difference: one rounded an ulp above dry would leave a remaining flow that rounds away, so the mass would never reach dry and mass_limit = mass_dry never fire. This only stretches the mass profile in time: with a fixed thrust direction, the delta-v and propellant use are exact, and the position and attitude differ from an exact cutoff by O(dt^2), once per burnout.

Parameters:

Name Type Description Default
config ThrusterConfig

Thruster configuration

required
cmd ActuatorCommand

Actuator command with thrust values

required
state State

Spacecraft state at the step start

required
vehicle VehicleConfig

Vehicle configuration

required
dt float64

Step length [s]

required

Returns:

Type Description
ThrusterWrench

ThrusterWrench with body-frame force [N], torque [N*m], mass flow

ThrusterWrench

rate [kg/s] and step-end mass [kg]

Source code in cosmic_warp/actuators/thrusters.py
@wp.func
def thruster_wrench(
    config: ThrusterConfig,
    cmd: ActuatorCommand,
    state: State,
    vehicle: VehicleConfig,
    dt: wp.float64
) -> ThrusterWrench:
    """
    Compute the total thruster force, torque and mass flow over one step.

    In the body frame these depend only on the commands, the configuration
    and the propellant left at the step start, so integrators evaluate them
    once per step. Struct built once via its constructor (adjoint-safe).

    On the step where the propellant runs out, all three are scaled by the
    burn fraction (the share of the step the propellant lasts at full flow),
    so the mass reaches the dry mass exactly at the step end. mass_end is
    then mass_dry itself, not a computed difference: one rounded an ulp
    above dry would leave a remaining flow that rounds away, so the mass
    would never reach dry and mass_limit = mass_dry never fire. This only
    stretches the mass profile in time: with a fixed thrust direction, the
    delta-v and propellant use are exact, and the position and attitude
    differ from an exact cutoff by O(dt^2), once per burnout.

    Args:
        config: Thruster configuration
        cmd: Actuator command with thrust values
        state: Spacecraft state at the step start
        vehicle: Vehicle configuration
        dt: Step length [s]

    Returns:
        ThrusterWrench with body-frame force [N], torque [N*m], mass flow
        rate [kg/s] and step-end mass [kg]
    """
    force = wp.vec3d()
    torque = wp.vec3d()
    total_thrust = wp.float64(0.0)

    # Out of propellant, thrusters produce nothing
    propellant = state.mass - vehicle.mass_dry
    if propellant <= wp.float64(0.0):
        return ThrusterWrench(force, torque, wp.float64(0.0),
                              wp.max(state.mass, vehicle.mass_dry))

    for i in range(MAX_THRUSTERS):
        thrust_val = wp.clamp(_get_thrust_cmd(cmd, i), wp.float64(0.0), wp.float64(1.0))
        direction = _get_thruster_dir(config, i)
        position = _get_thruster_pos(config, i)

        # F = T_max * cmd * dir
        thrust_force = config.max_thrust * thrust_val * direction
        force += thrust_force
        # τ = r × F
        torque += wp.cross(position, thrust_force)
        # Unused slots make no force, so they burn no propellant either.
        # config.active is integer: no adjoint traffic on the tape.
        if config.active[i] != 0:
            total_thrust += config.max_thrust * thrust_val

    # mdot = T / (Isp * g0); isp <= 0 models an ideal thruster (no consumption)
    mdot = wp.float64(0.0)
    if config.isp > wp.float64(0.0):
        mdot = total_thrust / (config.isp * wp.float64(G0_STD))

    # On the burnout step the remaining propellant is spread over the step
    # and the mass lands on dry exactly; otherwise one rounding of m - dt*mdot
    burn = wp.float64(1.0)
    mass_end = wp.max(state.mass - dt * mdot, vehicle.mass_dry)
    if mdot * dt >= propellant:
        burn = propellant / (mdot * dt)
        mass_end = vehicle.mass_dry

    return ThrusterWrench(burn * force, burn * torque, burn * mdot, mass_end)