comparison test/Grids/equidistant_grid_test.jl @ 1888:eb70a0941cd6 allocation_testing

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 03 Feb 2023 23:02:46 +0100
parents dfbd62c7eb09
children 9275d95e2d90 102ebdaf7c11
comparison
equal deleted inserted replaced
1887:24590890e124 1888:eb70a0941cd6
1 using Sbplib.Grids
2 using Test
3 using Sbplib.RegionIndices
4
5
6 @testset "EquidistantGrid" begin
7 @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid
8 @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid
9 # constuctor
10 @test_throws DomainError EquidistantGrid(0,0.0,1.0)
11 @test_throws DomainError EquidistantGrid(1,1.0,1.0)
12 @test_throws DomainError EquidistantGrid(1,1.0,-1.0)
13 @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,))
14
15 @testset "Base" begin
16 @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64
17 @test eltype(EquidistantGrid((4,3),(0,0),(1,3))) == Int
18 @test size(EquidistantGrid(4,0.0,1.0)) == (4,)
19 @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3)
20 @test ndims(EquidistantGrid(4,0.0,1.0)) == 1
21 @test ndims(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2
22 end
23
24 @testset "spacing" begin
25 @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13
26 @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13
27 end
28
29 @testset "inverse_spacing" begin
30 @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13
31 @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13
32 end
33
34 @testset "points" begin
35 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
36 gp = points(g);
37 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11);
38 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11);
39 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11);
40 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11);
41 (0.,0.) (0.,7.11/2) (0.,7.11)]
42 for i ∈ eachindex(gp)
43 @test [gp[i]...] ≈ [p[i]...] atol=5e-13
44 end
45 end
46
47 @testset "restrict" begin
48 g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
49 @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)
50 @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0)
51
52 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
53 @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0)
54 @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0)
55 @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0)
56 @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0))
57 @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
58 @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0))
59 @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0))
60 end
61
62 @testset "boundary_identifiers" begin
63 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
64 bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(),
65 CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(),
66 CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}())
67 @test boundary_identifiers(g) == bids
68 @inferred boundary_identifiers(g)
69 end
70
71 @testset "boundary_grid" begin
72 @testset "1D" begin
73 g = EquidistantGrid(5,0.0,2.0)
74 (id_l, id_r) = boundary_identifiers(g)
75 @test boundary_grid(g,id_l) == EquidistantGrid{Float64}()
76 @test boundary_grid(g,id_r) == EquidistantGrid{Float64}()
77 @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}())
78 @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}())
79 end
80 @testset "2D" begin
81 g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
82 (id_w, id_e, id_s, id_n) = boundary_identifiers(g)
83 @test boundary_grid(g,id_w) == restrict(g,2)
84 @test boundary_grid(g,id_e) == restrict(g,2)
85 @test boundary_grid(g,id_s) == restrict(g,1)
86 @test boundary_grid(g,id_n) == restrict(g,1)
87 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
88 end
89 @testset "3D" begin
90 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
91 (id_w, id_e,
92 id_s, id_n,
93 id_t, id_b) = boundary_identifiers(g)
94 @test boundary_grid(g,id_w) == restrict(g,[2,3])
95 @test boundary_grid(g,id_e) == restrict(g,[2,3])
96 @test boundary_grid(g,id_s) == restrict(g,[1,3])
97 @test boundary_grid(g,id_n) == restrict(g,[1,3])
98 @test boundary_grid(g,id_t) == restrict(g,[1,2])
99 @test boundary_grid(g,id_b) == restrict(g,[1,2])
100 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
101 end
102 end
103
104 @testset "refine" begin
105 @test refine(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
106 @test refine(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
107
108 g = EquidistantGrid((10,5),(0.,1.),(2.,3.))
109 @test refine(g, 1) == g
110 @test refine(g, 2) == EquidistantGrid((19,9),(0.,1.),(2.,3.))
111 @test refine(g, 3) == EquidistantGrid((28,13),(0.,1.),(2.,3.))
112 end
113
114 @testset "coarsen" begin
115 @test coarsen(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
116 @test coarsen(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
117
118 g = EquidistantGrid((7,13),(0.,1.),(2.,3.))
119 @test coarsen(g, 1) == g
120 @test coarsen(g, 2) == EquidistantGrid((4,7),(0.,1.),(2.,3.))
121 @test coarsen(g, 3) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
122
123 @test_throws DomainError(4, "Size minus 1 must be divisible by the ratio.") coarsen(g, 4) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
124 end
125 end