@@ -73,6 +73,7 @@ mutable struct LinearCache{TA, Tb, Tu, Tp, Talg, Tc, Tl, Tr, Ttol, issq, S}
7373 alg:: Talg
7474 cacheval:: Tc # store alg cache here
7575 isfresh:: Bool # false => cacheval is set wrt A, true => update cacheval wrt A
76+ precsisfresh:: Bool # false => PR,PL is set wrt A, true => update PR,PL wrt A
7677 Pl:: Tl # preconditioners
7778 Pr:: Tr
7879 abstol:: Ttol
8586
8687function Base. setproperty! (cache:: LinearCache , name:: Symbol , x)
8788 if name === :A
88- if hasproperty (cache. alg, :precs ) && ! isnothing (cache. alg. precs)
89- Pl, Pr = cache. alg. precs (x, cache. p)
90- setfield! (cache, :Pl , Pl)
91- setfield! (cache, :Pr , Pr)
92- end
9389 setfield! (cache, :isfresh , true )
90+ setfield! (cache, :precsisfresh , true )
9491 elseif name === :p
95- if hasproperty (cache. alg, :precs ) && ! isnothing (cache. alg. precs)
96- Pl, Pr = cache. alg. precs (cache. A, x)
97- setfield! (cache, :Pl , Pl)
98- setfield! (cache, :Pr , Pr)
99- end
92+ setfield! (cache, :precsisfresh , true )
10093 elseif name === :b
10194 # In case there is something that needs to be done when b is updated
10295 update_cacheval! (cache, :b , x)
@@ -208,11 +201,12 @@ function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
208201 cacheval = init_cacheval (alg, A, b, u0_, Pl, Pr, maxiters, abstol, reltol, verbose,
209202 assumptions)
210203 isfresh = true
204+ precsisfresh = false
211205 Tc = typeof (cacheval)
212206
213207 cache = LinearCache{typeof (A), typeof (b), typeof (u0_), typeof (p), typeof (alg), Tc,
214208 typeof (Pl), typeof (Pr), typeof (reltol), typeof (assumptions. issq),
215- typeof (sensealg)}(A, b, u0_, p, alg, cacheval, isfresh, Pl, Pr, abstol, reltol,
209+ typeof (sensealg)}(A, b, u0_, p, alg, cacheval, isfresh, precsisfresh, Pl, Pr, abstol, reltol,
216210 maxiters, verbose, assumptions, sensealg)
217211 return cache
218212end
@@ -223,27 +217,26 @@ function SciMLBase.reinit!(cache::LinearCache;
223217 b = cache. b,
224218 u = cache. u,
225219 p = nothing ,
226- reinit_cache = false ,)
220+ reinit_cache = false ,
221+ reuse_precs = false )
227222 (; alg, cacheval, abstol, reltol, maxiters, verbose, assumptions, sensealg) = cache
228223
229- precs = (hasproperty (alg, :precs ) && ! isnothing (alg. precs)) ? alg. precs : DEFAULT_PRECS
230- Pl, Pr = if isnothing (A) || isnothing (p)
231- if isnothing (A)
232- A = cache. A
233- end
234- if isnothing (p)
235- p = cache. p
236- end
237- precs (A, p)
238- else
239- (cache. Pl, cache. Pr)
240- end
241- isfresh = true
242224
225+ isfresh = ! isnothing (A)
226+ precsisfresh = ! reuse_precs && (isfresh || ! isnothing (p))
227+ isfresh |= cache. isfresh
228+ precsisfresh |= cache. precsisfresh
229+
230+ A = isnothing (A) ? cache. A : A
231+ b = isnothing (b) ? cache. b : b
232+ u = isnothing (u) ? cache. u : u
233+ p = isnothing (p) ? cache. p : p
234+ Pl = cache. Pl
235+ Pr = cache. Pr
243236 if reinit_cache
244237 return LinearCache{typeof (A), typeof (b), typeof (u), typeof (p), typeof (alg), typeof (cacheval),
245238 typeof (Pl), typeof (Pr), typeof (reltol), typeof (assumptions. issq),
246- typeof (sensealg)}(A, b, u, p, alg, cacheval, isfresh, Pl, Pr, abstol, reltol,
239+ typeof (sensealg)}(A, b, u, p, alg, cacheval, precsisfresh, isfresh, Pl, Pr, abstol, reltol,
247240 maxiters, verbose, assumptions, sensealg)
248241 else
249242 cache. A = A
@@ -253,6 +246,7 @@ function SciMLBase.reinit!(cache::LinearCache;
253246 cache. Pl = Pl
254247 cache. Pr = Pr
255248 cache. isfresh = true
249+ cache. precsisfresh = precsisfresh
256250 end
257251end
258252
0 commit comments