Skip to content

Exclude faces that touch NaN nodes from the SpatialHash table - #2802

Merged
wyatt-fluidnumerics merged 13 commits into
mainfrom
spatialhash-NaN-cell-treatment
Aug 5, 2026
Merged

Exclude faces that touch NaN nodes from the SpatialHash table#2802
wyatt-fluidnumerics merged 13 commits into
mainfrom
spatialhash-NaN-cell-treatment

Conversation

@wyatt-fluidnumerics

@wyatt-fluidnumerics wyatt-fluidnumerics commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

Previously, any faces that touched NaN nodes in curvilinear or unstructured grids were treated as a normal face. This meant they were quantized and added to the SpatialHash table. The resulting behavior was a pileup of faces being assigned to (0, 0, 0). For more details see the discussion on #2796. This PR resolves this issue by manually setting values in num_hash_per_face to 0 for faces which touch a node that is NaN. This prevents these faces from ever entering the SpatialHash table. This PR also adds testing to confirm that faces that touch NaN nodes are never in the SpatialHash table, and that this new behavior does not effect the query resolution of any other mesh cells in the grid.

Checklist

  • Tests added
  • This PR targets the correct branch (main for normal development, v3-support for v3 support)

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Describe how you used it (e.g., by pasting your prompt): Used Claude code in plan mode to discuss the best approaches for adding the NaN filter and testing. Claude found and fixed a bug in the new test that arose from not deep copying the lat/lon grid before injecting NaN values.

…/lon grid before injecting nan values. Otherwise the nan values break other tests.
@wyatt-fluidnumerics
wyatt-fluidnumerics marked this pull request as ready for review August 4, 2026 23:15
@wyatt-fluidnumerics

Copy link
Copy Markdown
Contributor Author

@erikvansebille, curious to hear your thoughts on this solution! To me it seemed like this approach would take the least amount of refactoring as it doesn't mess with the 2d array reconfiguration issues you had mentioned earlier today.

@erikvansebille

Copy link
Copy Markdown
Member

This works beautifully! My example that I showed earlier in #2796 (review) now looks much better
Screenshot 2026-08-05 at 08 39 35

Comment thread src/parcels/_core/spatialhash.py
Comment thread src/parcels/_core/spatialhash.py Outdated
return int((nx * ny * nz).sum())
# NaN values are not allowed in the SpatialHash table, so faces with a NaN
# bounding box do not contribute to the entry count
invalid_face = (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We tend to think of masks here, so perhaps rename to valid_mask?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I modified the logic so that the True corresponds to valid non-NaN cells.

Comment thread src/parcels/_reprs.py
Comment thread tests/test_spatialhash.py

@erikvansebille erikvansebille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! One more small suggestion

Comment thread src/parcels/_core/spatialhash.py Outdated
Co-authored-by: Erik van Sebille <e.vansebille@uu.nl>

@erikvansebille erikvansebille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very good! Now it's custom that you, as the initiator of this PR, also merge it. DO you have the rights to do that?

@wyatt-fluidnumerics
wyatt-fluidnumerics enabled auto-merge (squash) August 5, 2026 14:30
@wyatt-fluidnumerics

Copy link
Copy Markdown
Contributor Author

Just initiated it, I believe it should go through when the checks complete?

@wyatt-fluidnumerics
wyatt-fluidnumerics merged commit f8741f0 into main Aug 5, 2026
17 of 18 checks passed
@wyatt-fluidnumerics
wyatt-fluidnumerics deleted the spatialhash-NaN-cell-treatment branch August 5, 2026 14:36
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Parcels development Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants