Skip to content

[FIX] wrong volume and virial sign for left-handed cells - #618

Merged
CompRhys merged 3 commits into
TorchSim:mainfrom
akwarii:fix-left-handed-cell-volume
Oct 14, 2026
Merged

CompRhys merged 3 commits into
TorchSim:mainfrom
akwarii:fix-left-handed-cell-volume

Conversation

@akwarii

@akwarii akwarii commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #616

  • Fix: det(cell) is negative for left-handed cells, so anything derived from it flipped sign. This affected SimState.volume, the NPT integrators and barostats (Langevin, Nose-Hoover, C-Rescale), the cell filters used by FIRE/BFGS/L-BFGS cell optimization, the pair-potential stress and the D3 dispersion stress. Symptoms were a negative volume, flipped cell forces and wrong pressure.

  • Add ts.transforms.cell_volume(cell), which returns |det(cell)| for single or batched [..., 3, 3] cells, and use it at every place that computed a volume inline. Previously the abs was repeated at multiple call sites, and some were missed. This helper should help to ensure this problem is not present in future code.

  • Switch the volume measurements in the existing tests, and examples/scripts/3_dynamics.py, to the helper.

Tests

  • make_left_handed test helper (tests/conftest.py) re-describes the same crystal with a left-handed cell by negating the third lattice vector.
  • test_cell_volume_handedness_invariant: helper is positive and shape-correct for single and batched cells.
  • test_volume_is_positive_for_left_handed_cell: SimState.volume is positive and equal to the right-handed value.
  • test_npt_is_independent_of_cell_handedness: NPT trajectories match between left- and right-handed cells.
  • test_cell_optimization_is_independent_of_cell_handedness: cell relaxation matches for the unit and frechet filters, with and without cell forces.
  • test_d3_stress_is_independent_of_cell_handedness: D3 stress is unchanged by handedness.

Checklist

  • 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.

@lil-lon lil-lon left a comment

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.

Thanks for the fix! I'm not a maintainer, but I noticed a few things that could be improved, so I left some comments.

Comment thread torch_sim/transforms.py Outdated
Comment thread torch_sim/integrators/npt.py Outdated
@akwarii akwarii changed the title [FIX] negative volume and virial for left-handed cells [FIX] wrong volume and virial sign for left-handed cells Oct 13, 2026
@CompRhys

Copy link
Copy Markdown
Member

Thanks for the fix!

@CompRhys
CompRhys merged commit 2e9c16f into TorchSim:main Oct 14, 2026
62 of 64 checks passed
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.

SimState.volume is signed so cell filters, D3, electrostatics and NPT misbehave with left-handed cells

3 participants