Mercurial > repos > public > sbplib_julia
diff src/Grids/EquidistantGrid.jl @ 333:01b851161018 refactor/combine_to_one_package
Start converting to one package by moving all the files to their correct location
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 25 Sep 2020 13:06:02 +0200 |
parents | Grids/src/EquidistantGrid.jl@047dee8efaef |
children | a18bd337a280 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Grids/EquidistantGrid.jl Fri Sep 25 13:06:02 2020 +0200 @@ -0,0 +1,73 @@ +# EquidistantGrid is a grid with equidistant 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). + +export EquidistantGrid + +struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid + size::NTuple{Dim, Int} # First coordinate direction stored first + limit_lower::NTuple{Dim, T} + limit_upper::NTuple{Dim, T} + inverse_spacing::NTuple{Dim, T} # Reciprocal of grid spacing + + # General constructor + function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T + @assert all(size.>0) + @assert all(limit_upper.-limit_lower .!= 0) + inverse_spacing = (size.-1)./ abs.(limit_upper.-limit_lower) + return new{Dim,T}(size, limit_lower, limit_upper, inverse_spacing) + end +end + +function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T + return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) +end + +function Base.eachindex(grid::EquidistantGrid) + CartesianIndices(grid.size) +end + +Base.size(g::EquidistantGrid) = g.size + +# Returns the number of dimensions of an EquidistantGrid. +# +# @Input: grid - an EquidistantGrid +# @Return: dimension - The dimension of the grid +function dimension(grid::EquidistantGrid) + return length(grid.size) +end + +# Returns the reciprocal of the spacing of the grid +# +function inverse_spacing(grid::EquidistantGrid) + return grid.inverse_spacing +end +export inverse_spacing + +# Returns the reciprocal of the spacing of the grid +# +# TODO: Evaluate if divisions affect performance +function spacing(grid::EquidistantGrid) + return 1.0./grid.inverse_spacing +end +export spacing + +# Computes the points of an EquidistantGrid as an array of tuples with +# the same dimension as the grid. +# +# @Input: grid - an EquidistantGrid +# @Return: points - the points of the grid. +function points(grid::EquidistantGrid) + # TODO: Make this return an abstract array? + indices = Tuple.(CartesianIndices(grid.size)) + h = spacing(grid) + return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) +end + +function pointsalongdim(grid::EquidistantGrid, dim::Integer) + @assert dim<=dimension(grid) + @assert dim>0 + points = collect(range(grid.limit_lower[dim],stop=grid.limit_upper[dim],length=grid.size[dim])) +end