changeset 1827:ab397590898e refactor/grids/iterable_boundary_indices

Try to change the implementation
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 17 Sep 2024 11:19:08 +0200
parents b0c019acb50b
children 8adecef380b4
files src/Grids/tensor_grid.jl test/Grids/tensor_grid_test.jl
diffstat 2 files changed, 27 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/tensor_grid.jl	Tue Sep 17 09:12:52 2024 +0200
+++ b/src/Grids/tensor_grid.jl	Tue Sep 17 11:19:08 2024 +0200
@@ -84,17 +84,28 @@
     return TensorGrid(new_grids...)
 end
 
+
+function boundary_indices(g::TensorGrid{T,1} where T, id::TensorGridBoundary)
+    return boundary_indices(g.grids[grid_id(id)], boundary_id(id))
+end
 function boundary_indices(g::TensorGrid, id::TensorGridBoundary)
-    per_grid_ind = map(g.grids) do g
-        ntuple(i->:, ndims(g))
-    end
+    all_indices = map(eachindex, g.grids)
 
     local_b_ind = boundary_indices(g.grids[grid_id(id)], boundary_id(id))
-    b_ind = Base.setindex(per_grid_ind, local_b_ind, grid_id(id))
+
+    b_ind = Base.setindex(all_indices, local_b_ind, grid_id(id))
+
+    return view(_combine_indices(all_indices...), LazyTensors.concatenate_tuples(bla.(b_ind)...)...)
+end
 
-    return LazyTensors.concatenate_tuples(b_ind...)
+# function _combine_indices(Is::Vararg{Union{Int, <:AbstractRange}})
+function _combine_indices(Is...)
+    return CartesianIndices(LazyTensors.concatenate_tuples(bla.(Is)...))
 end
 
+bla(a) = (a,)
+bla(a::CartesianIndices) = a.indices
+
 function combined_coordinate_vector_type(coordinate_types...)
     combined_coord_length = mapreduce(_ncomponents, +, coordinate_types)
     combined_coord_type = mapreduce(eltype, promote_type, coordinate_types)
--- a/test/Grids/tensor_grid_test.jl	Tue Sep 17 09:12:52 2024 +0200
+++ b/test/Grids/tensor_grid_test.jl	Tue Sep 17 11:19:08 2024 +0200
@@ -181,14 +181,21 @@
         @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, Lower}())] == gf[:,1]
         @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, Upper}())] == gf[:,6]
 
+        gf = rand(11)
+        @show boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Lower}())
+        @test gf[boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Lower}())] == gf[1]
+        @test gf[boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Upper}())] == gf[11]
+        @test gf[boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, Lower}())]== gf[1]
+        @test gf[boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, Upper}())]== gf[11]
+
         @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, Lower}())) == [CartesianIndex(1,i) for i ∈ 1:6]
         @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, Upper}())) == [CartesianIndex(11,i) for i ∈ 1:6]
         @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, Lower}())) == [CartesianIndex(i,1) for i ∈ 1:11]
         @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, Upper}())) == [CartesianIndex(i,6) for i ∈ 1:11]
-        @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Lower}())) == 1
-        @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Upper}())) == 11
-        @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, Lower}())) == 1
-        @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, Upper}())) == 11
+        @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Lower}())) == fill(1)
+        @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, Upper}())) == fill(11)
+        @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, Lower}())) == fill(1)
+        @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, Upper}())) == fill(11)
     end
 end