Mercurial > repos > public > sbplib_julia
comparison src/Grids/mapped_grid.jl @ 1774:035af82f559a feature/grids/curvilinear
Rename logicalgrid to logical_grid
| author | Jonatan Werpers <jonatan@werpers.com> |
|---|---|
| date | Sun, 15 Sep 2024 18:03:37 +0200 |
| parents | 08e52f442872 |
| children | ecec2b0eea0f |
comparison
equal
deleted
inserted
replaced
| 1773:08e52f442872 | 1774:035af82f559a |
|---|---|
| 3 | 3 |
| 4 A grid defined by a coordinate mapping from a logical grid to some physical | 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 | 5 coordinates. The physical coordinates and the Jacobian are stored as grid |
| 6 functions corresponding to the logical grid. | 6 functions corresponding to the logical grid. |
| 7 | 7 |
| 8 See also: [`logicalgrid`](@ref), [`jacobian`](@ref), [`metric_tensor`](@ref). | 8 See also: [`logical_grid`](@ref), [`jacobian`](@ref), [`metric_tensor`](@ref). |
| 9 """ | 9 """ |
| 10 struct MappedGrid{T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractMatrix{<:Any}, D}} <: Grid{T,D} | 10 struct MappedGrid{T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractMatrix{<:Any}, D}} <: Grid{T,D} |
| 11 logicalgrid::GT | 11 logical_grid::GT |
| 12 physicalcoordinates::CT | 12 physicalcoordinates::CT |
| 13 jacobian::JT | 13 jacobian::JT |
| 14 | 14 |
| 15 """ | 15 """ |
| 16 MappedGrid(logicalgrid, physicalcoordinates, jacobian) | 16 MappedGrid(logical_grid, physicalcoordinates, jacobian) |
| 17 | 17 |
| 18 A MappedGrid with the given physical coordinates and jacobian. | 18 A MappedGrid with the given physical coordinates and jacobian. |
| 19 """ | 19 """ |
| 20 function MappedGrid(logicalgrid::GT, physicalcoordinates::CT, jacobian::JT) where {T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractMatrix{<:Any}, D}} | 20 function MappedGrid(logical_grid::GT, physicalcoordinates::CT, jacobian::JT) where {T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractMatrix{<:Any}, D}} |
| 21 if !(size(logicalgrid) == size(physicalcoordinates) == size(jacobian)) | 21 if !(size(logical_grid) == size(physicalcoordinates) == size(jacobian)) |
| 22 throw(ArgumentError("Sizes must match")) | 22 throw(ArgumentError("Sizes must match")) |
| 23 end | 23 end |
| 24 | 24 |
| 25 if size(first(jacobian)) != (length(first(physicalcoordinates)),D) | 25 if size(first(jacobian)) != (length(first(physicalcoordinates)),D) |
| 26 throw(ArgumentError("The size of the jacobian must match the dimensions of the grid and coordinates")) | 26 throw(ArgumentError("The size of the jacobian must match the dimensions of the grid and coordinates")) |
| 27 end | 27 end |
| 28 | 28 |
| 29 return new{T,D,GT,CT,JT}(logicalgrid, physicalcoordinates, jacobian) | 29 return new{T,D,GT,CT,JT}(logical_grid, physicalcoordinates, jacobian) |
| 30 end | 30 end |
| 31 end | 31 end |
| 32 | 32 |
| 33 function Base.:(==)(a::MappedGrid, b::MappedGrid) | 33 function Base.:(==)(a::MappedGrid, b::MappedGrid) |
| 34 same_logicalgrid = logicalgrid(a) == logicalgrid(b) | 34 same_logical_grid = logical_grid(a) == logical_grid(b) |
| 35 same_coordinates = collect(a) == collect(b) | 35 same_coordinates = collect(a) == collect(b) |
| 36 same_jacobian = jacobian(a) == jacobian(b) | 36 same_jacobian = jacobian(a) == jacobian(b) |
| 37 | 37 |
| 38 return same_logicalgrid && same_coordinates && same_jacobian | 38 return same_logical_grid && same_coordinates && same_jacobian |
| 39 end | 39 end |
| 40 | 40 |
| 41 # Review: rename function logicalgrid to logical_grid | 41 """ |
| 42 # for consistency with mapped_grid. | 42 logical_grid(g::MappedGrid) |
| 43 """ | |
| 44 logicalgrid(g::MappedGrid) | |
| 45 | 43 |
| 46 The logical grid of `g`. | 44 The logical grid of `g`. |
| 47 """ | 45 """ |
| 48 logicalgrid(g::MappedGrid) = g.logicalgrid | 46 logical_grid(g::MappedGrid) = g.logical_grid |
| 49 | 47 |
| 50 """ | 48 """ |
| 51 jacobian(g::MappedGrid) | 49 jacobian(g::MappedGrid) |
| 52 | 50 |
| 53 The Jacobian matrix of `g` as a grid function. | 51 The Jacobian matrix of `g` as a grid function. |
| 55 jacobian(g::MappedGrid) = g.jacobian | 53 jacobian(g::MappedGrid) = g.jacobian |
| 56 | 54 |
| 57 | 55 |
| 58 # Indexing interface | 56 # Indexing interface |
| 59 Base.getindex(g::MappedGrid, I::Vararg{Int}) = g.physicalcoordinates[I...] | 57 Base.getindex(g::MappedGrid, I::Vararg{Int}) = g.physicalcoordinates[I...] |
| 60 Base.eachindex(g::MappedGrid) = eachindex(g.logicalgrid) | 58 Base.eachindex(g::MappedGrid) = eachindex(g.logical_grid) |
| 61 | 59 |
| 62 Base.firstindex(g::MappedGrid, d) = firstindex(g.logicalgrid, d) | 60 Base.firstindex(g::MappedGrid, d) = firstindex(g.logical_grid, d) |
| 63 Base.lastindex(g::MappedGrid, d) = lastindex(g.logicalgrid, d) | 61 Base.lastindex(g::MappedGrid, d) = lastindex(g.logical_grid, d) |
| 64 | 62 |
| 65 # Iteration interface | 63 # Iteration interface |
| 66 Base.iterate(g::MappedGrid) = iterate(g.physicalcoordinates) | 64 Base.iterate(g::MappedGrid) = iterate(g.physicalcoordinates) |
| 67 Base.iterate(g::MappedGrid, state) = iterate(g.physicalcoordinates, state) | 65 Base.iterate(g::MappedGrid, state) = iterate(g.physicalcoordinates, state) |
| 68 | 66 |
| 69 Base.IteratorSize(::Type{<:MappedGrid{<:Any, D}}) where D = Base.HasShape{D}() | 67 Base.IteratorSize(::Type{<:MappedGrid{<:Any, D}}) where D = Base.HasShape{D}() |
| 70 Base.length(g::MappedGrid) = length(g.logicalgrid) | 68 Base.length(g::MappedGrid) = length(g.logical_grid) |
| 71 Base.size(g::MappedGrid) = size(g.logicalgrid) | 69 Base.size(g::MappedGrid) = size(g.logical_grid) |
| 72 Base.size(g::MappedGrid, d) = size(g.logicalgrid, d) | 70 Base.size(g::MappedGrid, d) = size(g.logical_grid, d) |
| 73 | 71 |
| 74 boundary_identifiers(g::MappedGrid) = boundary_identifiers(g.logicalgrid) | 72 boundary_identifiers(g::MappedGrid) = boundary_identifiers(g.logical_grid) |
| 75 boundary_indices(g::MappedGrid, id::TensorGridBoundary) = boundary_indices(g.logicalgrid, id) | 73 boundary_indices(g::MappedGrid, id::TensorGridBoundary) = boundary_indices(g.logical_grid, id) |
| 76 | 74 |
| 77 # Review: Error when calling plot(boundary_grid(g, id)) | 75 # Review: Error when calling plot(boundary_grid(g, id)) |
| 78 # Currently need to collect first, i.e., plot(collect(boundary_grid(g, id))) | 76 # Currently need to collect first, i.e., plot(collect(boundary_grid(g, id))) |
| 79 function boundary_grid(g::MappedGrid, id::TensorGridBoundary) | 77 function boundary_grid(g::MappedGrid, id::TensorGridBoundary) |
| 80 b_indices = boundary_indices(g.logicalgrid, id) | 78 b_indices = boundary_indices(g.logical_grid, id) |
| 81 | 79 |
| 82 # Calculate indices of needed jacobian components | 80 # Calculate indices of needed jacobian components |
| 83 D = ndims(g) | 81 D = ndims(g) |
| 84 all_indices = SVector{D}(1:D) | 82 all_indices = SVector{D}(1:D) |
| 85 free_variable_indices = deleteat(all_indices, grid_id(id)) | 83 free_variable_indices = deleteat(all_indices, grid_id(id)) |
| 88 # Create grid function for boundary grid jacobian | 86 # Create grid function for boundary grid jacobian |
| 89 boundary_jacobian = componentview((@view g.jacobian[b_indices...]) , jacobian_components...) | 87 boundary_jacobian = componentview((@view g.jacobian[b_indices...]) , jacobian_components...) |
| 90 boundary_physicalcoordinates = @view g.physicalcoordinates[b_indices...] | 88 boundary_physicalcoordinates = @view g.physicalcoordinates[b_indices...] |
| 91 | 89 |
| 92 return MappedGrid( | 90 return MappedGrid( |
| 93 boundary_grid(g.logicalgrid, id), | 91 boundary_grid(g.logical_grid, id), |
| 94 boundary_physicalcoordinates, | 92 boundary_physicalcoordinates, |
| 95 boundary_jacobian, | 93 boundary_jacobian, |
| 96 ) | 94 ) |
| 97 end | 95 end |
| 98 | 96 |
