comparison test/Grids/tensor_grid_test.jl @ 1854:654a2b7e6824 tooling/benchmarks

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 11 Jan 2025 10:19:47 +0100
parents 054447ac4b0e
children 516eaabf1169
comparison
equal deleted inserted replaced
1378:2b5480e2d4bf 1854:654a2b7e6824
1 using Test 1 using Test
2 using Sbplib.Grids 2 using Diffinitive.Grids
3 using StaticArrays 3 using StaticArrays
4 using Sbplib.RegionIndices
5 4
6 @testset "TensorGrid" begin 5 @testset "TensorGrid" begin
7 g₁ = EquidistantGrid(range(0,1,length=11)) 6 g₁ = EquidistantGrid(range(0,1,length=11))
8 g₂ = EquidistantGrid(range(2,3,length=6)) 7 g₂ = EquidistantGrid(range(2,3,length=6))
9 g₃ = EquidistantGrid(1:10) 8 g₃ = EquidistantGrid(1:10)
32 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64} 31 @test TensorGrid(g₁, g₄)[3] isa SVector{3,Float64}
33 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.] 32 @test TensorGrid(g₁, g₄)[3] == [0.2, 1., 2.]
34 33
35 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64} 34 @test TensorGrid(g₁, g₄, g₂)[3,2] isa SVector{4,Float64}
36 @test TensorGrid(g₁, g₄, g₂)[3,2] == [0.2, 1., 2., 2.2] 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]
37 end 41 end
38 42
39 @testset "cartesian indexing" begin 43 @testset "cartesian indexing" begin
40 cases = [ 44 cases = [
41 (TensorGrid(g₁, g₂), (1,1) ), 45 (TensorGrid(g₁, g₂), (1,1) ),
57 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10)) 61 @test eachindex(TensorGrid(g₁, g₃)) == CartesianIndices((11,10))
58 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10)) 62 @test eachindex(TensorGrid(g₁, g₂, g₃)) == CartesianIndices((11,6,10))
59 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,)) 63 @test eachindex(TensorGrid(g₁, g₄)) == CartesianIndices((11,))
60 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6)) 64 @test eachindex(TensorGrid(g₁, g₄, g₂)) == CartesianIndices((11,6))
61 end 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
62 end 78 end
63 79
64 @testset "Iterator interface" begin 80 @testset "Iterator interface" begin
65 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64} 81 @test eltype(TensorGrid(g₁, g₂)) == SVector{2,Float64}
66 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64} 82 @test eltype(TensorGrid(g₁, g₃)) == SVector{2,Float64}
67 @test eltype(TensorGrid(g₁, g₂, g₃)) == SVector{3,Float64} 83 @test eltype(TensorGrid(g₁, g₂, g₃)) == SVector{3,Float64}
68 @test eltype(TensorGrid(g₁, g₄)) == SVector{3,Float64} 84 @test eltype(TensorGrid(g₁, g₄)) == SVector{3,Float64}
69 @test eltype(TensorGrid(g₁, g₄, g₂)) == SVector{4,Float64} 85 @test eltype(TensorGrid(g₁, g₄, g₂)) == SVector{4,Float64}
70 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
71 @test size(TensorGrid(g₁, g₂)) == (11,6) 93 @test size(TensorGrid(g₁, g₂)) == (11,6)
72 @test size(TensorGrid(g₁, g₃)) == (11,10) 94 @test size(TensorGrid(g₁, g₃)) == (11,10)
73 @test size(TensorGrid(g₁, g₂, g₃)) == (11,6,10) 95 @test size(TensorGrid(g₁, g₂, g₃)) == (11,6,10)
74 @test size(TensorGrid(g₁, g₄)) == (11,) 96 @test size(TensorGrid(g₁, g₄)) == (11,)
75 @test size(TensorGrid(g₁, g₄, g₂)) == (11,6) 97 @test size(TensorGrid(g₁, g₄, g₂)) == (11,6)
76 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
77 @test Base.IteratorSize(TensorGrid(g₁, g₂)) == Base.HasShape{2}() 111 @test Base.IteratorSize(TensorGrid(g₁, g₂)) == Base.HasShape{2}()
78 @test Base.IteratorSize(TensorGrid(g₁, g₃)) == Base.HasShape{2}() 112 @test Base.IteratorSize(TensorGrid(g₁, g₃)) == Base.HasShape{2}()
79 @test Base.IteratorSize(TensorGrid(g₁, g₂, g₃)) == Base.HasShape{3}() 113 @test Base.IteratorSize(TensorGrid(g₁, g₂, g₃)) == Base.HasShape{3}()
80 @test Base.IteratorSize(TensorGrid(g₁, g₄)) == Base.HasShape{1}() 114 @test Base.IteratorSize(TensorGrid(g₁, g₄)) == Base.HasShape{1}()
81 @test Base.IteratorSize(TensorGrid(g₁, g₄, g₂)) == Base.HasShape{2}() 115 @test Base.IteratorSize(TensorGrid(g₁, g₄, g₂)) == Base.HasShape{2}()
91 @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] 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]
92 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)] 126 @test collect(TensorGrid(g₁, g₄)) == [@SVector[x,1,2] for x ∈ range(0,1,length=11)]
93 @test collect(TensorGrid(g₁, g₄, g₂)) == [@SVector[x,1,2,y] for x ∈ range(0,1,length=11), y ∈ range(2,3,length=6)] 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)]
94 end 128 end
95 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
96 @testset "refine" begin 170 @testset "refine" begin
97 g1(n) = EquidistantGrid(range(0,1,length=n)) 171 g1(n) = EquidistantGrid(range(0,1,length=n))
98 g2(n) = EquidistantGrid(range(2,3,length=n)) 172 g2(n) = EquidistantGrid(range(2,3,length=n))
99 173
100 @test refine(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6)) 174 @test refine(TensorGrid(g1(11), g2(6)),1) == TensorGrid(g1(11), g2(6))
114 @test coarsen(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄) 188 @test coarsen(TensorGrid(g1(11), g₄), 1) == TensorGrid(g1(11), g₄)
115 @test coarsen(TensorGrid(g1(21), g₄), 2) == TensorGrid(g1(11), g₄) 189 @test coarsen(TensorGrid(g1(21), g₄), 2) == TensorGrid(g1(11), g₄)
116 end 190 end
117 191
118 @testset "boundary_identifiers" begin 192 @testset "boundary_identifiers" begin
119 @test boundary_identifiers(TensorGrid(g₁, g₂)) == map((n,id)->TensorGridBoundary{n,id}(), (1,1,2,2), (Lower,Upper,Lower,Upper)) 193 @test boundary_identifiers(TensorGrid(g₁, g₂)) == map((n,id)->TensorGridBoundary{n,id}(), (1,1,2,2), (LowerBoundary,UpperBoundary,LowerBoundary,UpperBoundary))
120 @test boundary_identifiers(TensorGrid(g₁, g₄)) == (TensorGridBoundary{1,Lower}(),TensorGridBoundary{1,Upper}()) 194 @test boundary_identifiers(TensorGrid(g₁, g₄)) == (TensorGridBoundary{1,LowerBoundary}(),TensorGridBoundary{1,UpperBoundary}())
121 end 195 end
122 196
123 @testset "boundary_grid" begin 197 @testset "boundary_grid" begin
124 @test boundary_grid(TensorGrid(g₁, g₂), TensorGridBoundary{1, Upper}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₂) 198 @test boundary_grid(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}()) == TensorGrid(ZeroDimGrid(g₁[end]), g₂)
125 @test boundary_grid(TensorGrid(g₁, g₄), TensorGridBoundary{1, Upper}()) == 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,)
126 end 215 end
127 end 216 end
128 217
129 @testset "combined_coordinate_vector_type" begin 218 @testset "combined_coordinate_vector_type" begin
130 @test Grids.combined_coordinate_vector_type(Float64) == Float64 219 @test Grids.combined_coordinate_vector_type(Float64) == Float64
142 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64} 231 @test Grids.combine_coordinates(1,2.,3) isa SVector{3, Float64}
143 @test Grids.combine_coordinates(1,2.,3) == [1,2,3] 232 @test Grids.combine_coordinates(1,2.,3) == [1,2,3]
144 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64} 233 @test Grids.combine_coordinates(1,@SVector[2.,3]) isa SVector{3, Float64}
145 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3] 234 @test Grids.combine_coordinates(1,@SVector[2.,3]) == [1,2,3]
146 end 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