1
1
2
2
# # Preconditioners
3
3
4
+ """
5
+ P * x = x .* (1/P.s)
6
+
7
+ Pi * x = x .* (P.s)
8
+ """
4
9
struct ScaleVector{T}
5
10
s:: T
6
11
isleft:: Bool
7
12
end
8
13
9
- function LinearAlgebra. ldiv! (P:: ScaleVector , x)
10
- P. s == one (eltype (P. s)) && return x
14
+ function LinearAlgebra. mul! (y, A:: ScaleVector , x)
15
+ end
16
+
17
+ function LinearAlgebra. mul! (C, A:: ScaleVector , B, α, β)
18
+ end
19
+
20
+ function LinearAlgebra. ldiv! (A:: ScaleVector , x)
21
+ A. s == one (eltype (A. s)) && return x
11
22
12
- if P . isleft
13
- @. x = x * P . s # @.. doesnt speed up scalar computation
23
+ if A . isleft
24
+ @. x = x * A . s
14
25
else
15
- @. x = x / P . s
26
+ @. x = x / A . s
16
27
end
17
28
end
18
29
19
- function LinearAlgebra. ldiv! (y, P :: ScaleVector , x)
20
- P. s == one (eltype (P . s)) && return y = x
30
+ function LinearAlgebra. ldiv! (y, A :: ScaleVector , x)
31
+ P. s == one (eltype (A . s)) && return y = x
21
32
22
- if P . isleft
23
- @. y = x / P . s
33
+ if A . isleft
34
+ @. y = x * A . s
24
35
else
25
- @. y = x * P . s
36
+ @. y = x / A . s
26
37
end
27
38
end
28
39
40
+ Base. eltype (A:: ScaleVector ) = eltype (A. s)
41
+
42
+ """
43
+ C * x = P * Q * x
44
+
45
+ Ci * x = Qi * Pi * x
46
+ """
29
47
struct ComposePreconditioner{Ti,To}
30
48
inner:: Ti
31
49
outer:: To
32
- isleft:: Bool
33
50
end
34
51
35
- function LinearAlgebra. ldiv! (P:: ComposePreconditioner , x)
36
- @unpack inner, outer, isleft = P
37
- if isleft
38
- ldiv! (outer, x)
39
- ldiv! (inner, x)
40
- else
41
- ldiv! (inner, x)
42
- ldiv! (outer, x)
43
- end
52
+ function LinearAlgebra. ldiv! (A:: ComposePreconditioner , x)
53
+ @unpack inner, outer, isleft = A
54
+
55
+ ldiv! (inner, x)
56
+ ldiv! (outer, x)
57
+ end
58
+
59
+ function LinearAlgebra. ldiv! (y, A:: ComposePreconditioner , x)
60
+ @unpack inner, outer = A
61
+
62
+ ldiv! (y, inner, x)
63
+ ldiv! (outer, y)
64
+ end
65
+
66
+ function LinearAlgebra. mul! (y, A:: ComposePreconditioner , x)
44
67
end
45
68
46
- function LinearAlgebra. ldiv! (y, P:: ComposePreconditioner , x)
47
- @unpack inner, outer, isleft = P
69
+ function LinearAlgebra. mul! (C, A:: ComposePreconditioner , B, α, β)
48
70
end
49
71
50
72
# # Krylov.jl
@@ -150,8 +172,8 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
150
172
cache = set_cacheval (cache, solver)
151
173
end
152
174
153
- # Pl = ComposePreconditioner(alg.Pl, cache.Pl, true)
154
- # Pr = ComposePreconditioner(alg.Pr, cache.Pr, false)
175
+ M = alg . Pl # ComposePreconditioner(alg.Pl, cache.Pl) # left precond
176
+ N = alg . Pr # ComposePreconditioner(alg.Pr, cache.Pr) # right
155
177
156
178
atol = cache. abstol
157
179
rtol = cache. reltol
@@ -163,20 +185,20 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
163
185
alg. kwargs... )
164
186
165
187
if cache. cacheval isa Krylov. CgSolver
166
- alg . Pr != LinearAlgebra. I &&
188
+ N != LinearAlgebra. I &&
167
189
@warn " $(alg. KrylovAlg) doesn't support right preconditioning."
168
- Krylov. solve! (args... ; M= alg . Pl ,
190
+ Krylov. solve! (args... ; M= M ,
169
191
kwargs... )
170
192
elseif cache. cacheval isa Krylov. GmresSolver
171
- Krylov. solve! (args... ; M= alg . Pl , N= alg . Pr ,
193
+ Krylov. solve! (args... ; M= M , N= N ,
172
194
kwargs... )
173
195
elseif cache. cacheval isa Krylov. BicgstabSolver
174
- Krylov. solve! (args... ; M= alg . Pl , N= alg . Pr ,
196
+ Krylov. solve! (args... ; M= M , N= N ,
175
197
kwargs... )
176
198
elseif cache. cacheval isa Krylov. MinresSolver
177
- alg . Pr != LinearAlgebra. I &&
199
+ N != LinearAlgebra. I &&
178
200
@warn " $(alg. KrylovAlg) doesn't support right preconditioning."
179
- Krylov. solve! (args... ; M= alg . Pl ,
201
+ Krylov. solve! (args... ; M= M ,
180
202
kwargs... )
181
203
else
182
204
Krylov. solve! (args... ; kwargs... )
@@ -228,8 +250,8 @@ function init_cacheval(alg::IterativeSolversJL, cache::LinearCache)
228
250
Pl = alg. Pl
229
251
Pr = alg. Pr
230
252
231
- # Pl = ComposePreconditioner(alg.Pl, cache.Pl, true )
232
- # Pr = ComposePreconditioner(alg.Pr, cache.Pr, false )
253
+ # Pl = ComposePreconditioner(alg.Pl, cache.Pl)
254
+ # Pr = ComposePreconditioner(alg.Pr, cache.Pr)
233
255
234
256
abstol = cache. abstol
235
257
reltol = cache. reltol
0 commit comments