comparison grid.jl @ 23:9031fe054f2c

Return the unsigned distances from grid.spacings(). Add comments
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Tue, 08 Jan 2019 11:17:20 +0100
parents f2dc3e09fffc
children 32a53cbee6c5
comparison
equal deleted inserted replaced
22:f2dc3e09fffc 23:9031fe054f2c
1 module grid 1 module grid
2 2
3 abstract type Grid end 3 abstract type Grid end
4 4
5 function numberOfDimensions(grid::Grid) 5 function numberOfDimensions(grid::Grid)
6 error("Not yet implemented") 6 error("Not implemented for abstact type Grid")
7 end 7 end
8 8
9 function numberOfPoints(grid::Grid) 9 function numberOfPoints(grid::Grid)
10 error("Not yet implemented") 10 error("Not implemented for abstact type Grid")
11 end 11 end
12 12
13 function points(grid::Grid) 13 function points(grid::Grid)
14 error("Not yet implemented") 14 error("Not implemented for abstact type Grid")
15 end 15 end
16 16
17 # TODO: Should this be here? 17 # TODO: Should this be here?
18 abstract type BoundaryId end 18 abstract type BoundaryId end
19 19
20 # TODO: Move to seperate file. 20 # EquidistantGrid is a grid with equidisant grid spacing per coordinat
21 # Prefer to use UInt here, but printing UInt returns hex. 21 # direction. The domain is defined through the two points P1 = x̄₁, P2 = x̄₂
22 # by the exterior product of the vectors obtained by projecting (x̄₂-x̄₁) onto
23 # the coordinate directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2)
24 # the domain is defined as (-1,1)x(0,2).
22 struct EquidistantGrid <: Grid 25 struct EquidistantGrid <: Grid
23 numberOfPointsPerDim::Tuple 26 numberOfPointsPerDim::Tuple # First coordinate direction stored first, then
24 limits::NTuple{2,Tuple} # Stores the points at the lower and upper corner of the domain. 27 # second, then third.
25 # e.g (-1,0) and (1,2) for a domain of size (-1,1)x(0,2) 28 limits::NTuple{2,Tuple} # Stores the two points which defines the range of
29 # the e.g (-1,0) and (1,2) for a domain of size
30 # (-1,1)x(0,2)
26 31
27 # General constructor 32 # General constructor
28 function EquidistantGrid(nPointsPerDim::Tuple, lims::NTuple{2,Tuple}) 33 function EquidistantGrid(nPointsPerDim::Tuple, lims::NTuple{2,Tuple})
29 @assert length(nPointsPerDim) > 0 34 @assert length(nPointsPerDim) > 0
30 @assert count(x -> x > 0, nPointsPerDim) == length(nPointsPerDim) 35 @assert count(x -> x > 0, nPointsPerDim) == length(nPointsPerDim)
32 @assert length(lims[2]) == length(nPointsPerDim) 37 @assert length(lims[2]) == length(nPointsPerDim)
33 # TODO: Assert that the same values are not passed in both lims[1] and lims[2] 38 # TODO: Assert that the same values are not passed in both lims[1] and lims[2]
34 # i.e the domain length is positive for all dimensions 39 # i.e the domain length is positive for all dimensions
35 return new(nPointsPerDim, lims) 40 return new(nPointsPerDim, lims)
36 end 41 end
37 # 1D constructor which can be called as EquidistantGrid(m, (x_l,x_r)) 42 # 1D constructor which can be called as EquidistantGrid(m, (xl,xr))
38 function EquidistantGrid(nPointsPerDim::Int, lims::NTuple{2,Int}) 43 function EquidistantGrid(nPointsPerDim::Integer, lims::NTuple{2,Integer})
39 return EquidistantGrid((nPointsPerDim,), ((lims[1],),(lims[2],))) 44 return EquidistantGrid((nPointsPerDim,), ((lims[1],),(lims[2],)))
40 end 45 end
41 46
42 end 47 end
43 48
49 # Returns the number of dimensions of an EquidistantGrid.
50 #
51 # @Input: grid - an EquidistantGrid
52 # @Return: numberOfPoints - The number of dimensions
44 function numberOfDimensions(grid::EquidistantGrid) 53 function numberOfDimensions(grid::EquidistantGrid)
45 return length(grid.numberOfPointsPerDim) 54 return length(grid.numberOfPointsPerDim)
46 end 55 end
47 56
57 # Computes the total number of points of an EquidistantGrid.
58 #
59 # @Input: grid - an EquidistantGrid
60 # @Return: numberOfPoints - The total number of points
48 function numberOfPoints(grid::EquidistantGrid) 61 function numberOfPoints(grid::EquidistantGrid)
49 numberOfPoints = grid.numberOfPointsPerDim[1]; 62 numberOfPoints = grid.numberOfPointsPerDim[1];
50 for i = 2:length(grid.numberOfPointsPerDim); 63 for i = 2:length(grid.numberOfPointsPerDim);
51 numberOfPoints = numberOfPoints*grid.numberOfPointsPerDim[i] 64 numberOfPoints = numberOfPoints*grid.numberOfPointsPerDim[i]
52 end 65 end
53 return numberOfPoints 66 return numberOfPoints
54 end 67 end
55 68
56 # TODO: Decide if spacings should be positive or if it is allowed to be negative 69 # Computes the grid spacing of an EquidistantGrid, i.e the unsigned distance
57 # If defined as positive, then need to do something extra when calculating the 70 # between two points for each coordinate direction.
58 # points. The current implementation works for arbitarily given limits of the grid. 71 #
72 # @Input: grid - an EquidistantGrid
73 # @Return: h̄ - Grid spacing for each coordinate direction stored in a tuple.
59 function spacings(grid::EquidistantGrid) 74 function spacings(grid::EquidistantGrid)
60 h = Vector{Real}(undef, numberOfDimensions(grid)) 75 h̄ = Vector{Real}(undef, numberOfDimensions(grid))
61 for i ∈ eachindex(h) 76 for i ∈ eachindex(h̄)
62 h[i] = (grid.limits[2][i]-grid.limits[1][i])/(grid.numberOfPointsPerDim[i]-1) 77 h̄[i] = abs(grid.limits[2][i]-grid.limits[1][i])/(grid.numberOfPointsPerDim[i]-1)
63 end 78 end
64 return Tuple(h) 79 return Tuple(h̄)
65 end 80 end
66 81
82 # Computes the points of an EquidistantGrid as a vector of tuples. The vector is ordered
83 # such that points in the first coordinate direction varies first, then the second
84 # and lastely the third (if applicable)
85 #
86 # @Input: grid - an EquidistantGrid
87 # @Return: points - the points of the grid.
67 function points(grid::EquidistantGrid) 88 function points(grid::EquidistantGrid)
89 # Compute signed grid spacings
90 dx̄ = Vector{Real}(undef, numberOfDimensions(grid))
91 for i ∈ eachindex(dx̄)
92 dx̄[i] = (grid.limits[2][i]-grid.limits[1][i])/(grid.numberOfPointsPerDim[i]-1)
93 end
94 dx̄ = Tuple(dx̄)
95
68 nPoints = numberOfPoints(grid) 96 nPoints = numberOfPoints(grid)
69 points = Vector{NTuple{numberOfDimensions(grid),Real}}(undef, nPoints) 97 points = Vector{NTuple{numberOfDimensions(grid),Real}}(undef, nPoints)
98 # Compute the points based on their Cartesian indices and the signed
99 # grid spacings
70 cartesianIndices = CartesianIndices(grid.numberOfPointsPerDim) 100 cartesianIndices = CartesianIndices(grid.numberOfPointsPerDim)
71 for i ∈ 1:nPoints 101 for i ∈ 1:nPoints
72 ci = Tuple(cartesianIndices[i]) .-1 102 ci = Tuple(cartesianIndices[i]) .-1
73 points[i] = grid.limits[1] .+ spacings(grid).*ci 103 points[i] = grid.limits[1] .+ dx̄.*ci
74 end 104 end
75 return points 105 return points
76 end 106 end
77 107
78 end 108 end