Mercurial > repos > public > sbplib_julia
annotate src/Grids/equidistant_grid.jl @ 1253:ff8f335c32d1 refactor/grids
Fix indexing with begin and end for EquidistantGrid
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 22 Feb 2023 12:53:16 +0100 |
parents | 40ca0af6e480 |
children | 1989d432731a |
rev | line source |
---|---|
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
1 """ |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
2 # TODO: |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
3 """ |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
4 #TODO: Document recomendations for type of range. (LinRange is faster?) |
1222 | 5 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1,1} |
6 points::R | |
7 end | |
75
93c833019857
Make EquidistantGrid more concrete
Jonatan Werpers <jonatan@werpers.com>
parents:
74
diff
changeset
|
8 |
1222 | 9 Base.eltype(g::EquidistantGrid{T}) where T = T |
10 Base.getindex(g::EquidistantGrid, i) = g.points[i] | |
11 Base.size(g::EquidistantGrid) = size(g.points) | |
12 Base.length(g::EquidistantGrid) = length(g.points) | |
13 Base.eachindex(g::EquidistantGrid) = eachindex(g.points) | |
14 | |
1253
ff8f335c32d1
Fix indexing with begin and end for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1250
diff
changeset
|
15 Base.firstindex(g::EquidistantGrid) = firstindex(g.points) |
ff8f335c32d1
Fix indexing with begin and end for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1250
diff
changeset
|
16 Base.lastindex(g::EquidistantGrid) = lastindex(g.points) |
ff8f335c32d1
Fix indexing with begin and end for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1250
diff
changeset
|
17 |
1222 | 18 # TODO: Make sure collect works! |
19 | |
20 | |
406
c377fc37c04b
Clean up EquidistantGrid and tests after deciding that side lengths must be positive.
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
405
diff
changeset
|
21 """ |
1222 | 22 spacing(grid::EquidistantGrid) |
23 | |
24 The spacing between grid points. | |
25 """ | |
26 spacing(g::EquidistantGrid) = step(g.points) | |
27 | |
51
614b56a017b9
Split grid.jl into AbstractGrid.jl and EquidistantGrid.jl
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
28 |
1222 | 29 """ |
30 inverse_spacing(grid::EquidistantGrid) | |
31 | |
32 The reciprocal of the spacing between grid points. | |
33 """ | |
34 inverse_spacing(g::EquidistantGrid) = 1/step(g.points) | |
35 | |
36 | |
37 boundary_identifiers(::EquidistantGrid) = (Lower(), Upper()) | |
38 boundary_grid(g::EquidistantGrid, id::Lower) = ZeroDimGrid(g[begin]) | |
39 boundary_grid(g::EquidistantGrid, id::Upper) = ZeroDimGrid(g[end]) | |
40 | |
41 | |
42 """ | |
43 refine(g::EquidistantGrid, r::Int) | |
44 | |
45 Refines `grid` by a factor `r`. The factor is applied to the number of | |
46 intervals which is 1 less than the size of the grid. | |
47 | |
48 See also: [`coarsen`](@ref) | |
49 """ | |
50 function refine(g::EquidistantGrid, r::Int) | |
51 new_sz = (length(g) - 1)*r + 1 | |
52 return EquidistantGrid(change_length(g.points, new_sz)) | |
877
dd2ab001a7b6
Implement refine function, move exports to the top of the file, change location of constuctors.
Jonatan Werpers <jonatan@werpers.com>
parents:
688
diff
changeset
|
53 end |
680
1d3e29ffc6c6
Add support for 0-dimensional grid, and add method boundary_grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
661
diff
changeset
|
54 |
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
55 """ |
1222 | 56 coarsen(grid::EquidistantGrid, r::Int) |
57 | |
58 Coarsens `grid` by a factor `r`. The factor is applied to the number of | |
59 intervals which is 1 less than the size of the grid. If the number of | |
60 intervals are not divisible by `r` an error is raised. | |
61 | |
62 See also: [`refine`](@ref) | |
63 """ | |
64 function coarsen(g::EquidistantGrid, r::Int) | |
65 if (length(g)-1)%r != 0 | |
66 throw(DomainError(r, "Size minus 1 must be divisible by the ratio.")) | |
67 end | |
68 | |
69 new_sz = (length(g) - 1)÷r + 1 | |
70 | |
1249 | 71 return EquidistantGrid(change_length(g.points, new_sz)) |
1222 | 72 end |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 """ | |
81 equidistant_grid(size::Dims, limit_lower, limit_upper) | |
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
82 |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
83 Construct an equidistant grid with corners at the coordinates `limit_lower` and |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
84 `limit_upper`. |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
85 |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
86 The length of the domain sides are given by the components of |
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
87 `limit_upper-limit_lower`. E.g for a 2D grid with `limit_lower=(-1,0)` and |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
88 `limit_upper=(1,2)` the domain is defined as `(-1,1)x(0,2)`. The side lengths |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
89 of the grid are not allowed to be negative. |
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
90 |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
91 The number of equidistantly spaced points in each coordinate direction are given |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
92 by the tuple `size`. |
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
93 |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
94 Note: If `limit_lower` and `limit_upper` are integers and `size` would allow a |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
95 completely integer grid, `equidistant_grid` will still return a floating point |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
96 grid. This simlifies the implementation and avoids certain surprise |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
97 behaviours. |
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
98 """ |
1222 | 99 function equidistant_grid(size::Dims, limit_lower, limit_upper) |
1248
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
100 if any(size .<= 0) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
101 throw(DomainError("all components of size must be postive")) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
102 end |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
103 |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
104 if any(limit_upper.-limit_lower .<= 0) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
105 throw(DomainError("all side lengths must be postive")) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
106 end |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
107 |
1222 | 108 gs = map(size, limit_lower, limit_upper) do s,l,u |
109 EquidistantGrid(range(l, u, length=s)) # TBD: Should it use LinRange instead? | |
110 end | |
381
dacbcba33d7d
Refactor EquidistantGrid to not store spacing or inverse spacing
Jonatan Werpers <jonatan@werpers.com>
parents:
358
diff
changeset
|
111 |
1222 | 112 return TensorGrid(gs...) |
352
a18bd337a280
Add function for getting a one dimensional grid for a given dimension from a equidistant grid
Jonatan Werpers <jonatan@werpers.com>
parents:
333
diff
changeset
|
113 end |
658
26b0eb83aea4
Add function for getting boundary identifiers from equidistant grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
406
diff
changeset
|
114 |
1116
c2d7e940639e
Rename AbstractGrid to Grid and clean up Grids module
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1115
diff
changeset
|
115 |
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
116 """ |
1222 | 117 equidistant_grid(size::Int, limit_lower::T, limit_upper::T) |
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
118 |
1222 | 119 Constructs a 1D equidistant grid. |
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
120 """ |
1222 | 121 function equidistant_grid(size::Int, limit_lower::T, limit_upper::T) where T |
122 return equidistant_grid((size,),(limit_lower,),(limit_upper,)) | |
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
123 end |
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
124 |
1111
5b3d4a8ec3ab
Change to using filter for picking out orthogonal dimensions
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
909
diff
changeset
|
125 |
680
1d3e29ffc6c6
Add support for 0-dimensional grid, and add method boundary_grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
661
diff
changeset
|
126 |
1d3e29ffc6c6
Add support for 0-dimensional grid, and add method boundary_grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
661
diff
changeset
|
127 """ |
1222 | 128 change_length(::AbstractRange, n) |
680
1d3e29ffc6c6
Add support for 0-dimensional grid, and add method boundary_grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
661
diff
changeset
|
129 |
1222 | 130 Change the length of a range to `n`, keeping the same start and stop. |
877
dd2ab001a7b6
Implement refine function, move exports to the top of the file, change location of constuctors.
Jonatan Werpers <jonatan@werpers.com>
parents:
688
diff
changeset
|
131 """ |
1250
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
132 function change_length end |
908 | 133 |
1250
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
134 change_length(r::UnitRange, n) = StepRange{Int,Int}(range(r[begin], r[end], n)) |
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
135 change_length(r::StepRange, n) = StepRange{Int,Int}(range(r[begin], r[end], n)) |
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
136 change_length(r::StepRangeLen, n) = range(r[begin], r[end], n) |
1222 | 137 change_length(r::LinRange, n) = LinRange(r[begin], r[end], n) |
138 # TODO: Test the above |