Skip to content

Commit 871e233

Browse files
authored
Merge branch 'main' into mooncake-ext
2 parents 6f44671 + d1000fe commit 871e233

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

src/algorithms/nonlinear_solver.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,16 @@ function newton(
482482
new_residuals .= guess_update
483483
else
484484
fact∇ =.lu!(∇, check = false)
485-
if !.issuccess(fact∇)
486-
fact∇ =.qr(∇, ℒ.ColumnNorm())
485+
try
486+
if !.issuccess(fact∇)
487+
fact∇ =.qr(∇, ℒ.ColumnNorm())
488+
end
489+
.ldiv!(fact∇, new_residuals)
490+
catch
491+
rel_xtol_reached = typemax(T)
492+
new_residuals_norm = typemax(T)
493+
break
487494
end
488-
.ldiv!(fact∇, new_residuals)
489495
end
490496

491497
guess_update_norm =.norm(new_residuals)
@@ -530,10 +536,16 @@ function newton(
530536
new_residuals .= guess_update
531537
else
532538
fact∇ =.lu!(∇, check = false)
533-
if !.issuccess(fact∇)
534-
fact∇ =.qr(∇, ℒ.ColumnNorm())
539+
try
540+
if !.issuccess(fact∇)
541+
fact∇ =.qr(∇, ℒ.ColumnNorm())
542+
end
543+
.ldiv!(fact∇, new_residuals)
544+
catch
545+
rel_xtol_reached = typemax(T)
546+
new_residuals_norm = typemax(T)
547+
break
535548
end
536-
.ldiv!(fact∇, new_residuals)
537549
end
538550

539551
guess_update_norm =.norm(new_residuals)

src/filter/find_shocks.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,14 @@ function find_shocks(::Val{:LagrangeNewton},
7777
# return x, false
7878
# end
7979

80-
f̂xλp = try
81-
.factorize(fxλp)
80+
try
81+
f̂xλp =.factorize(fxλp)
82+
.ldiv!(Δxλ, f̂xλp, fxλ)
8283
catch
8384
# ℒ.svd(fxλp)
8485
# println("factorization fails")
8586
return x, false
8687
end
87-
88-
# Δxλ = fxλp \ fxλ
89-
.ldiv!(Δxλ, f̂xλp, fxλ)
9088

9189
if !all(isfinite,Δxλ) break end
9290

@@ -283,16 +281,14 @@ function find_shocks(::Val{:LagrangeNewton},
283281
# fXλp = [reshape((2 * 𝐒ⁱ²ᵉ + 6 * 𝐒ⁱ³ᵉ * ℒ.kron(ℒ.I(length(x)), ℒ.kron(ℒ.I(length(x)),x)))' * λ, size(𝐒ⁱ, 2), size(𝐒ⁱ, 2)) - 2*ℒ.I(size(𝐒ⁱ, 2)) (𝐒ⁱ + 2 * 𝐒ⁱ²ᵉ * ℒ.kron(ℒ.I(length(x)), x) + 3 * 𝐒ⁱ³ᵉ * ℒ.kron(ℒ.I(length(x)), ℒ.kron(x, x)))'
284282
# -(𝐒ⁱ + 2 * 𝐒ⁱ²ᵉ * ℒ.kron(ℒ.I(length(x)), x) + 3 * 𝐒ⁱ³ᵉ * ℒ.kron(ℒ.I(length(x)), ℒ.kron(x, x))) zeros(size(𝐒ⁱ, 1),size(𝐒ⁱ, 1))]
285283

286-
f̂xλp = try
287-
.factorize(fxλp)
284+
try
285+
f̂xλp =.factorize(fxλp)
286+
.ldiv!(Δxλ, f̂xλp, fxλ)
288287
catch
289288
# ℒ.svd(fxλp)
290289
# println("factorization fails")
291290
return x, false
292291
end
293-
294-
# Δxλ = fxλp \ fxλ
295-
.ldiv!(Δxλ, f̂xλp, fxλ)
296292

297293
if !all(isfinite,Δxλ) break end
298294

src/filter/inversion.jl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,14 @@ function rrule(::typeof(calculate_inversion_filter_loglikelihood),
729729

730730
copy!(jacct, jacc[i]')
731731

732-
jacc_fact =.factorize(jacct) # otherwise this fails for nshocks > nexo
733-
734-
# λ[i] = jacc[i]' \ x[i] * 2
735-
.ldiv!(λ[i], jacc_fact, x[i])
732+
try
733+
jacc_fact =.factorize(jacct) # otherwise this fails for nshocks > nexo
734+
# λ[i] = jacc[i]' \ x[i] * 2
735+
.ldiv!(λ[i], jacc_fact, x[i])
736+
catch
737+
if opts.verbose println("Inversion filter failed at step $i") end
738+
return -Inf, x -> NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent()
739+
end
736740

737741
.rmul!(λ[i], 2)
738742

@@ -1410,10 +1414,14 @@ function rrule(::typeof(calculate_inversion_filter_loglikelihood),
14101414

14111415
copy!(jacct, jacc[i]')
14121416

1413-
jacc_fact =.factorize(jacct)
1414-
1415-
# λ[i] = jacc[i]' \ x[i] * 2
1416-
.ldiv!(λ[i], jacc_fact, x[i])
1417+
try
1418+
jacc_fact =.factorize(jacct)
1419+
# λ[i] = jacc[i]' \ x[i] * 2
1420+
.ldiv!(λ[i], jacc_fact, x[i])
1421+
catch
1422+
if opts.verbose println("Inversion filter failed at step $i") end
1423+
return -Inf, x -> NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent(), NoTangent()
1424+
end
14171425

14181426
# ℒ.ldiv!(λ[i], jacc_fact', x[i])
14191427
.rmul!(λ[i], 2)

0 commit comments

Comments
 (0)