Mercurial > repos > public > sbplib_julia
comparison 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 |
comparison
equal
deleted
inserted
replaced
332:535f1bff4bcc | 333:01b851161018 |
---|---|
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} # Reciprocal of 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 EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T | |
25 return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) | |
26 end | |
27 | |
28 function Base.eachindex(grid::EquidistantGrid) | |
29 CartesianIndices(grid.size) | |
30 end | |
31 | |
32 Base.size(g::EquidistantGrid) = g.size | |
33 | |
34 # Returns the number of dimensions of an EquidistantGrid. | |
35 # | |
36 # @Input: grid - an EquidistantGrid | |
37 # @Return: dimension - The dimension of the grid | |
38 function dimension(grid::EquidistantGrid) | |
39 return length(grid.size) | |
40 end | |
41 | |
42 # Returns the reciprocal of the spacing of the grid | |
43 # | |
44 function inverse_spacing(grid::EquidistantGrid) | |
45 return grid.inverse_spacing | |
46 end | |
47 export inverse_spacing | |
48 | |
49 # Returns the reciprocal of the spacing of the grid | |
50 # | |
51 # TODO: Evaluate if divisions affect performance | |
52 function spacing(grid::EquidistantGrid) | |
53 return 1.0./grid.inverse_spacing | |
54 end | |
55 export spacing | |
56 | |
57 # Computes the points of an EquidistantGrid as an array of tuples with | |
58 # the same dimension as the grid. | |
59 # | |
60 # @Input: grid - an EquidistantGrid | |
61 # @Return: points - the points of the grid. | |
62 function points(grid::EquidistantGrid) | |
63 # TODO: Make this return an abstract array? | |
64 indices = Tuple.(CartesianIndices(grid.size)) | |
65 h = spacing(grid) | |
66 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) | |
67 end | |
68 | |
69 function pointsalongdim(grid::EquidistantGrid, dim::Integer) | |
70 @assert dim<=dimension(grid) | |
71 @assert dim>0 | |
72 points = collect(range(grid.limit_lower[dim],stop=grid.limit_upper[dim],length=grid.size[dim])) | |
73 end |