comparison src/Grids/EquidistantGrid.jl @ 406:c377fc37c04b test/equidistantgrid

Clean up EquidistantGrid and tests after deciding that side lengths must be positive.
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Tue, 13 Oct 2020 17:16:28 +0200
parents 16dc5b19843d
children 26b0eb83aea4
comparison
equal deleted inserted replaced
405:16dc5b19843d 406:c377fc37c04b
1 # EquidistantGrid is a grid with equidistant grid spacing per coordinat 1 """
2 # direction. The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ 2 EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}
3 # by the exterior product of the vectors obtained by projecting (x̄₂-x̄₁) onto
4 # the coordinate directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2)
5 # the domain is defined as (-1,1)x(0,2).
6 3
7 export EquidistantGrid 4 EquidistantGrid is a grid with equidistant grid spacing per coordinat direction.
8 5 The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ by the exterior
6 product of the vectors obtained by projecting (x̄₂-x̄₁) onto the coordinate
7 directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2) the domain is defined
8 as (-1,1)x(0,2). The side lengths of the grid are not allowed to be negative
9 """
9 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid 10 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid
10 size::NTuple{Dim, Int} 11 size::NTuple{Dim, Int}
11 limit_lower::NTuple{Dim, T} 12 limit_lower::NTuple{Dim, T}
12 limit_upper::NTuple{Dim, T} 13 limit_upper::NTuple{Dim, T}
13 14
14 # General constructor 15 # General constructor
15 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T 16 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T
16 all(size.>0) || throw(DomainError("size must be postive")) 17 if any(size .<= 0)
17 # TODO: Is it reasonable to restrict side lengths to be positive? 18 throw(DomainError("all components of size must be postive"))
18 all(limit_upper.-limit_lower .> 0) || throw(DomainError("side lengths must be postive")) 19 end
20 if any(limit_upper.-limit_lower .<= 0)
21 throw(DomainError("all side lengths must be postive"))
22 end
19 return new{Dim,T}(size, limit_lower, limit_upper) 23 return new{Dim,T}(size, limit_lower, limit_upper)
20 end 24 end
21 end 25 end
26 export EquidistantGrid
22 27
28
29 """
30 EquidistantGrid(size::Int, limit_lower::T, limit_upper::T)
31
32 Convenience constructor for 1D grids.
33 """
23 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T 34 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T
24 return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) 35 return EquidistantGrid((size,),(limit_lower,),(limit_upper,))
25 end 36 end
26 37
27 function Base.eachindex(grid::EquidistantGrid) 38 function Base.eachindex(grid::EquidistantGrid)
28 CartesianIndices(grid.size) 39 CartesianIndices(grid.size)
29 end 40 end
30 41
31 Base.size(g::EquidistantGrid) = g.size 42 Base.size(g::EquidistantGrid) = g.size
32 43
33 # Returns the number of dimensions of an EquidistantGrid. 44 """
34 # 45 dimension(grid::EquidistantGrid)
35 # @Input: grid - an EquidistantGrid 46
36 # @Return: dimension - The dimension of the grid 47 The dimension of the grid.
48 """
37 function dimension(grid::EquidistantGrid) 49 function dimension(grid::EquidistantGrid)
38 return length(grid.size) 50 return length(grid.size)
39 end 51 end
40 52
41 53
42 """ 54 """
43 spacing(grid::EquidistantGrid) 55 spacing(grid::EquidistantGrid)
44 56
45 The spacing between the grid points of the grid. 57 The spacing between the grid points of the grid.
46 """ 58 """
47 # TODO: If we restrict side lenghts to be positive, then we should remove the abs here. 59 spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
48 spacing(grid::EquidistantGrid) = abs.(grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
49 export spacing 60 export spacing
50 61
51 """ 62 """
52 spacing(grid::EquidistantGrid) 63 inverse_spacing(grid::EquidistantGrid)
53 64
54 The reciprocal of the spacing between the grid points of the grid. 65 The reciprocal of the spacing between the grid points of the grid.
55 """ 66 """
56 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid) 67 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid)
57 export inverse_spacing 68 export inverse_spacing
58 69
59 # Computes the points of an EquidistantGrid as an array of tuples with 70 """
60 # the same dimension as the grid. 71 points(grid::EquidistantGrid)
61 # 72
62 # @Input: grid - an EquidistantGrid 73 The point of the grid as an array of tuples with the same dimension as the grid.
63 # @Return: points - the points of the grid. 74 The points are stored as [(x1,y1), (x1,y2), … (x1,yn);
64 # TODO: Does not work if side lengths are allowed to be negative. 75 (x2,y1), (x2,y2), … (x2,yn);
76 ⋮ ⋮ ⋮
77 (xm,y1), (xm,y2), … (xm,yn)]
78 """
65 function points(grid::EquidistantGrid) 79 function points(grid::EquidistantGrid)
66 # TODO: Make this return an abstract array?
67 indices = Tuple.(CartesianIndices(grid.size)) 80 indices = Tuple.(CartesianIndices(grid.size))
68 h = spacing(grid) 81 h = spacing(grid)
69 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) 82 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices)
70 end 83 end
71 84