comparison Grids/src/EquidistantGrid.jl @ 211:1ad91e11b1f4 package_refactor

Move DiffOps and Grids into packages
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 26 Jun 2019 10:44:20 +0200
parents EquidistantGrid.jl@99308f68e548
children 235f0a771c8f
comparison
equal deleted inserted replaced
210:2aa33d0eef90 211:1ad91e11b1f4
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 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid
8 size::NTuple{Dim, Int} # First coordinate direction stored first
9 limit_lower::NTuple{Dim, T}
10 limit_upper::NTuple{Dim, T}
11 inverse_spacing::NTuple{Dim, T} # The reciprocal of the grid spacing
12
13 # General constructor
14 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T
15 @assert all(size.>0)
16 @assert all(limit_upper.-limit_lower .!= 0)
17 inverse_spacing = (size.-1)./abs.(limit_upper.-limit_lower)
18 return new{Dim,T}(size, limit_lower, limit_upper, inverse_spacing)
19 end
20 end
21
22 function Base.eachindex(grid::EquidistantGrid)
23 CartesianIndices(grid.size)
24 end
25
26 # Returns the number of dimensions of an EquidistantGrid.
27 #
28 # @Input: grid - an EquidistantGrid
29 # @Return: dimension - The dimension of the grid
30 function dimension(grid::EquidistantGrid)
31 return length(grid.size)
32 end
33
34 # Returns the spacing of the grid
35 #
36 function spacing(grid::EquidistantGrid)
37 return 1.0./grid.inverse_spacing
38 end
39
40 # Computes the points of an EquidistantGrid as an array of tuples with
41 # the same dimension as the grid.
42 #
43 # @Input: grid - an EquidistantGrid
44 # @Return: points - the points of the grid.
45 function points(grid::EquidistantGrid)
46 # TODO: Make this return an abstract array?
47 indices = Tuple.(CartesianIndices(grid.size))
48 h = spacing(grid)
49 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices)
50 end
51
52 function pointsalongdim(grid::EquidistantGrid, dim::Integer)
53 @assert dim<=dimension(grid)
54 @assert dim>0
55 points = collect(range(grid.limit_lower[dim],stop=grid.limit_upper[dim],length=grid.size[dim]))
56 end