.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated_examples/1-advanced/02-zbl.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_examples_1-advanced_02-zbl.py: Training a model with ZBL corrections ===================================== This tutorial demonstrates how to train a model with ZBL corrections. The training set for this example consists of a subset of the ethanol moleculs from the `rMD17 dataset `_. The models are trained using the following training options, respectively: .. literalinclude:: options-no-zbl.yaml :language: yaml .. literalinclude:: options_zbl.yaml :language: yaml As you can see, they are identical, except for the ``zbl`` key in the ``model`` section. A detailed step-by-step introduction on how to train a model is provided in the :ref:`label_basic_usage` tutorial. .. GENERATED FROM PYTHON SOURCE LINES 25-27 First, we start by importing the necessary libraries, including the integration of ASE calculators for metatensor atomistic models. .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: Python import subprocess import ase import matplotlib.pyplot as plt import numpy as np import torch from metatomic.torch.ase_calculator import MetatomicCalculator .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. code-block:: Python # Here, we run training as a subprocess, in reality you would run this from the command # line, e.g. ``mtt train options-no-zbl.yaml -o model_no_zbl.pt``. subprocess.run( ["mtt", "train", "options-no-zbl.yaml", "-o", "model_no_zbl.pt"], check=True ) subprocess.run(["mtt", "train", "options_zbl.yaml", "-o", "model_zbl.pt"], check=True) .. rst-class:: sphx-glr-script-out .. code-block:: none CompletedProcess(args=['mtt', 'train', 'options_zbl.yaml', '-o', 'model_zbl.pt'], returncode=0) .. GENERATED FROM PYTHON SOURCE LINES 48-54 Setting up the dimers --------------------- We set up a series of dimers with different atom pairs and distances. We will calculate the energies of these dimers using the models trained with and without ZBL corrections. .. GENERATED FROM PYTHON SOURCE LINES 55-68 .. code-block:: Python distances = np.linspace(0.5, 6.0, 200) pairs = {} for pair in [("H", "H"), ("H", "C"), ("C", "C"), ("C", "O"), ("O", "O"), ("H", "O")]: structures = [] for distance in distances: atoms = ase.Atoms( symbols=[pair[0], pair[1]], positions=[[0, 0, 0], [0, 0, distance]], ) structures.append(atoms) pairs[pair] = structures .. GENERATED FROM PYTHON SOURCE LINES 69-70 We now load the two exported models, one with and one without ZBL corrections .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: Python calc_no_zbl = MetatomicCalculator("model_no_zbl.pt", extensions_directory="extensions/") calc_zbl = MetatomicCalculator("model_zbl.pt", extensions_directory="extensions/") .. GENERATED FROM PYTHON SOURCE LINES 77-82 Calculate and plot energies without ZBL --------------------------------------- We calculate the energies of the dimer curves for each pair of atoms and plot the results, using the non-ZBL-corrected model. .. GENERATED FROM PYTHON SOURCE LINES 83-99 .. code-block:: Python for pair, structures_for_pair in pairs.items(): energies = [] for atoms in structures_for_pair: atoms.set_calculator(calc_no_zbl) with torch.jit.optimized_execution(False): energies.append(atoms.get_potential_energy()) energies = np.array(energies) - energies[-1] plt.plot(distances, energies, label=f"{pair[0]}-{pair[1]}") plt.title("Dimer curves - no ZBL") plt.xlabel("Distance (Å)") plt.ylabel("Energy (eV)") plt.legend() plt.tight_layout() plt.show() .. image-sg:: /generated_examples/1-advanced/images/sphx_glr_02-zbl_001.png :alt: Dimer curves - no ZBL :srcset: /generated_examples/1-advanced/images/sphx_glr_02-zbl_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/metatrain/metatrain/examples/1-advanced/02-zbl.py:87: FutureWarning: Please use atoms.calc = calc atoms.set_calculator(calc_no_zbl) .. GENERATED FROM PYTHON SOURCE LINES 100-104 Calculate and plot energies from the ZBL-corrected model -------------------------------------------------------- We repeat the same procedure as above, but this time with the ZBL-corrected model. .. GENERATED FROM PYTHON SOURCE LINES 105-121 .. code-block:: Python for pair, structures_for_pair in pairs.items(): energies = [] for atoms in structures_for_pair: atoms.set_calculator(calc_zbl) with torch.jit.optimized_execution(False): energies.append(atoms.get_potential_energy()) energies = np.array(energies) - energies[-1] plt.plot(distances, energies, label=f"{pair[0]}-{pair[1]}") plt.title("Dimer curves - with ZBL") plt.xlabel("Distance (Å)") plt.ylabel("Energy (eV)") plt.legend() plt.tight_layout() plt.show() .. image-sg:: /generated_examples/1-advanced/images/sphx_glr_02-zbl_002.png :alt: Dimer curves - with ZBL :srcset: /generated_examples/1-advanced/images/sphx_glr_02-zbl_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/metatrain/metatrain/examples/1-advanced/02-zbl.py:109: FutureWarning: Please use atoms.calc = calc atoms.set_calculator(calc_zbl) .. GENERATED FROM PYTHON SOURCE LINES 122-128 It can be seen that all the dimer curves include a strong repulsion at short distances, which is due to the ZBL contribution. Even the H-H dimer, whose ZBL correction is very weak due to the small covalent radii of hydrogen, would show a strong repulsion closer to the origin (here, we only plotted starting from a distance of 0.5 Å). Let's zoom in on the H-H dimer to see this effect more clearly. .. GENERATED FROM PYTHON SOURCE LINES 129-152 .. code-block:: Python new_distances = np.linspace(0.1, 2.0, 200) structures = [] for distance in new_distances: atoms = ase.Atoms( symbols=["H", "H"], positions=[[0, 0, 0], [0, 0, distance]], ) structures.append(atoms) for atoms in structures: atoms.set_calculator(calc_zbl) with torch.jit.optimized_execution(False): energies = [atoms.get_potential_energy() for atoms in structures] energies = np.array(energies) - energies[-1] plt.plot(new_distances, energies, label="H-H") plt.title("Dimer curve - H-H with ZBL") plt.xlabel("Distance (Å)") plt.ylabel("Energy (eV)") plt.legend() plt.tight_layout() plt.show() .. image-sg:: /generated_examples/1-advanced/images/sphx_glr_02-zbl_003.png :alt: Dimer curve - H-H with ZBL :srcset: /generated_examples/1-advanced/images/sphx_glr_02-zbl_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/metatrain/metatrain/examples/1-advanced/02-zbl.py:141: FutureWarning: Please use atoms.calc = calc atoms.set_calculator(calc_zbl) .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 17.472 seconds) .. _sphx_glr_download_generated_examples_1-advanced_02-zbl.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02-zbl.ipynb <02-zbl.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02-zbl.py <02-zbl.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02-zbl.zip <02-zbl.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_