Mercurial > repos > public > sbplib_julia
changeset 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 |
files | grid.jl |
diffstat | 1 files changed, 48 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/grid.jl Tue Dec 18 14:51:41 2018 +0100 +++ b/grid.jl Tue Jan 08 11:17:20 2019 +0100 @@ -3,26 +3,31 @@ abstract type Grid end function numberOfDimensions(grid::Grid) - error("Not yet implemented") + error("Not implemented for abstact type Grid") end function numberOfPoints(grid::Grid) - error("Not yet implemented") + error("Not implemented for abstact type Grid") end function points(grid::Grid) - error("Not yet implemented") + error("Not implemented for abstact type Grid") end # TODO: Should this be here? abstract type BoundaryId end -# TODO: Move to seperate file. -# Prefer to use UInt here, but printing UInt returns hex. +# EquidistantGrid is a grid with equidisant grid spacing per coordinat +# direction. The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ +# by the exterior product of the vectors obtained by projecting (x̄₂-x̄₁) onto +# the coordinate directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2) +# the domain is defined as (-1,1)x(0,2). struct EquidistantGrid <: Grid - numberOfPointsPerDim::Tuple - limits::NTuple{2,Tuple} # Stores the points at the lower and upper corner of the domain. - # e.g (-1,0) and (1,2) for a domain of size (-1,1)x(0,2) + numberOfPointsPerDim::Tuple # First coordinate direction stored first, then + # second, then third. + limits::NTuple{2,Tuple} # Stores the two points which defines the range of + # the e.g (-1,0) and (1,2) for a domain of size + # (-1,1)x(0,2) # General constructor function EquidistantGrid(nPointsPerDim::Tuple, lims::NTuple{2,Tuple}) @@ -34,17 +39,25 @@ # i.e the domain length is positive for all dimensions return new(nPointsPerDim, lims) end - # 1D constructor which can be called as EquidistantGrid(m, (x_l,x_r)) - function EquidistantGrid(nPointsPerDim::Int, lims::NTuple{2,Int}) + # 1D constructor which can be called as EquidistantGrid(m, (xl,xr)) + function EquidistantGrid(nPointsPerDim::Integer, lims::NTuple{2,Integer}) return EquidistantGrid((nPointsPerDim,), ((lims[1],),(lims[2],))) end end +# Returns the number of dimensions of an EquidistantGrid. +# +# @Input: grid - an EquidistantGrid +# @Return: numberOfPoints - The number of dimensions function numberOfDimensions(grid::EquidistantGrid) return length(grid.numberOfPointsPerDim) end +# Computes the total number of points of an EquidistantGrid. +# +# @Input: grid - an EquidistantGrid +# @Return: numberOfPoints - The total number of points function numberOfPoints(grid::EquidistantGrid) numberOfPoints = grid.numberOfPointsPerDim[1]; for i = 2:length(grid.numberOfPointsPerDim); @@ -53,24 +66,41 @@ return numberOfPoints end -# TODO: Decide if spacings should be positive or if it is allowed to be negative -# If defined as positive, then need to do something extra when calculating the -# points. The current implementation works for arbitarily given limits of the grid. +# Computes the grid spacing of an EquidistantGrid, i.e the unsigned distance +# between two points for each coordinate direction. +# +# @Input: grid - an EquidistantGrid +# @Return: h̄ - Grid spacing for each coordinate direction stored in a tuple. function spacings(grid::EquidistantGrid) - h = Vector{Real}(undef, numberOfDimensions(grid)) - for i ∈ eachindex(h) - h[i] = (grid.limits[2][i]-grid.limits[1][i])/(grid.numberOfPointsPerDim[i]-1) + h̄ = Vector{Real}(undef, numberOfDimensions(grid)) + for i ∈ eachindex(h̄) + h̄[i] = abs(grid.limits[2][i]-grid.limits[1][i])/(grid.numberOfPointsPerDim[i]-1) end - return Tuple(h) + return Tuple(h̄) end +# Computes the points of an EquidistantGrid as a vector of tuples. The vector is ordered +# such that points in the first coordinate direction varies first, then the second +# and lastely the third (if applicable) +# +# @Input: grid - an EquidistantGrid +# @Return: points - the points of the grid. function points(grid::EquidistantGrid) + # Compute signed grid spacings + dx̄ = Vector{Real}(undef, numberOfDimensions(grid)) + for i ∈ eachindex(dx̄) + dx̄[i] = (grid.limits[2][i]-grid.limits[1][i])/(grid.numberOfPointsPerDim[i]-1) + end + dx̄ = Tuple(dx̄) + nPoints = numberOfPoints(grid) points = Vector{NTuple{numberOfDimensions(grid),Real}}(undef, nPoints) + # Compute the points based on their Cartesian indices and the signed + # grid spacings cartesianIndices = CartesianIndices(grid.numberOfPointsPerDim) for i ∈ 1:nPoints ci = Tuple(cartesianIndices[i]) .-1 - points[i] = grid.limits[1] .+ spacings(grid).*ci + points[i] = grid.limits[1] .+ dx̄.*ci end return points end