Skip to content

Commit 4c22a29

Browse files
Update non-conservative PET-MAD recipe to mention kokkos (#161)
* Add LAMMPS example of NC forces with RESPA * Folded bmimbcl so molecules are entire * Make step sizes consistent * Added KOKKOS instructions * Proofreading --------- Co-authored-by: Michele Ceriotti <michele.ceriotti@gmail.com>
1 parent fc5a60c commit 4c22a29

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

examples/pet-mad-nc/data/lammps-respa.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ mass 2 35.453
99
mass 3 1.00784
1010
mass 4 14.0067
1111

12-
pair_style hybrid/scaled &
13-
1.0 metatomic_1 pet-mad-latest.pt device cpu non_conservative on &
14-
-1.0 metatomic_2 pet-mad-latest.pt device cpu non_conservative on &
15-
1.0 metatomic_3 pet-mad-latest.pt device cpu non_conservative off
12+
pair_style hybrid/overlay &
13+
metatomic_1 pet-mad-latest.pt device cpu non_conservative on scale 1.0 &
14+
metatomic_2 pet-mad-latest.pt device cpu non_conservative on scale -1.0 &
15+
metatomic_3 pet-mad-latest.pt device cpu non_conservative off scale 1.0
1616

1717
pair_coeff * * metatomic_1 6 17 1 7
1818
pair_coeff * * metatomic_2 6 17 1 7

examples/pet-mad-nc/pet-mad-nc.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@
3434
#
3535

3636
import linecache
37-
import os
3837
import subprocess
3938
import time
4039

4140
import ase.io
4241

43-
# i-PI scripting utilities
42+
# visualization
4443
import chemiscope
4544
import matplotlib.pyplot as plt
46-
import metatomic.torch as mta
45+
46+
# i-PI scripting utilities
4747
from ipi.utils.parsing import read_output, read_trajectory
4848
from ipi.utils.scripting import InteractiveSimulation
4949

50-
# pet-mad ASE calculator
51-
from pet_mad.calculator import PETMADCalculator
50+
# metatomic ASE calculator
51+
from metatomic.torch.ase_calculator import MetatomicCalculator
5252

5353

5454
if hasattr(__import__("builtins"), "get_ipython"):
@@ -60,19 +60,24 @@
6060
# We first download the latest version of the PET-MAD model, and
6161
# export the model as a torchscript file.
6262

63-
# downloads the model checkpoint and export it
64-
model_filename = "pet-mad-latest.pt"
65-
if not os.path.exists(model_filename):
66-
calculator = PETMADCalculator(version="latest", device="cpu")
67-
calculator._model.save(model_filename)
63+
# download the model checkpoint and export it, using metatrain from the command line:
64+
# mtt export https://huggingface.co/lab-cosmo/pet-mad/resolve/main/models/pet-mad-latest.ckpt # noqa: E501
65+
66+
subprocess.run(
67+
[
68+
"mtt",
69+
"export",
70+
"https://huggingface.co/lab-cosmo/pet-mad/resolve/main/models/pet-mad-latest.ckpt", # noqa: E501
71+
]
72+
)
6873

6974
# %%
7075
# The model can also be loaded from this torchscript dump, which often
7176
# speeds up calculation as it involves compilation, and is functionally
7277
# equivalent unless you plan on fine-tuning, or otherwise modifying
7378
# the model.
7479

75-
calculator = mta.ase_calculator.MetatomicCalculator(model_filename, device="cpu")
80+
calculator = MetatomicCalculator("pet-mad-latest.pt", device="cpu")
7681

7782
# %%
7883
#
@@ -107,8 +112,8 @@
107112
energy_c = structure.get_potential_energy()
108113
forces_c = structure.get_forces()
109114

110-
calculator_nc = mta.ase_calculator.MetatomicCalculator(
111-
model_filename, device="cpu", non_conservative=True
115+
calculator_nc = MetatomicCalculator(
116+
"pet-mad-latest.pt", device="cpu", non_conservative=True
112117
)
113118

114119
structure.calc = calculator_nc
@@ -369,7 +374,7 @@
369374

370375
# %%
371376
# We first launch conservative and non-conservative trajectories for
372-
# reference. These use the `metatomic` interface to LAMMPS (whhich
377+
# reference. These use the `metatomic` interface to LAMMPS (which
373378
# requires a custom LAMMPS build, available through the `metatensor`
374379
# conda forge). See also `the metatomic documentation
375380
# <https://docs.metatensor.org/metatomic/latest/engines/lammps.html>`_
@@ -394,8 +399,12 @@
394399
# %%
395400
# The multiple time stepping integrator can be implemented in lammps
396401
# using a ``pair_style hybrid/overlay``, providing multiple
397-
# ``metatomic_X`` pair styles - one for the fast forces, one for
398-
# the slow force and one for the correction.
402+
# ``metatomic_X`` pair styles - one for the fast (non-conservative) forces, and two
403+
# for the slow correction (conservative minus non-conservative).
404+
# Note that you can also use ``pair_style hybrid/scaled``, which however
405+
# is affected by a `bug <https://github.com/lammps/lammps/issues/3492`_ at the
406+
# time of writing, which prevents it from working correctly with the GPU build
407+
# of LAMMPS.
399408

400409
for lineno in [12, 13, 14, 15, 17, 18, 19, 24, 27]:
401410
print(linecache.getline("data/lammps-respa.in", lineno), end="")
@@ -418,3 +427,30 @@
418427
MTS (M=8): {time_lammps_mts / 16:.4f} s/step
419428
"""
420429
)
430+
431+
# %%
432+
# Running LAMMPS on GPUs with KOKKOS
433+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
434+
# If you have a GPU available, you can achieve a dramatic speedup
435+
# by running the `metatomic` model on the GPU, which you can achieve
436+
# by setting ``device cuda`` for the `metatomic` pair style in the LAMMPS input files.
437+
# The MD integration will however still be run on the CPU, which can become the
438+
# bottleneck - especially because atomic positions need to be transfered to the GPU
439+
# at each call. LAMMPS can also be run directly on the GPU using the KOKKOS package,
440+
# see `the installation instructions
441+
# <https://docs.metatensor.org/metatomic/latest/engines/lammps.html>`_ for
442+
# the metatrain-enabled version.
443+
444+
# %%
445+
# In order to enable the KOKKOS execution, you then have to use additional command-line
446+
# arguments when running LAMMPS, e.g.
447+
# ``lmp -k on g <NGPUS> -pk kokkos newton on neigh half -sf kk``.
448+
# The commands to execute the LAMMPS simulation examples with Kokkos enabled, using
449+
# conservative, non-conservative, and MTS force evaluations, are
450+
#
451+
# .. code-block:: bash
452+
#
453+
# lmp -k on g 1 -pk kokkos newton on neigh half -sf kk -in data/lammps-c.in
454+
# lmp -k on g 1 -pk kokkos newton on neigh half -sf kk -in data/lammps-nc.in
455+
# lmp -k on g 1 -pk kokkos newton on neigh half -sf kk -in data/lammps-respa.in
456+
#

0 commit comments

Comments
 (0)