Mercurial > repos > public > sbplib_julia
annotate src/Grids/equidistant_grid.jl @ 1222:5f677cd6f0b6 refactor/grids
Start refactoring
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Sat, 18 Feb 2023 11:37:35 +0100 |
| parents | 50b008d2e937 |
| children | 2abec782cf5b |
| rev | line source |
|---|---|
| 1222 | 1 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1,1} |
| 2 points::R | |
| 3 end | |
|
75
93c833019857
Make EquidistantGrid more concrete
Jonatan Werpers <jonatan@werpers.com>
parents:
74
diff
changeset
|
4 |
| 1222 | 5 Base.eltype(g::EquidistantGrid{T}) where T = T |
| 6 Base.getindex(g::EquidistantGrid, i) = g.points[i] | |
| 7 Base.size(g::EquidistantGrid) = size(g.points) | |
| 8 Base.length(g::EquidistantGrid) = length(g.points) | |
| 9 Base.eachindex(g::EquidistantGrid) = eachindex(g.points) | |
| 10 | |
| 11 # TODO: Make sure collect works! | |
| 12 | |
| 13 | |
|
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
|
14 """ |
| 1222 | 15 spacing(grid::EquidistantGrid) |
| 16 | |
| 17 The spacing between grid points. | |
| 18 """ | |
| 19 spacing(g::EquidistantGrid) = step(g.points) | |
| 20 | |
|
51
614b56a017b9
Split grid.jl into AbstractGrid.jl and EquidistantGrid.jl
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
21 |
| 1222 | 22 """ |
| 23 inverse_spacing(grid::EquidistantGrid) | |
| 24 | |
| 25 The reciprocal of the spacing between grid points. | |
| 26 """ | |
| 27 inverse_spacing(g::EquidistantGrid) = 1/step(g.points) | |
| 28 | |
| 29 | |
| 30 boundary_identifiers(::EquidistantGrid) = (Lower(), Upper()) | |
| 31 boundary_grid(g::EquidistantGrid, id::Lower) = ZeroDimGrid(g[begin]) | |
| 32 boundary_grid(g::EquidistantGrid, id::Upper) = ZeroDimGrid(g[end]) | |
| 33 | |
| 34 | |
| 35 """ | |
| 36 refine(g::EquidistantGrid, r::Int) | |
| 37 | |
| 38 Refines `grid` by a factor `r`. The factor is applied to the number of | |
| 39 intervals which is 1 less than the size of the grid. | |
| 40 | |
| 41 See also: [`coarsen`](@ref) | |
| 42 """ | |
| 43 function refine(g::EquidistantGrid, r::Int) | |
| 44 new_sz = (length(g) - 1)*r + 1 | |
| 45 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
|
46 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
|
47 |
|
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
48 """ |
| 1222 | 49 coarsen(grid::EquidistantGrid, r::Int) |
| 50 | |
| 51 Coarsens `grid` by a factor `r`. The factor is applied to the number of | |
| 52 intervals which is 1 less than the size of the grid. If the number of | |
| 53 intervals are not divisible by `r` an error is raised. | |
| 54 | |
| 55 See also: [`refine`](@ref) | |
| 56 """ | |
| 57 function coarsen(g::EquidistantGrid, r::Int) | |
| 58 if (length(g)-1)%r != 0 | |
| 59 throw(DomainError(r, "Size minus 1 must be divisible by the ratio.")) | |
| 60 end | |
| 61 | |
| 62 new_sz = (length(g) - 1)÷r + 1 | |
| 63 | |
| 64 return EquidistantGrid(change_length(g.points), new_sz) | |
| 65 end | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 """ | |
| 74 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
|
75 |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
76 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
|
77 `limit_upper`. |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
78 |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
79 The length of the domain sides are given by the components of |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
80 `limit_upper-limit_lower`. E.g for a 2D grid with `limit_lower=(-1,0)` and `limit_upper=(1,2)` the domain is defined |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
81 as `(-1,1)x(0,2)`. The side lengths of the grid are not allowed to be negative. |
|
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 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
|
84 by the tuple `size`. |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
85 """ |
| 1222 | 86 function equidistant_grid(size::Dims, limit_lower, limit_upper) |
| 87 gs = map(size, limit_lower, limit_upper) do s,l,u | |
| 88 EquidistantGrid(range(l, u, length=s)) # TBD: Should it use LinRange instead? | |
| 89 end | |
|
381
dacbcba33d7d
Refactor EquidistantGrid to not store spacing or inverse spacing
Jonatan Werpers <jonatan@werpers.com>
parents:
358
diff
changeset
|
90 |
| 1222 | 91 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
|
92 end |
|
658
26b0eb83aea4
Add function for getting boundary identifiers from equidistant grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
406
diff
changeset
|
93 |
|
1116
c2d7e940639e
Rename AbstractGrid to Grid and clean up Grids module
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1115
diff
changeset
|
94 |
|
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
95 """ |
| 1222 | 96 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
|
97 |
| 1222 | 98 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
|
99 """ |
| 1222 | 100 function equidistant_grid(size::Int, limit_lower::T, limit_upper::T) where T |
| 101 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
|
102 end |
|
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
103 |
|
1111
5b3d4a8ec3ab
Change to using filter for picking out orthogonal dimensions
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
909
diff
changeset
|
104 |
|
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
|
105 |
|
1d3e29ffc6c6
Add support for 0-dimensional grid, and add method boundary_grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
661
diff
changeset
|
106 """ |
| 1222 | 107 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
|
108 |
| 1222 | 109 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
|
110 """ |
| 1222 | 111 function change_length(::AbstractRange, n) end |
| 908 | 112 |
| 1222 | 113 change_length(r::LinRange, n) = LinRange(r[begin], r[end], n) |
| 114 change_length(r::StepRangeLen, n) = range(r[begin], r[end], n) | |
| 115 # TODO: Test the above |
