changeset 409:b4e65cb18423

Merge test/equidistantgrid
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Tue, 13 Oct 2020 18:33:43 +0200
parents 48d57f185f86 (current diff) 8fc429a22c8d (diff)
children 26e186b565b3
files
diffstat 2 files changed, 70 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl	Wed Oct 07 12:33:19 2020 +0200
+++ b/src/Grids/EquidistantGrid.jl	Tue Oct 13 18:33:43 2020 +0200
@@ -1,11 +1,12 @@
-# EquidistantGrid is a grid with equidistant grid spacing per coordinat
-# direction. The domain is defined through the two points P1 = x̄₁, P2 = x̄₂
-# by the exterior product of the vectors obtained by projecting (x̄₂-x̄₁) onto
-# the coordinate directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2)
-# the domain is defined as (-1,1)x(0,2).
+"""
+    EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}
 
-export EquidistantGrid
-
+EquidistantGrid is a grid with equidistant grid spacing per coordinat direction.
+The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ by the exterior
+product of the vectors obtained by projecting (x̄₂-x̄₁) onto the coordinate
+directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2) the domain is defined
+as (-1,1)x(0,2). The side lengths of the grid are not allowed to be negative
+"""
 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid
     size::NTuple{Dim, Int}
     limit_lower::NTuple{Dim, T}
@@ -13,12 +14,23 @@
 
     # General constructor
     function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T
-        @assert all(size.>0)
-        @assert all(limit_upper.-limit_lower .!= 0)
+        if any(size .<= 0)
+            throw(DomainError("all components of size must be postive"))
+        end
+        if any(limit_upper.-limit_lower .<= 0)
+            throw(DomainError("all side lengths must be postive"))
+        end
         return new{Dim,T}(size, limit_lower, limit_upper)
     end
 end
+export EquidistantGrid
 
+
+"""
+    EquidistantGrid(size::Int, limit_lower::T, limit_upper::T)
+
+Convenience constructor for 1D grids.
+"""
 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T
 	return EquidistantGrid((size,),(limit_lower,),(limit_upper,))
 end
@@ -29,10 +41,11 @@
 
 Base.size(g::EquidistantGrid) = g.size
 
-# Returns the number of dimensions of an EquidistantGrid.
-#
-# @Input: grid - an EquidistantGrid
-# @Return: dimension - The dimension of the grid
+"""
+    dimension(grid::EquidistantGrid)
+
+The dimension of the grid.
+"""
 function dimension(grid::EquidistantGrid)
     return length(grid.size)
 end
@@ -43,25 +56,27 @@
 
 The spacing between the grid points of the grid.
 """
-spacing(grid::EquidistantGrid) = abs.(grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
-# TODO: Evaluate if divisions affect performance
+spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
 export spacing
 
 """
-    spacing(grid::EquidistantGrid)
+    inverse_spacing(grid::EquidistantGrid)
 
 The reciprocal of the spacing between the grid points of the grid.
 """
 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid)
 export inverse_spacing
 
-# Computes the points of an EquidistantGrid as an array of tuples with
-# the same dimension as the grid.
-#
-# @Input: grid - an EquidistantGrid
-# @Return: points - the points of the grid.
+"""
+    points(grid::EquidistantGrid)
+
+The point of the grid as an array of tuples with the same dimension as the grid.
+The points are stored as [(x1,y1), (x1,y2), … (x1,yn);
+						  (x2,y1), (x2,y2), … (x2,yn);
+						  	⋮		 ⋮            ⋮
+						  (xm,y1), (xm,y2), … (xm,yn)]
+"""
 function points(grid::EquidistantGrid)
-    # TODO: Make this return an abstract array?
     indices = Tuple.(CartesianIndices(grid.size))
     h = spacing(grid)
     return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices)
--- a/test/testGrids.jl	Wed Oct 07 12:33:19 2020 +0200
+++ b/test/testGrids.jl	Tue Oct 13 18:33:43 2020 +0200
@@ -6,9 +6,41 @@
 @testset "EquidistantGrid" begin
     @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid
     @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid
-    @test dimension(EquidistantGrid(4,0.0,1.0)) == 1
+    # 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,))
 
+    # size
+    @test size(EquidistantGrid(4,0.0,1.0)) == (4,)
+    @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3)
+
+    # dimension
+    @test dimension(EquidistantGrid(4,0.0,1.0)) == 1
+    @test dimension(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2
+
+    # spacing
+    @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
+
+    # inverse_spacing
+    @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
+
+    # points
+    g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
+    gp = points(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);
+         (-0.25,0.)    (-0.25,7.11/2) (-0.25,7.11);
+         (0.,0.)       (0.,7.11/2)    (0.,7.11)]
+    for i ∈ eachindex(gp)
+        @test [gp[i]...] ≈ [p[i]...] atol=5e-13
+    end
+
+    # restrict
     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)