Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefines equality (==) for Grids #2028

Merged
merged 14 commits into from
Oct 28, 2021
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Oceananigans"
uuid = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
version = "0.63.2"
version = "0.63.3"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
12 changes: 12 additions & 0 deletions src/Grids/Grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ Base.eltype(::AbstractGrid{FT}) where FT = FT
Base.size(grid::AbstractGrid) = (grid.Nx, grid.Ny, grid.Nz)
Base.length(grid::AbstractGrid) = (grid.Lx, grid.Ly, grid.Lz)

function Base.:(==)(grid1::AbstractGrid, grid2::AbstractGrid)
navidcy marked this conversation as resolved.
Show resolved Hide resolved
#check if grids are of the same type
!isa(grid2, typeof(grid1).name.wrapper) && return false

topology(grid1) !== topology(grid2) && return false

x1, y1, z1 = nodes((Face, Face, Face), grid1)
x2, y2, z2 = nodes((Face, Face, Face), grid2)

return x1 == x2 && y1 == y2 && z1 == z2
end

halo_size(grid) = (grid.Hx, grid.Hy, grid.Hz)

topology(::AbstractGrid{FT, TX, TY, TZ}) where {FT, TX, TY, TZ} = (TX, TY, TZ)
Expand Down
27 changes: 27 additions & 0 deletions test/test_grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ function test_flat_size_regular_rectilinear_grid(FT)
return nothing
end

function test_grid_equality()
topo = (Periodic, Periodic, Bounded)
Nx, Ny, Nz = 4, 7, 9
grid1 = RegularRectilinearGrid(topology=topo, size=(Nx, Ny, Nz), x=(0, 1), y=(-1, 1), z=(0, Nz))
grid2 = VerticallyStretchedRectilinearGrid(architecture=CPU(), topology=topo, size=(Nx, Ny, Nz), x=(0, 1), y=(-1, 1), z_faces=0:Nz)
grid3 = VerticallyStretchedRectilinearGrid(architecture=CPU(), topology=topo, size=(Nx, Ny, Nz), x=(0, 1), y=(-1, 1), z_faces=0:Nz)

return grid1==grid1 && grid2 == grid3 && grid1 !== grid3
end

function test_grid_equality_over_architectures()
grid_cpu = VerticallyStretchedRectilinearGrid(architecture=CPU(), topology=(Periodic, Periodic, Bounded), size=(3, 7, 9), x=(0, 1), y=(-1, 1), z_faces=0:9)
grid_gpu = VerticallyStretchedRectilinearGrid(architecture=GPU(), topology=(Periodic, Periodic, Bounded), size=(3, 7, 9), x=(0, 1), y=(-1, 1), z_faces=0:9)

return grid_cpu == grid_gpu
end

#####
##### Vertically stretched grids
#####
Expand Down Expand Up @@ -546,6 +563,16 @@ end
end
end

@testset "Grid equality" begin
@info " Testing grid equality operator (==)..."

test_grid_equality()

if CUDA.has_cuda()
test_grid_equality_over_architectures()
end
end

# Testing show function
topo = (Periodic, Periodic, Periodic)

Expand Down