Skip to content

ForceFreeStates - BUGFIX! - Trigger the Gaussian reduction on the conditioning of U₁ and factor U₁ in the plasma energy matrix - #520

Open
jhalpern30 wants to merge 1 commit into
bugfix/high-n-sing-start-qlow-unormfrom
bugfix/ffs-reduction-conditioning-trigger
Open

jhalpern30 wants to merge 1 commit into
bugfix/high-n-sing-start-qlow-unormfrom
bugfix/ffs-reduction-conditioning-trigger

Conversation

@jhalpern30

@jhalpern30 jhalpern30 commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #490 and enables #481. Stacked on #662 (base bugfix/high-n-sing-start-qlow-unorm), which now carries the reduction-storage fix; retarget to develop once #662 merges.

Two changes to the forward integrator:

  • Plasma energy matrix. plasma_energy_matrix(u, psio) forms W_p = U₂U₁⁻¹/ψ₀² by pivoted LU of U₁. The old U₂ / U₁ factored U₁ᴴ, whose rows carry the column-norm spread, so partial pivoting failed on graded columns (relative error 0.15 on the 4×4 case from review, 2e-16 now). Used by free_run, free_compute_total and build_result.
  • Reduction trigger. Forward reduces when the 1-norm condition number of U₁ with unit columns, cond(U₁ ./ ‖columns‖, 1), exceeds [Forward] renorm_threshold (default 1e6), instead of when column-norm growth spreads. Columns that grow alike and turn parallel now trigger; a large norm spread with independent columns no longer does. Riccati is unchanged ([Riccati] renorm_threshold keeps its entry-size meaning and its 1e3 default).
Regime develop + #662 → this PR Case
Small singfac_min 800× off → ≤ 1.7e-5 Solovev q₀ 1.9, singfac_min 1e-8, et₁ vs reduce-every-step
High n ifix 370–434 at 1e3 → 0.6–1.1% of steps at 1e6 DIII-D-like n = 1–10 (N ≤ 91), multi-n (N = 148); et₁ within 2e-7 of reduce-every-step
#676 n = 3 bump max ‖W−W†‖/‖W+W†‖ 1.55e-10 → 8.5e-12 DIII-D-like n = 3 Forward, singfac_min 5e-4, on #676's head
Tolerance scan (#490) spikes → ≤ 1e-6 Solovev; threshold ladder 2–1e8 matches 1.0 at rtol 1e-6 (runtests_fullruns.jl)

Release note

  • Audience: users
  • Numerical impact: forward-integrator energies and perturbed-equilibrium outputs move by at most 5e-6 relative in energies and 3e-5 in torques on the harness decks (below); Riccati results move only through plasma_energy_matrix, at rounding level (≤ 2e-13).
  • Migration: forward decks that set [Forward] renorm_threshold by hand should drop it and take the new default 1e6. The value is now a limit on the condition number of U₁ with unit columns, not on the spread of column-norm growth: small values such as 10 reduce very often, and values above about 1e10 lose digits. Riccati decks need no change.

Regression report

Full harness, all 28 cases, on Landau (2026-11-18), develop + #662 (b037e9323) against this change at the time; the later commits only rename, inline and restructure, and a spot check at head a395b14aa (solovev_n1, diiid_multi_n) reproduces those values bit for bit (all 43 quantities, runtime aside).

  • Unchanged (bit-identical or ≤ 2e-13): both Riccati cases, all SLAYER cases, both Galerkin cases, ray, EFIT fixed-boundary, lar_ggj_*.
  • Forward energies: ≤ 5e-6 relative (diiid_kinetic_multi_n et₁ 6e-9); accepted steps +0.3 to +3%; runtime within noise.
  • Torques: ≤ 3e-5 (diiid_error_field EFC scan 1.4e-5).
  • One large raw-norm change: tj_kinetic_calculated raw pe_clebsch_{psi1,alpha} norms +7% and b_theta_reg +1.2%, while its NTV torque moves 3e-4 and the energies ≤ 5e-6. These are raw matrix norms on a changed step grid; not yet compared on a common grid.

Unit tests: runtests_eulerlagrange.jl 224/224 at a395b14aa; CI runs the full suite.

@github-actions github-actions Bot added bugfix Something was wrong and now is not changed-results Results move or an interface breaks - read before upgrading labels Oct 12, 2026
@jhalpern30 jhalpern30 self-assigned this Oct 12, 2026
@jhalpern30
jhalpern30 requested a review from d-burg October 12, 2026 21:53
@jhalpern30
jhalpern30 marked this pull request as ready for review October 12, 2026 21:53
@jhalpern30

jhalpern30 commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

cc-ing @logan-nc @matt-pharr since this would basically fundamentally change the decades-old approach of how fixups are done in ForceFreeStates. After a lot of investigation/tests, it seems that the growth oriented reduction does not properly capture when a fixup is needed. Instead, the condition number of the displacement columns is more accurate, producing good numerics. Specifically, this fixes the Solov'ev case, which has a higher alpha (Frobenius coefficient in the expansion) at the singular surfaces, and was giving garbage as singfac_min was decreased

And on a related note, I've verified and tested that the 3D code also has this same issue (but worse) and is fixed by this method

@logan-nc

Copy link
Copy Markdown
Collaborator

I'm happy to implement tested improvemlike this! this! The fixup is the magic sauce, so having our own recipe is a good result of the rewrite effort

@matt-pharr

Copy link
Copy Markdown
Collaborator

@jhalpern30 be sure to post some plots highlighting the results, a table is nice but a plot is a much better record!

@logan-nc

Copy link
Copy Markdown
Collaborator

@matt-pharr figures are in the artifact linked in in the description

@d-burg d-burg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The mechanism is convincing to me: columns that grow alike and turn parallel are invisible to a spread test, and the new unit tests separate the two triggers nicely. The storage overflow and the U₂ / lu(U₁) fix are clear bug fixes. I'm less sure the evidence is there yet for replacing the reduction trigger itself.

My and claude's brains merged into a disconcerting synthetic hybrid network and yielded the following items:

Blocking

  1. Migration. The title carries ! and Migration starts with "none", which is what fails the title check, and substantively a forward-path deck with a hand-set ucrit is reinterpreted without a warning. Suggested text: "Migration: forward-integrator decks that set ucrit by hand should return it to the default 1e3. The value is now compared with a condition estimate of the column-normalized U₁, not with the spread of column-norm growth; small values such as 10 now reduce very often, and values above about 1e10 lose digits. Riccati decks need no change."
  2. Regression report. The code block is empty and the stamp (1dc6138aa) predates the last two commits. This changes when every forward run reduces and how W is formed for every integrator, so I'd like the full harness now, d50a5e5b6 against the current head as two git refs, especially the Riccati, perturbed-equilibrium, kinetic Solovev and multi-n cases. Rerunning at merge time is cheap; reviewing blind is not.

Evidence I'd want for "changes the decades-old approach"

  • Every error in the table is measured against the same build with ucrit = 1, which shows self-consistency, not correctness. One independent reference per headline row would help: the Riccati integrator on the same Solovev decks at singfac_min 1e-8, or a tolerance-converged or Fortran value where available. The figures sit behind a link "shared on request"; the numbers the argument rests on should be in the PR.
  • Per case: accepted steps, number of reductions, the largest estimate at the moment of reduction, wall time and peak memory against #489's head. Without reduction counts nobody can judge 1e3 against 1e6, and with unbounded storage a run that reduces every other step is a memory problem the old 100-slot cap used to hide.
  • Cases not yet covered: an ideal-unstable equilibrium where det U₁ passes through zero inside the plasma (U₁ is singular there for a physical reason, so the trigger fires whatever its threshold), a kinetic run, and the largest multi-n deck.

On your five decisions

  1. 1e3 vs 1e6: undecided until the reduction counts are shown; leaning 1e6 for the forward path. The stated reason for 1e3 is about Riccati sharing the key, not about the forward path.
  2. One key, two meanings: I'd add a forward-path key. Decks are reinterpreted silently, switching integrator changes what the same number means, and the shared key is already forcing a default neither path would pick on its own. The ! in the title makes a new key cheap now.
  3. cond(U₁) rather than cond([U₁; U₂]): agree; the elimination pivots on U₁ and W inverts U₁.
  4. Elimination rather than an (S, I) or QR reset: agree for this PR. Worth a follow-up issue: the elimination never rescales columns, which is why the norms span 1e110 in the first place.
  5. No test for the lu fix: disagree; a 4×4 matrix reproduces it (inline).

Suggestion: the storage fix and the lu fix stand on their own and are easy to verify; the trigger change is a design decision that needs the evidence above. Splitting them would let the two bug fixes go in quickly on #489.

Small, outside the diff

  • The OdeState field docstrings in EulerLagrange.jl still give the reduction arrays fixed shapes ((numpert_total, numunorms_init), "length numunorms_init"); they are initial capacities now. docs/src/stability.md still calls numunorms_init a "renorm checkpoint budget".
  • The comment at Riccati/Driver.jl:77 still quotes wp = u[:,:,2] / u[:,:,1].
  • The description's notes refer to column_condition, which the last two commits inlined.

Comment thread src/ForceFreeStates/EulerLagrange.jl Resolved
Comment thread src/ForceFreeStates/Free.jl Resolved
Comment thread src/ForceFreeStates/CoreTypes.jl Resolved
Comment thread src/ForceFreeStates/Utils.jl Resolved
Comment thread test/runtests_fullruns.jl Resolved
Comment thread test/runtests_fullruns.jl Resolved

@logan-nc logan-nc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One item not in the six open threads, on the kinetic crossing. compute_solution_norms! (head 2baa20b, EulerLagrange.jl:1007) returns early when odet.new && !at_ideal_crossing, resetting the baseline. The kinetic-crossing call at EulerLagrange.jl:847 passes sing_flag = true with no resonant_rows, so when the step before the surface already reduced (odet.new == true), that crossing resets and skips the reduction the flag is supposed to force. Fortran GPEC closed the same hole in PrincetonUniversity/GPEC#332 (fix_required = sing_flag ahead of the branch). The early return is pre-existing on develop, but this PR owns the trigger now, so if odet.new && !at_ideal_crossing && !sing_flag (or passing the surface's rows from the kinetic path too) belongs here.

@matt-pharr

Copy link
Copy Markdown
Collaborator

@jhalpern30 what is the status on this? Can this make it in for 2.0.0?

Base automatically changed from bugfix/ffs-crossing-resonant-elimination to develop October 29, 2026 01:20
@matt-pharr

Copy link
Copy Markdown
Collaborator

@logan-nc thoughts? This seems like it would be valuable. If Jake doesn't have time before APS then I guess this could be 2.1? Or someone else could get into the weeds on this?

@jhalpern30

Copy link
Copy Markdown
Collaborator Author

@jhalpern30 what is the status on this? Can this make it in for 2.0.0?

Probably not. Daniel raised some valid points that I don't have the time/effort to look into before APS

@logan-nc logan-nc removed this from the GPEC v2.0.0 milestone Oct 29, 2026
@logan-nc

Copy link
Copy Markdown
Collaborator

@jhalpern30 @matt-pharr it's time to face reality on v2.0 scope and it seems reasonable that such a fundamental change could come in a minor version update with clean validation. That lets us release the "decades-old approach" for warm familiarity and creates a clean historical record of the improvement.

I am not afraid of releasing 2.1 in Dec. so please do keep this on your todo list for immediately post APS.

@logan-nc

logan-nc commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

@jhalpern30 The kinetic-crossing hole I flagged above (Fortran GPEC PR 332) is fixed separately in #626, so it doesn't wait on this PR's trigger change. In compute_solution_norms!, a call with sing_flag set now always reduces, and if it comes right after a reduction it keeps that reduction's pivot order (Fortran commit 1f8abc54).

This PR rewrites the trigger in the same function, so whichever of the two lands second needs a small rebase there. The changes compose: #626 only replaces the early return, and your conditioning test slots in where the uratio test is. This PR also needs a rebase onto develop for #602's move of ctrl.renorm_threshold to ctrl.algorithm.renorm_threshold.

…ditioning of U₁ and factor U₁ in the plasma energy matrix

- `plasma_energy_matrix(u, psio)` forms W_p = U₂U₁⁻¹/ψ₀² by pivoted LU of U₁; `U₂ / U₁` factored U₁ᴴ,
  whose rows carry the column-norm spread, so partial pivoting failed on graded columns
- Forward reduces when the 1-norm condition number of U₁ with unit columns exceeds `renorm_threshold`
  (default 1e6), instead of on the column-norm growth ratio; a large norm spread with independent
  columns no longer reduces
- Decks drop the `[Forward] renorm_threshold = 1e3` pin; docs updated
- Tests: 4×4 graded pivot test, conditioning trigger cases, threshold ladder in fullruns
@jhalpern30
jhalpern30 force-pushed the bugfix/ffs-reduction-conditioning-trigger branch from b8e1e71 to a395b14 Compare November 19, 2026 16:21
@jhalpern30 jhalpern30 changed the title ForceFreeStates - BUGFIX! - Trigger the Gaussian reduction on the conditioning of U₁ and factor U₁ in the plasma response matrix ForceFreeStates - BUGFIX! - Trigger the Gaussian reduction on the conditioning of U₁ and factor U₁ in the plasma energy matrix Nov 19, 2026
@jhalpern30
jhalpern30 removed this pull request from stack #545 November 19, 2026 16:24
@jhalpern30
jhalpern30 changed the base branch from develop to bugfix/high-n-sing-start-qlow-unorm November 19, 2026 16:24
@jhalpern30
jhalpern30 added this pull request to stack #685 November 19, 2026 16:24
@jhalpern30
jhalpern30 removed this pull request from stack #685 November 19, 2026 16:25
@jhalpern30
jhalpern30 added this pull request to stack #686 November 19, 2026 16:25
@jhalpern30

Copy link
Copy Markdown
Collaborator Author

Thanks, this reshaped the PR. Rebased and rescoped; here is where each item landed.

Blocking

  1. Migration: the release note now says to drop hand-set [Forward] renorm_threshold values, with your wording on what small and large values do. The key is forward-only since ForceFreeStates - BUGFIX! - Force the reduction at kinetic singular crossings in the previous pivot order #626, so Riccati decks keep their meaning.
  2. Regression report: full harness against ForceFreeStates - BUGFIX - Move a rational qlow start off its surface and grow the reduction store on demand #662's head, above.

Evidence

  • Independent reference: Riccati on the same equilibrium. DIII-D-like n = 3: Riccati et₁ 0.2599621990, this PR 0.2599621982 (1e6), 0.2599622024 (1e3). On Solovev q₀ 1.9 at singfac_min 1e-8, where develop's trigger is 800× off reduce-every-step, this PR is within 1.7e-5 at every threshold.
  • Reduction counts: at 1e6, reductions are 0.6–1.1% of accepted steps on DIII-D-like n = 1–10 and multi-n (N up to 148); at 1e3 there are about twice as many. The estimate at the moment of reduction overshoots the threshold by a median of 1–1.5×, max 9× (Solovev internal-kink set). The post-reduction baseline grows with N (median 36 at N = 35, 200–600 at N = 91), which is why the default is 1e6.
  • det U₁ = 0 inside the plasma: no singfac_min-robust Newcomb crossing exists in the Solovev or TJ-analytic decks. The two cases where an eigenvalue of W⁻¹ does pass through zero have the steps straddling the zero (max κ ≤ 1.75e6); the trigger fires at 2–3% of steps at 1e6, never every step, and et₁ stays within 8e-6 of reduce-every-step.
  • Kinetic and largest multi-n: in the harness (solovev_kinetic_*, tj_kinetic_*, diiid_kinetic_multi_n, diiid_multi_n).

Your five decisions

  1. 1e6 for the forward path.
  2. Separate keys: done by ForceFreeStates - BUGFIX! - Force the reduction at kinetic singular crossings in the previous pivot order #626 ([Forward] / [Riccati]).
  3. cond(U₁): kept.
  4. Column rescaling: we tried QR and (S, I) resets between surfaces. Both were as accurate as the elimination but no better, and (S, I) was fragile in the forward path (resetting every step near high-α surfaces, cond(U₂) up to 1e22). So no follow-up for now.
  5. Your 4×4 case is now a unit test at rtol 1e-12.

Split: the storage fix went to #662; this PR is now the lu fix and the trigger, in a GitHub stack on #662 (the old stack with #489 is gone).

Small items: the OdeState docstrings call the reduction arrays initial capacities (#662); the Riccati driver comment no longer quotes the old expression; column_condition is gone from the description.

@jhalpern30

Copy link
Copy Markdown
Collaborator Author

@logan-nc Rebased onto develop with #626 and #602. The conditioning test replaces the uratio test in #626's fix_required slot; the forced reduction on sing_flag, keeping the previous pivot order right after a reduction, stays as #626 has it, and its Case 6 still passes. There is no early return anymore.

@jhalpern30

jhalpern30 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hi all - human Jake here. I think this is now ready to go. Incorporated Daniel's comments, got something I think is fairly robust/clean in the cond(U_1) estimate and affects runtime in the < 2% range / doesn't break on any case I can find. This is now stacked on 662 which has the reduction storage resizing that was originally on this branch. Ready for another look through

A short artifact to remind what the main results were here: https://claude.ai/artifact/DCuvCWYrkv2Pe7m3rNHurT

@d-burg d-burg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@jhalpern30 Thanks, this answers everything from my October review. The Riccati et₁ on n = 3 is the independent reference I was after, and the reduction counts settle 1e6 for the forward path. Splitting the storage fix into #662 makes this PR easy to read. I checked the head (a395b14aa):

  • The trigger normalizes by the current column norms before taking cond(·, 1), and still sets the pivot order from the growth since the last reduction. An exactly singular u₁ gives cond = Inf and reduces, which is the right behavior.
  • fix_required = sing_flag with no early return, so Logan's kinetic-crossing hole is closed.
  • Every shipped deck that still sets renorm_threshold does so under [Riccati], so no deck is silently reinterpreted.

Before merge (both mechanical, and both CI failures):

  1. Stamp. The release note needs _(harness @ <sha>)_ at the head. The report ran at an earlier commit plus a spot check, and the gate wants a run at the stamped commit with no src/ changes after it.
  2. Formatter. JuliaFormatter wants (L.*exp10.(range(110, 0; length=4))') at test/runtests_eulerlagrange.jl:392.

Non-blocking:

  • On the sf 1e-8 test, I take the CI-time point about an eight-run ladder. A single pair would still pin the motivating case, though: the default threshold against threshold 1.0 at singfac_min 1e-8, with the stock ladder left as is. If even two runs are too slow for CI, fine as is.
  • docs/src/stability.md says "condition number of u₁" in two places. "with unit columns" matters here, as Integrators.jl says.
  • compute_solution_norms! still ends with "Port of Fortran ode_unorm". The trigger no longer matches it, so I'd drop that line.

Approving. It lands after #662, which it's stacked on.

@jhalpern30

Copy link
Copy Markdown
Collaborator Author

@d-burg thanks. I will clean up CI once 662 is merged.

@logan-nc

Copy link
Copy Markdown
Collaborator

Poll @d-burg @matt-pharr @jhalpern30 - I have my ai proactively fix clear bugs and minor annotation cleanup when certain. In the review above, it could have made this pr g2g and saved a full iteration. An iteration, despite being small, could take weeks depending on how busy a author is and how high a priority the pr is for them. On the other hand, its always possible an author disagrees and has to revert... but that is doable and the commit diff is more clear then an ai block of verbose text? What do you prefer? Proactive help or just listing requested changes even when minor formatting and such?

If the second, at least have your ai enter any 1-3 line changes as "suggest changes" inline so the author can just click accept as they read through. Please and thank you.

@matt-pharr

Copy link
Copy Markdown
Collaborator

@logan-nc i think the best etiquette here is if you can't work something and you don't feel strongly about custodianship then you should precisely document your intentions, reasoning, and results, and then deassign yourself from the PR. That way we can distinguish from places where the author would prefer the PR is/is not touched by someone else yet. This can also be achieved with a DM.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Something was wrong and now is not changed-results Results move or an interface breaks - read before upgrading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ForceFreeStates - Gaussian reduction trigger misses near-parallel solutions

4 participants