Skip to content

Machine‐Learning Force Fields

Julien Steffen edited this page May 22, 2024 · 5 revisions

The new Machine Learning Force field (ML-FF) within VASP allows for the on-the-fly generation of force fields from AIMD trajectories. In total, four different calculation modes are available:

  1. On-the-fly training (here): Generate a new training set and a force field.
  2. Reselection of local reference configurations (here) from a given training set.
  3. Generation of a fast ML-FF (here) from a given trainset.
  4. Application of a generated ML-FF (here), either MD calculations or geometry optimizations.

On The Fly Training

The input for such a calculation is similar to that for an AIMD simulation (see dedicated section). First, set up such a calculation for the system of interest (either NVT or NpT), then add the following keywords to the INCAR file:

  • ML_LMLFF = .TRUE. Activates the ML-FF mode (master keyword).
  • ML_MODE = train Activates the learning. It starts from scratch if a new calculation without previous training set data is done and continues with a given training set, if this is given in the ML_AB file.
  • ML_MB = [number] The number of basis functions (local reference configurations) per element. Depending on the complexity and flexibility of a system, this number could be grow large. For the start 2000-4000 should be sufficient. If the maximum value is reached, old configurations are automatically replaced by new ones during learning and the number is kept.
  • ML_MCONF = [number] The number of collected reference configurations (full system screenshots). Should be set to 3000-4000.
  • ML_LBASIS_DISCARD = .TRUE. The calculation will not aborted if the number of collected reference configurations (ML_MCONF) is reached. Instead, the first configuration is deleted. Important for large systems, where ML_MCONF cannot be raised arbitrarily due to memory problems!
  • ML_RCUT1 = [value] The cutoff for the distance (pair) descriptors in Å. The default value (since VASP 6.4.2) is 8.0, before it was 5.0. Values between those should be OK mostly. The cutoff should not be larger than half of the smallest unit cell dimension! (else, image flags will be learned).
  • ML_RCUT2 = [value] The cutoff value for the angle (or three-body) descriptor in Å. Default is 5.0. Should always be smaller or equal to ML_RCUT1.
  • TEBEG = [value1] In machine learning, a temperature-ramp should always be applied. The lower temperature should be low enough to enable a stable simulation with the early force field, but should not be too low, since the calculation might take too long and many unnecessary low-energy configurations are sampled. 300-500 K should be OK.
  • TEEND = [value2] The final value of the heating ramp should be 1.2 or 1.3 times the maximum application temperature of the ML-FF. If, e.g., a ML-FF shall be applied for calculations up to 1000 K, TEEND = 1250 might be a reasonable choice.

An on-the-fly learning can take several days for weeks. In such a case it is advisable to use our ml_long.sh script, which also enables steered MD options.

After learning, the (expected) quality of the force field can directly be analyzed by postprocessing the ML_LOGFILE file. The predicted energy+force component error can be printed out by:

grep ERR ML_LOGFILE

If you want to restart a learning trajectory of a system at a certain point without the ml_long.sh script, it is important to the ML_CTIFOR parameter, which avoids the automatic dense sampling of DFT reference at the beginning of a trajectory. First evaluate the ML_LOGFILE of the previous calculation:

grep THRUPD ML_LOGFILE

Which gives something like the following:

  .....
 # THRUPD ##################################################### ...
 # THRUPD            nstep      ctifor_prev       ctifor_new    ... 
 # THRUPD                2                3                4    ...
 # THRUPD ##################################################### ...
 THRUPD                 13   2.00000000E-03   3.46063871E-02    ...
 THRUPD                 26   3.46063871E-02   3.11928675E-02    ...
  ....
 THRUPD             434606   2.33277435E-02   2.29238666E-02    ...
 THRUPD             459015   2.29238666E-02   2.30776577E-02    ...
 THRUPD             466213   2.30776577E-02   2.31027590E-02    ...

The last ctifor_new value needs to be taken (2.31027590E-02) and given as keyword in the INCAR file for the next run: ML_CTIFOR = 31027590E-02 in this case. Further, the start temperature TEBEG` must be recalculated based on the fraction of the total trajectory already sampled.

Reselection of Training Set Data

If a learning has been finished, all relevant training set information is stored into the ML_ABN file. In the header section of this file, a long list of basis functions is noted. This is a collection of all atoms that are included into the actual ML-FF parametrization. The environments around those atoms (within the chosen cutoff) are the reference environments compared with the actual structure during the ML-FF application.

It is possible to reselect this list from the given structures within a ML_AB file. For this take the input files of a ML-FF on-the-fly learning calculation and rename the obtained ML_ABN file to ML_AB. Then, change the ML_MODE keyword in the INCAR file:

  • ML_MODE = Select Activates the selection mode
  • NSW = 1 Only one energy evaluation (optional)
  • ML_CDOUB = 4 Error threshold multiplier (optional)

The optional settings are set automatically by the ML_MODE keyword. If nothing else is changed, the reselection will just select the same set of basis functions.

A common approach is to raise the number of basis functions, which often needs to much memory if done in the on-the-fly learning. One could for example raise the number of basis functions ML_MB from 3000 in an on-the-fly learning to 8000 in the reselection:

  • ML_MB = 8000

Further, cutoff radii can be changed, which also will have an influence on the basis function selection. The respective keywords are:

  • ML_RCUT1 = [value] The radial cutoff in Angstrom
  • ML_RCUT2 = [value] The angular cutoff in Angstrom

Unfortunately, selection calculations cannot be restarted. Especially for large number of configurations (ML_MCONF) the selection calculation can take several days, imposing a serious problem if done on a calculation cluster with walltime limit.

In order to avoid that and also to make other types of calculations like the combination of several ML_AB files possible, we have developed the program mlff_select (see wiki page). With this, the reselection can be accelerated significantly, to usually less than one hour, and arbitrarily large and complicated training sets can be processed.

Generate a fast ML-FF

Although on-the-fly learning and reselection calculations generate ML_FF files, these are usually less suited for actual production calculations, since their application is way too slow.

The new fast mode avoids this problem. To generate a fast ML-FF, copy all input files from an on-the-fly learning or selection calculation and add the following keywords to the INCAR file:

  • ML_MODE = Refit Activates the refitting to a fast ML-FF
  • ML_LFAST = .True. A fast ML-FF will be generated (optional)
  • NSW = 1 Only one energy evaluation (optional)

The optional keywords are set automatically be the ML_MODE keyword.

This calculation usually only takes some minutes to some hours in the worst case and produces a ML_FFN file which can be used for production runs.

If this mode is used to generate a ML_FF file from a ML_AB file obtained my the mlff_select program, it is important to essentially deactivate the removal of basis functions, which is done by default! This can be done by adding the following keyword:

  • ML_EPS_LOW = 1E-20 Sets the throughput for basis functions to remain so low that almost none of them should be removed during the refit process.

Apply a ML-FF

After on-the-fly learning, optional reselection and fitting to a fast ML-FF, production runs without any DFT calculations can be done. Here, always the fast ML-FF should be used!

Both geometry optimizations and molecular dynamics simulations can be done. All usual settings for those calculations should be adopted from usual DFT calculations first (without the electronic structure keywords).

Besides the usual input files (INCAR, POTCAR, KPOINTS and POSCAR), the ML_FF file needs to be present as well! Further, the main ML-FF flags need to be activated in the INCAR file:

  • ML_LMLFF = .TRUE. Activates machine learning
  • ML_LFAST = .TRUE. Activates fast ML-FFs
  • ML_IALGO_LINREG = 4 Always needed for fast ML-FFs, else, an error is thrown!

All other settings (number of basis functions and configurations, cutoffs) depend on the current force field. They can be checked by looking at the header line of the ML-FF file (which, apart from the first line, is a binary file).

Simply type

head -1 ML_FF

and a complete list of all relevant keywords and information (number of elements, basis functions per element etc.) is printed:

ML_FF 0.2.1 binary { "date" : "2023-08-03T10:45:54.138", "ML_LFAST" : True, "ML_DESC_TYPE" :   0, "types" : [ "Ga", "Pt" ], "training_structures" : 4619, "local_reference_cfgs" : [ 5500, 5437 ], "descriptors" : [ 476, 476 ], "ML_IALGO_LINREG" : 4, "ML_RCUT1" :  5.0000E+00, "ML_RCUT2" :  5.0000E+00, "ML_W1" :  1.0000E-01, "ML_SION1" :  5.0000E-01, "ML_SION2" :  5.0000E-01, "ML_LMAX2" : 3, "ML_MRB1" : 12, "ML_MRB2" : 8, "ML_IWEIGHT" : 3, "ML_WTOTEN" :  1.0000E+00, "ML_WTIFOR" :  1.0000E+00, "ML_WTSIF" :  1.0000E+00 

Optionally, all keywords can also be set in the INCAR, e.g.:

  • ML_MCONF = 5000
  • ML_MB = 5500
  • ML_RCUT1 = 5.0
  • ML_RCUT2 = 5.0

Besides these PES-specific keywords, it is often helpful to reduce the output which can become really huge for long MD trajectories. Useful settings are:

  • ML_OUTPUT_MODE = 0 Reduced output in each time step (e.g., no radial distribution functions)
  • ML_OUTBLOCK = [number] Only each N'th frame is printed out in OUTCAR and XDATCAR. Values between 10 and 50 might be useful.