changeset 912:f800f97b3a45 feature/variable_derivatives

Merge default
author Jonatan Werpers <jonatan@werpers.com>
date Tue, 15 Feb 2022 15:15:11 +0100
parents 33c7e266e1a9 (current diff) a378ab959b6f (diff)
children cad3a9f82009
files
diffstat 2 files changed, 60 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl	Tue Feb 15 09:56:07 2022 +0100
+++ b/src/Grids/EquidistantGrid.jl	Tue Feb 15 15:15:11 2022 +0100
@@ -5,30 +5,18 @@
 export boundary_identifiers
 export boundary_grid
 export refine
+export coarsen
 
 """
-    EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T})
-	EquidistantGrid{T}()
-
-`EquidistantGrid` is a grid with equidistant grid spacing per coordinat direction.
+    EquidistantGrid{Dim,T<:Real} <: AbstractGrid
 
-`EquidistantGrid(size, limit_lower, limit_upper)` construct the grid with the
-domain defined by the two points P1, and P2 given by `limit_lower` and
-`limit_upper`. The length of the domain sides are given by the components of
-(P2-P1). E.g for a 2D grid with P1=(-1,0) and P2=(1,2) the domain is defined
-as (-1,1)x(0,2). The side lengths of the grid are not allowed to be negative.
-The number of equidistantly spaced points in each coordinate direction are given
-by `size`.
-
-`EquidistantGrid{T}()` constructs a 0-dimensional grid.
-
+`Dim`-dimensional equidistant grid with coordinates of type `T`.
 """
 struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid
     size::NTuple{Dim, Int}
     limit_lower::NTuple{Dim, T}
     limit_upper::NTuple{Dim, T}
 
-    # General constructor
     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"))
@@ -40,9 +28,28 @@
     end
 end
 
+"""
+    EquidistantGrid(size, limit_lower, limit_upper)
+
+Construct an equidistant grid with corners at the coordinates `limit_lower` and
+`limit_upper`.
+
+The length of the domain sides are given by the components of
+`limit_upper-limit_lower`. E.g for a 2D grid with `limit_lower=(-1,0)` and `limit_upper=(1,2)` the domain is defined
+as `(-1,1)x(0,2)`. The side lengths of the grid are not allowed to be negative.
+
+The number of equidistantly spaced points in each coordinate direction are given
+by the tuple `size`.
+"""
 function EquidistantGrid(size, limit_lower, limit_upper)
     return EquidistantGrid{length(size), eltype(limit_lower)}(size, limit_lower, limit_upper)
 end
+
+"""
+    EquidistantGrid{T}()
+
+Constructs a 0-dimensional grid.
+"""
 EquidistantGrid{T}() where T = EquidistantGrid{0,T}((),(),()) # Convenience constructor for 0-dim grid
 
 """
@@ -70,14 +77,14 @@
 """
     spacing(grid::EquidistantGrid)
 
-The spacing between the grid points of the grid.
+The spacing between grid points.
 """
 spacing(grid::EquidistantGrid) = (grid.limit_upper.-grid.limit_lower)./(grid.size.-1)
 
 """
     inverse_spacing(grid::EquidistantGrid)
 
-The reciprocal of the spacing between the grid points of the grid.
+The reciprocal of the spacing between grid points.
 """
 inverse_spacing(grid::EquidistantGrid) = 1 ./ spacing(grid)
 
@@ -99,7 +106,7 @@
 """
     restrict(::EquidistantGrid, dim)
 
-Pick out given dimensions from the grid and return a grid for them
+Pick out given dimensions from the grid and return a grid for them.
 """
 function restrict(grid::EquidistantGrid, dim)
     size = grid.size[dim]
@@ -145,9 +152,32 @@
 
 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.
+
+See also: [`coarsen`](@ref)
 """
 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
+
+"""
+    coarsen(grid::EquidistantGrid, r::Int)
+
+Coarsens `grid` by a factor `r`. The factor is applied to the number of
+intervals which is 1 less than the size of the grid. If the number of
+intervals are not divisible by `r` an error is raised.
+
+See also: [`refine`](@ref)
+"""
+function coarsen(grid::EquidistantGrid, r::Int)
+    sz = size(grid)
+
+    if !all(n -> (n % r == 0), sz.-1)
+        throw(DomainError(r, "Size minus 1 must be divisible by the ratio."))
+    end
+
+    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	Tue Feb 15 09:56:07 2022 +0100
+++ b/test/Grids/EquidistantGrid_test.jl	Tue Feb 15 15:15:11 2022 +0100
@@ -108,4 +108,16 @@
         @test refine(g, 2) == EquidistantGrid((19,9),(0.,1.),(2.,3.))
         @test refine(g, 3) == EquidistantGrid((28,13),(0.,1.),(2.,3.))
     end
+
+    @testset "coarsen" begin
+        @test coarsen(EquidistantGrid{Float64}(), 1) == EquidistantGrid{Float64}()
+        @test coarsen(EquidistantGrid{Float64}(), 2) == EquidistantGrid{Float64}()
+
+        g = EquidistantGrid((7,13),(0.,1.),(2.,3.))
+        @test coarsen(g, 1) == g
+        @test coarsen(g, 2) == EquidistantGrid((4,7),(0.,1.),(2.,3.))
+        @test coarsen(g, 3) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
+
+        @test_throws DomainError(4, "Size minus 1 must be divisible by the ratio.") coarsen(g, 4) == EquidistantGrid((3,5),(0.,1.),(2.,3.))
+    end
 end