Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit fc849d6

Browse files
committed
Fix the tests
1 parent 9f31a04 commit fc849d6

File tree

12 files changed

+228
-213
lines changed

12 files changed

+228
-213
lines changed

src/ad.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
function scalar_nlsolve_ad(prob, alg, args...; kwargs...)
22
f = prob.f
33
p = value(prob.p)
4-
u0 = value(prob.u0)
54
if prob isa IntervalNonlinearProblem
6-
tspan = value(prob.tspan)
5+
tspan = value.(prob.tspan)
76
newprob = IntervalNonlinearProblem(f, tspan, p; prob.kwargs...)
87
else
98
u0 = value(prob.u0)

src/bracketing/bisection.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ function __bisection(left, right, fl, fr, f::F; abstol, maxiters, prob, alg) whe
8686
end
8787

8888
fm = f(mid)
89-
if abs((right - left) / 2) < abstol || abs(fm) < abstol
89+
if abs((right - left) / 2) < abstol
9090
sol = build_solution(prob, alg, mid, fm; left, right,
9191
retcode = ReturnCode.Success)
9292
break
9393
end
9494

95-
if sign(fl * fm) < 0
96-
right, fr = mid, fm
95+
if iszero(fm)
96+
right = mid
97+
fr = fm
9798
else
98-
left, fl = mid, fm
99+
left = mid
100+
fl = fm
99101
end
100102

101103
i += 1

src/nlsolve/broyden.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleBroyden, args...;
4343

4444
@bb @. δJ⁻¹n = (δx - J⁻¹δf) / d
4545

46-
@bb δJ⁻¹ = δJ⁻¹n × transpose(xᵀJ⁻¹)
46+
δJ⁻¹n_ = _vec(δJ⁻¹n)
47+
xᵀJ⁻¹_ = _vec(xᵀJ⁻¹)
48+
@bb δJ⁻¹ = δJ⁻¹n_ × transpose(xᵀJ⁻¹_)
4749
@bb J⁻¹ .+= δJ⁻¹
4850

4951
@bb copyto!(xo, x)

src/nlsolve/dfsane.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane, args...;
7272
fx_norm = norm(fx)^nexp
7373
α_1 = one(T)
7474
f_1 = fx_norm
75+
7576
history_f_k = fill(fx_norm, M)
7677

7778
# Generate the cache
@@ -118,6 +119,8 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane, args...;
118119

119120
fx = __eval_f(prob, fx, x)
120121
fx_norm_new = norm(fx)^nexp
122+
123+
k += 1
121124
end
122125

123126
tc_sol = check_termination(tc_cache, fx, x, xo, prob, alg)

src/nlsolve/halley.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleHalley, args...;
5858
@bb A .*= -1
5959
bᵢ = dfx \ Aaᵢ
6060

61-
@bb @. cᵢ = (aᵢ * aᵢ) / (-aᵢ + (T(0.5) * bᵢ))
61+
cᵢ_ = _vec(cᵢ)
62+
@bb @. cᵢ_ = (aᵢ * aᵢ) / (-aᵢ + (T(0.5) * bᵢ))
63+
cᵢ = _restructure(cᵢ, cᵢ_)
6264

6365
if i == 1
6466
if iszero(fx)

src/nlsolve/klement.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleKlement, args...;
5555

5656
@bb copyto!(δx, fprev)
5757
if setindex_trait(δx) === CanSetindex()
58-
ldiv!(F_, δx)
58+
ldiv!(F_, _vec(δx))
5959
else
60-
δx = F_ \ δx
60+
δx = _restructure(δx, F_ \ _vec(δx))
6161
end
6262
@bb @. x = xo - δx
6363
fx = __eval_f(prob, fx, x)

src/nlsolve/trustRegion.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleTrustRegion, args.
7676

7777
fₖ = 0.5 * norm(fx)^2
7878
H = ∇f' * ∇f
79-
g = ∇f' * fx
79+
g = _restructure(x, ∇f' * _vec(fx))
8080
shrink_counter = 0
8181

8282
@bb δsd = copy(x)
@@ -96,7 +96,8 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleTrustRegion, args.
9696
fₖ₊₁ = norm(fx)^2 / T(2)
9797

9898
# Compute the ratio of the actual to predicted reduction.
99-
@bb= H × δ
99+
# @show size(H), size(δ)
100+
@bb= H × vec(δ)
100101
r = (fₖ₊₁ - fₖ) / (dot', g) + dot', Hδ) / T(2))
101102

102103
# Update the trust region radius.
@@ -124,7 +125,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleTrustRegion, args.
124125
fₖ = fₖ₊₁
125126

126127
@bb H = transpose(∇f) × ∇f
127-
@bb g = transpose(∇f) × fx
128+
@bb g = transpose(∇f) × vec(fx)
128129
end
129130
end
130131

@@ -135,7 +136,7 @@ function dogleg_method!!(cache, J, f, g, Δ)
135136
(; δsd, δN_δsd, δN) = cache
136137

137138
# Compute the Newton step.
138-
@bb δN .= J \ f
139+
@bb δN .= _restructure(δN, J \ _vec(f))
139140
@bb δN .*= -1
140141
# Test if the full step is within the trust region.
141142
(norm(δN) Δ) && return δN

test/23_test_problems.jl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using SimpleNonlinearSolve, LinearAlgebra, NonlinearProblemLibrary, Test
2+
3+
problems = NonlinearProblemLibrary.problems
4+
dicts = NonlinearProblemLibrary.dicts
5+
6+
function test_on_library(problems, dicts, alg_ops, broken_tests, ϵ = 1e-4;
7+
skip_tests = nothing)
8+
for (idx, (problem, dict)) in enumerate(zip(problems, dicts))
9+
x = dict["start"]
10+
res = similar(x)
11+
nlprob = NonlinearProblem(problem, copy(x))
12+
@testset "$idx: $(dict["title"])" begin
13+
for alg in alg_ops
14+
try
15+
sol = solve(nlprob, alg;
16+
termination_condition = AbsNormTerminationMode())
17+
problem(res, sol.u, nothing)
18+
19+
skip = skip_tests !== nothing && idx in skip_tests[alg]
20+
if skip
21+
@test_skip norm(res) ϵ
22+
continue
23+
end
24+
broken = idx in broken_tests[alg] ? true : false
25+
@test norm(res)ϵ broken=broken
26+
catch
27+
broken = idx in broken_tests[alg] ? true : false
28+
if broken
29+
@test false broken=true
30+
else
31+
@test 1 == 2
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end
38+
39+
@testset "SimpleNewtonRaphson 23 Test Problems" begin
40+
alg_ops = (SimpleNewtonRaphson(),)
41+
42+
# dictionary with indices of test problems where method does not converge to small residual
43+
broken_tests = Dict(alg => Int[] for alg in alg_ops)
44+
broken_tests[alg_ops[1]] = [6]
45+
46+
test_on_library(problems, dicts, alg_ops, broken_tests)
47+
end
48+
49+
@testset "SimpleTrustRegion 23 Test Problems" begin
50+
alg_ops = (SimpleTrustRegion(),)
51+
52+
# dictionary with indices of test problems where method does not converge to small residual
53+
broken_tests = Dict(alg => Int[] for alg in alg_ops)
54+
broken_tests[alg_ops[1]] = [3, 6, 15, 16, 21]
55+
56+
test_on_library(problems, dicts, alg_ops, broken_tests)
57+
end
58+
59+
@testset "SimpleDFSane 23 Test Problems" begin
60+
alg_ops = (SimpleDFSane(),)
61+
62+
broken_tests = Dict(alg => Int[] for alg in alg_ops)
63+
broken_tests[alg_ops[1]] = [1, 2, 3, 4, 5, 6, 7, 9, 11, 12, 13, 15, 16, 17, 21, 22]
64+
65+
test_on_library(problems, dicts, alg_ops, broken_tests)
66+
end
67+
68+
@testset "SimpleBroyden 23 Test Problems" begin
69+
alg_ops = (SimpleBroyden(),)
70+
71+
broken_tests = Dict(alg => Int[] for alg in alg_ops)
72+
broken_tests[alg_ops[1]] = [1, 2, 4, 5, 6, 11, 12, 13, 14]
73+
74+
skip_tests = Dict(alg => Int[] for alg in alg_ops)
75+
skip_tests[alg_ops[1]] = [22]
76+
77+
test_on_library(problems, dicts, alg_ops, broken_tests; skip_tests)
78+
end
79+
80+
@testset "SimpleKlement 23 Test Problems" begin
81+
alg_ops = (SimpleKlement(),)
82+
83+
broken_tests = Dict(alg => Int[] for alg in alg_ops)
84+
broken_tests[alg_ops[1]] = [1, 2, 4, 5, 6, 7, 9, 10, 11, 12, 13, 19, 21, 22]
85+
86+
test_on_library(problems, dicts, alg_ops, broken_tests)
87+
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
44
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
55
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
66
LinearSolve = "7ed4a6bd-45f5-4d41-b270-4a48e9bafcae"
7+
NonlinearProblemLibrary = "b7050fa9-e91f-4b37-bcee-a89a063da141"
78
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
89
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
910
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"

0 commit comments

Comments
 (0)