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 VerticallyStretchedRectilinearGrids #2019

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions src/Grids/vertically_stretched_rectilinear_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,22 @@ function min_Δz(grid::VerticallyStretchedRectilinearGrid)
return minimum(parent(grid.Δzᵃᵃᶜ))
end
end


"""
grid1 == grid2

Redefines Base.:(==) to tests if two grids are equal by testing `==` for every individual
field of the two elements. (The default behavior is using `===`, which makes it so that
`grid1==deepcopy(grid1)` for `VerticallyStretchedRectilinearGrid`s.
"""
function Base.:(==)(grid1::VerticallyStretchedRectilinearGrid, grid2::VerticallyStretchedRectilinearGrid)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about other types of grids? Curvilinear grids for example? Would they show the same "problem" as the VerticallyStretchedRectilinearGrid? Why don't we redefine Base.:(==)to work forAbstactGrid`?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea for other fields, so I wanted to be conservative for now. This seems to be related to array comparisons and, since vertically stretched grids needs an array, this error gets thrown. Do other grids have arrays in their struct?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap. Other grids have it.

No need to be conservative. Let's fix it for all grids!
Would the == as above fail for RegularRectilinearGrids?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the == as above fail for RegularRectilinearGrids?

I think that would work for all grids without change actually. But I haven't tested it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== works (now) for RegularRectilinearGrid because it is isbits I think. We may not need a special method for this grid. The same goes for RegularLatitudeLongitudeGrid.

For any grid that contains arrays, we need to define our own numeric equality operator ==.

names = fieldnames(typeof(grid1))
for name in names
if getproperty(grid1, name) != getproperty(grid2, name)
return false
end
end

return true
end