Skip to content

Commit b832f2f

Browse files
committed
fix non-identity mass matrix with default solver and test
1 parent fdf946b commit b832f2f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/alg_utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,5 @@ is_mass_matrix_alg(alg::Union{OrdinaryDiffEqAlgorithm, DAEAlgorithm}) = false
10811081
is_mass_matrix_alg(alg::CompositeAlgorithm) = all(is_mass_matrix_alg, alg.algs)
10821082
is_mass_matrix_alg(alg::RosenbrockAlgorithm) = true
10831083
is_mass_matrix_alg(alg::NewtonAlgorithm) = !isesdirk(alg)
1084+
# hack for the default alg
1085+
is_mass_matrix_alg(alg::CompositeAlgorithm{<:Any, <:Tuple{Tsit5, Vern7, Rosenbrock23, Rodas5P, FBDF, FBDF}}) = true

src/composite_algs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function default_autoswitch(AS::AutoSwitchCache, integrator)
173173
AS.is_stiffalg, AS.current) ?
174174
AS.count < 0 ? 1 : AS.count + 1 :
175175
AS.count > 0 ? -1 : AS.count - 1
176-
if (!AS.is_stiffalg && AS.count > AS.maxstiffstep)
176+
if integrator.f.mass_matrix != I || (!AS.is_stiffalg && AS.count > AS.maxstiffstep)
177177
integrator.dt = dt * AS.dtfac
178178
AS.is_stiffalg = true
179179
AS.current = stiffchoice(reltol, len)

test/interface/default_solver_tests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ vernsol = solve(prob_ode_2Dlinear, Vern7(), reltol=1e-10)
1818
@test sol.stats.nf == vernsol.stats.nf
1919
@test all(isequal(2), sol.alg_choice)
2020

21+
prob_ode_linear_fast = ODEProblem(ODEFunction(f_2dlinear, mass_matrix=2*I(2)), rand(2), (0.0, 1.0), 1.01)
22+
sol = solve(prob_ode_linear_fast)
23+
@test all(isequal(3), sol.alg_choice)
24+
# for some reason the timestepping here is different from regular Rosenbrock23 (including the initial timestep)
25+
2126
function rober(u, p, t)
2227
y₁, y₂, y₃ = u
2328
k₁, k₂, k₃ = p
@@ -68,3 +73,12 @@ for n in (100, ) # 600 should be added but currently is broken for unknown reaso
6873
@test sol.alg_choice[1] == 1
6974
@test sol.alg_choice[end] == stiffalg
7075
end
76+
77+
function swaplinear(u, p, t)
78+
[u[2], u[1]].*p
79+
end
80+
swaplinearf = ODEFunction(swaplinear, mass_matrix=ones(2,2)-I(2))
81+
prob_swaplinear = ODEProblem(swaplinearf, rand(2), (0., 1.) 1.01)
82+
sol = solve(prob_swaplinear, reltol=1e-7) # reltol must be set to avoid running into a bug with Rosenbrock23
83+
@test all(isequal(4), sol.alg_choice)
84+
# for some reason the timestepping here is different from regular Rodas5P (including the initial timestep)

0 commit comments

Comments
 (0)