Skip to content
Merged
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
10 changes: 8 additions & 2 deletions lib/OrdinaryDiffEqDefault/src/default_alg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,18 @@ function default_autoswitch(AS::AutoSwitchCache, integrator)

# Choose the starting method
if AS.current == 0
choice = if AS.stiffalgfirst || !_is_identity_massmatrix(integrator.f.mass_matrix)
# A mass-matrix problem starts on the stiff branch and is never switched (see
# below), so record that in `is_stiffalg`, as `stiffalgfirst` does. The stiffness
# test and the `do_error_check` gate read it: left `false`, every step on which the
# test reported stiffness turned off `check_error!` (`maxiters`, `dtmin`, NaN `dt`,
# `unstable_check`) for that step.
AS.is_stiffalg = AS.stiffalgfirst ||
!_is_identity_massmatrix(integrator.f.mass_matrix)
AS.current = if AS.is_stiffalg
stiffchoice(reltol, len, integrator.f.mass_matrix)
else
nonstiffchoice(reltol)
end
AS.current = choice
return AS.current
end

Expand Down
37 changes: 37 additions & 0 deletions lib/OrdinaryDiffEqDefault/test/massmatrix_error_check.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using OrdinaryDiffEqDefault, SciMLBase, LinearAlgebra, Test

# A mass-matrix problem starts the default algorithm on its stiff branch and is never
# switched. The integrator's error checks (`maxiters`, `dtmin`, NaN `dt`, `unstable_check`)
# must stay on there: with a large Jacobian norm the stiffness test reports "stiff", which
# used to turn `do_error_check` off because the switcher still believed it was on the
# nonstiff branch.
function dae!(du, u, p, t)
k, c = p
du[1] = c - u[1]
du[2] = k * (u[2]^3 + u[2] - u[1])
return nothing
end
fun = ODEFunction(dae!; mass_matrix = Diagonal([1.0, 0.0]))
u0 = [1.0, 0.6823278038280184] # consistent: y^3 + y = 1

@testset "maxiters on a mass-matrix problem, k = $k" for k in (1.0, 1.0e5)
prob = ODEProblem(fun, u0, (0.0, 10.0), [k, 2.0])
for alg in (DefaultODEAlgorithm(), DefaultODEAlgorithm(stiffalgfirst = true))
sol = solve(prob, alg; maxiters = 3)
@test sol.retcode == ReturnCode.MaxIters
@test length(sol.t) == 4
end
# without the cap the solve succeeds and never leaves the stiff branch
sol = solve(prob, DefaultODEAlgorithm())
@test SciMLBase.successful_retcode(sol)
@test sol.u[end][1] ≈ 2.0 rtol = 1.0e-3
@test allequal(sol.alg_choice)

# `reinit!` resets the switcher, and the checks must stay on after it
integ = init(prob, DefaultODEAlgorithm(); maxiters = 3)
solve!(integ)
@test integ.sol.retcode == ReturnCode.MaxIters
reinit!(integ)
solve!(integ)
@test integ.sol.retcode == ReturnCode.MaxIters
end
1 change: 1 addition & 0 deletions lib/OrdinaryDiffEqDefault/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if TEST_GROUP == "Core" || TEST_GROUP == "ALL"
@time @safetestset "Composite reinit!" include("composite_reinit.jl")
@time @safetestset "Composite reinit! with ForwardDiff" include("composite_reinit_ad.jl")
@time @safetestset "DefaultCache argument types" include("default_cache_argtypes.jl")
@time @safetestset "Mass-matrix error checks" include("massmatrix_error_check.jl")
end

# Run QA tests (JET, Aqua)
Expand Down
Loading