Mercurial > repos > public > sbplib_julia
comparison src/Grids/EquidistantGrid.jl @ 409:b4e65cb18423
Merge test/equidistantgrid
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Tue, 13 Oct 2020 18:33:43 +0200 |
parents | c377fc37c04b |
children | 26b0eb83aea4 |
comparison
equal
deleted
inserted
replaced
404:48d57f185f86 | 409:b4e65cb18423 |
---|---|
1 # EquidistantGrid is a grid with equidistant grid spacing per coordinat | 1 """ |
2 # direction. The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ | 2 EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T} |
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 | 3 |
7 export EquidistantGrid | 4 EquidistantGrid is a grid with equidistant grid spacing per coordinat direction. |
8 | 5 The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ by the exterior |
6 product of the vectors obtained by projecting (x̄₂-x̄₁) onto the coordinate | |
7 directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2) the domain is defined | |
8 as (-1,1)x(0,2). The side lengths of the grid are not allowed to be negative | |
9 """ | |
9 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid | 10 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid |
10 size::NTuple{Dim, Int} | 11 size::NTuple{Dim, Int} |
11 limit_lower::NTuple{Dim, T} | 12 limit_lower::NTuple{Dim, T} |
12 limit_upper::NTuple{Dim, T} | 13 limit_upper::NTuple{Dim, T} |
13 | 14 |
14 # General constructor | 15 # General constructor |
15 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T | 16 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T |
16 @assert all(size.>0) | 17 if any(size .<= 0) |
17 @assert all(limit_upper.-limit_lower .!= 0) | 18 throw(DomainError("all components of size must be postive")) |
19 end | |
20 if any(limit_upper.-limit_lower .<= 0) | |
21 throw(DomainError("all side lengths must be postive")) | |
22 end | |
18 return new{Dim,T}(size, limit_lower, limit_upper) | 23 return new{Dim,T}(size, limit_lower, limit_upper) |
19 end | 24 end |
20 end | 25 end |
26 export EquidistantGrid | |
21 | 27 |
28 | |
29 """ | |
30 EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) | |
31 | |
32 Convenience constructor for 1D grids. | |
33 """ | |
22 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T | 34 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T |
23 return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) | 35 return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) |
24 end | 36 end |
25 | 37 |
26 function Base.eachindex(grid::EquidistantGrid) | 38 function Base.eachindex(grid::EquidistantGrid) |
27 CartesianIndices(grid.size) | 39 CartesianIndices(grid.size) |
28 end | 40 end |
29 | 41 |
30 Base.size(g::EquidistantGrid) = g.size | 42 Base.size(g::EquidistantGrid) = g.size |
31 | 43 |
32 # Returns the number of dimensions of an EquidistantGrid. | 44 """ |
33 # | 45 dimension(grid::EquidistantGrid) |
34 # @Input: grid - an EquidistantGrid | 46 |
35 # @Return: dimension - The dimension of the grid | 47 The dimension of the grid. |
48 """ | |
36 function dimension(grid::EquidistantGrid) | 49 function dimension(grid::EquidistantGrid) |
37 return length(grid.size) | 50 return length(grid.size) |
38 end | 51 end |
39 | 52 |
40 | 53 |
41 """ | 54 """ |
42 spacing(grid::EquidistantGrid) | 55 spacing(grid::EquidistantGrid) |
43 | 56 |
44 The spacing between the grid points of the grid. | 57 The spacing between the grid points of the grid. |
45 """ | 58 """ |
46 spacing(grid::EquidistantGrid) = abs.(grid.limit_upper.-grid.limit_lower)./(grid.size.-1) | 59 spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1) |
47 # TODO: Evaluate if divisions affect performance | |
48 export spacing | 60 export spacing |
49 | 61 |
50 """ | 62 """ |
51 spacing(grid::EquidistantGrid) | 63 inverse_spacing(grid::EquidistantGrid) |
52 | 64 |
53 The reciprocal of the spacing between the grid points of the grid. | 65 The reciprocal of the spacing between the grid points of the grid. |
54 """ | 66 """ |
55 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid) | 67 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid) |
56 export inverse_spacing | 68 export inverse_spacing |
57 | 69 |
58 # Computes the points of an EquidistantGrid as an array of tuples with | 70 """ |
59 # the same dimension as the grid. | 71 points(grid::EquidistantGrid) |
60 # | 72 |
61 # @Input: grid - an EquidistantGrid | 73 The point of the grid as an array of tuples with the same dimension as the grid. |
62 # @Return: points - the points of the grid. | 74 The points are stored as [(x1,y1), (x1,y2), … (x1,yn); |
75 (x2,y1), (x2,y2), … (x2,yn); | |
76 ⋮ ⋮ ⋮ | |
77 (xm,y1), (xm,y2), … (xm,yn)] | |
78 """ | |
63 function points(grid::EquidistantGrid) | 79 function points(grid::EquidistantGrid) |
64 # TODO: Make this return an abstract array? | |
65 indices = Tuple.(CartesianIndices(grid.size)) | 80 indices = Tuple.(CartesianIndices(grid.size)) |
66 h = spacing(grid) | 81 h = spacing(grid) |
67 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) | 82 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) |
68 end | 83 end |
69 | 84 |