Mercurial > repos > public > sbplib_julia
annotate src/Grids/equidistant_grid.jl @ 1250:40ca0af6e480 refactor/grids
Add methods to change_length and fix incorrect function declaration
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 22 Feb 2023 12:43:36 +0100 |
parents | 215f36712332 |
children | ff8f335c32d1 |
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 | |
15 # TODO: Make sure collect works! | |
16 | |
17 | |
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
|
18 """ |
1222 | 19 spacing(grid::EquidistantGrid) |
20 | |
21 The spacing between grid points. | |
22 """ | |
23 spacing(g::EquidistantGrid) = step(g.points) | |
24 | |
51
614b56a017b9
Split grid.jl into AbstractGrid.jl and EquidistantGrid.jl
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
25 |
1222 | 26 """ |
27 inverse_spacing(grid::EquidistantGrid) | |
28 | |
29 The reciprocal of the spacing between grid points. | |
30 """ | |
31 inverse_spacing(g::EquidistantGrid) = 1/step(g.points) | |
32 | |
33 | |
34 boundary_identifiers(::EquidistantGrid) = (Lower(), Upper()) | |
35 boundary_grid(g::EquidistantGrid, id::Lower) = ZeroDimGrid(g[begin]) | |
36 boundary_grid(g::EquidistantGrid, id::Upper) = ZeroDimGrid(g[end]) | |
37 | |
38 | |
39 """ | |
40 refine(g::EquidistantGrid, r::Int) | |
41 | |
42 Refines `grid` by a factor `r`. The factor is applied to the number of | |
43 intervals which is 1 less than the size of the grid. | |
44 | |
45 See also: [`coarsen`](@ref) | |
46 """ | |
47 function refine(g::EquidistantGrid, r::Int) | |
48 new_sz = (length(g) - 1)*r + 1 | |
49 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
|
50 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
|
51 |
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
52 """ |
1222 | 53 coarsen(grid::EquidistantGrid, r::Int) |
54 | |
55 Coarsens `grid` by a factor `r`. The factor is applied to the number of | |
56 intervals which is 1 less than the size of the grid. If the number of | |
57 intervals are not divisible by `r` an error is raised. | |
58 | |
59 See also: [`refine`](@ref) | |
60 """ | |
61 function coarsen(g::EquidistantGrid, r::Int) | |
62 if (length(g)-1)%r != 0 | |
63 throw(DomainError(r, "Size minus 1 must be divisible by the ratio.")) | |
64 end | |
65 | |
66 new_sz = (length(g) - 1)÷r + 1 | |
67 | |
1249 | 68 return EquidistantGrid(change_length(g.points, new_sz)) |
1222 | 69 end |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 """ | |
78 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
|
79 |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
80 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
|
81 `limit_upper`. |
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 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
|
84 `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
|
85 `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
|
86 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
|
87 |
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
88 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
|
89 by the tuple `size`. |
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
90 |
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
91 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
|
92 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
|
93 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
|
94 behaviours. |
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
95 """ |
1222 | 96 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
|
97 if any(size .<= 0) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
98 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
|
99 end |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
100 |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
101 if any(limit_upper.-limit_lower .<= 0) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
102 throw(DomainError("all side lengths must be postive")) |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
103 end |
a5f2372d0e87
Add argument checking to equdistant_grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1247
diff
changeset
|
104 |
1222 | 105 gs = map(size, limit_lower, limit_upper) do s,l,u |
106 EquidistantGrid(range(l, u, length=s)) # TBD: Should it use LinRange instead? | |
107 end | |
381
dacbcba33d7d
Refactor EquidistantGrid to not store spacing or inverse spacing
Jonatan Werpers <jonatan@werpers.com>
parents:
358
diff
changeset
|
108 |
1222 | 109 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
|
110 end |
658
26b0eb83aea4
Add function for getting boundary identifiers from equidistant grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
406
diff
changeset
|
111 |
1116
c2d7e940639e
Rename AbstractGrid to Grid and clean up Grids module
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1115
diff
changeset
|
112 |
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
113 """ |
1222 | 114 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
|
115 |
1222 | 116 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
|
117 """ |
1222 | 118 function equidistant_grid(size::Int, limit_lower::T, limit_upper::T) where T |
119 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
|
120 end |
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
121 |
1111
5b3d4a8ec3ab
Change to using filter for picking out orthogonal dimensions
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
909
diff
changeset
|
122 |
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
|
123 |
1d3e29ffc6c6
Add support for 0-dimensional grid, and add method boundary_grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
661
diff
changeset
|
124 """ |
1222 | 125 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
|
126 |
1222 | 127 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
|
128 """ |
1250
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
129 function change_length end |
908 | 130 |
1250
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
131 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
|
132 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
|
133 change_length(r::StepRangeLen, n) = range(r[begin], r[end], n) |
1222 | 134 change_length(r::LinRange, n) = LinRange(r[begin], r[end], n) |
135 # TODO: Test the above |