Avoid normal equations in inverse least-squares updates - #103
Open
nicamo wants to merge 3 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace the unconstrained inverse solver's regularised normal equations,
(A.T @ A + R) delta = A.T @ b,with the equivalent augmented least-squares problem,
[A; sqrt(R)] delta ~= [b; 0],solved using
numpy.linalg.lstsq.Motivation
Forming
A.T @ Asquares the nonzero condition number of the response matrix and obscures rank deficiency. This matters for isoflux constraints becauseNpoints generateN(N-1)/2pairwise residual rows, although those rows contain at mostN-1independent flux differences.Representative FreeGSNKE matrices confirmed the redundancy:
Ahas shape(32, 10)and rank 6;Ahas shape(32, 11)and rank 7;Ais only about 30, but the unregularisedA.T @ Ahas a condition number of order10^17.The current default regularisation protects these two cases, but low or zero regularisation can make the normal-equation update inaccurate without an explicit failure. In a controlled pairwise-isoflux system with
cond(A) = 10^8, the relative solution errors were:5.6e-16.2e-10Uniform complete pairwise differences do not themselves increase the nonzero condition number; they expose rank redundancy. The avoidable numerical loss comes from subsequently forming the normal equations.
Changes
For diagonal
R, appendingsqrt(R)preserves the existing objective exactly:||A delta - b||^2 + delta.T R delta.With zero regularisation and a rank-deficient matrix,
lstsqreturns the well-defined minimum-norm solution.Validation
Focused tests cover:
cond(A) > 10^7;Results:
The MAST-U-like diverted inverse case was also run without current limits to force the unconstrained paths through the augmented solver. It converged successfully in 28 iterations with finite coil currents and flux.
The updated Example 1a notebook was also parsed and converted to HTML successfully.
Integration with current
mainCurrent
mainwas merged in2dadc31. This includes the up-down symmetry correction from #101. The two changes combine without a behavioural conflict: #101 constrains the plasma states used by the inverse and full-Jacobian forward solves, while this PR changes only how their unconstrained least-squares update is calculated. The combined branch retains both the symmetry propagation and the augmented least-squares solve.