11
22# # Preconditioners
33
4- scaling_preconditioner (s, isleft ) = isleft ? I * s : I * (1 / s)
4+ scaling_preconditioner (s) = I * s , I * (1 / s)
55
66struct ComposePreconditioner{Ti,To}
77 inner:: Ti
@@ -41,6 +41,23 @@ function LinearAlgebra.mul!(y, A::InvComposePreconditioner, x)
4141 ldiv! (y, P, x)
4242end
4343
44+ function get_preconditioner (Pi, Po)
45+
46+ ifPi = Pi != = Identity ()
47+ ifPo = Po != = Identity ()
48+
49+ P =
50+ if ifPi & ifPo
51+ ComposePreconditioner (Pi, Po)
52+ elseif ifPi | ifPo
53+ ifPi ? Pi : Po
54+ else
55+ Identity ()
56+ end
57+
58+ return P
59+ end
60+
4461# # Krylov.jl
4562
4663struct KrylovJL{F,Tl,Tr,I,A,K} <: AbstractKrylovSubspaceMethod
@@ -53,10 +70,14 @@ struct KrylovJL{F,Tl,Tr,I,A,K} <: AbstractKrylovSubspaceMethod
5370 kwargs:: K
5471end
5572
56- function KrylovJL (args... ; KrylovAlg = Krylov. gmres!, Pl= I, Pr= I,
73+ function KrylovJL (args... ; KrylovAlg = Krylov. gmres!,
74+ Pl= nothing , Pr= nothing ,
5775 gmres_restart= 0 , window= 0 ,
5876 kwargs... )
5977
78+ Pl = (Pl === nothing ) ? Identity () : Pl
79+ Pr = (Pr === nothing ) ? Identity () : Pr
80+
6081 return KrylovJL (KrylovAlg, Pl, Pr, gmres_restart, window,
6182 args, kwargs)
6283end
@@ -144,16 +165,11 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
144165 cache = set_cacheval (cache, solver)
145166 end
146167
147- M = I # left precond
148- N = I # right precond
168+ M = get_preconditioner (alg . Pl, cache . Pl)
169+ N = get_preconditioner (alg . Pr, cache . Pr)
149170
150- if (cache. Pl != I) | (alg. Pl != I)
151- M = InvComposePreconditioner (alg. Pl, cache. Pl)
152- end
153-
154- if (cache. Pr != I) | (alg. Pr != I)
155- N = InvComposePreconditioner (alg. Pr, cache. Pr)
156- end
171+ M = (M === Identity ()) ? I : inv (M)
172+ N = (N === Identity ()) ? I : inv (N)
157173
158174 atol = cache. abstol
159175 rtol = cache. reltol
@@ -165,7 +181,7 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
165181 alg. kwargs... )
166182
167183 if cache. cacheval isa Krylov. CgSolver
168- N != I &&
184+ N != = I &&
169185 @warn " $(alg. KrylovAlg) doesn't support right preconditioning."
170186 Krylov. solve! (args... ; M= M,
171187 kwargs... )
@@ -176,7 +192,7 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
176192 Krylov. solve! (args... ; M= M, N= N,
177193 kwargs... )
178194 elseif cache. cacheval isa Krylov. MinresSolver
179- N != I &&
195+ N != = I &&
180196 @warn " $(alg. KrylovAlg) doesn't support right preconditioning."
181197 Krylov. solve! (args... ; M= M,
182198 kwargs... )
200216
201217function IterativeSolversJL (args... ;
202218 generate_iterator = IterativeSolvers. gmres_iterable!,
203- Pl= IterativeSolvers. Identity (),
204- Pr= IterativeSolvers. Identity (),
219+ Pl= nothing , Pr= nothing ,
205220 gmres_restart= 0 , kwargs... )
221+
222+ Pl = (Pl === nothing ) ? Identity () : Pl
223+ Pr = (Pr === nothing ) ? Identity () : Pr
224+
206225 return IterativeSolversJL (generate_iterator, Pl, Pr, gmres_restart,
207226 args, kwargs)
208227end
@@ -227,16 +246,8 @@ IterativeSolversJL_MINRES(args...;kwargs...) =
227246function init_cacheval (alg:: IterativeSolversJL , cache:: LinearCache )
228247 @unpack A, b, u = cache
229248
230- Pl = IterativeSolvers. Identity ()
231- Pr = IterativeSolvers. Identity ()
232-
233- if (cache. Pl != I) | (alg. Pl != IterativeSolvers. Identity ())
234- Pl = ComposePreconditioner (alg. Pl, cache. Pl)
235- end
236-
237- if (cache. Pr != I) | (alg. Pr != IterativeSolvers. Identity ())
238- Pr = ComposePreconditioner (alg. Pr, cache. Pr)
239- end
249+ Pl = get_preconditioner (alg. Pl, cache. Pl)
250+ Pr = get_preconditioner (alg. Pr, cache. Pr)
240251
241252 abstol = cache. abstol
242253 reltol = cache. reltol
@@ -249,15 +260,15 @@ function init_cacheval(alg::IterativeSolversJL, cache::LinearCache)
249260 alg. kwargs... )
250261
251262 iterable = if alg. generate_iterator === IterativeSolvers. cg_iterator!
252- Pr != IterativeSolvers . Identity () &&
263+ Pr != = Identity () &&
253264 @warn " $(alg. generate_iterator) doesn't support right preconditioning"
254265 alg. generate_iterator (u, A, b, Pl;
255266 kwargs... )
256267 elseif alg. generate_iterator === IterativeSolvers. gmres_iterable!
257268 alg. generate_iterator (u, A, b; Pl= Pl, Pr= Pr, restart= restart,
258269 kwargs... )
259270 elseif alg. generate_iterator === IterativeSolvers. bicgstabl_iterator!
260- Pr != IterativeSolvers . Identity () &&
271+ Pr != = Identity () &&
261272 @warn " $(alg. generate_iterator) doesn't support right preconditioning"
262273 alg. generate_iterator (u, A, b, alg. args... ; Pl= Pl,
263274 abstol= abstol, reltol= reltol,
0 commit comments