diff 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
line wrap: on
line diff
--- a/test/Grids/equidistant_grid_test.jl	Sat May 20 14:26:36 2023 +0200
+++ b/test/Grids/equidistant_grid_test.jl	Sun May 21 21:55:14 2023 +0200
@@ -1,51 +1,114 @@
 using Sbplib.Grids
 using Test
 using Sbplib.RegionIndices
+using Sbplib.LazyTensors
 
 
 @testset "EquidistantGrid" begin
-    @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid
-    @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid
-    # constuctor
-    @test_throws DomainError EquidistantGrid(0,0.0,1.0)
-    @test_throws DomainError EquidistantGrid(1,1.0,1.0)
-    @test_throws DomainError EquidistantGrid(1,1.0,-1.0)
-    @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,))
+    @test EquidistantGrid(0:0.1:10) isa EquidistantGrid
+    @test EquidistantGrid(range(0,1,length=10)) isa EquidistantGrid
+    @test EquidistantGrid(LinRange(0,1,11)) isa EquidistantGrid
+
+    @testset "Indexing Interface" begin
+        g = EquidistantGrid(0:0.1:10)
+        @test g[1] == 0.0
+        @test g[5] == 0.4
+        @test g[101] == 10.0
+
+        @test g[begin] == 0.0
+        @test g[end] == 10.0
+
+        @test all(eachindex(g) .== 1:101)
+    end
+
+    @testset "Iterator interface" begin
+        @test eltype(EquidistantGrid(0:10)) == Int
+        @test eltype(EquidistantGrid(0:2:10)) == Int
+        @test eltype(EquidistantGrid(0:0.1:10)) == Float64
+        @test size(EquidistantGrid(0:10)) == (11,)
+        @test size(EquidistantGrid(0:0.1:10)) == (101,)
+
+        @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
+
+        @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}()
+    end
 
     @testset "Base" begin
-        @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64
-        @test eltype(EquidistantGrid((4,3),(0,0),(1,3))) == Int
-        @test size(EquidistantGrid(4,0.0,1.0)) == (4,)
-        @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3)
-        @test ndims(EquidistantGrid(4,0.0,1.0)) == 1
-        @test ndims(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2
+        @test ndims(EquidistantGrid(0:10)) == 1
+    end
+
+    @testset "spacing" begin
+        @test spacing(EquidistantGrid(0:10)) == 1
+        @test spacing(EquidistantGrid(0:0.1:10)) == 0.1
+    end
+
+    @testset "inverse_spacing" begin
+        @test inverse_spacing(EquidistantGrid(0:10)) == 1
+        @test inverse_spacing(EquidistantGrid(0:0.1:10)) == 10
+    end
+
+    @testset "boundary_identifiers" begin
+        g = EquidistantGrid(0:0.1:10)
+        @test boundary_identifiers(g) == (Lower(), Upper())
+        @inferred boundary_identifiers(g)
+    end
+
+    @testset "boundary_grid" begin
+        g = EquidistantGrid(0:0.1:1)
+        @test boundary_grid(g, Lower()) == ZeroDimGrid(0.0)
+        @test boundary_grid(g, Upper()) == ZeroDimGrid(1.0)
+    end
+
+    @testset "refine" begin
+        g = EquidistantGrid(0:0.1:1)
+        @test refine(g, 1) == g
+        @test refine(g, 2) == EquidistantGrid(0:0.05:1)
+        @test refine(g, 3) == EquidistantGrid(0:(0.1/3):1)
+    end
+
+    @testset "coarsen" begin
+        g = EquidistantGrid(0:1:10)
+        @test coarsen(g, 1) == g
+        @test coarsen(g, 2) == EquidistantGrid(0:2:10)
+
+        g = EquidistantGrid(0:0.1:1)
+        @test coarsen(g, 1) == g
+        @test coarsen(g, 2) == EquidistantGrid(0:0.2:1)
+
+        g = EquidistantGrid(0:10)
+        @test coarsen(g, 1) == EquidistantGrid(0:1:10)
+        @test coarsen(g, 2) == EquidistantGrid(0:2:10)
+
+        @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(g, 3)
+    end
+end
+
+
+@testset "equidistant_grid" begin
+    @test equidistant_grid(4,0.0,1.0) isa EquidistantGrid
+    @test equidistant_grid((4,3),(0.0,0.0),(8.0,5.0)) isa TensorGrid
+
+    # constuctor
+    @test_throws DomainError equidistant_grid(0,0.0,1.0)
+    @test_throws DomainError equidistant_grid(1,1.0,1.0)
+    @test_throws DomainError equidistant_grid(1,1.0,-1.0)
+
+    @test_throws DomainError equidistant_grid((0,0),(0.0,0.0),(1.0,1.0))
+    @test_throws DomainError equidistant_grid((1,1),(1.0,1.0),(1.0,1.0))
+    @test_throws DomainError equidistant_grid((1,1),(1.0,1.0),(-1.0,-1.0))
+
+    @testset "Base" begin
+        @test eltype(equidistant_grid(4,0.0,1.0)) == Float64
+        @test eltype(equidistant_grid((4,3),(0,0),(1,3))) <: AbstractVector{Float64}
+        @test size(equidistant_grid(4,0.0,1.0)) == (4,)
+        @test size(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3)
+        @test ndims(equidistant_grid(4,0.0,1.0)) == 1
+        @test ndims(equidistant_grid((5,3), (0.0,0.0), (2.0,1.0))) == 2
     end
 
     @testset "getindex" begin
-        g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
-        @test g[1,1] == (-1.0,0.0)
-        @test g[1,3] == (-1.0,7.11)
-        @test g[5,1] == (0.0,0.0)
-        @test g[5,3] == (0.0,7.11)
-
-        @test g[4,2] == (-0.25,7.11/2)
-
-        @test g[CartesianIndex(1,3)] == (-1.0,7.11)
-    end
-
-    @testset "spacing" begin
-        @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13
-        @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13
-    end
-
-    @testset "inverse_spacing" begin
-        @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13
-        @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13
-    end
-
-    @testset "points" begin
-        g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
-        gp = points(g);
+        g = equidistant_grid((5,3), (-1.0,0.0), (0.0,7.11))
+        gp = collect(g);
         p = [(-1.,0.)      (-1.,7.11/2)   (-1.,7.11);
             (-0.75,0.)    (-0.75,7.11/2) (-0.75,7.11);
             (-0.5,0.)     (-0.5,7.11/2)  (-0.5,7.11);
@@ -55,83 +118,18 @@
             @test [gp[i]...] ≈ [p[i]...] atol=5e-13
         end
     end
+end
 
-    @testset "restrict" begin
-        g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
-        @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)
-        @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0)
 
-        g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
-        @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0)
-        @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0)
-        @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0)
-        @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0))
-        @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
-        @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0))
-        @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0))
-    end
-
-    @testset "boundary_identifiers" begin
-        g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
-        bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(),
-                CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(),
-                CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}())
-        @test boundary_identifiers(g) == bids
-        @inferred boundary_identifiers(g)
-    end
+@testset "change_length" begin
+    @test Grids.change_length(0:20, 21) == 0:20
+    @test Grids.change_length(0:20, 11) == 0:2:20
+    @test Grids.change_length(0:2:20, 21) == 0:20
 
-    @testset "boundary_grid" begin
-            @testset "1D" begin
-                g = EquidistantGrid(5,0.0,2.0)
-                (id_l, id_r) = boundary_identifiers(g)
-                @test boundary_grid(g,id_l) == EquidistantGrid{Float64}()
-                @test boundary_grid(g,id_r) == EquidistantGrid{Float64}()
-                @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}())
-                @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}())
-            end
-            @testset "2D" begin
-                g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
-                (id_w, id_e, id_s, id_n) = boundary_identifiers(g)
-                @test boundary_grid(g,id_w) == restrict(g,2)
-                @test boundary_grid(g,id_e) == restrict(g,2)
-                @test boundary_grid(g,id_s) == restrict(g,1)
-                @test boundary_grid(g,id_n) == restrict(g,1)
-                @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
-            end
-            @testset "3D" begin
-                g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
-                (id_w, id_e,
-                 id_s, id_n,
-                 id_t, id_b) = boundary_identifiers(g)
-                @test boundary_grid(g,id_w) == restrict(g,[2,3])
-                @test boundary_grid(g,id_e) == restrict(g,[2,3])
-                @test boundary_grid(g,id_s) == restrict(g,[1,3])
-                @test boundary_grid(g,id_n) == restrict(g,[1,3])
-                @test boundary_grid(g,id_t) == restrict(g,[1,2])
-                @test boundary_grid(g,id_b) == restrict(g,[1,2])
-                @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
-            end
-    end
+    @test Grids.change_length(range(0,1,length=10), 10) == range(0,1,length=10)
+    @test Grids.change_length(range(0,1,length=10), 5) == range(0,1,length=5)
+    @test Grids.change_length(range(0,1,length=10), 20) == range(0,1,length=20)
 
-    @testset "refine" begin
-        @test refine(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
-        @test refine(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
-
-        g = EquidistantGrid((10,5),(0.,1.),(2.,3.))
-        @test refine(g, 1) == g
-        @test refine(g, 2) == EquidistantGrid((19,9),(0.,1.),(2.,3.))
-        @test refine(g, 3) == EquidistantGrid((28,13),(0.,1.),(2.,3.))
-    end
-
-    @testset "coarsen" begin
-        @test coarsen(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
-        @test coarsen(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
-
-        g = EquidistantGrid((7,13),(0.,1.),(2.,3.))
-        @test coarsen(g, 1) == g
-        @test coarsen(g, 2) == EquidistantGrid((4,7),(0.,1.),(2.,3.))
-        @test coarsen(g, 3) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
-
-        @test_throws DomainError(4, "Size minus 1 must be divisible by the ratio.") coarsen(g, 4) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
-    end
+    @test Grids.change_length(LinRange(1,2,10),10) == LinRange(1,2,10)
+    @test Grids.change_length(LinRange(1,2,10),15) == LinRange(1,2,15)
 end