Skip to content

PerturbedEquilibrium - PERFORMANCE - Coarse-grid rzphi geometry in field reconstruction#264

Merged
github-actions[bot] merged 3 commits into
developfrom
performance/field-reconstruction-geometry-cache
Jun 5, 2026
Merged

PerturbedEquilibrium - PERFORMANCE - Coarse-grid rzphi geometry in field reconstruction#264
github-actions[bot] merged 3 commits into
developfrom
performance/field-reconstruction-geometry-cache

Conversation

@logan-nc

@logan-nc logan-nc commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

Speeds up FieldReconstruction.jl's R/Z/φ reconstruction by eliminating redundant and over-sampled equilibrium-geometry evaluation. reconstruct_physical_fields is ~2× faster (364 → 175 ms warm, DIII-D n=1, -t 6) at machine-precision accuracy.

Stacked on #263 (threading). This PR is based on performance/parallel-field-reconstruction and shows only the two geometry commits; retarget to develop once #263 merges.

Background

Profiling showed the field-reconstruction bottleneck is equilibrium bicubic-spline evaluation, not the mode-space linear algebra. Two issues:

  1. _compute_rzphi_modes was called twice (ξ and b) and recomputed the identical (ψ,θ) transform matrices, which depend only on geometry.
  2. The smooth geometry was sampled at every fine u_store ψ (npsi≈1158) when the rzphi bicubic only carries ~188 ψ-knots — a ~6× over-sampling.

Changes

  • De-dup (53a88e8): split into _build_rzphi_geometry (built once) + _apply_rzphi_transform (per field). Byte-identical.
  • Coarse-grid resample (a2e1cd4): sample the smooth rzphi primitives (r², η-offset, J, θ-derivatives) at the 188 rzphi_xs ψ-knots, build a single cubic Series interpolant in ψ (same convention as _build_metric_interp), and resample onto the fine grid — replacing 2-D bicubic evals with cheap 1-D cubic evals. ψ-derivatives come from differentiating the resample spline (resampling the bicubic ψ-derivatives directly rings to ~2600% near axis/edge, as they are only C¹).

Verification

  • Accuracy: the R/Z/φ mode outputs (the only fields these functions affect) match a byte-identical baseline to ~1e-15 relative. NB: the regression harness does not track R/Z/φ, so this was validated by direct HDF5 comparison.
  • Regression diiid_n1: 0-diff on all 30 tracked quantities.
  • Threaded determinism: full gpec.h5 (239 numeric datasets) bit-identical across -t 1/2/6.

Scope notes

Reconstruction is a small fraction of total runtime (the stability ODE dominates), so the total-run speedup is modest; the win is largest for multi-n runs, scans, and repeated-response loops. I also implemented and benchmarked an area/b_n coarse-grid cache and DFT GEMM batching, but reverted both — measured net-negative (Series overhead at mthsurf=256; batching thin DFTs loses the per-ψ @threads parallelism, 175→255 ms).

🤖 Generated with Claude Code

logan-nc and others added 2 commits June 4, 2026 21:01
…metry

Split _compute_rzphi_modes into _build_rzphi_geometry (the (ψ,θ) transform
matrices t11..t33 and J_theta, which depend only on equilibrium geometry) and
_apply_rzphi_transform (the per-field IDFT → pointwise transform → forward DFT).
reconstruct_physical_fields now builds the geometry and FourierTransform once and
applies them for both ξ and b, instead of recomputing the identical geometry on
the full (npsi × mtheta) grid twice.

Byte-identical: regression diiid_n1 is 0-diff vs develop on all 30 quantities.
This also isolates the geometry build as the single hotspot for the upcoming
coarse-grid resample.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…e ψ grid

The rzphi transform geometry is smooth in ψ but was being built by evaluating
the 2-D equilibrium bicubic at every fine u_store ψ (npsi≈1158) × θ — a ~6×
over-sampling, since rzphi only carries ~mpsi+1 (≈188) ψ-knots of information.

_build_rzphi_geometry now samples the smooth rzphi primitives (r², η-offset, J,
and their θ-derivatives) at the coarse equilibrium ψ-knots × DFT θ grid, builds a
single cubic Series interpolant in ψ (same convention as _build_metric_interp),
and resamples onto the fine grid — replacing expensive 2-D bicubic evals with
cheap 1-D cubic evals. The ψ-derivatives (dr²/dψ, dη/dψ) are obtained by
differentiating the resample spline, not by resampling the bicubic ψ-derivatives
directly (those are only C¹ and ring badly near axis/edge).

Because the function-value resample reproduces the bicubic exactly on its native
knots, the result is accurate to machine precision: the R/Z/φ mode outputs match
the direct-bicubic reconstruction to ~1e-15 relative (verified against a
byte-identical baseline). Regression diiid_n1 remains 0-diff on all tracked
quantities.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Base automatically changed from performance/parallel-field-reconstruction to develop June 5, 2026 12:39
@logan-nc

logan-nc commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

Verification: field reconstruction unchanged (machine precision)

Validated directly on the reconstruction mode-space outputs (which the regression harness does not track), comparing develop (incl. #263) vs this branch, both at -t 1 so the only difference is the coarse-grid geometry resample.

Only the six R/Z/φ outputs produced by _compute_rzphi_modes change at all, and only at the ~1e-15 level; every other field is exactly bit-identical.

field max|Δ| max rel status
xi_psi, xi_psi_J, xi_theta, xi_zeta, xi_theta_reg, xi_zeta_reg 0.0 0.0 bit-identical
clebsch_psi, clebsch_psi1, clebsch_alpha 0.0 0.0 bit-identical
xi_cova_psi/theta/zeta, b_cova_psi/theta/zeta 0.0 0.0 bit-identical
b_n, xi_n, b_theta, b_zeta, b_theta_reg, b_zeta_reg, psi_area 0.0 0.0 bit-identical
xi_R 3.8e-17 1.0e-15 machine precision
xi_Z 4.9e-17 7.0e-16 machine precision
xi_phi 7.9e-17 1.6e-16 machine precision
b_R 1.6e-17 1.5e-15 machine precision
b_Z 1.7e-17 9.2e-16 machine precision
b_phi 8.7e-19 1.6e-16 machine precision

22/28 reconstruction fields exactly bit-identical; the remaining 6 (R/Z/φ) agree to ≤1.5e-15 relative. This matches expectation: the resample reproduces the bicubic on its native ψ-knots, and ψ-derivatives come from differentiating the resample spline.

Overlay below: m=2 and m=5 components of the R/Z/φ fields vs ψ — develop+#263 (solid lines) vs #264 coarse-grid (open circles). The circles land on the lines through all the resonant structure.

image

@logan-nc logan-nc self-assigned this Jun 5, 2026
@logan-nc logan-nc added performance auto-merge Automatically merge PR when CI passes labels Jun 5, 2026
@github-actions github-actions Bot enabled auto-merge (squash) June 5, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-merge Automatically merge PR when CI passes performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant