Getting Started#

A minimal example using ASE calculator#

MatterSim provides an interface to the Atomic Simulation Environment (ASE) to facilitate the use of MatterSim potentials in the popular ASE package.

 1import torch
 2from ase.build import bulk
 3from ase.units import GPa
 4from mattersim.forcefield import MatterSimCalculator
 5
 6device = "cuda" if torch.cuda.is_available() else "cpu"
 7print(f"Running MatterSim on {device}")
 8
 9si = bulk("Si", "diamond", a=5.43)
10si.calc = MatterSimCalculator(device=device)
11print(f"Energy (eV)                 = {si.get_potential_energy()}")
12print(f"Energy per atom (eV/atom)   = {si.get_potential_energy()/len(si)}")
13print(f"Forces of first atom (eV/A) = {si.get_forces()[0]}")
14print(f"Stress[0][0] (eV/A^3)       = {si.get_stress(voigt=False)[0][0]}")
15print(f"Stress[0][0] (GPa)          = {si.get_stress(voigt=False)[0][0] / GPa}")

In the example above, the MatterSimCalculator class implements the ASE calculator interface. However, with MatterSimCalculator, one can only predict the properties of a single structure at a time, which is not efficient for large-scale calculations to effectively utilize the GPU. Thus, we also provide a more efficient way to predict the properties of multiple structures using the Potential class.

Batch prediction using the Potential class#

The Potential class provides a more efficient way to predict the properties of multiple structures using the predict_properties method. In the following example, we demonstrate how to predict the properties of a list of structures using the Potential class.

 1import torch
 2import numpy as np
 3from ase.build import bulk
 4from ase.units import GPa
 5from mattersim.forcefield.potential import Potential
 6from mattersim.datasets.utils.build import build_dataloader
 7
 8# set up the structure
 9si = bulk("Si", "diamond", a=5.43)
10
11# replicate the structures to form a list
12structures = [si] * 10
13
14# load the model
15device = "cuda" if torch.cuda.is_available() else "cpu"
16print(f"Running MatterSim on {device}")
17potential = Potential.load(device=device)
18
19# build the dataloader that is compatible with MatterSim
20dataloader = build_dataloader(structures, only_inference=True)
21
22# make predictions
23predictions = potential.predict_properties(dataloader, include_forces=True, include_stresses=True)
24
25# print the predictions
26print(f"Total energy in eV: {predictions[0]}")
27print(f"Forces in eV/Angstrom: {predictions[1]}")
28print(f"Stresses in GPa: {predictions[2]}")
29print(f"Stresses in eV/A^3: {np.array(predictions[2])*GPa}")

Warning

By default, MatterSim potential.predict_properties predicts stress tensors in GPa. To convert the stress tensor to \(\mathrm{eV}\cdot\mathrm{\mathring{A}}^{-3}\), multiply the stress tensor by the conversion factor GPa.