|
| 1 | +using DiffEqBase, SciMLBase, LinearAlgebra, Test |
| 2 | +using DiffEqBase: FunctionWrappersWrappers |
| 3 | + |
| 4 | +# The resolved default algorithm uses finite differences, so concretization takes the |
| 5 | +# non-ForwardDiff `promote_f` path. A mass matrix must not disable wrapping there, |
| 6 | +# otherwise every mass-matrix model solved with the default algorithm keeps its own |
| 7 | +# RHS type in the integrator and recompiles the whole solve. |
| 8 | +struct FiniteDiffAlg <: SciMLBase.AbstractODEAlgorithm end |
| 9 | +SciMLBase.forwarddiffs_model(::FiniteDiffAlg) = false |
| 10 | +SciMLBase.isadaptive(::FiniteDiffAlg) = true |
| 11 | + |
| 12 | +g1(du, u, p, t) = (du[1] = u[1]; du[2] = -u[2] + u[1]; nothing) |
| 13 | +g2(du, u, p, t) = (du[1] = u[1]; du[2] = -2u[2] + u[1]; nothing) |
| 14 | +M = Diagonal([0.0, 1.0]) |
| 15 | +u0 = [0.0, 1.0] |
| 16 | + |
| 17 | +@testset "mass-matrix problems are wrapped on the finite-difference path" begin |
| 18 | + for spec in (SciMLBase.AutoDespecialize, SciMLBase.AutoSpecialize) |
| 19 | + probs = map((g1, g2)) do g |
| 20 | + ODEProblem{true, spec}( |
| 21 | + ODEFunction{true, spec}(g; mass_matrix = M), u0, (0.0, 1.0), (; k = 1.0) |
| 22 | + ) |
| 23 | + end |
| 24 | + concrete = map(p -> DiffEqBase.get_concrete_problem(p, true; alg = FiniteDiffAlg()), probs) |
| 25 | + for c in concrete |
| 26 | + @test c.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper |
| 27 | + @test c.f.mass_matrix === M |
| 28 | + du = similar(u0) |
| 29 | + c.f(du, u0, c.p, 0.0) |
| 30 | + @test du[1] == u0[1] |
| 31 | + end |
| 32 | + @test typeof(concrete[1]) === typeof(concrete[2]) |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +@testset "a user Jacobian still opts out of the finite-difference wrapping path" begin |
| 37 | + jac!(J, u, p, t) = (J .= [1.0 0.0; 1.0 -1.0]; nothing) |
| 38 | + prob = ODEProblem{true, SciMLBase.AutoSpecialize}( |
| 39 | + ODEFunction{true, SciMLBase.AutoSpecialize}(g1; mass_matrix = M, jac = jac!), |
| 40 | + u0, (0.0, 1.0) |
| 41 | + ) |
| 42 | + c = DiffEqBase.get_concrete_problem(prob, true; alg = FiniteDiffAlg()) |
| 43 | + @test !(c.f.f isa FunctionWrappersWrappers.FunctionWrappersWrapper) |
| 44 | +end |
0 commit comments