Skip to content

Commit 0bed167

Browse files
committed
Get level construction working
1 parent 5a0c548 commit 0bed167

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

src/AMG.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export split_nodes, RS
99
include("gallery.jl")
1010
export poisson
1111

12+
include("smoother.jl")
13+
14+
include("multilevel.jl")
15+
1216
include("classical.jl")
1317

1418
end # module

src/classical.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,38 @@ struct Solver{S,T,P,PS}
77
max_coarse::Int64
88
end
99

10-
struct Level{T}
11-
A::T
12-
end
13-
1410
function ruge_stuben(A::SparseMatrixCSC;
15-
strength = Classical(),
11+
strength = Classical(0.25),
1612
CF = RS(),
1713
presmoother = GaussSiedel(),
1814
postsmoother = GaussSiedel(),
1915
max_levels = 10,
2016
max_coarse = 500)
2117

22-
s = Solver(strength, CF, presmoother,
23-
postsmoother, max_levels, max_levels)
18+
s = Solver(strength, CF, presmoother,
19+
postsmoother, max_levels, max_levels)
2420

25-
levels = [Level(A)]
21+
levels = Vector{Level}()
2622

27-
while length(levels) < max_levels && size(levels[end].A, 1)
28-
extend_heirarchy!(levels, strength, CF, A)
23+
while length(levels) < max_levels
24+
A = extend_heirarchy!(levels, strength, CF, A)
25+
if size(levels[end].A, 1) < max_coarse
26+
break
2927
end
28+
end
29+
levels
3030
end
3131

3232
function extend_heirarchy!(levels::Vector{Level}, strength, CF, A)
3333
S = strength_of_connection(strength, A)
3434
splitting = split_nodes(CF, S)
3535
P, R = direct_interpolation(A, S, splitting)
36+
push!(levels, Level(A, P, R))
37+
A = R * A * P
3638
end
3739

3840
function direct_interpolation{T,V}(A::T, S::T, splitting::Vector{V})
39-
41+
4042
fill!(S.nzval, 1.)
4143
S = A .* S
4244
Pp = rs_direct_interpolation_pass1(S, A, splitting)
@@ -102,6 +104,7 @@ function rs_direct_interpolation_pass1(S, A, splitting)
102104
sum_strong_pos = 0
103105
sum_strong_neg = 0
104106
for jj = Sp[i]: Sp[i+1]-1
107+
jj > length(Sp) && continue
105108
if splitting[Sj[jj]] == C_NODE && Sj[jj] != i
106109
if Sx[jj] < 0
107110
sum_strong_neg += Sx[jj]
@@ -115,6 +118,7 @@ function rs_direct_interpolation_pass1(S, A, splitting)
115118
sum_all_neg = 0
116119
diag = 0;
117120
for jj = Ap[i]:Ap[i+1]
121+
jj > length(Ap) && continue
118122
if Aj[jj] == i
119123
diag += Ax[jj]
120124
else
@@ -139,6 +143,7 @@ function rs_direct_interpolation_pass1(S, A, splitting)
139143

140144
nnz = Bp[i]
141145
for jj = Sp[i]:Sp[i+1]
146+
jj > length(Sj) && continue
142147
if splitting[Sj[jj]] == C_NODE && Sj[jj] != i
143148
Bj[nnz] = Sj[jj]
144149
if Sx[jj] < 0
@@ -159,6 +164,7 @@ function rs_direct_interpolation_pass1(S, A, splitting)
159164
sum += splitting[i]
160165
end
161166
for i = 1:Bp[n_nodes]
167+
Bj[i] == 0 && continue
162168
Bj[i] = m[Bj[i]]
163169
end
164170

src/multilevel.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
struct Level{Ti,Tv}
22
A::SparseMatrixCSC{Ti,Tv}
3+
P::SparseMatrixCSC{Ti,Tv}
4+
R::SparseMatrixCSC{Ti,Tv}
35
end

src/smoother.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abstract type Smoother end
2+
struct GaussSiedel <: Smoother
3+
end

src/strength.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ abstract type Strength end
22
struct Classical{T} <: Strength
33
θ::T
44
end
5+
Classical(;θ = 0.25) = Classical(θ)
56

67
function strength_of_connection{T}(c::Classical{T}, A::SparseMatrixCSC)
78

@@ -14,8 +15,8 @@ function strength_of_connection{T}(c::Classical{T}, A::SparseMatrixCSC)
1415

1516
for i = 1:n
1617
neighbors = A[:,i]
17-
m = find_max_off_diag(neighbors, i)
18-
threshold = θ * m
18+
_m = find_max_off_diag(neighbors, i)
19+
threshold = θ * _m
1920
for j in nzrange(A, i)
2021
row = A.rowval[j]
2122
val = A.nzval[j]
@@ -26,7 +27,7 @@ function strength_of_connection{T}(c::Classical{T}, A::SparseMatrixCSC)
2627
end
2728
end
2829
end
29-
S = sparse(I, J, V)
30+
S = sparse(I, J, V, m, n)
3031

3132
scale_cols_by_largest_entry(S)
3233
end
@@ -51,16 +52,16 @@ function scale_cols_by_largest_entry(A::SparseMatrixCSC)
5152

5253
k = 1
5354
for i = 1:n
54-
m = maximum(A[:,i])
55+
_m = maximum(A[:,i])
5556
for j in nzrange(A, i)
5657
row = A.rowval[j]
5758
val = A.nzval[j]
5859
I[k] = row
5960
J[k] = i
60-
V[k] = val / m
61+
V[k] = val / _m
6162
k += 1
6263
end
6364
end
6465

65-
sparse(I,J,V)
66+
sparse(I,J,V,m,n)
6667
end

0 commit comments

Comments
 (0)