|
34 | 34 | # |
35 | 35 |
|
36 | 36 | import linecache |
37 | | -import os |
38 | 37 | import subprocess |
39 | 38 | import time |
40 | 39 |
|
41 | 40 | import ase.io |
42 | 41 |
|
43 | | -# i-PI scripting utilities |
| 42 | +# visualization |
44 | 43 | import chemiscope |
45 | 44 | import matplotlib.pyplot as plt |
46 | | -import metatomic.torch as mta |
| 45 | + |
| 46 | +# i-PI scripting utilities |
47 | 47 | from ipi.utils.parsing import read_output, read_trajectory |
48 | 48 | from ipi.utils.scripting import InteractiveSimulation |
49 | 49 |
|
50 | | -# pet-mad ASE calculator |
51 | | -from pet_mad.calculator import PETMADCalculator |
| 50 | +# metatomic ASE calculator |
| 51 | +from metatomic.torch.ase_calculator import MetatomicCalculator |
52 | 52 |
|
53 | 53 |
|
54 | 54 | if hasattr(__import__("builtins"), "get_ipython"): |
|
60 | 60 | # We first download the latest version of the PET-MAD model, and |
61 | 61 | # export the model as a torchscript file. |
62 | 62 |
|
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 | +) |
68 | 73 |
|
69 | 74 | # %% |
70 | 75 | # The model can also be loaded from this torchscript dump, which often |
71 | 76 | # speeds up calculation as it involves compilation, and is functionally |
72 | 77 | # equivalent unless you plan on fine-tuning, or otherwise modifying |
73 | 78 | # the model. |
74 | 79 |
|
75 | | -calculator = mta.ase_calculator.MetatomicCalculator(model_filename, device="cpu") |
| 80 | +calculator = MetatomicCalculator("pet-mad-latest.pt", device="cpu") |
76 | 81 |
|
77 | 82 | # %% |
78 | 83 | # |
|
107 | 112 | energy_c = structure.get_potential_energy() |
108 | 113 | forces_c = structure.get_forces() |
109 | 114 |
|
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 |
112 | 117 | ) |
113 | 118 |
|
114 | 119 | structure.calc = calculator_nc |
|
369 | 374 |
|
370 | 375 | # %% |
371 | 376 | # 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 |
373 | 378 | # requires a custom LAMMPS build, available through the `metatensor` |
374 | 379 | # conda forge). See also `the metatomic documentation |
375 | 380 | # <https://docs.metatensor.org/metatomic/latest/engines/lammps.html>`_ |
|
394 | 399 | # %% |
395 | 400 | # The multiple time stepping integrator can be implemented in lammps |
396 | 401 | # 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. |
399 | 408 |
|
400 | 409 | for lineno in [12, 13, 14, 15, 17, 18, 19, 24, 27]: |
401 | 410 | print(linecache.getline("data/lammps-respa.in", lineno), end="") |
|
418 | 427 | MTS (M=8): {time_lammps_mts / 16:.4f} s/step |
419 | 428 | """ |
420 | 429 | ) |
| 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