comparison test/Grids/tensor_grid_test.jl @ 1858:4a9be96f2569 feature/documenter_logo

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 12 Jan 2025 21:18:44 +0100
parents 054447ac4b0e
children 516eaabf1169
comparison
equal deleted inserted replaced
1857:ffde7dad9da5 1858:4a9be96f2569
1 using Test
2 using Diffinitive.Grids
3 using StaticArrays
4
5 @testset "TensorGrid" begin
6 g₁ = EquidistantGrid(range(0,1,length=11))
7 g₂ = EquidistantGrid(range(2,3,length=6))
8 g₃ = EquidistantGrid(1:10)
9 g₄ = ZeroDimGrid(@SVector[1,2])
10
11 @test TensorGrid(g₁, g₂) isa TensorGrid
12 @test TensorGrid(g₁, g₂) isa Grid{SVector{2,Float64}, 2}
13 @test TensorGrid(g₃, g₃) isa Grid{SVector{2,Int}, 2}
14 @test TensorGrid(g₁, g₂, g₃) isa Grid{SVector{3,Float64}, 3}
15 @test TensorGrid(g₁, g₄) isa Grid{SVector{3,Float64}, 1}
16 @test TensorGrid(g₁, g₄, g₂) isa Grid{SVector{4,Float64}, 2}
17
18 @testset "Indexing Interface" begin
19 @testset "regular indexing" begin
20 @test TensorGrid(g₁, g₂)[1,1] isa SVector{2,Float64}
21 @test TensorGrid(g₁, g₂)[1,1] == [0.0,2.0]
22 @test TensorGrid(g₁, g₂)[3,5] == [0.2,2.8]
23 @test TensorGrid(g₁, g₂)[10,6] == [0.9,3.0]
24
25 @test TensorGrid(g₁, g₃)[1,1] isa SVector{2,Float64}
26 @test TensorGrid(g₁, g₃)[1,1] == [0.0,1.0]
27
28 @test TensorGrid(g₁, g₂, g₃)[3,4,5] isa SVector{3,Float64}
29 @test TensorGrid(g₁, g₂, g₃)[3,4,5] == [0.2, 2.6, 5.0]
30
31 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64}
32 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.]
33
34 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64}
35 @test TensorGrid(g₁, g₄, g₂)[3,2] == [0.2, 1., 2., 2.2]
36
37 g = TensorGrid(g₁, g₂)
38 @test g[begin, begin] == g[1,1]
39 @test g[begin, end] == g[1,6]
40 @test g[end, end] == g[11,6]
41 end
42
43 @testset "cartesian indexing" begin
44 cases = [
45 (TensorGrid(g₁, g₂), (1,1) ),
46 (TensorGrid(g₁, g₂), (3,5) ),
47 (TensorGrid(g₁, g₂), (10,6) ),
48 (TensorGrid(g₁, g₃), (1,1) ),
49 (TensorGrid(g₁, g₂, g₃), (3,4,5)),
50 (TensorGrid(g₁, g₄), (3) ),
51 (TensorGrid(g₁, g₄, g₂), (3,2) ),
52 ]
53
54 @testset "i = $is" for (g, is) ∈ cases
55 @test g[CartesianIndex(is...)] == g[is...]
56 end
57 end
58
59 @testset "eachindex" begin
60 @test eachindex(TensorGrid(g₁, g₂)) == CartesianIndices((11,6))
61 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10))
62 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10))
63 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,))
64 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6))
65 end
66
67 @testset "firstindex" begin
68 @test firstindex(TensorGrid(g₁, g₂, g₃), 1) == 1
69 @test firstindex(TensorGrid(g₁, g₂, g₃), 2) == 1
70 @test firstindex(TensorGrid(g₁, g₂, g₃), 3) == 1
71 end
72
73 @testset "lastindex" begin
74 @test lastindex(TensorGrid(g₁, g₂, g₃), 1) == 11
75 @test lastindex(TensorGrid(g₁, g₂, g₃), 2) == 6
76 @test lastindex(TensorGrid(g₁, g₂, g₃), 3) == 10
77 end
78 end
79
80 @testset "Iterator interface" begin
81 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64}
82 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64}
83 @test eltype(TensorGrid(g₁, g₂, g₃)) == SVector{3,Float64}
84 @test eltype(TensorGrid(g₁, g₄)) == SVector{3,Float64}
85 @test eltype(TensorGrid(g₁, g₄, g₂)) == SVector{4,Float64}
86
87 @test eltype(typeof(TensorGrid(g₁, g₂))) == SVector{2,Float64}
88 @test eltype(typeof(TensorGrid(g₁, g₃))) == SVector{2,Float64}
89 @test eltype(typeof(TensorGrid(g₁, g₂, g₃))) == SVector{3,Float64}
90 @test eltype(typeof(TensorGrid(g₁, g₄))) == SVector{3,Float64}
91 @test eltype(typeof(TensorGrid(g₁, g₄, g₂))) == SVector{4,Float64}
92
93 @test size(TensorGrid(g₁, g₂)) == (11,6)
94 @test size(TensorGrid(g₁, g₃)) == (11,10)
95 @test size(TensorGrid(g₁, g₂, g₃)) == (11,6,10)
96 @test size(TensorGrid(g₁, g₄)) == (11,)
97 @test size(TensorGrid(g₁, g₄, g₂)) == (11,6)
98
99 @test size(TensorGrid(g₁, g₂, g₃),1) == 11
100 @test size(TensorGrid(g₁, g₂, g₃),2) == 6
101 @test size(TensorGrid(g₁, g₂, g₃),3) == 10
102 @test size(TensorGrid(g₁, g₄, g₂),1) == 11
103 @test size(TensorGrid(g₁, g₄, g₂),2) == 6
104
105 @test length(TensorGrid(g₁, g₂)) == 66
106 @test length(TensorGrid(g₁, g₃)) == 110
107 @test length(TensorGrid(g₁, g₂, g₃)) == 660
108 @test length(TensorGrid(g₁, g₄)) == 11
109 @test length(TensorGrid(g₁, g₄, g₂)) == 66
110
111 @test Base.IteratorSize(TensorGrid(g₁, g₂)) == Base.HasShape{2}()
112 @test Base.IteratorSize(TensorGrid(g₁, g₃)) == Base.HasShape{2}()
113 @test Base.IteratorSize(TensorGrid(g₁, g₂, g₃)) == Base.HasShape{3}()
114 @test Base.IteratorSize(TensorGrid(g₁, g₄)) == Base.HasShape{1}()
115 @test Base.IteratorSize(TensorGrid(g₁, g₄, g₂)) == Base.HasShape{2}()
116
117 @test iterate(TensorGrid(g₁, g₂))[1] isa SVector{2,Float64}
118 @test iterate(TensorGrid(g₁, g₃))[1] isa SVector{2,Float64}
119 @test iterate(TensorGrid(g₁, g₂, g₃))[1] isa SVector{3,Float64}
120 @test iterate(TensorGrid(g₁, g₄))[1] isa SVector{3,Float64}
121 @test iterate(TensorGrid(g₁, g₄, g₂))[1] isa SVector{4,Float64}
122
123 @test collect(TensorGrid(g₁, g₂)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)]
124 @test collect(TensorGrid(g₁, g₃)) == [@SVector[x,y] for x ∈ range(0,1,length=11), y ∈ 1:10]
125 @test collect(TensorGrid(g₁, g₂, g₃)) == [@SVector[x,y,z] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6), z ∈ 1:10]
126 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)]
127 @test collect(TensorGrid(g₁, g₄, g₂)) == [@SVector[x,1,2,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)]
128 end
129
130 @testset "Base" begin
131 g₁ = EquidistantGrid(range(0,1,length=11))
132 g₂ = EquidistantGrid(range(2,3,length=6))
133 g = TensorGrid(g₁, g₂)
134
135 @test axes(g, 1) == 1:11
136 @test axes(g, 2) == 1:6
137 @test axes(g) == (1:11,1:6)
138 end
139
140 @testset "spacing" begin
141 g₁ = EquidistantGrid(range(0,1,length=11))
142 g₂ = EquidistantGrid(range(2,3,length=6))
143 g₃ = ZeroDimGrid(@SVector[1,2])
144
145 @test spacing(TensorGrid(g₁)) == (1/10,)
146 @test spacing(TensorGrid(g₂)) == (1/5,)
147
148 @test spacing(TensorGrid(g₁, g₂)) == (1/10, 1/5)
149
150 @test spacing(TensorGrid(g₁, g₃)) == (1/10,)
151 @test spacing(TensorGrid(g₃, g₂)) == (1/5,)
152
153
154 @test spacing(TensorGrid(g₁, g₂, g₁)) == (1/10, 1/5, 1/10)
155
156 @test spacing(TensorGrid(g₃, g₂, g₁)) == (1/5, 1/10)
157 @test spacing(TensorGrid(g₁, g₃, g₁)) == (1/10, 1/10)
158 @test spacing(TensorGrid(g₁, g₂, g₃)) == (1/10, 1/5)
159 end
160
161 @testset "min_spacing" begin
162 g₁ = EquidistantGrid(range(0,1,length=11))
163 g₂ = EquidistantGrid(range(2,3,length=6))
164 g₃ = ZeroDimGrid(@SVector[1,2])
165
166 @test min_spacing(TensorGrid(g₁, g₂)) == 1/10
167 @test min_spacing(TensorGrid(g₂, g₃)) == 1/5
168 end
169
170 @testset "refine" begin
171 g1(n) = EquidistantGrid(range(0,1,length=n))
172 g2(n) = EquidistantGrid(range(2,3,length=n))
173
174 @test refine(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6))
175 @test refine(TensorGrid(g1(11), g2(6)),2) == TensorGrid(g1(21), g2(11))
176 @test refine(TensorGrid(g1(11), g2(6)),3) == TensorGrid(g1(31), g2(16))
177 @test refine(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄)
178 @test refine(TensorGrid(g1(11), g₄), 2) == TensorGrid(g1(21), g₄)
179 end
180
181 @testset "coarsen" begin
182 g1(n) = EquidistantGrid(range(0,1,length=n))
183 g2(n) = EquidistantGrid(range(2,3,length=n))
184
185 @test coarsen(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6))
186 @test coarsen(TensorGrid(g1(21), g2(11)),2) == TensorGrid(g1(11), g2(6))
187 @test coarsen(TensorGrid(g1(31), g2(16)),3) == TensorGrid(g1(11), g2(6))
188 @test coarsen(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄)
189 @test coarsen(TensorGrid(g1(21), g₄), 2) == TensorGrid(g1(11), g₄)
190 end
191
192 @testset "boundary_identifiers" begin
193 @test boundary_identifiers(TensorGrid(g₁, g₂)) == map((n,id)->TensorGridBoundary{n,id}(), (1,1,2,2), (LowerBoundary,UpperBoundary,LowerBoundary,UpperBoundary))
194 @test boundary_identifiers(TensorGrid(g₁, g₄)) == (TensorGridBoundary{1,LowerBoundary}(),TensorGridBoundary{1,UpperBoundary}())
195 end
196
197 @testset "boundary_grid" begin
198 @test boundary_grid(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₂)
199 @test boundary_grid(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₄)
200 end
201
202 @testset "boundary_indices" begin
203 g₁ = EquidistantGrid(range(0,1,length=11))
204 g₂ = EquidistantGrid(range(2,3,length=6))
205 g₄ = ZeroDimGrid(@SVector[1,2])
206
207 @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, LowerBoundary}()) == (1,:)
208 @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}()) == (11,:)
209 @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, LowerBoundary}()) == (:,1)
210 @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, UpperBoundary}()) == (:,6)
211 @test boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}()) == (1,)
212 @test boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}()) == (11,)
213 @test boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, LowerBoundary}()) == (1,)
214 @test boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, UpperBoundary}()) == (11,)
215 end
216 end
217
218 @testset "combined_coordinate_vector_type" begin
219 @test Grids.combined_coordinate_vector_type(Float64) == Float64
220 @test Grids.combined_coordinate_vector_type(Float64, Int) == SVector{2,Float64}
221 @test Grids.combined_coordinate_vector_type(Float32, Int16, Int32) == SVector{3,Float32}
222
223 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}) == SVector{2,Float64}
224 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Float64}) == SVector{3,Float64}
225 @test Grids.combined_coordinate_vector_type(SVector{2,Float64}, SVector{1,Int}, SVector{3, Float32}) == SVector{6,Float64}
226 end
227
228 @testset "combine_coordinates" begin
229 @test Grids.combine_coordinates(1,2,3) isa SVector{3, Int}
230 @test Grids.combine_coordinates(1,2,3) == [1,2,3]
231 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64}
232 @test Grids.combine_coordinates(1,2.,3) == [1,2,3]
233 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64}
234 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3]
235 end
236
237 @testset "grid_and_local_dim_index" begin
238 cases = [
239 ((1,), 1) => (1,1),
240
241 ((1,1), 1) => (1,1),
242 ((1,1), 2) => (2,1),
243
244 ((1,2), 1) => (1,1),
245 ((1,2), 2) => (2,1),
246 ((1,2), 3) => (2,2),
247
248 ((2,1), 1) => (1,1),
249 ((2,1), 2) => (1,2),
250 ((2,1), 3) => (2,1),
251
252 ((2,1,3), 1) => (1,1),
253 ((2,1,3), 2) => (1,2),
254 ((2,1,3), 3) => (2,1),
255 ((2,1,3), 4) => (3,1),
256 ((2,1,3), 5) => (3,2),
257 ((2,1,3), 6) => (3,3),
258 ]
259
260 @testset "grid_and_local_dim_index$args" for (args, expected) ∈ cases
261 @test Grids.grid_and_local_dim_index(args...) == expected
262 end
263 end