Skip to content

Impulses

impulses

Known-time, native-array state updates for explicit integration.

StateImpulse(time, update, name=None) dataclass

Replace the state at one fixed time with update(time, pre_state).

The pure callback returns the post-state in the same shape, dtype and array namespace. It must not mutate its input. Parameters may be captured as native arrays; event times are static, not differentiable inputs. name is an optional descriptive label, not a semantic identity for the callback.

Examples:

>>> import numpy as np
>>> event = StateImpulse(0.5, lambda time, state: state + 2.0)
>>> event.update(event.time, np.array([1.0, 3.0])).tolist()
[3.0, 5.0]

__post_init__()

Validate static event metadata without evaluating the callback.

Raises:

Type Description
ValueError

Time is not a finite real scalar or name is empty.

TypeError

The update is not callable or name is not a string.

Source code in src/op_engine/impulses.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __post_init__(self) -> None:
    """Validate static event metadata without evaluating the callback.

    Raises:
        ValueError: Time is not a finite real scalar or name is empty.
        TypeError: The update is not callable or name is not a string.
    """
    msg = "Impulse time must be a finite real scalar"
    if not isinstance(self.time, Real) or isinstance(self.time, bool):
        raise ValueError(msg)  # noqa: TRY004 - invalid schedule coordinate
    try:
        time = float(self.time)
    except (OverflowError, ValueError) as error:
        raise ValueError(msg) from error
    if not math.isfinite(time):
        raise ValueError(msg)
    object.__setattr__(self, "time", time)
    if not callable(self.update):
        msg = "Impulse update must be callable"
        raise TypeError(msg)
    if self.name is not None:
        if not isinstance(self.name, str):
            msg = "Impulse name must be a string or None"
            raise TypeError(msg)
        if not self.name.strip():
            msg = "Impulse name cannot be empty"
            raise ValueError(msg)