1
1
2
2
# # Preconditioners
3
3
4
- scaling_preconditioner (s, isleft ) = isleft ? I * s : I * (1 / s)
4
+ scaling_preconditioner (s) = I * s , I * (1 / s)
5
5
6
6
struct ComposePreconditioner{Ti,To}
7
7
inner:: Ti
@@ -41,6 +41,23 @@ function LinearAlgebra.mul!(y, A::InvComposePreconditioner, x)
41
41
ldiv! (y, P, x)
42
42
end
43
43
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
+
44
61
# # Krylov.jl
45
62
46
63
struct KrylovJL{F,Tl,Tr,I,A,K} <: AbstractKrylovSubspaceMethod
@@ -53,10 +70,14 @@ struct KrylovJL{F,Tl,Tr,I,A,K} <: AbstractKrylovSubspaceMethod
53
70
kwargs:: K
54
71
end
55
72
56
- function KrylovJL (args... ; KrylovAlg = Krylov. gmres!, Pl= I, Pr= I,
73
+ function KrylovJL (args... ; KrylovAlg = Krylov. gmres!,
74
+ Pl= nothing , Pr= nothing ,
57
75
gmres_restart= 0 , window= 0 ,
58
76
kwargs... )
59
77
78
+ Pl = (Pl === nothing ) ? Identity () : Pl
79
+ Pr = (Pr === nothing ) ? Identity () : Pr
80
+
60
81
return KrylovJL (KrylovAlg, Pl, Pr, gmres_restart, window,
61
82
args, kwargs)
62
83
end
@@ -144,16 +165,11 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
144
165
cache = set_cacheval (cache, solver)
145
166
end
146
167
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)
149
170
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)
157
173
158
174
atol = cache. abstol
159
175
rtol = cache. reltol
@@ -165,7 +181,7 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
165
181
alg. kwargs... )
166
182
167
183
if cache. cacheval isa Krylov. CgSolver
168
- N != I &&
184
+ N != = I &&
169
185
@warn " $(alg. KrylovAlg) doesn't support right preconditioning."
170
186
Krylov. solve! (args... ; M= M,
171
187
kwargs... )
@@ -176,7 +192,7 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
176
192
Krylov. solve! (args... ; M= M, N= N,
177
193
kwargs... )
178
194
elseif cache. cacheval isa Krylov. MinresSolver
179
- N != I &&
195
+ N != = I &&
180
196
@warn " $(alg. KrylovAlg) doesn't support right preconditioning."
181
197
Krylov. solve! (args... ; M= M,
182
198
kwargs... )
200
216
201
217
function IterativeSolversJL (args... ;
202
218
generate_iterator = IterativeSolvers. gmres_iterable!,
203
- Pl= IterativeSolvers. Identity (),
204
- Pr= IterativeSolvers. Identity (),
219
+ Pl= nothing , Pr= nothing ,
205
220
gmres_restart= 0 , kwargs... )
221
+
222
+ Pl = (Pl === nothing ) ? Identity () : Pl
223
+ Pr = (Pr === nothing ) ? Identity () : Pr
224
+
206
225
return IterativeSolversJL (generate_iterator, Pl, Pr, gmres_restart,
207
226
args, kwargs)
208
227
end
@@ -227,16 +246,8 @@ IterativeSolversJL_MINRES(args...;kwargs...) =
227
246
function init_cacheval (alg:: IterativeSolversJL , cache:: LinearCache )
228
247
@unpack A, b, u = cache
229
248
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)
240
251
241
252
abstol = cache. abstol
242
253
reltol = cache. reltol
@@ -249,15 +260,15 @@ function init_cacheval(alg::IterativeSolversJL, cache::LinearCache)
249
260
alg. kwargs... )
250
261
251
262
iterable = if alg. generate_iterator === IterativeSolvers. cg_iterator!
252
- Pr != IterativeSolvers . Identity () &&
263
+ Pr != = Identity () &&
253
264
@warn " $(alg. generate_iterator) doesn't support right preconditioning"
254
265
alg. generate_iterator (u, A, b, Pl;
255
266
kwargs... )
256
267
elseif alg. generate_iterator === IterativeSolvers. gmres_iterable!
257
268
alg. generate_iterator (u, A, b; Pl= Pl, Pr= Pr, restart= restart,
258
269
kwargs... )
259
270
elseif alg. generate_iterator === IterativeSolvers. bicgstabl_iterator!
260
- Pr != IterativeSolvers . Identity () &&
271
+ Pr != = Identity () &&
261
272
@warn " $(alg. generate_iterator) doesn't support right preconditioning"
262
273
alg. generate_iterator (u, A, b, alg. args... ; Pl= Pl,
263
274
abstol= abstol, reltol= reltol,
0 commit comments