|
1 | | -using ForwardDiff, |
2 | | - NonlinearSolve, MINPACK, NLsolve, StaticArrays, Sundials, Test, LinearAlgebra |
| 1 | +@testsetup module ForwardADTesting |
| 2 | +using Reexport, NonlinearSolve |
| 3 | +@reexport using ForwardDiff, MINPACK, NLsolve, StaticArrays, Sundials, LinearAlgebra |
3 | 4 |
|
4 | 5 | test_f!(du, u, p) = (@. du = u^2 - p) |
5 | 6 | test_f(u, p) = (@. u^2 - p) |
@@ -58,49 +59,55 @@ __compatible(::NLsolveJL, ::Val{:oop_cache}) = false |
58 | 59 | __compatible(::KINSOL, ::Val{:iip_cache}) = false |
59 | 60 | __compatible(::KINSOL, ::Val{:oop_cache}) = false |
60 | 61 |
|
61 | | -@testset "ForwardDiff.jl Integration: $(alg)" for alg in (NewtonRaphson(), TrustRegion(), |
62 | | - LevenbergMarquardt(), PseudoTransient(; alpha_initial = 10.0), Broyden(), Klement(), |
63 | | - DFSane(), nothing, NLsolveJL(), CMINPACK(), KINSOL()) |
64 | | - us = (2.0, @SVector[1.0, 1.0], [1.0, 1.0], ones(2, 2), @SArray ones(2, 2)) |
| 62 | +export test_f!, test_f, jacobian_f, solve_with, __compatible |
| 63 | +end |
| 64 | + |
| 65 | +@testitem "ForwardDiff.jl Integration" setup=[ForwardADTesting] begin |
| 66 | + for alg in (NewtonRaphson(), TrustRegion(), |
| 67 | + LevenbergMarquardt(), PseudoTransient(; alpha_initial = 10.0), Broyden(), Klement(), |
| 68 | + DFSane(), nothing, NLsolveJL(), CMINPACK(), |
| 69 | + KINSOL(; globalization_strategy = :LineSearch)) |
| 70 | + us = (2.0, @SVector[1.0, 1.0], [1.0, 1.0], ones(2, 2), @SArray ones(2, 2)) |
65 | 71 |
|
66 | | - @testset "Scalar AD" begin |
67 | | - for p in 1.0:0.1:100.0, u0 in us, mode in (:iip, :oop, :iip_cache, :oop_cache) |
68 | | - __compatible(u0, alg) || continue |
69 | | - __compatible(u0, Val(mode)) || continue |
70 | | - __compatible(alg, Val(mode)) || continue |
| 72 | + @testset "Scalar AD" begin |
| 73 | + for p in 1.0:0.1:100.0, u0 in us, mode in (:iip, :oop, :iip_cache, :oop_cache) |
| 74 | + __compatible(u0, alg) || continue |
| 75 | + __compatible(u0, Val(mode)) || continue |
| 76 | + __compatible(alg, Val(mode)) || continue |
71 | 77 |
|
72 | | - sol = solve(NonlinearProblem(test_f, u0, p), alg) |
73 | | - if SciMLBase.successful_retcode(sol) |
74 | | - gs = abs.(ForwardDiff.derivative(solve_with(Val{mode}(), u0, alg), p)) |
75 | | - gs_true = abs.(jacobian_f(u0, p)) |
76 | | - if !(isapprox(gs, gs_true, atol = 1e-5)) |
77 | | - @show sol.retcode, sol.u |
78 | | - @error "ForwardDiff Failed for u0=$(u0) and p=$(p) with $(alg)" forwardiff_gradient=gs true_gradient=gs_true |
79 | | - else |
80 | | - @test abs.(gs)≈abs.(gs_true) atol=1e-5 |
| 78 | + sol = solve(NonlinearProblem(test_f, u0, p), alg) |
| 79 | + if SciMLBase.successful_retcode(sol) |
| 80 | + gs = abs.(ForwardDiff.derivative(solve_with(Val{mode}(), u0, alg), p)) |
| 81 | + gs_true = abs.(jacobian_f(u0, p)) |
| 82 | + if !(isapprox(gs, gs_true, atol = 1e-5)) |
| 83 | + @show sol.retcode, sol.u |
| 84 | + @error "ForwardDiff Failed for u0=$(u0) and p=$(p) with $(alg)" forwardiff_gradient=gs true_gradient=gs_true |
| 85 | + else |
| 86 | + @test abs.(gs)≈abs.(gs_true) atol=1e-5 |
| 87 | + end |
81 | 88 | end |
82 | 89 | end |
83 | 90 | end |
84 | | - end |
85 | 91 |
|
86 | | - @testset "Jacobian" begin |
87 | | - for u0 in us, p in ([2.0, 1.0], [2.0 1.0; 3.0 4.0]), |
88 | | - mode in (:iip, :oop, :iip_cache, :oop_cache) |
| 92 | + @testset "Jacobian" begin |
| 93 | + for u0 in us, p in ([2.0, 1.0], [2.0 1.0; 3.0 4.0]), |
| 94 | + mode in (:iip, :oop, :iip_cache, :oop_cache) |
89 | 95 |
|
90 | | - __compatible(u0, p) || continue |
91 | | - __compatible(u0, alg) || continue |
92 | | - __compatible(u0, Val(mode)) || continue |
93 | | - __compatible(alg, Val(mode)) || continue |
| 96 | + __compatible(u0, p) || continue |
| 97 | + __compatible(u0, alg) || continue |
| 98 | + __compatible(u0, Val(mode)) || continue |
| 99 | + __compatible(alg, Val(mode)) || continue |
94 | 100 |
|
95 | | - sol = solve(NonlinearProblem(test_f, u0, p), alg) |
96 | | - if SciMLBase.successful_retcode(sol) |
97 | | - gs = abs.(ForwardDiff.jacobian(solve_with(Val{mode}(), u0, alg), p)) |
98 | | - gs_true = abs.(jacobian_f(u0, p)) |
99 | | - if !(isapprox(gs, gs_true, atol = 1e-5)) |
100 | | - @show sol.retcode, sol.u |
101 | | - @error "ForwardDiff Failed for u0=$(u0) and p=$(p) with $(alg)" forwardiff_jacobian=gs true_jacobian=gs_true |
102 | | - else |
103 | | - @test abs.(gs)≈abs.(gs_true) atol=1e-5 |
| 101 | + sol = solve(NonlinearProblem(test_f, u0, p), alg) |
| 102 | + if SciMLBase.successful_retcode(sol) |
| 103 | + gs = abs.(ForwardDiff.jacobian(solve_with(Val{mode}(), u0, alg), p)) |
| 104 | + gs_true = abs.(jacobian_f(u0, p)) |
| 105 | + if !(isapprox(gs, gs_true, atol = 1e-5)) |
| 106 | + @show sol.retcode, sol.u |
| 107 | + @error "ForwardDiff Failed for u0=$(u0) and p=$(p) with $(alg)" forwardiff_jacobian=gs true_jacobian=gs_true |
| 108 | + else |
| 109 | + @test abs.(gs)≈abs.(gs_true) atol=1e-5 |
| 110 | + end |
104 | 111 | end |
105 | 112 | end |
106 | 113 | end |
|
0 commit comments