Mercurial > repos > public > sbplib_julia
comparison src/Grids/tensor_grid.jl @ 1222:5f677cd6f0b6 refactor/grids
Start refactoring
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sat, 18 Feb 2023 11:37:35 +0100 |
parents | |
children | 95e294576c2a |
comparison
equal
deleted
inserted
replaced
1221:b3b4d29b46c3 | 1222:5f677cd6f0b6 |
---|---|
1 struct TensorGrid{T,D,RD,GT<:NTuple{N,Grid} where N} <: Grid{T,D,RD} | |
2 grids::GT | |
3 | |
4 function TensorGrid(gs...) | |
5 T = eltype(gs[1]) # All gs should have the same T | |
6 D = sum(ndims,gs) | |
7 RD = sum(nrangedims, gs) | |
8 | |
9 return new{T,D,RD,typeof(gs)}(gs) | |
10 end | |
11 end | |
12 | |
13 function Base.size(g::TensorGrid) | |
14 return concatenate_tuples(size.(g.grids)...) | |
15 end | |
16 | |
17 function Base.getindex(g::TensorGrid, I...) | |
18 szs = ndims.(g.grids) | |
19 | |
20 Is = split_tuple(I, szs) | |
21 ps = map((g,I)->SVector(g[I...]), g.grids, Is) | |
22 | |
23 return vcat(ps...) | |
24 end | |
25 | |
26 IndexStyle(::TensorGrid) = IndexCartesian() | |
27 | |
28 function Base.eachindex(g::TensorGrid) | |
29 szs = concatenate_tuples(size.(g.grids)...) | |
30 return CartesianIndices(szs) | |
31 end | |
32 | |
33 | |
34 | |
35 ## Pre refactor code: | |
36 """ | |
37 orthogonal_dims(grid::EquidistantGrid,dim) | |
38 | |
39 Returns the dimensions of grid orthogonal to that of dim. | |
40 """ | |
41 function orthogonal_dims(grid::EquidistantGrid, dim) | |
42 orth_dims = filter(i -> i != dim, dims(grid)) | |
43 if orth_dims == dims(grid) | |
44 throw(DomainError(string("dimension ",string(dim)," not matching grid"))) | |
45 end | |
46 return orth_dims | |
47 end | |
48 | |
49 """ | |
50 restrict(::EquidistantGrid, dim) | |
51 | |
52 Pick out given dimensions from the grid and return a grid for them. | |
53 """ | |
54 function restrict(grid::EquidistantGrid, dim) | |
55 size = grid.size[dim] | |
56 limit_lower = grid.limit_lower[dim] | |
57 limit_upper = grid.limit_upper[dim] | |
58 | |
59 return EquidistantGrid(size, limit_lower, limit_upper) | |
60 end | |
61 | |
62 | |
63 | |
64 """ | |
65 boundary_identifiers(::EquidistantGrid) | |
66 | |
67 Returns a tuple containing the boundary identifiers for the grid, stored as | |
68 (CartesianBoundary(1,Lower), | |
69 CartesianBoundary(1,Upper), | |
70 CartesianBoundary(2,Lower), | |
71 ...) | |
72 """ | |
73 boundary_identifiers(g::EquidistantGrid) = (((ntuple(i->(CartesianBoundary{i,Lower}(),CartesianBoundary{i,Upper}()),ndims(g)))...)...,) | |
74 | |
75 | |
76 """ | |
77 boundary_grid(grid::EquidistantGrid, id::CartesianBoundary) | |
78 | |
79 Creates the lower-dimensional restriciton of `grid` spanned by the dimensions | |
80 orthogonal to the boundary specified by `id`. The boundary grid of a 1-dimensional | |
81 grid is a zero-dimensional grid. | |
82 """ | |
83 function boundary_grid(grid::EquidistantGrid, id::CartesianBoundary) | |
84 orth_dims = orthogonal_dims(grid, dim(id)) | |
85 return restrict(grid, orth_dims) | |
86 end | |
87 boundary_grid(::EquidistantGrid{1,T},::CartesianBoundary{1}) where T = EquidistantGrid{T}() |