|
2 | 2 | ## Preconditioners
|
3 | 3 |
|
4 | 4 | """
|
| 5 | + LEFT |
5 | 6 | P * x = x .* (1/P.s)
|
6 |
| -
|
7 | 7 | Pi * x = x .* (P.s)
|
| 8 | +
|
| 9 | + Right |
| 10 | + P * x = x .* (P.s) |
| 11 | + Pi * x = x .* (1/P.s) |
8 | 12 | """
|
9 | 13 | struct ScaleVector{T}
|
10 | 14 | s::T
|
11 | 15 | isleft::Bool
|
12 | 16 | end
|
13 | 17 |
|
| 18 | +Base.eltype(A::ScaleVector) = eltype(A.s) |
| 19 | + |
| 20 | +#function Base.*(A::ScaleVector, x) |
| 21 | +# y = similar(x) |
| 22 | +# mul!(y, A, x) |
| 23 | +#end |
| 24 | + |
14 | 25 | # y = A x
|
15 | 26 | function LinearAlgebra.mul!(y, A::ScaleVector, x)
|
16 | 27 | A.s == one(eltype(A.s)) && return y = x
|
17 | 28 |
|
18 |
| - if A.isleft |
19 |
| - @. x = x / A.s |
20 |
| - else |
21 |
| - @. x = x * A.s |
22 |
| - end |
| 29 | + s = A.isleft ? 1/A.s : A.s |
| 30 | + mul!(y, s, x) |
| 31 | + |
23 | 32 | end
|
24 | 33 |
|
25 | 34 | # A B α + C β
|
26 | 35 | function LinearAlgebra.mul!(C, A::ScaleVector, B, α, β)
|
27 |
| - |
28 |
| - tmp = zero(B) |
29 |
| - C = β * C + α * mul!(tmp, A, B) |
| 36 | + A.s == one(eltype(A.s)) && return @. C = α * B + β * C |
| 37 | + |
| 38 | + s = A.isleft ? 1/A.s : A.s |
| 39 | + mul!(C, s, B, α, β) |
30 | 40 | end
|
31 | 41 |
|
32 | 42 | function LinearAlgebra.ldiv!(A::ScaleVector, x)
|
33 | 43 | A.s == one(eltype(A.s)) && return x
|
34 | 44 |
|
35 |
| - if A.isleft |
36 |
| - @. x = x * A.s |
37 |
| - else |
38 |
| - @. x = x / A.s |
39 |
| - end |
| 45 | + s = A.isleft ? A.s : 1/A.s |
| 46 | + @. x = x * s |
40 | 47 | end
|
41 | 48 |
|
42 | 49 | function LinearAlgebra.ldiv!(y, A::ScaleVector, x)
|
43 |
| - P.s == one(eltype(A.s)) && return y = x |
| 50 | + A.s == one(eltype(A.s)) && return y = x |
44 | 51 |
|
45 |
| - if A.isleft |
46 |
| - @. y = x * A.s |
47 |
| - else |
48 |
| - @. y = x / A.s |
49 |
| - end |
| 52 | + s = A.isleft ? A.s : 1/A.s |
| 53 | + mul!(y, s, x) |
50 | 54 | end
|
51 | 55 |
|
52 |
| -Base.eltype(A::ScaleVector) = eltype(A.s) |
53 |
| - |
54 | 56 | """
|
55 | 57 | C * x = P * Q * x
|
56 |
| -
|
57 | 58 | Ci * x = Qi * Pi * x
|
58 | 59 | """
|
59 | 60 | struct ComposePreconditioner{Ti,To}
|
60 | 61 | inner::Ti
|
61 | 62 | outer::To
|
62 | 63 | end
|
63 | 64 |
|
| 65 | +Base.eltype(A::ComposePreconditioner) = Float64 #eltype(A.inner) |
| 66 | + |
64 | 67 | # y = A x
|
65 | 68 | function LinearAlgebra.mul!(y, A::ComposePreconditioner, x)
|
66 | 69 | @unpack inner, outer = A
|
67 |
| - mul!(y, inner, x) |
68 |
| - y = outer * y |
| 70 | + tmp = similar(y) |
| 71 | + mul!(tmp, outer, x) |
| 72 | + mul!(y, inner, tmp) |
69 | 73 | end
|
70 | 74 |
|
71 | 75 | # A B α + C β
|
72 | 76 | function LinearAlgebra.mul!(C, A::ComposePreconditioner, B, α, β)
|
73 | 77 | @unpack inner, outer = A
|
| 78 | + tmp = similar(B) |
| 79 | + mul!(tmp, inner, B) |
| 80 | + mul!(C, outer, tmp, α, β) |
74 | 81 | end
|
75 | 82 |
|
76 | 83 | function LinearAlgebra.ldiv!(A::ComposePreconditioner, x)
|
@@ -190,8 +197,11 @@ function SciMLBase.solve(cache::LinearCache, alg::KrylovJL; kwargs...)
|
190 | 197 | cache = set_cacheval(cache, solver)
|
191 | 198 | end
|
192 | 199 |
|
193 |
| - M = alg.Pl #ComposePreconditioner(alg.Pl, cache.Pl) # left precond |
194 |
| - N = alg.Pr #ComposePreconditioner(alg.Pr, cache.Pr) # right |
| 200 | + M = alg.Pl |
| 201 | + N = alg.Pr |
| 202 | + |
| 203 | +# M = ComposePreconditioner(alg.Pl, cache.Pl) # left precond |
| 204 | +# N = ComposePreconditioner(alg.Pr, cache.Pr) # right |
195 | 205 |
|
196 | 206 | atol = cache.abstol
|
197 | 207 | rtol = cache.reltol
|
@@ -265,11 +275,8 @@ IterativeSolversJL_MINRES(args...;kwargs...) =
|
265 | 275 | function init_cacheval(alg::IterativeSolversJL, cache::LinearCache)
|
266 | 276 | @unpack A, b, u = cache
|
267 | 277 |
|
268 |
| - Pl = alg.Pl |
269 |
| - Pr = alg.Pr |
270 |
| - |
271 |
| -# Pl = ComposePreconditioner(alg.Pl, cache.Pl) |
272 |
| -# Pr = ComposePreconditioner(alg.Pr, cache.Pr) |
| 278 | + Pl = ComposePreconditioner(alg.Pl, cache.Pl) |
| 279 | + Pr = ComposePreconditioner(alg.Pr, cache.Pr) |
273 | 280 |
|
274 | 281 | abstol = cache.abstol
|
275 | 282 | reltol = cache.reltol
|
|
0 commit comments