Mercurial > repos > public > sbplib_julia
annotate src/Grids/equidistant_grid.jl @ 1359:646027afe74b bugfix/lazytensors
Merge default
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Sat, 20 May 2023 14:33:25 +0200 |
| parents | 08f06bfacd5c |
| children | 4684c7f1c4cb |
| rev | line source |
|---|---|
|
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
1 """ |
|
1331
ed3ea0630825
Add some docs for Grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1282
diff
changeset
|
2 EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1} |
|
ed3ea0630825
Add some docs for Grid
Jonatan Werpers <jonatan@werpers.com>
parents:
1282
diff
changeset
|
3 |
|
1335
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
4 A one-dimensional equidistant grid. Most users are expected to use |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
5 [`equidistant_grid`](@ref) for constructing equidistant grids. |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
6 |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
7 See also: [`equidistant_grid`](@ref) |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
8 |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
9 |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
10 ## Note |
|
cca45af5e724
Some docs for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1331
diff
changeset
|
11 The type of range used for the points can likely impact performance. |
|
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
12 """ |
|
1257
198ccda331a6
Remove range dim as a type paratmeter on Grid as it is already encoded in T if available
Jonatan Werpers <jonatan@werpers.com>
parents:
1255
diff
changeset
|
13 struct EquidistantGrid{T,R<:AbstractRange{T}} <: Grid{T,1} |
| 1222 | 14 points::R |
| 15 end | |
|
75
93c833019857
Make EquidistantGrid more concrete
Jonatan Werpers <jonatan@werpers.com>
parents:
74
diff
changeset
|
16 |
|
1255
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
17 # Indexing interface |
|
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
18 Base.getindex(g::EquidistantGrid, i) = g.points[i] |
|
1344
760a4a1ec4b7
Add 2D tests for dissipation operators and fix bug
Jonatan Werpers <jonatan@werpers.com>
parents:
1335
diff
changeset
|
19 Base.eachindex(g::EquidistantGrid) = eachindex(g.points) |
|
1253
ff8f335c32d1
Fix indexing with begin and end for EquidistantGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1250
diff
changeset
|
20 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
|
21 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
|
22 |
|
1255
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
23 # Iteration interface |
|
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
24 Base.iterate(g::EquidistantGrid) = iterate(g.points) |
|
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
25 Base.iterate(g::EquidistantGrid, state) = iterate(g.points, state) |
|
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
26 |
|
1262
5e28ae42caf2
Clean up IteratorSize and eltype for EquidistantGrid, Grid, and ZeroDimGrid
Jonatan Werpers <jonatan@werpers.com>
parents:
1259
diff
changeset
|
27 Base.IteratorSize(::Type{<:EquidistantGrid}) = Base.HasShape{1}() |
|
1255
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
28 Base.length(g::EquidistantGrid) = length(g.points) |
|
1989d432731a
Implement the interfaces for iteration and indexing on EquidistantGrid. Make collect() work
Jonatan Werpers <jonatan@werpers.com>
parents:
1253
diff
changeset
|
29 Base.size(g::EquidistantGrid) = size(g.points) |
| 1222 | 30 |
| 31 | |
|
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
|
32 """ |
| 1222 | 33 spacing(grid::EquidistantGrid) |
| 34 | |
| 35 The spacing between grid points. | |
| 36 """ | |
| 37 spacing(g::EquidistantGrid) = step(g.points) | |
| 38 | |
|
51
614b56a017b9
Split grid.jl into AbstractGrid.jl and EquidistantGrid.jl
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff
changeset
|
39 |
| 1222 | 40 """ |
| 41 inverse_spacing(grid::EquidistantGrid) | |
| 42 | |
| 43 The reciprocal of the spacing between grid points. | |
| 44 """ | |
| 45 inverse_spacing(g::EquidistantGrid) = 1/step(g.points) | |
| 46 | |
| 47 | |
| 48 boundary_identifiers(::EquidistantGrid) = (Lower(), Upper()) | |
| 49 boundary_grid(g::EquidistantGrid, id::Lower) = ZeroDimGrid(g[begin]) | |
| 50 boundary_grid(g::EquidistantGrid, id::Upper) = ZeroDimGrid(g[end]) | |
| 51 | |
| 52 | |
| 53 """ | |
| 54 refine(g::EquidistantGrid, r::Int) | |
| 55 | |
|
1347
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
56 The grid where `g` is refined by the factor `r`. The factor is applied to the number of |
|
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
57 intervals, i.e., 1 less than the size of `g`. |
| 1222 | 58 |
| 59 See also: [`coarsen`](@ref) | |
| 60 """ | |
| 61 function refine(g::EquidistantGrid, r::Int) | |
| 62 new_sz = (length(g) - 1)*r + 1 | |
| 63 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
|
64 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
|
65 |
|
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
66 """ |
|
1347
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
67 coarsen(g::EquidistantGrid, r::Int) |
| 1222 | 68 |
|
1347
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
69 The grid where `g` is coarsened by the factor `r`. The factor is applied to the number of |
|
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
70 intervals, i.e., 1 less than the size of `g`. If the number of |
| 1222 | 71 intervals are not divisible by `r` an error is raised. |
| 72 | |
| 73 See also: [`refine`](@ref) | |
| 74 """ | |
| 75 function coarsen(g::EquidistantGrid, r::Int) | |
| 76 if (length(g)-1)%r != 0 | |
| 77 throw(DomainError(r, "Size minus 1 must be divisible by the ratio.")) | |
| 78 end | |
| 79 | |
| 80 new_sz = (length(g) - 1)÷r + 1 | |
| 81 | |
| 1249 | 82 return EquidistantGrid(change_length(g.points, new_sz)) |
| 1222 | 83 end |
| 84 | |
| 85 | |
| 86 """ | |
| 87 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
|
88 |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
89 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
|
90 `limit_upper`. |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
91 |
|
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
92 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
|
93 `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
|
94 `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
|
95 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
|
96 |
|
1347
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
97 The number of equispaced points in each coordinate direction are given |
|
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
98 by the tuple `size`. |
|
1247
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
99 |
|
2abec782cf5b
Add todo for EquidistantGrid docs, improve equidistant_grid docs
Jonatan Werpers <jonatan@werpers.com>
parents:
1222
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 behaviours. |
|
909
b900fea1c057
Clean up the docs a bit
Jonatan Werpers <jonatan@werpers.com>
parents:
908
diff
changeset
|
104 """ |
| 1222 | 105 function equidistant_grid(size::Dims, limit_lower, limit_upper) |
|
1282
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
106 gs = map(equidistant_grid, size, limit_lower, limit_upper) |
| 1222 | 107 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
|
108 end |
|
658
26b0eb83aea4
Add function for getting boundary identifiers from equidistant grid
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
406
diff
changeset
|
109 |
|
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
110 """ |
| 1222 | 111 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
|
112 |
| 1222 | 113 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
|
114 """ |
| 1222 | 115 function equidistant_grid(size::Int, limit_lower::T, limit_upper::T) where T |
|
1282
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
116 if any(size .<= 0) |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
117 throw(DomainError("size must be postive")) |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
118 end |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
119 |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
120 if any(limit_upper.-limit_lower .<= 0) |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
121 throw(DomainError("side length must be postive")) |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
122 end |
|
11b08b242e48
Make equdistant_grid return an EquidistantGrid for the 1d Case
Jonatan Werpers <jonatan@werpers.com>
parents:
1280
diff
changeset
|
123 return EquidistantGrid(range(limit_lower, limit_upper, length=size)) # TBD: Should it use LinRange instead? |
|
1113
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
124 end |
|
4e4c5011140d
Add functions orthogonal_dims and boundary_size
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1111
diff
changeset
|
125 |
|
1280
17d435c08773
Add missing exports and alias TensorGridBoundary to CartesianBoundary
Jonatan Werpers <jonatan@werpers.com>
parents:
1279
diff
changeset
|
126 CartesianBoundary{D,BID} = TensorGridBoundary{D,BID} # TBD: What should we do about the naming of this boundary? |
|
1111
5b3d4a8ec3ab
Change to using filter for picking out orthogonal dimensions
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
909
diff
changeset
|
127 |
|
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
|
128 |
|
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 """ |
|
1347
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
130 change_length(r::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
|
131 |
|
1347
08f06bfacd5c
Fix typos and formatting of documentation
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
1344
diff
changeset
|
132 Change the length of `r` 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
|
133 """ |
|
1250
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
134 function change_length end |
| 908 | 135 |
|
1250
40ca0af6e480
Add methods to change_length and fix incorrect function declaration
Jonatan Werpers <jonatan@werpers.com>
parents:
1249
diff
changeset
|
136 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
|
137 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
|
138 change_length(r::StepRangeLen, n) = range(r[begin], r[end], n) |
| 1222 | 139 change_length(r::LinRange, n) = LinRange(r[begin], r[end], n) |
