Mercurial > repos > public > sbplib_julia
changeset 907:e81b89ae17c4 feature/equidistant_grid/refine
Add coarsen()
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 15 Feb 2022 11:36:12 +0100 |
parents | dd2ab001a7b6 |
children | bc71dd5b8311 |
files | src/Grids/EquidistantGrid.jl test/Grids/EquidistantGrid_test.jl |
diffstat | 2 files changed, 25 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl Mon Feb 14 09:39:58 2022 +0100 +++ b/src/Grids/EquidistantGrid.jl Tue Feb 15 11:36:12 2022 +0100 @@ -5,6 +5,7 @@ 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}) @@ -151,3 +152,15 @@ new_sz = (sz .- 1).*r .+ 1 return EquidistantGrid{dimension(grid), eltype(grid)}(new_sz, grid.limit_lower, grid.limit_upper) end + +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 Mon Feb 14 09:39:58 2022 +0100 +++ b/test/Grids/EquidistantGrid_test.jl Tue Feb 15 11:36:12 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