Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions lib/OrdinaryDiffEqNonlinearSolve/src/initialize_dae.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(nlprob) = SciMLBase.specialization(nlprob.f) === SciMLBase.FullSpecialize
function _init_autodiff(u, nlprob, autodiff, chunksize)
autodiff || return AutoFiniteDiff()
return _needs_ode_tag(nlprob) ? _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.
#
# 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(
::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(u, nlprob)
)
end
function default_nlsolve(
Expand All @@ -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(u, nlprob)
)
end
function default_nlsolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
147 changes: 147 additions & 0 deletions lib/OrdinaryDiffEqNonlinearSolve/test/default_nlsolve_u0_len_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
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 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 = SCCNonlinearProblem((small_oop, small_oop), explicit, nothing, Val(true))
for iip in (Val(true), Val(false))
@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)),
[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}

# 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
1 change: 1 addition & 0 deletions lib/OrdinaryDiffEqNonlinearSolve/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down