Skip to content

fix: non-periodic systems in TorchSim graph construction#161

Merged
yanghan234 merged 3 commits into
mainfrom
fix/torchsim-nonperiodic-consistency
May 28, 2026
Merged

fix: non-periodic systems in TorchSim graph construction#161
yanghan234 merged 3 commits into
mainfrom
fix/torchsim-nonperiodic-consistency

Conversation

@yanghan234

Copy link
Copy Markdown
Contributor

Summary

Fixes the TorchSim interface to produce results consistent with the ASE calculator for non-periodic (molecular) systems.

Closes #160

Problem

The ASE calculator path (_normalize_atoms in converter.py) handles non-periodic molecules by creating a large fake periodic cell and wrapping positions into it. The TorchSim path (build_graph_from_simstate) skipped this normalization, passing pbc=False and a zero cell directly to the model. This produced different graph inputs for the same molecule, causing energy and force mismatches.

This was caught by torch-sim CI (job link), where all 4 molecular consistency tests failed (benzene, pentacene, aspirin, alanine dipeptide) while all 18 periodic systems passed.

Changes

src/mattersim/torchsim/graph_construction.py

  • Add _normalize_nonperiodic_systems() that detects fully non-periodic systems and applies the same fake-cell + position-wrapping logic as _normalize_atoms
  • Update build_graph_from_simstate() to call this normalization before create_batch_graph_dict

tests/torchsim/test_torchsim.py

  • Add unit tests verifying periodic systems pass through unchanged, non-periodic molecules get a fake cell, and that cell/positions match the ASE code path exactly
  • Add end-to-end regression test comparing TorchSimWrapper vs MatterSimCalculator on a water molecule

yanghan234 and others added 2 commits May 28, 2026 13:43
The TorchSim code path (build_graph_from_simstate) was passing raw
pbc=False and a zero cell for non-periodic molecules, while the ASE
calculator path (_normalize_atoms) creates a large fake periodic cell
and wraps positions into it. This caused the M3GNet model to receive
different graph inputs for the same molecule, producing inconsistent
energies and forces.

Add _normalize_nonperiodic_systems() that replicates the same fake-cell
normalization for the TorchSim path, ensuring both code paths feed
identical inputs to the model.

Fixes #160

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add unit tests for _normalize_nonperiodic_systems() verifying:
- Periodic systems pass through unchanged
- Non-periodic molecules get a large fake periodic cell
- Cell and positions match the ASE _normalize_atoms code path
- Original SimState tensors are not mutated

Add end-to-end test (test_wrapper_molecule_consistency) comparing
TorchSimWrapper vs MatterSimCalculator on a water molecule.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@yanghan234 yanghan234 requested a review from danielzuegner May 28, 2026 12:57
Phono3py v4 defaults to compact force constants format, changing
fc2 shape from (n_satom, n_satom, 3, 3) to (n_patom, n_satom, 3, 3).
Explicitly pass is_compact_fc=False to produce_fc2() and produce_fc3()
to maintain the full format expected by downstream code.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@yanghan234 yanghan234 merged commit 40a1eb8 into main May 28, 2026
7 checks passed
@yanghan234 yanghan234 deleted the fix/torchsim-nonperiodic-consistency branch May 28, 2026 15:18
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.

[Bug]: TorchSim Interface Inconsistent with ASE for non-periodic systems.

2 participants