Skip to content

Fix Newmark crash with polyalgorithm nonlinear solvers - #4112

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:newmark-polyalg-u
Aug 4, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
singhharsh1708:newmark-polyalg-u

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

nlsolve is a public keyword argument on both NewmarkBeta and GeneralizedAlpha.
The default NewtonRaphson works, and so does any other algorithm whose cache steps
in place, such as TrustRegion, Broyden and Klement. Everything else crashed on the first
step. RobustMultiNewton and FastShortcutNonlinearPolyalg threw type NonlinearSolvePolyAlgorithmCache has no field p; every SimpleNonlinearSolve
algorithm threw type NonlinearSolveNoInitCache has no field p. Same bug class as
#3859, fixed for NonlinearSolveAlg in #3959, in a different sublibrary.

The integrator cache was using the inner nonlinear cache as its own storage.
perform_step! constructed a fresh NewmarkDiscretizationCache on every step, and
recovered the three DiffCache AD buffers it needed by reaching into nlcache.p.
A cache that keeps its parameter object somewhere other than a p field has nothing
to reach into. The discretization state now lives on the integrator cache and is
refreshed in place per step, so nothing is read back out of the inner cache.

That alone is not sufficient. NonlinearSolveBase's @generated CommonSolve.solve!
specialization for NonlinearSolvePolyAlgorithmCache returns the winning
sub-cache's solution and never writes back the parent cache's retcode, which stays
at ReturnCode.Default; the NonlinearSolveNoInitCache specialization delegates to
a one-shot solve and likewise leaves the cache's own retcode untouched. A caller
that drives these caches through solve! therefore has to read the returned
solution rather than the cache. This is specific to solve!. InternalAPI.step!
does assign cache.retcode (NonlinearSolveBase/src/polyalg.jl:316, 323, 359), which
is why the newton.jl path merged in #3959 drives with step! and is unaffected.

Both halves are load-bearing. With only the storage defect fixed, the guard reads
nlcache.retcode, sees Default, sets force_stepfail on every step, and the solve
returns ReturnCode.ConvergenceFailure with the state still at u0 and
t[end] == 0.0, a crash traded for a silent stall.

The two in-place perform_step! bodies and the two initialize! bodies differed
only in which αm/αf they passed into the discretization cache, so they are now
shared Union methods. Newmark-β is generalized-α with αm = αf = 0, and those two
parameters enter only through the residual the inner solver evaluates. Neither the
state update nor the Zienkiewicz-Xie error estimate references them, so the merge is
exact, not an approximation. They no longer need to live on GeneralizedAlphaCache
at all, since they are read once when the discretization cache is built. Output was
checked bit-identical against master across 285 configurations. Across the changed
source files the diff removes more lines than it adds.

Result matrix, u'' = -u as a SecondOrderODEProblem, dt = 0.1,
adaptive = false, identical for both schemes:

inner solver before after
NewtonRaphson (default), TrustRegion, Broyden, Klement Success Success, unchanged to the last bit
RobustMultiNewton, FastShortcutNonlinearPolyalg throws, no field p Success
SimpleNewtonRaphson, SimpleTrustRegion, SimpleBroyden, SimpleKlement throws, no field p Success

All twenty configurations agree with the reference to 1e-8 relative and sit at the
expected second-order error against the analytic solution (7.0e-4 for Newmark-β,
1.11e-3 for generalized-α at rho_inf = 0.8).

Not rebuilding a cache per step also drops allocations: on a two-DOF harmonic
oscillator at dt = 0.01, perform_step! goes from 2512 to 2336 bytes for
NewmarkBeta and from 2528 to 2336 for GeneralizedAlpha. AllocCheck's static site
count goes from 518 to 517; that test is still broken on both sides.

The new test/nlsolve_cache_tests.jl adds 98 assertions, taking the sublibrary's
Core group from 16 to 114. The six pre-existing testsets are unchanged at 16 of 16 on
both trees. The new file pins both state components of both schemes against a
closed-form step map rather than against each other, so an error common to every
inner solver still shows; it steps across a tstop that clips dt to 0.05, so a
discretization cache refreshed only at init cannot pass; it rebinds integrator.p
from a callback and checks the result against two chained constant-parameter solves,
so the parameters actually reaching the residual are pinned; and it drives a stub
inner solver that reports ConvergenceFailure alongside a finite iterate, so the
step-failure branch is exercised rather than assumed. Against master the file gives
24 passed and 24 errored.

Mutation testing confirms it discriminates. Deleting evalcache.dt = dt, deleting
evalcache.t = t, deleting evalcache.p = p, deleting the retcode guard, reverting
nlsol.u to nlcache.u, reverting nlsol.retcode to nlcache.retcode, and
building the generalized-α cache with αm = αf = 0 each leave the suite red.

NonlinearSolve enters the sublibrary as a test-only dependency; 4.20.3 is the
lowest floor that resolves against the rest of the downgrade floor set, and the new
tests pass there.

Every number above is an adaptive = false number. Adaptive Newmark is separately
broken on master: solve(prob, NewmarkBeta(), dt = 0.1) with no further keyword
arguments throws DimensionMismatch from the error-estimate broadcast, on master and
on this branch alike. That is unrelated to the inner solver and is fixed in a
separate PR.

This does not route Newmark through build_nlsolver/NonlinearSolveAlg. That path's
unknown is the full state under a single scalar γ, while Newmark solves for the
acceleration alone, which enters position with dt²β and velocity with dt·γ.
Mapping it onto the DIRK form pins β = γ², i.e. Trapezoid. NewmarkBeta() and
Trapezoid() already agree to 3e-16 on u'' = -u. The rho_inf
parameterization of generalized-α gives β = γ²/4 identically, so no dissipative
member is representable. Reaching the shared path needs a second-order residual mode
and a second-order Jacobian interface for (1-αm)M - (1-αf)(dt²β ∂fᵤ + dt·γ ∂fᵥ),
which is #1570 / #3474 territory. The two changes here are correct either way: both
replace reaching into a foreign cache's internals with the public API.

AI Disclosure

Claude assisted with this work.

@ChrisRackauckas

Copy link
Copy Markdown
Member

This should just use NonlinearSolveAlg?

@singhharsh1708

Copy link
Copy Markdown
Member Author

Can't. NLSolver's γ is scalar over the whole state, so Newmark's two coefficients collapse to β = γ², which is just Trapezoid. Generalized-α gives β = γ²/4 for every rho_inf, so nothing dissipative survives either. termi-official hit this in #2187 and left it.

@singhharsh1708

Copy link
Copy Markdown
Member Author

NSA also still trips the same #3859 class: SimpleNewtonRaphson throws on stats, Broyden diverges after step 1. Those stats reads are mine from #3959, I have to fix them.

@singhharsh1708

Copy link
Copy Markdown
Member Author

we can do that but it needs the second-order residual and Jacobian interface first. This PR is just the crash.

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