Skip to content

Commit 940862d

Browse files
committed
Add a few fixes to fit candidates routine and tests
1 parent 0d3e2e0 commit 940862d

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

src/aggregation.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ function extend_hierarchy!(levels, strength, aggregate, smooth,
5757
S = strength_of_connection(strength, A, bsr_flag)
5858

5959
# Aggregation operator
60-
AggOp = aggregation(aggregate, S')
61-
60+
AggOp = aggregation(aggregate, S)
6261
# b = zeros(eltype(A), size(A, 1))
6362

6463
# Improve candidates
6564
# relax!(improve_candidates, A, B, b)
6665
T, B = fit_candidates(AggOp, B)
6766

68-
P = smooth_prolongator(smooth, A, T, S, B)
67+
P = smooth_prolongator(smooth, A, T, S', B)
6968
R = construct_R(symmetry, P)
7069
push!(levels, Level(A, P, R))
7170

@@ -82,16 +81,20 @@ construct_R(::HermitianSymmetry, P) = P'
8281
function fit_candidates(AggOp, B, tol = 1e-10)
8382

8483
A = AggOp.'
85-
n_coarse = size(A, 2)
86-
n_fine = size(A, 1)
84+
n_fine, n_coarse = size(A)
8785
n_col = n_coarse
8886

8987
R = zeros(eltype(B), n_coarse)
9088
Qx = zeros(eltype(B), nnz(A))
9189

9290
copy!(Qx, B)
93-
copy!(A.nzval, B)
94-
91+
# copy!(A.nzval, B)
92+
for i = 1:n_col
93+
for j in nzrange(A,i)
94+
row = A.rowval[j]
95+
A.nzval[j] = B[row]
96+
end
97+
end
9598
k = 1
9699
for i = 1:n_col
97100
norm_i = norm_col(A, Qx, i)
@@ -107,19 +110,20 @@ function fit_candidates(AggOp, B, tol = 1e-10)
107110
row = A.rowval[j]
108111
# Qx[row] *= scale
109112
#@show k
110-
Qx[k] *= scale
111-
k += 1
113+
# Qx[k] *= scale
114+
# k += 1
115+
A.nzval[j] *= scale
112116
end
113117
end
114118

115-
SparseMatrixCSC(size(A)..., A.colptr, A.rowval, Qx), R
116-
# A, R
119+
# SparseMatrixCSC(size(A)..., A.colptr, A.rowval, Qx), R
120+
A, R
117121
end
118122
function norm_col(A, Qx, i)
119123
s = zero(eltype(A))
120124
for j in nzrange(A, i)
121-
# val = Qx[A.rowval[j]]
122-
val = A.nzval[A.rowval[j]]
125+
val = Qx[A.rowval[j]]
126+
# val = A.nzval[A.rowval[j]]
123127
s += val*val
124128
end
125129
sqrt(s)

src/smoother.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function weight(::DiagonalWeighting, S, ω)
111111
D_inv = 1 ./ diag(S)
112112
D_inv_S = scale_rows(S, D_inv)
113113
(eltype(S)(ω) / approximate_spectral_radius(D_inv_S)) * D_inv_S
114-
#(ω) * D_inv_S
114+
# (ω) * D_inv_S
115115
end
116116

117117
#approximate_spectral_radius(A) =
@@ -127,4 +127,4 @@ function scale_rows!(ret, S, v)
127127
end
128128
ret
129129
end
130-
scale_rows(S, v) = scale_rows!(deepcopy(S), S, v)
130+
scale_rows(S, v) = scale_rows!(deepcopy(S), S, v)

src/utils.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function approximate_spectral_radius(A, tol = 0.01,
55
symmetric = false
66

77
# Initial guess
8-
v0 = rand(eltype(A), size(A,1))
8+
v0 = rand(eltype(A), size(A,2))
99
maxiter = min(size(A, 1), maxiter)
1010
ev = zeros(eltype(A), maxiter)
1111
max_index = 0
@@ -21,7 +21,7 @@ function approximate_spectral_radius(A, tol = 0.01,
2121
# m, max_index = findmax(abs.(ev))
2222
m, max_index = findmaxabs(ev)
2323
error = H[nvecs, nvecs-1] * evect[end, max_index]
24-
if abs(error) / abs(ev[max_index]) < tol || flag
24+
if (abs(error) / abs(ev[max_index]) < tol) || flag
2525
# v0 = X * evect[:, max_index]
2626
A_mul_B!(v0, X, evect[:, max_index])
2727
break
@@ -63,6 +63,7 @@ function approximate_eigenvalues(A, tol, maxiter, symmetric, v0)
6363
# V = Vector{Vector{eltype(A)}}(maxiter + 1)
6464
# V = zeros(size(A,1), maxiter)
6565
# V[1] = v0
66+
breakdown = find_breakdown(eltype(A))
6667
flag = false
6768

6869
for j = 1:maxiter
@@ -73,11 +74,11 @@ function approximate_eigenvalues(A, tol, maxiter, symmetric, v0)
7374
for (i,v) in enumerate(V)
7475
# for i = 1:j
7576
v = V[i]
76-
H[i,j] = dot(v, w)
77+
H[i,j] = dot(conj.(v), w)
7778
BLAS.axpy!(-H[i,j], v, w)
7879
end
7980
H[j+1,j] = norm(w)
80-
if H[j+1, j] < eps()
81+
if H[j+1, j] < breakdown
8182
flag = true
8283
if H[j+1,j] != 0
8384
scale!(w, 1/H[j+1,j])
@@ -94,3 +95,6 @@ function approximate_eigenvalues(A, tol, maxiter, symmetric, v0)
9495

9596
Vects, Eigs, H, V, flag
9697
end
98+
99+
find_breakdown(::Type{Float64}) = eps(Float64) * 10^6
100+
find_breakdown(::Type{Float32}) = eps(Float64) * 10^3

test/sa_tests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ function generate_fit_candidates_cases()
176176
B = ones(T, 9)
177177
push!(cases, (AggOp, B))
178178

179-
#AggOp = SparseMatrixCSC(3, 9, collect(1:10),
180-
#[3,2,1,1,2,3,2,1,3], ones(T,9))
181-
#B = T.(collect(1:9))
182-
#push!(cases, (AggOp, B))
179+
AggOp = SparseMatrixCSC(3, 9, collect(1:10),
180+
[3,2,1,1,2,3,2,1,3], ones(T,9))
181+
B = T.(collect(1:9))
182+
push!(cases, (AggOp, B))
183183
end
184184

185185
cases

0 commit comments

Comments
 (0)