diff --git a/src/broyden.jl b/src/broyden.jl index a6981a668..6be29a77b 100644 --- a/src/broyden.jl +++ b/src/broyden.jl @@ -101,7 +101,8 @@ function perform_step!(cache::GeneralBroydenCache{true}) else mul!(_vec(J⁻¹df), J⁻¹, _vec(dfu)) mul!(J⁻¹₂, _vec(du)', J⁻¹) - du .= (du .- J⁻¹df) ./ (dot(du, J⁻¹df) .+ T(1e-5)) + denom = dot(du, J⁻¹df) + du .= (du .- J⁻¹df) ./ ifelse(iszero(denom), T(1e-5), denom) mul!(J⁻¹, _vec(du), J⁻¹₂, 1, 1) end fu .= fu2 @@ -136,7 +137,8 @@ function perform_step!(cache::GeneralBroydenCache{false}) else cache.J⁻¹df = _restructure(cache.J⁻¹df, cache.J⁻¹ * _vec(cache.dfu)) cache.J⁻¹₂ = _vec(cache.du)' * cache.J⁻¹ - cache.du = (cache.du .- cache.J⁻¹df) ./ (dot(cache.du, cache.J⁻¹df) .+ T(1e-5)) + denom = dot(cache.du, cache.J⁻¹df) + cache.du = (cache.du .- cache.J⁻¹df) ./ ifelse(iszero(denom), T(1e-5), denom) cache.J⁻¹ = cache.J⁻¹ .+ _vec(cache.du) * cache.J⁻¹₂ end cache.fu = cache.fu2 diff --git a/src/lbroyden.jl b/src/lbroyden.jl index efaa1bb04..d045d0b20 100644 --- a/src/lbroyden.jl +++ b/src/lbroyden.jl @@ -123,8 +123,8 @@ function perform_step!(cache::LimitedMemoryBroydenCache{true}) __lbroyden_matvec!(_vec(cache.vᵀ_cache), cache.Ux, U_part, Vᵀ_part, _vec(cache.du)) __lbroyden_rmatvec!(_vec(cache.u_cache), cache.xᵀVᵀ, U_part, Vᵀ_part, _vec(cache.dfu)) - cache.u_cache .= (du .- cache.u_cache) ./ - (dot(cache.vᵀ_cache, cache.dfu) .+ T(1e-5)) + denom = dot(cache.vᵀ_cache, cache.dfu) + cache.u_cache .= (du .- cache.u_cache) ./ ifelse(iszero(denom), T(1e-5), denom) idx = mod1(cache.iterations_since_reset + 1, size(cache.U, 1)) selectdim(cache.U, 1, idx) .= _vec(cache.u_cache) @@ -179,8 +179,8 @@ function perform_step!(cache::LimitedMemoryBroydenCache{false}) __lbroyden_matvec(U_part, Vᵀ_part, _vec(cache.du))) cache.u_cache = _restructure(cache.u_cache, __lbroyden_rmatvec(U_part, Vᵀ_part, _vec(cache.dfu))) - cache.u_cache = (cache.du .- cache.u_cache) ./ - (dot(cache.vᵀ_cache, cache.dfu) .+ T(1e-5)) + denom = dot(cache.vᵀ_cache, cache.dfu) + cache.u_cache = (cache.du .- cache.u_cache) ./ ifelse(iszero(denom), T(1e-5), denom) idx = mod1(cache.iterations_since_reset + 1, size(cache.U, 1)) selectdim(cache.U, 1, idx) .= _vec(cache.u_cache) diff --git a/test/23_test_problems.jl b/test/23_test_problems.jl index 57f9de68c..a35476dc3 100644 --- a/test/23_test_problems.jl +++ b/test/23_test_problems.jl @@ -7,7 +7,7 @@ function test_on_library(problems, dicts, alg_ops, broken_tests, ϵ = 1e-4) for (idx, (problem, dict)) in enumerate(zip(problems, dicts)) x = dict["start"] res = similar(x) - nlprob = NonlinearProblem(problem, x) + nlprob = NonlinearProblem(problem, copy(x)) @testset "$idx: $(dict["title"])" begin for alg in alg_ops try @@ -86,9 +86,9 @@ end GeneralBroyden(; linesearch = BackTracking())) broken_tests = Dict(alg => Int[] for alg in alg_ops) - broken_tests[alg_ops[1]] = [1, 3, 4, 5, 6, 11, 12, 13, 14] - broken_tests[alg_ops[2]] = [1, 2, 3, 4, 5, 6, 9, 11, 13, 15, 16, 21, 22] - broken_tests[alg_ops[3]] = [1, 2, 4, 5, 6, 11, 12, 13, 14, 21] + broken_tests[alg_ops[1]] = [1, 2, 4, 5, 6, 11, 12, 13, 14] + broken_tests[alg_ops[2]] = [1, 2, 3, 4, 5, 6, 9, 11, 13, 22] + broken_tests[alg_ops[3]] = [1, 4, 5, 6, 11, 12, 13, 14, 21] test_on_library(problems, dicts, alg_ops, broken_tests) end