Skip to content

Fix constraint atom_idx not remapped on reordered slice#457

Merged
curtischong merged 4 commits intoTorchSim:mainfrom
falletta:fix-constraints
Feb 18, 2026
Merged

Fix constraint atom_idx not remapped on reordered slice#457
curtischong merged 4 commits intoTorchSim:mainfrom
falletta:fix-constraints

Conversation

@falletta
Copy link
Copy Markdown
Contributor

When slicing a batched SimState with a non-sequential system order (e.g. state[[1, 0]]), select_constraint correctly filters constraint atom indices by the kept-atom mask, but the resulting indices were not remapped to reflect the new atom ordering. This caused FixAtoms (and any AtomConstraint) to target the wrong atoms in the sliced state.

This PR adds a remap step in _filter_attrs_by_index that translates constraint atom_idx from dense-mask positions to their final reordered positions, so constraints remain physically consistent after any reordering slice.

@falletta falletta changed the title Fix constraint atom_idx not remapped on reordered slice#4 Fix constraint atom_idx not remapped on reordered slice Feb 18, 2026
Comment thread tests/test_constraints.py Outdated
atom_idx so the constraint targets the same physical atoms in the new ordering.
"""
state = mixed_double_sim_state
n_sys0 = (state.system_idx == 0).sum().item()
Copy link
Copy Markdown
Collaborator

@curtischong curtischong Feb 18, 2026

Choose a reason for hiding this comment

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

maybe state.n_atoms_per_system()[0].item() is more clear

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.

Sure!

Comment thread tests/test_constraints.py Outdated
constraint = sliced.constraints[0]
assert isinstance(constraint, FixAtoms)

assert constraint.atom_idx.item() == 1
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this line of code is slightly hard to follow because I need to look around and figure out what 1 represents. can we use like a variable to describe it? Maybe we also wouldn't need to use the (state.system_idx == 0).sum().item() above. maybe we can just use constants to say: "original_system1_idx = 1" or "system1_atom_idx=1"

Then we can go something like:

assert constraint.atom_idx.item() == system1_atom_idx

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.

Ok I defined expected_atom_idx = 1 which I then use for the asserts

Comment thread tests/test_constraints.py Outdated
Comment thread torch_sim/state.py Outdated
# Remap constraint atom_idx to account for reordering by atom_indices
atom_remap = torch.empty(state.n_atoms, dtype=torch.long, device=state.device)
atom_remap[atom_indices] = torch.arange(len(atom_indices), device=state.device)
dense_to_reordered = atom_remap[torch.where(atom_mask)[0]]
Copy link
Copy Markdown
Collaborator

@curtischong curtischong Feb 18, 2026

Choose a reason for hiding this comment

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

It's hard for me to understand what dense_to_reordered means. Maybe rename to new_atom_idx

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.

Sure!

Copy link
Copy Markdown
Collaborator

@curtischong curtischong left a comment

Choose a reason for hiding this comment

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

looks good! If we can get the final rename done as well that'd be great

@curtischong curtischong enabled auto-merge (squash) February 18, 2026 20:57
@curtischong curtischong merged commit 63d683b into TorchSim:main Feb 18, 2026
68 checks passed
@falletta falletta deleted the fix-constraints branch March 26, 2026 17:48
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.

2 participants