changeset 1873:3dd453015f7d

Merge refactor/grids/iterable_boundary_indices
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 27 Jan 2025 09:18:50 +0100
parents 0d8d56eca0c8 (current diff) 3e07a90ef26f (diff)
children 610ac5ca5d43 9c2613880213
files
diffstat 5 files changed, 41 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/equidistant_grid.jl	Sat Jan 11 10:22:43 2025 +0100
+++ b/src/Grids/equidistant_grid.jl	Mon Jan 27 09:18:50 2025 +0100
@@ -71,8 +71,8 @@
 boundary_identifiers(::EquidistantGrid) = (LowerBoundary(), UpperBoundary())
 boundary_grid(g::EquidistantGrid, id::LowerBoundary) = ZeroDimGrid(g[begin])
 boundary_grid(g::EquidistantGrid, id::UpperBoundary) = ZeroDimGrid(g[end])
-boundary_indices(g::EquidistantGrid, id::LowerBoundary) = (firstindex(g),)
-boundary_indices(g::EquidistantGrid, id::UpperBoundary) = (lastindex(g),)
+boundary_indices(g::EquidistantGrid, id::LowerBoundary) = firstindex(g)
+boundary_indices(g::EquidistantGrid, id::UpperBoundary) = lastindex(g)
 
 """
     refine(g::EquidistantGrid, r::Int)
--- a/src/Grids/mapped_grid.jl	Sat Jan 11 10:22:43 2025 +0100
+++ b/src/Grids/mapped_grid.jl	Mon Jan 27 09:18:50 2025 +0100
@@ -82,8 +82,8 @@
     jacobian_components = (:, free_variable_indices)
 
     # Create grid function for boundary grid jacobian
-    boundary_jacobian = componentview((@view g.jacobian[b_indices...])  , jacobian_components...)
-    boundary_physicalcoordinates = @view g.physicalcoordinates[b_indices...]
+    boundary_jacobian = componentview((@view g.jacobian[b_indices])  , jacobian_components...)
+    boundary_physicalcoordinates = @view g.physicalcoordinates[b_indices]
 
     return MappedGrid(
         boundary_grid(g.logical_grid, id),
@@ -182,14 +182,8 @@
 The outward pointing normal as a grid function on the corresponding boundary grid.
 """
 function normal(g::MappedGrid, boundary)
-    b_indices = boundary_indices(g, boundary)
-    σ = _boundary_sign(component_type(g), boundary)
-
-    # TODO: Refactor this when `boundary_indices(g, ...)` has been made iterable.
-    return map(jacobian(g)[b_indices...]) do ∂x∂ξ
-        ∂ξ∂x = inv(∂x∂ξ)
-        k = grid_id(boundary)
-        σ*∂ξ∂x[k,:]/norm(∂ξ∂x[k,:])
+    return map(boundary_indices(g, boundary)) do I
+        normal(g, boundary, Tuple(I)...)
     end
 end
 
--- a/src/Grids/tensor_grid.jl	Sat Jan 11 10:22:43 2025 +0100
+++ b/src/Grids/tensor_grid.jl	Mon Jan 27 09:18:50 2025 +0100
@@ -94,15 +94,16 @@
     return TensorGrid(new_grids...)
 end
 
-function boundary_indices(g::TensorGrid, id::TensorGridBoundary)
-    per_grid_ind = map(g.grids) do g
-        ntuple(i->:, ndims(g))
-    end
 
+function boundary_indices(g::TensorGrid{<:Any, 1}, id::TensorGridBoundary)
+    return boundary_indices(g.grids[grid_id(id)], boundary_id(id))
+end
+function boundary_indices(g::TensorGrid, id::TensorGridBoundary)
     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))
 
-    return LazyTensors.concatenate_tuples(b_ind...)
+    b_ind = Base.setindex(map(eachindex, g.grids), local_b_ind, grid_id(id))
+
+    return view(eachindex(g), b_ind...)
 end
 
 function combined_coordinate_vector_type(coordinate_types...)
--- a/test/Grids/equidistant_grid_test.jl	Sat Jan 11 10:22:43 2025 +0100
+++ b/test/Grids/equidistant_grid_test.jl	Mon Jan 27 09:18:50 2025 +0100
@@ -74,13 +74,16 @@
 
     @testset "boundary_indices" begin
         g = EquidistantGrid(0:0.1:1)
-        @test boundary_indices(g, LowerBoundary()) == (1,)
-        @test boundary_indices(g, UpperBoundary()) == (11,)
+        @test boundary_indices(g, LowerBoundary()) == 1
+        @test boundary_indices(g, UpperBoundary()) == 11
+
+        gf = collect(g)
+        @test gf[boundary_indices(g, LowerBoundary())] == gf[1]
+        @test gf[boundary_indices(g, UpperBoundary())] == gf[11]
 
         g = EquidistantGrid(2:0.1:10)
-        @test boundary_indices(g, LowerBoundary()) == (1,)
-        @test boundary_indices(g, UpperBoundary()) == (81,)
-
+        @test boundary_indices(g, LowerBoundary()) == 1
+        @test boundary_indices(g, UpperBoundary()) == 81
     end
 
     @testset "refine" begin
--- a/test/Grids/tensor_grid_test.jl	Sat Jan 11 10:22:43 2025 +0100
+++ b/test/Grids/tensor_grid_test.jl	Mon Jan 27 09:18:50 2025 +0100
@@ -204,14 +204,26 @@
         g₂ = EquidistantGrid(range(2,3,length=6))
         g₄ = ZeroDimGrid(@SVector[1,2])
 
-        @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, LowerBoundary}()) == (1,:)
-        @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}()) == (11,:)
-        @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, LowerBoundary}()) == (:,1)
-        @test boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, UpperBoundary}()) == (:,6)
-        @test boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}()) == (1,)
-        @test boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}()) == (11,)
-        @test boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, LowerBoundary}()) == (1,)
-        @test boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, UpperBoundary}()) == (11,)
+        gf = reshape(1:(11*6),11,6)
+        @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, LowerBoundary}())] == gf[1,:]
+        @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}())] == gf[11,:]
+        @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, LowerBoundary}())] == gf[:,1]
+        @test gf[boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, UpperBoundary}())] == gf[:,6]
+
+        gf = rand(11)
+        @test gf[boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}())] == gf[1]
+        @test gf[boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}())] == gf[11]
+        @test gf[boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, LowerBoundary}())] == gf[1]
+        @test gf[boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, UpperBoundary}())] == gf[11]
+
+        @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, LowerBoundary}())) == [CartesianIndex(1,i) for i ∈ 1:6]
+        @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{1, UpperBoundary}())) == [CartesianIndex(11,i) for i ∈ 1:6]
+        @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, LowerBoundary}())) == [CartesianIndex(i,1) for i ∈ 1:11]
+        @test collect(boundary_indices(TensorGrid(g₁, g₂), TensorGridBoundary{2, UpperBoundary}())) == [CartesianIndex(i,6) for i ∈ 1:11]
+        @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}())) == fill(1)
+        @test collect(boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, UpperBoundary}())) == fill(11)
+        @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, LowerBoundary}())) == fill(1)
+        @test collect(boundary_indices(TensorGrid(g₄,g₁), TensorGridBoundary{2, UpperBoundary}())) == fill(11)
     end
 end