CHGNet

Tests Codacy Badge arXiv GitHub repo size PyPI Docs Requires Python 3.10+

A pretrained universal neural network potential for charge-informed atomistic modeling (see publication) Logo Crystal Hamiltonian Graph neural Network is pretrained on the GGA/GGA+U static and relaxation trajectories from Materials Project, a comprehensive dataset consisting of more than 1.5 Million structures from 146k compounds spanning the whole periodic table.

CHGNet highlights its ability to study electron interactions and charge distribution in atomistic modeling with near DFT accuracy. The charge inference is realized by regularizing the atom features with DFT magnetic moments, which carry rich information about both local ionic environments and charge distribution.

Pretrained CHGNet achieves excellent performance on materials stability prediction from unrelaxed structures according to Matbench Discovery [repo].

Model F1 ↑ DAF ↑ Prec ↑ Acc ↑ TPR ↑ TNR ↑ MAE ↓ RMSE ↓ R2 Training Set Model Params Targets Date Added
eqV2 0.917 6.047 0.924 0.975 0.910 0.986 0.020 0.072 0.848 3M (102.4M) (OMat24+MPtrj) 86.6M EFSD 2024-10-18
ORB 0.880 6.041 0.924 0.965 0.841 0.987 0.028 0.077 0.824 3M (32.1M) (MPtrj+Alex) 25.2M EFSD 2024-10-11
MatterSim 0.859 5.646 0.863 0.957 0.856 0.975 0.026 0.080 0.812 17M (MatterSim) 182.0M EFSD 2024-06-16
GNoME 0.829 5.523 0.844 0.955 0.814 0.972 0.035 0.085 0.785 6M (89.0M) (GNoME) 16.2M EFC 2024-02-03
eqV2 DeNS 0.815 5.042 0.771 0.941 0.864 0.953 0.036 0.085 0.788 146K (1.6M) (MPtrj) 31.2M EFSD 2024-10-18
ORB MPtrj 0.765 4.702 0.719 0.922 0.817 0.941 0.045 0.091 0.756 146K (1.6M) (MPtrj) 25.2M EFSD 2024-10-14
SevenNet 0.724 4.252 0.650 0.904 0.818 0.919 0.048 0.092 0.750 146K (1.6M) (MPtrj) 842.4K EFSC 2024-07-13
MACE 0.669 3.777 0.577 0.878 0.796 0.893 0.057 0.101 0.697 146K (1.6M) (MPtrj) 4.7M EFSC 2023-07-14
CHGNet 0.613 3.361 0.514 0.851 0.758 0.868 0.063 0.103 0.689 146K (1.6M) (MPtrj) 412.5K EFSCM 2023-03-03
M3GNet 0.569 2.882 0.441 0.813 0.803 0.813 0.075 0.118 0.585 63K (188.3K) (MPF) 227.5K EFSC 2022-09-20
ALIGNN 0.567 3.206 0.490 0.841 0.672 0.872 0.093 0.154 0.297 155K (MP 2022) 4.0M E 2023-06-02
MEGNet 0.510 2.959 0.452 0.826 0.585 0.870 0.130 0.206 -0.248 133K (MP Graphs) 167.8K E 2022-11-14
CGCNN 0.507 2.855 0.436 0.818 0.605 0.857 0.138 0.233 -0.603 155K (MP 2022) 128.4K (N=10) E 2022-12-28
CGCNN+P 0.500 2.563 0.392 0.786 0.693 0.803 0.113 0.182 0.019 155K (MP 2022) 128.4K (N=10) E 2023-02-03
Wrenformer 0.466 2.256 0.345 0.745 0.719 0.750 0.110 0.186 -0.018 155K (MP 2022) 5.2M (N=10) E 2022-11-26
BOWSR 0.423 1.964 0.300 0.712 0.718 0.693 0.118 0.167 0.151 133K (MP Graphs) 167.8K E 2022-11-17
Voronoi RF 0.333 1.579 0.241 0.668 0.535 0.692 0.148 0.212 -0.329 155K (MP 2022) 26.2M E 2022-11-26
Dummy 0.185 1 0.154 0.687 0.232 0.769 0.124 0.184 0

Example notebooks

Notebooks Google Colab Descriptions
CHGNet Basics Open in Google Colab Examples for loading pre-trained CHGNet, predicting energy, force, stress, magmom as well as running structure optimization and MD.
Tuning CHGNet Open in Google Colab Examples of fine tuning the pretrained CHGNet to your system of interest.
Visualize Relaxation Open in Google Colab Crystal Toolkit app that visualizes convergence of atom positions, energies and forces of a structure during CHGNet relaxation.
Phonon DOS + Bands Open in Google Colab Use CHGNet with the atomate2 phonon workflow based on finite displacements as implemented in Phonopy to calculate phonon density of states and band structure for Si (mp-149).
Elastic tensor + bulk/shear modulus Open in Google Colab Use CHGNet with the atomate2 elastic workflow based on a stress-strain approach to calculate elastic tensor and derived bulk and shear modulus for Si (mp-149).

Installation

pip install chgnet

if PyPI installation fails or you need the latest main branch commits, you can install from source:

pip install git+https://github.com/CederGroupHub/chgnet

Tutorials and Docs

2023-11-02-sciML-webinar

See the sciML webinar tutorial on 2023-11-02 and API docs.

Usage

Direct Inference (Static Calculation)

Pretrained CHGNet can predict the energy (eV/atom), force (eV/A), stress (GPa) and magmom ($\mu_B$) of a given structure.

from chgnet.model.model import CHGNet
from pymatgen.core import Structure

chgnet = CHGNet.load()
structure = Structure.from_file('examples/mp-18767-LiMnO2.cif')
prediction = chgnet.predict_structure(structure)

for key, unit in [
    ("energy", "eV/atom"),
    ("forces", "eV/A"),
    ("stress", "GPa"),
    ("magmom", "mu_B"),
]:
    print(f"CHGNet-predicted {key} ({unit}):\n{prediction[key[0]]}\n")

Molecular Dynamics

Charge-informed molecular dynamics can be simulated with pretrained CHGNet through ASE python interface (see below), or through LAMMPS.

from chgnet.model.model import CHGNet
from chgnet.model.dynamics import MolecularDynamics
from pymatgen.core import Structure
import warnings
warnings.filterwarnings("ignore", module="pymatgen")
warnings.filterwarnings("ignore", module="ase")

structure = Structure.from_file("examples/mp-18767-LiMnO2.cif")
chgnet = CHGNet.load()

md = MolecularDynamics(
    atoms=structure,
    model=chgnet,
    ensemble="nvt",
    temperature=1000,  # in K
    timestep=2,  # in femto-seconds
    trajectory="md_out.traj",
    logfile="md_out.log",
    loginterval=100,
)
md.run(50)  # run a 0.1 ps MD simulation

The MD defaults to CUDA if available, to manually set device to cpu or mps: MolecularDynamics(use_device='cpu').

MD outputs are saved to the ASE trajectory file, to visualize the MD trajectory and magnetic moments after the MD run:

from ase.io.trajectory import Trajectory
from pymatgen.io.ase import AseAtomsAdaptor
from chgnet.utils import solve_charge_by_mag

traj = Trajectory("md_out.traj")
mag = traj[-1].get_magnetic_moments()

# get the non-charge-decorated structure
structure = AseAtomsAdaptor.get_structure(traj[-1])
print(structure)

# get the charge-decorated structure
struct_with_chg = solve_charge_by_mag(structure)
print(struct_with_chg)

To manipulate the MD trajectory, convert to other data formats, calculate mean square displacement, etc, please refer to ASE trajectory documentation.

Structure Optimization

CHGNet can perform fast structure optimization and provide site-wise magnetic moments. This makes it ideal for pre-relaxation and MAGMOM initialization in spin-polarized DFT.

from chgnet.model import StructOptimizer

relaxer = StructOptimizer()
result = relaxer.relax(structure)
print("CHGNet relaxed structure", result["final_structure"])
print("relaxed total energy in eV:", result['trajectory'].energies[-1])

Available Weights

CHGNet 0.3.0 is released with new pretrained weights! (release date: 10/22/23)

CHGNet.load() now loads 0.3.0 by default, previous 0.2.0 version can be loaded with CHGNet.load('0.2.0')

Model Training / Fine-tune

Fine-tuning will help achieve better accuracy if a high-precision study is desired. To train/tune a CHGNet, you need to define your data in a pytorch Dataset object. The example datasets are provided in data/dataset.py

from chgnet.data.dataset import StructureData, get_train_val_test_loader
from chgnet.trainer import Trainer

dataset = StructureData(
    structures=list_of_structures,
    energies=list_of_energies,
    forces=list_of_forces,
    stresses=list_of_stresses,
    magmoms=list_of_magmoms,
)
train_loader, val_loader, test_loader = get_train_val_test_loader(
    dataset, batch_size=32, train_ratio=0.9, val_ratio=0.05
)
trainer = Trainer(
    model=chgnet,
    targets="efsm",
    optimizer="Adam",
    criterion="MSE",
    learning_rate=1e-2,
    epochs=50,
    use_device="cuda",
)

trainer.train(train_loader, val_loader, test_loader)

Notes for Training

Check fine-tuning example notebook

  1. The target quantity used for training should be energy/atom (not total energy) if you’re fine-tuning the pretrained CHGNet.
  2. The pretrained dataset of CHGNet comes from GGA+U DFT with MaterialsProject2020Compatibility corrections applied. The parameter for VASP is described in MPRelaxSet. If you’re fine-tuning with MPRelaxSet, it is recommended to apply the MP2020 compatibility to your energy labels so that they’re consistent with the pretrained dataset.
  3. If you’re fine-tuning to functionals other than GGA, we recommend you refit the AtomRef.
  4. CHGNet stress is in units of GPa, and the unit conversion has already been included in dataset.py. So VASP stress can be directly fed to StructureData
  5. To save time from graph conversion step for each training, we recommend you use GraphData defined in dataset.py, which reads graphs directly from saved directory. To create saved graphs, see examples/make_graphs.py.

MPtrj Dataset

The Materials Project trajectory (MPtrj) dataset used to pretrain CHGNet is available at figshare.

The MPtrj dataset consists of all the GGA/GGA+U DFT calculations from the September 2022 Materials Project. By using the MPtrj dataset, users agree to abide the Materials Project terms of use.

Reference

If you use CHGNet or MPtrj dataset, please cite this paper:

@article{deng_2023_chgnet,
    title={CHGNet as a pretrained universal neural network potential for charge-informed atomistic modelling},
    DOI={10.1038/s42256-023-00716-3},
    journal={Nature Machine Intelligence},
    author={Deng, Bowen and Zhong, Peichen and Jun, KyuJung and Riebesell, Janosh and Han, Kevin and Bartel, Christopher J. and Ceder, Gerbrand},
    year={2023},
    pages={1–11}
}

Development & Bugs

CHGNet is under active development, if you encounter any bugs in installation and usage, please open an issue. We appreciate your contributions!