11using AllocCheck, LinearSolve, SimpleNonlinearSolve, StaticArrays, Random,
2- LinearAlgebra, Test , ForwardDiff, DiffEqBase
2+ LinearAlgebra, XUnit , ForwardDiff, DiffEqBase
33import PolyesterForwardDiff
44
55_nameof (x) = applicable (nameof, x) ? nameof (x) : _nameof (typeof (x))
@@ -25,33 +25,29 @@ const TERMINATION_CONDITIONS = [
2525 AbsSafeTerminationMode (), RelSafeBestTerminationMode (), AbsSafeBestTerminationMode (),
2626]
2727
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
3736
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 ---
4338
39+ @testcase " $(alg) " for alg in (SimpleNewtonRaphson, SimpleTrustRegion)
4440 @testset " AutoDiff: $(_nameof (autodiff)) " for autodiff in (AutoFiniteDiff (),
4541 AutoForwardDiff (), AutoPolyesterForwardDiff ())
4642 @testset " [OOP] u0: $(typeof (u0)) " for u0 in ([1.0 , 1.0 ], @SVector [1.0 , 1.0 ], 1.0 )
4743 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) )
4945 @test SciMLBase. successful_retcode (sol)
5046 @test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
5147 end
5248
5349 @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) )
5551 @test SciMLBase. successful_retcode (sol)
5652 @test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
5753 end
6763
6864# --- SimpleHalley tests ---
6965
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
7667 @testset " AutoDiff: $(_nameof (autodiff)) " for autodiff in (AutoFiniteDiff (),
7768 AutoForwardDiff ())
7869 @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) )
8071 @test SciMLBase. successful_retcode (sol)
8172 @test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
8273 end
9283
9384# --- SimpleBroyden / SimpleKlement / SimpleLimitedMemoryBroyden tests ---
9485
95- @testset " $(_nameof (alg)) " for alg in [SimpleBroyden (), SimpleKlement (), SimpleDFSane (),
86+ @testcase " $(_nameof (alg)) " for alg in [SimpleBroyden (), SimpleKlement (), SimpleDFSane (),
9687 SimpleLimitedMemoryBroyden (), SimpleBroyden (; linesearch = Val (true )),
9788 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-
10889 @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 )
11091 @test SciMLBase. successful_retcode (sol)
11192 @test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
11293 end
11394
11495 @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 )
11697 @test SciMLBase. successful_retcode (sol)
11798 @test all (abs .(sol. u .* sol. u .- 2 ) .< 1e-9 )
11899 end
@@ -126,16 +107,11 @@ end
126107end
127108
128109@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-
134110 u0 = [- 10.0 , - 1.0 , 1.0 , 2.0 , 3.0 , 4.0 , 10.0 ]
135111 p = [0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ]
136112
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)
139115 @test SciMLBase. successful_retcode (sol)
140116 @test all (abs .(newton_fails (sol. u, p)) .< 1e-9 )
141117 end
144120# --- Allocation Checks ---
145121
146122# # 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 (),
148124 SimpleHalley (), SimpleBroyden (), SimpleKlement (), SimpleLimitedMemoryBroyden (),
149125 SimpleTrustRegion (), SimpleDFSane (), SimpleBroyden (; linesearch = Val (true )),
150126 SimpleLimitedMemoryBroyden (; linesearch = Val (true )))
173149
174150# --- Interval Nonlinear Problems ---
175151
176- @testset " Interval Nonlinear Problem: $(alg) " for alg in (Bisection (), Falsi (), Ridder (),
152+ @testcase " Interval Nonlinear Problem: $(alg) " for alg in (Bisection (), Falsi (), Ridder (),
177153 Brent (), ITP (), Alefeld ())
178154 tspan = (1.0 , 20.0 )
179155
215191 end
216192end
217193
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 ())
219196 tspan = (1.0 , 20.0 )
220197 probB = IntervalNonlinearProblem (quadratic_f, tspan, 2.0 )
221198 tols = [0.1 , 0.01 , 0.001 , 0.0001 , 1e-5 , 1e-6 , 1e-7 ]
228205 end
229206end
230207
231- @testset " Tolerance Tests Interval Methods: $(alg) " for alg in (Ridder (), Brent ())
208+ @testcase " Tolerance Tests Interval Methods: $(alg) " for alg in (Ridder (), Brent ())
232209 tspan = (1.0 , 20.0 )
233210 probB = IntervalNonlinearProblem (quadratic_f, tspan, 2.0 )
234211 tols = [0.1 ] # Ridder and Brent converge rapidly so as we lower tolerance below 0.01, it converges with max precision to the solution
241218 end
242219end
243220
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 (),
245222 Falsi (), Brent (), ITP (), Ridder ())
246223 f1 (u, p) = u * u - p
247224 f2 (u, p) = p - u * u
0 commit comments