Mercurial > repos > public > sbplib_julia
comparison src/Grids/EquidistantGrid.jl @ 405:16dc5b19843d test/equidistantgrid
Fix exception handling in constructor of EquidistantGrid and add a bunch of tests
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Sat, 10 Oct 2020 20:05:39 +0200 |
parents | dacbcba33d7d |
children | c377fc37c04b |
comparison
equal
deleted
inserted
replaced
402:1936e38fe51e | 405:16dc5b19843d |
---|---|
11 limit_lower::NTuple{Dim, T} | 11 limit_lower::NTuple{Dim, T} |
12 limit_upper::NTuple{Dim, T} | 12 limit_upper::NTuple{Dim, T} |
13 | 13 |
14 # General constructor | 14 # General constructor |
15 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T | 15 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) | 16 all(size.>0) || throw(DomainError("size must be postive")) |
17 @assert all(limit_upper.-limit_lower .!= 0) | 17 # TODO: Is it reasonable to restrict side lengths to be positive? |
18 all(limit_upper.-limit_lower .> 0) || throw(DomainError("side lengths must be postive")) | |
18 return new{Dim,T}(size, limit_lower, limit_upper) | 19 return new{Dim,T}(size, limit_lower, limit_upper) |
19 end | 20 end |
20 end | 21 end |
21 | 22 |
22 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T | 23 function EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) where T |
41 """ | 42 """ |
42 spacing(grid::EquidistantGrid) | 43 spacing(grid::EquidistantGrid) |
43 | 44 |
44 The spacing between the grid points of the grid. | 45 The spacing between the grid points of the grid. |
45 """ | 46 """ |
47 # TODO: If we restrict side lenghts to be positive, then we should remove the abs here. | |
46 spacing(grid::EquidistantGrid) = abs.(grid.limit_upper.-grid.limit_lower)./(grid.size.-1) | 48 spacing(grid::EquidistantGrid) = abs.(grid.limit_upper.-grid.limit_lower)./(grid.size.-1) |
47 # TODO: Evaluate if divisions affect performance | |
48 export spacing | 49 export spacing |
49 | 50 |
50 """ | 51 """ |
51 spacing(grid::EquidistantGrid) | 52 spacing(grid::EquidistantGrid) |
52 | 53 |
58 # Computes the points of an EquidistantGrid as an array of tuples with | 59 # Computes the points of an EquidistantGrid as an array of tuples with |
59 # the same dimension as the grid. | 60 # the same dimension as the grid. |
60 # | 61 # |
61 # @Input: grid - an EquidistantGrid | 62 # @Input: grid - an EquidistantGrid |
62 # @Return: points - the points of the grid. | 63 # @Return: points - the points of the grid. |
64 # TODO: Does not work if side lengths are allowed to be negative. | |
63 function points(grid::EquidistantGrid) | 65 function points(grid::EquidistantGrid) |
64 # TODO: Make this return an abstract array? | 66 # TODO: Make this return an abstract array? |
65 indices = Tuple.(CartesianIndices(grid.size)) | 67 indices = Tuple.(CartesianIndices(grid.size)) |
66 h = spacing(grid) | 68 h = spacing(grid) |
67 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) | 69 return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices) |