MatterSim
MatterSim is a deep learning atomistic model developed by Microsoft, designed for simulating materials across various elements, temperatures, and pressures. It provides a flexible framework for energy, force, and stress calculations, and integrates with the Atomic Simulation Environment (ASE). The library is actively maintained with frequent patch releases addressing compatibility and bug fixes, typically on major versions like 1.x.
Common errors
-
ImportError: cannot import name 'Filter' from 'ase.optimize.precon'
cause The `Filter` class was moved or removed in `ase` version 3.27+. Older MatterSim versions are not compatible with this change.fixUpgrade `mattersim` to version `1.2.1` or newer: `pip install --upgrade mattersim`. -
ModuleNotFoundError: No module named 'mattersim.utils.stress'
cause Incorrect or broken import path for internal stress calculation utilities in MatterSim versions prior to 1.2.2.fixUpgrade `mattersim` to version `1.2.2` or newer: `pip install --upgrade mattersim`. -
ERROR: Package 'mattersim' requires 'torch<X.Y.Z', but you have 'torch A.B.C'.
cause Your installed `torch` version conflicts with the strict version requirement of an older `mattersim` release.fixUpgrade `mattersim` to `1.1.2` or newer: `pip install --upgrade mattersim`. Alternatively, downgrade your `torch` version to satisfy the `mattersim` requirement if upgrading `mattersim` is not an option.
Warnings
- breaking Upgrading Atomic Simulation Environment (ASE) to version 3.27 or newer may cause import errors related to `Filter` or API changes in optimizers when using older MatterSim versions.
- gotcha Older MatterSim versions (prior to 1.2.1) had compatibility issues with `setuptools` version 82 or higher, leading to import failures.
- gotcha MatterSim versions prior to 1.2.2 had an incorrect import path for stress calculation utilities, which could lead to `ImportError` when attempting to compute stress.
- deprecated Older MatterSim versions (prior to 1.1.2) had strict `torch` version pinning (`torch<2.5.0`), which could conflict with other libraries or newer hardware/software setups.
Install
-
pip install mattersim
Imports
- MatterSimModel
from mattersim.models import MatterSimModel
- MatterSimCalculator
from mattersim.calc import MatterSimCalculator
Quickstart
import torch
from ase import Atoms
from ase.optimize import LBFGS
from mattersim.models import MatterSimModel
from mattersim.calc import MatterSimCalculator
# 1. Load a pre-trained MatterSim model
# Using torch.hub.load handles model downloading automatically
model = torch.hub.load("microsoft/mattersim", "mattersim_model_v1_0")
# 2. Initialize MatterSimCalculator
# Use 'cuda' if a GPU is available, otherwise 'cpu'
calculator = MatterSimCalculator(model=model, device="cpu")
# 3. Create an ASE Atoms object and set the calculator
atoms = Atoms("H2O", positions=[[0, 0, 0], [0, 1, 0], [0.5, -0.5, 0]], cell=[10,10,10], pbc=False)
atoms.set_calculator(calculator)
# 4. Perform an energy minimization using an ASE optimizer
print(f"Initial energy: {atoms.get_potential_energy():.4f} eV")
optimizer = LBFGS(atoms)
optimizer.run(fmax=0.01) # Relax until forces are below 0.01 eV/Å
print(f"Final energy: {atoms.get_potential_energy():.4f} eV")
print(f"Relaxed positions:\n{atoms.get_positions()}")