changeset 1823:054447ac4b0e feature/grids/tensor_grid/spacing

Resolve review comments by filtering ZeroDimGrids
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 20 Oct 2024 20:58:44 +0200
parents 189e69d44056
children bf1ea1a9b995
files src/Grids/tensor_grid.jl test/Grids/tensor_grid_test.jl
diffstat 2 files changed, 18 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/tensor_grid.jl	Sun Oct 20 20:38:39 2024 +0200
+++ b/src/Grids/tensor_grid.jl	Sun Oct 20 20:58:44 2024 +0200
@@ -47,10 +47,10 @@
 Base.size(g::TensorGrid) = LazyTensors.concatenate_tuples(size.(g.grids)...)
 Base.size(g::TensorGrid, d) = size(g)[d]
 
-# Review: Handle boundary grids as well?
-# spacing(boundary_grid(g,id)) fails right now with
-# MethodError: no method matching spacing(::ZeroDimGrid{Float64})
-spacing(g::TensorGrid) = spacing.(g.grids)
+function spacing(g::TensorGrid)
+    relevant_grids = filter(g->!isa(g,ZeroDimGrid),g.grids)
+    return spacing.(relevant_grids)
+end
 
 function min_spacing(g::TensorGrid)
     relevant_grids = filter(g->!isa(g,ZeroDimGrid),g.grids)
--- a/test/Grids/tensor_grid_test.jl	Sun Oct 20 20:38:39 2024 +0200
+++ b/test/Grids/tensor_grid_test.jl	Sun Oct 20 20:58:44 2024 +0200
@@ -140,8 +140,22 @@
     @testset "spacing" begin
         g₁ = EquidistantGrid(range(0,1,length=11))
         g₂ = EquidistantGrid(range(2,3,length=6))
+        g₃ = ZeroDimGrid(@SVector[1,2])
+
+        @test spacing(TensorGrid(g₁)) == (1/10,)
+        @test spacing(TensorGrid(g₂)) == (1/5,)
 
         @test spacing(TensorGrid(g₁, g₂)) == (1/10, 1/5)
+
+        @test spacing(TensorGrid(g₁, g₃)) == (1/10,)
+        @test spacing(TensorGrid(g₃, g₂)) == (1/5,)
+
+
+        @test spacing(TensorGrid(g₁, g₂, g₁)) == (1/10, 1/5, 1/10)
+
+        @test spacing(TensorGrid(g₃, g₂, g₁)) == (1/5, 1/10)
+        @test spacing(TensorGrid(g₁, g₃, g₁)) == (1/10, 1/10)
+        @test spacing(TensorGrid(g₁, g₂, g₃)) == (1/10, 1/5)
     end
 
     @testset "min_spacing" begin