-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f7e100
commit 77d93d9
Showing
17 changed files
with
290 additions
and
339 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,76 @@ | ||
Getting Started | ||
=============== | ||
|
||
A minimal example | ||
----------------- | ||
A minimal example using ASE calculator | ||
-------------------------------------- | ||
|
||
The following example demonstrates how to load a pre-trained potential and make predictions for a single structure. | ||
MatterSim provides an interface to the Atomic Simulation Environment (ASE) to | ||
facilitate the use of MatterSim potentials in the popular ASE package. | ||
|
||
.. code-block:: python | ||
:linenos: | ||
import torch | ||
from ase.build import bulk | ||
from mattersim.forcefield.potential import Potential | ||
from mattersim.datasets.utils.build import build_dataloader | ||
# set up the structure | ||
si = bulk("Si", "diamond", a=5.43) | ||
# load the model | ||
potential = Potential.load(load_path="/path/to/checkpoint", device="cuda:0") | ||
from ase.units import GPa | ||
from mattersim.forcefield import MatterSimCalculator | ||
# build the dataloader that is compatible with MatterSim | ||
dataloader = build_dataloader([si], only_inference=True, model_type=model_name) | ||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
print(f"Running MatterSim on {device}") | ||
# make predictions | ||
predictions = potential.predict_properties(dataloader, include_forces=True, include_stresses=True) | ||
si = bulk("Si", "diamond", a=5.43) | ||
si.calc = MatterSimCalculator(device=device) | ||
print(f"Energy (eV) = {si.get_potential_energy()}") | ||
print(f"Energy per atom (eV/atom) = {si.get_potential_energy()/len(si)}") | ||
print(f"Forces of first atom (eV/A) = {si.get_forces()[0]}") | ||
print(f"Stress[0][0] (eV/A^3) = {si.get_stress(voigt=False)[0][0]}") | ||
print(f"Stress[0][0] (GPa) = {si.get_stress(voigt=False)[0][0] / GPa}") | ||
# print the predictions | ||
print(f"Total energy in eV: {predictions[0]}") | ||
print(f"Forces in eV/Angstrom: {predictions[1]}") | ||
print(f"Stresses in GPa: {predictions[2]}") | ||
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. | ||
|
||
Interface to ASE | ||
---------------- | ||
Batch prediction using the ``Potential`` class | ||
---------------------------------------------- | ||
|
||
MatterSim provides an interface to the Atomic Simulation Environment (ASE) to facilitate the use of MatterSim potentials in the popular ASE package. | ||
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. | ||
|
||
.. code-block:: python | ||
:linenos: | ||
import numpy as np | ||
from ase.build import bulk | ||
from ase.units import GPa | ||
from mattersim.forcefield.potential import DeepCalculator | ||
from mattersim.forcefield.potential import Potential | ||
from mattersim.datasets.utils.build import build_dataloader | ||
# same as before | ||
# set up the structure | ||
si = bulk("Si", "diamond", a=5.43) | ||
potential = Potential.load(load_path="/path/to/checkpoint", device="cuda:0") | ||
# set up the calculator | ||
calculator = DeepCalculator( | ||
potential=potential, | ||
# important! convert GPa to eV/Angstrom^3 | ||
stress_weight=GPa, | ||
) | ||
# replicate the structures to form a list | ||
structures = [si] * 10 | ||
si.calc = calculator | ||
# or | ||
si.set_calculator(calculator) | ||
# load the model | ||
potential = Potential.load(load_path="/path/to/checkpoint", device="cuda:0") | ||
print(si.get_potential_energy()) | ||
print(si.get_forces()) | ||
print(si.get_stress(voigt=False)) | ||
# build the dataloader that is compatible with MatterSim | ||
dataloader = build_dataloader(structures, only_inference=True) | ||
# make predictions | ||
predictions = potential.predict_properties(dataloader, include_forces=True, include_stresses=True) | ||
In the example above, the `DeepCalculator` class implements the ASE calculator interface. The **stress_weight** parameter is used to convert the stress tensor from GPa to :math:`\mathrm{eV}\cdot\mathrm{\mathring{A}}^{-3}`. | ||
# print the predictions | ||
print(f"Total energy in eV: {predictions[0]}") | ||
print(f"Forces in eV/Angstrom: {predictions[1]}") | ||
print(f"Stresses in GPa: {predictions[2]}") | ||
print(f"Stresses in eV/A^3: {np.array(predictions[2])*GPa}") | ||
.. warning :: | ||
By default, the ASE package assumes :math:`\mathrm{eV}\cdot\mathrm{\mathring{A}}^{-3}` for the stress tensor. However, MatterSim uses GPa for the stress tensor. Therefore, the **stress_weight** parameter is necessary to convert the stress tensor from GPa to :math:`\mathrm{eV}\cdot\mathrm{\mathring{A}}^{-3}`. | ||
By default, MatterSim ``potential.predict_properties`` predicts stress tensors in GPa. | ||
To convert the stress tensor to :math:`\mathrm{eV}\cdot\mathrm{\mathring{A}}^{-3}`, | ||
multiply the stress tensor by the conversion factor ``GPa``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.