changeset 1143:9275d95e2d90 refactor/grids

Merge with default
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 19 Oct 2022 22:36:02 +0200
parents c4ea28d904f5 (diff) 0aa8ce9f30e2 (current diff)
children cfe6a09974fb
files src/Grids/EquidistantGrid.jl src/Grids/equidistant_grid.jl test/Grids/EquidistantGrid_test.jl test/Grids/equidistant_grid_test.jl
diffstat 2 files changed, 20 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/equidistant_grid.jl	Wed Oct 12 10:44:44 2022 +0200
+++ b/src/Grids/equidistant_grid.jl	Wed Oct 19 22:36:02 2022 +0200
@@ -20,7 +20,6 @@
     end
 end
 
-
 """
     EquidistantGrid(size, limit_lower, limit_upper)
 
@@ -37,7 +36,7 @@
 function EquidistantGrid(size, limit_lower, limit_upper)
     return EquidistantGrid{length(size), eltype(limit_lower)}(size, limit_lower, limit_upper)
 end
-
+# TBD: Should it be an AbstractArray?
 
 """
     EquidistantGrid{T}()
@@ -64,6 +63,13 @@
 
 Base.ndims(::EquidistantGrid{Dim}) where Dim = Dim
 
+function Base.getindex(g::EquidistantGrid, I::Vararg{Int})
+    h = spacing(g)
+    return g.limit_lower .+ (I.-1).*h
+end
+
+Base.getindex(g::EquidistantGrid, I::CartesianIndex) = g[Tuple(I)...]
+# TBD: Can this method be removed if `EquidistantGrid` is an AbstractArray?
 
 
 
@@ -99,7 +105,6 @@
     return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices)
 end
 
-
 """
     restrict(::EquidistantGrid, dim)
 
--- a/test/Grids/equidistant_grid_test.jl	Wed Oct 12 10:44:44 2022 +0200
+++ b/test/Grids/equidistant_grid_test.jl	Wed Oct 19 22:36:02 2022 +0200
@@ -44,6 +44,18 @@
         end
     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)
+
+        @test g[CartesianIndex(1,3)] == (-1.0,7.11)
+    end
+
     @testset "restrict" begin
         g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
         @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)