M3GNet
- M3GNet is an universal NNP, and is now contained in Materials Graph Library (MatGL): https://matgl.ai/
Install
pip install matgl
- MatGL uses DGL (Deep Graph Library), and DGL does'nt work with the new PyTorch versions. To revolve this,
- use python version 3.12
- use
torch==2.3.0
and install dgl==2.2.1
with pip install dgl==2.2.1 -f https://data.dgl.ai/wheels/repo.html
.
Energy calculation
- Single point energy calculation can be done as follows.
- Model is built using ASE.
import warnings
import matgl
from ase.build import add_adsorbate, fcc111, molecule
from matgl.ext.ase import PESCalculator
warnings.simplefilter("ignore")
slab = fcc111('Pt', size=(3, 3, 4), vacuum=10.0)
mol = molecule("CO")
add_adsorbate(slab=slab, adsorbate=mol, height=2.5, position="fcc")
pot = matgl.load_model("M3GNet-MP-2021.2.8-PES")
slab.calc = PESCalculator(pot)
energy = slab.get_potential_energy()
print(f"Energy = {energy:5.3f} eV")
Molecular dynamics
- Molecular dynamics (using ASE) can be done as follows.
import warnings
import matgl
from ase import units
from ase.build import add_adsorbate, fcc111, molecule
from ase.constraints import FixAtoms
from ase.md import Langevin
from ase.visualize import view
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
from matgl.ext.ase import PESCalculator
warnings.simplefilter("ignore")
slab = fcc111('Pt', size=(3, 3, 4), vacuum=10.0)
mol = molecule("CO")
add_adsorbate(slab=slab, adsorbate=mol, height=2.5, position="fcc")
from ase.constraints import FixAtoms
mask = [atom.tag >= 3 for atom in slab]
slab.set_constraint(FixAtoms(mask=mask))
pot = matgl.load_model("M3GNet-MP-2021.2.8-PES")
slab.calc = PESCalculator(pot)
temperature_K = 300
timestep = 1 * units.fs
friction = 0.10 / units.fs
MaxwellBoltzmannDistribution(slab, temperature_K=temperature_K)
dyn = Langevin(slab, timestep=timestep, temperature_K=temperature_K, friction=friction, trajectory="md.traj")
dyn.run(500)
- Using pymatgen to build a model.
from pymatgen.core import Lattice, Structure
from pymatgen.io.ase import AseAtomsAdaptor
import matgl
from matgl.ext.ase import PESCalculator
import warnings
from ase import units
warnings.simplefilter("ignore")
struct = Structure.from_spacegroup("Pm-3m", Lattice.cubic(4.5), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]])
atoms = AseAtomsAdaptor.get_atoms(struct)
atoms *= [3, 3, 3]
pot = matgl.load_model("M3GNet-MP-2021.2.8-PES")
atoms.calc = PESCalculator(pot)
energy = atoms.get_potential_energy()
print(f"Energy = {energy:5.3f} eV")