Skip to main content

Materials Graph with Three-body Interactions

Project description

GitHub licenseLintingTestingDownloads

M3GNet

M3GNet is a new materials graph neural network architecture that incorporates 3-body interactions. A key difference with prior materials graph implementations such as MEGNet is the addition of the coordinates for atoms and the 3×3 lattice matrix in crystals, which are necessary for obtaining tensorial quantities such as forces and stresses via auto-differentiation.

As a framework, M3GNet has diverse applications, including:

  • Interatomic potential development. With the same training data, M3GNet performs similarly to state-of-the-art machine learning interatomic potentials (ML-IAPs). However, a key feature of a graph-based potential is its flexibility to scale to diverse chemical spaces. One of the key accomplishments of M3GNet is the development of a universal IAP that can work across the entire periodic table of the elements by training on relaxations performed in the Materials Project.
  • Surrogate models for property predictions. Like the previous MEGNet architecture, M3GNet can be used to develop surrogate models for property predictions, achieving in many cases accuracies that better or similar to other state of the art ML models.

For detailed performance benchmarks, please refer to the publication in the References section. The Sphinx-generated API documentation is available via the Github Page.

Table of Contents

System requirements

Inferences using the pre-trained models can be ran on any standard computer. For model training, the GPU memory needs to be > 18 Gb for a batch size of 32 using the crystal training data. In our work, we used a single RTX 3090 GPU for model training.

Installation

M3GNet can be installed via pip:

pip install m3gnet

You can also directly download the source from Github and install from source.

Apple Silicon Installation

Special care may be needed for Apple Silicon (M1, M1 Pro, M1 Max, M1 Ultra) machines. Apple Silicon has extremely powerful ML capabilities. Here are the recommended steps to get m3gnet working on Apple Silicon devices.

  1. Ensure that you already have XCode and CLI installed.
  2. Install Miniconda or Anaconda.
  3. Create a Python 3.9 environment,
conda create --name m3gnet python=3.9
conda activate m3gnet
  1. First install tensorflow and its dependencies for Apple Silicon.
conda install -c apple tensorflow-deps
pip install tensorflow-macos
  1. If you wish, you can install tensorflow-metal, which helps speed up training. If you encounter weird tensorflow errors, you should uninstall tensorflow-metal and see if it fixes the errors first.
pip install tensorflow-metal 
  1. Install m3gnet but ignore dependencies (otherwise, pip will look for tensorflow).
pip install --no-deps m3gnet
  1. Install other dependencies like pymatgen, etc. manually.
pip install protobuf==3.20.0 pymatgen ase cython
  1. Once you are done, you can try running pytest m3gnet to see if all tests pass.

Usage

Structure relaxation

A M3Gnet universal potential for the periodic table has been developed using data from Materials Project relaxations since 2012. This universal potential can be used to perform structural relaxation of any arbitrary crystal as follows.

from pymatgen.core import Structure, Lattice
from m3gnet.models import Relaxer

# Init a Mo structure with stretched lattice (DFT lattice constant ~ 3.168)
mo = Structure(Lattice.cubic(3.3), 
               ["Mo", "Mo"], [[0., 0., 0.], [0.5, 0.5, 0.5]])

relaxer = Relaxer()  # This loads the default pre-trained model

relax_results = relaxer.relax(mo)

final_structure = relax_results['final_structure']
final_energy = relax_results['trajectory'].energies[-1] / 2

print(f"Relaxed lattice parameter is {final_structure.lattice.abc[0]: .3f} Å")
print(f"Final energy is {final_energy.item(): .3f} eV/atom")

We will see the following output:

Relaxed lattice parameter is  3.169 Å
Final energy is -10.859 eV/atom

The initial lattice parameter of 3.3 Å was successfully relaxed to 3.169 Å, close to the DFT value of 3.168 Å.

The final energy -10.859 eV/atom is also close to Materials Project DFT value of -10.8456 eV/atom.

The relaxation takes less than 20 seconds on a single laptop.

Molecular dynamics

Similarly the universal IAP can be used to perform molecular dynamics (MD) simulations as well.

from pymatgen.core import Structure, Lattice
from m3gnet.models import MolecularDynamics

# Init a Mo structure with stretched lattice (DFT lattice constant ~ 3.168)
mo = Structure(Lattice.cubic(3.3), 
               ["Mo", "Mo"], [[0., 0., 0.], [0.5, 0.5, 0.5]])

md = MolecularDynamics(
    atoms=mo,
    temperature=1000,  # 1000 K
    ensemble='nvt',  # NVT ensemble
    timestep=1, # 1fs,
    trajectory="mo.traj",  # save trajectory to mo.traj
    logfile="mo.log",  # log file for MD
    loginterval=100,  # interval for record the log
)

md.run(steps=1000)

After the run, mo.log contains thermodynamic information similar to the following:

Time[ps]      Etot[eV]     Epot[eV]     Ekin[eV]    T[K]
0.0000         -21.3307     -21.3307       0.0000     0.0
0.1000         -21.3307     -21.3307       0.0000     0.0
0.2000         -21.2441     -21.3087       0.0645   249.7
0.3000         -21.0466     -21.2358       0.1891   731.6
0.4000         -20.9702     -21.1149       0.1447   559.6
0.5000         -20.9380     -21.1093       0.1713   662.6
0.6000         -20.9176     -21.1376       0.2200   850.9
0.7000         -20.9016     -21.1789       0.2773  1072.8
0.8000         -20.8804     -21.1638       0.2835  1096.4
0.9000         -20.8770     -21.0695       0.1925   744.5
1.0000         -20.8908     -21.0772       0.1864   721.2

The MD run takes less than 1 minute.

Model training

You can also train your own IAP using the PotentialTrainer in m3gnet.trainers. The training dataset can include:

  • structures, a list of pymatgen Structures
  • energies, a list of energy floats with unit eV.
  • forces, a list of nx3 force matrix with unit eV/Å, where n is the number of atom in each structure. n does not need to be the same for all structures.
  • stresses, a list of 3x3 stress matrices with unit GPa (optional)

For stresses, we use the convention that compressive stress gives negative values. Stresses obtained from VASP calculations (default unit is kBar) should be multiplied by -0.1 to work directly with the model.

We use validation dataset to select the stopping epoch number. The dataset has similar format as the training dataset.

A minimal example of model training is shown below.

from m3gnet.models import M3GNet, Potential
from m3gnet.trainers import PotentialTrainer

import tensorflow as tf

m3gnet = M3GNet(is_intensive=False)
potential = Potential(model=m3gnet)

trainer = PotentialTrainer(
    potential=potential, optimizer=tf.keras.optimizers.Adam(1e-3)
)

trainer.train(
    structures,
    energies,
    forces,
    stresses,
    validation_graphs_or_structures=val_structures,
    val_energies=val_energies,
    val_forces=val_forces,
    val_stresses=val_stresses,
    epochs=100,
    fit_per_element_offset=True,
    save_checkpoint=False,
)

Datasets

The training data used to develop the universal M3GNet IAP is MPF.2021.2.8 and is hosted on figshare with DOI 10.6084/m9.figshare.19470599.

Reference

Please cite the following work:

Chi Chen, and Shyue Ping Ong. "A Universal Graph Deep Learning Interatomic Potential for the Periodic Table." 
arXiv preprint [arXiv:2202.02450](https://arxiv.org/abs/2202.02450) (2022).

Acknowledgements

This work was primarily supported by the Materials Project, funded by the U.S. Department of Energy, Office of Science, Office of Basic Energy Sciences, Materials Sciences and Engineering Division under contract no. DE-AC02-05-CH11231: Materials Project program KC23MP. This work used the Expanse supercomputing cluster at the Extreme Science and Engineering Discovery Environment (XSEDE), which is supported by National Science Foundation grant number ACI-1548562.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

m3gnet-0.0.3.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

m3gnet-0.0.3-cp39-cp39-macosx_11_0_arm64.whl (467.7 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

File details

Details for the file m3gnet-0.0.3.tar.gz.

File metadata

  • Download URL: m3gnet-0.0.3.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for m3gnet-0.0.3.tar.gz
Algorithm Hash digest
SHA256 40beac2a734012c61902e4e40fed437be731eac14b5131694e31e22108bef75b
MD5 b4649f581607bcafa893daab37288956
BLAKE2b-256 d20b9a9851172e106b40e0f8ef172dd8ec61d813d3d2f1de19505d2e540542cc

See more details on using hashes here.

File details

Details for the file m3gnet-0.0.3-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for m3gnet-0.0.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c05575b7a4e6fab56975efeeae23e62e70a476e459481fdc24a0cb3aa7efb19d
MD5 ebf44f6feb3c330fc46f11cf5f3236e7
BLAKE2b-256 0700da47c22585b3ec64030a7b6d3c4965df43159f15fd7443d43e537d684e06

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page