-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from Alexander-Barth/master
implement W and F cycles and n-dimensional Poisson matrix
- Loading branch information
Showing
6 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,63 @@ | ||
poisson(T, n) = sparse(Tridiagonal(fill(T(-1), n-1), | ||
poisson(T, n) = sparse(Tridiagonal(fill(T(-1), n-1), | ||
fill(T(2), n), fill(T(-1), n-1))) | ||
poisson(n) = poisson(Float64, n) | ||
|
||
function stencil_grid(T,stencil,sz) | ||
# upper-bound for storage | ||
n = prod(sz) * sum(.!iszero,stencil) | ||
|
||
# indices and value of nonzero elements | ||
Si = zeros(Int,n) | ||
Sj = zeros(Int,n) | ||
Ss = zeros(T,n) | ||
|
||
linindices = LinearIndices(sz) | ||
nnz = 0 | ||
|
||
stencil_sz = size(stencil) | ||
offset = CartesianIndex((stencil_sz .+ 1) .÷ 2) | ||
|
||
for i in CartesianIndices(sz) | ||
for k in CartesianIndices(stencil_sz) | ||
if stencil[k] != 0 | ||
j = i + k - offset | ||
if checkbounds(Bool,linindices,j) | ||
nnz = nnz + 1 | ||
Si[nnz] = linindices[i] | ||
Sj[nnz] = linindices[j] | ||
Ss[nnz] = stencil[k] | ||
end | ||
end | ||
end | ||
end | ||
|
||
sparse((@view Si[1:nnz]), | ||
(@view Sj[1:nnz]), | ||
(@view Ss[1:nnz]), | ||
prod(sz),prod(sz)) | ||
end | ||
|
||
|
||
|
||
function poisson(T,sz::NTuple{N,Int}) where N | ||
#= | ||
In 2D stencil has the value: | ||
stencil = [0 -1 0; | ||
-1 4 -1; | ||
0 -1 0] | ||
=# | ||
stencil = zeros(T,ntuple(i -> 3,Val(N))) | ||
for i = 0:N-1 | ||
Ipre = CartesianIndex(ntuple(l -> 2,i)) | ||
Ipost = CartesianIndex(ntuple(l -> 2,N-i-1)) | ||
stencil[Ipre, 1, Ipost] = -1 | ||
stencil[Ipre, 3, Ipost] = -1 | ||
end | ||
Icenter = CartesianIndex(ntuple(l -> 2,Val(N))) | ||
stencil[Icenter] = 2*N | ||
|
||
stencil_grid(T,stencil,sz) | ||
end | ||
|
||
poisson(sz::NTuple{N,Int}) where N = poisson(Float64,sz) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import AlgebraicMultigrid: ruge_stuben, smoothed_aggregation, | ||
poisson, aspreconditioner | ||
|
||
import IterativeSolvers: cg | ||
|
||
function test_cycles() | ||
|
||
A = poisson((50,50)) | ||
b = A * ones(size(A,1)) | ||
|
||
reltol = 1e-8 | ||
|
||
for method in [ruge_stuben, smoothed_aggregation] | ||
ml = method(A) | ||
|
||
for cycle in [AlgebraicMultigrid.V(),AlgebraicMultigrid.W(),AlgebraicMultigrid.F()] | ||
x,convhist = solve(ml, b, cycle; reltol = reltol, log = true) | ||
|
||
@debug "number of iterations for $cycle using $method: $(length(convhist))" | ||
@test norm(b - A*x) < reltol * norm(b) | ||
end | ||
|
||
for cycle in [AlgebraicMultigrid.V(),AlgebraicMultigrid.W(),AlgebraicMultigrid.F()] | ||
p = aspreconditioner(ml,cycle) | ||
|
||
x,convhist = cg(A, b, Pl = p; reltol = reltol, log = true) | ||
@debug "CG: number of iterations for $cycle using $method: $(convhist.iters)" | ||
@test norm(b - A*x) <= reltol * norm(b) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters