Skip to content

Conversation

@brunocucco
Copy link

This PR adds a wrapper for the CHGNet model (Deng et al., Nat. Mach. Intell 5, 1031–1041, 2023) to TorchSim. CHGNet is a pretrained universal neural network potential for charge-informed atomistic modeling.

Changes Made

  • Added a new model: torch_sim/models/chgnet.py - CHGNetModel wrapper
  • Added tests: tests/models/test_chgnet.py - Comprehensive test suite
  • Added example: examples/scripts/1_Introduction/1.4_CHGNet.py - Demonstration
  • Added dependencies: Added chgnet>=0.4.2 to pyproject.toml

Features

  • Inherits from ModelInterface (consistent with other models)
  • Supports batched calculations for multiple systems
  • Handles periodic boundary conditions correctly
  • Integrates with TorchSim workflows (optimization, dynamics)

Testing

  • All tests pass: python -m pytest tests/models/test_chgnet.py -v
  • Example script runs successfully and results are in-line with MACE.

Notes

  • CHGNet uses different energy scales than other models (Handled and documented at torch_sim/models/chgnet.py)
  • Implementation follows TorchSim patterns already in-place for MACE and FairChem.
  • Example script demonstrates CHGNet vs MACE comparison showing both models correctly minimize forces and predict similar values.

How to Test

pip install chgnet>=0.4.2
python -m pytest tests/models/test_chgnet.py -v
python examples/scripts/1_Introduction/1.4_CHGNet.py

- Integrates CHGNet (Crystal Hamiltonian Graph Neural Network), a neural network potential developed by Ceder's Group. Similar to MACE, CHGNet is a message-passing network trained on the Materials Project database, however, it uses a different architecture. Some of these differences include: (1) CHGNet includes magnetic moment calculations, (2) uses charge-informed graph features, (3) trained on a different set of Materials Project data.
- CHGNetModel wrapper inherits from ModelInterface
- Add comprehensive test suite
- Create example script demonstrating CHGNet usage with MACE comparison
- Add chgnet>=0.4.2 as optional dependency in pyproject.toml
- Support batched calculations, PBC, and TorchSim workflows
- Include proper error handling for missing dependencies
@abhijeetgangan
Copy link
Collaborator

Thanks for the contribution! I left some comments and happy to merge once those are added. Also, it would be good to the model in CI in test.yml in test-model. On a side note would it make sense to add other outputs of CHGNet?

Copy link
Collaborator

@orionarcher orionarcher left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! CHGNet will be great to have.

Would you mind working through the steps described in Adding New Models? You've already nailed 1 and 2 but are missing a few steps to make sure the interface is fully tested.

3. Add torch_sim.models.tests.make_validate_model_outputs_test and torch_sim.models.tests.make_model_calculator_consistency_test as models tests. See any of the other model tests for examples.

4. Update test.yml to include proper installation and testing of the relevant model.

5. Pull the model import up to torch_sim.models by adding import to torch_sim.models.__init__.py in try except clause.

6. Update docs/conf.py to include model in autodoc_mock_imports = [...]

@orionarcher orionarcher added ecosystem Comp-chem ecosystem related models Model-related issues and PRs labels Oct 21, 2025
@brunocucco
Copy link
Author

Hi,

Thank you for all the comments and suggestions, happy to contribute. I've been working on those and will update the PR as soon as possible. Best.

- Expanded CHGNet model wrapper to include stress and magnetic moments.
- Add custom ASE calculator for consistency testing.
- Integrate CHGNet into CI pipeline and documentation.
- Enhanced example script with stress/magnetic moment output and reduced printing with a single clean table.
- Rewrote tests to follow MatterSim, removing redundant tests.
@abhijeetgangan
Copy link
Collaborator

Looks like the test for chgnet are failing. Good to merge once the comment is resolved and the failing tests are fixed.

Copy link
Collaborator

@orionarcher orionarcher left a comment

Choose a reason for hiding this comment

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

Just a couple comments. As @abhijeetgangan pointed out, the existing ASE CHGNet calculator should be used and the tests should be fixed. Otherwise, nice work!

orionarcher and others added 2 commits October 27, 2025 15:10
- Replace custom CHGNetCalculator with official one from chgnet.model.dynamics
- Add stress unit conversion (GPa to eV/A^3) to match ASE calculator
- Remove redundant atomic numbers validation
- Remove redundant test test_chgnet_missing_atomic_numbers
- Fixed failing tests
@abhijeetgangan
Copy link
Collaborator

Looks like the CHGnet is moved to matgl as per the latest changes on that repo. Maybe we should adapt to that repo?

Also, the tests are failing on mac os.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem Comp-chem ecosystem related models Model-related issues and PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants