Mercurial > repos > public > sbplib_julia
comparison src/Grids/tensor_grid.jl @ 1236:95e294576c2a refactor/grids
Implement boundary methods for TensorGrid
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sun, 19 Feb 2023 22:50:02 +0100 |
parents | 5f677cd6f0b6 |
children | 6f75f2d2bf5c |
comparison
equal
deleted
inserted
replaced
1235:d58015e224ca | 1236:95e294576c2a |
---|---|
29 szs = concatenate_tuples(size.(g.grids)...) | 29 szs = concatenate_tuples(size.(g.grids)...) |
30 return CartesianIndices(szs) | 30 return CartesianIndices(szs) |
31 end | 31 end |
32 | 32 |
33 | 33 |
34 struct TensorBoundary{N, BID<:BoundaryIdentifier} <: BoundaryIdentifier end | |
35 grid_id(::TensorBoundary{N, BID}) where {N, BID} = N | |
36 boundary_id(::TensorBoundary{N, BID}) where {N, BID} = BID() | |
34 | 37 |
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 | 38 |
49 """ | 39 """ |
50 restrict(::EquidistantGrid, dim) | 40 boundary_identifiers(::TensorGrid) |
51 | 41 |
52 Pick out given dimensions from the grid and return a grid for them. | 42 Returns a tuple containing the boundary identifiers for the grid. |
53 """ | 43 """ |
54 function restrict(grid::EquidistantGrid, dim) | 44 function boundary_identifiers(g::TensorGrid) |
55 size = grid.size[dim] | 45 n = length(g.grids) |
56 limit_lower = grid.limit_lower[dim] | 46 per_grid = map(eachindex(g.grids)) do i |
57 limit_upper = grid.limit_upper[dim] | 47 return map(bid -> TensorBoundary{i, bid}(), boundary_identifiers(g.grids[i])) |
58 | 48 end |
59 return EquidistantGrid(size, limit_lower, limit_upper) | 49 return concatenate_tuples(per_grid...) |
60 end | 50 end |
61 | 51 |
62 | 52 |
53 """ | |
54 boundary_grid(grid::TensorGrid, id::TensorBoundary) | |
63 | 55 |
56 The grid for the boundary specified by `id`. | |
64 """ | 57 """ |
65 boundary_identifiers(::EquidistantGrid) | 58 function boundary_grid(g::TensorGrid, bid::TensorBoundary) |
66 | 59 local_boundary_grid = boundary_grid(g.grids[grid_id(bid)], boundary_id(bid)) |
67 Returns a tuple containing the boundary identifiers for the grid, stored as | 60 new_grids = Base.setindex(g.grids, local_boundary_grid, grid_id(bid)) |
68 (CartesianBoundary(1,Lower), | 61 return TensorGrid(new_grids...) |
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 | 62 end |
87 boundary_grid(::EquidistantGrid{1,T},::CartesianBoundary{1}) where T = EquidistantGrid{T}() |