1
1
using AllocCheck, LinearSolve, SimpleNonlinearSolve, StaticArrays, Random,
2
- LinearAlgebra, Test , ForwardDiff, DiffEqBase
2
+ LinearAlgebra, XUnit , ForwardDiff, DiffEqBase
3
3
import PolyesterForwardDiff
4
4
5
5
_nameof (x) = applicable (nameof, x) ? nameof (x) : _nameof (typeof (x))
@@ -25,33 +25,29 @@ const TERMINATION_CONDITIONS = [
25
25
AbsSafeTerminationMode (), RelSafeBestTerminationMode (), AbsSafeBestTerminationMode (),
26
26
]
27
27
28
- # --- SimpleNewtonRaphson tests ---
29
-
30
- @testset " $(alg) " for alg in (SimpleNewtonRaphson, SimpleTrustRegion)
31
- # Eval else the alg is type unstable
32
- @eval begin
33
- function benchmark_nlsolve_oop (f, u0, p = 2.0 ; autodiff = nothing )
34
- prob = NonlinearProblem {false} (f, u0, p)
35
- return solve (prob, $ (alg)(; autodiff), abstol = 1e-9 )
36
- end
28
+ function benchmark_nlsolve_oop (f:: F , u0, p = 2.0 ; solver) where {F}
29
+ prob = NonlinearProblem {false} (f, u0, p)
30
+ return solve (prob, solver; abstol = 1e-9 )
31
+ end
32
+ function benchmark_nlsolve_iip (f!:: F , u0, p = 2.0 ; solver) where {F}
33
+ prob = NonlinearProblem {true} (f!, u0, p)
34
+ return solve (prob, solver; abstol = 1e-9 )
35
+ end
37
36
38
- function benchmark_nlsolve_iip (f, u0, p = 2.0 ; autodiff = nothing )
39
- prob = NonlinearProblem {true} (f, u0, p)
40
- return solve (prob, $ (alg)(; autodiff), abstol = 1e-9 )
41
- end
42
- end
37
+ # --- SimpleNewtonRaphson tests ---
43
38
39
+ @testcase " $(alg) " for alg in (SimpleNewtonRaphson, SimpleTrustRegion)
44
40
@testset " AutoDiff: $(_nameof (autodiff)) " for autodiff in (AutoFiniteDiff (),
45
41
AutoForwardDiff (), AutoPolyesterForwardDiff ())
46
42
@testset " [OOP] u0: $(typeof (u0)) " for u0 in ([1.0 , 1.0 ], @SVector [1.0 , 1.0 ], 1.0 )
47
43
u0 isa SVector && autodiff isa AutoPolyesterForwardDiff && continue
48
- sol = benchmark_nlsolve_oop (quadratic_f, u0; autodiff)
44
+ sol = benchmark_nlsolve_oop (quadratic_f, u0; solver = alg (; autodiff) )
49
45
@test SciMLBase. successful_retcode (sol)
50
46
@test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
51
47
end
52
48
53
49
@testset " [IIP] u0: $(typeof (u0)) " for u0 in ([1.0 , 1.0 ],)
54
- sol = benchmark_nlsolve_iip (quadratic_f!, u0; autodiff)
50
+ sol = benchmark_nlsolve_iip (quadratic_f!, u0; solver = alg (; autodiff) )
55
51
@test SciMLBase. successful_retcode (sol)
56
52
@test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
57
53
end
67
63
68
64
# --- SimpleHalley tests ---
69
65
70
- @testset " SimpleHalley" begin
71
- function benchmark_nlsolve_oop (f, u0, p = 2.0 ; autodiff = nothing )
72
- prob = NonlinearProblem {false} (f, u0, p)
73
- return solve (prob, SimpleHalley (; autodiff), abstol = 1e-9 )
74
- end
75
-
66
+ @testcase " SimpleHalley" begin
76
67
@testset " AutoDiff: $(_nameof (autodiff)) " for autodiff in (AutoFiniteDiff (),
77
68
AutoForwardDiff ())
78
69
@testset " [OOP] u0: $(typeof (u0)) " for u0 in ([1.0 , 1.0 ], @SVector [1.0 , 1.0 ], 1.0 )
79
- sol = benchmark_nlsolve_oop (quadratic_f, u0; autodiff)
70
+ sol = benchmark_nlsolve_oop (quadratic_f, u0; solver = SimpleHalley (; autodiff) )
80
71
@test SciMLBase. successful_retcode (sol)
81
72
@test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
82
73
end
92
83
93
84
# --- SimpleBroyden / SimpleKlement / SimpleLimitedMemoryBroyden tests ---
94
85
95
- @testset " $(_nameof (alg)) " for alg in [SimpleBroyden (), SimpleKlement (), SimpleDFSane (),
86
+ @testcase " $(_nameof (alg)) " for alg in [SimpleBroyden (), SimpleKlement (), SimpleDFSane (),
96
87
SimpleLimitedMemoryBroyden (), SimpleBroyden (; linesearch = Val (true )),
97
88
SimpleLimitedMemoryBroyden (; linesearch = Val (true ))]
98
- function benchmark_nlsolve_oop (f, u0, p = 2.0 )
99
- prob = NonlinearProblem {false} (f, u0, p)
100
- return solve (prob, alg, abstol = 1e-9 )
101
- end
102
-
103
- function benchmark_nlsolve_iip (f, u0, p = 2.0 )
104
- prob = NonlinearProblem {true} (f, u0, p)
105
- return solve (prob, alg, abstol = 1e-9 )
106
- end
107
-
108
89
@testset " [OOP] u0: $(typeof (u0)) " for u0 in ([1.0 , 1.0 ], @SVector [1.0 , 1.0 ], 1.0 )
109
- sol = benchmark_nlsolve_oop (quadratic_f, u0)
90
+ sol = benchmark_nlsolve_oop (quadratic_f, u0; solver = alg )
110
91
@test SciMLBase. successful_retcode (sol)
111
92
@test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
112
93
end
113
94
114
95
@testset " [IIP] u0: $(typeof (u0)) " for u0 in ([1.0 , 1.0 ],)
115
- sol = benchmark_nlsolve_iip (quadratic_f!, u0)
96
+ sol = benchmark_nlsolve_iip (quadratic_f!, u0; solver = alg )
116
97
@test SciMLBase. successful_retcode (sol)
117
98
@test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
118
99
end
@@ -126,16 +107,11 @@ end
126
107
end
127
108
128
109
@testset " Newton Fails" begin
129
- function benchmark_nlsolve_oop (f, u0, p, alg)
130
- prob = NonlinearProblem {false} (f, u0, p)
131
- return solve (prob, alg; abstol = 1e-9 )
132
- end
133
-
134
110
u0 = [- 10.0 , - 1.0 , 1.0 , 2.0 , 3.0 , 4.0 , 10.0 ]
135
111
p = [0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ]
136
112
137
- for alg in (SimpleDFSane (), SimpleTrustRegion (), SimpleHalley ())
138
- sol = benchmark_nlsolve_oop (newton_fails, u0, p, alg)
113
+ @testcase " $(alg) " for alg in (SimpleDFSane (), SimpleTrustRegion (), SimpleHalley ())
114
+ sol = benchmark_nlsolve_oop (newton_fails, u0, p; solver = alg)
139
115
@test SciMLBase. successful_retcode (sol)
140
116
@test all (abs .(newton_fails (sol. u, p)) .< 1e-9 )
141
117
end
144
120
# --- Allocation Checks ---
145
121
146
122
# # SimpleDFSane needs to allocate a history vector
147
- @testset " Allocation Checks: $(_nameof (alg)) " for alg in (SimpleNewtonRaphson (),
123
+ @testcase " Allocation Checks: $(_nameof (alg)) " for alg in (SimpleNewtonRaphson (),
148
124
SimpleHalley (), SimpleBroyden (), SimpleKlement (), SimpleLimitedMemoryBroyden (),
149
125
SimpleTrustRegion (), SimpleDFSane (), SimpleBroyden (; linesearch = Val (true )),
150
126
SimpleLimitedMemoryBroyden (; linesearch = Val (true )))
173
149
174
150
# --- Interval Nonlinear Problems ---
175
151
176
- @testset " Interval Nonlinear Problem: $(alg) " for alg in (Bisection (), Falsi (), Ridder (),
152
+ @testcase " Interval Nonlinear Problem: $(alg) " for alg in (Bisection (), Falsi (), Ridder (),
177
153
Brent (), ITP (), Alefeld ())
178
154
tspan = (1.0 , 20.0 )
179
155
215
191
end
216
192
end
217
193
218
- @testset " Tolerance Tests Interval Methods: $(alg) " for alg in (Bisection (), Falsi (), ITP ())
194
+ @testcase " Tolerance Tests Interval Methods: $(alg) " for alg in (Bisection (), Falsi (),
195
+ ITP ())
219
196
tspan = (1.0 , 20.0 )
220
197
probB = IntervalNonlinearProblem (quadratic_f, tspan, 2.0 )
221
198
tols = [0.1 , 0.01 , 0.001 , 0.0001 , 1e-5 , 1e-6 , 1e-7 ]
228
205
end
229
206
end
230
207
231
- @testset " Tolerance Tests Interval Methods: $(alg) " for alg in (Ridder (), Brent ())
208
+ @testcase " Tolerance Tests Interval Methods: $(alg) " for alg in (Ridder (), Brent ())
232
209
tspan = (1.0 , 20.0 )
233
210
probB = IntervalNonlinearProblem (quadratic_f, tspan, 2.0 )
234
211
tols = [0.1 ] # Ridder and Brent converge rapidly so as we lower tolerance below 0.01, it converges with max precision to the solution
241
218
end
242
219
end
243
220
244
- @testset " Flipped Signs and Reversed Tspan: $(alg) " for alg in (Alefeld (), Bisection (),
221
+ @testcase " Flipped Signs and Reversed Tspan: $(alg) " for alg in (Alefeld (), Bisection (),
245
222
Falsi (), Brent (), ITP (), Ridder ())
246
223
f1 (u, p) = u * u - p
247
224
f2 (u, p) = p - u * u
0 commit comments