Mercurial > repos > public > sbplib_julia
changeset 1829:871f3f1decea refactor/grids/iterable_boundary_indices
Merge default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Sun, 20 Oct 2024 21:38:09 +0200 |
parents | 8adecef380b4 (diff) 9b47cdbbba75 (current diff) |
children | 805b9b7fcc39 |
files | src/Diffinitive.jl src/Grids/equidistant_grid.jl src/Grids/tensor_grid.jl test/Grids/equidistant_grid_test.jl test/Grids/tensor_grid_test.jl |
diffstat | 3 files changed, 47 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
diff -r 9b47cdbbba75 -r 871f3f1decea src/Grids/tensor_grid.jl --- a/src/Grids/tensor_grid.jl Fri Oct 18 21:18:58 2024 +0200 +++ b/src/Grids/tensor_grid.jl Sun Oct 20 21:38:09 2024 +0200 @@ -90,17 +90,29 @@ 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 +# TODO: There must be a way to make the above code cleaner? - 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)
diff -r 9b47cdbbba75 -r 871f3f1decea test/Grids/equidistant_grid_test.jl --- a/test/Grids/equidistant_grid_test.jl Fri Oct 18 21:18:58 2024 +0200 +++ b/test/Grids/equidistant_grid_test.jl Sun Oct 20 21:38:09 2024 +0200 @@ -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
diff -r 9b47cdbbba75 -r 871f3f1decea test/Grids/tensor_grid_test.jl --- a/test/Grids/tensor_grid_test.jl Fri Oct 18 21:18:58 2024 +0200 +++ b/test/Grids/tensor_grid_test.jl Sun Oct 20 21:38:09 2024 +0200 @@ -183,14 +183,28 @@ 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) + @show boundary_indices(TensorGrid(g₁, g₄), TensorGridBoundary{1, LowerBoundary}()) + @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) + # TBD: What do we actually expect for 1D grids? end end