Running machine-learned force fields in LAMMPS: Difference between revisions

From VASP Wiki
No edit summary
No edit summary
Line 1: Line 1:
= Quick How-To for experienced '''VASP'''/'''LAMMPS''' users =
{{NB|warning|Write introduction to LAMMPS!||ToDo:}}


# Just like in '''VASP''' pick a template from the <code>arch</code> directory and copy it to the base directory, e.g.
== LAMMPS input scripts ==


<pre>cp arch/makefile.include.gnu makefile.include</pre>
LAMMPS comes with its own powerful script language which allows the user to specify all relevant MD simulation parameters in a single file. Please consult the [https://docs.lammps.org/Commands_input.html LAMMPS documentation] for details. Within the LAMMPS script language the commands <code>pair_style</code> and <code>pair_coeff</code> are responsible for selecting a force field. The patch '''VASPml''' provides introduces a new <code>pair_style</code> called <code>vasp</code>. The <code>pair_style vasp</code> command does not have any additional arguments, all configurable settings are given as arguments to the <code>pair_coeff</code> command in this format:
<ol start="2" style="list-style-type: decimal;">
<li>Modify the build settings in <code>makefile.include</code> according to your system.</li>
<li>Compile a patched version of '''LAMMPS''' with support for '''VASP''' machine-learned force fields:</li></ol>
 
<pre>make lammps -j</pre>
<ol start="4" style="list-style-type: decimal;">
<li>Switch to the <code>examples/lammps/CsPbBr3</code> directory and try to run the example MD simulation, e.g. with</li></ol>
 
<pre>mpirun -np 4 ../../../bin/lmp_mpi -in in.lmp</pre>
<ol start="5" style="list-style-type: decimal;">
<li>Inspect the '''LAMMPS''' input script <code>in.lmp</code> and modify to your needs.</li></ol>
 
= Running LAMMPS with VASP machine-learned force field =
 
=== LAMMPS input scripts ===
 
'''LAMMPS''' comes with its own powerful script language which allows the user to specify all relevant MD simulation parameters in a single file. Please consult the [https://docs.lammps.org/Commands_input.html LAMMPS documentation] for details. Within the '''LAMMPS''' script language the commands <code>pair_style</code> and <code>pair_coeff</code> are responsible selecting a force field. The patch '''VASPml''' provides introduces a new <code>pair_style</code> called <code>vasp</code>. The <code>pair_style vasp</code> command does not have any additional arguments, all configurable settings are given as arguments to the <code>pair_coeff</code> command in this format:


<pre>pair_style vasp
<pre>pair_style vasp
pair_coeff * * file types</pre>
pair_coeff * * file types</pre>
The <code>pair_coeff</code> command must be followed by <code>* *</code>, then followed by the name of the '''VASP''' force field file, typically <code>ML_FF</code>. Finally, there comes a mapping from '''LAMMPS''' atom types to '''VASP''' force fiel types, e.g., <code>H O Na Cl</code> means that '''LAMMPS''' types <code>1</code>, <code>2</code>, <code>3</code> and <code>4</code> are mapped to '''VASP''' types <code>H</code>, <code>O</code>, <code>Na</code> and <code>Cl</code>, respectively. A valid example may look like this:
The <code>pair_coeff</code> command must be followed by <code>* *</code>, then followed by the name of the {{VASP}} force field file, typically <code>ML_FF</code>. Finally, there comes a mapping from LAMMPS atom types to {{VASP}} force fiel types, e.g., <code>H O Na Cl</code> means that LAMMPS types <code>1</code>, <code>2</code>, <code>3</code> and <code>4</code> are mapped to {{VASP}} types <code>H</code>, <code>O</code>, <code>Na</code> and <code>Cl</code>, respectively. A valid example may look like this:


<pre>pair_style vasp
<pre>pair_style vasp
pair_coeff * * ML_FF Pb Br Cs</pre>
pair_coeff * * ML_FF Pb Br Cs</pre>
This will map the '''LAMMPS''' atom types <code>1</code>, <code>2</code> and <code>3</code> in the [[#LAMMPS%20input%20data%20file|input data file]] to the types <code>Pb</code>, <code>Br</code> and <code>Cs</code> for which a pre-trained machine-learned force field should be present in the <code>ML_FF</code> file in the execution directory. A summary of the type mapping is provided in the screen output and the <code>log.lammps</code> file, e.g. for the example above it looks like this:
This will map the LAMMPS atom types <code>1</code>, <code>2</code> and <code>3</code> in the [[#LAMMPS%20input%20data%20file|input data file]] to the types <code>Pb</code>, <code>Br</code> and <code>Cs</code> for which a pre-trained machine-learned force field should be present in the <code>ML_FF</code> file in the execution directory. A summary of the type mapping is provided in the screen output and the <code>log.lammps</code> file, e.g. for the example above it looks like this:


<pre>  LAMMPS      pair_coeff      VASP      |            VASP force field
<pre>  LAMMPS      pair_coeff      VASP      |            VASP force field
Line 36: Line 19:
         2 &lt;---&gt; Br        &lt;---&gt; 1        |        1 &lt;---&gt; Br    &lt;---&gt; 1         
         2 &lt;---&gt; Br        &lt;---&gt; 1        |        1 &lt;---&gt; Br    &lt;---&gt; 1         
         3 &lt;---&gt; Cs        &lt;---&gt; 2        |        2 &lt;---&gt; Cs    &lt;---&gt; 2  </pre>
         3 &lt;---&gt; Cs        &lt;---&gt; 2        |        2 &lt;---&gt; Cs    &lt;---&gt; 2  </pre>
On the left side we find the mapping, the right side gives an overview of types present in the force field file. In this example, there is a one-to-one mapping, hence, the table looks pretty obvious and contains somewhat redundant information. However, it is also possible to leave out a mapping from specified '''LAMMPS''' types by supplying <code>NULL</code> instead of a valid '''VASP''' type name. This can be helpful when multiple force fields should be combined, see [https://docs.lammps.org/pair_hybrid.html <code>pair_style hybrid</code>]. Furthermore, multiple '''LAMMPS''' types may be mapped to the same '''VASP''' types. Finally, the force field file may contain types which are not used in the current MD simulation. Therefore, a more complicated example may look like this:
On the left side we find the mapping, the right side gives an overview of types present in the force field file. In this example, there is a one-to-one mapping, hence, the table looks pretty obvious and contains somewhat redundant information. However, it is also possible to leave out a mapping from specified LAMMPS types by supplying <code>NULL</code> instead of a valid {{VASP}} type name. This can be helpful when multiple force fields should be combined, see [https://docs.lammps.org/pair_hybrid.html <code>pair_style hybrid</code>]. Furthermore, multiple LAMMPS types may be mapped to the same {{VASP}} types. Finally, the force field file may contain types which are not used in the current MD simulation. Therefore, a more complicated example may look like this:


<pre>pair_coeff * * vasp ML_FF NULL Cs NULL Br Pb Br</pre>
<pre>pair_coeff * * vasp ML_FF NULL Cs NULL Br Pb Br</pre>
Line 52: Line 35:
It is important to always ensure that the type mapping is correctly set up because mixed-up types may not immediately result in errors. An MD simulation may still run and only post-processing may ultimately reveal inconsistencies which can be tedious to trace back to type-mapping mistakes.
It is important to always ensure that the type mapping is correctly set up because mixed-up types may not immediately result in errors. An MD simulation may still run and only post-processing may ultimately reveal inconsistencies which can be tedious to trace back to type-mapping mistakes.


The <code>pair_style vasp</code> expects input coordinates to be in the units of Ångström and returns energies and forces with the energy unit of eV. Hence, it is only compatible with the '''LAMMPS''' setting <code>units metal</code> in the input script, otherwise an error will occur.
The <code>pair_style vasp</code> expects input coordinates to be in the units of Ångström and returns energies and forces with the energy unit of eV. Hence, it is only compatible with the LAMMPS setting <code>units metal</code> in the input script, otherwise an error will occur.


=== LAMMPS input data file ===
== LAMMPS input data file ==


Where '''VASP''' uses POSCAR files to define the input structure (lattice and ion positions) '''LAMMPS''' uses [https://docs.lammps.org/read_data.html its own file format] to start MD simulations from. For simple cubic or orthorhombic systems the files can be manually converted with little effort. However, this becomes more cumbersome with triclinic simulation cells because '''LAMMPS''' originally only supported ''restricted triclinic'' boxes. Here, the first lattice vector is ''restricted'' to lie along the x-axis of the Cartesian coordinate system and the second vector must lie in the xy-plane. The third lattice vector can be arbitrary as long as it points out of the xy-plane and the three vectors form a right-hand system. Note that any set of lattice vectors can be transformed (rotated and/or mirrored) to fulfill these conditions without changing the physical situation. These restrictions do not apply to '''VASP''' POSCAR files and therefore ''general'' triclinic lattices need to be [https://docs.lammps.org/Howto_triclinic.html transformed] to create a valid '''LAMMPS''' input file. This task can be performed by the Python script <code>poscar2lammps_data.py</code> which is located in the <code>res</code> directory relative to the base folder. It takes two command-line arguments:
Where {{VASP}} uses POSCAR files to define the input structure (lattice and ion positions) LAMMPS uses [https://docs.lammps.org/read_data.html its own file format] to start MD simulations from. For simple cubic or orthorhombic systems the files can be manually converted with little effort. However, this becomes more cumbersome with triclinic simulation cells because LAMMPS originally only supported ''restricted triclinic'' boxes. Here, the first lattice vector is ''restricted'' to lie along the x-axis of the Cartesian coordinate system and the second vector must lie in the xy-plane. The third lattice vector can be arbitrary as long as it points out of the xy-plane and the three vectors form a right-hand system. Note that any set of lattice vectors can be transformed (rotated and/or mirrored) to fulfill these conditions without changing the physical situation. These restrictions do not apply to {{VASP}} POSCAR files and therefore ''general'' triclinic lattices need to be [https://docs.lammps.org/Howto_triclinic.html transformed] to create a valid LAMMPS input file. This task can be performed by the Python script <code>poscar2lammps_data.py</code> which is located in the <code>res</code> directory relative to the base folder. It takes two command-line arguments:


<pre>poscar2lammps_data.py &lt;in&gt; &lt;out&gt;</pre>
<pre>poscar2lammps_data.py &lt;in&gt; &lt;out&gt;</pre>
where <code>&lt;in&gt;</code> is the POSCAR input file and <code>out</code> is the resulting '''LAMMPS''' data file in restricted triclinic form. If <code>&lt;out&gt;</code> is omitted, the output file is written to <code>lammps.data</code>.
where <code>&lt;in&gt;</code> is the POSCAR input file and <code>out</code> is the resulting LAMMPS data file in restricted triclinic form. If <code>&lt;out&gt;</code> is omitted, the output file is written to <code>lammps.data</code>.


Please be aware that this script is not heavily tested and its results should be checked for consistency. Also, recent versions of '''LAMMPS''' (e.g. <code>patch_17Apr2024</code>) do support general triclinic lattices for convenience, see remarks [https://docs.lammps.org/Howto_triclinic.html#general-triclinic-simulation-boxes-in-lammps here].
Please be aware that this script is not heavily tested and its results should be checked for consistency. Also, recent versions of LAMMPS (e.g. <code>patch_17Apr2024</code>) do support general triclinic lattices for convenience, see remarks [https://docs.lammps.org/Howto_triclinic.html#general-triclinic-simulation-boxes-in-lammps here].


=== Example directory ===
== MD run in example directory ==


An example '''LAMMPS''' MD simulation of Cesium Lead Bromide can be found in the following directory relative to the '''VASPml''' base directory:
An example LAMMPS MD simulation of Cesium Lead Bromide can be found in the following directory relative to the '''VASPml''' base directory:


<pre>examples/lammps/CsPbBr3</pre>
<pre>examples/lammps/CsPbBr3</pre>
To execute, first [[#Automatic-patching-and-compilations-of-**LAMMPS**|compile the patched '''LAMMPS''' executable]] change into the directory above and run a parallel MD simulation with this command:
To execute, first [[#Automatic-patching-and-compilations-of-**LAMMPS**|compile the patched LAMMPS executable]] change into the directory above and run a parallel MD simulation with this command:


<pre>mpirun -np 4 ../../../bin/lmp_mpi -in in.lmp</pre>
<pre>mpirun -np 4 ../../../bin/lmp_mpi -in in.lmp</pre>
Here, <code>lmp_mpi</code> is the patched '''LAMMPS''' executable and <code>-in in.lmp</code> is one of its command line arguments specifying that the '''LAMMPS''' commands should be read from a script file called <code>in.lmp</code>. This file is present in the example directory and contains an already advanced MD setup for a simulation of 100 time steps sampling the NpT ensemble. <code>in.lmp</code> also specifies that the output trajectory should be written to <code>out.dump</code> and global thermodynamic properties (e.g. potential energy, pressure,…) are written to <code>out.prop</code>. The example '''LAMMPS''' script file can be easily altered to sample also NVE or NVT ensembles. Many other simulation parameters can also be modified by changing the variable values at the beginning of the file. Please have a look at the comments in <code>in.lmp</code> and visit the [https://docs.lammps.org/Manual.html '''LAMMPS''' documentation] for more information.
Here, <code>lmp_mpi</code> is the patched LAMMPS executable and <code>-in in.lmp</code> is one of its command line arguments specifying that the LAMMPS commands should be read from a script file called <code>in.lmp</code>. This file is present in the example directory and contains an already advanced MD setup for a simulation of 100 time steps sampling the NpT ensemble. <code>in.lmp</code> also specifies that the output trajectory should be written to <code>out.dump</code> and global thermodynamic properties (e.g. potential energy, pressure,…) are written to <code>out.prop</code>. The example LAMMPS script file can be easily altered to sample also NVE or NVT ensembles. Many other simulation parameters can also be modified by changing the variable values at the beginning of the file. Please have a look at the comments in <code>in.lmp</code> and visit the [https://docs.lammps.org/Manual.html LAMMPS documentation] for more information.
 
= Quick How-To for experienced {{VASP}}/LAMMPS users =
 
# Just like in {{VASP}} pick a template from the <code>arch</code> directory and copy it to the base directory, e.g.
 
<pre>cp arch/makefile.include.gnu makefile.include</pre>
<ol start="2" style="list-style-type: decimal;">
<li>Modify the build settings in <code>makefile.include</code> according to your system.</li>
<li>Compile a patched version of LAMMPS with support for {{VASP}} machine-learned force fields:</li></ol>
 
<pre>make lammps -j</pre>
<ol start="4" style="list-style-type: decimal;">
<li>Switch to the <code>examples/lammps/CsPbBr3</code> directory and try to run the example MD simulation, e.g. with</li></ol>
 
<pre>mpirun -np 4 ../../../bin/lmp_mpi -in in.lmp</pre>
<ol start="5" style="list-style-type: decimal;">
<li>Inspect the LAMMPS input script <code>in.lmp</code> and modify to your needs.</li></ol>

Revision as of 09:32, 18 October 2024

ToDo: Write introduction to LAMMPS!

LAMMPS input scripts

LAMMPS comes with its own powerful script language which allows the user to specify all relevant MD simulation parameters in a single file. Please consult the LAMMPS documentation for details. Within the LAMMPS script language the commands pair_style and pair_coeff are responsible for selecting a force field. The patch VASPml provides introduces a new pair_style called vasp. The pair_style vasp command does not have any additional arguments, all configurable settings are given as arguments to the pair_coeff command in this format:

pair_style vasp
pair_coeff * * file types

The pair_coeff command must be followed by * *, then followed by the name of the VASP force field file, typically ML_FF. Finally, there comes a mapping from LAMMPS atom types to VASP force fiel types, e.g., H O Na Cl means that LAMMPS types 1, 2, 3 and 4 are mapped to VASP types H, O, Na and Cl, respectively. A valid example may look like this:

pair_style vasp
pair_coeff * * ML_FF Pb Br Cs

This will map the LAMMPS atom types 1, 2 and 3 in the input data file to the types Pb, Br and Cs for which a pre-trained machine-learned force field should be present in the ML_FF file in the execution directory. A summary of the type mapping is provided in the screen output and the log.lammps file, e.g. for the example above it looks like this:

   LAMMPS       pair_coeff      VASP      |             VASP force field
    types       names           subtypes  |     types       names        subtypes
----------------------------------------- | -------------------------------------
        1 <---> Pb        <---> 0         |         0 <---> Pb     <---> 0        
        2 <---> Br        <---> 1         |         1 <---> Br     <---> 1        
        3 <---> Cs        <---> 2         |         2 <---> Cs     <---> 2  

On the left side we find the mapping, the right side gives an overview of types present in the force field file. In this example, there is a one-to-one mapping, hence, the table looks pretty obvious and contains somewhat redundant information. However, it is also possible to leave out a mapping from specified LAMMPS types by supplying NULL instead of a valid VASP type name. This can be helpful when multiple force fields should be combined, see pair_style hybrid. Furthermore, multiple LAMMPS types may be mapped to the same VASP types. Finally, the force field file may contain types which are not used in the current MD simulation. Therefore, a more complicated example may look like this:

pair_coeff * * vasp ML_FF NULL Cs NULL Br Pb Br

and the corresponding table could contain this information:

   LAMMPS       pair_coeff      VASP      |             VASP force field
    types       names           subtypes  |     types       names        subtypes
----------------------------------------- | -------------------------------------
        1 <---> unmapped! <---> unmapped! |         0 <---> Ca     <---> unused!
        2 <---> Cs        <---> 2         |         1 <---> Pb     <---> 0        
        3 <---> unmapped! <---> unmapped! |         2 <---> O      <---> unused!
        4 <---> Br        <---> 1         |         3 <---> Br     <---> 1        
        5 <---> Pb        <---> 0         |         4 <---> Cs     <---> 2        
        6 <---> Br        <---> 1         |

It is important to always ensure that the type mapping is correctly set up because mixed-up types may not immediately result in errors. An MD simulation may still run and only post-processing may ultimately reveal inconsistencies which can be tedious to trace back to type-mapping mistakes.

The pair_style vasp expects input coordinates to be in the units of Ångström and returns energies and forces with the energy unit of eV. Hence, it is only compatible with the LAMMPS setting units metal in the input script, otherwise an error will occur.

LAMMPS input data file

Where VASP uses POSCAR files to define the input structure (lattice and ion positions) LAMMPS uses its own file format to start MD simulations from. For simple cubic or orthorhombic systems the files can be manually converted with little effort. However, this becomes more cumbersome with triclinic simulation cells because LAMMPS originally only supported restricted triclinic boxes. Here, the first lattice vector is restricted to lie along the x-axis of the Cartesian coordinate system and the second vector must lie in the xy-plane. The third lattice vector can be arbitrary as long as it points out of the xy-plane and the three vectors form a right-hand system. Note that any set of lattice vectors can be transformed (rotated and/or mirrored) to fulfill these conditions without changing the physical situation. These restrictions do not apply to VASP POSCAR files and therefore general triclinic lattices need to be transformed to create a valid LAMMPS input file. This task can be performed by the Python script poscar2lammps_data.py which is located in the res directory relative to the base folder. It takes two command-line arguments:

poscar2lammps_data.py <in> <out>

where <in> is the POSCAR input file and out is the resulting LAMMPS data file in restricted triclinic form. If <out> is omitted, the output file is written to lammps.data.

Please be aware that this script is not heavily tested and its results should be checked for consistency. Also, recent versions of LAMMPS (e.g. patch_17Apr2024) do support general triclinic lattices for convenience, see remarks here.

MD run in example directory

An example LAMMPS MD simulation of Cesium Lead Bromide can be found in the following directory relative to the VASPml base directory:

examples/lammps/CsPbBr3

To execute, first compile the patched LAMMPS executable change into the directory above and run a parallel MD simulation with this command:

mpirun -np 4 ../../../bin/lmp_mpi -in in.lmp

Here, lmp_mpi is the patched LAMMPS executable and -in in.lmp is one of its command line arguments specifying that the LAMMPS commands should be read from a script file called in.lmp. This file is present in the example directory and contains an already advanced MD setup for a simulation of 100 time steps sampling the NpT ensemble. in.lmp also specifies that the output trajectory should be written to out.dump and global thermodynamic properties (e.g. potential energy, pressure,…) are written to out.prop. The example LAMMPS script file can be easily altered to sample also NVE or NVT ensembles. Many other simulation parameters can also be modified by changing the variable values at the beginning of the file. Please have a look at the comments in in.lmp and visit the LAMMPS documentation for more information.

Quick How-To for experienced VASP/LAMMPS users

  1. Just like in VASP pick a template from the arch directory and copy it to the base directory, e.g.
cp arch/makefile.include.gnu makefile.include
  1. Modify the build settings in makefile.include according to your system.
  2. Compile a patched version of LAMMPS with support for VASP machine-learned force fields:
make lammps -j
  1. Switch to the examples/lammps/CsPbBr3 directory and try to run the example MD simulation, e.g. with
mpirun -np 4 ../../../bin/lmp_mpi -in in.lmp
  1. Inspect the LAMMPS input script in.lmp and modify to your needs.