Skip to content

Migrate interpolation backend to FastInterpolations.jl - #307

Merged
jmcclena merged 13 commits into
masterfrom
migrate/fast_interp
Jun 23, 2026
Merged

Migrate interpolation backend to FastInterpolations.jl#307
jmcclena merged 13 commits into
masterfrom
migrate/fast_interp

Conversation

@mgyoo86

@mgyoo86 mgyoo86 commented Jun 23, 2026

Copy link
Copy Markdown
Member

Summary

Replace Interpolations.jl and DataInterpolations.jl with FastInterpolations.jl for all interpolation in IMAS, and drop both legacy dependencies from Project.toml.

Behavior is preserved within interpolation tolerance; performance improves across the board with no regressions.

Performance

@benchmark (BenchmarkTools, minimum time), master worktree vs this branch, identical public-API calls
on the bundled samples (D3D 65×65, omas 17×17). All comparisons are pure backend swaps — same algorithm,
only the interpolation library differs.

Interpolation primitives

operation master branch speedup
cubic_interp1d build 2.4 µs 0.47 µs 5.1×
linear_interp1d build+eval ×400 7.0 µs 2.1 µs 3.4×
PSI eval (50×50 grid) 59 µs 22 µs 2.7×
Br_Bz (∇ψ) ×200 7.7 µs 3.0 µs 2.6×
avgZ ×1000 456 µs 261 µs 1.8×
ψ_interpolant build 68 µs 47 µs 1.4×
ρ_interpolant build+eval 76 µs 52 µs 1.5×

Real IMAS routines

routine master branch speedup
Jtor_2_Jpar (currents) 25.5 µs 8.0 µs 3.2×
line_average 5.8 µs 2.7 µs 2.1×
trace_surfaces 377 µs 324 µs 1.17×
flux_surfaces 4.72 ms 4.23 ms 1.12×

Notes

  • Extrapolation semantics mapped: Interpolations.Line()ExtendExtrap(), Flat()ClampExtrap().
  • FastInterpolations' default CubicFit boundary condition reproduces cubic polynomials exactly; on the
    production equilibrium grids the migrated results match the previous ones within the test tolerances.

mgyoo86 added 11 commits June 22, 2026 10:10
…tions.jl

Migrate the last Interpolations.jl call sites to FastInterpolations and
remove the dependency:

- ρ_interpolant: cubic_spline_interpolation → FI.cubic_interp (ExtendExtrap)
- avgZinterpolator: Gridded(Linear)+Flat → FI.linear_interp (ClampExtrap)
- q_95 expression: linear_interpolation → FI.linear_interp
- add _deduplicate_knots! (math.jl) replacing Interpolations.deduplicate_knots!
  for the SOL P(r)/r(P) interpolants
- drop Interpolations from Project.toml and src/physics.jl

Extend the interpolation characterization tests with avgZ Flat-extrapolation
and _deduplicate_knots! contract checks. Full interpolation suite green.
… FastInterpolations

Migrate the last direct DataInterpolations calls to FI and remove the dependency:

- sol.jl: CubicSpline → FI.cubic_interp (find_levels_from_P r(ψ))
- sources.jl: LinearInterpolation+Constant → FI.linear_interp+ClampExtrap (total_sources)
- signal.jl: linear up-sampling → FI.linear_interp (smooth_by_convolution)
- remove DataInterpolations import (physics.jl) and dependency (Project.toml)

Extend test/runtests_interp1d.jl with smooth_by_convolution, total_sources, and
find_levels_from_P characterization tests covering these sites.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates IMAS’s interpolation backend from Interpolations.jl/DataInterpolations.jl to FastInterpolations.jl, updating core physics workflows and adding characterization tests to preserve behavior within tolerances while improving performance.

Changes:

  • Replace 1D/2D interpolation construction, evaluation, gradient, and extrapolation semantics with FastInterpolations equivalents across physics and expressions code.
  • Update helper APIs (cubic_interp1d, linear_interp1d) and remove legacy interpolation dependencies from Project.toml.
  • Add new test suites covering interpolation-heavy workflows and 1D interpolation helpers, and wire them into runtests.jl.

Reviewed changes

Copilot reviewed 19 out of 20 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/runtests.jl Adds new interpolation-focused test files to the default test run.
test/runtests_interpolations.jl New characterization/regression tests for interpolation-backed equilibrium/profile workflows.
test/runtests_interp1d.jl New characterization/regression tests for IMAS 1D interpolation helpers and consumers.
src/signal.jl Replaces DataInterpolations-based upsampling with FastInterpolations linear interpolation.
src/physics/thermal_loads.jl Switches cubic interpolation usage to the updated one-shot helper API.
src/physics/sources.jl Replaces DataInterpolations constant extrapolation with FastInterpolations clamp extrapolation.
src/physics/sol.jl Updates interpolant types and knot deduplication; adjusts interpolant evaluation style.
src/physics/profiles.jl Replaces Interpolations gridded linear+flat extrapolation with FastInterpolations linear+clamp.
src/physics/magnetics.jl Updates interpolant argument types to FastInterpolations’ abstract interpolant.
src/physics/interferometer.jl Updates segment interpolation evaluation to use FastInterpolations-backed interpolants.
src/physics/fluxsurfaces.jl Updates interpolant argument types and uses new one-shot cubic interpolation call site(s).
src/physics/fields.jl Replaces Interpolations.gradient with FastInterpolations gradient API; updates types/imports.
src/physics/equilibrium.jl Replaces cubic spline interpolant builders with FastInterpolations cubic interpolation + extrapolation.
src/physics/currents.jl Switches cubic interpolation call sites to the updated one-shot helper API.
src/physics.jl Drops imports of legacy interpolation packages.
src/math.jl Re-implements cubic_interp1d / linear_interp1d on FastInterpolations and adds one-shot overloads; adds _deduplicate_knots!.
src/IMAS.jl Imports FastInterpolations as FI for module-wide use.
src/expressions/onetime.jl Updates cubic interpolation usage to new helper overloads (vectorized evaluation).
src/expressions/dynamic.jl Replaces Interpolations linear interpolation with FastInterpolations.
Project.toml Removes DataInterpolations/Interpolations deps and adds FastInterpolations (with compat entry).
Comments suppressed due to low confidence (1)

src/physics/magnetics.jl:57

  • PSI_interpolant.(r, z) is broadcasting over scalar r/z, which adds unnecessary broadcast overhead; the interpolant is already callable on scalars.
        r = sum(pos.r for pos in loop.position) / length(loop.position)
        z = sum(pos.z for pos in loop.position) / length(loop.position)
        psi = PSI_interpolant.(r, z)
        set_time_array(loop.flux, :data, time0, psi)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/physics/sol.jl
Comment thread src/physics/magnetics.jl
Comment thread test/runtests_interp1d.jl Outdated
Comment thread test/runtests_interp1d.jl Outdated
@mgyoo86 mgyoo86 changed the title Migrate/fast interp Migrate interpolation backend to FastInterpolations.jl Jun 23, 2026

@jmcclena jmcclena left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. If the FUSE regressions pass, let's merge it.

…ent seam

Downstream packages (FUSE/FRESCO) pass Interpolations.jl interpolants into
IMAS, which broke after the FastInterpolations migration restricted args to
::FI.AbstractInterpolant (MethodError in find_magnetic_axis and
trace_simple_surfaces!->Br_Bz; FUSE CI run 28040722813).

Restore compatibility without re-adding Interpolations as a dependency:
- drop ::FI.AbstractInterpolant on PSI_interpolant args in
  fields/fluxsurfaces/sol/magnetics (scalar eval itp(r,z) is backend-agnostic)
- funnel the only FI-specific coupling (gradient) through _psi_gradient in
  fields.jl: FI -> FI.gradient (analytic, FI numerics unchanged);
  otherwise -> parentmodule(typeof(itp)).gradient (resolves the owning module
  from the object, so no Interpolations dependency is declared)
- Br_Bz is the single gradient call site; Bp/trace_*/field! route through it

fluxsurfaces_cubic.jl internal helpers left as-is (always FI, FI.value_gradient).

Verified: runtests_interpolations.jl golden values green; smoke test with a real
Interpolations cubic interpolant passes find_magnetic_axis and Br_Bz.
@mgyoo86

mgyoo86 commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

@jmcclena
The FUSE regression tests pass with this IMAS branch.
(https://github.com/ProjectTorreyPines/FUSE.jl/actions/runs/28040722813)

It seems this PR is safe to merge.
Before merging this PR, could you please merge #306 first?
(This PR is built on top of PR #306)

Thanks!

Base automatically changed from compat/optim to master June 23, 2026 20:44
@jmcclena
jmcclena merged commit bf561c1 into master Jun 23, 2026
1 check passed
@jmcclena
jmcclena deleted the migrate/fast_interp branch June 23, 2026 20:44
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.

3 participants