comparison src/Grids/equidistant_grid.jl @ 1854:654a2b7e6824 tooling/benchmarks

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 11 Jan 2025 10:19:47 +0100
parents 244311761969
children 516eaabf1169 81559cb7b11c
comparison
equal deleted inserted replaced
1378:2b5480e2d4bf 1854:654a2b7e6824
13 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1} 13 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1}
14 points::R 14 points::R
15 end 15 end
16 16
17 # Indexing interface 17 # Indexing interface
18 Base.getindex(g::EquidistantGrid, i) = g.points[i] 18 Base.getindex(g::EquidistantGrid, i::Int) = g.points[i]
19 Base.eachindex(g::EquidistantGrid) = eachindex(g.points) 19 Base.eachindex(g::EquidistantGrid) = eachindex(g.points)
20 Base.firstindex(g::EquidistantGrid) = firstindex(g.points) 20 Base.firstindex(g::EquidistantGrid) = firstindex(g.points)
21 Base.lastindex(g::EquidistantGrid) = lastindex(g.points) 21 Base.lastindex(g::EquidistantGrid) = lastindex(g.points)
22
23 Base.axes(g::EquidistantGrid, d) = axes(g.points, d)
22 24
23 # Iteration interface 25 # Iteration interface
24 Base.iterate(g::EquidistantGrid) = iterate(g.points) 26 Base.iterate(g::EquidistantGrid) = iterate(g.points)
25 Base.iterate(g::EquidistantGrid, state) = iterate(g.points, state) 27 Base.iterate(g::EquidistantGrid, state) = iterate(g.points, state)
26 28
27 Base.IteratorSize(::Type{<:EquidistantGrid}) = Base.HasShape{1}() 29 Base.IteratorSize(::Type{<:EquidistantGrid}) = Base.HasShape{1}()
28 Base.length(g::EquidistantGrid) = length(g.points) 30 Base.length(g::EquidistantGrid) = length(g.points)
29 Base.size(g::EquidistantGrid) = size(g.points) 31 Base.size(g::EquidistantGrid) = size(g.points)
32 Base.size(g::EquidistantGrid, d) = size(g.points)[d]
30 33
31 34
32 """ 35 """
33 spacing(grid::EquidistantGrid) 36 spacing(grid::EquidistantGrid)
34 37
42 45
43 The reciprocal of the spacing between grid points. 46 The reciprocal of the spacing between grid points.
44 """ 47 """
45 inverse_spacing(g::EquidistantGrid) = 1/step(g.points) 48 inverse_spacing(g::EquidistantGrid) = 1/step(g.points)
46 49
50 min_spacing(g::EquidistantGrid) = spacing(g)
47 51
48 boundary_identifiers(::EquidistantGrid) = (Lower(), Upper()) 52 """
49 boundary_grid(g::EquidistantGrid, id::Lower) = ZeroDimGrid(g[begin]) 53 LowerBoundary <: BoundaryIdentifier
50 boundary_grid(g::EquidistantGrid, id::Upper) = ZeroDimGrid(g[end])
51 54
55 Boundary identifier for the the lower (left) boundary of a one-dimensional grid.
56
57 See also: [`BoundaryIdentifier`](@ref)
58 """
59 struct LowerBoundary <: BoundaryIdentifier end
60
61 """
62 UpperBoundary <: BoundaryIdentifier
63
64 Boundary identifier for the the upper (right) boundary of a one-dimensional grid.
65
66 See also: [`BoundaryIdentifier`](@ref)
67 """
68 struct UpperBoundary <: BoundaryIdentifier end
69
70
71 boundary_identifiers(::EquidistantGrid) = (LowerBoundary(), UpperBoundary())
72 boundary_grid(g::EquidistantGrid, id::LowerBoundary) = ZeroDimGrid(g[begin])
73 boundary_grid(g::EquidistantGrid, id::UpperBoundary) = ZeroDimGrid(g[end])
74 boundary_indices(g::EquidistantGrid, id::LowerBoundary) = (firstindex(g),)
75 boundary_indices(g::EquidistantGrid, id::UpperBoundary) = (lastindex(g),)
52 76
53 """ 77 """
54 refine(g::EquidistantGrid, r::Int) 78 refine(g::EquidistantGrid, r::Int)
55 79
56 The grid where `g` is refined by the factor `r`. The factor is applied to the number of 80 The grid where `g` is refined by the factor `r`. The factor is applied to the number of
82 return EquidistantGrid(change_length(g.points, new_sz)) 106 return EquidistantGrid(change_length(g.points, new_sz))
83 end 107 end
84 108
85 109
86 """ 110 """
87 equidistant_grid(size::Dims, limit_lower, limit_upper) 111 equidistant_grid(limit_lower, limit_upper, dims...)
88 112
89 Construct an equidistant grid with corners at the coordinates `limit_lower` and 113 Construct an equidistant grid with corners at the coordinates `limit_lower` and
90 `limit_upper`. 114 `limit_upper`.
91 115
92 The length of the domain sides are given by the components of 116 The length of the domain sides are given by the components of
93 `limit_upper-limit_lower`. E.g for a 2D grid with `limit_lower=(-1,0)` and 117 `limit_upper-limit_lower`. E.g for a 2D grid with `limit_lower=(-1,0)` and
94 `limit_upper=(1,2)` the domain is defined as `(-1,1)x(0,2)`. The side lengths 118 `limit_upper=(1,2)` the domain is defined as `(-1,1)x(0,2)`. The side lengths
95 of the grid are not allowed to be negative. 119 of the grid are not allowed to be negative.
96 120
97 The number of equispaced points in each coordinate direction are given 121 The number of equispaced points in each coordinate direction are given
98 by the tuple `size`. 122 by the tuple `dims`.
99 123
100 Note: If `limit_lower` and `limit_upper` are integers and `size` would allow a 124 Note: If `limit_lower` and `limit_upper` are integers and `dims` would allow a
101 completely integer grid, `equidistant_grid` will still return a floating point 125 completely integer grid, `equidistant_grid` will still return a floating point
102 grid. This simlifies the implementation and avoids certain surprise 126 grid. This simplifies the implementation and avoids certain surprise
103 behaviours. 127 behaviors.
104 """ 128 """
105 function equidistant_grid(size::Dims, limit_lower, limit_upper) 129 function equidistant_grid(limit_lower, limit_upper, dims::Vararg{Int})
106 gs = map(equidistant_grid, size, limit_lower, limit_upper) 130 if !(length(limit_lower) == length(limit_upper) == length(dims))
131 throw(ArgumentError("All arguments must be of the same length"))
132 end
133 gs = map(equidistant_grid, limit_lower, limit_upper, dims)
107 return TensorGrid(gs...) 134 return TensorGrid(gs...)
108 end 135 end
109 136
110 """ 137 """
111 equidistant_grid(size::Int, limit_lower::T, limit_upper::T) 138 equidistant_grid(limit_lower::T, limit_upper::T, size::Int)
112 139
113 Constructs a 1D equidistant grid. 140 Constructs a 1D equidistant grid.
114 """ 141 """
115 function equidistant_grid(size::Int, limit_lower::T, limit_upper::T) where T 142 function equidistant_grid(limit_lower::Number, limit_upper::Number, size::Int)
116 if any(size .<= 0) 143 if any(size .<= 0)
117 throw(DomainError("size must be postive")) 144 throw(DomainError("size must be postive"))
118 end 145 end
119 146
120 if any(limit_upper.-limit_lower .<= 0) 147 if any(limit_upper.-limit_lower .<= 0)