comparison src/Grids/EquidistantGrid.jl @ 877:dd2ab001a7b6 feature/equidistant_grid/refine

Implement refine function, move exports to the top of the file, change location of constuctors. The constructors were changed have only one inner constructor and simpler outer constructors.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 14 Feb 2022 09:39:58 +0100
parents e9e46a587370
children e81b89ae17c4
comparison
equal deleted inserted replaced
864:9a2776352c2a 877:dd2ab001a7b6
1 export EquidistantGrid
2 export spacing
3 export inverse_spacing
4 export restrict
5 export boundary_identifiers
6 export boundary_grid
7 export refine
8
1 """ 9 """
2 EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) 10 EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T})
3 EquidistantGrid{T}() 11 EquidistantGrid{T}()
4 12
5 `EquidistantGrid` is a grid with equidistant grid spacing per coordinat direction. 13 `EquidistantGrid` is a grid with equidistant grid spacing per coordinat direction.
19 size::NTuple{Dim, Int} 27 size::NTuple{Dim, Int}
20 limit_lower::NTuple{Dim, T} 28 limit_lower::NTuple{Dim, T}
21 limit_upper::NTuple{Dim, T} 29 limit_upper::NTuple{Dim, T}
22 30
23 # General constructor 31 # General constructor
24 function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T 32 function EquidistantGrid{Dim,T}(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where {Dim,T}
25 if any(size .<= 0) 33 if any(size .<= 0)
26 throw(DomainError("all components of size must be postive")) 34 throw(DomainError("all components of size must be postive"))
27 end 35 end
28 if any(limit_upper.-limit_lower .<= 0) 36 if any(limit_upper.-limit_lower .<= 0)
29 throw(DomainError("all side lengths must be postive")) 37 throw(DomainError("all side lengths must be postive"))
30 end 38 end
31 return new{Dim,T}(size, limit_lower, limit_upper) 39 return new{Dim,T}(size, limit_lower, limit_upper)
32 end 40 end
41 end
33 42
34 # Specialized constructor for 0-dimensional grid 43 function EquidistantGrid(size, limit_lower, limit_upper)
35 EquidistantGrid{T}() where T = new{0,T}((),(),()) 44 return EquidistantGrid{length(size), eltype(limit_lower)}(size, limit_lower, limit_upper)
36 end 45 end
37 export EquidistantGrid 46 EquidistantGrid{T}() where T = EquidistantGrid{0,T}((),(),()) # Convenience constructor for 0-dim grid
38
39 47
40 """ 48 """
41 EquidistantGrid(size::Int, limit_lower::T, limit_upper::T) 49 EquidistantGrid(size::Int, limit_lower::T, limit_upper::T)
42 50
43 Convenience constructor for 1D grids. 51 Convenience constructor for 1D grids.
63 spacing(grid::EquidistantGrid) 71 spacing(grid::EquidistantGrid)
64 72
65 The spacing between the grid points of the grid. 73 The spacing between the grid points of the grid.
66 """ 74 """
67 spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1) 75 spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
68 export spacing
69 76
70 """ 77 """
71 inverse_spacing(grid::EquidistantGrid) 78 inverse_spacing(grid::EquidistantGrid)
72 79
73 The reciprocal of the spacing between the grid points of the grid. 80 The reciprocal of the spacing between the grid points of the grid.
74 """ 81 """
75 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid) 82 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid)
76 export inverse_spacing
77 83
78 """ 84 """
79 points(grid::EquidistantGrid) 85 points(grid::EquidistantGrid)
80 86
81 The point of the grid as an array of tuples with the same dimension as the grid. 87 The point of the grid as an array of tuples with the same dimension as the grid.
100 limit_lower = grid.limit_lower[dim] 106 limit_lower = grid.limit_lower[dim]
101 limit_upper = grid.limit_upper[dim] 107 limit_upper = grid.limit_upper[dim]
102 108
103 return EquidistantGrid(size, limit_lower, limit_upper) 109 return EquidistantGrid(size, limit_lower, limit_upper)
104 end 110 end
105 export restrict
106 111
107 """ 112 """
108 boundary_identifiers(::EquidistantGrid) 113 boundary_identifiers(::EquidistantGrid)
109 114
110 Returns a tuple containing the boundary identifiers for the grid, stored as 115 Returns a tuple containing the boundary identifiers for the grid, stored as
112 CartesianBoundary(1,Upper), 117 CartesianBoundary(1,Upper),
113 CartesianBoundary(2,Lower), 118 CartesianBoundary(2,Lower),
114 ...) 119 ...)
115 """ 120 """
116 boundary_identifiers(g::EquidistantGrid) = (((ntuple(i->(CartesianBoundary{i,Lower}(),CartesianBoundary{i,Upper}()),dimension(g)))...)...,) 121 boundary_identifiers(g::EquidistantGrid) = (((ntuple(i->(CartesianBoundary{i,Lower}(),CartesianBoundary{i,Upper}()),dimension(g)))...)...,)
117 export boundary_identifiers
118 122
119 123
120 """ 124 """
121 boundary_grid(grid::EquidistantGrid,id::CartesianBoundary) 125 boundary_grid(grid::EquidistantGrid,id::CartesianBoundary)
122 boundary_grid(::EquidistantGrid{1},::CartesianBoundary{1}) 126 boundary_grid(::EquidistantGrid{1},::CartesianBoundary{1})
131 if orth_dims == dims 135 if orth_dims == dims
132 throw(DomainError("boundary identifier not matching grid")) 136 throw(DomainError("boundary identifier not matching grid"))
133 end 137 end
134 return restrict(grid,orth_dims) 138 return restrict(grid,orth_dims)
135 end 139 end
136 export boundary_grid
137 boundary_grid(::EquidistantGrid{1,T},::CartesianBoundary{1}) where T = EquidistantGrid{T}() 140 boundary_grid(::EquidistantGrid{1,T},::CartesianBoundary{1}) where T = EquidistantGrid{T}()
141
142
143 """
144 refine(grid::EquidistantGrid, r::Int)
145
146 Refines `grid` by a factor `r`. The factor is applied to the number of
147 intervals which is 1 less than the size of the grid.
148 """
149 function refine(grid::EquidistantGrid, r::Int)
150 sz = size(grid)
151 new_sz = (sz .- 1).*r .+ 1
152 return EquidistantGrid{dimension(grid), eltype(grid)}(new_sz, grid.limit_lower, grid.limit_upper)
153 end