changeset 1474:276c38a48aac feature/grids/componentview

Start implementing componentview
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 01 Dec 2023 13:34:03 +0100
parents 869a40cda18c
children 76b190ca9a27
files src/Grids/Grids.jl src/Grids/grid.jl test/Grids/grid_test.jl
diffstat 3 files changed, 24 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/Grids.jl	Fri Dec 01 11:48:04 2023 +0100
+++ b/src/Grids/Grids.jl	Fri Dec 01 13:34:03 2023 +0100
@@ -13,10 +13,10 @@
 export boundary_indices
 export boundary_identifiers
 export boundary_grid
+export coarsen
+export refine
 export eval_on
-export coarsen
-export getcomponent
-export refine
+export componentview
 
 export BoundaryIdentifier
 export TensorGridBoundary
--- a/src/Grids/grid.jl	Fri Dec 01 11:48:04 2023 +0100
+++ b/src/Grids/grid.jl	Fri Dec 01 13:34:03 2023 +0100
@@ -37,8 +37,17 @@
 
 The type of the components of the coordinate vector of `Grid` `g`.
 """
-component_type(::Type{<:Grid{T}}) where T = eltype(T)
+component_type(::Type{<:Grid{T}}) where T = eltype(T) #TBD: removable?
 component_type(g::Grid) = component_type(typeof(g))
+component_type(T::Type) = eltype(eltype(T))
+component_type(t) = component_type(typeof(t))
+
+function componentview(gf, component_index...)
+    A = reinterpret(reshape, component_type(gf), gf)
+
+    component_linear_index = LinearIndices(first(gf))[component_index...]
+    return @view A[component_linear_index,:,:]
+end
 
 """
     refine(g::Grid, r)
--- a/test/Grids/grid_test.jl	Fri Dec 01 11:48:04 2023 +0100
+++ b/test/Grids/grid_test.jl	Fri Dec 01 13:34:03 2023 +0100
@@ -63,6 +63,17 @@
     @test eval_on(g, f) == map(x̄->f(x̄...), g)
 end
 
+@testset "componentview" begin
+    v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+
+    @test componentview(v, 1, 1) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+    @test componentview(v, 1, 2) == [3 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+    @test componentview(v, 2, 1) == [2 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+
+    # should work with colon
+    # should return a view which can be index like the grid function
+end
+
 @testset "_ncomponents" begin
     @test Grids._ncomponents(Int) == 1
     @test Grids._ncomponents(Float64) == 1