Skip to content

Commit 422a767

Browse files
committed
Add option to use as preconditioner
1 parent a3aa1eb commit 422a767

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/AMG.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ export solve
1919
include("classical.jl")
2020
export ruge_stuben
2121

22+
include("preconditioner.jl")
23+
export aspreconditioner
24+
2225
end # module

src/multilevel.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ function solve{T}(ml::MultiLevel, b::Vector{T}; maxiter = 100,
6565
residuals = Vector{T}()
6666
A = ml.levels[1].A
6767
normb = norm(b)
68+
if normb != 0
69+
tol *= normb
70+
end
6871
push!(residuals, norm(b - A*x))
6972

7073
lvl = 1
@@ -74,7 +77,7 @@ function solve{T}(ml::MultiLevel, b::Vector{T}; maxiter = 100,
7477
else
7578
x = __solve(cycle, ml, x, b, lvl)
7679
end
77-
push!(residuals, norm(A*x - b))
80+
push!(residuals, norm(b - A * x))
7881
end
7982
x
8083
end

src/preconditioner.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Base: \, *, A_ldiv_B!, A_mul_B!
2+
3+
struct Preconditioner
4+
ml::MultiLevel
5+
end
6+
7+
aspreconditioner(ml::MultiLevel) = Preconditioner(ml)
8+
9+
\(p::Preconditioner, b) = p * b
10+
*(p::Preconditioner, b) = solve(p.ml, b; cycle = V(), maxiter = 1, tol = 1e-12)
11+
12+
A_ldiv_B!(x, p::Preconditioner, b) = copy!(x, p \ b)
13+
A_mul_B!(b, p::Preconditioner, x) = A_mul_B!(b, p.ml.levels[1].A, x)

0 commit comments

Comments
 (0)