Skip to content

Commit eb26276

Browse files
committed
Safe condition number
1 parent 93b99af commit eb26276

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/broyden.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ end
5050

5151
function __alg_print_modifiers(alg::GeneralBroyden{IJ, UR}) where {IJ, UR}
5252
modifiers = String[]
53-
IJ !== :identity && push!(modifiers, "init_jacobian = :$(IJ)")
54-
UR !== :good_broyden && push!(modifiers, "update_rule = :$(UR)")
53+
IJ !== :identity && push!(modifiers, "init_jacobian = Val(:$(IJ))")
54+
UR !== :good_broyden && push!(modifiers, "update_rule = Val(:$(UR))")
5555
alg.alpha !== nothing && push!(modifiers, "alpha = $(alg.alpha)")
5656
return modifiers
5757
end
@@ -141,8 +141,8 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg_::GeneralBroyd
141141

142142
abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fu, u,
143143
termination_condition)
144-
trace = init_nonlinearsolve_trace(alg, u, fu, J⁻¹, du; uses_jac_inverse = Val(true),
145-
kwargs...)
144+
trace = init_nonlinearsolve_trace(alg, u, fu, ApplyArray(__zero, J⁻¹), du;
145+
uses_jac_inverse = Val(true), kwargs...)
146146

147147
return GeneralBroydenCache{iip, IJ, UR}(f, alg, u, u_cache, du, fu, fu_cache, dfu, p,
148148
uf, J⁻¹, J⁻¹dfu, inv_alpha, alg.alpha, false, 0, alg.max_resets, maxiters,

src/klement.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949

5050
function __alg_print_modifiers(alg::GeneralKlement{IJ}) where {IJ}
5151
modifiers = String[]
52-
IJ !== :identity && push!(modifiers, "init_jacobian = :$(IJ)")
52+
IJ !== :identity && push!(modifiers, "init_jacobian = Val(:$(IJ))")
5353
alg.alpha !== nothing && push!(modifiers, "alpha = $(alg.alpha)")
5454
return modifiers
5555
end
@@ -231,7 +231,7 @@ function perform_step!(cache::GeneralKlementCache{iip, IJ}) where {iip, IJ}
231231
else
232232
# Klement Updates to the Full Jacobian don't work for most problems, we should
233233
# probably be using the Broyden Update Rule here
234-
@bb @. cache.J_cache = cache.J' ^ 2
234+
@bb @. cache.J_cache = cache.J'^2
235235
@bb @. cache.Jdu = cache.du^2
236236
@bb cache.Jdu_cache = cache.J_cache × vec(cache.Jdu)
237237
@bb cache.Jdu = cache.J × vec(cache.du)

src/trace.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function init_nonlinearsolve_trace(alg, ::Val{show_trace},
174174
print("\nAlgorithm: ")
175175
Base.printstyled(alg, "\n\n"; color = :green, bold = true)
176176
end
177-
J_ = uses_jac_inverse ? (trace_level isa TraceMinimal ? J : inv(J)) : J
177+
J_ = uses_jac_inverse ? (trace_level isa TraceMinimal ? J : __safe_inv(J)) : J
178178
history = __init_trace_history(Val{show_trace}(), trace_level, Val{store_trace}(), u,
179179
fu, J_, δu)
180180
return NonlinearSolveTrace{show_trace, store_trace}(history, trace_level)
@@ -231,7 +231,7 @@ function update_trace!(cache::AbstractNonlinearSolveCache, α = true)
231231
nothing, cache.du, α)
232232
else
233233
update_trace!(trace, cache.stats.nsteps + 1, get_u(cache), get_fu(cache),
234-
ApplyArray(inv, J_inv), cache.du, α)
234+
ApplyArray(__safe_inv, J_inv), cache.du, α)
235235
end
236236
else
237237
update_trace!(trace, cache.stats.nsteps + 1, get_u(cache), get_fu(cache), J,

src/utils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ end
528528
@inline __diag(x::Number) = x
529529

530530
# Safe Inverse: Try to use `inv` but if lu fails use `pinv`
531+
__safe_inv(A::AbstractMatrix) = pinv(A)
531532
function __safe_inv(A::StridedMatrix{T}) where {T}
532533
LinearAlgebra.checksquare(A)
533534
if istriu(A)

0 commit comments

Comments
 (0)