Skip to content

Commit 32c71ef

Browse files
Document FlashMD usage in LAMMPS (#19)
--------- Co-authored-by: Sanggyu "Raymond" Chong <87842409+SanggyuChong@users.noreply.github.com>
1 parent cb3b6ce commit 32c71ef

File tree

2 files changed

+243
-17
lines changed

2 files changed

+243
-17
lines changed

README.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ FlashMD: universal long-stride molecular dynamics
33

44
This repository contains custom integrators to run MD trajectories with FlashMD models. These models are
55
designed to learn and predict molecular dynamics trajectories using long strides, therefore allowing
6-
very large time steps. Before using this method, make sure you are aware of its limitations, which are
6+
very large time steps. When using this method, make sure you are aware of its limitations, which are
77
discussed in [this preprint](http://arxiv.org/abs/2505.19350).
88

99
The pre-trained models we make available are trained to reproduce ab-initio MD at the r2SCAN level of theory.
1010

11-
Quickstart
12-
----------
11+
ASE Quickstart (see below for LAMMPS)
12+
-------------------------------------
1313

1414
You can install the package with
1515

1616
```bash
1717
pip install flashmd
1818
```
1919

20-
After installation, you can run accelerated molecular dynamics with ASE as follows:
20+
After installation, you can run accelerated molecular dynamics as follows:
2121

2222
```py
2323
import ase.build
@@ -87,7 +87,7 @@ but also an energy model, which is itself just a machine-learned interatomic pot
8787
This is the energy model that the FlashMD model was trained on. You might want to use it
8888
if...
8989

90-
Case 1: you want to run FlashMD with exact energy conservation, available through the
90+
**Case 1**: you want to run FlashMD with exact energy conservation, available through the
9191
integrator's (``dyn`` above) parameter ``rescale_energy=True`` (this is enabled by
9292
default only when targeting the NVE ensemble with ``VelocityVerlet``). In that case,
9393
besides setting this flag, you should attach the energy calculator to the atoms before
@@ -103,11 +103,11 @@ atoms.calc = calculator
103103
... # running FlashMD
104104
```
105105

106-
Case 2: you want to compute energies after running FlashMD for your own analysis. In
106+
**Case 2**: you want to compute energies after running FlashMD for your own analysis. In
107107
this case, you can create the calculator just like in case 1, but possibly after running
108108
FlashMD and/or in a different script.
109109

110-
Case 3: you found something interesting during a FlashMD run and you want to confirm it
110+
**Case 3**: you found something interesting during a FlashMD run and you want to confirm it
111111
with traditional MD. Then, you can just use ASE's MD modules as usual after attaching
112112
the energy calculator:
113113

@@ -122,20 +122,34 @@ atoms.calc = calculator
122122

123123
In general, the energy models are slower and have a larger memory footprint compared to
124124
the FlashMD models. As summarized above, you should use `do_gradients_with_energy=False`
125-
to save computation and memory when you don't need forces.
125+
to save computation and memory when you do not need forces.
126+
127+
Using FlashMD in LAMMPS
128+
-----------------------
129+
130+
LAMMPS can allow you to run FlashMD with better computational and memory efficiency.
131+
Furthermore, it will give you access to more sophisticated types of simulations, such as
132+
simulations in the NPT ensemble and metadynamics. See [here](docs/lammps.md) for a guide
133+
on using FlashMD in LAMMPS.
134+
135+
Using FlashMD in i-PI
136+
---------------------
137+
138+
You can see
139+
[this cookbook recipe](https://atomistic-cookbook.org/examples/flashmd/flashmd-demo.html)
140+
for usage examples. i-PI is our most mature interface, and the one that was used to
141+
generate all our published results.
126142

127143
Disclaimer
128144
----------
129145

130-
This is experimental software and should only be used if you know what you're doing.
131-
We recommend using the i-PI integrators for any serious work and/or if you need to perform
132-
constant-pressure (NPT) molecular dynamics. You can see
133-
[this cookbook recipe](https://atomistic-cookbook.org/examples/flashmd/flashmd-demo.html)
134-
for a usage example.
135-
Given that the main issue we observe in direct MD trajectories is loss of equipartition
136-
of energy between different degrees of freedom, we recommend using a local Langevin
137-
thermostat, and to monitor the temperature of different atomic types or different
138-
parts of the simulated system.
146+
This is experimental software and should only be used if you know what you are doing.
147+
Given that the main issue we observe in FlashMD is loss of equipartition
148+
of energy between different degrees of freedom, we recommend using a Langevin
149+
thermostat, possibly monitoring the temperature of different atomic types or different
150+
parts of the simulated system. For the time being, we also recommend checking all
151+
FlashMD-powered findings with traditional MD. The energy models that were used to train
152+
FlashMD, and that we make available in this repository, can be used for this purpose.
139153

140154

141155
Publication

docs/lammps.md

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
Running FlashMD in LAMMPS
2+
=========================
3+
4+
Installation
5+
------------
6+
7+
Running in LAMMPS is supported starting from FlashMD 0.2.5. Make sure you have an
8+
updated version:
9+
10+
```bash
11+
pip install flashmd --upgrade
12+
```
13+
14+
Next, you will need to install a version of LAMMPS with FlashMD enabled. Here, we focus
15+
on installing it quickly using conda with GPU acceleration via Kokkos. For other
16+
installation configurations (compiling from source, CPU-only, etc.), as well as more
17+
details, you can refer to this
18+
[page](https://docs.metatensor.org/metatomic/latest/engines/lammps.html#how-to-install-the-code).
19+
20+
**Step 1** Find your GPU's architecture, either via the command
21+
``nvidia-smi --query-gpu=compute_cap --format=csv,noheader`` or by looking it up
22+
from this [page](https://developer.nvidia.com/cuda/gpus). Remove the . (dot) from the
23+
number you get and match it to the number from the following list:
24+
``VOLTA70, AMPERE80, AMPERE86, ADA89, HOPPER90``. For example, if you got 8.9, go for
25+
"ADA89".
26+
27+
**Step 2** Run ``conda install -c metatensor -c conda-forge "lammps-metatomic=*=cuda*ADA89*nompi*"``,
28+
where you should replace ``ADA89`` with the string you found above. If you do not have
29+
conda yet, we recommend the [Miniforge](https://github.com/conda-forge/miniforge) conda provider.
30+
On HPC systems, it is safer to execute this command on a GPU-enabled compute (or debug)
31+
node, as sometimes nvidia drivers are not present on login nodes and this can prevent
32+
conda from installing the correct GPU libraries.
33+
34+
You are now ready to run FlashMD in LAMMPS!
35+
36+
**Note:** FlashMD in LAMMPS does not support running on multiple GPUs yet, and we are
37+
actively working on this feature. However, FlashMD supports between 120k and 300k atoms
38+
even on a single GPU on modern HPC clusters.
39+
40+
**Note:** FlashMD in LAMMPS does not support running with exact energy conservation. You
41+
can use the i-PI and ASE interfaces if you need this functionality.
42+
43+
Usage
44+
-----
45+
46+
LAMMPS will need the FlashMD model and, in some cases, the energy model (MLIP) it was
47+
trained on. Here is how you can get them in the current directory from Python:
48+
49+
```py
50+
from flashmd import get_pretrained
51+
52+
time_step = 16 # in fs, also available: 1, 2, 4, 8, 32, 64, 128
53+
energy_model, flashmd_model = get_pretrained("pet-omatpes", time_step)
54+
energy_model.save("mlip.pt")
55+
flashmd_model.save(f"flashmd-{time_step}.pt")
56+
```
57+
58+
**Note:** Stick to no more than 30x the time step that you would use in normal MD for your system!
59+
60+
Here below you will see how to run different types of molecular dynamics. In all cases,
61+
you should launch LAMMPS as ``lmp -in in.flashmd -k on g 1 -pk kokkos newton on neigh half -sf kk``
62+
(assuming your input file is named ``in.flashmd``), or ``lmp -in in.flashmd`` if you
63+
want to run without Kokkos acceleration. The following sections will present some input
64+
files that you can take inspiration from. Trivially, we advise you to consult the [LAMMPS
65+
documentation](https://docs.lammps.org) for further explanation of all the parameters.
66+
67+
# NVT (Langevin thermostat)
68+
69+
Langevin dynamics is conceptually one of the easiest methods to target the NVT ensemble
70+
in molecular dynamics. Here is an input file to use a Langevin thermostat with FlashMD:
71+
72+
```
73+
# Create a block of Al -- replace it with your system
74+
units metal
75+
atom_style atomic
76+
boundary p p p
77+
lattice fcc 4.05
78+
region box block 0 5 0 5 0 5
79+
create_box 1 box
80+
create_atoms 1 box
81+
82+
# Setting the energy model is useful if you want to have a look at the energy, but it
83+
# will slow down your simulation. Uncomment it if you need it.
84+
# pair_style metatomic mlip.pt device cuda non_conservative on
85+
# pair_coeff * * 13
86+
87+
mass 1 26.9815386
88+
89+
timestep 0.016 # 16 fs here, must match the time step of the FlashMD model!
90+
91+
velocity all create 700.0 12345 mom yes rot yes dist gaussian
92+
93+
fix 0 all metatomic flashmd-16.pt types 13 device cuda
94+
fix 1 all langevin 700.0 700.0 0.1 12345
95+
96+
thermo 10
97+
98+
run 1000 # runs 16 ps
99+
```
100+
101+
Here, the "13" appearing in ``pair_coeff`` and ``fix metatomic`` is the atomic number of
102+
Al. If you have a system with multiple LAMMPS types, you will need to replace "13" by the
103+
atomic numbers corresponding to the LAMMPS atom types, in order (e.g., often "8 1" for
104+
water simulations).
105+
106+
The same goes for the mass. For example, for water,
107+
```
108+
mass 1 15.9994
109+
mass 2 1.008
110+
```
111+
FlashMD is not transferable across different masses. Therefore, if your atomic masses do
112+
not correspond to those used to train FlashMD (which are the standard atomic weights,
113+
i.e., abundance-weighted isotopic averages), an error will be raised. The same will
114+
happen if your time step does not match that of the FlashMD model.
115+
116+
# NVT (CSVR thermostat)
117+
118+
Although coupling a system with a Langevin thermostat samples the NVT ensemble
119+
correctly, it can alter the dynamical properties of the simulation. In contrast, the
120+
CSVR thermostat results in NVT sampling while leaving the dynamical properties largely
121+
unaffected.
122+
123+
```
124+
# Create a block of Al -- replace it with your system
125+
units metal
126+
atom_style atomic
127+
boundary p p p
128+
lattice fcc 4.05
129+
region box block 0 5 0 5 0 5
130+
create_box 1 box
131+
create_atoms 1 box
132+
133+
# Setting the energy model is useful if you want to have a look at the energy, but it
134+
# will slow down your simulation. Uncomment it if you need it.
135+
# pair_style metatomic mlip.pt device cuda non_conservative on
136+
# pair_coeff * * 13
137+
138+
mass 1 26.9815386
139+
140+
timestep 0.016 # 16 fs here, must match the time step of the FlashMD model!
141+
142+
velocity all create 700.0 12345 mom yes rot yes dist gaussian
143+
144+
fix 0 all metatomic flashmd-16.pt types 13 device cuda
145+
fix 1 all temp/csvr 700.0 700.0 0.1 12345
146+
147+
thermo 10
148+
149+
run 1000 # runs 16 ps
150+
```
151+
152+
Beware! When using the CSVR thermostat, although FlashMD will still very often be
153+
qualitatively correct, the dynamical properties of the simulation might be
154+
quantitatively off, especially when using large time steps. When using this thermostat,
155+
you should confirm any findings with normal MD: uncomment the ``pair_style`` and
156+
``pair_coeff`` lines, remove ``non_conservative on``, replace the ``fix metatomic`` line
157+
with ``fix 0 all nve`` and you will be ready to run traditional MD with the r2SCAN model
158+
that FlashMD was trained on.
159+
160+
# NPT
161+
162+
To run NPT, just add a pressure-control fix to the simulation. For example:
163+
164+
```
165+
# Create a block of Al -- replace it with your system
166+
units metal
167+
atom_style atomic
168+
boundary p p p
169+
lattice fcc 4.05
170+
region box block 0 5 0 5 0 5
171+
create_box 1 box
172+
create_atoms 1 box
173+
174+
# Setting the energy model is useful if you want to have a look at the energy, but it
175+
# will slow down your simulation. Uncomment it if you need it.
176+
pair_style metatomic mlip.pt device cuda
177+
pair_coeff * * 13
178+
179+
mass 1 26.9815386
180+
181+
timestep 0.016 # 16 fs here, must match the time step of the FlashMD model!
182+
183+
velocity all create 700.0 12345 mom yes rot yes dist gaussian
184+
185+
fix 0 all metatomic flashmd-16.pt types 13 device cuda
186+
fix 1 all langevin 700.0 700.0 0.1 12345 # or CSVR
187+
fix 2 all press/langevin iso 1.0 1.0 1.0 temp 700.0 700.0 67890
188+
189+
thermo 10
190+
191+
run 1000 # runs 16 ps
192+
```
193+
194+
Note that this time we uncommented the lines defining the energy model:
195+
```
196+
pair_style metatomic mlip.pt device cuda
197+
pair_coeff * * 13
198+
```
199+
(and we removed ``non_conservative on``). This is because the energy model is used to
200+
provide stresses for pressure control. This will slow your simulation down quite a
201+
bit, but we are actively working to make NPT dynamics with FlashMD more efficient!
202+
203+
# Metadynamics using PLUMED
204+
205+
To run metadynamics with PLUMED, you can just add a PLUMED fix to the simulation.
206+
For example:
207+
208+
```
209+
fix 0 all metatomic flashmd-16.pt types 13 device cuda
210+
fix 1 all langevin 700.0 700.0 0.1 12345
211+
fix 2 all plumed plumedfile plumed.dat outfile plumed.log
212+
```

0 commit comments

Comments
 (0)