Fix accuracy-order loss for even derivatives on non-uniform grids - #112
Open
gaoflow wants to merge 1 commit into
Open
Fix accuracy-order loss for even derivatives on non-uniform grids#112gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 fromacctoacc-1. The non-uniform Laplacian is therefore only first-order accurate in the interior. Odd-order derivatives are unaffected.The sizing lives in
coefs.pyin both non-uniform code paths (coefficients_non_uniandcalc_coefs_non_uni_batched), so it propagates through bothDiff.__call__and the.matrix()operator path (PDEs, eigenvalue problems). The fix sizes the central stencil like the one-sided schemes,num_coef // 2points per side, so it reaches orderaccwithout relying on the symmetry bonus. For odd derivativesnum_coef // 2equals the previous value, so those paths are unchanged; the matrix/apply machinery is data-driven off the returned offsets andnum_bndry, so no further changes are needed.Verified three ways:
accscheme reproduces polynomials of degreederiv+acc-1exactly. On a graded grid the even-derivative interior was off by ~1e-2 before and is now at machine precision.d2/dx2atacc=2measured order 1.0 before (want 2),acc=4order 3.0; both now reach their requested order in the interior and in the.matrix()operator.Regression tests in
tests/test_nonuniform_accuracy.pycover 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.