-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathbdf_convergence_tests.jl
More file actions
142 lines (120 loc) · 4.79 KB
/
bdf_convergence_tests.jl
File metadata and controls
142 lines (120 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# This definitely needs cleaning
using OrdinaryDiffEqBDF, ODEProblemLibrary, DiffEqDevTools, ADTypes, LinearSolve, StaticArrays
using OrdinaryDiffEqNonlinearSolve: NLFunctional, NLAnderson, NonlinearSolveAlg
using Test, Random
Random.seed!(100)
testTol = 0.2
dts = 1 .// 2 .^ (9:-1:5)
dts3 = 1 .// 2 .^ (12:-1:7)
if isempty(VERSION.prerelease)
using Enzyme
end
@testset "Implicit Solver Convergence Tests ($(["out-of-place", "in-place"][i]))" for i in
1:2
prob = (
ODEProblemLibrary.prob_ode_linear,
ODEProblemLibrary.prob_ode_2Dlinear,
)[i]
sim = test_convergence(dts, prob, ABDF2())
@test sim.𝒪est[:final] ≈ 2 atol = testTol
@test sim.𝒪est[:l2] ≈ 2 atol = testTol
@test sim.𝒪est[:l∞] ≈ 2 atol = testTol
sim = test_convergence(dts, prob, ABDF2(nlsolve = NLFunctional()))
@test sim.𝒪est[:final] ≈ 2 atol = testTol
@test sim.𝒪est[:l2] ≈ 2 atol = testTol
@test sim.𝒪est[:l∞] ≈ 2 atol = testTol
# QBDF
sim = test_convergence(dts, prob, QBDF1())
@test sim.𝒪est[:final] ≈ 1 atol = testTol
@test sim.𝒪est[:l2] ≈ 1 atol = testTol
@test sim.𝒪est[:l∞] ≈ 1 atol = testTol
sim = test_convergence(dts, prob, QBDF2())
@test sim.𝒪est[:final] ≈ 2 atol = testTol
@test sim.𝒪est[:l2] ≈ 2 atol = testTol
@test sim.𝒪est[:l∞] ≈ 2 atol = testTol
# QNDF
sim = test_convergence(dts, prob, QNDF1())
@test sim.𝒪est[:final] ≈ 1 atol = testTol
@test sim.𝒪est[:l2] ≈ 1 atol = testTol
@test sim.𝒪est[:l∞] ≈ 1 atol = testTol
sim = test_convergence(dts, prob, QNDF1(autodiff = AutoFiniteDiff()))
@test sim.𝒪est[:final] ≈ 1 atol = testTol
@test sim.𝒪est[:l2] ≈ 1 atol = testTol
@test sim.𝒪est[:l∞] ≈ 1 atol = testTol
if isempty(VERSION.prerelease)
sim = test_convergence(
dts,
prob,
QNDF1(
autodiff = AutoEnzyme(
mode = set_runtime_activity(Enzyme.Forward),
function_annotation = Enzyme.Const
)
)
)
@test sim.𝒪est[:final] ≈ 1 atol = testTol
@test sim.𝒪est[:l2] ≈ 1 atol = testTol
@test sim.𝒪est[:l∞] ≈ 1 atol = testTol
sim = test_convergence(
dts,
prob,
QNDF1(
autodiff = AutoEnzyme(
mode = set_runtime_activity(Enzyme.Forward),
function_annotation = Enzyme.Const
),
linsolve = LinearSolve.KrylovJL()
)
)
@test sim.𝒪est[:final] ≈ 1 atol = testTol
@test sim.𝒪est[:l2] ≈ 1 atol = testTol
@test sim.𝒪est[:l∞] ≈ 1 atol = testTol
end
sim = test_convergence(dts3, prob, QNDF2())
@test sim.𝒪est[:final] ≈ 2 atol = testTol
@test sim.𝒪est[:l2] ≈ 2 atol = testTol
@test sim.𝒪est[:l∞] ≈ 2 atol = testTol
sim = test_convergence(dts, prob, QNDF2(nlsolve = NLFunctional()))
@test sim.𝒪est[:final] ≈ 2 atol = testTol
@test sim.𝒪est[:l2] ≈ 2 atol = testTol
@test sim.𝒪est[:l∞] ≈ 2 atol = testTol
@test_nowarn solve(prob, QNDF())
# MEBDF2
sim21 = test_convergence(dts, prob, MEBDF2(extrapolant = :linear))
@test sim21.𝒪est[:final] ≈ 2 atol = testTol
sim22 = test_convergence(dts, prob, MEBDF2(nlsolve = NLFunctional()), reltol = 1.0e-2)
@test sim22.𝒪est[:final] ≈ 2 atol = testTol
sim23 = test_convergence(dts, prob, MEBDF2(nlsolve = NLAnderson()), reltol = 1.0e-2)
@test sim23.𝒪est[:final] ≈ 2 atol = testTol
sim24 = test_convergence(
dts, prob, MEBDF2(nlsolve = NonlinearSolveAlg()), reltol = 1.0e-2
)
@test sim24.𝒪est[:final] ≈ 2 atol = testTol
#FBDF
@test_nowarn solve(prob, FBDF())
end
@testset "Static Array (SVector) Tests" begin
f_oop(u, p, t) = -0.5 * u
u0_sv = SVector(1.0, 2.0)
prob_sv = ODEProblem(f_oop, u0_sv, (0.0, 1.0))
# QNDF2 has a known startup instability with auto-dt on simple problems;
# skip it to avoid version-dependent @test_broken issues
for alg in (QNDF(), QNDF1(), FBDF())
name = nameof(typeof(alg))
@testset "$name" begin
sol = solve(prob_sv, alg, abstol = 1.0e-8, reltol = 1.0e-8)
@test sol.u[end] isa SVector
@test isapprox(sol.u[end], exp(-0.5) * u0_sv, rtol = 1.0e-3)
end
end
# Scalar
prob_scalar = ODEProblem(f_oop, 1.0, (0.0, 1.0))
for alg in (QNDF(), FBDF())
name = nameof(typeof(alg))
@testset "$name scalar" begin
sol = solve(prob_scalar, alg, abstol = 1.0e-8, reltol = 1.0e-8)
@test sol.u[end] isa Number
@test isapprox(sol.u[end], exp(-0.5), rtol = 1.0e-5)
end
end
end