Skip to content

Commit 37d9576

Browse files
committed
save top residual calculation when preconditioning
1 parent b70d841 commit 37d9576

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/multilevel.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ function solve!(x, ml::MultiLevel, b::AbstractVector{T},
144144
maxiter::Int = 100,
145145
tol::Float64 = 1e-5,
146146
verbose::Bool = false,
147-
log::Bool = false) where {T}
148-
147+
log::Bool = false,
148+
calculate_residual = true) where {T}
149+
149150
A = length(ml) == 1 ? ml.final_A : ml.levels[1].A
150151
V = promote_type(eltype(A), eltype(b))
151152
tol = eltype(b)(tol)
@@ -156,15 +157,20 @@ function solve!(x, ml::MultiLevel, b::AbstractVector{T},
156157
end
157158
log && push!(residuals, normb)
158159

160+
res = ml.workspace.res_vecs[1]
159161
itr = lvl = 1
160-
while itr <= maxiter && normres > tol
162+
while itr <= maxiter && (!calculate_residual || normres > tol)
161163
if length(ml) == 1
162164
ml.coarse_solver(x, b)
163165
else
164166
__solve!(x, ml, cycle, b, lvl)
165167
end
166-
normres = norm(b - A * x)
167-
log && push!(residuals, normres)
168+
if calculate_residual
169+
mul!(res, A, x)
170+
res .= b .- res
171+
normres = norm(res)
172+
log && push!(residuals, normres)
173+
end
168174
itr += 1
169175
end
170176

@@ -176,7 +182,10 @@ function __solve!(x, ml, v::V, b, lvl)
176182
A = ml.levels[lvl].A
177183
ml.presmoother(A, x, b)
178184

179-
res = b - A * x
185+
res = ml.workspace.res_vecs[lvl]
186+
mul!(res, A, x)
187+
res .= b .- res
188+
180189
coarse_b = ml.levels[lvl].R * res
181190
coarse_x = zeros(eltype(coarse_b), size(coarse_b))
182191

src/preconditioner.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ else
1515
mul!(b, p::Preconditioner, x) = mul!(b, p.ml.levels[1].A, x)
1616
end
1717

18-
\(p::Preconditioner, b) = solve(p.ml, b, maxiter = 1, tol = 1e-12)
18+
\(p::Preconditioner, b) = solve(p.ml, b, maxiter = 1, calculate_residual = false)
1919
*(p::Preconditioner, b) = p.ml.levels[1].A * x

0 commit comments

Comments
 (0)