Skip to content

fix OOP dampen_jacobian!! for scalar damping - #1068

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
hexaeder:hw/oop_jac_damping
Jul 14, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
hexaeder:hw/oop_jac_damping

Conversation

@hexaeder

Copy link
Copy Markdown
Contributor

dampen_jacobian!! has a mutable path (damping written in place) and an out-of-place fallback
for Jacobians that cannot setindex!. For scalar D the fallback was wrong:

function dampen_jacobian!!(J_cache, J::AbstractMatrix, D::Number)
    ArrayInterface.can_setindex(J_cache) || return J .+ D   # adds D to EVERY entry
    ...

Needs to be J + D*I to actually damp the diagonal.

Impact

StaticArrays, CPU. An SMatrix Jacobian takes the fallback, so PseudoTransient and
Levenberg-Marquardt have been damping static problems wrong. It fails silently: damping does not
move the fixed point, so the root is still correct, only the iteration degrades. On
f(u) = [u₁²-2, u₂²-3], PseudoTransient took 736 steps with an SVector vs 387 with a Vector.
After the fix the two agree exactly.

GPU sparse. CuSparse* matrices are mutable only through nonzeros, so they take the
fallback too. This line is what made PseudoTransient fail with Scalar indexing is disallowed
on a CuSparseMatrixCSC/CuSparseMatrixCSR jac_prototype; verified on hardware that it now
converges for both layouts. (J + D*I returns an SMatrix for static input and a
CuSparseMatrixCSC for GPU sparse, so one expression covers both.)

Test

lib/NonlinearSolveBase/test/dampen_jacobian.jl pins the invariant the comment states: the
mutable and out-of-place paths must produce the same matrix, for all three damping types
(Number, Diagonal, AbstractMatrix). Fails 3 assertions with the fix reverted.

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

@ChrisRackauckas
ChrisRackauckas merged commit c223b21 into SciML:master Jul 14, 2026
83 of 93 checks passed
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.

2 participants