Skip to content

Commit 5def912

Browse files
committed
Fix all tests
1 parent ba26318 commit 5def912

File tree

11 files changed

+47
-52
lines changed

11 files changed

+47
-52
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NonlinearSolve"
22
uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
33
authors = ["SciML"]
4-
version = "2.9.0"
4+
version = "2.10.0"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
@@ -72,7 +72,7 @@ Reexport = "0.2, 1"
7272
SafeTestsets = "0.1"
7373
SciMLBase = "2.9"
7474
SciMLOperators = "0.3"
75-
SimpleNonlinearSolve = "1"
75+
SimpleNonlinearSolve = "0.1.23"
7676
SparseArrays = "<0.0.1, 1"
7777
SparseDiffTools = "2.14"
7878
StaticArrays = "1"

src/broyden.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function perform_step!(cache::GeneralBroydenCache{iip}) where {iip}
116116
@bb cache.u_cache = transpose(cache.J⁻¹) × vec(cache.du)
117117
denom = dot(cache.du, cache.J⁻¹dfu)
118118
@bb @. cache.du = (cache.du - cache.J⁻¹dfu) / ifelse(iszero(denom), T(1e-5), denom)
119-
@bb cache.J⁻¹ += vec(cache.du) × transpose(cache.u_cache)
119+
@bb cache.J⁻¹ += vec(cache.du) × transpose(_vec(cache.u_cache))
120120
end
121121

122122
@bb copyto!(cache.fu_cache, cache.fu)

src/jacobian.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,6 @@ function __lr_mul(cache::AbstractNonlinearSolveCache, JᵀJ::KrylovJᵀJ, Jᵀf)
262262
return dot(_vec(Jᵀf), _vec(cache.lr_mul_cache))
263263
end
264264
function __lr_mul(cache::AbstractNonlinearSolveCache, JᵀJ, Jᵀf)
265-
@bb cache.lr_mul_cache = JᵀJ × Jᵀf
265+
@bb cache.lr_mul_cache = JᵀJ × vec(Jᵀf)
266266
return dot(_vec(Jᵀf), _vec(cache.lr_mul_cache))
267267
end

src/lbroyden.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function _rmatvec!!(y, xᵀU, U, Vᵀ, x)
187187
x_ = vec(x)
188188
xᵀU_ = view(xᵀU, 1:η)
189189
@bb xᵀU_ = transpose(U) × x_
190-
@bb y = transpose(Vᵀ) × xᵀU_
190+
@bb y = transpose(Vᵀ) × vec(xᵀU_)
191191
@bb @. y -= x
192192
return y
193193
end
@@ -202,7 +202,7 @@ function _matvec!!(y, Vᵀx, U, Vᵀ, x)
202202
x_ = vec(x)
203203
Vᵀx_ = view(Vᵀx, 1:η)
204204
@bb Vᵀx_ = Vᵀ × x_
205-
@bb y = U × Vᵀx_
205+
@bb y = U × vec(Vᵀx_)
206206
@bb @. y -= x
207207
return y
208208
end

src/levenberg.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function SciMLBase.__init(prob::Union{NonlinearProblem{uType, iip},
224224
@bb u_cache = copy(u)
225225
@bb u_cache_2 = similar(u)
226226
@bb fu_cache_2 = similar(fu)
227-
Jv = J * v
227+
Jv = J * _vec(v)
228228
@bb v_cache = zero(v)
229229

230230
return LevenbergMarquardtCache{iip, fastls}(f, alg, u, u_cache, u_cache_2, fu, fu_cache,
@@ -265,16 +265,15 @@ function perform_step!(cache::LevenbergMarquardtCache{iip, fastls}) where {iip,
265265
end
266266
linres = dolinsolve(alg.precs, linsolve; A = cache.mat_tmp,
267267
b = cache.rhs_tmp, linu = _vec(cache.v), cache.p, reltol = cache.abstol)
268-
cache.linsolve = linres.cache
269-
@bb @. cache.v = -linres.u
270268
else
271269
@bb cache.u_cache_2 = transpose(cache.J) × cache.fu
272270
@bb @. cache.mat_tmp = cache.JᵀJ + cache.λ * cache.DᵀD
273271
linres = dolinsolve(alg.precs, linsolve; A = __maybe_symmetric(cache.mat_tmp),
274272
b = _vec(cache.u_cache_2), linu = _vec(cache.v), cache.p, reltol = cache.abstol)
275-
cache.linsolve = linres.cache
276-
@bb @. cache.v = -linres.u
277273
end
274+
cache.linsolve = linres.cache
275+
linu = _restructure(cache.v, linres.u)
276+
@bb @. cache.v = -linu
278277

279278
update_trace!(cache.trace, cache.stats.nsteps + 1, get_u(cache), get_fu(cache), cache.J,
280279
cache.v)
@@ -285,9 +284,9 @@ function perform_step!(cache::LevenbergMarquardtCache{iip, fastls}) where {iip,
285284

286285
# The following lines do: cache.a = -cache.mat_tmp \ cache.fu_tmp
287286
# NOTE: Don't pass `A`` in again, since we want to reuse the previous solve
288-
@bb cache.Jv = cache.J × cache.v
289-
@bb @. cache.fu_cache_2 = (2 / cache.h) *
290-
((cache.fu_cache_2 - cache.fu) / cache.h - cache.Jv)
287+
@bb cache.Jv = cache.J × vec(cache.v)
288+
Jv = _restructure(cache.fu_cache_2, cache.Jv)
289+
@bb @. cache.fu_cache_2 = (2 / cache.h) * ((cache.fu_cache_2 - cache.fu) / cache.h - Jv)
291290
if fastls
292291
if setindex_trait(cache.rhs_tmp) === CanSetindex()
293292
cache.rhs_tmp[1:length(cache.fu)] .= _vec(cache.fu_cache_2)
@@ -296,15 +295,14 @@ function perform_step!(cache::LevenbergMarquardtCache{iip, fastls}) where {iip,
296295
end
297296
linres = dolinsolve(alg.precs, linsolve; b = cache.rhs_tmp, linu = _vec(cache.a),
298297
cache.p, reltol = cache.abstol)
299-
cache.linsolve = linres.cache
300-
@bb @. cache.a = -linres.u
301298
else
302299
@bb cache.u_cache_2 = transpose(cache.J) × cache.fu_cache_2
303300
linres = dolinsolve(alg.precs, linsolve; b = _vec(cache.u_cache_2),
304301
linu = _vec(cache.a), cache.p, reltol = cache.abstol)
305-
cache.linsolve = linres.cache
306-
@bb @. cache.a = -linres.u
307302
end
303+
cache.linsolve = linres.cache
304+
linu = _restructure(cache.a, linres.u)
305+
@bb @. cache.a = -linu
308306

309307
cache.stats.nsolve += 2
310308
cache.stats.nfactors += 2

src/trustRegion.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,16 @@ function dogleg!(cache::TrustRegionCache{iip}) where {iip}
504504
# outside of trust region
505505
l_grad = cache.internalnorm(cache.Jᵀf) # length of the gradient
506506
d_cauchy = l_grad^3 / __lr_mul(cache)
507+
g = _restructure(cache.du, cache.Jᵀf)
507508
if d_cauchy cache.trust_r
508509
# step to the end of the trust region
509-
@bb @. cache.du = -(cache.trust_r / l_grad) * cache.Jᵀf
510+
@bb @. cache.du = -(cache.trust_r / l_grad) * g
510511
return
511512
end
512513

513514
# Take the intersection of dogleg with trust region if Cauchy point lies inside the
514515
# trust region
515-
@bb @. cache.u_cauchy = -(d_cauchy / l_grad) * cache.Jᵀf # compute Cauchy point
516+
@bb @. cache.u_cauchy = -(d_cauchy / l_grad) * g # compute Cauchy point
516517
@bb @. cache.u_cache_2 = cache.u_gauss_newton - cache.u_cauchy # calf of the dogleg
517518

518519
a = dot(cache.u_cache_2, cache.u_cache_2)
@@ -547,13 +548,19 @@ function not_terminated(cache::TrustRegionCache)
547548
return true
548549
end
549550

550-
# FIXME: Update the JacVec Operator for Yuan
551+
# FIXME: Reinit `JᵀJ` operator if `p` is changed
551552
function __reinit_internal!(cache::TrustRegionCache; kwargs...)
553+
if cache.jvp_operator !== nothing
554+
cache.jvp_operator = __jacvec(cache.uf, cache.u; cache.fu,
555+
autodiff = __get_nonsparse_ad(cache.alg.ad))
556+
@bb cache.Jᵀf = cache.jvp_operator × cache.fu
557+
end
552558
cache.loss = __trust_region_loss(cache, cache.fu)
559+
cache.loss_new = cache.loss
553560
cache.shrink_counter = 0
554561
cache.trust_r = convert(eltype(cache.u),
555-
ifelse(cache.alg.initial_trust_radius == 0, cache.alg.initial_trust_radius,
556-
cache.max_trust_r / 11))
562+
ifelse(cache.alg.initial_trust_radius == 0, cache.max_trust_r / 11,
563+
cache.alg.initial_trust_radius))
557564
cache.make_new_J = true
558565
return nothing
559566
end

test/23_test_problems.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ end
9595
alg_ops = (GeneralBroyden(; max_resets = 10),)
9696

9797
broken_tests = Dict(alg => Int[] for alg in alg_ops)
98-
broken_tests[alg_ops[1]] = [1, 2, 4, 5, 6, 11, 12, 13, 14]
98+
broken_tests[alg_ops[1]] = [1, 4, 5, 6, 11, 12, 13, 14]
9999

100100
skip_tests = Dict(alg => Int[] for alg in alg_ops)
101-
skip_tests[alg_ops[1]] = [22]
101+
skip_tests[alg_ops[1]] = [2, 22]
102102

103103
test_on_library(problems, dicts, alg_ops, broken_tests; skip_tests)
104104
end

test/gpu.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,22 @@ A = cu(rand(4, 4))
66
u0 = cu(rand(4))
77
b = cu(rand(4))
88

9-
function f(du, u, p)
10-
du .= A * u .+ b
11-
end
9+
linear_f(du, u, p) = (du .= A * u .+ b)
1210

13-
prob = NonlinearProblem(f, u0)
11+
prob = NonlinearProblem(linear_f, u0)
1412

15-
# TrustRegion is broken
16-
# LimitedMemoryBroyden will diverge!
1713
for alg in (NewtonRaphson(), LevenbergMarquardt(; linsolve = QRFactorization()),
1814
PseudoTransient(; alpha_initial = 1.0f0), GeneralKlement(), GeneralBroyden(),
19-
LimitedMemoryBroyden())
15+
LimitedMemoryBroyden(), TrustRegion())
2016
@test_nowarn sol = solve(prob, alg; abstol = 1.0f-8, reltol = 1.0f-8)
2117
end
2218

23-
f(u, p) = A * u .+ b
19+
linear_f(u, p) = A * u .+ b
2420

25-
prob = NonlinearProblem{false}(f, u0)
21+
prob = NonlinearProblem{false}(linear_f, u0)
2622

27-
# TrustRegion is broken
28-
# LimitedMemoryBroyden will diverge!
2923
for alg in (NewtonRaphson(), LevenbergMarquardt(; linsolve = QRFactorization()),
3024
PseudoTransient(; alpha_initial = 1.0f0), GeneralKlement(), GeneralBroyden(),
31-
LimitedMemoryBroyden())
25+
LimitedMemoryBroyden(), TrustRegion())
3226
@test_nowarn sol = solve(prob, alg; abstol = 1.0f-8, reltol = 1.0f-8)
3327
end

test/infeasible.jl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,10 @@ end
5656
@test all(!isnan, sol.u)
5757
@test !SciMLBase.successful_retcode(sol.retcode)
5858

59-
try
60-
u0 = @SVector [0.0, 0.0, 0.0]
61-
prob = NonlinearProblem(f1, u0)
62-
sol = solve(prob)
59+
u0 = @SVector [0.0, 0.0, 0.0]
60+
prob = NonlinearProblem(f1, u0)
61+
sol = solve(prob)
6362

64-
@test all(!isnan, sol.u)
65-
@test !SciMLBase.successful_retcode(sol.retcode)
66-
catch err
67-
# Static Arrays has different default linearsolve which throws an error
68-
@test err isa SingularException
69-
end
63+
@test all(!isnan, sol.u)
64+
@test !SciMLBase.successful_retcode(sol.retcode)
7065
end

test/matrix_resizing.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ prob = NonlinearProblem(ff, u0, p)
88

99
for alg in (NewtonRaphson(), TrustRegion(), LevenbergMarquardt(), PseudoTransient(),
1010
RobustMultiNewton(), FastShortcutNonlinearPolyalg(), GeneralBroyden(), GeneralKlement(),
11-
LimitedMemoryBroyden())
11+
LimitedMemoryBroyden(; threshold = 2))
1212
@test vec(solve(prob, alg).u) == solve(vecprob, alg).u
1313
end
1414

@@ -19,6 +19,7 @@ vecprob = NonlinearProblem(fiip, vec(u0), p)
1919
prob = NonlinearProblem(fiip, u0, p)
2020

2121
for alg in (NewtonRaphson(), TrustRegion(), LevenbergMarquardt(), PseudoTransient(),
22-
RobustMultiNewton(), FastShortcutNonlinearPolyalg(), GeneralBroyden(), GeneralKlement())
22+
RobustMultiNewton(), FastShortcutNonlinearPolyalg(), GeneralBroyden(), GeneralKlement(),
23+
LimitedMemoryBroyden(; threshold = 2))
2324
@test vec(solve(prob, alg).u) == solve(vecprob, alg).u
2425
end

0 commit comments

Comments
 (0)