Repository navigation
Start the default DAE init solve at Newton for small systems - #5008
Open
piyushraj44 wants to merge 2 commits into
Open
piyushraj44 wants to merge 2 commits into
piyushraj44 wants to merge 2 commits into
Conversation
default_nlsolve built FastShortcutNonlinearPolyalg without u0_len, so the init solve always started at Broyden. On small systems that can hop to a far-away root and still report Success, e.g. the two-rod linkage in SciML#5005 lands at ~24 rad from a guess 0.005 rad off. NonlinearSolve's own default passes u0_len and starts at Newton for <= 25 unknowns, so do the same. The size is taken from the init problem rather than from `u`, since BrownFullBasicInit passes the full state but only solves the algebraic part. SCC problems are sized by their largest block. Starting at Newton means the first thing the solve does is a ForwardDiff Jacobian, which exposed a tag mismatch on the OverrideInit path: an AutoSpecialize init problem gets wrapped by NonlinearSolveBase for its own tag, and it only re-tags untagged backends, so forcing OrdinaryDiffEqTag there throws NoFunctionWrapperFoundError. Keep OrdinaryDiffEqTag for the FullSpecialize residuals Brown/Shampine build (they call the ODE's wrappers) and hand an untagged backend to everything else. The reinit! test in dae_initialization_tests asserted 1e-8 while the init solve runs at abstol 1e-6. Newton now stops at 2.00000009 (inside the tolerance) where Broyden happened to stop closer, so the assertion now matches the solve tolerance. Fixes SciML#5005
The first CI run turned up three things with starting the init solve at Newton: - On GPU arrays Newton's first ForwardDiff Jacobian scalar-indexes the state (test/gpu/dae_tests.jl and the JLArray DAE test in InterfaceV). Only pass u0_len when both `u` and the init problem's u0 have fast scalar indexing, so GPU states keep the Broyden-first order, which doesn't need a Jacobian to get going. - SciMLBase.state_values isn't public there, which the QA explicit imports check flags. Read nlprob.u0 directly instead. - Sizing SCC problems moved MTK's chain_dae init around inside abstol, and its remake test compares two solves at sqrt(eps), so it tipped over (1.83e-8 vs 1.49e-8). Put the SCC methods back the way they are on master for now. That change needs coordinating with MTK.
This was referenced Nov 17, 2026
Member
|
LGTM, the #5005 MWE gives [-0.7, -1.2] here against [24.02, 5.67] on master, for OverrideInit and BrownFullBasicInit in place and out of place. |
This branch has not been deployed
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.
Fixes #5005.
default_nlsolvewas buildingFastShortcutNonlinearPolyalgwithoutu0_len, so the default initialization solve always started at Broyden. NonlinearSolve's ownsolve(prob, nothing)passesu0_lenand starts at Newton for ≤ 25 unknowns. Broyden can wander off to a distant root that still reportsSuccess, which is what happens with the two-rod linkage in the issue.What changed
u0_lenis now passed in the plainAbstractNonlinearProblemmethods, which is whereOverrideInit,BrownFullBasicInitandShampineCollocationInitend up. The size comes from the init problem, not fromu, becauseBrownFullBasicInitpasses the full state but only solves for the algebraic vars.ArrayInterface.fast_scalar_indexingis false foruor for the init problem'su0, nou0_lenis passed. Newton needs a dense ForwardDiff Jacobian on its very first step, and seeding one scalar-indexes the array. Broyden starts from an identity Jacobian, so those cases behave exactly like before.SCCNonlinearProbleminit problems are not changed here. More on that below.FastShortcutNLLSPolyalghas nou0_lenand already starts at Gauss-Newton.FastShortcutHomotopyPolyalgupstream.A second bug this exposed
Once Newton runs first, the init solve takes a ForwardDiff Jacobian right away, and the issue's MWE then crashed with
No matching function wrapper was found!. The cause:AutoSpecializeinit problem (the default for a hand-builtNonlinearProblempassed throughOverrideInitData) gets wrapped by NonlinearSolveBase forNonlinearSolveTagDuals.standardize_forwarddiff_tagonly swaps that tag into an untaggedAutoForwardDiff, so theOrdinaryDiffEqTagwe were forcing never matched.OverrideInitsolve that fell through to Newton would have hit it.The
OrdinaryDiffEqTag(#3067) is only needed for theFullSpecializeresiduals that Brown and Shampine build, because those call the ODE's own wrappers. So in the plain methods,FullSpecializeproblems keep the tag, and everything else gets an untagged backend with the same chunk size, letting NonlinearSolve choose the matching tag.SciMLBase.specializationand the untagged re-stamping both exist at the current compat floors, so no compat bumps are needed.Why SCC problems are left out for now
My first push also sized SCC problems by their largest block. That made
model_independent_initialization.jlfail in the ModelingToolkitInitializationdownstream group. The test solveschain_dae(6), remakes the problem fromsol.u[1], and checksremadesol.u[end] ≈ sol.u[end]at the default √eps tolerance. I reproduced it locally, with the same 1.5079e-8 difference CI printed:≈Both orders find the same (unique) root and both stop inside the requested
abstol = 1e-6. The remade solve then starts from a point that is already converged, and Newton polishes it a little further. So it's a tolerance thing rather than a wrong answer, and master's n=9 row moves by about the same amount. Still, switching SCC problems changes MTK's init results and needs a matching tweak to that test, so I'd rather do it as a separate PR together with MTK than fold it in here. The SCC methods are byte-for-byte what master has.Tests
default_nlsolve_u0_len_tests.jl(new) covers:ulonger or shorter than the problemFullSpecializeandAutoSpecializeproblems, including a full solve through Newton's JacobianOverrideInitandBrownFullBasicInitOn master both of those last paths return
[24.0228, 5.6732]. With this PR both return[-0.7, -1.2].I changed one existing assertion. The
reinit!test indae_initialization_tests.jlcheckedatol = 1e-8, but the init solve runs at the integrator's defaultabstol = 1e-6. Newton (starting fromu = 1) stops at2.0000000929, with a residual of 3.7e-7, inside the tolerance. Broyden happened to stop closer. The test is checking thatreinit!reruns init (2 rather than the 0 guess), so I set the assertion to the solve tolerance and left a comment.Run locally on Julia 1.13.4 with every sublibrary in this repo dev'd (NonlinearSolve 4.32.0, NonlinearSolveBase 2.55.1, SciMLBase 3.58.0, ModelingToolkit 11.46.1):
--check-bounds=yes)default_nlsolve_u0_len_tests.jl(new)dae_initialization_tests.jl/brown_inplace_tests.jl/sparse_dae_initialization_tests.jl/checkinit_tests.jl@test_brokeninnlstep_tests.jl)InitializationgroupI couldn't run
test/gpu/dae_tests.jlhere (no CUDA); the JLArray test exercises the same scalar-indexing path.CI failures that aren't from this PR
These fail with identical counts on master (or on other PRs touching this sublibrary):
Newton Tests: aLineSearchExceptionfromBackTrackinginside the stepper'sNLNewton, also failing on Resize the member caches of a CompositeCache in a DDEIntegrator #4958. Its two problems (Lorenz, Oregonator) have identity mass matrices, so the init code isn't reached; I double-checked by making the new init helpers throw and it still fails the same way.autodiff_events.jl), same numbers as master.autoswitch.jl(also in the OrdinaryDiffEqDefault GPU job): a GPUCompiler deprecation warning trips@test_nowarn, same on master.One more that CI can't show yet, because
Newton Testsaborts the Core run before it gets there: locally,nsa_noinit_tests.jlhas 4MaxIterscells (TRBDF2 with eachSimple*inner solver on the Robertson mass-matrix problem). I get exactly the same 4 with master's init code, and the init helpers are never called there, sinceu0is already consistent and Brown returns before solving anything.One trade-off to flag
The issue mentions that Broyden and Klement stay available as fallbacks. They don't: the polyalgorithm never goes below
start_index, so small systems skip them entirely. That matches what NonlinearSolve's default already does. The practical difference: if the initial guess sits exactly where the Jacobian is singular (e.g.u^2 - 4fromu = 0), the Jacobian-based methods stall where Broyden could have moved.solve(prob)in NonlinearSolve stalls on that too. A loudInitialFailureseemed like a better failure mode than silently landing on the wrong root, but I'm happy to change it if you'd rather handle it differently.