API

class pydiffsol.AdjointCheckpoint

Bases: object

class pydiffsol.InitialConditionSolverOptions

Bases: object

armijo_constant
max_linear_solver_setups
max_linesearch_iterations
max_newton_iterations
step_reduction_factor
use_linesearch
class pydiffsol.JitBackendType

Bases: object

classmethod all()
classmethod from_str(value)
llvm = JitBackendType.llvm
class pydiffsol.LinearSolverType

Bases: object

Enumerates the possible linear solver types for diffsol

Attr default:

use the solver’s default linear solver choice, typically LU

Attr lu:

use LU decomposition linear solver (dense or sparse as appropriate)

Attr klu:

use KLU sparse linear solver

classmethod all()
classmethod from_str(value)
default = LinearSolverType.default
klu = LinearSolverType.klu
lu = LinearSolverType.lu
class pydiffsol.MatrixType

Bases: object

Enumerates the possible matrix types for diffsol

Attr nalgebra_dense:

dense matrix using nalgebra crate (https://nalgebra.rs/)

Attr faer_dense:

dense matrix using faer crate (https://faer.veganb.tw/)

Attr faer_sparse:

sparse matrix using faer crate (https://faer.veganb.tw/)

classmethod all()

Get all available matrix types :return: list of MatrixType

classmethod from_str(value)

Create MatrixType from string name :param name: string representation of matrix type :return: valid MatrixType or exception if name is invalid

faer_dense = MatrixType.faer_dense
faer_sparse = MatrixType.faer_sparse
nalgebra_dense = MatrixType.nalgebra_dense
class pydiffsol.Ode(code, jit_backend=None, scalar_type=Ellipsis, matrix_type=Ellipsis, linear_solver=Ellipsis, ode_solver=Ellipsis)

Bases: object

has_stop()

Check if the diffsl code has a stop event defined.

rhs(params, t, y)

Evaluate the right-hand side function at time t and state y.

rhs_jac_mul(params, t, y, v)

Evaluate the right-hand side Jacobian-vector product Jv at time t and state y.

solve(params, final_time)

Solve the problem up to time final_time. Stop/reset events defined in the DiffSL code are handled automatically.

The number of params must match the expected params in the diffsl code.

Parameters:
  • params (numpy.ndarray) – 1D array of solver parameters

  • final_time (float) – end time of solver

Returns:

Solution object with fields ys and ts

Return type:

Solution

Example

>>> print(ode.solve(np.array([]), 0.5))
solve_adjoint_bkwd(solution, checkpoint, dgdu_eval)

Solve the discrete adjoint backward pass using a prior forward adjoint checkpoint and the gradient of a scalar objective with respect to model outputs at each saved evaluation time.

Parameters:

dgdu_eval (numpy.ndarray) – 2D array, shape (nout, len(solution.ts)); float32 or float64

Returns:

parameter gradient matrix

Return type:

numpy.ndarray

solve_adjoint_fwd(params, t_eval)

Solve the forward problem at t_eval and retain checkpoint data for a later discrete adjoint backward pass.

Returns:

tuple of (solution, checkpoint)

Return type:

tuple[Solution, AdjointCheckpoint]

solve_continuous_adjoint(params, final_time)

Solve the continuous adjoint problem for the integral of the model output from the initial time to final_time.

Returns:

tuple of (integral, gradient)

Return type:

tuple[numpy.ndarray, numpy.ndarray]

solve_dense(params, t_eval)

Solve the problem up to time t_eval[t_eval.len()-1]. Returns a Solution object with values at timepoints given by t_eval. Stop/reset events defined in the DiffSL code are handled automatically.

The number of params must match the expected params in the diffsl code.

Parameters:
  • params (numpy.ndarray) – 1D array of solver parameters

  • t_eval (numpy.ndarray) – 1D array of solver times

Returns:

Solution object with fields ys and ts

Return type:

Solution

solve_fwd_sens(params, t_eval)

Solve the problem up to time t_eval[t_eval.len()-1]. Returns a Solution object with values at t_eval and sensitivity arrays at the same timepoints. Stop/reset events defined in the DiffSL code are handled automatically.

The number of params must match the expected params in the diffsl code.

Parameters:
  • params (numpy.ndarray) – 1D array of solver parameters

  • t_eval (numpy.ndarray) – 1D array of solver times

Returns:

Solution object with fields ys, ts, and sens

Return type:

Solution

y0(params)

Get the initial condition vector y0 as a 1D numpy array.

atol

Absolute tolerance for the solver, default 1e-6. Governs the error as the solution goes to zero.

code

Get the DiffSl code used to generate this ODE.

h0

Initial step size for the solver, default 1.0.

ic_options
integrate_out

Whether to integrate output equations alongside state equations.

jit_backend

JIT backend used for compilation. This is fixed after construction.

linear_solver

Linear solver type used in the ODE solver. Set to default to use the solver’s default choice, which is typically an LU solver.

matrix_type

Matrix type used in the ODE solver. This is fixed after construction.

nout

Get the number of outputs in the ODE system. If there is no out tensor, this is the number of states.

nparams

Get the number of parameters expected by the diffsl code.

nstates

Get the number of states in the ODE system.

ode_solver

ODE solver method, default Bdf (backward differentiation formula).

options
out_atol

Absolute tolerance for integrated output equations.

out_rtol

Relative tolerance for integrated output equations.

param_atol

Absolute tolerance for adjoint parameter gradient equations.

param_rtol

Relative tolerance for adjoint parameter gradient equations.

rtol

Relative tolerance for the solver, default 1e-6. Governs the error relative to the solution size.

scalar_type

Scalar type used in the ODE solver. This is fixed after construction.

sens_atol

Absolute tolerance for forward sensitivity or adjoint equations.

sens_rtol

Relative tolerance for forward sensitivity or adjoint equations.

t0

Initial time for the solver, default 0.0.

class pydiffsol.OdeSolverOptions

Bases: object

max_error_test_failures
max_nonlinear_solver_iterations
min_timestep
threshold_to_update_jacobian
threshold_to_update_rhs_jacobian
update_jacobian_after_steps
update_rhs_jacobian_after_steps
class pydiffsol.OdeSolverType

Bases: object

classmethod all()
classmethod from_str(value)
bdf = OdeSolverType.bdf
esdirk34 = OdeSolverType.esdirk34
tr_bdf2 = OdeSolverType.tr_bdf2
tsit45 = OdeSolverType.tsit45
class pydiffsol.ScalarType

Bases: object

classmethod all()
classmethod from_str(value)
f32 = ScalarType.f32
f64 = ScalarType.f64
class pydiffsol.Solution

Bases: object

sens
ts
ys
pydiffsol.default_enabled_jit_backend()
pydiffsol.diffsl_version()
pydiffsol.diffsol_c_version()
pydiffsol.diffsol_version()
pydiffsol.is_klu_available()

Indicate whether KLU functions are available.

pydiffsol.is_sens_available()

Indicate whether sensitivity analysis is available.

pydiffsol.version()

Get version of this pydiffsol module