/
idrs.jl
219 lines (172 loc) · 5.59 KB
/
idrs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
export idrs, idrs!
using Random
"""
idrs(A, b; s = 8, kwargs...) -> x, [history]
Same as [`idrs!`](@ref), but allocates a solution vector `x` initialized with zeros.
"""
idrs(A, b; kwargs...) = idrs!(zerox(A,b), A, b; kwargs...)
"""
idrs!(x, A, b; s = 8, kwargs...) -> x, [history]
Solve the problem ``Ax = b`` approximately with IDR(s), where `s` is the dimension of the
shadow space.
# Arguments
- `x`: Initial guess, will be updated in-place;
- `A`: linear operator;
- `b`: right-hand side.
## Keywords
- `s::Integer = 8`: dimension of the shadow space;
- `Pl::precT`: left preconditioner,
- `abstol::Real = zero(real(eltype(b)))`,
`reltol::Real = sqrt(eps(real(eltype(b))))`: absolute and relative
tolerance for the stopping condition
`|r_k| ≤ max(reltol * |r_0|, abstol)`, where `r_k = A * x_k - b`
is the residual in the `k`th iteration;
- `maxiter::Int = size(A, 2)`: maximum number of iterations;
- `log::Bool`: keep track of the residual norm in each iteration;
- `verbose::Bool`: print convergence information during the iterations.
# Return values
**if `log` is `false`**
- `x`: approximate solution.
**if `log` is `true`**
- `x`: approximate solution;
- `history`: convergence history.
"""
function idrs!(x, A, b;
s = 8,
Pl = Identity(),
abstol::Real = zero(real(eltype(b))),
reltol::Real = sqrt(eps(real(eltype(b)))),
maxiter=size(A, 2),
log::Bool=false,
kwargs...)
history = ConvergenceHistory(partial=!log)
history[:abstol] = abstol
history[:reltol] = reltol
log && reserve!(history, :resnorm, maxiter)
idrs_method!(history, x, A, b, s, Pl, abstol, reltol, maxiter; kwargs...)
log && shrink!(history)
log ? (x, history) : x
end
#########################
# Method Implementation #
#########################
@inline function omega(t, s)
angle = sqrt(2.)/2
ns = norm(s)
nt = norm(t)
ts = dot(t,s)
rho = abs(ts/(nt*ns))
om = ts/(nt*nt)
if rho < angle
om = om*convert(typeof(om),angle)/rho
end
om
end
function idrs_method!(log::ConvergenceHistory, X, A, C::T,
s::Number, Pl::precT, abstol::Real, reltol::Real, maxiter::Number; smoothing::Bool=false, verbose::Bool=false
) where {T, precT}
verbose && @printf("=== idrs ===\n%4s\t%7s\n","iter","resnorm")
R = C - A*X
normR = norm(R)
iter = 1
tol = max(reltol * normR, abstol)
if smoothing
X_s = copy(X)
R_s = copy(R)
T_s = zero(R)
end
if normR <= tol # Initial guess is a good enough solution
setconv(log, 0<=normR<tol)
return X
end
Z = zero(C)
P = T[rand!(copy(C)) for k in 1:s]
U = T[copy(Z) for k in 1:s]
G = T[copy(Z) for k in 1:s]
Q = copy(Z)
V = copy(Z)
M = Matrix{eltype(C)}(I,s,s)
f = zeros(eltype(C),s)
c = zeros(eltype(C),s)
om::eltype(C) = 1
while normR > tol && iter ≤ maxiter
for i in 1:s
f[i] = dot(P[i], R)
end
for k in 1:s
nextiter!(log,mvps=1)
# Solve small system and make v orthogonal to P
c = LowerTriangular(M[k:s,k:s])\f[k:s]
V .= c[1] .* G[k]
Q .= c[1] .* U[k]
for i = k+1:s
V .+= c[i-k+1] .* G[i]
Q .+= c[i-k+1] .* U[i]
end
# Compute new U[:,k] and G[:,k], G[:,k] is in space G_j
V .= R .- V
# Preconditioning
ldiv!(Pl, V)
U[k] .= Q .+ om .* V
mul!(G[k], A, U[k])
# Bi-orthogonalise the new basis vectors
for i in 1:k-1
alpha = dot(P[i],G[k])/M[i,i]
G[k] .-= alpha .* G[i]
U[k] .-= alpha .* U[i]
end
# New column of M = P'*G (first k-1 entries are zero)
for i in k:s
M[i,k] = dot(P[i],G[k])
end
# Make r orthogonal to q_i, i = 1..k
beta = f[k]/M[k,k]
R .-= beta .* G[k]
X .+= beta .* U[k]
normR = norm(R)
if smoothing
T_s .= R_s .- R
gamma = dot(R_s, T_s)/dot(T_s, T_s)
R_s .-= gamma .* T_s
X_s .-= gamma .* (X_s .- X)
normR = norm(R_s)
end
push!(log, :resnorm, normR)
verbose && @printf("%3d\t%1.2e\n",iter,normR)
if normR < tol || iter == maxiter
setconv(log, 0<=normR<tol)
return X
end
if k < s
f[k+1:s] .-= beta*M[k+1:s,k]
end
iter += 1
end
# Now we have sufficient vectors in G_j to compute residual in G_j+1
# Note: r is already perpendicular to P so v = r
copyto!(V, R)
# Preconditioning
ldiv!(Pl, V)
mul!(Q, A, V)
om = omega(Q, R)
R .-= om .* Q
X .+= om .* V
normR = norm(R)
if smoothing
T_s .= R_s .- R
gamma = dot(R_s, T_s)/dot(T_s, T_s)
R_s .-= gamma .* T_s
X_s .-= gamma .* (X_s .- X)
normR = norm(R_s)
end
iter += 1
nextiter!(log, mvps=1)
push!(log, :resnorm, normR)
end
if smoothing
copyto!(X, X_s)
end
verbose && @printf("\n")
setconv(log, 0<=normR<tol)
X
end