Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Nov 6, 2024
1 parent 145c0b3 commit 884be33
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/coarsening/regular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ RegularCoarsening(factors::Vararg{Int,N}) where {N} = RegularCoarsening(factors)

function coarsen(grid::OrthoRegularGrid, method::RegularCoarsening)
factors = fitdims(method.factors, paramdim(grid))
dims = _targetsize(grid, factors)
dims = _coarsesize(grid, factors)
RegularGrid(minimum(grid), maximum(grid), dims=dims)
end

function coarsen(grid::RectilinearGrid, method::RegularCoarsening)
factors = fitdims(method.factors, paramdim(grid))
inds = _targetinds(grid, factors)
inds = _coarseinds(grid, factors)
xyzₛ = xyz(grid)
xyzₜ = ntuple(i -> xyzₛ[i][inds[i]], paramdim(grid))
RectilinearGrid{manifold(grid),crs(grid)}(xyzₜ)
end

function coarsen(grid::StructuredGrid, method::RegularCoarsening)
factors = fitdims(method.factors, paramdim(grid))
inds = _targetinds(grid, factors)
inds = _coarseinds(grid, factors)
XYZₛ = XYZ(grid)
XYZₜ = ntuple(i -> XYZₛ[i][inds...], paramdim(grid))
StructuredGrid{manifold(grid),crs(grid)}(XYZₜ)
Expand All @@ -49,16 +49,16 @@ coarsen(grid::TransformedGrid, method::RegularCoarsening) =
# HELPER FUNCTIONS
# -----------------

function _targetsize(grid, factors)
function _coarsesize(grid, factors)
dims = size(grid)
axes = ntuple(i -> 1:dims[i], paramdim(grid))
size(TileIterator(axes, factors))
end

_targetvsize(grid, factors) = _targetsize(grid, factors) .+ .!isperiodic(grid)
_coarsevsize(grid, factors) = _coarsesize(grid, factors) .+ .!isperiodic(grid)

function _targetinds(grid, factors)
function _coarseinds(grid, factors)
dims = vsize(grid)
tdims = _targetvsize(grid, factors)
tdims = _coarsevsize(grid, factors)
ntuple(i -> floor.(Int, range(start=1, stop=dims[i], length=tdims[i])), paramdim(grid))
end
17 changes: 11 additions & 6 deletions test/coarsening.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
@test coarsen(grid, RegularCoarsening(2, 4)) == tgrid

# non-multiple dimensions
grid = CartesianGrid(cart(0, 0), cart(11, 17), dims=(11, 17))
tgrid = CartesianGrid(cart(0, 0), cart(11, 17), dims=(3, 6))
grid = CartesianGrid(cart(0, 0), cart(13, 17), dims=(13, 17))
tgrid = CartesianGrid(cart(0, 0), cart(13, 17), dims=(3, 6))
@test coarsen(grid, RegularCoarsening(5, 3)) == tgrid
rgrid = convert(RectilinearGrid, grid)
@test size(coarsen(rgrid, RegularCoarsening(5, 3)) ) == (3, 6)
@test size(coarsen(rgrid, RegularCoarsening(5, 3))) == (3, 6)
sgrid = convert(StructuredGrid, grid)
@test size(coarsen(sgrid, RegularCoarsening(5, 3))) == (3, 6)
tfgrid = TransformedGrid(grid, Identity())
@test size(coarsen(tfgrid, RegularCoarsening(5, 3))) == (3, 6)

# large grid
grid = CartesianGrid(cart(0, 0), cart(16200, 8100), dims=(16200, 8100))
tgrid = CartesianGrid(cart(0, 0), cart(16200, 8100), dims=(203, 203))
@test coarsen(grid, RegularCoarsening(80, 40)) == tgrid

# 3D grids
grid = cartgrid(100, 100, 100)
tgrid = CartesianGrid(minimum(grid), maximum(grid), dims=(50, 25, 20))
Expand All @@ -41,11 +46,11 @@
@test coarsen(tfgrid, RegularCoarsening(2, 4, 5)) == coarsen(grid, RegularCoarsening(2, 4, 5))

# non-multiple dimensions
grid = CartesianGrid(cart(0, 0, 0), cart(11, 17, 23), dims=(11, 17, 23))
tgrid = CartesianGrid(cart(0, 0, 0), cart(11, 17, 23), dims=(2, 4, 8))
grid = CartesianGrid(cart(0, 0, 0), cart(13, 17, 23), dims=(13, 17, 23))
tgrid = CartesianGrid(cart(0, 0, 0), cart(13, 17, 23), dims=(2, 4, 8))
@test coarsen(grid, RegularCoarsening(7, 5, 3)) == tgrid
rgrid = convert(RectilinearGrid, grid)
@test size(coarsen(rgrid, RegularCoarsening(7, 5, 3)) ) == (2, 4, 8)
@test size(coarsen(rgrid, RegularCoarsening(7, 5, 3))) == (2, 4, 8)
sgrid = convert(StructuredGrid, grid)
@test size(coarsen(sgrid, RegularCoarsening(7, 5, 3))) == (2, 4, 8)
tfgrid = TransformedGrid(grid, Identity())
Expand Down

0 comments on commit 884be33

Please sign in to comment.