changeset 1432:64b60b42d367 feature/grids/curvilinear

Implement indexing interface
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 28 Aug 2023 09:30:34 +0200
parents 6adf31ba6cfd
children 1656228095b5
files src/Grids/curvilinear_grid.jl test/Grids/curvilinear_grid_test.jl
diffstat 2 files changed, 47 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
diff -r 6adf31ba6cfd -r 64b60b42d367 src/Grids/curvilinear_grid.jl
--- a/src/Grids/curvilinear_grid.jl	Fri Aug 25 15:41:19 2023 +0200
+++ b/src/Grids/curvilinear_grid.jl	Mon Aug 28 09:30:34 2023 +0200
@@ -9,7 +9,11 @@
 
 
 # Indexing interface
-Base.getindex(g::CurvilinearGrid, I...) = g.physicalcoordinates[I...]
+Base.getindex(g::CurvilinearGrid, I::Vararg{Int}) = g.physicalcoordinates[I...]
+Base.eachindex(g::CurvilinearGrid) = eachindex(g.logicalgrid)
+
+Base.firstindex(g::CurvilinearGrid, d) = firstindex(g.logicalgrid, d)
+Base.lastindex(g::CurvilinearGrid, d) = lastindex(g.logicalgrid, d)
 
 # function Base.getindex(g::TensorGrid, I...)
 #     szs = ndims.(g.grids)
diff -r 6adf31ba6cfd -r 64b60b42d367 test/Grids/curvilinear_grid_test.jl
--- a/test/Grids/curvilinear_grid_test.jl	Fri Aug 25 15:41:19 2023 +0200
+++ b/test/Grids/curvilinear_grid_test.jl	Mon Aug 28 09:30:34 2023 +0200
@@ -3,29 +3,57 @@
 using StaticArrays
 
 @testset "CurvilinearGrid" begin
-    g = equidistant_grid((10,10), (0,0), (1,1))
-    x̄ = map(ξ̄ -> 2ξ̄, g)
-    J = map(ξ̄ -> @SArray(fill(2., 2, 2)), g)
+    lg = equidistant_grid((11,11), (0,0), (1,1))
+    x̄ = map(ξ̄ -> 2ξ̄, lg)
+    J = map(ξ̄ -> @SArray(fill(2., 2, 2)), lg)
+    cg = CurvilinearGrid(lg, x̄, J)
 
-    @test CurvilinearGrid(g, x̄, J) isa Grid{SVector{2, Float64},2}
+    @test cg isa Grid{SVector{2, Float64},2}
 
-    cg = CurvilinearGrid(g, x̄, J)
     @test jacobian(cg) isa Array{<:AbstractMatrix}
     @test logicalgrid(cg) isa Grid
 
+    @testset "Indexing Interface" begin
+        cg = CurvilinearGrid(lg, x̄, J)
+        @test cg[1,1] == [0.0, 0.0]
+        @test cg[4,2] == [0.6, 0.2]
+        @test cg[6,10] == [1., 1.8]
 
-    @testset "Indexing Interface" begin
-        # cg = CurvilinearGrid(g, x̄, J)
-        # @test cg[1,1] == [0.0, 0.0]
-        # @test cg[4,2] == [3/9,1/9]
-        # @test cg[6,10] == [5/9, 1]
+        @test cg[begin, begin] == [0.0, 0.0]
+        @test cg[end,end] == [2.0, 2.0]
+        @test cg[begin,end] == [0., 2.]
+
+        @test eachindex(cg) == CartesianIndices((11,11))
+
+        @testset "cartesian indexing" begin
+            cases = [
+                 (1,1) ,
+                 (3,5) ,
+                 (10,6),
+                 (1,1) ,
+                 (3,2) ,
+            ]
 
-        # @test cg[begin, begin] == [0.0, 0.0]
-        # @test cg[end,end] == [1.0, 1.0]
-        # @test cg[begin,end] == [0., 1.]
+            @testset "i = $is" for (lg, is) ∈ cases
+                @test cg[CartesianIndex(is...)] == cg[is...]
+            end
+        end
+
+        @testset "eachindex" begin
+            @test eachindex(cg) == CartesianIndices((11,11))
+        end
 
-        # @test eachindex(cg) == 1:101
+        @testset "firstindex" begin
+            @test firstindex(cg, 1) == 1
+            @test firstindex(cg, 2) == 1
+        end
+
+        @testset "lastindex" begin
+            @test lastindex(cg, 1) == 11
+            @test lastindex(cg, 2) == 11
+        end
     end
+    # TODO: Test with different types of logical grids
 
     @testset "Iterator interface" begin
         # @test eltype(EquidistantGrid(0:10)) == Int