|
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 |
8 | 11 | end |
| 12 | + return true |
9 | 13 | end |
10 | | - return true |
11 | | -end |
12 | 14 |
|
13 | 15 |
|
14 | | -@testset "get_periodic_coupling_info" begin |
| 16 | + @testset "get_periodic_coupling_info" begin |
15 | 17 |
|
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 |
30 | 33 | end |
31 | | -end |
32 | 34 |
|
33 | 35 |
|
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 |
38 | 47 |
|
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 |
44 | 55 | end |
45 | 56 |
|
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 |
50 | 62 | return false |
51 | 63 | end |
52 | 64 | end |
53 | | - end |
54 | 65 |
|
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 |
62 | 67 | end |
63 | 68 |
|
64 | | - return true |
65 | | -end |
| 69 | + @testset "get_periodic_coupling_matrix" begin |
66 | 70 |
|
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 |
68 | 77 |
|
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 |
75 | 84 |
|
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 |
82 | 91 |
|
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) |
89 | 99 |
|
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 |
101 | 104 | end |
| 105 | + |
| 106 | + return nothing |
102 | 107 | end |
0 commit comments