.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/ti/run_ti.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_tutorials_ti_run_ti.py: Thermodynamic integration ========================= This example shows how to perform thermodynamic integration (TI). We here demonstrate the TI from an effective harmonic potential to either EMT or MTP. Note that the ASE EMT is very slow; the timing is: - With MTP: takes around 16 s on Apple M1 Pro single core. - With ASAP EMT: takes around 12 s on Apple M1 Pro single core. - With ASE EMT: takes around 110 s on Apple M1 Pro single core. Therefore, if ASAP is available, this exmaple uses its EMT implementation. You can run the script either on a single core as ``python ./ti_example.py`` or in parallel as ``mpirun -n 9 python ./ti_example.py``, where 3 * 3 = 9 (volume, temperature) grid points are parallelized over. .. GENERATED FROM PYTHON SOURCE LINES 21-37 .. code-block:: Python from itertools import product from pathlib import Path import numpy as np from ase.build import bulk from ase.parallel import parprint import directupsampling from directupsampling.calculators.emt import EMT from directupsampling.effqh.effqh import EffQH from directupsampling.plot_tools import plot from directupsampling.ti import ThermoInt # from directupsampling.calculators.mtp import MTP .. GENERATED FROM PYTHON SOURCE LINES 38-39 Get pretrained EffQH and the target calculator. .. GENERATED FROM PYTHON SOURCE LINES 39-46 .. code-block:: Python resource_path = Path(directupsampling.__file__).parents[1] / "tests/resources" calc = EMT() # calc = MTP.read_potential(resource_path / "full_test/twostep_emt_4g_with_hs.mtp") # calc.species = (13,) # motep # calc.species = ("Al",) # mlippy .. GENERATED FROM PYTHON SOURCE LINES 47-48 Make grid. .. GENERATED FROM PYTHON SOURCE LINES 48-59 .. code-block:: Python vmin = 13.0 vmax = 17.0 nvolumes = 2 tmin = 298.0 tmax = 500.0 ntemperatures = 2 grid_fah = product( np.linspace(vmin, vmax, nvolumes), np.linspace(tmin, tmax, ntemperatures), ) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Make an initial ASE Atoms object. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python atoms = bulk("Al", "fcc", cubic=True) * 3 .. GENERATED FROM PYTHON SOURCE LINES 64-65 Make EffQH .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python effqh = EffQH.read_fc_fit(resource_path / "full_test/fc_fit.hdf5", atoms) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Run thermodynamic integration. .. GENERATED FROM PYTHON SOURCE LINES 69-81 .. code-block:: Python nlambdas = 5 ti = ThermoInt( atoms, effqh.calc, calc, grid_fah, lambdas=np.linspace(0.0, 1.0, nlambdas), sampling_setting={"sampling_offset": 0}, ) ti.run_sampling(nsnapshots=5, file="test.db") fdiff_qh_to_mtp = ti.free_energy_differences .. rst-class:: sphx-glr-script-out .. code-block:: none /usr/local/lib/python3.12/site-packages/ase/md/velocitydistribution.py:147: FutureWarning: `communicator` has been deprecated since ASE 3.26.0 and will be removed in ASE 3.27.0. Use `comm` instead. warnings.warn(msg, FutureWarning) /usr/local/lib/python3.12/site-packages/ase/md/velocitydistribution.py:154: FutureWarning: `comm=="serial"` has been deprecated since ASE 3.26.0 and will be removed in ASE 3.27.0. Use `comm=DummyMPI()` instead. warnings.warn(msg, FutureWarning) /usr/local/lib/python3.12/site-packages/ase/md/velocitydistribution.py:147: FutureWarning: `communicator` has been deprecated since ASE 3.26.0 and will be removed in ASE 3.27.0. Use `comm` instead. warnings.warn(msg, FutureWarning) /usr/local/lib/python3.12/site-packages/ase/md/velocitydistribution.py:154: FutureWarning: `comm=="serial"` has been deprecated since ASE 3.26.0 and will be removed in ASE 3.27.0. Use `comm=DummyMPI()` instead. warnings.warn(msg, FutureWarning) .. GENERATED FROM PYTHON SOURCE LINES 82-83 Write the TI snapshots to an ASE db format and print and save *F*\ :sup:`ah`. .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: Python ti.snapshot_container.write("thermodynamic_integration.db") parprint("Results in meV/atom:") parprint(fdiff_qh_to_mtp * 1e3) fdiff_qh_to_mtp.to_csv("fah.csv") .. rst-class:: sphx-glr-script-out .. code-block:: none Results in meV/atom: 0 1 volume temperature 13.0 298.0 NaN NaN 500.0 NaN NaN 17.0 298.0 NaN NaN 500.0 NaN NaN .. GENERATED FROM PYTHON SOURCE LINES 88-89 Plot Δ\ *U* as a function of λ. .. GENERATED FROM PYTHON SOURCE LINES 89-90 .. code-block:: Python plot(ti).show() .. image-sg:: /tutorials/ti/images/sphx_glr_run_ti_001.png :alt: linear, cubic, tan, tan2, glogit :srcset: /tutorials/ti/images/sphx_glr_run_ti_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /builds/axefor/direct-upsampling/directupsampling/plot_tools.py:306: UserWarning: No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument. ax.legend(loc="center right", fontsize=8) .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 45.317 seconds) .. _sphx_glr_download_tutorials_ti_run_ti.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: run_ti.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: run_ti.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: run_ti.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_