Skip to content

Commit c428bd9

Browse files
committed
Fix 23 test problems
1 parent fefe476 commit c428bd9

File tree

9 files changed

+45
-81
lines changed

9 files changed

+45
-81
lines changed

src/broyden.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ function SciMLBase.reinit!(cache::GeneralBroydenCache{iip}, u0 = cache.u; p = ca
185185
cache.u = u0
186186
cache.fu = cache.f(cache.u, p)
187187
end
188-
termination_condition = _get_reinit_termination_condition(cache,
189-
abstol,
190-
reltol,
188+
189+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
191190
termination_condition)
192191

193192
cache.abstol = abstol

src/dfsane.jl

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,18 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg::DFSane, args.
114114
𝒹, uₙ₋₁, fuₙ, fuₙ₋₁ = copy(uₙ), copy(uₙ), copy(uₙ), copy(uₙ)
115115

116116
if iip
117-
# f = (dx, x) -> prob.f(dx, x, p)
118-
# f(fuₙ₋₁, uₙ₋₁)
119117
prob.f(fuₙ₋₁, uₙ₋₁, p)
120118
else
121-
# f = (x) -> prob.f(x, p)
122-
fuₙ₋₁ = prob.f(uₙ₋₁, p) # f(uₙ₋₁)
119+
fuₙ₋₁ = prob.f(uₙ₋₁, p)
123120
end
124121

125122
f₍ₙₒᵣₘ₎ₙ₋₁ = norm(fuₙ₋₁)^nₑₓₚ
126123
f₍ₙₒᵣₘ₎₀ = f₍ₙₒᵣₘ₎ₙ₋₁
127124

128125
= fill(f₍ₙₒᵣₘ₎ₙ₋₁, M)
129126

130-
abstol, reltol, termination_condition = _init_termination_elements(abstol,
131-
reltol,
132-
termination_condition,
133-
T)
127+
abstol, reltol, termination_condition = _init_termination_elements(abstol, reltol,
128+
termination_condition, T)
134129

135130
mode = DiffEqBase.get_termination_mode(termination_condition)
136131

@@ -167,14 +162,13 @@ function perform_step!(cache::DFSaneCache{true})
167162

168163
f(cache.fuₙ, cache.uₙ)
169164
f₍ₙₒᵣₘ₎ₙ = norm(cache.fuₙ)^nₑₓₚ
170-
for _ in 1:(cache.alg.max_inner_iterations)
165+
for jjj in 1:(cache.alg.max_inner_iterations)
171166
𝒸 =+ η - γ * α₊^2 * f₍ₙₒᵣₘ₎ₙ₋₁
172167

173168
f₍ₙₒᵣₘ₎ₙ 𝒸 && break
174169

175170
α₊ = clamp(α₊^2 * f₍ₙₒᵣₘ₎ₙ₋₁ / (f₍ₙₒᵣₘ₎ₙ + (T(2) * α₊ - T(1)) * f₍ₙₒᵣₘ₎ₙ₋₁),
176-
τₘᵢₙ * α₊,
177-
τₘₐₓ * α₊)
171+
τₘᵢₙ * α₊, τₘₐₓ * α₊)
178172
@. cache.uₙ = cache.uₙ₋₁ - α₋ * cache.𝒹
179173

180174
f(cache.fuₙ, cache.uₙ)
@@ -183,8 +177,7 @@ function perform_step!(cache::DFSaneCache{true})
183177
f₍ₙₒᵣₘ₎ₙ .≤ 𝒸 && break
184178

185179
α₋ = clamp(α₋^2 * f₍ₙₒᵣₘ₎ₙ₋₁ / (f₍ₙₒᵣₘ₎ₙ + (T(2) * α₋ - T(1)) * f₍ₙₒᵣₘ₎ₙ₋₁),
186-
τₘᵢₙ * α₋,
187-
τₘₐₓ * α₋)
180+
τₘᵢₙ * α₋, τₘₐₓ * α₋)
188181

189182
@. cache.uₙ = cache.uₙ₋₁ + α₊ * cache.𝒹
190183
f(cache.fuₙ, cache.uₙ)
@@ -207,7 +200,7 @@ function perform_step!(cache::DFSaneCache{true})
207200
# Spectral parameter bounds check
208201
if abs(cache.σₙ) > σₘₐₓ || abs(cache.σₙ) < σₘᵢₙ
209202
test_norm = sqrt(sum(abs2, cache.fuₙ₋₁))
210-
cache.σₙ = clamp(1.0 / test_norm, 1, 1e5)
203+
cache.σₙ = clamp(T(1) / test_norm, T(1), T(1e5))
211204
end
212205

213206
# Take step
@@ -283,7 +276,7 @@ function perform_step!(cache::DFSaneCache{false})
283276
# Spectral parameter bounds check
284277
if abs(cache.σₙ) > σₘₐₓ || abs(cache.σₙ) < σₘᵢₙ
285278
test_norm = sqrt(sum(abs2, cache.fuₙ₋₁))
286-
cache.σₙ = clamp(1.0 / test_norm, 1, 1e5)
279+
cache.σₙ = clamp(T(1) / test_norm, T(1), T(1e5))
287280
end
288281

289282
# Take step
@@ -337,9 +330,7 @@ function SciMLBase.reinit!(cache::DFSaneCache{iip}, u0 = cache.uₙ; p = cache.p
337330
T = eltype(cache.uₙ)
338331
cache.σₙ = T(cache.alg.σ_1)
339332

340-
termination_condition = _get_reinit_termination_condition(cache,
341-
abstol,
342-
reltol,
333+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
343334
termination_condition)
344335

345336
cache.abstol = abstol

src/gaussnewton.jl

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ function SciMLBase.__init(prob::NonlinearLeastSquaresProblem{uType, iip}, alg_::
118118
nothing
119119

120120
return GaussNewtonCache{iip}(f, alg, u, copy(u), fu1, fu2, zero(fu1), du, p, uf,
121-
linsolve, J,
122-
JᵀJ, Jᵀf, jac_cache, false, maxiters, internalnorm, ReturnCode.Default, abstol,
123-
reltol,
124-
prob, NLStats(1, 0, 0, 0, 0), storage, termination_condition)
121+
linsolve, J, JᵀJ, Jᵀf, jac_cache, false, maxiters, internalnorm, ReturnCode.Default,
122+
abstol, reltol, prob, NLStats(1, 0, 0, 0, 0), storage, termination_condition)
125123
end
126124

127125
function perform_step!(cache::GaussNewtonCache{true})
@@ -147,10 +145,7 @@ function perform_step!(cache::GaussNewtonCache{true})
147145
@. u = u - du
148146
f(cache.fu_new, u, p)
149147

150-
(termination_condition(cache.fu_new .- cache.fu1,
151-
cache.u,
152-
u_prev,
153-
cache.abstol,
148+
(termination_condition(cache.fu_new .- cache.fu1, cache.u, u_prev, cache.abstol,
154149
cache.reltol) ||
155150
termination_condition(cache.fu_new, cache.u, u_prev, cache.abstol, cache.reltol)) &&
156151
(cache.force_stop = true)
@@ -217,9 +212,8 @@ function SciMLBase.reinit!(cache::GaussNewtonCache{iip}, u0 = cache.u; p = cache
217212
cache.u = u0
218213
cache.fu1 = cache.f(cache.u, p)
219214
end
220-
termination_condition = _get_reinit_termination_condition(cache,
221-
abstol,
222-
reltol,
215+
216+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
223217
termination_condition)
224218

225219
cache.abstol = abstol

src/klement.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ function SciMLBase.reinit!(cache::GeneralKlementCache{iip}, u0 = cache.u; p = ca
238238
cache.fu = cache.f(cache.u, p)
239239
end
240240

241-
termination_condition = _get_reinit_termination_condition(cache,
242-
abstol,
243-
reltol,
241+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
244242
termination_condition)
243+
245244
cache.abstol = abstol
246245
cache.reltol = reltol
247246
cache.termination_condition = termination_condition

src/pseudotransient.jl

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function PseudoTransient(; concrete_jac = nothing, linsolve = nothing,
5151
return PseudoTransient{_unwrap_val(concrete_jac)}(ad, linsolve, precs, alpha_initial)
5252
end
5353

54-
@concrete mutable struct PseudoTransientCache{iip}
54+
@concrete mutable struct PseudoTransientCache{iip} <: AbstractNonlinearSolveCache{iip}
5555
f
5656
alg
5757
u
@@ -78,8 +78,6 @@ end
7878
tc_storage
7979
end
8080

81-
isinplace(::PseudoTransientCache{iip}) where {iip} = iip
82-
8381
function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg_::PseudoTransient,
8482
args...; alias_u0 = false, maxiters = 1000, abstol = nothing, reltol = nothing,
8583
termination_condition = nothing, internalnorm = DEFAULT_NORM,
@@ -174,22 +172,6 @@ function perform_step!(cache::PseudoTransientCache{false})
174172
return nothing
175173
end
176174

177-
function SciMLBase.solve!(cache::PseudoTransientCache)
178-
while !cache.force_stop && cache.stats.nsteps < cache.maxiters
179-
perform_step!(cache)
180-
cache.stats.nsteps += 1
181-
end
182-
183-
if cache.stats.nsteps == cache.maxiters
184-
cache.retcode = ReturnCode.MaxIters
185-
else
186-
cache.retcode = ReturnCode.Success
187-
end
188-
189-
return SciMLBase.build_solution(cache.prob, cache.alg, cache.u, cache.fu1;
190-
cache.retcode, cache.stats)
191-
end
192-
193175
function SciMLBase.reinit!(cache::PseudoTransientCache{iip}, u0 = cache.u; p = cache.p,
194176
alpha_new,
195177
abstol = cache.abstol, reltol = cache.reltol,
@@ -205,9 +187,7 @@ function SciMLBase.reinit!(cache::PseudoTransientCache{iip}, u0 = cache.u; p = c
205187
cache.fu1 = cache.f(cache.u, p)
206188
end
207189

208-
termination_condition = _get_reinit_termination_condition(cache,
209-
abstol,
210-
reltol,
190+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
211191
termination_condition)
212192

213193
cache.alpha = convert(eltype(cache.u), alpha_new)

src/raphson.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,9 @@ function SciMLBase.reinit!(cache::NewtonRaphsonCache{iip}, u0 = cache.u; p = cac
179179
cache.fu1 = cache.f(cache.u, p)
180180
end
181181

182-
termination_condition = _get_reinit_termination_condition(cache,
183-
abstol,
184-
reltol,
182+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
185183
termination_condition)
184+
186185
cache.abstol = abstol
187186
cache.reltol = reltol
188187
cache.termination_condition = termination_condition

src/trustRegion.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,8 @@ function SciMLBase.reinit!(cache::TrustRegionCache{iip}, u0 = cache.u; p = cache
736736
cache.u = u0
737737
cache.fu = cache.f(cache.u, p)
738738
end
739-
termination_condition = _get_reinit_termination_condition(cache,
740-
abstol,
741-
reltol,
739+
740+
termination_condition = _get_reinit_termination_condition(cache, abstol, reltol,
742741
termination_condition)
743742

744743
cache.abstol = abstol

src/utils.jl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,16 @@ end
218218
function _init_termination_elements(abstol, reltol, termination_condition,
219219
::Type{T}; mode = NLSolveTerminationMode.AbsNorm) where {T}
220220
if termination_condition !== nothing
221-
abstol !== nothing ?
222-
(abstol != termination_condition.abstol ?
223-
error("Incompatible absolute tolerances found. The tolerances supplied as the keyword argument and the one supplied in the termination condition should be same.") :
224-
nothing) : nothing
225-
reltol !== nothing ?
226-
(reltol != termination_condition.abstol ?
227-
error("Incompatible relative tolerances found. The tolerances supplied as the keyword argument and the one supplied in the termination condition should be same.") :
228-
nothing) : nothing
221+
if abstol !== nothing && abstol != termination_condition.abstol
222+
error("Incompatible absolute tolerances found. The tolerances supplied as the \
223+
keyword argument and the one supplied in the termination condition should \
224+
be same.")
225+
end
226+
if reltol !== nothing && reltol != termination_condition.reltol
227+
error("Incompatible relative tolerances found. The tolerances supplied as the \
228+
keyword argument and the one supplied in the termination condition should \
229+
be same.")
230+
end
229231
abstol = _get_tolerance(abstol, termination_condition.abstol, T)
230232
reltol = _get_tolerance(reltol, termination_condition.reltol, T)
231233
return abstol, reltol, termination_condition
@@ -239,18 +241,18 @@ end
239241

240242
function _get_reinit_termination_condition(cache, abstol, reltol, termination_condition)
241243
if termination_condition != cache.termination_condition
242-
if abstol != cache.abstol
243-
if abstol != termination_condition.abstol
244-
error("Incompatible absolute tolerances found. The tolerances supplied as the keyword argument and the one supplied in the termination condition should be same.")
245-
end
244+
if abstol != cache.abstol && abstol != termination_condition.abstol
245+
error("Incompatible absolute tolerances found. The tolerances supplied as the \
246+
keyword argument and the one supplied in the termination condition \
247+
should be same.")
246248
end
247249

248-
if reltol != cache.reltol
249-
if reltol != termination_condition.reltol
250-
error("Incompatible absolute tolerances found. The tolerances supplied as the keyword argument and the one supplied in the termination condition should be same.")
251-
end
250+
if reltol != cache.reltol && reltol != termination_condition.reltol
251+
error("Incompatible absolute tolerances found. The tolerances supplied as the \
252+
keyword argument and the one supplied in the termination condition \
253+
should be same.")
252254
end
253-
termination_condition
255+
return termination_condition
254256
else
255257
# Build the termination_condition with new abstol and reltol
256258
return NLSolveTerminationCondition{

test/23_test_problems.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function test_on_library(problems, dicts, alg_ops, broken_tests, ϵ = 1e-4)
1111
@testset "$idx: $(dict["title"])" begin
1212
for alg in alg_ops
1313
try
14-
sol = solve(nlprob, alg, abstol = 1e-18, reltol = 1e-18)
14+
sol = solve(nlprob, alg)
1515
problem(res, sol.u, nothing)
16+
1617
broken = idx in broken_tests[alg] ? true : false
1718
@test norm(res)ϵ broken=broken
1819
catch

0 commit comments

Comments
 (0)