`unwrap_positions` gives wrong results for triclinic cells
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 85/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- machine-learning
Research direction
Open torch_sim/transforms.py and read unwrap_positions next to pbc_wrap_batched, which treats the columns of cell as lattice vectors. Transpose cells once at the top of unwrap_positions so both the constant-cell and the (T, n_systems, 3, 3) branch use the same convention. Run the reproducer in the issue to confirm the max error drops to ~0, then extend the existing cubic-cell test with a triclinic cell so the regression is covered.
Written by the indexing model from the issue text.
Description
torch_sim.transforms.unwrap_positions seems to assume the cell rows are the lattice vectors. It computes fractional coordinates as positions @ inv(cell) and converts back with dfrac @ cell. But SimState.cell, and pbc_wrap_batched, store the lattice vectors as columns. For orthorhombic cells the two are the same, so the current test, which uses a cubic cell, passes. For a triclinic cell the unwrapped trajectory comes out wrong. Both the constant-cell and the time-dependent (T, n_systems, 3, 3) paths are affected.
Repro (CPU, main @ 35306ec):
import torch
import torch_sim.transforms as tst
dtype = torch.float64
cell = torch.tensor([[6.0, 0.0, 0.0],
[1.5, 5.0, 0.0],
[1.0, 1.2, 4.5]], dtype=dtype).T.unsqueeze(0) # columns = a, b, c
system_idx = torch.zeros(2, dtype=torch.long)
start = torch.tensor([[5.5, 4.5, 4.0], [1.0, 1.0, 1.0]], dtype=dtype)
step = torch.tensor([[0.3, 0.25, 0.2], [-0.2, 0.1, -0.15]], dtype=dtype)
true_pos = torch.stack([start + i * step for i in range(20)])
wrapped = torch.stack([tst.pbc_wrap_batched(p, cell, system_idx) for p in true_pos])
out = tst.unwrap_positions(wrapped, cell, system_idx)
print((out - true_pos).abs().max()) # expected ~0
print((tst.unwrap_positions(wrapped, cell.mT, system_idx) - true_pos).abs().max())
Expected: a max error of about 0.
Actual: a max error of 2.5. For example, the last frame of atom 0 should be [11.2, 9.25, 7.8] but comes out as [8.7, 9.55, 10.0]. Passing cell.mT gives about 1e-15, so it's only the convention.
Suggested fix: transpose the cell at the top of unwrap_positions (cells = cells.mT), which covers both branches, and add a triclinic case to the tests. I have a small patch with a regression test and can open a PR if that's welcome.
Disclosure: I used an AI assistant to help find and write up this bug, and I checked the repro myself.
- Dominant language
- Python
- Stars
- 502
- Forks
- 108
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 12
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TorchSim/torch-sim
-
Autobatcher memory probe does not recognize cuequivariance out-of-memory errorsPossibly taken @lil-lon claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
TorchSim/torch-sim#627 · 4 comments · 1 reaction ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 2 days
-
Add MLIPOps backendsOpenhelp wanted ml-eng-friendly
Difficulty 5/5 Over a week Newbie friendliness 25/100
TorchSim/torch-sim#606 · 6 comments · 1 reaction ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
TorchSim/torch-sim#597 · 7 comments ·
Maintainers usually reply within 2 days
-
Per-system pbc: batching periodic and non-periodic systems togetherPossibly taken A pull request linked to this issue is open or already merged. Openenhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
TorchSim/torch-sim#584 · 2 comments ·
Maintainers usually reply within 2 days
All issues in TorchSim/torch-sim
Similar issues
-
PluginProvider.storeInstance races and clobbers the dedup cache for concurrent loadsPossibly taken @Wilson0511 claimed this today. Openneeds-triage
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 2 days
-
community documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug(sdk): add_toleration_json silently drops camelCase tolerationSeconds for static dict/list inputPossibly taken @ravikumar266 claimed this today. Openarea/sdk kind/bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
kubeflow/pipelines#15347 · 3 comments ·
Maintainers usually reply within 1 day
-
predict_file with crop_model duplicates predictions once per imagePossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
weecology/DeepForest#1445 ·