Skip to content

Commit b31832f

Browse files
committed
Add a number of gauss seidel smoothing tests
1 parent 940862d commit b31832f

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

src/aggregation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function extend_hierarchy!(levels, strength, aggregate, smooth,
6464
# relax!(improve_candidates, A, B, b)
6565
T, B = fit_candidates(AggOp, B)
6666

67-
P = smooth_prolongator(smooth, A, T, S', B)
67+
P = smooth_prolongator(smooth, A, T, S, B)
6868
R = construct_R(symmetry, P)
6969
push!(levels, Level(A, P, R))
7070

src/multilevel.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function solve{T}(ml::MultiLevel, b::Vector{T},
8888
push!(residuals, T(norm(b - A * x)))
8989
end
9090

91+
@show residuals
9192
if log
9293
return x, residuals
9394
else

src/smoother.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ presmoother!(s, A, x, b) = smoother!(s, s.sweep, A, x, b)
1818
postsmoother!(s, A, x, b) = smoother!(s, s.sweep, A, x, b)
1919
relax!(s, A, x, b) = smoother!(s, s.sweep, A, x, b)
2020

21-
smoother!(s::GaussSeidel, ::ForwardSweep, A, x, b) =
22-
gs!(A, b, x, 1, 1, size(A, 1))
21+
function smoother!(s::GaussSeidel, ::ForwardSweep, A, x, b)
22+
for i in 1:s.iter
23+
gs!(A, b, x, 1, 1, size(A, 1))
24+
end
25+
end
2326

2427
function smoother!(s::GaussSeidel, ::SymmetricSweep, A, x, b)
2528
for i in 1:s.iter
@@ -28,8 +31,11 @@ function smoother!(s::GaussSeidel, ::SymmetricSweep, A, x, b)
2831
end
2932
end
3033

31-
smoother!(s::GaussSeidel, ::BackwardSweep, A, x, b) =
32-
gs!(A, b, x, size(A,1), -1, 1)
34+
function smoother!(s::GaussSeidel, ::BackwardSweep, A, x, b)
35+
for i in 1:s.iter
36+
gs!(A, b, x, size(A,1), -1, 1)
37+
end
38+
end
3339

3440

3541
function gs!(A, b, x, start, step, stop)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,7 @@ end
258258
end
259259
end
260260

261+
@testset "Gauss Seidel" begin
262+
test_gauss_seidel()
263+
end
261264
end

test/sa_tests.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,79 @@ function test_approximate_spectral_radius()
227227

228228
end
229229

230+
end
231+
232+
# Test Gauss Seidel
233+
import AMG: gs!, relax!
234+
function test_gauss_seidel()
235+
236+
N = 1
237+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
238+
(0, -1, 1), N, N)
239+
x = eltype(A).(collect(0:N-1))
240+
b = zeros(N)
241+
s = GaussSeidel(ForwardSweep())
242+
relax!(s, A, x, b)
243+
@test sum(abs2, x - zeros(N)) < 1e-8
244+
245+
N = 3
246+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
247+
(0, -1, 1), N, N)
248+
x = eltype(A).(collect(0:N-1))
249+
b = zeros(N)
250+
s = GaussSeidel(ForwardSweep())
251+
relax!(s, A, x, b)
252+
@test sum(abs2, x - [1.0/2.0, 5.0/4.0, 5.0/8.0]) < 1e-8
253+
254+
N = 1
255+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
256+
(0, -1, 1), N, N)
257+
x = eltype(A).(collect(0:N-1))
258+
b = zeros(N)
259+
s = GaussSeidel(BackwardSweep())
260+
relax!(s, A, x, b)
261+
@test sum(abs2, x - zeros(N)) < 1e-8
262+
263+
N = 3
264+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
265+
(0, -1, 1), N, N)
266+
x = eltype(A).(collect(0:N-1))
267+
b = zeros(N)
268+
s = GaussSeidel(BackwardSweep())
269+
relax!(s, A, x, b)
270+
@test sum(abs2, x - [1.0/8.0, 1.0/4.0, 1.0/2.0]) < 1e-8
271+
272+
N = 1
273+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
274+
(0, -1, 1), N, N)
275+
x = eltype(A).(collect(0:N-1))
276+
b = eltype(A).([10.])
277+
s = GaussSeidel(ForwardSweep())
278+
relax!(s, A, x, b)
279+
@test sum(abs2, x - [5.]) < 1e-8
280+
281+
N = 3
282+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
283+
(0, -1, 1), N, N)
284+
x = eltype(A).(collect(0:N-1))
285+
b = eltype(A).([10., 20., 30.])
286+
s = GaussSeidel(ForwardSweep())
287+
relax!(s, A, x, b)
288+
@test sum(abs2, x - [11.0/2.0, 55.0/4, 175.0/8.0]) < 1e-8
289+
290+
N = 100
291+
A = spdiagm((2 * ones(N), -ones(N-1), -ones(N-1)),
292+
(0, -1, 1), N, N)
293+
x = ones(eltype(A), N)
294+
b = zeros(eltype(A), N)
295+
s1 = GaussSeidel(ForwardSweep(), 200)
296+
relax!(s1, A, x, b)
297+
resid1 = norm(A*x,2)
298+
x = ones(eltype(A), N)
299+
s2 = GaussSeidel(BackwardSweep(), 200)
300+
relax!(s2, A, x, b)
301+
resid2 = norm(A*x,2)
302+
@test resid1 < 0.01 && resid2 < 0.01
303+
@test isapprox(resid1, resid2)
304+
230305
end

0 commit comments

Comments
 (0)