diff --git a/src/common.jl b/src/common.jl index 379b54a26..3f145fc69 100644 --- a/src/common.jl +++ b/src/common.jl @@ -223,7 +223,7 @@ function SciMLBase.reinit!(cache::LinearCache; isfresh = !isnothing(A) - precsisfresh = reuse_precs || isfresh || !isnothing(p) + precsisfresh = !reuse_precs && (isfresh || !isnothing(p)) isfresh |= cache.isfresh precsisfresh |= cache.precsisfresh @@ -246,7 +246,7 @@ function SciMLBase.reinit!(cache::LinearCache; cache.Pl = Pl cache.Pr = Pr cache.isfresh = true - cache.isfresh = true + cache.precsisfresh = precsisfresh end end diff --git a/test/basictests.jl b/test/basictests.jl index 6d274a07f..e9492e4d9 100644 --- a/test/basictests.jl +++ b/test/basictests.jl @@ -284,6 +284,30 @@ end end end + @testset "Reuse precs" begin + num_precs_calls = 0 + + function countingprecs(A, p = nothing) + num_precs_calls += 1 + (BlockJacobiPreconditioner(A, 2), I) + end + + n = 10 + A = spdiagm(-1 => -ones(n - 1), 0 => fill(10.0, n), 1 => -ones(n - 1)) + b = rand(n) + p = LinearProblem(A, b) + x0 = solve(p, KrylovJL_CG(precs = countingprecs, ldiv = false)) + cache = x0.cache + x0 = copy(x0) + for i in 4:(n - 3) + A[i, i + 3] -= 1.0e-4 + A[i - 3, i] -= 1.0e-4 + end + LinearSolve.reinit!(cache; A, reuse_precs = true) + x1 = copy(solve!(cache)) + @test all(x0 .< x1) && num_precs_calls == 1 + end + if VERSION >= v"1.9-" @testset "IterativeSolversJL" begin kwargs = (; gmres_restart = 5)