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 |
|---|---|
|
Time-averaged value. |
|
Standard deviation over time. |
|
Root-mean-square value. |
|
Maximum value. |
|
Minimum value. |
|
Both max and min. |
|
Maximum absolute value. |
|
Positive peaks (local maxima). |
|
Negative peaks (local minima). |
|
Maximum of the first derivative. |
|
Minimum of the first derivative. |
|
Both max and min of the first derivative. |
|
Maximum of the second derivative. |
|
Minimum of the second derivative. |
|
Both max and min of the second derivative. |
|
Mean and standard deviation combined. |
|
Standard deviation and RMS combined. |
|
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:
stateandobservablesThe state and observable trajectories.
time_domain_arrayThe states followed by the observables, in one array. Read
time_domain_legendfor the name of each column.timeThe time of each saved point, one column per run.
summaries_arrayEvery summary metric, one row per summary window. Read
summaries_legendfor the name of each column, such as"x mean".iteration_countersThe solver work between saved points, as described above.
status_codesandstatus_messagesOne status word per run,
0for success. Look up a failed run’s index instatus_messagesto see the names of its status codes; see Troubleshooting.parameter_legendandfixed_parameter_valuesCubie sorts the swept parameters by name, so the same row number can belong to different parameters in two batches. Read
parameter_legendfor the name of the parameter in each row, andfixed_parameter_valuesfor the value of each parameter that cubie held constant across the batch.
You can also convert the result:
result.as_numpyreturns a dict of the arrays and legends.result.as_numpy_per_summaryreturns the same, with the summaries split into one array per metric.result.as_pandasreturns 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.streamstraight away; to read the outputs on the host, synchronise that stream first and then copy them, for example withresult.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_ivpcloses its solver before it returns, so it has noon_deviceoption.