Skip to content

core.solvers

Numerical ODE solvers used by the simulation engine.

rk4.integrate_scalar_ode integrates a scalar ODE over an interval in equal classical fourth-order Runge-Kutta steps of at most one time constant each. Integrating the Chamber Pressure explains the limit. rk4.step_ode_system takes one classical fourth-order Runge-Kutta step for a system of variables.

machwave.core.solvers.rk4

get_stable_step_count(variable_name, initial_value, compute_derivatives, interval, **constant_arguments)

Return the number of equal Runge-Kutta steps to split interval into.

Each step spans at most _MAX_TIME_CONSTANTS_PER_STEP time constants. A longer step can overshoot equilibrium. A variable that is not settling toward equilibrium takes a single step.

Parameters:

Name Type Description Default
variable_name str

Keyword under which compute_derivatives takes the variable.

required
initial_value float

Value of the variable at the start of the interval.

required
compute_derivatives Callable

Callable returning a tuple whose first item is the time derivative of the variable.

required
interval float

Time interval to integrate over [s].

required
**constant_arguments Any

Keyword arguments passed unchanged to compute_derivatives.

{}

Returns:

Type Description
int

Number of steps, at least one.

References

Dahlquist, G., "A Special Stability Problem for Linear Multistep Methods", BIT 3, 1963, pp. 27-43.

Source code in machwave/core/solvers/rk4.py
def get_stable_step_count(
    variable_name: str,
    initial_value: float,
    compute_derivatives: Callable,
    interval: float,
    **constant_arguments: Any,
) -> int:
    """
    Return the number of equal Runge-Kutta steps to split `interval` into.

    Each step spans at most `_MAX_TIME_CONSTANTS_PER_STEP` time constants.
    A longer step can overshoot equilibrium.
    A variable that is not settling toward equilibrium takes a single step.

    Args:
        variable_name: Keyword under which `compute_derivatives` takes the variable.
        initial_value: Value of the variable at the start of the interval.
        compute_derivatives: Callable returning a tuple whose first item is the time
            derivative of the variable.
        interval: Time interval to integrate over [s].
        **constant_arguments: Keyword arguments passed unchanged to
            `compute_derivatives`.

    Returns:
        Number of steps, at least one.

    References:
        Dahlquist, G., "A Special Stability Problem for Linear Multistep Methods",
        BIT 3, 1963, pp. 27-43.
    """
    finite_difference_step = _RELATIVE_FINITE_DIFFERENCE_STEP * max(
        abs(initial_value), _MIN_FINITE_DIFFERENCE_SCALE
    )
    decay_rate = -(
        compute_derivatives(
            **{variable_name: initial_value + finite_difference_step},
            **constant_arguments,
        )[0]
        - compute_derivatives(
            **{variable_name: initial_value - finite_difference_step},
            **constant_arguments,
        )[0]
    ) / (2 * finite_difference_step)

    return max(1, math.ceil(decay_rate * interval / _MAX_TIME_CONSTANTS_PER_STEP))

integrate_scalar_ode(variable_name, initial_value, compute_derivatives, interval, **constant_arguments)

Integrate a scalar ODE over interval in equal Runge-Kutta steps.

Parameters:

Name Type Description Default
variable_name str

Keyword under which compute_derivatives takes the variable.

required
initial_value float

Value of the variable at the start of the interval.

required
compute_derivatives Callable

Callable returning a tuple whose first item is the time derivative of the variable.

required
interval float

Time interval to integrate over [s].

required
**constant_arguments Any

Keyword arguments passed unchanged to compute_derivatives.

{}

Returns:

Type Description
float

Value of the variable at the end of the interval and the number of steps

int

taken.

Source code in machwave/core/solvers/rk4.py
def integrate_scalar_ode(
    variable_name: str,
    initial_value: float,
    compute_derivatives: Callable,
    interval: float,
    **constant_arguments: Any,
) -> tuple[float, int]:
    """
    Integrate a scalar ODE over `interval` in equal Runge-Kutta steps.

    Args:
        variable_name: Keyword under which `compute_derivatives` takes the variable.
        initial_value: Value of the variable at the start of the interval.
        compute_derivatives: Callable returning a tuple whose first item is the time
            derivative of the variable.
        interval: Time interval to integrate over [s].
        **constant_arguments: Keyword arguments passed unchanged to
            `compute_derivatives`.

    Returns:
        Value of the variable at the end of the interval and the number of steps
        taken.
    """
    step_count = get_stable_step_count(
        variable_name,
        initial_value,
        compute_derivatives,
        interval,
        **constant_arguments,
    )
    value = initial_value

    for _ in range(step_count):
        value = step_ode_system(
            {variable_name: value},
            compute_derivatives,
            interval / step_count,
            **constant_arguments,
        )[0]

    return value, step_count

step_ode_system(initial_values, compute_derivatives, time_step, **constant_arguments)

Advance a system of ODEs by one classical fourth-order Runge-Kutta step.

Parameters:

Name Type Description Default
initial_values dict[str, float]

Mapping of variable name to its value at the start of the step.

required
compute_derivatives Callable

Callable taking the variables and constant_arguments as keywords and returning the time derivatives of the variables, in the order of initial_values.

required
time_step float

Step size [s].

required
**constant_arguments Any

Keyword arguments passed unchanged to compute_derivatives at all four stages. A term that depends on a variable must be evaluated inside compute_derivatives, or passed as a callable it evaluates, so that it follows the stage values.

{}

Returns:

Type Description
float

Values of the variables at the end of the step, in the order of

...

initial_values.

Source code in machwave/core/solvers/rk4.py
def step_ode_system(
    initial_values: dict[str, float],
    compute_derivatives: Callable,
    time_step: float,
    **constant_arguments: Any,
) -> tuple[float, ...]:
    """
    Advance a system of ODEs by one classical fourth-order Runge-Kutta step.

    Args:
        initial_values: Mapping of variable name to its value at the start of the step.
        compute_derivatives: Callable taking the variables and `constant_arguments`
            as keywords and returning the time derivatives of the variables, in the
            order of `initial_values`.
        time_step: Step size [s].
        **constant_arguments: Keyword arguments passed unchanged to
            `compute_derivatives` at all four stages. A term that depends on a variable
            must be evaluated inside `compute_derivatives`, or passed as a callable it
            evaluates, so that it follows the stage values.

    Returns:
        Values of the variables at the end of the step, in the order of
        `initial_values`.
    """
    stage_1_derivatives = compute_derivatives(**initial_values, **constant_arguments)
    stage_2_derivatives = compute_derivatives(
        **{
            name: value + 0.5 * stage_1_derivatives[index] * time_step
            for index, (name, value) in enumerate(initial_values.items())
        },
        **constant_arguments,
    )
    stage_3_derivatives = compute_derivatives(
        **{
            name: value + 0.5 * stage_2_derivatives[index] * time_step
            for index, (name, value) in enumerate(initial_values.items())
        },
        **constant_arguments,
    )
    stage_4_derivatives = compute_derivatives(
        **{
            name: value + stage_3_derivatives[index] * time_step
            for index, (name, value) in enumerate(initial_values.items())
        },
        **constant_arguments,
    )

    return tuple(
        value
        + (1 / 6)
        * (
            stage_1_derivatives[index]
            + 2 * (stage_2_derivatives[index] + stage_3_derivatives[index])
            + stage_4_derivatives[index]
        )
        * time_step
        for index, value in enumerate(initial_values.values())
    )