Working with Results

Every call to solve_ivp() or solve() returns a SolveResult.

When integrating large batches, you end up with large data. Unless you’re checking the method against some known reference as a sanity check, you probably don’t need the full time-domain series across the whole integration. To cut down the output data, and speed up the solver, you can save only certain variables, at only certain times, or you can never save the time-domain signals and instead calculate summary variables (like averages, or peak times) on the GPU while integrating.

We use the Lotka-Volterra system with one observable in the examples on this page:

import numpy as np
from cubie import create_ODE_system, solve_ivp

LV = create_ODE_system(
    """
    predator_death_rate = c*y
    dx = a*x - b*x*y
    dy = -predator_death_rate + d*x*y
    """,
    parameters={"a": 0.1, "b": 0.02, "c": 0.3, "d": 0.01},
    states={"x": 0.5, "y": 0.3},
    observables=["predator_death_rate"],
)

Output types

Choose what cubie records with the output_types list. If you leave it out, cubie records ["state"].

"state"

Time-domain state trajectories.

"observables"

Time-domain observable trajectories.

"time"

The time of each saved point.

"iteration_counters"

The solver’s work between saved points: Newton iterations, Krylov iterations, accepted steps and rejected steps.

Add any summary metric name, such as "mean" or "max", to record that metric; see Summary metrics below. Set how often cubie saves a time-domain point with save_every; see Timing Parameters.

result = solve_ivp(
    LV,
    parameters={"b": np.linspace(0.01, 0.05, 10000)},
    method="ode45",
    duration=100.0,
    save_every=1.0,
    output_types=["state", "observables", "time", "iteration_counters"],
)

counters = result.iteration_counters
newton_iters = counters[:, 0, :]    # per save point, per run
krylov_iters = counters[:, 1, :]
accepted_steps = counters[:, 2, :]
rejected_steps = counters[:, 3, :]

Use the iteration counters to spot runs where an implicit algorithm is struggling.

Summary metrics

With summary metrics, you can extract statistics such as the mean, the maximum or the peaks of a variable without ever writing its full trajectory to VRAM. cubie updates each metric on the GPU as it integrates. Set how often cubie samples the variables with sample_summaries_every, which you must set whenever you ask for summaries, and the length of each summary window with summarise_every. If you leave summarise_every out, cubie calculates one summary over the whole integration. See Timing Parameters for how the two intervals fit together.

result = solve_ivp(
    LV,
    parameters={"b": np.linspace(0.01, 0.05, 10000)},
    method="ode45",
    duration=100.0,
    sample_summaries_every=0.5,
    output_types=["mean", "max"],
)

per_summary = result.as_numpy_per_summary
mean_prey = per_summary["mean"][0, 0, :]   # window 0, x, every run
peak_prey = per_summary["max"][0, 0, :]

Metric

Description

"mean"

Time-averaged value.

"std"

Standard deviation over time.

"rms"

Root-mean-square value.

"max"

Maximum value.

"min"

Minimum value.

"extrema"

Both max and min.

"max_magnitude"

Maximum absolute value.

"peaks"

Positive peaks (local maxima).

"negative_peaks"

Negative peaks (local minima).

"dxdt_max"

Maximum of the first derivative.

"dxdt_min"

Minimum of the first derivative.

"dxdt_extrema"

Both max and min of the first derivative.

"d2xdt2_max"

Maximum of the second derivative.

"d2xdt2_min"

Minimum of the second derivative.

"d2xdt2_extrema"

Both max and min of the second derivative.

"mean_std"

Mean and standard deviation combined.

"std_rms"

Standard deviation and RMS combined.

"mean_std_rms"

Mean, standard deviation, and RMS combined.

Selecting variables

By default cubie saves and summarises every state and observable. To reduce memory and improve speed, name only the variables you need with save_variables for the time-domain output and summarise_variables for the summaries:

result = solve_ivp(
    LV,
    parameters={"b": np.linspace(0.01, 0.05, 10000)},
    method="ode45",
    duration=100.0,
    save_every=1.0,
    sample_summaries_every=0.5,
    save_variables=["x"],
    summarise_variables=["x", "y"],
    output_types=["state", "mean", "max"],
)

If you leave summarise_variables out, cubie summarises the variables you save.

Result formats

Cubie returns each output in the SolveResult as a NumPy array, indexed by time (or summary window) first, then variable, then run:

state and observables

The state and observable trajectories.

time_domain_array

The states followed by the observables, in one array. Read time_domain_legend for the name of each column.

time

The time of each saved point, one column per run.

summaries_array

Every summary metric, one row per summary window. Read summaries_legend for the name of each column, such as "x mean".

iteration_counters

The solver work between saved points, as described above.

status_codes and status_messages

One status word per run, 0 for success. Look up a failed run’s index in status_messages to see the names of its status codes; see Troubleshooting.

parameter_legend and fixed_parameter_values

Cubie sorts the swept parameters by name, so the same row number can belong to different parameters in two batches. Read parameter_legend for the name of the parameter in each row, and fixed_parameter_values for the value of each parameter that cubie held constant across the batch.

You can also convert the result:

  • result.as_numpy returns a dict of the arrays and legends.

  • result.as_numpy_per_summary returns the same, with the summaries split into one array per metric.

  • result.as_pandas returns pandas DataFrames of the time-domain output and the summaries.

Device-resident results

In a normal solve, cubie copies the outputs from the GPU to the host before it returns. If you feed the outputs straight into more GPU work, pass on_device=True to Solver.solve to skip that copy. You get a DeviceSolveResult of device arrays instead:

from cubie import Solver

solver = Solver(LV, algorithm="ode45")
result = solver.solve(
    parameters={"b": np.linspace(0.01, 0.05, 10000)},
    duration=100.0,
    on_device=True,
)
result.stream.synchronize()      # the outputs are ready after this
device_state = result.state      # GPU array, no host copy made

Keep these points in mind:

  • Cubie returns from the solve without waiting for the GPU. You can queue more work on result.stream straight away; to read the outputs on the host, synchronise that stream first and then copy them, for example with result.state.copy_to_host().

  • Cubie overwrites the arrays on the next solve() on the same solver.

  • cubie skips the host-side work, so you get no output legends and no NaN masking of failed runs.

  • Keep the batch small enough to fit on the GPU in one chunk; otherwise cubie raises ValueError.

  • solve_ivp closes its solver before it returns, so it has no on_device option.