From cedbeb098ae97b1c5b0428a3f7b601df79ea6743 Mon Sep 17 00:00:00 2001 From: Piyush Raj <280929314+piyushraj44@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:02:18 +0530 Subject: [PATCH 1/2] Start the default DAE init solve at Newton for small systems 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 #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 #5005 --- .../src/initialize_dae.jl | 54 ++++++- .../test/dae_initialization_tests.jl | 7 +- .../test/default_nlsolve_u0_len_tests.jl | 139 ++++++++++++++++++ .../test/runtests.jl | 1 + 4 files changed, 193 insertions(+), 8 deletions(-) create mode 100644 lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl diff --git a/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl b/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl index 7db17b2772a..8796c5f7e87 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl @@ -25,13 +25,52 @@ _tagged_autodiff(u, ::Val{0}) = _tagged_autodiff(u, Val(1)) function _tagged_autodiff(u, ::Val{CS} = Val(1)) where {CS} return AutoForwardDiff{CS}(ForwardDiff.Tag(OrdinaryDiffEqTag(), eltype(u))) end +_untagged_autodiff(::Val{nothing}) = _untagged_autodiff(Val(1)) +_untagged_autodiff(::Val{0}) = _untagged_autodiff(Val(1)) +_untagged_autodiff(::Val{CS} = Val(1)) where {CS} = AutoForwardDiff(; chunksize = CS) + +# Which ForwardDiff tag the init solve should use. `BrownFullBasicInit` and +# `ShampineCollocationInit` build `FullSpecialize` residuals that call the ODE function, +# whose FunctionWrappers were compiled for `OrdinaryDiffEqTag` duals, so they need that tag. +# An init problem handed over through `OverrideInitData` is a separate function though. If it +# is `AutoSpecialize` (the default), NonlinearSolveBase wraps it for `NonlinearSolveTag` duals +# and only swaps that tag into an *untagged* backend, so ours would hit +# `NoFunctionWrapperFoundError` on the first Jacobian. Leave the tag to NonlinearSolve there. +_needs_ode_tag(prob) = SciMLBase.specialization(prob.f) === SciMLBase.FullSpecialize +function _needs_ode_tag(prob::SciMLBase.SCCNonlinearProblem) + return all(p -> !(p isa AbstractNonlinearProblem) || _needs_ode_tag(p), prob.probs) +end +function _init_autodiff(u, prob, autodiff, chunksize) + autodiff || return AutoFiniteDiff() + return _needs_ode_tag(prob) ? _tagged_autodiff(u, chunksize) : _untagged_autodiff(chunksize) +end + +# `FastShortcutNonlinearPolyalg` only starts at NewtonRaphson (instead of Broyden) when it +# is told the system is small (`u0_len <= 25`), which is what `solve(prob, nothing)` does in +# NonlinearSolve. Without it, a good initial guess can still send Broyden off to a distant +# root that also reports `Success` (SciML/OrdinaryDiffEq.jl#5005). +# +# The size has to come from the problem rather than from `u`: `BrownFullBasicInit` passes +# the full state as `u` but only solves for the algebraic variables. +_init_u0_len(prob) = _init_u0_len_of(SciMLBase.state_values(prob)) +_init_u0_len_of(::Nothing) = nothing +_init_u0_len_of(u0) = length(u0) +# The SCC solve hands the same algorithm to every block, so size it by the largest block. +# That keeps the Newton start whenever NonlinearSolve's own default would pick it for all +# of the blocks. Blocks without a state (e.g. `LinearProblem`s) don't use the polyalg. +function _init_u0_len(prob::SciMLBase.SCCNonlinearProblem) + return maximum(prob.probs; init = 0) do p + p isa AbstractNonlinearProblem ? something(_init_u0_len(p), 0) : 0 + end +end function default_nlsolve( - ::Nothing, isinplace::Val{true}, u, ::AbstractNonlinearProblem, + ::Nothing, isinplace::Val{true}, u, nlprob::AbstractNonlinearProblem, autodiff = false, chunksize = Val(1) ) return FastShortcutNonlinearPolyalg(; - autodiff = autodiff ? _tagged_autodiff(u, chunksize) : AutoFiniteDiff() + autodiff = _init_autodiff(u, nlprob, autodiff, chunksize), + u0_len = _init_u0_len(nlprob) ) end function default_nlsolve( @@ -43,11 +82,12 @@ function default_nlsolve( ) end function default_nlsolve( - ::Nothing, isinplace::Val{false}, u, ::AbstractNonlinearProblem, + ::Nothing, isinplace::Val{false}, u, nlprob::AbstractNonlinearProblem, autodiff = false, chunksize = Val(1) ) return FastShortcutNonlinearPolyalg(; - autodiff = autodiff ? _tagged_autodiff(u, chunksize) : AutoFiniteDiff() + autodiff = _init_autodiff(u, nlprob, autodiff, chunksize), + u0_len = _init_u0_len(nlprob) ) end function default_nlsolve( @@ -156,7 +196,8 @@ function default_nlsolve( ) any(p -> p isa SciMLBase.HomotopyProblem, prob.probs) && return nothing return FastShortcutNonlinearPolyalg(; - autodiff = autodiff ? _tagged_autodiff(u, chunksize) : AutoFiniteDiff() + autodiff = _init_autodiff(u, prob, autodiff, chunksize), + u0_len = _init_u0_len(prob) ) end function default_nlsolve( @@ -165,7 +206,8 @@ function default_nlsolve( ) any(p -> p isa SciMLBase.HomotopyProblem, prob.probs) && return nothing return FastShortcutNonlinearPolyalg(; - autodiff = autodiff ? _tagged_autodiff(u, chunksize) : AutoFiniteDiff() + autodiff = _init_autodiff(u, prob, autodiff, chunksize), + u0_len = _init_u0_len(prob) ) end function default_nlsolve( diff --git a/lib/OrdinaryDiffEqNonlinearSolve/test/dae_initialization_tests.jl b/lib/OrdinaryDiffEqNonlinearSolve/test/dae_initialization_tests.jl index 883cd63b81f..d0d9dec4521 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/test/dae_initialization_tests.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/test/dae_initialization_tests.jl @@ -166,9 +166,12 @@ integrator = init( end prob = ODEProblem(fn, [2.0, 0.0], (0.0, 1.0)) integ = init(prob, Rodas5P()) - @test integ.u ≈ [2.0, 2.0] atol = 1.0e-8 + # The init solve runs at the integrator's default abstol (1e-6), and Newton stops as soon + # as it is inside that, so only ask for that much here. What this is checking is that + # `reinit!` brings u[2] back to 2 instead of leaving it at the 0 guess. + @test integ.u ≈ [2.0, 2.0] atol = 1.0e-6 reinit!(integ) - @test integ.u ≈ [2.0, 2.0] atol = 1.0e-8 + @test integ.u ≈ [2.0, 2.0] atol = 1.0e-6 step!(integ, 0.01, true) @test SciMLBase.successful_retcode(integ.sol.retcode) reinit!(integ, reinit_dae = false) diff --git a/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl b/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl new file mode 100644 index 00000000000..71b1eb6ebed --- /dev/null +++ b/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl @@ -0,0 +1,139 @@ +using OrdinaryDiffEqNonlinearSolve, OrdinaryDiffEqRosenbrock +using OrdinaryDiffEqNonlinearSolve: default_nlsolve, _init_autodiff +using NonlinearSolve +using SciMLBase +using SciMLBase: OverrideInitData, OverrideInit +using ADTypes: AutoForwardDiff, AutoFiniteDiff +using ForwardDiff +using Test + +# SciML/OrdinaryDiffEq.jl#5005: the default initialization solver has to tell +# `FastShortcutNonlinearPolyalg` how big the system is, the same way `solve(prob, nothing)` +# does in NonlinearSolve. Small systems then start at NewtonRaphson instead of Broyden, +# which could wander off to a far-away root and still report Success. +# +# Starting at Newton means the init solve now takes a ForwardDiff Jacobian right away, so the +# backend's tag has to match how NonlinearSolve wrapped the init function. + +start_index(alg) = alg.start_index + +small_oop = NonlinearProblem((u, p) -> u .^ 2 .- 4, [1.5, 1.5]) +small_iip = NonlinearProblem((du, u, p) -> (du .= u .^ 2 .- 4; nothing), [1.5, 1.5]) +big_oop = NonlinearProblem((u, p) -> u .^ 2 .- 4, fill(1.5, 26)) +edge_oop = NonlinearProblem((u, p) -> u .^ 2 .- 4, fill(1.5, 25)) + +@testset "polyalg start follows the init problem size" begin + for ad in (false, true) + @test start_index(default_nlsolve(nothing, Val(false), small_oop.u0, small_oop, ad)) == 3 + @test start_index(default_nlsolve(nothing, Val(true), small_iip.u0, small_iip, ad)) == 3 + # NonlinearSolve's cutoff is `u0_len <= 25` + @test start_index(default_nlsolve(nothing, Val(false), edge_oop.u0, edge_oop, ad)) == 3 + @test start_index(default_nlsolve(nothing, Val(false), big_oop.u0, big_oop, ad)) == 1 + end +end + +@testset "size comes from the problem, not from `u`" begin + # BrownFullBasicInit passes the whole state as `u` but only solves the algebraic part, + # so a big ODE with a handful of algebraic variables should still start with Newton... + @test start_index(default_nlsolve(nothing, Val(true), zeros(100), small_iip)) == 3 + @test start_index(default_nlsolve(nothing, Val(false), zeros(100), small_oop)) == 3 + # ...and a short `u` must not make a big problem look small. + @test start_index(default_nlsolve(nothing, Val(false), [0.0], big_oop)) == 1 +end + +@testset "SCCNonlinearProblem is sized by its largest block" begin + explicit = (Returns(nothing), Returns(nothing)) + scc_small = SCCNonlinearProblem((small_oop, small_oop), explicit, nothing, Val(true)) + scc_mixed = SCCNonlinearProblem((small_oop, big_oop), explicit, nothing, Val(true)) + for iip in (Val(true), Val(false)) + # 2 + 2 unknowns, each block small + @test start_index(default_nlsolve(nothing, iip, zeros(4), scc_small)) == 3 + # one block is above the cutoff, so behave like NonlinearSolve does on that block + @test start_index(default_nlsolve(nothing, iip, zeros(28), scc_mixed)) == 1 + end +end + +@testset "least-squares default is untouched" begin + nlls = NonlinearLeastSquaresProblem( + NonlinearFunction((u, p) -> [u[1]^2 - 4, u[2] - 1, u[1] - 2]; resid_prototype = zeros(3)), + [1.5, 1.5] + ) + alg = default_nlsolve(nothing, Val(false), nlls.u0, nlls) + @test start_index(alg) == 1 + @test first(alg.algs).name === :GaussNewton +end + +tagof(::AutoForwardDiff{CS, T}) where {CS, T} = T + +@testset "ForwardDiff tag matches the init problem's specialization" begin + resid!(du, u, p) = (du .= u .^ 2 .- 4; nothing) + auto = NonlinearProblem(resid!, [1.5, 1.5]) # AutoSpecialize by default + full = NonlinearProblem(NonlinearFunction{true, SciMLBase.FullSpecialize}(resid!), [1.5, 1.5]) + u = [1.5, 1.5] + + @test _init_autodiff(u, auto, false, Val(1)) isa AutoFiniteDiff + @test _init_autodiff(u, full, false, Val(1)) isa AutoFiniteDiff + + # FullSpecialize residuals (what BrownFullBasicInit / ShampineCollocationInit build) call + # the ODE's FunctionWrappers, so they keep the OrdinaryDiffEqTag. + @test tagof(_init_autodiff(u, full, true, Val(1))) <: ForwardDiff.Tag + # AutoSpecialize init problems get wrapped by NonlinearSolve for its own tag, which it + # only stamps onto an untagged backend. + @test tagof(_init_autodiff(u, auto, true, Val(1))) === Nothing + # chunk size normalization is the same either way + @test _init_autodiff(u, auto, true, Val(nothing)) isa AutoForwardDiff{1} + @test _init_autodiff(u, auto, true, Val(0)) isa AutoForwardDiff{1} + @test _init_autodiff(u, auto, true, Val(2)) isa AutoForwardDiff{2} + + explicit = (Returns(nothing), Returns(nothing)) + scc_full = SCCNonlinearProblem((full, full), explicit, nothing, Val(true)) + scc_auto = SCCNonlinearProblem((full, auto), explicit, nothing, Val(true)) + @test tagof(_init_autodiff(zeros(4), scc_full, true, Val(1))) <: ForwardDiff.Tag + @test tagof(_init_autodiff(zeros(4), scc_auto, true, Val(1))) === Nothing + + # and the default solver actually goes through Newton's Jacobian on the wrapped problem + for prob in (auto, full) + sol = solve(prob, default_nlsolve(nothing, Val(true), u, prob, true); abstol = 1.0e-12) + @test SciMLBase.successful_retcode(sol) + @test sol.u ≈ [2.0, 2.0] + end +end + +# The reproducer from the issue: a closed two-rod linkage between fixed anchors, unknowns +# are the absolute rod angles. The guess is 0.005 rad away from the intended assembly, and +# the closure equations have other (far away) solutions that Broyden used to land on. +function closure!(f, ang, (l1, l2, x, y)) + f[1] = l1 * cos(ang[1]) + l2 * cos(ang[2]) - x + f[2] = l1 * sin(ang[1]) + l2 * sin(ang[2]) - y + return nothing +end + +const intended = [-0.7, -1.2] +const guess = intended .+ [0.005, -0.005] +const linkage_p = ( + 1.0, 0.7, + cos(intended[1]) + 0.7cos(intended[2]), + sin(intended[1]) + 0.7sin(intended[2]), +) + +@testset "OverrideInit converges to the nearby assembly (#5005)" begin + data = OverrideInitData( + NonlinearProblem(closure!, copy(guess), linkage_p), nothing, sol -> copy(sol.u), nothing + ) + f = ODEFunction( + (du, u, p, t) -> closure!(du, u, p); mass_matrix = zeros(2, 2), + initialization_data = data + ) + prob = ODEProblem(f, copy(guess), (0.0, 0.01), linkage_p) + integ = init(prob, Rodas5P(); initializealg = OverrideInit(), abstol = 1.0e-10, reltol = 1.0e-10) + @test integ.u ≈ intended atol = 1.0e-8 +end + +@testset "BrownFullBasicInit converges to the nearby assembly (#5005)" begin + f = ODEFunction((du, u, p, t) -> closure!(du, u, p); mass_matrix = zeros(2, 2)) + prob = ODEProblem(f, copy(guess), (0.0, 0.01), linkage_p) + integ = init( + prob, Rodas5P(); initializealg = BrownFullBasicInit(), abstol = 1.0e-10, reltol = 1.0e-10 + ) + @test integ.u ≈ intended atol = 1.0e-8 +end diff --git a/lib/OrdinaryDiffEqNonlinearSolve/test/runtests.jl b/lib/OrdinaryDiffEqNonlinearSolve/test/runtests.jl index e015b1be3a0..1d8a92dd62b 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/test/runtests.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/test/runtests.jl @@ -53,6 +53,7 @@ if TEST_GROUP ∉ ("QA", "ModelingToolkit") @time @safetestset "NonlinearSolveAlg Jacobian Reuse Tests" include("nsa_jacobian_reuse_tests.jl") @time @safetestset "Homotopy Nonlinear Solver Tests" include("homotopy_nlsolve_tests.jl") @time @safetestset "Homotopy init default_nlsolve Tests" include("homotopy_default_nlsolve_tests.jl") + @time @safetestset "Init default_nlsolve u0_len Tests" include("default_nlsolve_u0_len_tests.jl") @time @safetestset "NonlinearSolveAlg Sparse Jacobian Tests" include("nsa_sparse_tests.jl") @time @safetestset "NonlinearSolveAlg Matrix-Free WOperator Tests" include("nsa_matrixfree_tests.jl") @time @safetestset "NonlinearSolveAlg Split W (LHL) Tests" include("nsa_lhl_split_w_tests.jl") From b2c8af8e686667fe49e96962774b1d7870e83dbe Mon Sep 17 00:00:00 2001 From: Piyush Raj <280929314+piyushraj44@users.noreply.github.com> Date: Wed, 18 Nov 2026 01:46:40 +0530 Subject: [PATCH 2/2] Keep GPU and SCC init solves on the old solver order 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. --- .../src/initialize_dae.jl | 42 +++++++++---------- .../test/default_nlsolve_u0_len_tests.jl | 34 +++++++++------ 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl b/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl index 8796c5f7e87..7c36f86ae12 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl @@ -36,13 +36,10 @@ _untagged_autodiff(::Val{CS} = Val(1)) where {CS} = AutoForwardDiff(; chunksize # is `AutoSpecialize` (the default), NonlinearSolveBase wraps it for `NonlinearSolveTag` duals # and only swaps that tag into an *untagged* backend, so ours would hit # `NoFunctionWrapperFoundError` on the first Jacobian. Leave the tag to NonlinearSolve there. -_needs_ode_tag(prob) = SciMLBase.specialization(prob.f) === SciMLBase.FullSpecialize -function _needs_ode_tag(prob::SciMLBase.SCCNonlinearProblem) - return all(p -> !(p isa AbstractNonlinearProblem) || _needs_ode_tag(p), prob.probs) -end -function _init_autodiff(u, prob, autodiff, chunksize) +_needs_ode_tag(nlprob) = SciMLBase.specialization(nlprob.f) === SciMLBase.FullSpecialize +function _init_autodiff(u, nlprob, autodiff, chunksize) autodiff || return AutoFiniteDiff() - return _needs_ode_tag(prob) ? _tagged_autodiff(u, chunksize) : _untagged_autodiff(chunksize) + return _needs_ode_tag(nlprob) ? _tagged_autodiff(u, chunksize) : _untagged_autodiff(chunksize) end # `FastShortcutNonlinearPolyalg` only starts at NewtonRaphson (instead of Broyden) when it @@ -52,16 +49,19 @@ end # # The size has to come from the problem rather than from `u`: `BrownFullBasicInit` passes # the full state as `u` but only solves for the algebraic variables. -_init_u0_len(prob) = _init_u0_len_of(SciMLBase.state_values(prob)) -_init_u0_len_of(::Nothing) = nothing -_init_u0_len_of(u0) = length(u0) -# The SCC solve hands the same algorithm to every block, so size it by the largest block. -# That keeps the Newton start whenever NonlinearSolve's own default would pick it for all -# of the blocks. Blocks without a state (e.g. `LinearProblem`s) don't use the polyalg. -function _init_u0_len(prob::SciMLBase.SCCNonlinearProblem) - return maximum(prob.probs; init = 0) do p - p isa AbstractNonlinearProblem ? something(_init_u0_len(p), 0) : 0 - end +# +# GPU arrays are left out on purpose. Newton needs a Jacobian straight away, and building a +# dense ForwardDiff Jacobian of a GPU array scalar-indexes it, while Broyden gets going +# without one. So for those we keep the old Broyden-first order (`u0_len = nothing`). +# +# The `SCCNonlinearProblem` methods further down are not sized yet and still start with +# Broyden. They hand one algorithm to every block, and switching them moves the init results +# of SCC-split models around inside the solve tolerance, so that is left for its own change. +function _init_u0_len(u, nlprob) + u0 = nlprob.u0 + u0 === nothing && return nothing + fast = ArrayInterface.fast_scalar_indexing(u) && ArrayInterface.fast_scalar_indexing(u0) + return fast ? length(u0) : nothing end function default_nlsolve( @@ -70,7 +70,7 @@ function default_nlsolve( ) return FastShortcutNonlinearPolyalg(; autodiff = _init_autodiff(u, nlprob, autodiff, chunksize), - u0_len = _init_u0_len(nlprob) + u0_len = _init_u0_len(u, nlprob) ) end function default_nlsolve( @@ -87,7 +87,7 @@ function default_nlsolve( ) return FastShortcutNonlinearPolyalg(; autodiff = _init_autodiff(u, nlprob, autodiff, chunksize), - u0_len = _init_u0_len(nlprob) + u0_len = _init_u0_len(u, nlprob) ) end function default_nlsolve( @@ -196,8 +196,7 @@ function default_nlsolve( ) any(p -> p isa SciMLBase.HomotopyProblem, prob.probs) && return nothing return FastShortcutNonlinearPolyalg(; - autodiff = _init_autodiff(u, prob, autodiff, chunksize), - u0_len = _init_u0_len(prob) + autodiff = autodiff ? _tagged_autodiff(u, chunksize) : AutoFiniteDiff() ) end function default_nlsolve( @@ -206,8 +205,7 @@ function default_nlsolve( ) any(p -> p isa SciMLBase.HomotopyProblem, prob.probs) && return nothing return FastShortcutNonlinearPolyalg(; - autodiff = _init_autodiff(u, prob, autodiff, chunksize), - u0_len = _init_u0_len(prob) + autodiff = autodiff ? _tagged_autodiff(u, chunksize) : AutoFiniteDiff() ) end function default_nlsolve( diff --git a/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl b/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl index 71b1eb6ebed..f5999e550fa 100644 --- a/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl +++ b/lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl @@ -41,18 +41,32 @@ end @test start_index(default_nlsolve(nothing, Val(false), [0.0], big_oop)) == 1 end -@testset "SCCNonlinearProblem is sized by its largest block" begin +@testset "SCCNonlinearProblem default is not changed here" begin + # SCC init problems keep their current Broyden-first default for now; see the note next + # to `_init_u0_len` in src/initialize_dae.jl. explicit = (Returns(nothing), Returns(nothing)) - scc_small = SCCNonlinearProblem((small_oop, small_oop), explicit, nothing, Val(true)) - scc_mixed = SCCNonlinearProblem((small_oop, big_oop), explicit, nothing, Val(true)) + scc = SCCNonlinearProblem((small_oop, small_oop), explicit, nothing, Val(true)) for iip in (Val(true), Val(false)) - # 2 + 2 unknowns, each block small - @test start_index(default_nlsolve(nothing, iip, zeros(4), scc_small)) == 3 - # one block is above the cutoff, so behave like NonlinearSolve does on that block - @test start_index(default_nlsolve(nothing, iip, zeros(28), scc_mixed)) == 1 + @test start_index(default_nlsolve(nothing, iip, zeros(4), scc)) == 1 end end +# Stand-in for a GPU array: anything that says scalar indexing is slow. Newton would need a +# dense ForwardDiff Jacobian of it up front, so those keep the Broyden-first order. +struct SlowIndexVector{T} <: AbstractVector{T} + x::Vector{T} +end +Base.size(v::SlowIndexVector) = size(v.x) +Base.getindex(v::SlowIndexVector, i::Int) = v.x[i] +OrdinaryDiffEqNonlinearSolve.ArrayInterface.fast_scalar_indexing(::Type{<:SlowIndexVector}) = false + +@testset "arrays without fast scalar indexing keep the old order" begin + slow = NonlinearProblem((u, p) -> u .^ 2 .- 4, SlowIndexVector([1.5, 1.5])) + @test start_index(default_nlsolve(nothing, Val(false), slow.u0, slow)) == 1 + # a slow `u` alone is enough, e.g. Brown's full state living on the GPU + @test start_index(default_nlsolve(nothing, Val(false), SlowIndexVector(zeros(4)), small_oop)) == 1 +end + @testset "least-squares default is untouched" begin nlls = NonlinearLeastSquaresProblem( NonlinearFunction((u, p) -> [u[1]^2 - 4, u[2] - 1, u[1] - 2]; resid_prototype = zeros(3)), @@ -85,12 +99,6 @@ tagof(::AutoForwardDiff{CS, T}) where {CS, T} = T @test _init_autodiff(u, auto, true, Val(0)) isa AutoForwardDiff{1} @test _init_autodiff(u, auto, true, Val(2)) isa AutoForwardDiff{2} - explicit = (Returns(nothing), Returns(nothing)) - scc_full = SCCNonlinearProblem((full, full), explicit, nothing, Val(true)) - scc_auto = SCCNonlinearProblem((full, auto), explicit, nothing, Val(true)) - @test tagof(_init_autodiff(zeros(4), scc_full, true, Val(1))) <: ForwardDiff.Tag - @test tagof(_init_autodiff(zeros(4), scc_auto, true, Val(1))) === Nothing - # and the default solver actually goes through Newton's Jacobian on the wrapped problem for prob in (auto, full) sol = solve(prob, default_nlsolve(nothing, Val(true), u, prob, true); abstol = 1.0e-12)