8585
8686function Base. setproperty! (cache:: LinearCache , name:: Symbol , x)
8787 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
8893 setfield! (cache, :isfresh , true )
94+ 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
89100 elseif name === :b
90101 # In case there is something that needs to be done when b is updated
91102 update_cacheval! (cache, :b , x)
@@ -121,6 +132,8 @@ default_alias_b(::Any, ::Any, ::Any) = false
121132default_alias_A (:: AbstractKrylovSubspaceMethod , :: Any , :: Any ) = true
122133default_alias_b (:: AbstractKrylovSubspaceMethod , :: Any , :: Any ) = true
123134
135+ DEFAULT_PRECS (A, p) = IdentityOperator (size (A)[1 ]), IdentityOperator (size (A)[2 ])
136+
124137function __init_u0_from_Ab (A, b)
125138 u0 = similar (b, size (A, 2 ))
126139 fill! (u0, false )
@@ -136,12 +149,12 @@ function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
136149 reltol = default_tol (real (eltype (prob. b))),
137150 maxiters:: Int = length (prob. b),
138151 verbose:: Bool = false ,
139- Pl = IdentityOperator ( size (prob . A)[ 1 ]) ,
140- Pr = IdentityOperator ( size (prob . A)[ 2 ]) ,
152+ Pl = nothing ,
153+ Pr = nothing ,
141154 assumptions = OperatorAssumptions (issquare (prob. A)),
142155 sensealg = LinearSolveAdjoint (),
143156 kwargs... )
144- @unpack A, b, u0, p = prob
157+ (; A, b, u0, p) = prob
145158
146159 A = if alias_A || A isa SMatrix
147160 A
@@ -167,6 +180,24 @@ function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
167180 reltol = real (eltype (prob. b))(reltol)
168181 abstol = real (eltype (prob. b))(abstol)
169182
183+ precs = if hasproperty (alg, :precs )
184+ isnothing (alg. precs) ? DEFAULT_PRECS : alg. precs
185+ else
186+ DEFAULT_PRECS
187+ end
188+ _Pl, _Pr = precs (A, p)
189+ if isnothing (Pl)
190+ Pl = _Pl
191+ else
192+ # TODO : deprecate once all docs are updated to the new form
193+ # @warn "passing Preconditioners at `init`/`solve` time is deprecated. Instead add a `precs` function to your algorithm."
194+ end
195+ if isnothing (Pr)
196+ Pr = _Pr
197+ else
198+ # TODO : deprecate once all docs are updated to the new form
199+ # @warn "passing Preconditioners at `init`/`solve` time is deprecated. Instead add a `precs` function to your algorithm."
200+ end
170201 cacheval = init_cacheval (alg, A, b, u0_, Pl, Pr, maxiters, abstol, reltol, verbose,
171202 assumptions)
172203 isfresh = true
@@ -179,6 +210,45 @@ function SciMLBase.init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
179210 return cache
180211end
181212
213+
214+ function SciMLBase. reinit! (cache:: LinearCache ;
215+ A = nothing ,
216+ b = cache. b,
217+ u = cache. u,
218+ p = nothing ,
219+ reinit_cache = false ,)
220+ (; alg, cacheval, abstol, reltol, maxiters, verbose, assumptions, sensealg) = cache
221+
222+ precs = (hasproperty (alg, :precs ) && ! isnothing (alg. precs)) ? alg. precs : DEFAULT_PRECS
223+ Pl, Pr = if isnothing (A) || isnothing (p)
224+ if isnothing (A)
225+ A = cache. A
226+ end
227+ if isnothing (p)
228+ p = cache. p
229+ end
230+ precs (A, p)
231+ else
232+ (cache. Pl, cache. Pr)
233+ end
234+ isfresh = true
235+
236+ if reinit_cache
237+ return LinearCache{typeof (A), typeof (b), typeof (u), typeof (p), typeof (alg), typeof (cacheval),
238+ typeof (Pl), typeof (Pr), typeof (reltol), typeof (assumptions. issq),
239+ typeof (sensealg)}(A, b, u, p, alg, cacheval, isfresh, Pl, Pr, abstol, reltol,
240+ maxiters, verbose, assumptions, sensealg)
241+ else
242+ cache. A = A
243+ cache. b = b
244+ cache. u = u
245+ cache. p = p
246+ cache. Pl = Pl
247+ cache. Pr = Pr
248+ cache. isfresh = true
249+ end
250+ end
251+
182252function SciMLBase. solve (prob:: LinearProblem , args... ; kwargs... )
183253 return solve (prob, nothing , args... ; kwargs... )
184254end
0 commit comments