comparison test/Grids/equidistant_grid_test.jl @ 1395:bdcdbd4ea9cd feature/boundary_conditions

Merge with default. Comment out broken tests for boundary_conditions at sat
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 26 Jul 2023 21:35:50 +0200
parents 4684c7f1c4cb
children 447833be2ecc
comparison
equal deleted inserted replaced
1217:ea2e8254820a 1395:bdcdbd4ea9cd
1 using Sbplib.Grids 1 using Sbplib.Grids
2 using Test 2 using Test
3 using Sbplib.RegionIndices 3 using Sbplib.RegionIndices
4 using Sbplib.LazyTensors
4 5
5 6
6 @testset "EquidistantGrid" begin 7 @testset "EquidistantGrid" begin
7 @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid 8 @test EquidistantGrid(0:0.1:10) isa EquidistantGrid
8 @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid 9 @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid
9 # constuctor 10 @test EquidistantGrid(LinRange(0,1,11)) isa EquidistantGrid
10 @test_throws DomainError EquidistantGrid(0,0.0,1.0) 11
11 @test_throws DomainError EquidistantGrid(1,1.0,1.0) 12 @testset "Indexing Interface" begin
12 @test_throws DomainError EquidistantGrid(1,1.0,-1.0) 13 g = EquidistantGrid(0:0.1:10)
13 @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,)) 14 @test g[1] == 0.0
15 @test g[5] == 0.4
16 @test g[101] == 10.0
17
18 @test g[begin] == 0.0
19 @test g[end] == 10.0
20
21 @test all(eachindex(g) .== 1:101)
22 end
23
24 @testset "Iterator interface" begin
25 @test eltype(EquidistantGrid(0:10)) == Int
26 @test eltype(EquidistantGrid(0:2:10)) == Int
27 @test eltype(EquidistantGrid(0:0.1:10)) == Float64
28 @test size(EquidistantGrid(0:10)) == (11,)
29 @test size(EquidistantGrid(0:0.1:10)) == (101,)
30
31 @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
32
33 @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}()
34 end
14 35
15 @testset "Base" begin 36 @testset "Base" begin
16 @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64 37 @test ndims(EquidistantGrid(0:10)) == 1
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 38 end
23 39
24 @testset "spacing" begin 40 @testset "spacing" begin
25 @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13 41 @test spacing(EquidistantGrid(0:10)) == 1
26 @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13 42 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1
27 end 43 end
28 44
29 @testset "inverse_spacing" begin 45 @testset "inverse_spacing" begin
30 @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13 46 @test inverse_spacing(EquidistantGrid(0:10)) == 1
31 @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13 47 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10
32 end 48 end
33 49
34 @testset "points" begin 50 @testset "boundary_identifiers" begin
35 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) 51 g = EquidistantGrid(0:0.1:10)
36 gp = points(g); 52 @test boundary_identifiers(g) == (Lower(), Upper())
53 @inferred boundary_identifiers(g)
54 end
55
56 @testset "boundary_grid" begin
57 g = EquidistantGrid(0:0.1:1)
58 @test boundary_grid(g, Lower()) == ZeroDimGrid(0.0)
59 @test boundary_grid(g, Upper()) == ZeroDimGrid(1.0)
60 end
61
62 @testset "refine" begin
63 g = EquidistantGrid(0:0.1:1)
64 @test refine(g, 1) == g
65 @test refine(g, 2) == EquidistantGrid(0:0.05:1)
66 @test refine(g, 3) == EquidistantGrid(0:(0.1/3):1)
67 end
68
69 @testset "coarsen" begin
70 g = EquidistantGrid(0:1:10)
71 @test coarsen(g, 1) == g
72 @test coarsen(g, 2) == EquidistantGrid(0:2:10)
73
74 g = EquidistantGrid(0:0.1:1)
75 @test coarsen(g, 1) == g
76 @test coarsen(g, 2) == EquidistantGrid(0:0.2:1)
77
78 g = EquidistantGrid(0:10)
79 @test coarsen(g, 1) == EquidistantGrid(0:1:10)
80 @test coarsen(g, 2) == EquidistantGrid(0:2:10)
81
82 @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(g, 3)
83 end
84 end
85
86
87 @testset "equidistant_grid" begin
88 @test equidistant_grid(4,0.0,1.0) isa EquidistantGrid
89 @test equidistant_grid((4,3),(0.0,0.0),(8.0,5.0)) isa TensorGrid
90
91 # constuctor
92 @test_throws DomainError equidistant_grid(0,0.0,1.0)
93 @test_throws DomainError equidistant_grid(1,1.0,1.0)
94 @test_throws DomainError equidistant_grid(1,1.0,-1.0)
95
96 @test_throws DomainError equidistant_grid((0,0),(0.0,0.0),(1.0,1.0))
97 @test_throws DomainError equidistant_grid((1,1),(1.0,1.0),(1.0,1.0))
98 @test_throws DomainError equidistant_grid((1,1),(1.0,1.0),(-1.0,-1.0))
99
100 @testset "Base" begin
101 @test eltype(equidistant_grid(4,0.0,1.0)) == Float64
102 @test eltype(equidistant_grid((4,3),(0,0),(1,3))) <: AbstractVector{Float64}
103 @test size(equidistant_grid(4,0.0,1.0)) == (4,)
104 @test size(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3)
105 @test ndims(equidistant_grid(4,0.0,1.0)) == 1
106 @test ndims(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == 2
107 end
108
109 @testset "getindex" begin
110 g = equidistant_grid((5,3), (-1.0,0.0), (0.0,7.11))
111 gp = collect(g);
37 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); 112 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11);
38 (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); 113 (-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); 114 (-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); 115 (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11);
41 (0.,0.) (0.,7.11/2) (0.,7.11)] 116 (0.,0.) (0.,7.11/2) (0.,7.11)]
42 for i ∈ eachindex(gp) 117 for i ∈ eachindex(gp)
43 @test [gp[i]...] ≈ [p[i]...] atol=5e-13 118 @test [gp[i]...] ≈ [p[i]...] atol=5e-13
44 end 119 end
45 end 120 end
121 end
46 122
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 123
52 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) 124 @testset "change_length" begin
53 @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0) 125 @test Grids.change_length(0:20, 21) == 0:20
54 @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0) 126 @test Grids.change_length(0:20, 11) == 0:2:20
55 @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0) 127 @test Grids.change_length(0:2:20, 21) == 0:20
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 128
62 @testset "boundary_identifiers" begin 129 @test Grids.change_length(range(0,1,length=10), 10) == range(0,1,length=10)
63 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) 130 @test Grids.change_length(range(0,1,length=10), 5) == range(0,1,length=5)
64 bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(), 131 @test Grids.change_length(range(0,1,length=10), 20) == range(0,1,length=20)
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 132
71 @testset "boundary_grid" begin 133 @test Grids.change_length(LinRange(1,2,10),10) == LinRange(1,2,10)
72 @testset "1D" begin 134 @test Grids.change_length(LinRange(1,2,10),15) == LinRange(1,2,15)
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 135 end