Mercurial > repos > public > sbplib_julia
comparison test/Grids/equidistant_grid_test.jl @ 1365:4684c7f1c4cb feature/variable_derivatives
Merge with default
| author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
|---|---|
| date | Sun, 21 May 2023 21:55:14 +0200 |
| parents | 102ebdaf7c11 11b08b242e48 |
| children | 447833be2ecc |
comparison
equal
deleted
inserted
replaced
| 1358:e7861cfb6ede | 1365:4684c7f1c4cb |
|---|---|
| 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 | 38 end |
| 18 @test size(EquidistantGrid(4,0.0,1.0)) == (4,) | 39 |
| 19 @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) | 40 @testset "spacing" begin |
| 20 @test ndims(EquidistantGrid(4,0.0,1.0)) == 1 | 41 @test spacing(EquidistantGrid(0:10)) == 1 |
| 21 @test ndims(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2 | 42 @test spacing(EquidistantGrid(0:0.1:10)) == 0.1 |
| 43 end | |
| 44 | |
| 45 @testset "inverse_spacing" begin | |
| 46 @test inverse_spacing(EquidistantGrid(0:10)) == 1 | |
| 47 @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10 | |
| 48 end | |
| 49 | |
| 50 @testset "boundary_identifiers" begin | |
| 51 g = EquidistantGrid(0:0.1:10) | |
| 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 | |
| 22 end | 107 end |
| 23 | 108 |
| 24 @testset "getindex" begin | 109 @testset "getindex" begin |
| 25 g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) | 110 g = equidistant_grid((5,3), (-1.0,0.0), (0.0,7.11)) |
| 26 @test g[1,1] == (-1.0,0.0) | 111 gp = collect(g); |
| 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); | 112 p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); |
| 50 (-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); |
| 51 (-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); |
| 52 (-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); |
| 53 (0.,0.) (0.,7.11/2) (0.,7.11)] | 116 (0.,0.) (0.,7.11/2) (0.,7.11)] |
| 54 for i ∈ eachindex(gp) | 117 for i ∈ eachindex(gp) |
| 55 @test [gp[i]...] ≈ [p[i]...] atol=5e-13 | 118 @test [gp[i]...] ≈ [p[i]...] atol=5e-13 |
| 56 end | 119 end |
| 57 end | 120 end |
| 121 end | |
| 58 | 122 |
| 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 | 123 |
| 64 g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) | 124 @testset "change_length" begin |
| 65 @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0) | 125 @test Grids.change_length(0:20, 21) == 0:20 |
| 66 @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0) | 126 @test Grids.change_length(0:20, 11) == 0:2:20 |
| 67 @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0) | 127 @test Grids.change_length(0:2:20, 21) == 0:20 |
| 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 | 128 |
| 74 @testset "boundary_identifiers" begin | 129 @test Grids.change_length(range(0,1,length=10), 10) == range(0,1,length=10) |
| 75 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) |
| 76 bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(), | 131 @test Grids.change_length(range(0,1,length=10), 20) == range(0,1,length=20) |
| 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 | 132 |
| 83 @testset "boundary_grid" begin | 133 @test Grids.change_length(LinRange(1,2,10),10) == LinRange(1,2,10) |
| 84 @testset "1D" begin | 134 @test Grids.change_length(LinRange(1,2,10),15) == LinRange(1,2,15) |
| 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 | 135 end |
