Fix corner index ordering - #2801
Draft
willirath wants to merge 5 commits into
Draft
Conversation
Fails on main. Found while writing lcs_parcels. Co-authored-by: Claude <noreply@anthropic.com>
The index arrays were built particle-major for T/Z but corner-major for Y/X, while the reshape is corner-major. The orderings agree only when all particles share a time and depth index, so batches with a non-uniform clock or spread over depth levels were gathered from the wrong level. Derive all four index arrays from one layout in _gather_corners, and route the A-grid, C-grid velocity and C-grid tracer gathers through it. Also fixes CGrid_Tracer's np.repeat(ti) missing its repeats argument. test_nemo_3D_curvilinear_fieldset expectations recorded the old batch behaviour; they now match what each particle gets advected on its own. Co-authored-by: Claude <noreply@anthropic.com>
The per-axis index arrays were laid out with manual stride arithmetic: a running `before`, a `math.prod` of the trailing counts, and a comment spelling out the flat position of each level. Build them from the output shape instead. Each axis's levels are placed in their slot of the (T, Z, Y, X, particle) grid, broadcast over the other slots and flattened, which is exactly the inverse of the reshape that ends the function. Same index arrays, no loop-carried state. Co-authored-by: Claude <noreply@anthropic.com>
Drop the bold emphasis, the double-hyphen asides and the semicolons from the docstrings and comments added with the corner gather rework, and give the numpydoc parameter entries the `name : type` form used elsewhere. Co-authored-by: Claude <noreply@anthropic.com>
Member
|
Great that you spotted this bug @willirath, and thanks for the fix! I had a quick skim and I think your problem diagnosis and solution are sound. Let me know when you want me to do a full/thorough review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix corner index ordering in the A-grid and C-grid interpolators: the flattened gather currently in main silently produced wrong corners when particless time or depth indices were not aligned, so a particle could pick up a batch-mate's
ti/zi.Both interpolators now go through a single and tested
_gather_cornershelper that derives index arrays and output shape from the same layout.Adds a regression test that a trajectory does not depend on its batch-mate release times, plus interpolation tests for mixed per-particle indices.
Also updates the expected z values in
test_nemo_3D_curvilinear_fieldset[RK4_3D], which had recorded the buggy mixing.Reproducer with U uniform in space, varying in time, so each particle's displacement depends only on its own clock:
Particle 0 should be identical in both runs as only its batch-mates differ. Before the fix,
staggeredmoves particle 0 onto the other particle time index and the assert fails. The depth analogue is the same shape: two particles at differentzin one set get each other'szi.Checklist
main.AI Disclosure
Draft PR because it's late and I want to have a fresh pair of eyes (my own or someone else's) tomorrow.