fhat. Workspace

Second-order backward differentiation formula (BDF2)

Second-order fixed steps that damp fast transients.

errorO(h2)costnonlinear solve / stepimplicit · L-stable

Curtiss & Hirschfelder 1952; Hairer & Wanner 1996

Full skillv0.1.0

Use with your agent

Use the fhat method "bdf2" (https://fhat.dev/methods/bdf2/SKILL.md) for this problem. Check first whether it applies.

1. What this solves

A model with fast decay may need a tiny explicit step even when only its slow evolution matters. BDF2 uses the last two states and a new implicit slope to advance at second order. Fast decaying modes become small within a few steps. The method is L-stable in the multistep sense given below.

2. When to use

Use a smooth stiff ODE with unwanted fast damped modes, a constant grid, and a reliable endpoint solve. Supply f(t,y)f(t,y) and a Jacobian or solver for the same residual. If you already have two states, they must lie one step apart and be accurate to O(h2)O(h^2) or better.

3. When not to use (and what to use instead)

Situation Use instead
Rough stiff decay or a robust first damping step backward-euler.
Stiff problem with changing steps, requested tolerances, or dense output radau-iia-5 or a variable-step BDF implementation such as CVODE BDF.
Persistent conservative oscillations and long-time invariants implicit-midpoint or gauss-legendre-4 for canonical Hamiltonians.
Smooth nonstiff dynamics or explicit constraints rk4 or dormand-prince-rk45 for the former, rattle for constrained conservative mechanics with constant masses.

4. Algorithm

Let y−y_- and yy be the states at t−ht-h and tt. From y0y_0 alone, start by solving the backward Euler equation y1=y0+hf(t0+h,y1)y_1=y_0+h f(t_0+h,y_1). The error of that single startup step is O(h2)O(h^2), enough for second-order global convergence.

Set a=y+(y−y−)/3a=y+(y-y_-)/3, c=2h/3c=2h/3 and t1=t+ht_1=t+h. Solve

Y=a+cf(t1,Y).Y=a+c f(t_1,Y).

Start Newton at Y=yY=y and solve [I−cJf(t1,Y)]δ=Y−a−cf(t1,Y)[I-cJ_f(t_1,Y)]\delta=Y-a-c f(t_1,Y), then set Y←Y−δY\leftarrow Y-\delta. The starter uses the same solve with a=y0a=y_0, c=hc=h. Shift the two-state history after a successful step. The reference accepts a small scaled Newton correction and reports nonfinite arithmetic, a singular matrix or exhausted iterations.

For y′=λyy'=\lambda y and z=hλz=h\lambda, the amplification roots satisfy (3−2z)ρ2−4ρ+1=0(3-2z)\rho^2-4\rho+1=0. The roots satisfy the A-stability root condition for Re⁡(z)≤0\operatorname{Re}(z)\le0, and both tend to zero as ∣z∣|z| tends to infinity there. This is the L-stability claim for BDF2, expressed in terms of both multistep roots. A small sign change in a stiff component is compatible with rapid decay.

5. Parameters

Parameter How to choose it
Fixed nonzero step hh Resolve the quantities of interest and compare at least two step refinements. The fixed-step coefficients depend on equal spacing. For changing steps use coefficients derived from the step ratio and a variable-step stability and error controller.
Newton tolerance and limit For each component, let scale be its largest absolute value in the step input, the old iterate, and the updated iterate. Require that component’s correction to be at most max(tol * scale, 4 * ulp(scale)), where ulp is floating-point spacing. The reference defaults to tol=1e-13 and allows 20 updates. Tighten the solve to check that time discretization dominates the error.

6. Applying it to your code

Keep two accepted states as immutable history, and only overwrite them after an endpoint solve converges. Use an analytic or automatically differentiated Jacobian when available. Large systems need a linear solver suited to their structure. Use a properly started new history after a discontinuity or a change of fixed grid. Add the code comment bdf2 v0.1.0, Hairer & Wanner (1996).

7. Verification

Create port_adapter.py with start(fun, jac, t, y, h) -> y1 and step(fun, jac, t, previous, current, h) -> y_next. States and returned slopes are sequences, and jac(t,y)[i][j] differentiates equation ii with respect to component jj. The time of current is t; the RHS for a BDF2 step is evaluated at t+h. Run python <skill>/checks/check_port.py from the adapter directory and show the output. Other languages can use checks/cases.json directly.

On your model check fixed-time errors under refinement, track the fast component over its first few steps, and compare with backward-euler at the same grid. Both methods can step over fast decay. The difference in order should appear on the slow component once the step resolves it.

8. Failure signatures

Symptom Likely cause
Order collapses after changing the step Fixed-grid weights were applied to unequally spaced states, or the startup history is inaccurate.
A large stiff component survives startup The starter or main step used an explicit slope, or Newton returned a poor root.
Small correction is hard to obtain Check the Jacobian, variable scales, and whether the chosen step admits a useful nearby root.

9. Files and commands

reference/bdf2.py contains the starter, step and fixed-grid iterator. uv run examples/relaxation.py compares BDF2 and backward Euler. uv run verify/check_cases.py checks second order, stiff decay, startup and BDF2 steps on mixed-scale states, and four broken ports. uv run checks/make_cases.py prints the fixture JSON.

10. Sources

Evidence

Output of the verification scripts, run on the reference implementation of version 0.1.0 when this page was built.

Passed verify/check_cases.py: Regenerates fixtures, tests order and very stiff decay against a backward Euler startup, compares global error with backward Euler, checks mixed-scale startup and BDF2 solves, and rejects four faults.
PASS implicit startup only: endpoint difference 0.00e+00
PASS history and endpoint time: endpoint difference 0.00e+00
PASS coupled Jacobian: endpoint difference 0.00e+00
PASS nonlinear implicit residual: endpoint difference 0.00e+00
PASS stiff damping with startup: endpoint difference 0.00e+00
PASS orders 2.016, 2.007, 2.004; finest BDF2/backward Euler errors 2.34e-06/7.17e-04
PASS h*lambda=-1e5, root radius 2.236e-03; y1..y5 1.00e-05, -5.00e-06, -1.50e-10, 2.50e-11, 1.25e-15
PASS mixed-scale active errors in normalized units: startup 0.00e+00, BDF2 5.55e-17
PASS explicit startup: rejected by 4 fixture(s)
PASS BDF1 repeated: rejected by 4 fixture(s)
PASS old RHS time: rejected by 3 fixture(s)
PASS swapped history: rejected by 4 fixture(s)

More methods are coming.