comparison test/Grids/equidistant_grid_test.jl @ 1252:c150eabaf656 refactor/grids

Fix or mark tests broken where needed
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 22 Feb 2023 12:47:08 +0100
parents 31041ef8092a
children ff8f335c32d1
comparison
equal deleted inserted replaced
1251:6f75f2d2bf5c 1252:c150eabaf656
3 using Sbplib.RegionIndices 3 using Sbplib.RegionIndices
4 using Sbplib.LazyTensors 4 using Sbplib.LazyTensors
5 5
6 6
7 @testset "EquidistantGrid" begin 7 @testset "EquidistantGrid" begin
8 @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid 8 @test EquidistantGrid(0:0.1:10) isa EquidistantGrid
9 @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid 9 @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid
10 # constuctor 10 @test EquidistantGrid(LinRange(0,1,11)) isa EquidistantGrid
11 @test_throws DomainError EquidistantGrid(0,0.0,1.0)
12 @test_throws DomainError EquidistantGrid(1,1.0,1.0)
13 @test_throws DomainError EquidistantGrid(1,1.0,-1.0)
14 @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,))
15 11
16 @testset "Base" begin 12 @testset "Base" begin
17 @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64 13 @test eltype(EquidistantGrid(0:10)) == Int
18 @test eltype(EquidistantGrid((4,3),(0,0),(1,3))) == Int 14 @test eltype(EquidistantGrid(0:2:10)) == Int
19 @test size(EquidistantGrid(4,0.0,1.0)) == (4,) 15 @test eltype(EquidistantGrid(0:0.1:10)) == Float64
20 @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) 16 @test size(EquidistantGrid(0:10)) == (11,)
21 @test ndims(EquidistantGrid(4,0.0,1.0)) == 1 17 @test size(EquidistantGrid(0:0.1:10)) == (101,)
22 @test ndims(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2 18 @test ndims(EquidistantGrid(0:10)) == 1
23 end 19 end
24 20
25 @testset "spacing" begin 21 @testset "spacing" begin
26 @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13 22 @test spacing(EquidistantGrid(0:10)) == 1
27 @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13 23 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1
28 end 24 end
29 25
30 @testset "inverse_spacing" begin 26 @testset "inverse_spacing" begin
31 @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13 27 @test inverse_spacing(EquidistantGrid(0:10)) == 1
32 @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13 28 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10
33 end 29 end
34 30
35 @testset "points" begin 31 @testset "collect" begin
36 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) 32 g = EquidistantGrid(0:0.1:0.5)
37 gp = points(g); 33 @test_broken collect(g) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
34 end
35
36 @testset "getindex" begin
37 g = EquidistantGrid(0:0.1:10)
38 @test g[1] == 0.0
39 @test g[5] == 0.4
40 @test g[101] == 10.0
41
42 @test_broken g[begin] == 0.0
43 @test_broken g[end] == 10.0
44 end
45
46 @testset "boundary_identifiers" begin
47 g = EquidistantGrid(0:0.1:10)
48 @test boundary_identifiers(g) == (Lower(), Upper())
49 @inferred boundary_identifiers(g)
50 end
51
52 @testset "boundary_grid" begin
53 g = EquidistantGrid(0:0.1:1)
54 @test_broken boundary_grid(g, Lower()) == ZeroDimGrid(fill(0.0)) # TBD: Is fill necessary here? Why?
55 @test_broken boundary_grid(g, Upper()) == ZeroDimGrid(fill(1.0))
56 end
57
58 @testset "refine" begin
59 g = EquidistantGrid(0:0.1:1)
60 @test refine(g, 1) == g
61 @test refine(g, 2) == EquidistantGrid(0:0.05:1)
62 @test refine(g, 3) == EquidistantGrid(0:(0.1/3):1)
63 end
64
65 @testset "coarsen" begin
66 g = EquidistantGrid(0:1:10)
67 @test coarsen(g, 1) == g
68 @test coarsen(g, 2) == EquidistantGrid(0:2:10)
69
70 g = EquidistantGrid(0:0.1:1)
71 @test coarsen(g, 1) == g
72 @test coarsen(g, 2) == EquidistantGrid(0:0.2:1)
73
74 g = EquidistantGrid(0:10)
75 @test coarsen(g, 1) == EquidistantGrid(0:1:10)
76 @test coarsen(g, 2) == EquidistantGrid(0:2:10)
77
78 @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(g, 3)
79 end
80 end
81
82
83 @testset "equidistant_grid" begin
84 @test equidistant_grid(4,0.0,1.0) isa TensorGrid
85 @test equidistant_grid(4,0.0,8.0) isa TensorGrid
86 # constuctor
87 @test_throws DomainError equidistant_grid(0,0.0,1.0)
88 @test_throws DomainError equidistant_grid(1,1.0,1.0)
89 @test_throws DomainError equidistant_grid(1,1.0,-1.0)
90 @test equidistant_grid(4,0.0,1.0) == equidistant_grid((4,),(0.0,),(1.0,))
91
92 @testset "Base" begin
93 @test eltype(equidistant_grid(4,0.0,1.0)) == Float64
94 @test_broken eltype(equidistant_grid((4,3),(0,0),(1,3))) == AbstractVector{Float64}
95 @test size(equidistant_grid(4,0.0,1.0)) == (4,)
96 @test size(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3)
97 @test ndims(equidistant_grid(4,0.0,1.0)) == 1
98 @test ndims(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == 2
99 end
100
101 @testset "getindex" begin
102 g = equidistant_grid((5,3), (-1.0,0.0), (0.0,7.11))
103 # gp = collect(g);
104 gp = rand(size(g)...)
38 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); 105 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11);
39 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); 106 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11);
40 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); 107 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11);
41 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); 108 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11);
42 (0.,0.) (0.,7.11/2) (0.,7.11)] 109 (0.,0.) (0.,7.11/2) (0.,7.11)]
43 for i ∈ eachindex(gp) 110 for i ∈ eachindex(gp)
44 @test [gp[i]...] ≈ [p[i]...] atol=5e-13 111 @test_broken [gp[i]...] ≈ [p[i]...] atol=5e-13
45 end 112 end
46 end 113 end
47
48 @testset "getindex" begin
49 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
50 @test g[1,1] == (-1.0,0.0)
51 @test g[1,3] == (-1.0,7.11)
52 @test g[5,1] == (0.0,0.0)
53 @test g[5,3] == (0.0,7.11)
54
55 @test g[4,2] == (-0.25,7.11/2)
56
57 @test g[CartesianIndex(1,3)] == (-1.0,7.11)
58 end
59
60 @testset "evalOn" begin
61 g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
62
63 @test evalOn(g, (x,y) -> 0.) isa LazyArray
64 @test evalOn(g, (x,y) -> 0.) == fill(0., (5,3))
65
66 f(x,y) = sin(x)*cos(y)
67 @test evalOn(g, f) == map(p->f(p...), points(g))
68 end
69
70 @testset "restrict" begin
71 g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
72 @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)
73 @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0)
74
75 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
76 @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0)
77 @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0)
78 @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0)
79 @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0))
80 @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
81 @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0))
82 @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0))
83 end
84
85 @testset "boundary_identifiers" begin
86 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
87 bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(),
88 CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(),
89 CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}())
90 @test boundary_identifiers(g) == bids
91 @inferred boundary_identifiers(g)
92 end
93
94 @testset "boundary_grid" begin
95 @testset "1D" begin
96 g = EquidistantGrid(5,0.0,2.0)
97 (id_l, id_r) = boundary_identifiers(g)
98 @test boundary_grid(g,id_l) == EquidistantGrid{Float64}()
99 @test boundary_grid(g,id_r) == EquidistantGrid{Float64}()
100 @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}())
101 @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}())
102 end
103 @testset "2D" begin
104 g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
105 (id_w, id_e, id_s, id_n) = boundary_identifiers(g)
106 @test boundary_grid(g,id_w) == restrict(g,2)
107 @test boundary_grid(g,id_e) == restrict(g,2)
108 @test boundary_grid(g,id_s) == restrict(g,1)
109 @test boundary_grid(g,id_n) == restrict(g,1)
110 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
111 end
112 @testset "3D" begin
113 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
114 (id_w, id_e,
115 id_s, id_n,
116 id_t, id_b) = boundary_identifiers(g)
117 @test boundary_grid(g,id_w) == restrict(g,[2,3])
118 @test boundary_grid(g,id_e) == restrict(g,[2,3])
119 @test boundary_grid(g,id_s) == restrict(g,[1,3])
120 @test boundary_grid(g,id_n) == restrict(g,[1,3])
121 @test boundary_grid(g,id_t) == restrict(g,[1,2])
122 @test boundary_grid(g,id_b) == restrict(g,[1,2])
123 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
124 end
125 end
126
127 @testset "refine" begin
128 @test refine(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
129 @test refine(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
130
131 g = EquidistantGrid((10,5),(0.,1.),(2.,3.))
132 @test refine(g, 1) == g
133 @test refine(g, 2) == EquidistantGrid((19,9),(0.,1.),(2.,3.))
134 @test refine(g, 3) == EquidistantGrid((28,13),(0.,1.),(2.,3.))
135 end
136
137 @testset "coarsen" begin
138 @test coarsen(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
139 @test coarsen(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
140
141 g = EquidistantGrid((7,13),(0.,1.),(2.,3.))
142 @test coarsen(g, 1) == g
143 @test coarsen(g, 2) == EquidistantGrid((4,7),(0.,1.),(2.,3.))
144 @test coarsen(g, 3) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
145
146 @test_throws DomainError(4, "Size minus 1 must be divisible by the ratio.") coarsen(g, 4) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
147 end
148 end 114 end