Skip to content

Commit 7692907

Browse files
committed
Refactor tests
1 parent c65be99 commit 7692907

2 files changed

Lines changed: 87 additions & 79 deletions

File tree

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ function run_all_tests()
8989
run_dgblf_tests()
9090
run_nonlinear_operator_tests()
9191
run_itemintegrator_tests()
92-
return run_dt_tests()
92+
run_dt_tests()
93+
run_test_helper_functions()
94+
95+
return nothing
9396
end
9497

9598
run_all_tests()

test/test_helper_functions.jl

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,107 @@
1-
# check if coords are opposite to each other
2-
function test_coords(X, Y, xgrid, is_opposite)
3-
coords = xgrid[Coordinates]
4-
for (x, y) in zip(X, Y)
5-
if @views !is_opposite(coords[:, x], coords[:, y])
6-
@show coords[:, x] coords[:, y]
7-
return false
1+
function run_test_helper_functions()
2+
3+
# check if coords are opposite to each other
4+
function test_coords(X, Y, xgrid, is_opposite)
5+
coords = xgrid[Coordinates]
6+
for (x, y) in zip(X, Y)
7+
if @views !is_opposite(coords[:, x], coords[:, y])
8+
@show coords[:, x] coords[:, y]
9+
return false
10+
end
811
end
12+
return true
913
end
10-
return true
11-
end
1214

1315

14-
@testset "get_periodic_coupling_info" begin
16+
@testset "get_periodic_coupling_info" begin
1517

16-
xgrid = simplexgrid(0:100, 0:100)
17-
# couple left <-> right
18-
let
19-
is_opposite(x, y) = abs(x[2] - y[2]) < 1.0e-12
20-
FES = FESpace{H1Pk{1, 2, 1}}(xgrid)
21-
X, Y, F = get_periodic_coupling_info(FES, xgrid, 4, 2, is_opposite)
22-
@test test_coords(X, Y, xgrid, is_opposite)
23-
end
24-
# couple top <-> bottom
25-
let
26-
is_opposite(x, y) = abs(x[1] - y[1]) < 1.0e-12
27-
FES = FESpace{H1Pk{1, 2, 1}}(xgrid)
28-
X, Y, F = get_periodic_coupling_info(FES, xgrid, 1, 3, is_opposite)
29-
@test test_coords(X, Y, xgrid, is_opposite)
18+
xgrid = simplexgrid(0:100, 0:100)
19+
# couple left <-> right
20+
let
21+
is_opposite(x, y) = abs(x[2] - y[2]) < 1.0e-12
22+
FES = FESpace{H1Pk{1, 2, 1}}(xgrid)
23+
X, Y, F = get_periodic_coupling_info(FES, xgrid, 4, 2, is_opposite)
24+
@test test_coords(X, Y, xgrid, is_opposite)
25+
end
26+
# couple top <-> bottom
27+
let
28+
is_opposite(x, y) = abs(x[1] - y[1]) < 1.0e-12
29+
FES = FESpace{H1Pk{1, 2, 1}}(xgrid)
30+
X, Y, F = get_periodic_coupling_info(FES, xgrid, 1, 3, is_opposite)
31+
@test test_coords(X, Y, xgrid, is_opposite)
32+
end
3033
end
31-
end
3234

3335

34-
# check if matrix is a proper coupling matrix
35-
# for testing, we assume that the FES on the "from" boundary is
36-
# contained in the FES in the "to" boundary
37-
function test_matrix(matrix; structured_grid = true)
36+
# check if matrix is a proper coupling matrix
37+
# for testing, we assume that the FES on the "from" boundary is
38+
# contained in the FES in the "to" boundary
39+
function test_matrix(matrix; structured_grid = true)
40+
41+
if structured_grid
42+
# only 1.0 entries!
43+
if findfirst((1.0, atol = 1.0e-8), matrix.nzval) !== nothing
44+
@show "found entries ≉ 1.0 "
45+
return false
46+
end
3847

39-
if structured_grid
40-
# only 1.0 entries!
41-
if findfirst((1.0, atol = 1.0e-8), matrix.nzval) !== nothing
42-
@show "found entries ≉ 1.0 "
43-
return false
48+
# at most one entry in each col
49+
for i in 1:size(matrix, 2)
50+
if sum(matrix[:, i]) > 1.0 + 1.0e-8
51+
@show sum(matrix[:, i]) i
52+
return false
53+
end
54+
end
4455
end
4556

46-
# at most one entry in each col
47-
for i in 1:size(matrix, 2)
48-
if sum(matrix[:, i]) > 1.0 + 1.0e-8
49-
@show sum(matrix[:, i]) i
57+
# row sum is 0.0 or 1.0
58+
for i in 1:size(matrix, 1)
59+
row_sum = sum(matrix[i, :])
60+
if !(row_sum == 0.0 || row_sum 1.0)
61+
@show row_sum i
5062
return false
5163
end
5264
end
53-
end
5465

55-
# row sum is 0.0 or 1.0
56-
for i in 1:size(matrix, 1)
57-
row_sum = sum(matrix[i, :])
58-
if !(row_sum == 0.0 || row_sum 1.0)
59-
@show row_sum i
60-
return false
61-
end
66+
return true
6267
end
6368

64-
return true
65-
end
69+
@testset "get_periodic_coupling_matrix" begin
6670

67-
@testset "get_periodic_coupling_matrix" begin
71+
# combine left/right at x=0/1
72+
function give_opposite!(y, x)
73+
y .= x
74+
y[1] = 1 - x[1]
75+
return nothing
76+
end
6877

69-
# combine left/right at x=0/1
70-
function give_opposite!(y, x)
71-
y .= x
72-
y[1] = 1 - x[1]
73-
return nothing
74-
end
78+
let # 3D P1
79+
xgrid = simplexgrid(0:0.1:1.0, 0:0.1:1.0, 0:0.1:1.0)
80+
FES = FESpace{H1P1{1}}(xgrid)
81+
A = get_periodic_coupling_matrix(FES, xgrid, 4, 2, give_opposite!, sparsity_tol = 1.0e-8)
82+
@test test_matrix(A)
83+
end
7584

76-
let # 3D P1
77-
xgrid = simplexgrid(0:0.1:1.0, 0:0.1:1.0, 0:0.1:1.0)
78-
FES = FESpace{H1P1{1}}(xgrid)
79-
A = get_periodic_coupling_matrix(FES, xgrid, 4, 2, give_opposite!, sparsity_tol = 1.0e-8)
80-
@test test_matrix(A)
81-
end
85+
let # 3D P2 with 2 components
86+
xgrid = simplexgrid(0:0.5:1.0, 0:0.5:1.0, 0:0.5:1.0)
87+
FES = FESpace{H1P2{2, 3}}(xgrid)
88+
A = get_periodic_coupling_matrix(FES, xgrid, 4, 2, give_opposite!, sparsity_tol = 1.0e-8)
89+
@test test_matrix(A)
90+
end
8291

83-
let # 3D P2 with 2 components
84-
xgrid = simplexgrid(0:0.5:1.0, 0:0.5:1.0, 0:0.5:1.0)
85-
FES = FESpace{H1P2{2, 3}}(xgrid)
86-
A = get_periodic_coupling_matrix(FES, xgrid, 4, 2, give_opposite!, sparsity_tol = 1.0e-8)
87-
@test test_matrix(A)
88-
end
92+
let # 2D unstructured
93+
b = SimplexGridBuilder(Generator = Triangulate)
94+
grid1 = simplexgrid(0:1.0, 0:1.0)
95+
grid2 = simplexgrid(0:1.0, 0:0.5:1.0)
96+
bregions!(b, grid1, 1 => 1, 3 => 3, 4 => 4)
97+
bregions!(b, grid2, 2 => 2)
98+
xgrid = simplexgrid(b)
8999

90-
let # 2D unstructured
91-
b = SimplexGridBuilder(Generator = Triangulate)
92-
grid1 = simplexgrid(0:1.0, 0:1.0)
93-
grid2 = simplexgrid(0:1.0, 0:0.5:1.0)
94-
bregions!(b, grid1, 1 => 1, 3 => 3, 4 => 4)
95-
bregions!(b, grid2, 2 => 2)
96-
xgrid = simplexgrid(b)
97-
98-
FES = FESpace{H1P1{1}}(xgrid)
99-
A = get_periodic_coupling_matrix(FES, xgrid, 4, 2, give_opposite!, sparsity_tol = 1.0e-8)
100-
@test test_matrix(A; structured_grid = false)
100+
FES = FESpace{H1P1{1}}(xgrid)
101+
A = get_periodic_coupling_matrix(FES, xgrid, 4, 2, give_opposite!, sparsity_tol = 1.0e-8)
102+
@test test_matrix(A; structured_grid = false)
103+
end
101104
end
105+
106+
return nothing
102107
end

0 commit comments

Comments
 (0)