Running universal machine-learned force fields: Difference between revisions
No edit summary |
|||
| Line 1: | Line 1: | ||
{{Available|6.5.0}} | {{Available|6.5.0}} | ||
Universal machine-learned force fields (uMLFF, or alternatively: uMLIP for "universal machine-learned interatomic potentials") offer a fast alternative to the computational demands of DFT. Pre-trained uMLFFs can be used as an alternative to [[:Category:Machine-learned_force_fields|{{VASP}}-native force fields]] to drive | Universal machine-learned force fields (uMLFF, or alternatively: uMLIP for "universal machine-learned interatomic potentials") offer a fast alternative to the computational demands of DFT. Pre-trained uMLFFs can be used as an alternative to [[:Category:Machine-learned_force_fields|{{VASP}}-native force fields]] to drive any {{VASP}} simulation that uses or could use prediction-only mode ({{TAG|ML_MODE|run}}). This includes [[:Category:Molecular_dynamics|molecular dynamics simulations]], [[Structure_optimization|ionic optimization]] (see {{TAG|IBRION}}), and [[:Category:Advanced_molecular-dynamics_sampling|advanced sampling techniques]]. | ||
{{NB|mind|For GRACE force fields | {{NB|mind|For GRACE force fields, most importantly if you require fine-tuning capabilities, please refer to the [[Running GRACE force fields in VASP|dedicated wiki page]].}} | ||
We utilize | We utilize the [[Plugins]] feature to call pre-trained uMLFF models via Python and have them calculate forces and stresses. | ||
{{NB|important|When employing uMLFFs, the accuracy and reliability of your calculations and results may vary. Thorough validation is advised.}} | {{NB|important|When employing uMLFFs, the accuracy and reliability of your calculations and results may vary. Thorough validation is advised.}} | ||
| Line 13: | Line 13: | ||
== Step-by-step instructions == | == Step-by-step instructions == | ||
The following section, specifically Step 2, provides three examples of running different uMLFFs in VASP. Implementations vary between models, and the first step is always to understand how to call a particular model in Python directly before trying to have it called via the | The following section, specifically Step 2, provides three examples of running different uMLFFs in VASP. Implementations vary between models, and the first step is always to understand how to call a particular model in Python directly before trying to have it called via the plugin. This also allows for easier and more comprehensive debugging. | ||
=== Step 1: Compiling | === Step 1: Compiling with Plugins support === | ||
To | To use [[Plugins]], follow the [[Makefile.include#Plugins (optional)|instructions in the dedicated section of the Makefile.include wiki page]]. Note that a re-compilation of VASP is required to enable Plugins support. | ||
=== Step 2: Setting up vasp_plugin.py === | === Step 2: Setting up vasp_plugin.py === | ||
| Line 33: | Line 33: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Copy or move the <code>vasp_plugin.py</code> file to your | Copy or move the <code>vasp_plugin.py</code> file to your calculation folder. | ||
We will now look at three example models and how to run them | We will now look at three example models and how to run them via <code>vasp_plugin.py</code>. Use a dedicated Python virtual environment to install the required packages. | ||
{{NB|mind|These examples are chosen for demonstration purposes and are not recommendations.}} | {{NB|mind|These examples are chosen for demonstration purposes and are not recommendations.}} | ||
==== Example A: Model inference with the tensorpotential package (GRACE) ==== | ==== Example A: Model inference with the tensorpotential package (GRACE) ==== | ||
First, let's look at how to wire up some GRACE force fields via the [[Plugins]] infrastructure. To this end, we can check GRACE's [https://gracemaker.readthedocs.io/en/latest/gracemaker/install/#setting-up-the-environment documentation] to find we need a Python environment, and then install the <code>tensorpotential</code> package. GRACE offers [https://github.com/ICAMS/grace-tutorial/blob/main/3-foundation-models/1-python-ase/using-grace-fm.ipynb some examples] for how to load and run their foundation models, so we adapt this approach in <code>vasp_plugin.py</code>: | : First, let's look at how to wire up some GRACE force fields via the [[Plugins]] infrastructure. To this end, we can check GRACE's [https://gracemaker.readthedocs.io/en/latest/gracemaker/install/#setting-up-the-environment documentation] to find we need a Python environment, and then install the <code>tensorpotential</code> package. GRACE offers [https://github.com/ICAMS/grace-tutorial/blob/main/3-foundation-models/1-python-ase/using-grace-fm.ipynb some examples] for how to load and run their foundation models, so we adapt this approach in <code>vasp_plugin.py</code>: | ||
<syntaxhighlight lang=py> | : <syntaxhighlight lang=py> | ||
from tensorpotential.calculator.foundation_models import GRACEModels, grace_fm | from tensorpotential.calculator.foundation_models import GRACEModels, grace_fm | ||
calculator = grace_fm(GRACEModels.GRACE_2L_OMAT) | calculator = grace_fm(GRACEModels.GRACE_2L_OMAT) | ||
| Line 53: | Line 53: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note that before running this code, you may need to download the model. Do so via <code>grace_models download GRACE_2L_OMAT</code> in a terminal of your choice. You can also get a list of all available foundation models via <code>grace_models list</code>. If you want to use a different model, remember to also update the <code>GRACEModels</code> enum in <code>vasp_plugin.py</code>. | : Note that before running this code, you may need to download the model. Do so via <code>grace_models download GRACE_2L_OMAT</code> in a terminal of your choice. You can also get a list of all available foundation models via <code>grace_models list</code>. If you want to use a different model, remember to also update the <code>GRACEModels</code> enum in <code>vasp_plugin.py</code>. | ||
{{NB|important|In case you want to run a fine-tuned GRACE model, there is another GRACE [https://github.com/ICAMS/grace-tutorial/blob/main/3-foundation-models/3a-finetuning/validate.ipynb tutorial notebook] for how to access these using the <code>TPCalculator</code>. In <code>vasp_plugin.py</code>, simply use that calculator and point to your fine-tuned model.}} | : {{NB|important|In case you want to run a fine-tuned GRACE model, there is another GRACE [https://github.com/ICAMS/grace-tutorial/blob/main/3-foundation-models/3a-finetuning/validate.ipynb tutorial notebook] for how to access these using the <code>TPCalculator</code>. In <code>vasp_plugin.py</code>, simply use that calculator and point to your fine-tuned model.}} | ||
{{NB|mind|The recommended way for running GRACE force fields in VASP is documented on a [[Running GRACE force fields in VASP|dedicated wiki page]]. This example merely demonstrates an alternative way via [[Plugins]].}} | : {{NB|mind|The recommended way for running GRACE force fields in VASP (particularly when fine-tuning capabilities are required) is documented on a [[Running GRACE force fields in VASP|dedicated wiki page]]. This example merely demonstrates an alternative way for an inference-only approach via [[Plugins]].}} | ||
==== Example B: Model inference with the UPET package ==== | ==== Example B: Model inference with the UPET package ==== | ||
Next, let's use the models of the <code>UPET</code> package (see also the [https://github.com/lab-cosmo/upet UPET documentation]). The documentation clarifies how to load different models, so install the package into your environment and then follow their instructions in <code>vasp_plugin.py</code>: | : Next, let's use the models of the <code>UPET</code> package (see also the [https://github.com/lab-cosmo/upet UPET documentation]). The documentation clarifies how to load different models, so install the package into your environment and then follow their instructions in <code>vasp_plugin.py</code>: | ||
<syntaxhighlight lang=py> | : <syntaxhighlight lang=py> | ||
from upet.calculator import UPETCalculator | from upet.calculator import UPETCalculator | ||
calculator = UPETCalculator(model="pet-mad-s", version="1.5.0", device="cuda") | calculator = UPETCalculator(model="pet-mad-s", version="1.5.0", device="cuda") | ||
| Line 72: | Line 72: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Note that most models offer device choices depending on your CPU/GPU setup and preferences. | : Note that most models offer device choices depending on your CPU/GPU setup and preferences. | ||
==== Example C: Model inference with the DeePMD-Kit package ==== | ==== Example C: Model inference with the DeePMD-Kit package ==== | ||
Next, let's use the DPA-3.1-3M-FT model. The model checkpoint needs to be downloaded separately from the package, as indicated and linked on its [https://matbench-discovery.materialsproject.org/models/dpa-3.1-3m-ft matbench-discovery] page. The documentation links to the [https://github.com/deepmodeling/deepmd-kit DeePMD-kit] package (see [https://docs.deepmodeling.com/projects/deepmd/en/stable/getting-started/install.html#install-python-interface-with-pip pip installation instructions]). Notice the file checkpoint has a <code>.pth</code> file extension, which indicates the PyTorch workflow is the one we need. | : Next, let's use the DPA-3.1-3M-FT model. The model checkpoint needs to be downloaded separately from the package, as indicated and linked on its [https://matbench-discovery.materialsproject.org/models/dpa-3.1-3m-ft matbench-discovery] page. The documentation links to the [https://github.com/deepmodeling/deepmd-kit DeePMD-kit] package (see [https://docs.deepmodeling.com/projects/deepmd/en/stable/getting-started/install.html#install-python-interface-with-pip pip installation instructions]). Notice the file checkpoint has a <code>.pth</code> file extension, which indicates the PyTorch workflow is the one we need. | ||
The DeePMD-kit instructions are relatively straightforward, so adapt them for <code>vasp_plugin.py</code>: | : The DeePMD-kit instructions are relatively straightforward, so adapt them for <code>vasp_plugin.py</code>: | ||
<syntaxhighlight lang=py> | : <syntaxhighlight lang=py> | ||
from deepmd.calculator import DP | from deepmd.calculator import DP | ||
calculator = DP(model="/path/to/dpa-3.1-3m-ft.pth") | calculator = DP(model="/path/to/dpa-3.1-3m-ft.pth") | ||
| Line 93: | Line 93: | ||
=== Step 3: Setting up your INCAR === | === Step 3: Setting up your INCAR === | ||
Add the following tags to your INCAR file (the rest of your prior setup can stay largely the same): | Add the following tags to your {{FILE|INCAR}} file (the rest of your prior setup can stay largely the same): | ||
{{TAG|PLUGINS/FORCE_AND_STRESS|True}} | {{TAG|PLUGINS/FORCE_AND_STRESS|True}} | ||
| Line 101: | Line 101: | ||
PLUGINS { | PLUGINS { | ||
FORCE_AND_STRESS = True | |||
ML_MODE = run | |||
} | } | ||
You may find one or the other style more intuitive to read; they are functionally identical. | You may find one or the other style more intuitive to read; they are functionally identical. | ||
{{NB|important|The {{TAG|ML_MODE}} and {{TAG|PLUGINS/ML_MODE}} tags differ in scope and purpose. | {{NB|important|The {{TAG|ML_MODE}} and {{TAG|PLUGINS/ML_MODE}} tags differ in scope and purpose. {{TAG|ML_MODE}} controls {{FILE|ML_FF}} interaction (not relevant here). {{TAG|PLUGINS/ML_MODE}} specifically controls whether plugin-computed forces are added to VASP-computed forces or substitute them entirely. {{TAG|PLUGINS/ML_MODE|run}} ensures substitution.}} | ||
=== Step 4: Run calculation === | === Step 4: Run calculation === | ||
Start your | Start your calculation the same way you usually would. If everything works as expected, you should notice a significant speedup and no electronic steps showing up in your {{FILE|OUTCAR}}. | ||
== Recommendations and advice == | == Recommendations and advice == | ||
| Line 129: | Line 129: | ||
[[Category:Howto]] | [[Category:Howto]] | ||
[[Category:Machine-learned force fields]] | [[Category:Machine-learned force fields]] | ||
[[Category:Forces]] | |||
Revision as of 12:59, 16 June 2026
| Mind: Available as of VASP 6.5.0 |
Universal machine-learned force fields (uMLFF, or alternatively: uMLIP for "universal machine-learned interatomic potentials") offer a fast alternative to the computational demands of DFT. Pre-trained uMLFFs can be used as an alternative to VASP-native force fields to drive any VASP simulation that uses or could use prediction-only mode (ML_MODE = run). This includes molecular dynamics simulations, ionic optimization (see IBRION), and advanced sampling techniques.
| Mind: For GRACE force fields, most importantly if you require fine-tuning capabilities, please refer to the dedicated wiki page. |
We utilize the Plugins feature to call pre-trained uMLFF models via Python and have them calculate forces and stresses.
| Important: When employing uMLFFs, the accuracy and reliability of your calculations and results may vary. Thorough validation is advised. |
Model selection
A dedicated list of publicly available models, including performance benchmarks, can be found, for example, at MaterialsProject's matbench-discovery. As a general rule of thumb, choose a model with good relevant metrics that was trained on a broad choice of datasets. Between models with similar metrics, choose a model with fewer parameters for faster inference.
Step-by-step instructions
The following section, specifically Step 2, provides three examples of running different uMLFFs in VASP. Implementations vary between models, and the first step is always to understand how to call a particular model in Python directly before trying to have it called via the plugin. This also allows for easier and more comprehensive debugging.
Step 1: Compiling with Plugins support
To use Plugins, follow the instructions in the dedicated section of the Makefile.include wiki page. Note that a re-compilation of VASP is required to enable Plugins support.
Step 2: Setting up vasp_plugin.py
Most models specify how to call them from Python. The instructions for how to install the corresponding packages and load their calculator instances will differ from model to model, but the vasp_plugin.py file should always contain (at least) the following:
calculator = ... # different for each model
from vasp.force_field import AseForceField # VASP force field wrapper class
force_field = AseForceField(calculator) # apply wrapper class
def force_and_stress(constants, additions): # to compute force and stress via the uMLFF model instead of VASP's DFT routines
force_field.force_and_stress(constants, additions)
Copy or move the vasp_plugin.py file to your calculation folder.
We will now look at three example models and how to run them via vasp_plugin.py. Use a dedicated Python virtual environment to install the required packages.
| Mind: These examples are chosen for demonstration purposes and are not recommendations. |
Example A: Model inference with the tensorpotential package (GRACE)
- First, let's look at how to wire up some GRACE force fields via the Plugins infrastructure. To this end, we can check GRACE's documentation to find we need a Python environment, and then install the
tensorpotentialpackage. GRACE offers some examples for how to load and run their foundation models, so we adapt this approach invasp_plugin.py:
from tensorpotential.calculator.foundation_models import GRACEModels, grace_fm calculator = grace_fm(GRACEModels.GRACE_2L_OMAT) from vasp.force_field import AseForceField # VASP force field wrapper class force_field = AseForceField(calculator) # apply wrapper class def force_and_stress(constants, additions): # to compute force and stress via the uMLFF model instead of VASP's DFT routines force_field.force_and_stress(constants, additions)
- Note that before running this code, you may need to download the model. Do so via
grace_models download GRACE_2L_OMATin a terminal of your choice. You can also get a list of all available foundation models viagrace_models list. If you want to use a different model, remember to also update theGRACEModelsenum invasp_plugin.py.
Important: In case you want to run a fine-tuned GRACE model, there is another GRACE tutorial notebook for how to access these using the TPCalculator. In vasp_plugin.py, simply use that calculator and point to your fine-tuned model.
|
| Mind: The recommended way for running GRACE force fields in VASP (particularly when fine-tuning capabilities are required) is documented on a dedicated wiki page. This example merely demonstrates an alternative way for an inference-only approach via Plugins. |
Example B: Model inference with the UPET package
- Next, let's use the models of the
UPETpackage (see also the UPET documentation). The documentation clarifies how to load different models, so install the package into your environment and then follow their instructions invasp_plugin.py:
from upet.calculator import UPETCalculator calculator = UPETCalculator(model="pet-mad-s", version="1.5.0", device="cuda") from vasp.force_field import AseForceField # VASP force field wrapper class force_field = AseForceField(calculator) # apply wrapper class def force_and_stress(constants, additions): # to compute force and stress via the uMLFF model instead of VASP's DFT routines force_field.force_and_stress(constants, additions)
- Note that most models offer device choices depending on your CPU/GPU setup and preferences.
Example C: Model inference with the DeePMD-Kit package
- Next, let's use the DPA-3.1-3M-FT model. The model checkpoint needs to be downloaded separately from the package, as indicated and linked on its matbench-discovery page. The documentation links to the DeePMD-kit package (see pip installation instructions). Notice the file checkpoint has a
.pthfile extension, which indicates the PyTorch workflow is the one we need.
- The DeePMD-kit instructions are relatively straightforward, so adapt them for
vasp_plugin.py:
from deepmd.calculator import DP calculator = DP(model="/path/to/dpa-3.1-3m-ft.pth") from vasp.force_field import AseForceField # VASP force field wrapper class force_field = AseForceField(calculator) # apply wrapper class def force_and_stress(constants, additions): # to compute force and stress via the uMLFF model instead of VASP's DFT routines force_field.force_and_stress(constants, additions)
Step 3: Setting up your INCAR
Add the following tags to your INCAR file (the rest of your prior setup can stay largely the same):
PLUGINS/FORCE_AND_STRESS = TruePLUGINS/ML_MODE = run
Note that this can also be written differently:
PLUGINS {
FORCE_AND_STRESS = True
ML_MODE = run
}
You may find one or the other style more intuitive to read; they are functionally identical.
Important: The ML_MODE and PLUGINS/ML_MODE tags differ in scope and purpose. ML_MODE controls ML_FF interaction (not relevant here). PLUGINS/ML_MODE specifically controls whether plugin-computed forces are added to VASP-computed forces or substitute them entirely. PLUGINS/ML_MODE = run ensures substitution.
|
Step 4: Run calculation
Start your calculation the same way you usually would. If everything works as expected, you should notice a significant speedup and no electronic steps showing up in your OUTCAR.
Recommendations and advice
- All else being equal, try picking models with good benchmark metrics, broader training datasets, and fewer parameters. The number of parameters directly affects inference speed.
Related tags and articles
Plugins, Running GRACE force fields in VASP
Files: Makefile.include
Tags: PLUGINS/ML_MODE, PLUGINS/FORCE_AND_STRESS