fhat. Workspace

Fourth-order Gauss–Legendre collocation

Fourth-order geometric steps for coupled Hamiltonian dynamics.

errorO(h4)costnonlinear solve / stepsymplectic · reversible · implicit · A-stable

Butcher 1964; Hairer, Lubich & Wanner 2006Also known as two-stage Gauss–Legendre, Gauss–Legendre order 4

Full skillv0.1.0

Use with your agent

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

1. What this solves

Large phase errors can obscure a long conservative trajectory. Two-stage Gauss–Legendre uses two slopes inside each step and gives fourth-order endpoint accuracy. With accurately solved stages it is reversible and symplectic on canonical Hamiltonian systems, including nonseparable ones. It preserves quadratic first integrals in exact arithmetic.

2. When to use

Use a fixed step for a smooth canonical ODE with coupled position and momentum where long-run geometry and phase accuracy matter. Supply f(t,y)f(t,y) and a Jacobian or another reliable stage solver. Strongly oscillatory modes can be linearly stable with a large step while their phase still needs resolution.

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

Situation Use instead
Second-order accuracy suffices for a canonical problem implicit-midpoint solves only one stage.
Smooth separable mechanics with cheap force evaluations stormer-verlet, or yoshida4 when fourth order is useful.
Fast decay should vanish over a large step bdf2 for an accurate fixed grid or radau-iia-5 for adaptive steps.
Exact position and velocity constraints rattle when its mechanical assumptions apply.
General nonstiff adaptive integration dormand-prince-rk45.

4. Algorithm

Let d=3/6d=\sqrt3/6. Use c=(1/2−d,1/2+d)c=(1/2-d,1/2+d), b=(1/2,1/2)b=(1/2,1/2) and

A = [[1/4,     1/4 - d],
     [1/4 + d, 1/4    ]]

Find both stage states Yi=y+h∑jAijf(t+cjh,Yj)Y_i=y+h\sum_j A_{ij} f(t+c_jh,Y_j) together. Start with both stages equal to yy. Form the stage residuals ri=Yi−y−h∑jAijf(t+cjh,Yj)r_i=Y_i-y-h\sum_j A_{ij} f(t+c_jh,Y_j) and the Newton blocks δijI−hAijJf(t+cjh,Yj)\delta_{ij}I-h A_{ij}J_f(t+c_jh,Y_j). Solve for the corrections and subtract them from the stage states. Evaluate the slopes at the converged stages and return

y1=y+h2[f(t+c1h,Y1)+f(t+c2h,Y2)].y_1=y+\tfrac h2\big[f(t+c_1h,Y_1)+f(t+c_2h,Y_2)\big].

The weights satisfy biAij+bjAji=bibjb_i A_{ij}+b_j A_{ji}=b_i b_j. This yields symplecticity in canonical coordinates and exact preservation of quadratic invariants in exact arithmetic. The two nodes and weights give a symmetric map, so reversing the sign of hh reverses the step on the same root branch. For z=hλz=h\lambda the scalar stability factor is (1+z/2+z2/12)/(1−z/2+z2/12)(1+z/2+z^2/12)/(1-z/2+z^2/12). It has modulus at most one in the left half-plane, exactly one on the imaginary axis, and tends to one in the stiff limit.

5. Parameters

Parameter How to choose it
Signed fixed step hh Choose it for phase and trajectory accuracy. Refine on a fixed time interval and check the observed order. A zero step returns the input.
Newton controls The reference allows 12 updates and bounds each stage correction component by max(tol * scale, 4 * ulp(scale)). For each component, scale is its largest absolute value in the step input, the old stage, and the updated stage. Here ulp is floating-point spacing and the default is tol=2e-14. Compare invariants after tightening the solve. A singular matrix, nonfinite values or an exhausted limit is a failed step.

6. Applying it to your code

Implement the two stages with the row and column ordering shown above. Evaluate Jacobians at each stage time and state. For larger systems reuse a suitable nonlinear and sparse linear solver. Use a prescribed fixed step and the locally continuous root when reversible geometry matters. Add the code comment gauss-legendre-4 v0.1.0, Butcher (1964).

7. Verification

Export step(fun, jac, t, y, h) -> y_next in port_adapter.py, with jac rows indexed by output component. Run python <skill>/checks/check_port.py from the adapter directory and show the output. Other languages can use checks/cases.json. The checker tests order, signed-step reversal, and two quadratic invariants of nonlinear rigid-body rotation.

On your model compare fixed-time errors with implicit-midpoint on the same grids. Track early and late peak energy errors on a long well-resolved run. Nonquadratic energy is generally approximate even with exact stage solves.

8. Failure signatures

Symptom Likely cause
Order drops to two or errors depend strongly on forcing time Check the off-diagonal tableau signs, both stage times and the final weights.
Quadratic invariants or reversibility drift Verify that both coupled stages converged, the root branch is consistent, and the force corresponds to your equations.
Rapid decay persists The A-stable formula passes far-left modes with a factor near one. Use an L-stable solver for decay.
Stable trajectory has the wrong period Reduce the step to resolve the oscillation.

9. Files and commands

reference/gauss_legendre_4.py implements the fixed step. uv run examples/canonical.py compares with an exact solution obtained using shifted momentum. uv run verify/check_cases.py checks the port including mixed-scale stages, long-time energy, comparison with midpoint, oscillatory stability, and three broken ports. uv run checks/make_cases.py prints the fixture JSON.

10. Sources

  • Butcher analyzes implicit Runge–Kutta formulas based on Gaussian quadrature and notes that the two-stage case had been proposed by Hammer and Hollingsworth (1955). Butcher (1964)
  • Hairer, Lubich and Wanner prove that the Gauss methods conserve quadratic invariants (Thm. IV.2.1), are symmetric (Thm. V.2.1) and are symplectic (Thm. VI.4.2). Geometric Numerical Integration (2006)

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 and verifies order, invariants, reversibility, comparison with midpoint, bounded long-run energy, imaginary-axis stability, and mixed-scale Newton solves. Catches broken ports.
PASS canonical: endpoint difference 0.00e+00
PASS rotation: endpoint difference 0.00e+00
PASS forced: endpoint difference 0.00e+00
PASS imaginary: endpoint difference 0.00e+00
PASS nonseparable errors 2.40e-06, 1.51e-07, 9.43e-09, 5.89e-10; orders 3.993, 3.998, 4.000
PASS two quadratic invariants over 200 steps: peak drift 2.22e-15
PASS canonical forward/backward error 5.12e-16
PASS mixed-scale active error in normalized units: 1.11e-16
PASS Gauss/midpoint endpoint errors on 10 steps: 2.40e-06/4.21e-03
PASS Gauss/midpoint endpoint errors on 80 steps: 5.89e-10/6.60e-05
PASS step: 32,000 steps, energy first/last 2.99e-06/2.99e-06
PASS midpoint: 32,000 steps, energy first/last 1.42e-03/1.42e-03
PASS at h*lambda=100j: Padé error 3.33e-16, modulus 1.000000000000
PASS midpoint: rejected by 4 fixture(s)
PASS wrong_clock: rejected by 1 fixture(s)
PASS wrong_tableau: rejected by 1 fixture(s)

More methods are coming.