Repository navigation
Add mass-matrix damping to PseudoTransient - #1001
ChrisRackauckas merged 2 commits into
Conversation
Generalize the pseudo-transient continuation damping term from the identity `(1/α) I` to a user-supplied mass matrix `(1/α) M`, so the descent solves `(J + (1/α) M) δu = -F`. This extends ψtc to differential-algebraic (DAE) steady-state problems (Coffey, Kelley & Keyes, 2003), where `M` is the mass matrix of the underlying `M u' = -F(u)` dynamics — e.g. MNA circuit systems. Changes: - `PseudoTransient(; mass_matrix = nothing, ...)`: new keyword. When `nothing`, behavior is bit-for-bit identical to before; if the problem's `NonlinearFunction` carries a non-identity `mass_matrix`, it is picked up automatically. - `SwitchedEvolutionRelaxation(mass_matrix)`: the SER damping function now returns `(1/α) M` when a mass matrix is supplied, keeping the SER step-growth rule unchanged. - `dampen_jacobian!!`: split the diagonal (`Number`/`Diagonal`) fast paths from a new general `AbstractMatrix` path that adds the full damping matrix. A diagonal mass matrix keeps the efficient diagonal update; a general sparse/dense matrix is fully supported. - Tests covering regression (nothing == identity), diagonal and dense mass matrices, in-place problems, and auto-resolution from the `NonlinearFunction` mass matrix. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_017zCWMioFzF3DVwXrVCkCk4
Address three correctness issues found reviewing the mass-matrix damping: - Sparse Jacobian corruption: in `:simple` descent mode the Jacobian cache's own storage was aliased and then dampened in place, so adding a mass matrix with off-pattern entries grew the sparse Jacobian's sparsity pattern and broke the next Jacobian recompute. Now the cache is not aliased when the damping is matrix-valued (scalar/default path unchanged and still zero-allocation). - `mass_matrix = I` / `λI`: `UniformScaling` was passed through as the damping return value and later hit `size(::UniformScaling)`. The identity `I` now maps to the scalar identity-damping fast path, and a scaled `λI` is materialized into a concrete `Diagonal`. - Size-mismatched mass matrix: a wrong-sized `Diagonal` previously caused a silent out-of-bounds read in the `@inbounds` diagonal loop; the mass matrix size is now validated against the number of unknowns with a clear `DimensionMismatch`. Also removes an unreachable `scale_mass_matrix!!(::Nothing, ...)` method and extends the tests to cover sparse Jacobians, singular (DAE) mass matrices, `UniformScaling`, and the size-mismatch error. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_017zCWMioFzF3DVwXrVCkCk4
|
I marked it as draft to go an do some testing on real daes in Cadnip to see how it behaves in practice. Also seems like ci is a bit unhappy. |
|
The algorithm is correct though. And it's a published thing that supposedly helps some cases. So we might as well merge it as a correct implementation of an applicable algorithm regardless of whether it helps your specific case. |
|
Alright |
|
Tested this against real MNA circuit mass matrices from Cadnip.jl (dev'd Results on two real circuits (BJT bias networks, DC operating point):
So this is a real, reproducible improvement for circuits where the mass matrix meaningfully separates dynamics from algebra, but for circuits where most of the danger lives in the algebraic rows (nonlinear device equations with no local capacitance), it trades a slow non-convergence for a fast one — no worse in practice (still fails), but worth knowing that "mass-matrix damping" isn't a free win when M is mostly zero on the rows that matter. Thanks for merging this — we'll likely pick it up as one additional member in our |
Explanation from Claude: `SwitchedEvolutionRelaxationCache` has no `reinit!` method, so the generic no-op applies. Measured: `α⁻¹` goes from `1000.0` at init to `5.3e-8` after a solve and stays there through `reinit!`; the re-solve then runs with essentially no pseudo-time damping — silently a different (pure-Newton-like): algorithm, defeating the point of PseudoTransient on stiff problems. Since the mass-matrix support (SciML#1001) the cache also holds a scaled damping matrix `D` that would leak the same way. Contrast with `LevenbergMarquardtDampingCache`, which correctly resets `λ` and `DᵀD`.
Explanation from Claude: `SwitchedEvolutionRelaxationCache` has no `reinit!` method, so the generic no-op applies. Measured: `α⁻¹` goes from `1000.0` at init to `5.3e-8` after a solve and stays there through `reinit!`; the re-solve then runs with essentially no pseudo-time damping — silently a different (pure-Newton-like): algorithm, defeating the point of PseudoTransient on stiff problems. Since the mass-matrix support (SciML#1001) the cache also holds a scaled damping matrix `D` that would leak the same way. Contrast with `LevenbergMarquardtDampingCache`, which correctly resets `λ` and `DᵀD`.
* Fix reinit!(::EisenstatWalkerForcing2Cache) The old definition would set `cache.p = p`, but `cache.p` is the forcing algorithm struct and not the problem parameters. It just needs to update η, the rest is reinited by the use of `@internal_caches`. Also fixed resetting of `rnorm` and `rnorm_prev`. * Fix missing variable in NewtonDescent `N` is not defined, the value should come from `shared` instead. * Implement reinit!() for SwitchedEvolutionRelaxation Explanation from Claude: `SwitchedEvolutionRelaxationCache` has no `reinit!` method, so the generic no-op applies. Measured: `α⁻¹` goes from `1000.0` at init to `5.3e-8` after a solve and stays there through `reinit!`; the re-solve then runs with essentially no pseudo-time damping — silently a different (pure-Newton-like): algorithm, defeating the point of PseudoTransient on stiff problems. Since the mass-matrix support (#1001) the cache also holds a scaled damping matrix `D` that would leak the same way. Contrast with `LevenbergMarquardtDampingCache`, which correctly resets `λ` and `DᵀD`. * Drop extra kwargs from calling step!() `step!()` only supports `recompute_jacobian`. * Fix calculation of the dogleg Cauchy-point length Explanation from Claude: With `g = -Jᵀfu`, the code computes `δu_cache_mul = JᵀJ·g` and then `δuJᵀJδu = dot(δu_cache_mul, δu_cache_mul)` = ‖JᵀJ·g‖² = gᵀ(JᵀJ)²g. The non-normal-form branch:(lines 123–124) correctly computes ‖J·g‖² = gᵀJᵀJg. It should be `dot(δu_cauchy, cache.δu_cache_mul)`. * Bump SciMLBase compat Needed for downgrade CI. * Fix some tests - Fix method ambiguity - Update list of broken tests
Generalize the pseudo-transient continuation damping term from the identity
(1/α) Ito a user-supplied mass matrix(1/α) M, so the descent solves(J + (1/α) M) δu = -F. This extends ψtc to differential-algebraic (DAE) steady-state problems (Coffey, Kelley & Keyes, 2003), whereMis the mass matrix of the underlyingM u' = -F(u)dynamics — e.g. MNA circuit systems.Changes:
PseudoTransient(; mass_matrix = nothing, ...): new keyword. Whennothing, behavior is bit-for-bit identical to before; if the problem'sNonlinearFunctioncarries a non-identitymass_matrix, it is picked up automatically.SwitchedEvolutionRelaxation(mass_matrix): the SER damping function now returns(1/α) Mwhen a mass matrix is supplied, keeping the SER step-growth rule unchanged.dampen_jacobian!!: split the diagonal (Number/Diagonal) fast paths from a new generalAbstractMatrixpath that adds the full damping matrix. A diagonal mass matrix keeps the efficient diagonal update; a general sparse/dense matrix is fully supported.NonlinearFunctionmass matrix.Claude-Session: https://claude.ai/code/session_017zCWMioFzF3DVwXrVCkCk4
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
Add any other context about the problem here.