Skip to content

Commit 0c81482

Browse files
committed
Add sa test file
1 parent cc11e2b commit 0c81482

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/sa_tests.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import AMG: scale_cols_by_largest_entry!, strength_of_connection,
2+
SymmetricStrength, poisson
3+
function symmetric_soc(A::SparseMatrixCSC{T,V}, θ) where {T,V}
4+
D = abs.(diag(A))
5+
i,j,v = findnz(A)
6+
mask = i .!= j
7+
DD = D[i] .* D[j]
8+
mask = mask .& (abs.(v.^2) .>=* θ * DD))
9+
10+
i = i[mask]
11+
j = j[mask]
12+
v = v[mask]
13+
14+
S = sparse(i,j,v, size(A)...) + spdiagm(D)
15+
16+
scale_cols_by_largest_entry!(S)
17+
18+
for i = 1:size(S.nzval,1)
19+
S.nzval[i] = abs(S.nzval[i])
20+
end
21+
22+
S
23+
end
24+
25+
# Set up tests
26+
function test_symmetric_soc()
27+
28+
cases = generate_matrices()
29+
30+
for matrix in cases
31+
for θ in (0.0, 0.1, 0.5, 1., 10.)
32+
ref_matrix = symmetric_soc(matrix, θ)
33+
calc_matrix = strength_of_connection(SymmetricStrength(θ), matrix)
34+
35+
@test sum(abs2, ref_matrix - calc_matrix) < 1e-6
36+
end
37+
end
38+
end
39+
40+
function generate_matrices()
41+
42+
cases = []
43+
44+
# Random matrices
45+
srand(0)
46+
for T in (Float32, Float64)
47+
48+
for s in [2, 3, 5]
49+
push!(cases, sprand(T, s, s, 1.))
50+
end
51+
52+
for s in [2, 3, 5, 7, 10, 11, 19]
53+
push!(cases, poisson(T, s))
54+
end
55+
end
56+
57+
cases
58+
end

0 commit comments

Comments
 (0)