Mercurial > repos > public > sbplib_julia
comparison src/Grids/mapped_grid.jl @ 1746:44faa334adc6 feature/grids/curvilinear
Add docs
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Wed, 11 Sep 2024 14:41:14 +0200 |
| parents | 2f7974367cd3 |
| children | 03894fd7b132 e4353d5e8fc3 |
comparison
equal
deleted
inserted
replaced
| 1745:2f7974367cd3 | 1746:44faa334adc6 |
|---|---|
| 1 """ | |
| 2 MappedGrid{T,D} <: Grid{T,D} | |
| 3 | |
| 4 A grid defined by a coordinate mapping from a logical grid to some physical | |
| 5 coordinates. The physical coordinates and the Jacobian are stored as grid | |
| 6 functions corresponding to the logical grid. | |
| 7 | |
| 8 See also: [`logicalgrid`](@ref), [`jacobian`](@ref), [`metric_tensor`](@ref). | |
| 9 """ | |
| 1 struct MappedGrid{T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractArray{<:Any, 2}, D}} <: Grid{T,D} | 10 struct MappedGrid{T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractArray{<:Any, 2}, D}} <: Grid{T,D} |
| 2 logicalgrid::GT | 11 logicalgrid::GT |
| 3 physicalcoordinates::CT | 12 physicalcoordinates::CT |
| 4 jacobian::JT | 13 jacobian::JT |
| 5 | 14 |
| 15 """ | |
| 16 MappedGrid(logicalgrid, physicalcoordinates, jacobian) | |
| 17 | |
| 18 A MappedGrid with the given physical coordinates and jacobian. | |
| 19 """ | |
| 6 function MappedGrid(logicalgrid::GT, physicalcoordinates::CT, jacobian::JT) where {T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractArray{<:Any, 2}, D}} | 20 function MappedGrid(logicalgrid::GT, physicalcoordinates::CT, jacobian::JT) where {T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractArray{<:Any, 2}, D}} |
| 7 if !(size(logicalgrid) == size(physicalcoordinates) == size(jacobian)) | 21 if !(size(logicalgrid) == size(physicalcoordinates) == size(jacobian)) |
| 8 throw(ArgumentError("Sizes must match")) | 22 throw(ArgumentError("Sizes must match")) |
| 9 end | 23 end |
| 10 | 24 |
| 24 same_jacobian = jacobian(a) == jacobian(b) | 38 same_jacobian = jacobian(a) == jacobian(b) |
| 25 | 39 |
| 26 return same_logicalgrid && same_coordinates && same_jacobian | 40 return same_logicalgrid && same_coordinates && same_jacobian |
| 27 end | 41 end |
| 28 | 42 |
| 43 """ | |
| 44 logicalgrid(g::MappedGrid) | |
| 45 | |
| 46 The logical grid of `g`. | |
| 47 """ | |
| 48 logicalgrid(g::MappedGrid) = g.logicalgrid | |
| 49 | |
| 50 """ | |
| 51 jacobian(g::MappedGrid) | |
| 52 | |
| 53 The Jacobian matrix of `g` as a grid function. | |
| 54 """ | |
| 29 jacobian(g::MappedGrid) = g.jacobian | 55 jacobian(g::MappedGrid) = g.jacobian |
| 30 logicalgrid(g::MappedGrid) = g.logicalgrid | |
| 31 | 56 |
| 32 | 57 |
| 33 # Indexing interface | 58 # Indexing interface |
| 34 Base.getindex(g::MappedGrid, I::Vararg{Int}) = g.physicalcoordinates[I...] | 59 Base.getindex(g::MappedGrid, I::Vararg{Int}) = g.physicalcoordinates[I...] |
| 35 Base.eachindex(g::MappedGrid) = eachindex(g.logicalgrid) | 60 Base.eachindex(g::MappedGrid) = eachindex(g.logicalgrid) |
| 67 boundary_physicalcoordinates, | 92 boundary_physicalcoordinates, |
| 68 boundary_jacobian, | 93 boundary_jacobian, |
| 69 ) | 94 ) |
| 70 end | 95 end |
| 71 | 96 |
| 72 # TBD: refine and coarsen could be implemented once we have a simple manifold implementation. | 97 |
| 73 # Before we do, we should consider the overhead of including such a field in the mapped grid struct. | 98 """ |
| 74 | 99 mapped_grid(x, J, size::Vararg{Int}) |
| 100 | |
| 101 A `MappedGrid` with a default logical grid on a unit hyper box. `x` and `J` | |
| 102 are functions to be evaluated on the logical grid and `size` determines the | |
| 103 size of the logical grid. | |
| 104 """ | |
| 75 function mapped_grid(x, J, size::Vararg{Int}) | 105 function mapped_grid(x, J, size::Vararg{Int}) |
| 76 D = length(size) | 106 D = length(size) |
| 77 lg = equidistant_grid(ntuple(i->0., D), ntuple(i->1., D), size...) | 107 lg = equidistant_grid(ntuple(i->0., D), ntuple(i->1., D), size...) |
| 78 return mapped_grid(lg, x, J) | 108 return mapped_grid(lg, x, J) |
| 79 end | 109 end |
| 80 | 110 |
| 111 """ | |
| 112 mapped_grid(lg::Grid, x, J) | |
| 113 | |
| 114 A `MappedGrid` with logical grid `lg`. Physical coordinates and Jacobian are | |
| 115 determined by the functions `x` and `J`. | |
| 116 """ | |
| 81 function mapped_grid(lg::Grid, x, J) | 117 function mapped_grid(lg::Grid, x, J) |
| 82 return MappedGrid( | 118 return MappedGrid( |
| 83 lg, | 119 lg, |
| 84 map(x,lg), | 120 map(x,lg), |
| 85 map(J,lg), | 121 map(J,lg), |
| 86 ) | 122 ) |
| 87 end | 123 end |
| 88 | 124 |
| 125 """ | |
| 126 jacobian_determinant(g::MappedGrid) | |
| 127 | |
| 128 The jacobian determinant of `g` as a grid function. | |
| 129 """ | |
| 89 function jacobian_determinant(g::MappedGrid) | 130 function jacobian_determinant(g::MappedGrid) |
| 90 return map(jacobian(g)) do ∂x∂ξ | 131 return map(jacobian(g)) do ∂x∂ξ |
| 91 det(∂x∂ξ) | 132 det(∂x∂ξ) |
| 92 end | 133 end |
| 93 end | 134 end |
| 94 # TBD: Should this be changed to calculate sqrt(g) instead? | 135 # TBD: Should this be changed to calculate sqrt(g) instead? |
| 95 # This would make it well defined also for n-dim grids embedded in higher dimensions. | 136 # This would make it well defined also for n-dim grids embedded in higher dimensions. |
| 96 # TBD: Is there a better name? metric_determinant? | 137 # TBD: Is there a better name? metric_determinant? |
| 97 # TBD: Is the best option to delete it? | 138 # TBD: Is the best option to delete it? |
| 98 | 139 |
| 140 """ | |
| 141 metric_tensor(g::MappedGrid) | |
| 142 | |
| 143 The metric tensor of `g` as a grid function. | |
| 144 """ | |
| 99 function metric_tensor(g::MappedGrid) | 145 function metric_tensor(g::MappedGrid) |
| 100 return map(jacobian(g)) do ∂x∂ξ | 146 return map(jacobian(g)) do ∂x∂ξ |
| 101 ∂x∂ξ'*∂x∂ξ | 147 ∂x∂ξ'*∂x∂ξ |
| 102 end | 148 end |
| 103 end | 149 end |
| 104 | 150 |
| 151 """ | |
| 152 metric_tensor_inverse(g::MappedGrid) | |
| 153 | |
| 154 The inverse of the metric tensor of `g` as a grid function. | |
| 155 """ | |
| 105 function metric_tensor_inverse(g::MappedGrid) | 156 function metric_tensor_inverse(g::MappedGrid) |
| 106 return map(jacobian(g)) do ∂x∂ξ | 157 return map(jacobian(g)) do ∂x∂ξ |
| 107 inv(∂x∂ξ'*∂x∂ξ) | 158 inv(∂x∂ξ'*∂x∂ξ) |
| 108 end | 159 end |
| 109 end | 160 end |
| 143 end | 194 end |
| 144 | 195 |
| 145 """ | 196 """ |
| 146 normal(g::MappedGrid, boundary) | 197 normal(g::MappedGrid, boundary) |
| 147 | 198 |
| 148 The outward pointing normal as a grid function on the boundary | 199 The outward pointing normal as a grid function on the corresponding boundary grid. |
| 149 """ | 200 """ |
| 150 function normal(g::MappedGrid, boundary) | 201 function normal(g::MappedGrid, boundary) |
| 151 b_indices = boundary_indices(g, boundary) | 202 b_indices = boundary_indices(g, boundary) |
| 152 σ =_boundary_sign(component_type(g), boundary) | 203 σ =_boundary_sign(component_type(g), boundary) |
| 153 return map(jacobian(g)[b_indices...]) do ∂x∂ξ | 204 return map(jacobian(g)[b_indices...]) do ∂x∂ξ |
