diff --git a/lib/OrdinaryDiffEqDefault/src/default_alg.jl b/lib/OrdinaryDiffEqDefault/src/default_alg.jl index a42dbb6ab54..4686ff96051 100644 --- a/lib/OrdinaryDiffEqDefault/src/default_alg.jl +++ b/lib/OrdinaryDiffEqDefault/src/default_alg.jl @@ -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 diff --git a/lib/OrdinaryDiffEqDefault/test/massmatrix_error_check.jl b/lib/OrdinaryDiffEqDefault/test/massmatrix_error_check.jl new file mode 100644 index 00000000000..651871bc04e --- /dev/null +++ b/lib/OrdinaryDiffEqDefault/test/massmatrix_error_check.jl @@ -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 diff --git a/lib/OrdinaryDiffEqDefault/test/runtests.jl b/lib/OrdinaryDiffEqDefault/test/runtests.jl index fad4554cca3..ed70a52d838 100644 --- a/lib/OrdinaryDiffEqDefault/test/runtests.jl +++ b/lib/OrdinaryDiffEqDefault/test/runtests.jl @@ -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)