Repository navigation
Interpolate B and rho with Catmull-Rom splines - #90
Merged
DavidMStraub merged 1 commit intoNov 7, 2026
Merged
Conversation
Trilinear interpolation makes the derivatives of B and rho jump at every cell face, so jax.grad through trace() missed the jumps and returned wrong gradients (proximafusion#86). B and rho^2 are now interpolated with Catmull-Rom splines, which have continuous first derivatives. Across the stellarator symmetry planes the stencil reads the mirrored planes, so the derivatives stay continuous there too.
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.
Fixes #86.
The derivative of a ray with respect to its inputs needs second derivatives of B and rho. Trilinear interpolation has none inside a cell and puts all the curvature into jumps of the first derivative at the cell faces, which
jax.gradnever sees. This PR interpolates B and rho^2 with Catmull-Rom splines, which have continuous first derivatives, so the gradients come out right.d tau / d launch angle at tight tolerances:
Wall time: 1.11x in the TRAVIS step-1 scan and 1.02x in step 2. Each evaluation reads 64 nodes instead of 8, but rays need 30-40% fewer steps, mostly because the H-aware step control rejects fewer steps.
Changes
RegularGridInterpolatoris replaced byGridInterpolator: Catmull-Rom with 4 nodes per axis. Building it still does no work, so it costs nothing insidetrace, and gradients still reach the grid values.jax.jitcompile once. Insidejitthe evaluation is inlined.Accuracy
Against a reference on a 2x finer grid (W7-X) or the analytic field (tokamak), at default settings, the largest position error along the ray drops from 0.08-0.21 mm to 0.02-0.07 mm on W7-X and from 0.75-2.2 mm to below 0.04 mm on the tokamak.
TRAVIS validation, 1000 rays per step, compared on the same samples. The failed samples are the same in both versions (17 in step 1, 48 in step 2).
Alternatives
Prefiltered quadratic and cubic B-splines are more accurate inside the plasma, but they need a global prefilter of the whole grid on every
tracecall (90-680 ms on W7-X), and they gave worse W7-X rays (0.2-0.5 mm from the reference). The likely cause is the kink of B at rho = 1, where B is held at its edge value, which a global fit spreads inward.Follow-up
The right-hand side of the ray equations is now continuous, but its derivative still jumps at the cell faces. So Bosh3 (#72) is probably still the best solver. A follow-up will compare it with Tsit5 on the new interpolation.
Tests
fillat NaN nodes and outside the grid.