Skip to content

Commit 57e36d7

Browse files
Measure the first homotopy contraction
Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
1 parent cdfed89 commit 57e36d7

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

lib/NonlinearSolveBase/src/homotopy_sweep.jl

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ function _homotopy_corrector!(
401401
return sol, zero(T), zero(T), false, false
402402
end
403403

404-
residualnormprev = zero(L2_NORM(get_fu(cache)))
405-
observations = 0
404+
residualnormprev = L2_NORM(get_fu(cache))
406405
first_Θ = zero(T)
407406
rejected_Θ = zero(T)
408407
has_Θ = false
@@ -412,19 +411,16 @@ function _homotopy_corrector!(
412411
while not_terminated(cache)
413412
CommonSolve.step!(cache)
414413
residualnorm = L2_NORM(get_fu(cache))
415-
if observations > 0
416-
Θ = iszero(residualnormprev) ? zero(T) : T(residualnorm / residualnormprev)
417-
if !has_Θ
418-
first_Θ = Θ
419-
has_Θ = true
420-
end
421-
if !contraction_rejected && !<= Θreject)
422-
rejected_Θ = Θ
423-
contraction_rejected = true
424-
end
414+
Θ = iszero(residualnormprev) ? zero(T) : T(residualnorm / residualnormprev)
415+
if !has_Θ
416+
first_Θ = Θ
417+
has_Θ = true
418+
end
419+
if !contraction_rejected && !<= Θreject)
420+
rejected_Θ = Θ
421+
contraction_rejected = true
425422
end
426423
residualnormprev = residualnorm
427-
observations += 1
428424
end
429425

430426
sol = CommonSolve.solve!(cache)

test/Core/kantorovich_homotopy_tests__item1.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ function CommonSolve.solve!(cache::SlowContractionCache)
139139
)
140140
end
141141

142+
corrector_prob = NonlinearProblem((u, p) -> [u[1] - 1], [0.0])
143+
corrector_cache = init(corrector_prob, SlowContractionAlgorithm(); maxiters = 2)
144+
corrector_sol, first_Θ, rejected_Θ, has_Θ, contraction_rejected =
145+
NonlinearSolveBase._homotopy_corrector!(corrector_cache, alg, Float64)
146+
@test SciMLBase.successful_retcode(corrector_sol)
147+
@test has_Θ
148+
@test first_Θ 0.99
149+
@test rejected_Θ 0.99
150+
@test contraction_rejected
151+
142152
slowprob = HomotopyProblem((u, p, λ) -> [u[1] - λ], [0.0]; λspan = (0.0, 0.1))
143153
strictsol = solve(
144154
slowprob,

0 commit comments

Comments
 (0)