Skip to content

Commit

Permalink
Uniform grid helpers (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
luraess authored Mar 5, 2024
2 parents b93ac1b + d8d991a commit 48d0861
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Chmy"
uuid = "33a72cf0-4690-46d7-b987-06506c2248b9"
authors = ["Ivan Utkin <[email protected]>, Ludovic Raess <[email protected]>, and contributors"]
version = "0.1.0"
version = "0.1.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion src/Distributed/Distributed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Enables hiding MPI communication behind computations.
module Distributed

export CartesianTopology, global_rank, shared_rank, node_name, cart_comm, shared_comm
export dims, coords, neighbors, neighbor, has_neighbor, global_size, node_size
export dims, cart_coords, neighbors, neighbor, has_neighbor, global_size, node_size
export DistributedArchitecture, topology
export exchange_halo!, gather!

Expand Down
2 changes: 1 addition & 1 deletion src/Distributed/distributed_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Grids.StructuredGrid{C}(arch::DistributedArchitecture, axes::Vararg{Abs
end

local_dims = cld.(global_dims, dims(arch.topology))
offsets = coords(arch.topology) .* local_dims
offsets = cart_coords(arch.topology) .* local_dims

local_axes = ntuple(Val(N)) do D
Base.@_inline_meta
Expand Down
4 changes: 2 additions & 2 deletions src/Distributed/topology.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ Dimensions of the topology as NTuple.
dims(t::CartesianTopology) = t.dims

"""
coords(topo)
cart_coords(topo)
Coordinates of a current process within a Cartesian topology.
"""
coords(t::CartesianTopology) = t.cart_coords
cart_coords(t::CartesianTopology) = t.cart_coords

"""
neighbors(topo)
Expand Down
18 changes: 18 additions & 0 deletions src/Grids/structured_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ for (sp, desc) in ((:spacing, "grid spacings"), (:inv_spacing, "inverse grid spa
end
end

"""
spacing(grid::UniformGrid)
Return a tuple of grid spacing for a uniform grid `grid`.
"""
spacing(grid::UniformGrid) = getfield.(grid.axes, :spacing)
spacing(grid::UniformGrid, ::Dim{dim}) where {dim} = grid.axes[dim].spacing

"""
inv_spacing(grid::UniformGrid)
Return a tuple of inverse grid spacing for a uniform grid `grid`.
"""
inv_spacing(grid::UniformGrid) = getfield.(grid.axes, :inv_spacing)
inv_spacing(grid::UniformGrid, ::Dim{dim}) where {dim} = grid.axes[dim].inv_spacing

# coordinate lists

coords(grid::SG{N}, loc::LocOrLocs{N}) where {N} = coords.(grid.axes, loc)
Expand Down Expand Up @@ -181,6 +197,8 @@ for (dim, c) in enumerate((:x, :y, :z))
@eval begin
export $_Δ, $_coord, $_coords, $_vertex, $_center, $_vertices, $_centers

@propagate_inbounds $(grid::UniformGrid) = spacing(grid, Dim($dim))

@propagate_inbounds $(grid::SG{N}, loc, I::Vararg{Integer,N}) where {N} = spacing(grid, loc, Dim($dim), I...)
@propagate_inbounds $(grid::SG, loc, I) = spacing(grid, loc, Dim($dim), I)

Expand Down
10 changes: 10 additions & 0 deletions test/test_grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ using Chmy.Architectures
@test inv_spacing(grid, Center(), 1, 1) == inv_spacing(grid, (Center(), Center()), 1, 1)
end

@testset "uniform spacing" begin
# spacing
@test all(spacing(grid) .≈ (0.4, 0.2))
# inverse
@test all(inv_spacing(grid) .≈ (2.5, 5.0))
# cartesian
@test Δx(grid) 0.4
@test Δy(grid) 0.2
end

@testset "coords" begin
# one index
@test coord(grid, Vertex(), Dim(1), 1) -1.0
Expand Down

0 comments on commit 48d0861

Please sign in to comment.