Skip to content

Dual-typed dd and implicit derivatives for forward-mode AD; include derived-density ions in the thermal pressure - #326

Merged
tomneiser merged 17 commits into
masterfrom
forward-ad-plasma
Nov 15, 2026
Merged

tomneiser merged 17 commits into
masterfrom
forward-ad-plasma

Conversation

@tomneiser

@tomneiser tomneiser commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Dual-typed data dictionaries and the physics helpers that FUSE's whole-plasma workflows need for forward-mode AD (ForwardDiff), plus one fix to the thermal pressure that also affects Float64 results.

Requires IMASdd 8.6.4 (ProjectTorreyPines/IMASdd.jl#114): the new tests rely on its handling of expressions that return plain floats in a non-Float64 data structure. The compat floor is raised in this PR.

Dual-typed dd (new src/dual.jl)

  • retype converts a dd to another element type, keeping global_time and the auxiliary data. It also resolves an ambiguity with ForwardDiff's convert.
  • seed, value_dd, partials_dd and replace_values! seed inputs and split values from derivatives.
  • InputsTag / InputsDual{N}: an outer tag ordered before every other ForwardDiff tag, so that derivatives taken internally nest inside it.
  • JSON/HDF5 output writes one values file plus one file per partial.

Implicit derivatives

Searches still run on value parts. Their derivatives come from the conditions that define the solution (implicit function theorem):

  • implicit_solution / implicit_argmin, used by the core–edge blends for H and L mode.
  • Flux surfaces (new src/physics/fluxsurfaces_dual.jl): contour points, magnetic axis, X-points, surface extrema and the level of the last closed surface. Their value parts equal the Float64 result; the flux-surface averages then come from the existing code, factored out as _flux_surfaces_from_traced!.
  • The last closed surface takes the derivative of the X-point flux for a diverted plasma. Its level is found by a search on the contouring grid, so inside one grid cell it follows a node value, while over any finite change of the inputs it follows the X-point flux.

Element-type generic helpers

  • Sources and beams, line radiation, currents, fields, arc length, shapes, the psi interpolant grid and total_power_time.
  • Finite derivatives where the formula has none: a zero-safe sqrt for quantities that vanish on axis, the clamped ends of the radiation fit, the trapped fraction at the field maximum, and beam-power smoothing over an infinite time step.
  • j_ohmic_steady_state solves its affine condition in closed form for dual numbers.

Fix that changes Float64 results

pressure_thermal(ions) skipped every ion without a stored density_thermal. After quasi-neutrality is enforced on an ion without fast density, the result is stored in density and density_thermal is an expression. That ion therefore dropped out of pressure_ion_total, pressure_thermal, pressure and energy_thermal, which can be most of the thermal energy when it is the main ion. Ions with a stored total density are now included. New test/runtests_profiles.jl.

Tests

New test/runtests_dual.jl: dual-typed dd helpers, implicit derivatives, pedestal blends and flux surfaces against finite differences. Pkg.test() passes locally (Julia 1.11.7) with the IMASdd branch, after merging master.

Part of a set with ProjectTorreyPines/FUSE.jl#1226, which tests the whole stack in its CI.

🤖 Generated with Claude Code

tomneiser and others added 15 commits November 9, 2026 21:42
…it, I/O

- dual.jl: retype (element-type conversion that keeps global_time and aux),
  value_dd, partials_dd, replace_values!, seed, InputsTag/InputsDual (outer
  tag ordered before all other ForwardDiff tags), JSON/HDF5 output as one
  values file plus one file per partial
- resolve the convert(::Type{<:Dual}, ::IDS) ambiguity with ForwardDiff
- line radiation: finite derivative at the clamped ends of the Chebyshev fit
- sawteeth_source!: inversion radius in the element type of the dd
- total_power_time, inscribed wall polygon: element-type generic

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…dy-state ohmic amplitude, lenient replace_values!

- sqrt0: the grid volume, area and psi expressions take the square root of
  quantities that vanish on axis, where the derivative of sqrt is infinite
- j_ohmic_steady_state: the condition on the total current is affine in the
  ohmic amplitude; with dual numbers it is solved in closed form
- replace_values! skips leaves that hold data on only one side

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ches

- implicit_solution / implicit_argmin: derivative of a root or minimiser that
  was found on value parts, from the implicit function theorem
- blend_core_edge_Hmode: derivative of the exponents for the three kinds of
  minimum of its cost (vanishing mismatch, exponent at zero, smooth)
- blend_core_edge_Lmode: derivative of the exponent from the signed mismatch
- pedestal_search_tolerance: the tolerances of these searches, defaults unchanged

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The tracing runs on value parts; contour points, magnetic axis, X-points, surface
extrema and the level of the last closed surface are given the displacement that
follows from the conditions that define them (implicit function theorem), and the
flux-surface averages are computed from them by the existing code, now factored
out as _flux_surfaces_from_traced!. Value parts equal the Float64 result.

Also: arc_length and the psi interpolant grid are element-type generic, and the
trapped-fraction formula has a finite derivative at the field maximum.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…-level derivative

find_magnetic_axis and find_psi_boundary run on value parts and return the axis
and the levels with the derivative of the conditions that define them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…cation

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…l mean inside the bisection bracket

A contour traced on the grid changes topology when its level passes a node value
or, in a saddle cell, the cell mean. The one inside the bracket found by the
bisection is what limits the last closed surface.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…atives, generic beam, field-map, arc-length and shape functions

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…cit_solution holds fixed the unknowns whose condition is not met; lenient value reset

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…nd flux surfaces against finite differences

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… with ForwardDiff

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… the X-point

The level of the last closed surface is found by a search on the contouring grid, so it is a
piecewise function of psi(R,Z): inside one piece it follows a grid node, over any finite change
of the inputs it follows the flux at the X-point plus a small offset. The derivative is now that
of the X-point flux for a diverted plasma, and the points of that surface are displaced on the
smooth interpolant instead of along the grid edges, where the linear interpolation is poor next
to the X-point. The test compares with a finite-difference slope fitted over several steps.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A time basis that starts at -Inf gives a decay factor of exactly zero; with a dual-number time
constant its derivative part evaluated 0 * Inf. The factor is now an exact zero in that case.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… is derived from the total density

pressure_thermal(ions) skipped every ion without a stored density_thermal. After quasi-neutrality is
enforced on an ion without fast density, the result is stored in density and density_thermal is an
expression, so that ion dropped out of pressure_ion_total, pressure_thermal, pressure and energy_thermal.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Conflict in sawteeth_source!: the dual-number method now passes on the
exclude_indexes keyword added on master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Expressions in a data structure that is not Float64 and the selective_delete! fix are available from that release.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@tomneiser
tomneiser marked this pull request as ready for review November 14, 2026 20:11
@tomneiser
tomneiser merged commit 86c1ccd into master Nov 15, 2026
2 checks passed
@tomneiser
tomneiser deleted the forward-ad-plasma branch November 15, 2026 16: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.

1 participant