changeset 1524:b2b9693aa63b

Merge feature/grids/componentview
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 22 Mar 2024 08:59:36 +0100
parents 97af5489dcf0 (current diff) facbaa741e3d (diff)
children 2cee20ecc0bc
files
diffstat 3 files changed, 89 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/Grids.jl	Fri Mar 22 08:48:00 2024 +0100
+++ b/src/Grids/Grids.jl	Fri Mar 22 08:59:36 2024 +0100
@@ -13,10 +13,11 @@
 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 ArrayComponentView
 
 export BoundaryIdentifier
 export TensorGridBoundary
--- a/src/Grids/grid.jl	Fri Mar 22 08:48:00 2024 +0100
+++ b/src/Grids/grid.jl	Fri Mar 22 08:59:36 2024 +0100
@@ -49,6 +49,40 @@
 component_type(t) = component_type(typeof(t))
 
 """
+    componentview(gf, component_index...)
+
+A view of `gf` with only the components specified by `component_index...`.
+
+# Examples
+```julia-repl
+julia> componentview([[1,2], [2,3], [3,4]],2)
+3-element ArrayComponentView{Int64, Vector{Int64}, 1, Vector{Vector{Int64}}, Tuple{Int64}}:
+ 2
+ 3
+ 4
+```
+"""
+componentview(gf, component_index...) = ArrayComponentView(gf, component_index)
+
+struct ArrayComponentView{CT,T,D,AT <: AbstractArray{T,D}, IT} <: AbstractArray{CT,D}
+    v::AT
+    component_index::IT
+
+    function ArrayComponentView(v, component_index)
+        CT = typeof(first(v)[component_index...])
+        return new{CT, eltype(v), ndims(v), typeof(v), typeof(component_index)}(v,component_index)
+    end
+end
+
+Base.size(cv) = size(cv.v)
+Base.getindex(cv::ArrayComponentView, i::Int) = cv.v[i][cv.component_index...]
+Base.getindex(cv::ArrayComponentView, I::Vararg{Int}) = cv.v[I...][cv.component_index...]
+IndexStyle(::Type{<:ArrayComponentView{<:Any,<:Any,AT}}) where AT = IndexStyle(AT)
+
+# TODO: Implement `setindex!`?
+# TODO: Implement a more general ComponentView that can handle non-AbstractArrays.
+
+"""
     refine(g::Grid, r)
 
 The grid where `g` is refined by the factor `r`.
--- a/test/Grids/grid_test.jl	Fri Mar 22 08:48:00 2024 +0100
+++ b/test/Grids/grid_test.jl	Fri Mar 22 08:59:36 2024 +0100
@@ -65,6 +65,57 @@
     @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) isa AbstractArray
+    @test componentview(v, 1, :) isa AbstractArray
+
+    A = @SMatrix[
+            1 4 7;
+            2 5 8;
+            3 6 9;
+        ]
+    v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+    @test componentview(v, 2:3, 1:2) isa AbstractArray
+
+    # Correctness of the result is tested in ArrayComponentView
+end
+
+@testset "ArrayComponentView" begin
+    v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+
+    @testset "==" begin
+        @test ArrayComponentView(v, (1,1)) == ArrayComponentView(v, (1,1))
+        @test ArrayComponentView(v, (1,1)) == ArrayComponentView(copy(v), (1,1))
+        @test ArrayComponentView(v, (1,1)) == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4] == ArrayComponentView(v, (1,1))
+    end
+
+    @testset "components" begin
+        v = [@SMatrix[1 3; 2 4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+
+        @test ArrayComponentView(v, (1, 1))  == [1 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (1, 2))  == [3 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (2, 1))  == [2 .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+
+        @test ArrayComponentView(v, (1, :))  == [@SVector[1,3] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (2, :))  == [@SVector[2,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (:, 1))  == [@SVector[1,2] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (:, 2))  == [@SVector[3,4] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+
+
+        A = @SMatrix[
+            1 4 7;
+            2 5 8;
+            3 6 9;
+        ]
+        v = [A .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (1:2, 1:2)) == [@SMatrix[1 4;2 5] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+        @test ArrayComponentView(v, (2:3, 1:2)) == [@SMatrix[2 5;3 6] .+ 100*i .+ 10*j for i ∈ 1:3, j∈ 1:4]
+    end
+end
+
 @testset "_ncomponents" begin
     @test Grids._ncomponents(Int) == 1
     @test Grids._ncomponents(Float64) == 1