Skip to content

Fix accuracy-order loss for even derivatives on non-uniform grids - #112

Open
gaoflow wants to merge 1 commit into
maroba:masterfrom
gaoflow:fix-nonuniform-even-derivative-accuracy
Open

Fix accuracy-order loss for even derivatives on non-uniform grids#112
gaoflow wants to merge 1 commit into
maroba:masterfrom
gaoflow:fix-nonuniform-even-derivative-accuracy

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 20, 2026

Copy link
Copy Markdown

On a non-uniform grid the interior central stencil for every even-order derivative is sized as if it earned the uniform-grid symmetry bonus (even-derivative central differences gain one order for free on a symmetric grid). That bonus does not exist on a non-uniform grid, so the central stencil ends up one point too small — one fewer point than findiff's own forward/backward stencils — and the interior accuracy drops from acc to acc-1. The non-uniform Laplacian is therefore only first-order accurate in the interior. Odd-order derivatives are unaffected.

The sizing lives in coefs.py in both non-uniform code paths (coefficients_non_uni and calc_coefs_non_uni_batched), so it propagates through both Diff.__call__ and the .matrix() operator path (PDEs, eigenvalue problems). The fix sizes the central stencil like the one-sided schemes, num_coef // 2 points per side, so it reaches order acc without relying on the symmetry bonus. For odd derivatives num_coef // 2 equals the previous value, so those paths are unchanged; the matrix/apply machinery is data-driven off the returned offsets and num_bndry, so no further changes are needed.

Verified three ways:

  • Polynomial exactness: an order-acc scheme reproduces polynomials of degree deriv+acc-1 exactly. On a graded grid the even-derivative interior was off by ~1e-2 before and is now at machine precision.
  • Graded-grid convergence order: d2/dx2 at acc=2 measured order 1.0 before (want 2), acc=4 order 3.0; both now reach their requested order in the interior and in the .matrix() operator.
  • Fornberg-weight cross-check: the computed weights already matched Fornberg (1988) to ~1e-11 for the chosen stencil — the weights were correct, only the stencil width was wrong; adding one point cut the pointwise error from 3e-5 to 3e-9.

Regression tests in tests/test_nonuniform_accuracy.py cover polynomial exactness and convergence order for even and odd derivatives across accuracy orders, with uniform and odd-derivative controls. Three existing tests asserted the undersized sizing directly (that the even-derivative central stencil equals the equidistant/one-sided one on a uniform grid); they are updated to the corrected sizing. Full suite passes.

The interior central stencil for even-order derivatives was sized for the
uniform-grid symmetry bonus, which does not hold on non-uniform grids, so
accuracy dropped to acc-1 (the non-uniform Laplacian was only first-order in
the interior). Size the central stencil like the one-sided schemes so it
reaches order acc, in both coefs.py code paths and the .matrix() operator.
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.

1 participant