Skip to content

Trapezoidal conservation metric for 1D diffusion (restores strict tol, supersedes #566 loosening)#567

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:claude/mol-conservation-quadrature
Jun 4, 2026
Merged

Trapezoidal conservation metric for 1D diffusion (restores strict tol, supersedes #566 loosening)#567
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:claude/mol-conservation-quadrature

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor

Summary

This supersedes the conservation-check tolerance loosening introduced in #566 with a proper grid-weighted (trapezoidal) quadrature, so the assertion in Test 03 of test/pde_systems/MOL_1D_Linear_Diffusion.jl holds at a strict tolerance again — honestly, without any fudge.

#566 (the OrdinaryDiffEq 7 / SciMLBase 3 / RecursiveArrayTools 4 migration) fixed length(sol) for single-variable PDE solutions, which made the per-timestep mass-conservation check actually run at every saved time instead of only at i = 1. Once it ran for the whole trajectory, the crude sum(u) rectangle-rule proxy was exposed: on the edge-aligned grid (nodes sit half a step outside the domain) the rectangle rule has an O(Δx) boundary bias and drifts to ~1e-7, so #566 relaxed atol from 1e-10 to 1e-6 with an explanatory comment. That was a band-aid; this PR removes it.

What changed

  • Added a small trapezoidal_weights(xs) helper that builds composite trapezoidal weights from the actual grid spacing (each interior node gets the half-spacing on either side; the two endpoints get a single half-spacing). It is correct on the center-aligned grid, the half-step-offset edge-aligned grid, and non-uniform grids.
  • Replaced @test sum(u_approx[i, :]) ≈ 0 atol = 1e-6 with @test dot(w, u_approx[i, :]) ≈ integral0 atol = 1e-9, where integral0 = dot(w, u_approx[1, :]) is the trapezoidal integral of the initial condition. Comparing to the conserved initial integral (rather than hardcoding 0) keeps the metric meaningful for any IC.

Why the strict tolerance is honest, not a refit

The rectangle-rule drift was a quadrature artifact, not a conservation defect of the scheme. The MoL semi-discretization with homogeneous Neumann BCs conserves the trapezoidal integral exactly at the spatial level; the only residual is the time integrator's error. Measured max|∫u dx| over the trajectory:

grid Tsit5 tol = default (1e-6/1e-3) Tsit5 tol = 1e-12/1e-12
center-aligned (300 pts) 4.13e-13 6.79e-16
edge-aligned (301 pts) 5.76e-10 7.87e-16

Tightening the solver tolerance drives the residual to machine precision on both grids, confirming it is bounded by time integration. For comparison the old sum(u) rectangle rule hit 9.8e-11 (center) / 1.10e-7 (edge) at the default tolerances.

The test uses default Tsit5 tolerances, so the realistic worst case is the edge-aligned 5.76e-10. The new atol = 1e-9 sits just above that (≈1.7x headroom on a deterministic computation) and is 1000x tighter than #566's 1e-6; the center-aligned grid passes with ~4 orders of margin. Both grid configurations (discretization and discretization_edge) are exercised by the existing for disc in [...] loop.

Local test evidence

$ GROUP=Diffusion julia +1.11 -e 'using Pkg; Pkg.test()'
...
Test Summary:                                      | Pass  Total      Time
MOLFiniteDifference Interface: 1D Linear Diffusion |  289    289  10m49.1s
     Testing MethodOfLines tests passed

289/289 pass with the strict atol = 1e-9, both grid alignments, no skips/@test_broken/loosening anywhere.

Stacking

This is stacked on #566 and includes its commits (notably the length/size solution-interface fix that makes this conservation check run at every timestep). It should land with or after #566. The diff unique to this PR is a single test file.

Notes

Test 03 of MOL_1D_Linear_Diffusion checks that the diffusion equation with
homogeneous Neumann BCs conserves the spatial integral of u (here
∫₀^π cos(x) dx = 0). It previously used `sum(u)`, a rectangle rule that on the
edge-aligned grid (nodes offset half a step outside the domain) carries an
O(Δx) boundary bias and drifts to ~1e-7, which forced SciML#566 to loosen the
assertion from atol=1e-10 to atol=1e-6.

Integrate instead with composite trapezoidal weights built from the actual
grid spacing (interior nodes get the half-spacing on each side, endpoints a
single half-spacing), correct on both the center-aligned and the half-step
edge-aligned grids and on non-uniform grids. The trapezoidal integral conserves
down to the Tsit5 time-integration error, not the spatial quadrature error:
measured max|∫u dx| over the trajectory is ~4e-13 (center) / ~6e-10 (edge) at
the default Tsit5 tolerances, and both fall to ~1e-15 when the solver tolerance
is tightened to 1e-12 — confirming the residual is bounded by the time
integrator, not a real conservation defect. The assertion is therefore
restored to a strict atol=1e-9 (1000x tighter than SciML#566's 1e-6, with headroom
above the measured 6e-10 edge residual) and now compares against the initial
integral so it works regardless of the analytic value.

Verified locally with `GROUP=Diffusion`: 289/289 pass, both grid alignments.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
@ChrisRackauckas-Claude ChrisRackauckas-Claude force-pushed the claude/mol-conservation-quadrature branch from 7a3c866 to 26a5215 Compare June 4, 2026 14:42
@ChrisRackauckas ChrisRackauckas marked this pull request as ready for review June 4, 2026 14:47
@ChrisRackauckas ChrisRackauckas merged commit 6b37b7d into SciML:master Jun 4, 2026
3 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants