comparison src/Grids/EquidistantGrid.jl @ 381:dacbcba33d7d

Refactor EquidistantGrid to not store spacing or inverse spacing
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 30 Sep 2020 21:49:48 +0200
parents 64ad8ec0eae0
children 16dc5b19843d
comparison
equal deleted inserted replaced
380:81053b1992b6 381:dacbcba33d7d
5 # the domain is defined as (-1,1)x(0,2). 5 # the domain is defined as (-1,1)x(0,2).
6 6
7 export EquidistantGrid 7 export EquidistantGrid
8 8
9 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid 9 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid
10 size::NTuple{Dim, Int} # First coordinate direction stored first 10 size::NTuple{Dim, Int}
11 limit_lower::NTuple{Dim, T} 11 limit_lower::NTuple{Dim, T}
12 limit_upper::NTuple{Dim, T} 12 limit_upper::NTuple{Dim, T}
13 inverse_spacing::NTuple{Dim, T} # Reciprocal of grid spacing
14 13
15 # General constructor 14 # General constructor
16 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T 15 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T
17 @assert all(size.>0) 16 @assert all(size.>0)
18 @assert all(limit_upper.-limit_lower .!= 0) 17 @assert all(limit_upper.-limit_lower .!= 0)
19 inverse_spacing = (size.-1)./ abs.(limit_upper.-limit_lower) 18 return new{Dim,T}(size, limit_lower, limit_upper)
20 return new{Dim,T}(size, limit_lower, limit_upper, inverse_spacing)
21 end 19 end
22 end 20 end
23 21
24 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T 22 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T
25 return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) 23 return EquidistantGrid((size,),(limit_lower,),(limit_upper,))
37 # @Return: dimension - The dimension of the grid 35 # @Return: dimension - The dimension of the grid
38 function dimension(grid::EquidistantGrid) 36 function dimension(grid::EquidistantGrid)
39 return length(grid.size) 37 return length(grid.size)
40 end 38 end
41 39
42 # Returns the reciprocal of the spacing of the grid 40
43 # 41 """
44 function inverse_spacing(grid::EquidistantGrid) 42 spacing(grid::EquidistantGrid)
45 return grid.inverse_spacing 43
46 end 44 The spacing between the grid points of the grid.
45 """
46 spacing(grid::EquidistantGrid) = abs.(grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
47 # TODO: Evaluate if divisions affect performance
48 export spacing
49
50 """
51 spacing(grid::EquidistantGrid)
52
53 The reciprocal of the spacing between the grid points of the grid.
54 """
55 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid)
47 export inverse_spacing 56 export inverse_spacing
48
49 # Returns the reciprocal of the spacing of the grid
50 #
51 # TODO: Evaluate if divisions affect performance
52 function spacing(grid::EquidistantGrid)
53 return 1.0./grid.inverse_spacing
54 end
55 export spacing
56 57
57 # Computes the points of an EquidistantGrid as an array of tuples with 58 # Computes the points of an EquidistantGrid as an array of tuples with
58 # the same dimension as the grid. 59 # the same dimension as the grid.
59 # 60 #
60 # @Input: grid - an EquidistantGrid 61 # @Input: grid - an EquidistantGrid
77 limit_upper = grid.limit_upper[dim] 78 limit_upper = grid.limit_upper[dim]
78 79
79 return EquidistantGrid(size, limit_lower, limit_upper) 80 return EquidistantGrid(size, limit_lower, limit_upper)
80 end 81 end
81 export restrict 82 export restrict
82
83 function pointsalongdim(grid::EquidistantGrid, dim::Integer)
84 @assert dim<=dimension(grid)
85 @assert dim>0
86 points = collect(range(grid.limit_lower[dim],stop=grid.limit_upper[dim],length=grid.size[dim]))
87 end