phlearn.utils

The utils subpackage contains functionality that are handy when using the other phlearn subpackages.

Functions present in phlearn.utils

time_derivative()

to_tensor()

phlearn.utils.create_video(arrays, labels, x_axis=None, file_name='animation.mp4', fps=10, dpi=100, output_format='MP4')

Creates an MP4 video or GIF showing the evaluation of a system over time, given by data in an array where the time scale is along the first dimension.

Parameters
arrayslist of ndarray

List of numpy arrays containing the data to be plotted.

labelslist of str

List of labels for the data series, used in the legend of the plot, corresponding to the list of arrays.

x_axisndarray, optional

x-axis values. If provided, data will be plotted against these x-axis values. If None, data will be plotted against the array indices.

file_namestr, optional

Name of the output animation file. Defaults to ‘animation.mp4’.

fpsint, optional

Frames per second for the animation. Defaults to 10.

dpiint, optional

Dots per inch for the plot’s resolution. Defaults to 100.

output_formatstr, optional

Output format for the animation. Can be ‘MP4’ or ‘GIF’. Defaults to ‘MP4’.

Returns
Video or None

Returns a Video object if the output_format is ‘MP4’. If output_format is ‘GIF’, the GIF is displayed and None is returned.

phlearn.utils.midpoint_method(u, un, t, f, Df, dt, M, tol=1e-12, max_iter=5)

Integrates one step of the ODE system u_t = f, from u to un, with the implicit midpoint method. Uses Newton’s method to find un.

Parameters
undarray, shape (M,)

Initial state of the ODE system.

unndarray, shape (M,)

Initial guess on the state of the ODE system after one time step.

tfloat

Time at the initial state.

fcallable

Function that evaluates the right-hand side of the ODE system, given the state u and the time t. It should return an array_like of shape (M,).

Dfcallable

Function that evaluates the Jacobian matrix of f, given the state u and the time t. It should return an array_like of shape (M, M).

dtfloat

Time step size.

Mint

Number of equations in the ODE system.

tolfloat, optional

Tolerance for the Newton iteration. The iteration stops when the Euclidean norm of the residual is less than tol. Default is 1e-12.

max_iterint, optional

Maximum number of iterations for the Newton iteration. If the iteration does not converge after max_iter iterations, it stops. Default is 5.

Returns
unarray_like, shape (M,)

Final state of the ODE system after one time step.

phlearn.utils.time_derivative(integrator, x_dot, x_start, x_end, t_start, t_end, dt, u=None, xspatial=None)

Computes the time derivative of x using the provided function x_dot.

Parameters
integratorstr or bool

If ‘euler’ or False, the time derivative at x_start, t_start is computed. If ‘midpoint’, x_start, x_end, t_start, t_end are used to compute the discretized implricit midpoint estimate of the derivative. If ‘rk4’, x_start, t_start, dt are used to compute the explicit Runge-Kutta 4 estimate. If ‘srk4’, x_start, x_end, t_start, dt are used to compute the symmetric Runge-Kutta 4 estimate.

x_dotcallable

Callable taking three arguments, x, t and u, returning the time derivative at the provided points.

x_start(…, N) ndarray
x_end(…, N) ndarray
t_startnumber or (…, 1) ndarray
t_endnumber or (…, 1) ndarray
dtnumber
u(…, N) ndarray, default None

Controlled input to provide to x_dot. Will only be used if integrator is ‘srk4’.

Returns
(…, N) ndarray

The estimated time derivatives.

Raises
ValueError

If the integrator type is not recognized.

phlearn.utils.to_tensor(x, ttype=torch.float32)

Converts the input to a torch tensor if the input is not None.

Parameters
xlistlike or None
ttypetorch type, default torch.float32
Returns
torch.tensor or None

Return converted list/array/tensor unless x is None, in which case it returns None.