comparison Grids/src/EquidistantGrid.jl @ 231:fbabfd4e8f20

Merge in boundary_conditions
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 26 Jun 2019 15:07:47 +0200
parents 235f0a771c8f
children 1128ab4f5758
comparison
equal deleted inserted replaced
144:ce56727e4232 231:fbabfd4e8f20
1 # EquidistantGrid is a grid with equidistant grid spacing per coordinat
2 # direction. The domain is defined through the two points P1 = x̄₁, P2 = x̄₂
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
7 export EquidistantGrid
8
9 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid
10 size::NTuple{Dim, Int} # First coordinate direction stored first
11 limit_lower::NTuple{Dim, T}
12 limit_upper::NTuple{Dim, T}
13 inverse_spacing::NTuple{Dim, T} # The reciprocal of the grid spacing
14
15 # General constructor
16 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)
18 @assert all(limit_upper.-limit_lower .!= 0)
19 inverse_spacing = (size.-1)./abs.(limit_upper.-limit_lower)
20 return new{Dim,T}(size, limit_lower, limit_upper, inverse_spacing)
21 end
22 end
23
24 function Base.eachindex(grid::EquidistantGrid)
25 CartesianIndices(grid.size)
26 end
27
28 # Returns the number of dimensions of an EquidistantGrid.
29 #
30 # @Input: grid - an EquidistantGrid
31 # @Return: dimension - The dimension of the grid
32 function dimension(grid::EquidistantGrid)
33 return length(grid.size)
34 end
35
36 # Returns the spacing of the grid
37 #
38 function spacing(grid::EquidistantGrid)
39 return 1.0./grid.inverse_spacing
40 end
41
42 # Computes the points of an EquidistantGrid as an array of tuples with
43 # the same dimension as the grid.
44 #
45 # @Input: grid - an EquidistantGrid
46 # @Return: points - the points of the grid.
47 function points(grid::EquidistantGrid)
48 # TODO: Make this return an abstract array?
49 indices = Tuple.(CartesianIndices(grid.size))
50 h = spacing(grid)
51 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices)
52 end
53
54 function pointsalongdim(grid::EquidistantGrid, dim::Integer)
55 @assert dim<=dimension(grid)
56 @assert dim>0
57 points = collect(range(grid.limit_lower[dim],stop=grid.limit_upper[dim],length=grid.size[dim]))
58 end