Skip to content

Fix bug due to rounding errors in periodicity computation#146

Draft
efaulhaber wants to merge 1 commit intomainfrom
ef/fix-periodicity
Draft

Fix bug due to rounding errors in periodicity computation#146
efaulhaber wants to merge 1 commit intomainfrom
ef/fix-periodicity

Conversation

@efaulhaber
Copy link
Copy Markdown
Member

@efaulhaber efaulhaber commented Mar 17, 2026

On main, we first computed the periodic continuous coordinates and then converted to discrete cell coordinates. The first computation introduces rounding errors, sometimes resulting in coordinates outside the periodic box, which will then be converted to discrete cell coordinates outside the box as well. This leads to out-of-bounds errors.
This PR first computes the discrete cell coordinates and then applies a discrete modulo, so there are no rounding errors that can break anything.

@efaulhaber efaulhaber self-assigned this Mar 17, 2026
@efaulhaber efaulhaber added the bug Something isn't working label Mar 17, 2026
@efaulhaber efaulhaber requested a review from Copilot March 17, 2026 11:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a rounding error bug in periodicity computation by changing the approach: instead of first wrapping coordinates into the periodic box with periodic_coords and then computing cell indices (which can suffer from floating-point rounding at boundaries), it now computes cell indices from raw coordinates and then applies modular arithmetic to handle periodicity.

Changes:

  • Unified cell_coords and periodic_cell_index into generic versions that no longer dispatch on cell list type, using mod(. - 2, n_cells) + 2 for periodic wrapping
  • Removed FullGridCellList-specific specializations of cell_coords and periodic_cell_index from full_grid.jl
  • Introduced nonperiodic_cell_coords as a separate step before periodic index wrapping

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/nhs_grid.jl Refactored cell_coords to separate non-periodic coordinate computation from periodic wrapping; unified periodic_cell_index to use 2-based modulo for all cell list types
src/cell_lists/full_grid.jl Removed FullGridCellList-specific cell_coords and periodic_cell_index specializations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +609 to 613
@inline function nonperiodic_cell_coords(coords, neighborhood_search)
(; cell_size) = neighborhood_search

@inline function cell_coords(coords, periodic_box::PeriodicBox, cell_list, cell_size)
# Subtract `min_corner` to offset coordinates so that the min corner of the periodic
# box corresponds to the (0, 0, 0) cell of the NHS.
# This way, there are no partial cells in the domain if the domain size is an integer
# multiple of the cell size (which is required, see the constructor).
offset_coords = periodic_coords(coords, periodic_box) .- periodic_box.min_corner

# Add one for 1-based indexing. The min corner will be the (1, 1, 1)-cell.
return Tuple(floor_to_int.(offset_coords ./ cell_size)) .+ 1
return Tuple(floor_to_int.(coords ./ cell_size))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants