Skip to content

Interpolate B and rho with Catmull-Rom splines - #90

Merged
DavidMStraub merged 1 commit into
proximafusion:mainfrom
DavidMStraub:feat/c1-field-interpolation
Nov 7, 2026
Merged

DavidMStraub merged 1 commit into
proximafusion:mainfrom
DavidMStraub:feat/c1-field-interpolation

Conversation

@DavidMStraub

Copy link
Copy Markdown
Collaborator

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.grad never 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:

main this PR finite differences
Tokamak, O-mode -1.117 -2.112 -2.112
W7-X, O-mode -0.134 +1.869 +1.869

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

  • RegularGridInterpolator is replaced by GridInterpolator: Catmull-Rom with 4 nodes per axis. Building it still does no work, so it costs nothing inside trace, and gradients still reach the grid values.
  • rho is interpolated as rho^2, which is smooth through the magnetic axis, where rho itself has a cone. This makes rho near the axis about 20x more accurate.
  • For stellarators, beyond phi = 0 and phi = pi/nfp the stencil reads the stellarator-mirrored planes (Z reversed, B_R sign flipped), so the derivatives are continuous across the symmetry planes. This needs a Z grid symmetric about Z = 0, as the VMEC import builds it since Make the cylindrical Z grid symmetric about Z = 0 #87.
  • Calls outside jax.jit compile once. Inside jit the 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).

Median main this PR
Step 1, position RMS 0.61 mm 0.39 mm
Step 1, rho RMS at TRAVIS points 1.3e-3 8.0e-4
Step 1, steps 162 94
Step 2, position RMS 0.36 mm 0.19 mm
Step 2, tau error 6.4% 6.0%
Step 2, absorbed power error 1.43% 1.24%
Step 2, deposition rho difference 1.9e-3 2.1e-3
Step 2, steps 128 90

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 trace call (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

  • jax.grad of tau matches finite differences on the tokamak (fails on main with -1.12 against -2.09).
  • The interpolator matches interpax Catmull-Rom away from the grid edges, reproduces the nodes and gives fill at NaN nodes and outside the grid.
  • rho is exact for the circular tokamak, where rho^2 is quadratic.
  • B and rho and their gradients have no jump at phi = 0 and phi = pi/nfp.
  • Calls outside jit reuse one compilation.
  • The X1 light-line regression test uses a launch direction close to sample 928 that still crosses |N_parallel| = 1 with the new field.

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.
@DavidMStraub
DavidMStraub merged commit b1eedde into proximafusion:main Nov 7, 2026
7 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.

Gradients through trace() are wrong when B, rho or the profiles are linearly interpolated

1 participant