Skip to content

Fix FixSymmetry constraint system_idx remapping on reordered state slicing#509

Merged
CompRhys merged 7 commits intoTorchSim:mainfrom
danielzuegner:fix/fix-symmetry-system-idx-remap
Mar 17, 2026
Merged

Fix FixSymmetry constraint system_idx remapping on reordered state slicing#509
CompRhys merged 7 commits intoTorchSim:mainfrom
danielzuegner:fix/fix-symmetry-system-idx-remap

Conversation

@danielzuegner
Copy link
Copy Markdown
Contributor

@danielzuegner danielzuegner commented Mar 17, 2026

Summary

Problem

When slicing a SimState with out-of-order system indices (e.g. state[[3, 1, 4]]), FixSymmetry's per-system data (rotations, symm_maps, reference_cells) gets applied to the wrong systems.

This is triggered during relaxation with BinningAutoBatcher, which produces non-sorted system indices from its descending-volume bin-packing algorithm.

Root cause

_filter_attrs_by_index in state.py converts system_indices to a boolean mask before passing to select_constraint. Boolean masks lose ordering information — torch.where(mask) always returns indices in ascending order. While constraint atom_idx remapping already existed to correct for this, the analogous system_idx remapping was missing.

Fix

Build both atom_remap and system_remap tables upfront, then remap both atom_idx and system_idx on each constraint in a single pass after select_constraint. This also consolidates the previously separate atom/system remapping loops.

Test

Added two tests for out-of-order slicing:

  • test_fix_symmetry_system_idx_remapped_on_reordered_slice — creates a 2-system state with distinct FixSymmetry rotations per system, slices with state[[1, 0]], and verifies each output system is paired with the correct rotation and reference cell.
  • test_fix_com_system_idx_remapped_on_reordered_slice — constrains only system 0 with FixCom, slices with state[[1, 0]], and verifies system_idx is remapped to [1].

Disclaimer: This is human-checked and verified LLM code :)

Checklist

Before a pull request can be merged, the following items must be checked:

  • Doc strings have been added in the Google docstring format.
  • Run ruff on your code.
  • Tests have been added for any new functionality or bug fixes.

Daniel Zuegner and others added 6 commits March 12, 2026 14:45
When _filter_attrs_by_index selects systems in non-ascending order (e.g.
state[[3, 1, 4]]), the constraint's system_idx was remapped using only a
boolean mask, which always produces indices in ascending order. This caused
FixSymmetry's per-system data (rotations, symm_maps, reference_cells) to
be applied to the wrong systems.

BinningAutoBatcher triggers this because to_constant_volume_bins inserts
items in descending-volume order, producing non-sorted index bins.

Add system_idx remapping for constraints in _filter_attrs_by_index,
analogous to the existing atom_idx remapping.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@CompRhys
Copy link
Copy Markdown
Member

@falletta I think you changed this code in #457 can you take a look at this

@falletta
Copy link
Copy Markdown
Contributor

I tested the code on my use case, which required the fix in #457, and everything looks good to me.

@CompRhys
Copy link
Copy Markdown
Member

Offline @falletta confirmed this fix is consistent with his fix for his workflow. Thanks for the fix!

@CompRhys CompRhys merged commit 6b0ec0c into TorchSim:main Mar 17, 2026
68 checks passed
@danielzuegner danielzuegner deleted the fix/fix-symmetry-system-idx-remap branch March 18, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants