PerturbedEquilibrium - PERFORMANCE - Coarse-grid rzphi geometry in field reconstruction#264
Conversation
…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>
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 Only the six R/Z/φ outputs produced by
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.
|

Summary
Speeds up
FieldReconstruction.jl's R/Z/φ reconstruction by eliminating redundant and over-sampled equilibrium-geometry evaluation.reconstruct_physical_fieldsis ~2× faster (364 → 175 ms warm, DIII-D n=1,-t 6) at machine-precision accuracy.Background
Profiling showed the field-reconstruction bottleneck is equilibrium bicubic-spline evaluation, not the mode-space linear algebra. Two issues:
_compute_rzphi_modeswas called twice (ξ and b) and recomputed the identical (ψ,θ) transform matrices, which depend only on geometry.rzphibicubic only carries ~188 ψ-knots — a ~6× over-sampling.Changes
53a88e8): split into_build_rzphi_geometry(built once) +_apply_rzphi_transform(per field). Byte-identical.a2e1cd4): sample the smooth rzphi primitives (r², η-offset, J, θ-derivatives) at the 188rzphi_xsψ-knots, build a single cubicSeriesinterpolant 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
diiid_n1: 0-diff on all 30 tracked quantities.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-ψ
@threadsparallelism, 175→255 ms).🤖 Generated with Claude Code