Skip to content

reset

cosmic_warp.core.reset

Reset functionality for initializing/resetting simulation environments.

Random sampling draws from counter-based per-entity streams kept in device memory (cosmic_warp.core.rng), so repeated resets draw fresh values and remain valid inside CUDA graph captures.

ResetConfig

Configuration for environment reset (dynamic state only).

ResetManager

ResetManager(
    num_envs: int,
    device: str = "cuda",
    num_bodies: int = 1,
    pos_bounds: tuple = None,
    mom_bounds: tuple = None,
    ang_mom_bounds: tuple = None,
    mass_bounds: tuple = None,
    att_nominal: tuple = None,
    att_max_angle: float = None,
    seed: int = 42,
    vel_bounds: tuple = None,
)

Manages entity resets with configurable initial conditions.

Initialize reset manager.

The bounds are used for random sampling (overridden by reset_to_conditions).

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
pos_bounds tuple

(min, max) position bounds [m]

None
mom_bounds tuple

(min, max) momentum bounds [kg*m/s]

None
vel_bounds tuple

(min, max) velocity bounds [m/s], alternative to mom_bounds: momentum = sampled mass * sampled velocity, so the bounds hold for any mass. Default: circular 400 km LEO speed along +Y, +/- 1 m/s

None
ang_mom_bounds tuple

(min, max) angular momentum bounds [kg*m^2/s]

None
mass_bounds tuple

(min, max) mass bounds [kg]

None
att_nominal tuple

nominal attitude quaternion (x, y, z, w), default identity

None
att_max_angle float

attitude cone half-angle [rad] around att_nominal, in [0, pi]. Default pi = uniform over SO(3); 0 = always exactly att_nominal

None
seed int

RNG seed for random initial conditions

42
Source code in cosmic_warp/core/reset.py
def __init__(
    self,
    num_envs: int,
    device: str = "cuda",
    num_bodies: int = 1,
    pos_bounds: tuple = None,
    mom_bounds: tuple = None,
    ang_mom_bounds: tuple = None,
    mass_bounds: tuple = None,
    att_nominal: tuple = None,
    att_max_angle: float = None,
    seed: int = 42,
    vel_bounds: tuple = None
):
    """
    Initialize reset manager.

    The bounds are used for random sampling (overridden by
    reset_to_conditions).

    Args:
        num_envs: Number of parallel environments
        device: Device to run on
        num_bodies: Number of bodies per environment
        pos_bounds: (min, max) position bounds [m]
        mom_bounds: (min, max) momentum bounds [kg*m/s]
        vel_bounds: (min, max) velocity bounds [m/s], alternative to
            mom_bounds: momentum = sampled mass * sampled velocity, so the
            bounds hold for any mass. Default: circular 400 km LEO speed
            along +Y, +/- 1 m/s
        ang_mom_bounds: (min, max) angular momentum bounds [kg*m^2/s]
        mass_bounds: (min, max) mass bounds [kg]
        att_nominal: nominal attitude quaternion (x, y, z, w), default identity
        att_max_angle: attitude cone half-angle [rad] around att_nominal,
            in [0, pi]. Default pi = uniform over SO(3); 0 = always exactly
            att_nominal
        seed: RNG seed for random initial conditions
    """
    self.num_envs = num_envs
    self.num_bodies = num_bodies
    self.num_entities = num_envs * num_bodies
    self.device = device

    # Defaults: a 1 kg spacecraft near a circular 400 km LEO orbit,
    # consistent with the simulator's default gravity + J2 physics.
    r0 = RE_EARTH + 400.0e3
    v0 = (MU_EARTH / r0) ** 0.5

    if pos_bounds is None:
        pos_bounds = ((r0 - 1.0e3, -1.0e3, -1.0e3), (r0 + 1.0e3, 1.0e3, 1.0e3))

    if mom_bounds is not None and vel_bounds is not None:
        raise ValueError("provide at most one of mom_bounds / vel_bounds")
    if mom_bounds is None and vel_bounds is None:
        # Circular orbital velocity along +Y, small dispersion; a velocity
        # box keeps the orbit valid whatever mass is configured
        vel_bounds = ((-1.0, v0 - 1.0, -1.0), (1.0, v0 + 1.0, 1.0))

    if ang_mom_bounds is None:
        ang_mom_bounds = ((-0.1, -0.1, -0.1), (0.1, 0.1, 0.1))

    if mass_bounds is None:
        mass_bounds = (1.0, 1.0)

    if att_nominal is None:
        att_nominal = (0.0, 0.0, 0.0, 1.0)

    if att_max_angle is None:
        att_max_angle = math.pi

    # Per-body reset configs on device (all bodies start with the same
    # bounds; override per body via set_bounds / set_orbit_bounds).
    # Host-side copies are kept so partial overrides merge with the
    # current values. Orbit defaults match the Cartesian ones: a circular
    # 400 km LEO orbit, uniform in anomaly.
    self._bounds = [
        {
            "pos_bounds": pos_bounds,
            "mom_bounds": mom_bounds,
            "vel_bounds": vel_bounds,
            "ang_mom_bounds": ang_mom_bounds,
            "mass_bounds": mass_bounds,
            "att_nominal": att_nominal,
            "att_max_angle": att_max_angle,
            "sample_mode": 0,
            "semi_major_axis_bounds": (r0, r0),
            "eccentricity_bounds": (0.0, 0.0),
            "inclination_bounds": (0.0, 0.0),
            "raan_bounds": (0.0, 0.0),
            "arg_periapsis_bounds": (0.0, 0.0),
            "anomaly_bounds": (0.0, 2.0 * math.pi),
            "anomaly_is_mean": False,
        }
        for _ in range(num_bodies)
    ]
    self.config = wp.zeros(num_bodies, dtype=ResetConfig, device=device)
    self._update(range(num_bodies), {})

    # Vehicle configs whose dry masses floor every reset mass. The
    # simulator binds its own; a standalone manager's zeros floor nothing.
    self.vehicles = wp.zeros(self.num_entities, dtype=VehicleConfig, device=device)

    # Persistent per-entity RNG state
    self.rng_state = wp.empty(self.num_entities, dtype=RNG_STATE, device=device)
    self.reseed(seed)

    # Steps since each entity's last reset. Zeroed inside the reset
    # kernels themselves so state and counter can never drift apart;
    # incremented by the simulator each step.
    self.episode_steps = wp.zeros(self.num_entities, dtype=wp.int32, device=device)

    # Reusable selection buffers for build_mask (avoids per-call device
    # allocations)
    self._mask_buf = wp.zeros(self.num_entities, dtype=wp.bool, device=device)
    self._ids_buf = wp.zeros(self.num_entities, dtype=wp.int32, device=device)
    self._empty_mask = wp.empty(0, dtype=wp.bool, device=device)

    # Empty placeholders for omitted reset_to_conditions arrays (the
    # kernel treats shape 0 as "use the config default")
    self._empty_vec3 = wp.empty(0, dtype=wp.vec3d, device=device)
    self._empty_quat = wp.empty(0, dtype=wp.quatd, device=device)
    self._empty_f64 = wp.empty(0, dtype=wp.float64, device=device)

reseed

reseed(seed: int) -> None

Restart the per-entity RNG streams of the reset sampler at draw 0 of seed (an integer in [0, 2**64)).

Source code in cosmic_warp/core/reset.py
def reseed(self, seed: int) -> None:
    """Restart the per-entity RNG streams of the reset sampler at draw 0
    of ``seed`` (an integer in [0, 2**64))."""
    seed_states(self.rng_state, validation.seed("reset seed", seed))

set_bounds

set_bounds(
    *,
    pos_bounds: tuple = None,
    vel_bounds: tuple = None,
    mom_bounds: tuple = None,
    ang_mom_bounds: tuple = None,
    mass_bounds: tuple = None,
    att_nominal: tuple = None,
    att_max_angle: float = None,
    body_ids: list = None,
) -> None

Override reset sampling bounds for specific bodies.

Parameters:

Name Type Description Default
pos_bounds tuple

(min, max) position bounds [m] (None = keep current)

None
vel_bounds tuple

(min, max) velocity bounds [m/s]; momentum = sampled mass * sampled velocity. Setting one of mom_bounds/vel_bounds replaces the other (None = keep current)

None
mom_bounds tuple

(min, max) momentum bounds [kg*m/s] (None = keep current)

None
ang_mom_bounds tuple

(min, max) angular momentum bounds [kg*m^2/s] (None = keep current)

None
mass_bounds tuple

(min, max) mass bounds [kg] (None = keep current)

None
att_nominal tuple

nominal attitude quaternion (x, y, z, w) (None = keep current)

None
att_max_angle float

attitude cone half-angle [rad] around att_nominal, in [0, pi]; pi = uniform over SO(3), 0 = always exactly att_nominal (None = keep current)

None
body_ids list

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

None
Source code in cosmic_warp/core/reset.py
def set_bounds(
    self,
    *,
    pos_bounds: tuple = None,
    vel_bounds: tuple = None,
    mom_bounds: tuple = None,
    ang_mom_bounds: tuple = None,
    mass_bounds: tuple = None,
    att_nominal: tuple = None,
    att_max_angle: float = None,
    body_ids: list = None
) -> None:
    """
    Override reset sampling bounds for specific bodies.

    Args:
        pos_bounds: (min, max) position bounds [m] (None = keep current)
        vel_bounds: (min, max) velocity bounds [m/s]; momentum = sampled
            mass * sampled velocity. Setting one of mom_bounds/vel_bounds
            replaces the other (None = keep current)
        mom_bounds: (min, max) momentum bounds [kg*m/s] (None = keep current)
        ang_mom_bounds: (min, max) angular momentum bounds [kg*m^2/s] (None = keep current)
        mass_bounds: (min, max) mass bounds [kg] (None = keep current)
        att_nominal: nominal attitude quaternion (x, y, z, w) (None = keep current)
        att_max_angle: attitude cone half-angle [rad] around att_nominal,
            in [0, pi]; pi = uniform over SO(3), 0 = always exactly
            att_nominal (None = keep current)
        body_ids: List of body indices to configure (None = all bodies)
    """
    body_ids = (range(self.num_bodies) if body_ids is None
                else check_ids("body_ids", body_ids, self.num_bodies))
    if mom_bounds is not None and vel_bounds is not None:
        raise ValueError("provide at most one of mom_bounds / vel_bounds")
    changes = {key: value for key, value in (
        ("pos_bounds", pos_bounds), ("ang_mom_bounds", ang_mom_bounds),
        ("mass_bounds", mass_bounds), ("att_nominal", att_nominal),
        ("att_max_angle", att_max_angle),
    ) if value is not None}
    if mom_bounds is not None:
        changes.update(mom_bounds=mom_bounds, vel_bounds=None)
    if vel_bounds is not None:
        changes.update(mom_bounds=None, vel_bounds=vel_bounds)
    self._update(body_ids, changes)

set_orbit_bounds

set_orbit_bounds(
    *,
    semi_major_axis_bounds: tuple = None,
    altitude_bounds: tuple = None,
    eccentricity_bounds: tuple = None,
    inclination_bounds: tuple = None,
    raan_bounds: tuple = None,
    arg_periapsis_bounds: tuple = None,
    true_anomaly_bounds: tuple = None,
    mean_anomaly_bounds: tuple = None,
    body_ids: list = None,
) -> None

Sample orbital elements instead of Cartesian boxes for specific bodies.

Enables orbit sampling mode: on every reset each element is drawn uniformly within its bounds and converted to an ECI state entirely on device, so position and momentum are always dynamically consistent (momentum = mass * velocity). pos_bounds and mom_bounds are ignored in this mode; angular momentum, mass and attitude sampling apply unchanged. Angles in radians; the orbits are about Earth (MU_EARTH, the value the dynamics use).

Unspecified elements keep their current values (defaults: a circular 400 km LEO orbit, uniform true anomaly). Use set_sample_mode to switch a body back to Cartesian sampling.

Parameters:

Name Type Description Default
semi_major_axis_bounds tuple

(min, max) semi-major axis [m]

None
altitude_bounds tuple

(min, max) altitude above the Earth equatorial radius [m] (alternative to semi_major_axis_bounds)

None
eccentricity_bounds tuple

(min, max) eccentricity, within [0, 1)

None
inclination_bounds tuple

(min, max) inclination [rad], within [0, pi]

None
raan_bounds tuple

(min, max) right ascension of the ascending node [rad]

None
arg_periapsis_bounds tuple

(min, max) argument of periapsis [rad]

None
true_anomaly_bounds tuple

(min, max) true anomaly [rad]

None
mean_anomaly_bounds tuple

(min, max) mean anomaly [rad] — uniform in time along the orbit (alternative to true_anomaly_bounds)

None
body_ids list

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

None
Source code in cosmic_warp/core/reset.py
def set_orbit_bounds(
    self,
    *,
    semi_major_axis_bounds: tuple = None,
    altitude_bounds: tuple = None,
    eccentricity_bounds: tuple = None,
    inclination_bounds: tuple = None,
    raan_bounds: tuple = None,
    arg_periapsis_bounds: tuple = None,
    true_anomaly_bounds: tuple = None,
    mean_anomaly_bounds: tuple = None,
    body_ids: list = None
) -> None:
    """
    Sample orbital elements instead of Cartesian boxes for specific bodies.

    Enables orbit sampling mode: on every reset each element is drawn
    uniformly within its bounds and converted to an ECI state entirely
    on device, so position and momentum are always dynamically consistent
    (momentum = mass * velocity). pos_bounds and mom_bounds are ignored
    in this mode; angular momentum, mass and attitude sampling apply
    unchanged. Angles in radians; the orbits are about Earth (MU_EARTH,
    the value the dynamics use).

    Unspecified elements keep their current values (defaults: a circular
    400 km LEO orbit, uniform true anomaly). Use set_sample_mode to
    switch a body back to Cartesian sampling.

    Args:
        semi_major_axis_bounds: (min, max) semi-major axis [m]
        altitude_bounds: (min, max) altitude above the Earth equatorial
            radius [m] (alternative to semi_major_axis_bounds)
        eccentricity_bounds: (min, max) eccentricity, within [0, 1)
        inclination_bounds: (min, max) inclination [rad], within [0, pi]
        raan_bounds: (min, max) right ascension of the ascending node [rad]
        arg_periapsis_bounds: (min, max) argument of periapsis [rad]
        true_anomaly_bounds: (min, max) true anomaly [rad]
        mean_anomaly_bounds: (min, max) mean anomaly [rad] — uniform in
            time along the orbit (alternative to true_anomaly_bounds)
        body_ids: List of body indices to configure (None = all bodies)
    """
    if semi_major_axis_bounds is not None and altitude_bounds is not None:
        raise ValueError(
            "provide at most one of semi_major_axis_bounds / altitude_bounds")
    if true_anomaly_bounds is not None and mean_anomaly_bounds is not None:
        raise ValueError(
            "provide at most one of true_anomaly_bounds / mean_anomaly_bounds"
        )
    if altitude_bounds is not None:
        semi_major_axis_bounds = tuple(
            RE_EARTH + validation.scalar("altitude_bounds", v)
            for v in _pair("altitude_bounds", altitude_bounds))

    body_ids = (range(self.num_bodies) if body_ids is None
                else check_ids("body_ids", body_ids, self.num_bodies))
    changes = {key: value for key, value in (
        ("semi_major_axis_bounds", semi_major_axis_bounds),
        ("eccentricity_bounds", eccentricity_bounds),
        ("inclination_bounds", inclination_bounds), ("raan_bounds", raan_bounds),
        ("arg_periapsis_bounds", arg_periapsis_bounds),
    ) if value is not None}
    changes["sample_mode"] = 1
    if true_anomaly_bounds is not None:
        changes.update(anomaly_bounds=true_anomaly_bounds, anomaly_is_mean=False)
    if mean_anomaly_bounds is not None:
        changes.update(anomaly_bounds=mean_anomaly_bounds, anomaly_is_mean=True)
    self._update(body_ids, changes)

set_sample_mode

set_sample_mode(
    mode: str, *, body_ids: list = None
) -> None

Switch bodies between "cartesian" (box) and "orbit" (element) sampling.

Parameters:

Name Type Description Default
mode str

"cartesian" or "orbit"

required
body_ids list

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

None
Source code in cosmic_warp/core/reset.py
def set_sample_mode(self, mode: str, *, body_ids: list = None) -> None:
    """
    Switch bodies between "cartesian" (box) and "orbit" (element) sampling.

    Args:
        mode: "cartesian" or "orbit"
        body_ids: List of body indices to configure (None = all bodies)
    """
    modes = {"cartesian": 0, "orbit": 1}
    if mode not in modes:
        raise ValueError(f"mode must be one of {sorted(modes)}, got {mode!r}")
    body_ids = (range(self.num_bodies) if body_ids is None
                else check_ids("body_ids", body_ids, self.num_bodies))
    self._update(body_ids, {"sample_mode": modes[mode]})

get_bounds

get_bounds(body_id: int = 0) -> dict

The reset bounds of one body, keyed by the set_bounds and set_orbit_bounds argument names, plus "sample_mode" ("cartesian" or "orbit"). The momentum box is under vel_bounds or mom_bounds, whichever is set, and the anomaly under true_anomaly_bounds or mean_anomaly_bounds; an altitude is reported as its semi_major_axis_bounds. Bounds are tuples of floats. A copy: edit with set_bounds, set_orbit_bounds and set_sample_mode.

Source code in cosmic_warp/core/reset.py
def get_bounds(self, body_id: int = 0) -> dict:
    """
    The reset bounds of one body, keyed by the set_bounds and
    set_orbit_bounds argument names, plus "sample_mode" ("cartesian" or
    "orbit"). The momentum box is under vel_bounds or mom_bounds,
    whichever is set, and the anomaly under true_anomaly_bounds or
    mean_anomaly_bounds; an altitude is reported as its
    semi_major_axis_bounds. Bounds are tuples of floats. A copy: edit
    with set_bounds, set_orbit_bounds and set_sample_mode.
    """
    (body_id,) = check_ids("body_id", [body_id], self.num_bodies)
    b = self._bounds[body_id]
    momentum = "mom_bounds" if b["vel_bounds"] is None else "vel_bounds"
    anomaly = "mean_anomaly_bounds" if b["anomaly_is_mean"] else "true_anomaly_bounds"
    return {
        "sample_mode": "orbit" if b["sample_mode"] == 1 else "cartesian",
        **{key: b[key] for key in (
            "pos_bounds", momentum, "ang_mom_bounds", "mass_bounds", "att_nominal",
            "att_max_angle", "semi_major_axis_bounds", "eccentricity_bounds",
            "inclination_bounds", "raan_bounds", "arg_periapsis_bounds")},
        anomaly: b["anomaly_bounds"],
    }

build_mask

build_mask(
    env_ids: list = None,
    body_ids: list = None,
    entity_ids: list = None,
) -> array

Fill the reusable selection mask from id selections.

Reuses preallocated buffers, so no device memory is allocated. The returned array is shared between calls: consume it before calling build_mask again.

Parameters:

Name Type Description Default
env_ids list

List of environment indices (None = no selection)

None
body_ids list

List of body indices (None = all bodies)

None
entity_ids list

Optional flat entity indices (env_id * num_bodies + body_id)

None

Returns:

Type Description
array

Bool mask array [num_entities], or None when the selection is empty

Source code in cosmic_warp/core/reset.py
def build_mask(
    self,
    env_ids: list = None,
    body_ids: list = None,
    entity_ids: list = None
) -> wp.array:
    """
    Fill the reusable selection mask from id selections.

    Reuses preallocated buffers, so no device memory is allocated. The
    returned array is shared between calls: consume it before calling
    build_mask again.

    Args:
        env_ids: List of environment indices (None = no selection)
        body_ids: List of body indices (None = all bodies)
        entity_ids: Optional flat entity indices (env_id * num_bodies + body_id)

    Returns:
        Bool mask array [num_entities], or None when the selection is empty
    """
    entity_ids = expand_entity_ids(self.num_envs, self.num_bodies, env_ids, body_ids, entity_ids)
    if entity_ids is None or len(entity_ids) == 0:
        return None

    self._mask_buf.zero_()
    ids_host = wp.array(entity_ids, dtype=wp.int32, device="cpu")
    wp.copy(self._ids_buf, ids_host, count=len(entity_ids))
    wp.launch(
        kernel=init_mask_kernel,
        dim=len(entity_ids),
        inputs=[self._mask_buf, self._ids_buf],
        device=self.device
    )
    return self._mask_buf

reset_all

reset_all(states: array(dtype=State)) -> None

Reset all environments to random initial conditions.

Parameters:

Name Type Description Default
states array(dtype=State)

WARP array of State structs to reset

required
Source code in cosmic_warp/core/reset.py
def reset_all(self, states: wp.array(dtype=State)) -> None:
    """
    Reset all environments to random initial conditions.

    Args:
        states: WARP array of State structs to reset
    """
    wp.launch(
        kernel=reset_random_kernel,
        dim=self.num_entities,
        inputs=[states, self.config, self.vehicles, self.rng_state, self.episode_steps,
                self.num_bodies],
        device=self.device
    )

reset_masked

reset_masked(
    states: array(dtype=State), mask: array(dtype=bool)
) -> None

Reset specific entities using a boolean mask.

Parameters:

Name Type Description Default
states array(dtype=State)

WARP array of State structs

required
mask array(dtype=bool)

WARP array of bools where True indicates entity to reset

required
Source code in cosmic_warp/core/reset.py
def reset_masked(self, states: wp.array(dtype=State), mask: wp.array(dtype=wp.bool)) -> None:
    """
    Reset specific entities using a boolean mask.

    Args:
        states: WARP array of State structs
        mask: WARP array of bools where True indicates entity to reset
    """
    wp.launch(
        kernel=reset_masked_kernel,
        dim=self.num_entities,
        inputs=[states, self.config, self.vehicles, self.rng_state, mask,
                self.episode_steps, self.num_bodies],
        device=self.device
    )

check_orbit_defaults

check_orbit_defaults(
    env_ids: list = None,
    body_ids: list = None,
    entity_ids: list = None,
) -> None

Reject default positions/momenta for the selected orbit-sampled bodies.

The reset_to_conditions defaults are the Cartesian bound centers, which for a body configured via set_orbit_bounds are stale leftovers rather than a state on the configured orbits.

Source code in cosmic_warp/core/reset.py
def check_orbit_defaults(
    self,
    env_ids: list = None,
    body_ids: list = None,
    entity_ids: list = None
) -> None:
    """Reject default positions/momenta for the selected orbit-sampled
    bodies.

    The reset_to_conditions defaults are the Cartesian bound centers,
    which for a body configured via set_orbit_bounds are stale leftovers
    rather than a state on the configured orbits.
    """
    ids = expand_entity_ids(self.num_envs, self.num_bodies, env_ids, body_ids, entity_ids)
    selected = (range(self.num_bodies) if ids is None
                else sorted({i % self.num_bodies for i in ids}))
    for body_id in selected:
        if self._bounds[body_id]["sample_mode"] == 1:
            raise ValueError(
                f"body {body_id} uses orbital-element sampling: "
                "reset_to_conditions requires explicit positions and "
                "momenta for orbit-mode bodies (the Cartesian bound "
                "centers used as defaults would not lie on the "
                "configured orbits)."
            )

check_conditions

check_conditions(**arrays) -> None

Check reset_to_conditions arrays on their metadata (no device read): a Warp array of the field's dtype, of length num_entities, on this manager's device. A kernel would read a short array past its end, and an array on another device cannot be launched with.

Source code in cosmic_warp/core/reset.py
def check_conditions(self, **arrays) -> None:
    """Check reset_to_conditions arrays on their metadata (no device
    read): a Warp array of the field's dtype, of length num_entities, on
    this manager's device. A kernel would read a short array past its
    end, and an array on another device cannot be launched with."""
    dtypes = {"positions": wp.vec3d, "momenta": wp.vec3d, "angular_momenta": wp.vec3d,
              "quaternions": wp.quatd, "masses": wp.float64}
    device = self.config.device
    for name, array in arrays.items():
        if array is None:
            continue
        dtype = dtypes[name]
        if not isinstance(array, wp.array) or array.dtype != dtype:
            got = (f"a Warp array of {array.dtype.__name__}"
                   if isinstance(array, wp.array) else type(array).__name__)
            raise ValueError(f"{name} must be a Warp array of {dtype.__name__}, got {got}")
        if array.shape != (self.num_entities,):
            raise ValueError(
                f"{name} must have shape (num_entities,) = ({self.num_entities},), "
                f"got {array.shape} (arrays are indexed by flat entity id, "
                f"even when resetting a subset)"
            )
        if array.device != device:
            raise ValueError(f"{name} is on {array.device}, the simulator on {device}")

reset_to_conditions

reset_to_conditions(
    states: array(dtype=State),
    positions: array = None,
    momenta: array = None,
    angular_momenta: array = None,
    quaternions: array = None,
    masses: array = None,
    mask: array = None,
) -> None

Reset entities to specific conditions (dynamic state only).

The condition arrays are always indexed by flat entity id, so they keep the full [num_entities] length even when resetting a subset. Only the arrays' metadata is checked here; sim.reset_to_conditions also checks the values, the selection and orbit-mode bodies.

Parameters:

Name Type Description Default
states array(dtype=State)

WARP array of State structs

required
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
mask array

Bool mask [num_entities] selecting the entities to reset; None resets all

None

Raises:

Type Description
ValueError

if an array is not a Warp array of the listed dtype, of length num_entities, on this manager's device.

Source code in cosmic_warp/core/reset.py
def reset_to_conditions(
    self,
    states: wp.array(dtype=State),
    positions: wp.array = None,
    momenta: wp.array = None,
    angular_momenta: wp.array = None,
    quaternions: wp.array = None,
    masses: wp.array = None,
    mask: wp.array = None
) -> None:
    """
    Reset entities to specific conditions (dynamic state only).

    The condition arrays are always indexed by flat entity id, so they
    keep the full [num_entities] length even when resetting a subset.
    Only the arrays' metadata is checked here; sim.reset_to_conditions
    also checks the values, the selection and orbit-mode bodies.

    Args:
        states: WARP array of State structs
        positions: WARP array of positions [num_entities] (vec3d), None uses ResetConfig center
        momenta: WARP array of momenta [num_entities] (vec3d), None uses ResetConfig center
        angular_momenta: WARP array of angular momenta [num_entities] (vec3d), None uses ResetConfig center
        quaternions: WARP array of quaternions [num_entities] (quatd),
            normalized on write; None uses identity
        masses: WARP array of masses [num_entities] (float64), None uses ResetConfig center
        mask: Bool mask [num_entities] selecting the entities to reset;
            None resets all

    Raises:
        ValueError: if an array is not a Warp array of the listed dtype, of
            length num_entities, on this manager's device.
    """
    self.check_conditions(positions=positions, momenta=momenta,
                          angular_momenta=angular_momenta, quaternions=quaternions,
                          masses=masses)
    if mask is None:
        mask = self._empty_mask  # empty mask = all entities

    # Handle None by passing empty arrays (kernel checks shape[0])
    if positions is None:
        positions = self._empty_vec3
    if momenta is None:
        momenta = self._empty_vec3
    if angular_momenta is None:
        angular_momenta = self._empty_vec3
    if quaternions is None:
        quaternions = self._empty_quat
    if masses is None:
        masses = self._empty_f64

    wp.launch(
        kernel=reset_to_conditions_kernel,
        dim=self.num_entities,
        inputs=[
            states,
            self.config,
            self.vehicles,
            positions,
            momenta,
            angular_momenta,
            quaternions,
            masses,
            mask,
            self.episode_steps,
            self.num_bodies
        ],
        device=self.device
    )

reset_random_kernel

reset_random_kernel(
    states: array(dtype=State),
    config: array(dtype=ResetConfig),
    vehicles: array(dtype=VehicleConfig),
    rng_state: array(dtype=RNG_STATE),
    episode_steps: array(dtype=int32),
    num_bodies: int32,
) -> None

Reset all environments with random initial conditions (dynamic state only).

Source code in cosmic_warp/core/reset.py
@wp.kernel
def reset_random_kernel(
    states: wp.array(dtype=State),
    config: wp.array(dtype=ResetConfig),
    vehicles: wp.array(dtype=VehicleConfig),
    rng_state: wp.array(dtype=RNG_STATE),
    episode_steps: wp.array(dtype=wp.int32),
    num_bodies: wp.int32
) -> None:
    """Reset all environments with random initial conditions (dynamic state only)."""
    tid = wp.tid()
    states[tid] = _sample_state(config[tid % num_bodies], vehicles[tid].mass_dry, rng_state, tid)
    episode_steps[tid] = 0

reset_masked_kernel

reset_masked_kernel(
    states: array(dtype=State),
    config: array(dtype=ResetConfig),
    vehicles: array(dtype=VehicleConfig),
    rng_state: array(dtype=RNG_STATE),
    mask: array(dtype=bool),
    episode_steps: array(dtype=int32),
    num_bodies: int32,
) -> None

Reset only masked environments (dynamic state only).

Also the device path of reset_terminated, simulate(auto_reset_terminated) and the RL VecEnv, none of which can raise: a mass_bounds minimum below the dry mass (rejected by sim.reset(), but possible after a later set_bounds) spawns at the dry mass rather than below it.

Source code in cosmic_warp/core/reset.py
@wp.kernel
def reset_masked_kernel(
    states: wp.array(dtype=State),
    config: wp.array(dtype=ResetConfig),
    vehicles: wp.array(dtype=VehicleConfig),
    rng_state: wp.array(dtype=RNG_STATE),
    mask: wp.array(dtype=wp.bool),
    episode_steps: wp.array(dtype=wp.int32),
    num_bodies: wp.int32
) -> None:
    """Reset only masked environments (dynamic state only).

    Also the device path of reset_terminated, simulate(auto_reset_terminated)
    and the RL VecEnv, none of which can raise: a mass_bounds minimum below
    the dry mass (rejected by sim.reset(), but possible after a later
    set_bounds) spawns at the dry mass rather than below it.
    """
    tid = wp.tid()

    if mask[tid]:
        states[tid] = _sample_state(config[tid % num_bodies], vehicles[tid].mass_dry,
                                    rng_state, tid)
        episode_steps[tid] = 0

reset_to_conditions_kernel

reset_to_conditions_kernel(
    states: array(dtype=State),
    config: array(dtype=ResetConfig),
    vehicles: array(dtype=VehicleConfig),
    positions: array(dtype=vec3d),
    momenta: array(dtype=vec3d),
    angular_momenta: array(dtype=vec3d),
    quaternions: array(dtype=quatd),
    masses: array(dtype=float64),
    mask: array(dtype=bool),
    episode_steps: array(dtype=int32),
    num_bodies: int32,
) -> None

Reset to specific or default conditions (dynamic state only).

An empty mask (shape 0) selects all entities.

Source code in cosmic_warp/core/reset.py
@wp.kernel
def reset_to_conditions_kernel(
    states: wp.array(dtype=State),
    config: wp.array(dtype=ResetConfig),
    vehicles: wp.array(dtype=VehicleConfig),
    positions: wp.array(dtype=wp.vec3d),
    momenta: wp.array(dtype=wp.vec3d),
    angular_momenta: wp.array(dtype=wp.vec3d),
    quaternions: wp.array(dtype=wp.quatd),
    masses: wp.array(dtype=wp.float64),
    mask: wp.array(dtype=wp.bool),
    episode_steps: wp.array(dtype=wp.int32),
    num_bodies: wp.int32
) -> None:
    """Reset to specific or default conditions (dynamic state only).

    An empty mask (shape 0) selects all entities.
    """
    tid = wp.tid()
    if mask.shape[0] > 0 and not mask[tid]:
        return
    cfg = config[tid % num_bodies]

    # Position: use provided or default to center of bounds
    if positions.shape[0] > 0:
        pos = positions[tid]
    else:
        pos = (cfg.pos_min + cfg.pos_max) * wp.float64(0.5)


    # Angular momentum: use provided or default to center of bounds
    if angular_momenta.shape[0] > 0:
        ang_mom = angular_momenta[tid]
    else:
        ang_mom = (cfg.ang_mom_min + cfg.ang_mom_max) * wp.float64(0.5)

    # Quaternion: use provided (normalized, so the first step already rotates
    # with a unit quaternion) or default to identity
    if quaternions.shape[0] > 0:
        quat = wp.normalize(quaternions[tid])
    else:
        quat = wp.quat_identity(dtype=wp.float64)

    # Mass: use provided or default to center of bounds, floored at the dry
    # mass (sim.reset_to_conditions rejects either below it beforehand)
    if masses.shape[0] > 0:
        mass = masses[tid]
    else:
        mass = (cfg.mass_min + cfg.mass_max) * wp.float64(0.5)
    mass = wp.max(mass, vehicles[tid].mass_dry)

    # Momentum: use provided or default to center of bounds (center
    # velocity times mass for velocity bounds)
    if momenta.shape[0] > 0:
        mom = momenta[tid]
    else:
        mom = (cfg.mom_min + cfg.mom_max) * wp.float64(0.5)
        if cfg.mom_is_velocity == 1:
            mom = mom * mass

    # Set state (only dynamic variables)
    states[tid].pos = pos
    states[tid].mom = mom
    states[tid].quat = quat
    states[tid].ang_mom = ang_mom
    states[tid].mass = mass
    episode_steps[tid] = 0