changeset 877:dd2ab001a7b6 feature/equidistant_grid/refine

Implement refine function, move exports to the top of the file, change location of constuctors. The constructors were changed have only one inner constructor and simpler outer constructors.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 14 Feb 2022 09:39:58 +0100
parents 9a2776352c2a
children 00159066485b e81b89ae17c4
files src/Grids/EquidistantGrid.jl test/Grids/EquidistantGrid_test.jl
diffstat 2 files changed, 36 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl	Wed Jan 19 11:08:43 2022 +0100
+++ b/src/Grids/EquidistantGrid.jl	Mon Feb 14 09:39:58 2022 +0100
@@ -1,3 +1,11 @@
+export EquidistantGrid
+export spacing
+export inverse_spacing
+export restrict
+export boundary_identifiers
+export boundary_grid
+export refine
+
 """
     EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T})
 	EquidistantGrid{T}()
@@ -21,7 +29,7 @@
     limit_upper::NTuple{Dim, T}
 
     # General constructor
-    function EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where Dim where T
+    function EquidistantGrid{Dim,T}(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) where {Dim,T}
         if any(size .<= 0)
             throw(DomainError("all components of size must be postive"))
         end
@@ -30,12 +38,12 @@
         end
         return new{Dim,T}(size, limit_lower, limit_upper)
     end
+end
 
-	# Specialized constructor for 0-dimensional grid
-	EquidistantGrid{T}() where T = new{0,T}((),(),())
+function EquidistantGrid(size, limit_lower, limit_upper)
+    return EquidistantGrid{length(size), eltype(limit_lower)}(size, limit_lower, limit_upper)
 end
-export EquidistantGrid
-
+EquidistantGrid{T}() where T = EquidistantGrid{0,T}((),(),()) # Convenience constructor for 0-dim grid
 
 """
     EquidistantGrid(size::Int, limit_lower::T, limit_upper::T)
@@ -65,7 +73,6 @@
 The spacing between the grid points of the grid.
 """
 spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
-export spacing
 
 """
     inverse_spacing(grid::EquidistantGrid)
@@ -73,7 +80,6 @@
 The reciprocal of the spacing between the grid points of the grid.
 """
 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid)
-export inverse_spacing
 
 """
     points(grid::EquidistantGrid)
@@ -102,7 +108,6 @@
 
     return EquidistantGrid(size, limit_lower, limit_upper)
 end
-export restrict
 
 """
     boundary_identifiers(::EquidistantGrid)
@@ -114,7 +119,6 @@
 	 ...)
 """
 boundary_identifiers(g::EquidistantGrid) = (((ntuple(i->(CartesianBoundary{i,Lower}(),CartesianBoundary{i,Upper}()),dimension(g)))...)...,)
-export boundary_identifiers
 
 
 """
@@ -133,5 +137,17 @@
 	end
     return restrict(grid,orth_dims)
 end
-export boundary_grid
 boundary_grid(::EquidistantGrid{1,T},::CartesianBoundary{1}) where T = EquidistantGrid{T}()
+
+
+"""
+    refine(grid::EquidistantGrid, r::Int)
+
+Refines `grid` by a factor `r`. The factor is applied to the number of
+intervals which is 1 less than the size of the grid.
+"""
+function refine(grid::EquidistantGrid, r::Int)
+    sz = size(grid)
+    new_sz = (sz .- 1).*r .+ 1
+    return EquidistantGrid{dimension(grid), eltype(grid)}(new_sz, grid.limit_lower, grid.limit_upper)
+end
--- a/test/Grids/EquidistantGrid_test.jl	Wed Jan 19 11:08:43 2022 +0100
+++ b/test/Grids/EquidistantGrid_test.jl	Mon Feb 14 09:39:58 2022 +0100
@@ -98,4 +98,14 @@
                 @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}())
             end
     end
+
+    @testset "refine" begin
+        @test refine(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
+        @test refine(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
+
+        g = EquidistantGrid((10,5),(0.,1.),(2.,3.))
+        @test refine(g, 1) == g
+        @test refine(g, 2) == EquidistantGrid((19,9),(0.,1.),(2.,3.))
+        @test refine(g, 3) == EquidistantGrid((28,13),(0.,1.),(2.,3.))
+    end
 end