comparison test/Grids/equidistant_grid_test.jl @ 1355:102ebdaf7c11 feature/variable_derivatives

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 08 Feb 2023 21:21:28 +0100
parents test/Grids/EquidistantGrid_test.jl@c4ea28d904f5 test/Grids/EquidistantGrid_test.jl@dfbd62c7eb09
children 4684c7f1c4cb
comparison
equal deleted inserted replaced
1210:fa0800591306 1355:102ebdaf7c11
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 "getindex" begin
25 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
26 @test g[1,1] == (-1.0,0.0)
27 @test g[1,3] == (-1.0,7.11)
28 @test g[5,1] == (0.0,0.0)
29 @test g[5,3] == (0.0,7.11)
30
31 @test g[4,2] == (-0.25,7.11/2)
32
33 @test g[CartesianIndex(1,3)] == (-1.0,7.11)
34 end
35
36 @testset "spacing" begin
37 @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13
38 @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13
39 end
40
41 @testset "inverse_spacing" begin
42 @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13
43 @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13
44 end
45
46 @testset "points" begin
47 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
48 gp = points(g);
49 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11);
50 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11);
51 (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11);
52 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11);
53 (0.,0.) (0.,7.11/2) (0.,7.11)]
54 for i ∈ eachindex(gp)
55 @test [gp[i]...] ≈ [p[i]...] atol=5e-13
56 end
57 end
58
59 @testset "restrict" begin
60 g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
61 @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)
62 @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0)
63
64 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
65 @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0)
66 @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0)
67 @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0)
68 @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0))
69 @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
70 @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0))
71 @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0))
72 end
73
74 @testset "boundary_identifiers" begin
75 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
76 bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(),
77 CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(),
78 CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}())
79 @test boundary_identifiers(g) == bids
80 @inferred boundary_identifiers(g)
81 end
82
83 @testset "boundary_grid" begin
84 @testset "1D" begin
85 g = EquidistantGrid(5,0.0,2.0)
86 (id_l, id_r) = boundary_identifiers(g)
87 @test boundary_grid(g,id_l) == EquidistantGrid{Float64}()
88 @test boundary_grid(g,id_r) == EquidistantGrid{Float64}()
89 @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}())
90 @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}())
91 end
92 @testset "2D" begin
93 g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
94 (id_w, id_e, id_s, id_n) = boundary_identifiers(g)
95 @test boundary_grid(g,id_w) == restrict(g,2)
96 @test boundary_grid(g,id_e) == restrict(g,2)
97 @test boundary_grid(g,id_s) == restrict(g,1)
98 @test boundary_grid(g,id_n) == restrict(g,1)
99 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
100 end
101 @testset "3D" begin
102 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
103 (id_w, id_e,
104 id_s, id_n,
105 id_t, id_b) = boundary_identifiers(g)
106 @test boundary_grid(g,id_w) == restrict(g,[2,3])
107 @test boundary_grid(g,id_e) == restrict(g,[2,3])
108 @test boundary_grid(g,id_s) == restrict(g,[1,3])
109 @test boundary_grid(g,id_n) == restrict(g,[1,3])
110 @test boundary_grid(g,id_t) == restrict(g,[1,2])
111 @test boundary_grid(g,id_b) == restrict(g,[1,2])
112 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
113 end
114 end
115
116 @testset "refine" begin
117 @test refine(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
118 @test refine(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
119
120 g = EquidistantGrid((10,5),(0.,1.),(2.,3.))
121 @test refine(g, 1) == g
122 @test refine(g, 2) == EquidistantGrid((19,9),(0.,1.),(2.,3.))
123 @test refine(g, 3) == EquidistantGrid((28,13),(0.,1.),(2.,3.))
124 end
125
126 @testset "coarsen" begin
127 @test coarsen(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
128 @test coarsen(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
129
130 g = EquidistantGrid((7,13),(0.,1.),(2.,3.))
131 @test coarsen(g, 1) == g
132 @test coarsen(g, 2) == EquidistantGrid((4,7),(0.,1.),(2.,3.))
133 @test coarsen(g, 3) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
134
135 @test_throws DomainError(4, "Size minus 1 must be divisible by the ratio.") coarsen(g, 4) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
136 end
137 end