Skip to content

DAEs

diffsol-jax solves differential-algebraic equations of the form

\[ M(t)\, \frac{\mathrm{d}u}{\mathrm{d}t} = f(u, p, t), \]

where the mass matrix \(M\) may be singular: rows of \(M\) that are zero turn the corresponding rows of \(f\) into algebraic constraints \(0 = f_i(u, p, t)\). This is the semi-explicit, index-1 class of DAEs.

Marking Algebraic States

Write the algebraic equations as rows of rhs and mark them with algebraic=: an int (the last entries of the flat state are algebraic), a boolean mask, or a pytree of bools matching the state.

# y' = -p0 * y   and   0 = z - p1 * y
problem = ODEProblem(
    rhs=lambda x, p, t: jnp.array([-p[0] * x[0], x[1] - p[1] * x[0]]),
    state=2,
    params=2,
    algebraic=1,  # or algebraic=[False, True]
)

p = jnp.array([0.7, 2.0])
ys = solve(problem, jnp.array([1.0, p[1]]), p, t0, t1)

A DAE needs one of the implicit solvers: "bdf" (the default), "esdirk34" or "tr_bdf2".

With a pytree state, the mask follows the state rather than its flat order:

problem = ODEProblem(
    lambda y, p, t: {"y": -p[0] * y["y"], "z": y["z"] - p[1] * y["y"]},
    state={"y": jnp.zeros(()), "z": jnp.zeros(())},
    params=2,
    algebraic={"y": False, "z": True},
)

See the Robertson problem example for a classic stiff system solved with its conservation law as a constraint.

Consistent Initial Conditions

The initial state must satisfy the algebraic constraints. diffsol handles this automatically: a y0 whose algebraic entries are inconsistent is projected onto the constraints before the solve, so those entries are a starting guess. The sensitivity and adjoint systems get the matching consistent initialisation.

General Mass Matrices

For a non-diagonal or time-dependent mass matrix, pass mass=lambda v, p, t: ..., the action \(v \mapsto M(t)\,v\). It must be linear in v and must not use the parameters.

Limitations

  • Index 1 only. Higher-index DAEs must be reduced to index 1 first.
  • Linear in \(u'\). Fully implicit systems \(F(u', u, p, t) = 0\) that cannot be written with a state-independent mass matrix are not supported.