Skip to content

Add mass-matrix damping to PseudoTransient - #1001

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
pepijndevos:claude/mass-matrix-pseudotransient-bah271
Jul 5, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
pepijndevos:claude/mass-matrix-pseudotransient-bah271

Conversation

@pepijndevos

Copy link
Copy Markdown
Contributor

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.

Claude-Session: https://claude.ai/code/session_017zCWMioFzF3DVwXrVCkCk4

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

claude added 2 commits July 4, 2026 19:33
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
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 4, 2026 21:04
@pepijndevos

Copy link
Copy Markdown
Contributor Author

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.

@ChrisRackauckas

Copy link
Copy Markdown
Member

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.

@pepijndevos

Copy link
Copy Markdown
Contributor Author

Alright

@ChrisRackauckas
ChrisRackauckas merged commit c25a6a7 into SciML:master Jul 5, 2026
87 of 92 checks passed
@pepijndevos

Copy link
Copy Markdown
Contributor Author

Tested this against real MNA circuit mass matrices from Cadnip.jl (dev'd NonlinearSolveBase+NonlinearSolveFirstOrder from this branch directly, not just the stock mass_matrix kwarg on NonlinearFunction which predates this PR and can silently no-op if you don't also set concrete_jac=true).

Results on two real circuits (BJT bias networks, DC operating point):

  • Simple single-BJT bias circuit (n=14, mostly-dense mass matrix from junction caps): identity-damped PseudoTransient never leaves u=0 in 200 iterations — completely stuck. With mass_matrix=C (the real circuit mass matrix) it converges in 6 steps, matching plain NewtonRaphson exactly. Clear win.
  • Harder circuit (n=24, monostable multivibrator with excess-phase BJT model, only 4/24 rows differential): identity damping again gets stuck at u=0. With the real mass matrix, it takes a much larger first step (since the ~20 algebraic rows get zero damping) and blows up into the same exponential overflow (~1e75 residual) that plain undamped Newton hits on this circuit, bailing out with Unstable after 5 iterations instead of grinding uninformatively for 2000.

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 NonlinearSolvePolyAlgorithm fallback chain (not a replacement) once a release is cut, since poly-algorithms just move on to the next member on failure either way.

JamesWrigley added a commit to JamesWrigley/NonlinearSolve.jl that referenced this pull request Jul 13, 2026
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`.
JamesWrigley added a commit to JamesWrigley/NonlinearSolve.jl that referenced this pull request Jul 17, 2026
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`.
ChrisRackauckas pushed a commit that referenced this pull request Jul 19, 2026
* 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
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