changeset 366:0af6da238d95

Merge feature/equidistant_grid/subgrid. Add function for getting isolated dimensions of EquidistantGrid
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 28 Sep 2020 22:49:21 +0200
parents ffddaf053085 (current diff) 9ccdb8a7a438 (diff)
children 137a62635c49 8e55dee6a1a1
files
diffstat 2 files changed, 31 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl	Sun Sep 27 15:26:05 2020 +0200
+++ b/src/Grids/EquidistantGrid.jl	Mon Sep 28 22:49:21 2020 +0200
@@ -66,6 +66,20 @@
     return broadcast(I -> grid.limit_lower .+ (I.-1).*h, indices)
 end
 
+"""
+    restrict(::EquidistantGrid, dim)
+
+Pick out given dimensions from the grid and return a grid for them
+"""
+function restrict(grid::EquidistantGrid, dim)
+    size = grid.size[dim]
+    limit_lower = grid.limit_lower[dim]
+    limit_upper = grid.limit_upper[dim]
+
+    return EquidistantGrid(size, limit_lower, limit_upper)
+end
+export restrict
+
 function pointsalongdim(grid::EquidistantGrid, dim::Integer)
     @assert dim<=dimension(grid)
     @assert dim>0
--- a/test/testGrids.jl	Sun Sep 27 15:26:05 2020 +0200
+++ b/test/testGrids.jl	Mon Sep 28 22:49:21 2020 +0200
@@ -4,9 +4,23 @@
 @testset "Grids" begin
 
 @testset "EquidistantGrid" begin
-    @test EquidistantGrid(4,0,1) isa EquidistantGrid
-    @test dimension(EquidistantGrid(4,0,1)) == 1
-    @test EquidistantGrid(4,0,1) == EquidistantGrid((4,),(0,),(1,))
+    @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid
+    @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid
+    @test dimension(EquidistantGrid(4,0.0,1.0)) == 1
+    @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,))
+
+    g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))
+    @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0)
+    @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0)
+
+    g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0))
+    @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0)
+    @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0)
+    @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0)
+    @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0))
+    @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0))
+    @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0))
+    @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0))
 end
 
 end