Repository navigation
Fix find_nearest_cell on a toroidal HexGrid near the edges - #3979
Conversation
dietcoke-17
left a comment
There was a problem hiding this comment.
Thanks for the clear write-up and for including the brute-force comparison. The diagnosis is right: a non-periodic KDTree can't see across the wrap, and passing boxsize when torus=True is the idiomatic fix. Non-torus behaviour is unchanged, and the test checks distance rather than cell identity, so it handles ties and doesn't depend on the layout (so it should survive #3959).
LGTM.
|
Performance benchmarks:
|
|
Thanks @ARYAMANN7279 for the brute-force comparison and regression test. The numerical example explains the lookup error, but the real-model impact is still missing. Before maintainer review, please address these two blockers:
Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
The KD-tree only held the primary cell positions, so for a point close to an edge of a torus it could return a cell on the near side even if a cell across the edge was closer. Build the tree with the periodic box size when torus=True. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
6f26007 to
44b4424
Compare
|
I rebased the branch onto the current main (one conflict in the test file with #3959, I kept both tests). The new test fails on main without the fix and passes with it. I opened #4097 with the impact: on a toroidal HexGrid 7.6% of random points on a 10x10 grid (0.8% on 100x100) get a cell that is not the nearest, all of them near an edge. This PR fixes #4097. I don't have a published model that hits this, so the issue says that the numbers come from random points. |
|
Reproduced #4097 on current main and checked this fix against it. I used the script from the issue, 2000 random points per grid size, compared with a brute-force search that uses the wrapped distance. Share of points that get a cell that isn't the nearest:
Other things I tried on this branch:
One small note, not blocking: I'm not a maintainer, so no approval from me, but this looks right to me. |
|
Thanks @lahari936 for reproducing it and for checking points outside the box too. About the box size: yes, I rely on the constructor, which raises for a torus with an odd width or height ( |
* Update release notes and version for 4.0.0a1 Add the 4.0.0a1 release notes to HISTORY.md, leaving out changes already released in Mesa 3.5.1 to 3.5.4, and bump the version to 4.0.0a1. Move the #4182 replication id fix, which was added under 4.0.0a0, into the 4.0.0a1 notes where it was released. Assisted-by: Claude Opus 5.5 * Leave dependabot and pre-commit.ci bumps out of the 4.0.0a1 notes Assisted-by: Claude Opus 5.5 * Add PRs merged on 2026-11-30 to the 4.0.0a1 notes Seven PRs were merged after the notes were drafted: #4284, #3960, #4261, #3979, #4286, #4216 and #4301. Also moves the release date to 2026-11-30. Assisted-by: Claude Code
Pre-PR Checklist
Summary
HexGrid.find_nearest_cellreturns the wrong cell for points close to an edge of a toroidal grid, because the KD-tree does not know about the wrap-around.Bug / Issue
The tree is built from the cell positions only. For
torus=Truethe query point is wrapped into the grid, but the nearest cell can be on the other side of an edge, and the tree cannot see it. For example on a 4x4 torus, the point(0.0, 5.9)returns the cell aty=4.5, 1.4 away, while the cell across the top/bottom edge is 0.87 away. I did not find an existing issue for this. (#3958 and #3959 are about the odd-r/even-r layout, which is a separate thing. This is independent of that choice.)I compared
find_nearest_cellwith a brute force search over wrapped distances for 2000 random points per grid. On 4x4, 6x4, 6x6, 8x2 and 4x8 torus grids, between 11% and 28% of the answers were wrong. The non-torus grids had none.Implementation
KDTreesupports periodic boundaries throughboxsize. Whentorus=Truethe tree is now built withboxsize=(width * sqrt(3), height * 1.5), the same period thatfind_nearest_cellalready uses to wrap the query. All cell positions lie inside that box, as the periodic tree requires.Testing
New test
test_hex_grid_torus_nearest_cell: 200 random points on a 4x6 torus, checking that the returned cell is at the minimum wrapped distance. It does not depend on the layout, so it still holds if #3959 changes the positions. It fails onmainand passes with this change.tests/discrete_spacepasses (124 tests), andruff checkandruff formatare clean.Additional Notes
I used an LLM assistant (Claude Code) to help find and fix this. I reproduced the bug myself and I can explain the change.
🤖 Generated with Claude Code