comparison test/Grids/mapped_grid_test.jl @ 1506:535f32316637 feature/grids/curvilinear

Rename from curvilinear to mapped
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 16 Feb 2024 15:28:19 +0100
parents test/Grids/curvilinear_grid_test.jl@704a84eef8b6
children 69790e9d1652
comparison
equal deleted inserted replaced
1505:63101a1cd0e6 1506:535f32316637
1 using Sbplib.Grids
2 using Sbplib.RegionIndices
3 using Test
4 using StaticArrays
5
6 @testset "MappedGrid" begin
7 lg = equidistant_grid((11,11), (0,0), (1,1)) # TODO: Change dims of the grid to be different
8 x̄ = map(ξ̄ -> 2ξ̄, lg)
9 J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
10 mg = MappedGrid(lg, x̄, J)
11
12 # TODO: Test constructor for different dims of range and domain for the coordinates
13 # TODO: Test constructor with different type than TensorGrid. a dummy type?
14
15 @test_broken false # @test_throws ArgumentError("Sizes must match") MappedGrid(lg, map(ξ̄ -> @SArray[ξ̄[1], ξ̄[2], -ξ̄[1]], lg), rand(SMatrix{2,3,Float64},15,11))
16
17
18 @test mg isa Grid{SVector{2, Float64},2}
19
20 @test jacobian(mg) isa Array{<:AbstractMatrix}
21 @test logicalgrid(mg) isa Grid
22
23 @testset "Indexing Interface" begin
24 mg = MappedGrid(lg, x̄, J)
25 @test mg[1,1] == [0.0, 0.0]
26 @test mg[4,2] == [0.6, 0.2]
27 @test mg[6,10] == [1., 1.8]
28
29 @test mg[begin, begin] == [0.0, 0.0]
30 @test mg[end,end] == [2.0, 2.0]
31 @test mg[begin,end] == [0., 2.]
32
33 @test eachindex(mg) == CartesianIndices((11,11))
34
35 @testset "cartesian indexing" begin
36 cases = [
37 (1,1) ,
38 (3,5) ,
39 (10,6),
40 (1,1) ,
41 (3,2) ,
42 ]
43
44 @testset "i = $is" for (lg, is) ∈ cases
45 @test mg[CartesianIndex(is...)] == mg[is...]
46 end
47 end
48
49 @testset "eachindex" begin
50 @test eachindex(mg) == CartesianIndices((11,11))
51 end
52
53 @testset "firstindex" begin
54 @test firstindex(mg, 1) == 1
55 @test firstindex(mg, 2) == 1
56 end
57
58 @testset "lastindex" begin
59 @test lastindex(mg, 1) == 11
60 @test lastindex(mg, 2) == 11
61 end
62 end
63 # TODO: Test with different types of logical grids
64
65 @testset "Iterator interface" begin
66 sg = MappedGrid(
67 equidistant_grid((15,11), (0,0), (1,1)),
68 map(ξ̄ -> @SArray[ξ̄[1], ξ̄[2], -ξ̄[1]], lg), rand(SMatrix{2,3,Float64},15,11)
69 )
70
71 @test eltype(mg) == SVector{2,Float64}
72 @test eltype(sg) == SVector{3,Float64}
73
74 @test eltype(typeof(mg)) == SVector{2,Float64}
75 @test eltype(typeof(sg)) == SVector{3,Float64}
76
77 @test size(mg) == (11,11)
78 @test size(sg) == (15,11)
79
80 @test size(mg,2) == 11
81 @test size(sg,2) == 11
82
83 @test length(mg) == 121
84 @test length(sg) == 165
85
86 @test Base.IteratorSize(mg) == Base.HasShape{2}()
87 @test Base.IteratorSize(typeof(mg)) == Base.HasShape{2}()
88
89 @test Base.IteratorSize(sg) == Base.HasShape{2}()
90 @test Base.IteratorSize(typeof(sg)) == Base.HasShape{2}()
91
92 element, state = iterate(mg)
93 @test element == lg[1,1].*2
94 element, _ = iterate(mg, state)
95 @test element == lg[2,1].*2
96
97 element, state = iterate(sg)
98 @test element == sg.physicalcoordinates[1,1]
99 element, _ = iterate(sg, state)
100 @test element == sg.physicalcoordinates[2,1]
101
102 @test collect(mg) == 2 .* lg
103 end
104
105 @testset "Base" begin
106 @test ndims(mg) == 2
107 end
108
109 @testset "boundary_identifiers" begin
110 @test boundary_identifiers(mg) == boundary_identifiers(lg)
111 end
112
113 @testset "boundary_indices" begin
114 @test boundary_indices(mg, CartesianBoundary{1,Lower}()) == boundary_indices(lg,CartesianBoundary{1,Lower}())
115 @test boundary_indices(mg, CartesianBoundary{2,Lower}()) == boundary_indices(lg,CartesianBoundary{2,Lower}())
116 @test boundary_indices(mg, CartesianBoundary{1,Upper}()) == boundary_indices(lg,CartesianBoundary{1,Upper}())
117 end
118
119 @testset "boundary_grid" begin
120 x̄((ξ, η)) = @SVector[ξ, η*(1+ξ*(ξ-1))]
121 J((ξ, η)) = @SMatrix[
122 1 0;
123 η*(2ξ-1) 1+ξ*(ξ-1);
124 ]
125
126 mg = mapped_grid(x̄, J, 10, 11)
127 J1((ξ, η)) = @SMatrix[
128 1 ;
129 η*(2ξ-1);
130 ]
131 J2((ξ, η)) = @SMatrix[
132 0;
133 1+ξ*(ξ-1);
134 ]
135
136 function test_boundary_grid(mg, bId, Jb)
137 bg = boundary_grid(mg, bId)
138
139 lg = logicalgrid(mg)
140 expected_bg = MappedGrid(
141 boundary_grid(lg, bId),
142 map(x̄, boundary_grid(lg, bId)),
143 map(Jb, boundary_grid(lg, bId)),
144 )
145
146 @testset let bId=bId, bg=bg, expected_bg=expected_bg
147 @test collect(bg) == collect(expected_bg)
148 @test logicalgrid(bg) == logicalgrid(expected_bg)
149 @test jacobian(bg) == jacobian(expected_bg)
150 # TODO: Implement equality of a curvilinear grid and simlify the above
151 end
152 end
153
154 @testset test_boundary_grid(mg, TensorGridBoundary{1, Lower}(), J2)
155 @testset test_boundary_grid(mg, TensorGridBoundary{1, Upper}(), J2)
156 @testset test_boundary_grid(mg, TensorGridBoundary{2, Lower}(), J1)
157 @testset test_boundary_grid(mg, TensorGridBoundary{2, Upper}(), J1)
158 end
159
160 # TBD: Should curvilinear grid support refining and coarsening?
161 # This would require keeping the coordinate mapping around which seems burdensome, and might increase compilation time?
162 @testset "refine" begin
163 @test_broken refine(mg, 1) == mg
164 @test_broken refine(mg, 2) == MappedGrid(refine(lg,2), x̄, J)
165 @test_broken refine(mg, 3) == MappedGrid(refine(lg,3), x̄, J)
166 end
167
168 @testset "coarsen" begin
169 lg = equidistant_grid((11,11), (0,0), (1,1)) # TODO: Change dims of the grid to be different
170 x̄ = map(ξ̄ -> 2ξ̄, lg)
171 J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
172 mg = MappedGrid(lg, x̄, J)
173
174 @test_broken coarsen(mg, 1) == mg
175 @test_broken coarsen(mg, 2) == MappedGrid(coarsen(lg,2), x̄, J)
176
177 @test_broken false # @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(mg, 3)
178 end
179 end
180
181 @testset "mapped_grid" begin
182 x̄((ξ, η)) = @SVector[ξ, η*(1+ξ*(ξ-1))]
183 J((ξ, η)) = @SMatrix[
184 1 0;
185 η*(2ξ-1) 1+ξ*(ξ-1);
186 ]
187 mg = mapped_grid(x̄, J, 10, 11)
188 @test mg isa MappedGrid{SVector{2,Float64}, 2}
189
190 lg = equidistant_grid((10,11), (0,0), (1,1))
191 @test logicalgrid(mg) == lg
192 @test collect(mg) == map(x̄, lg)
193 end