Coming from MATLAB
You can run most of a MATLAB script in cubie once you translate it into
Python. The main difference is that cubie has a single solve function,
solve_ivp, and takes the integration method as an argument, instead of
one function per algorithm.
Porting a parfor sweep
Cubie directly replaces a parfor or for loop of ODE solves. You
pass all of the input values to one solve call, and cubie solves every run
in parallel on the GPU.
mus = linspace(0.5, 4, 10000);
opts = odeset('RelTol', 1e-6, 'AbsTol', 1e-8);
results = cell(numel(mus), 1);
parfor i = 1:numel(mus)
f = @(t, y) [y(2); mus(i)*(1 - y(1)^2)*y(2) - y(1)];
[t, y] = ode45(f, 0:0.1:20, [1; 0], opts);
results{i} = y;
end
import numpy as np
from cubie import solve_ivp
def f(t, y, mu):
return [y[1], mu * (1 - y[0] ** 2) * y[1] - y[0]]
result = solve_ivp(
f,
y0={"y1": 1.0, "y2": 0.0},
parameters={"mu": np.linspace(0.5, 4.0, 10000)},
method="ode45",
duration=20.0,
save_every=0.1,
rtol=1e-6,
atol=1e-8,
output_types=["state", "time"],
)
t = result.time # read run i's time points as t[:, i]
y = result.state # read run i as y[:, :, i], like results{i}
Key changes:
Python indexing: use square brackets and count from 0 instead of 1, so you write
y[0]where MATLAB hasy(1).Parameters as arguments: cubie can’t capture variables from your script the way a MATLAB anonymous function does. Add each parameter as an argument of your function, and give its values in
parameters=when you callsolve_ivp. cubie runs once for each value in the array.Return a list: return the derivatives as a Python list,
[dy1, dy2], or a tuple, instead of a column vector. cubie does not accept a NumPy array here.Time: instead of a time span, give the total integration time as
duration, a start time ast0if it isn’t 0, and the time between saved points assave_every. Withoutsave_every, cubie saves only the first and last points.Pass the
odesetoptions as keyword arguments ofsolve_ivp: writertolandatolforRelTolandAbsTol, anddt_maxforMaxStep. See Solver Options.cubie returns every run’s results in one array, inside a
SolveResult. Index the array by time first, then state, then run; see Working with Results. Add"time"tooutput_typesto get the time points. cubie sorts the state columns by name, so name the statesy1andy2to keep MATLAB’s order.If you supply arrays for several parameters or initial values, cubie runs every combination of the supplied values by default. See Batch Inputs for the other options.
Method names
MATLAB |
cubie |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
no direct equivalent; pick a stiff method from Choosing an Algorithm |
See Choosing an Algorithm for every method cubie offers.