|
| 1 | +using Test |
| 2 | +using OrdinaryDiffEqBDF |
| 3 | +using OrdinaryDiffEqSDIRK |
| 4 | +using OrdinaryDiffEqCore: initdt_alg, DefaultInitDt, SundialsInitDt |
| 5 | + |
| 6 | +@testset "SundialsInitDt Algorithm" begin |
| 7 | + |
| 8 | + @testset "Trait dispatch" begin |
| 9 | + # BDF methods should use SundialsInitDt |
| 10 | + @test initdt_alg(FBDF()) isa SundialsInitDt |
| 11 | + @test initdt_alg(QNDF()) isa SundialsInitDt |
| 12 | + @test initdt_alg(QNDF1()) isa SundialsInitDt |
| 13 | + @test initdt_alg(QNDF2()) isa SundialsInitDt |
| 14 | + @test initdt_alg(ABDF2()) isa SundialsInitDt |
| 15 | + |
| 16 | + # Non-BDF implicit methods should use DefaultInitDt |
| 17 | + @test initdt_alg(ImplicitEuler()) isa DefaultInitDt |
| 18 | + end |
| 19 | + |
| 20 | + @testset "Simple exponential decay (in-place)" begin |
| 21 | + function f_exp!(du, u, p, t) |
| 22 | + du[1] = -u[1] |
| 23 | + end |
| 24 | + prob = ODEProblem(f_exp!, [1.0], (0.0, 10.0)) |
| 25 | + sol = solve(prob, FBDF()) |
| 26 | + @test sol.retcode == ReturnCode.Success |
| 27 | + @test isapprox(sol.u[end][1], exp(-10.0), rtol=1e-2) |
| 28 | + end |
| 29 | + |
| 30 | + @testset "Simple exponential decay (out-of-place)" begin |
| 31 | + f_exp(u, p, t) = [-u[1]] |
| 32 | + prob = ODEProblem(f_exp, [1.0], (0.0, 10.0)) |
| 33 | + sol = solve(prob, FBDF()) |
| 34 | + @test sol.retcode == ReturnCode.Success |
| 35 | + @test isapprox(sol.u[end][1], exp(-10.0), rtol=1e-2) |
| 36 | + end |
| 37 | + |
| 38 | + @testset "Multi-scale with zero states and tiny abstol (in-place)" begin |
| 39 | + # This is the pathological case from issue #1496: |
| 40 | + # Some species start at 0 with tiny abstol, causing the Hairer algorithm |
| 41 | + # to produce catastrophically small initial dt. |
| 42 | + function f_ms!(du, u, p, t) |
| 43 | + du[1] = -100.0 * u[1] + 1.0 |
| 44 | + du[2] = 0.1 * u[1] |
| 45 | + du[3] = 1.84 # mimics TH2S |
| 46 | + end |
| 47 | + u0 = [1.0, 0.0, 0.0] |
| 48 | + prob = ODEProblem(f_ms!, u0, (0.0, 100.0)) |
| 49 | + |
| 50 | + # With FBDF (SundialsInitDt) - should succeed efficiently |
| 51 | + sol_fbdf = solve(prob, FBDF(), abstol=1e-15, reltol=1e-8) |
| 52 | + @test sol_fbdf.retcode == ReturnCode.Success |
| 53 | + @test sol_fbdf.t[end] == 100.0 |
| 54 | + |
| 55 | + # With ImplicitEuler (DefaultInitDt/Hairer) for comparison |
| 56 | + sol_ie = solve(prob, ImplicitEuler(), abstol=1e-15, reltol=1e-8) |
| 57 | + @test sol_ie.retcode == ReturnCode.Success |
| 58 | + |
| 59 | + # The SundialsInitDt should produce a MUCH larger initial dt |
| 60 | + dt_fbdf = sol_fbdf.t[2] - sol_fbdf.t[1] |
| 61 | + dt_ie = sol_ie.t[2] - sol_ie.t[1] |
| 62 | + @test dt_fbdf > dt_ie # SundialsInitDt is larger |
| 63 | + @test dt_fbdf / dt_ie > 10 # At least 10x larger |
| 64 | + |
| 65 | + # FBDF should use far fewer steps (more efficient) |
| 66 | + @test length(sol_fbdf.t) < length(sol_ie.t) |
| 67 | + end |
| 68 | + |
| 69 | + @testset "Multi-scale with zero states and tiny abstol (out-of-place)" begin |
| 70 | + function f_ms(u, p, t) |
| 71 | + return [-100.0 * u[1] + 1.0, 0.1 * u[1], 1.84] |
| 72 | + end |
| 73 | + u0 = [1.0, 0.0, 0.0] |
| 74 | + prob = ODEProblem(f_ms, u0, (0.0, 100.0)) |
| 75 | + |
| 76 | + sol = solve(prob, FBDF(), abstol=1e-15, reltol=1e-8) |
| 77 | + @test sol.retcode == ReturnCode.Success |
| 78 | + @test sol.t[end] == 100.0 |
| 79 | + end |
| 80 | + |
| 81 | + @testset "Stiff Robertson problem" begin |
| 82 | + # Classic stiff test problem |
| 83 | + function robertson!(du, u, p, t) |
| 84 | + du[1] = -0.04 * u[1] + 1e4 * u[2] * u[3] |
| 85 | + du[2] = 0.04 * u[1] - 1e4 * u[2] * u[3] - 3e7 * u[2]^2 |
| 86 | + du[3] = 3e7 * u[2]^2 |
| 87 | + end |
| 88 | + u0 = [1.0, 0.0, 0.0] |
| 89 | + prob = ODEProblem(robertson!, u0, (0.0, 1e5)) |
| 90 | + |
| 91 | + sol = solve(prob, FBDF(), abstol=1e-8, reltol=1e-8) |
| 92 | + @test sol.retcode == ReturnCode.Success |
| 93 | + @test sol.t[end] == 1e5 |
| 94 | + |
| 95 | + # Conservation law: u1 + u2 + u3 = 1 |
| 96 | + @test isapprox(sum(sol.u[end]), 1.0, atol=1e-6) |
| 97 | + end |
| 98 | + |
| 99 | + @testset "Backward integration" begin |
| 100 | + function f_back!(du, u, p, t) |
| 101 | + du[1] = -u[1] |
| 102 | + end |
| 103 | + prob = ODEProblem(f_back!, [exp(-10.0)], (10.0, 0.0)) |
| 104 | + sol = solve(prob, FBDF()) |
| 105 | + @test sol.retcode == ReturnCode.Success |
| 106 | + @test isapprox(sol.u[end][1], 1.0, rtol=2e-2) |
| 107 | + end |
| 108 | + |
| 109 | + @testset "User-specified dt still works" begin |
| 110 | + function f_dt!(du, u, p, t) |
| 111 | + du[1] = -u[1] |
| 112 | + end |
| 113 | + prob = ODEProblem(f_dt!, [1.0], (0.0, 1.0)) |
| 114 | + # When user specifies dt, SundialsInitDt should not be called |
| 115 | + sol = solve(prob, FBDF(), dt=0.01) |
| 116 | + @test sol.retcode == ReturnCode.Success |
| 117 | + end |
| 118 | + |
| 119 | + @testset "QNDF with multi-scale problem" begin |
| 120 | + function f_qndf!(du, u, p, t) |
| 121 | + du[1] = -100.0 * u[1] + 1.0 |
| 122 | + du[2] = 0.0 # starts at 0, stays at 0 |
| 123 | + end |
| 124 | + u0 = [1.0, 0.0] |
| 125 | + prob = ODEProblem(f_qndf!, u0, (0.0, 1.0)) |
| 126 | + |
| 127 | + sol = solve(prob, QNDF(), abstol=1e-12, reltol=1e-8) |
| 128 | + @test sol.retcode == ReturnCode.Success |
| 129 | + @test sol.t[end] == 1.0 |
| 130 | + end |
| 131 | +end |
0 commit comments