changeset 1090:9b40aeac4269 refactor/grids

Add getindex for grid
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 26 Apr 2022 14:28:42 +0200
parents 9abb140a4636
children e3b41d48b5aa c4ea28d904f5
files src/Grids/EquidistantGrid.jl test/Grids/EquidistantGrid_test.jl
diffstat 2 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl	Fri Apr 08 19:36:22 2022 +0200
+++ b/src/Grids/EquidistantGrid.jl	Tue Apr 26 14:28:42 2022 +0200
@@ -67,6 +67,11 @@
 
 Base.size(g::EquidistantGrid) = g.size
 
+function Base.getindex(g::EquidistantGrid, I...)
+    h = spacing(g)
+    return g.limit_lower .+ (I.-1).*h
+end
+
 """
     dimension(grid::EquidistantGrid)
 
--- a/test/Grids/EquidistantGrid_test.jl	Fri Apr 08 19:36:22 2022 +0200
+++ b/test/Grids/EquidistantGrid_test.jl	Tue Apr 26 14:28:42 2022 +0200
@@ -43,6 +43,17 @@
         @test [gp[i]...] ≈ [p[i]...] atol=5e-13
     end
 
+
+    @testset "getindex" begin
+        g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11))
+        @test g[1,1] == (-1.0,0.0)
+        @test g[1,3] == (-1.0,7.11)
+        @test g[5,1] == (0.0,0.0)
+        @test g[5,3] == (0.0,7.11)
+
+        @test g[4,2] == (-0.25,7.11/2)
+    end
+
     # restrict
     g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
     @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)