{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Thermodynamic integration\nThis example shows how to perform thermodynamic integration (TI).\n\nWe here demonstrate the TI from an effective harmonic potential to either EMT\nor MTP.\n\nNote that the ASE EMT is very slow; the timing is:\n\n- With MTP: takes around 16 s on Apple M1 Pro single core.\n- With ASAP EMT: takes around 12 s on Apple M1 Pro single core.\n- With ASE EMT: takes around 110 s on Apple M1 Pro single core.\n\nTherefore, if ASAP is available, this exmaple uses its EMT implementation.\n\nYou can run the script either on a single core as ``python ./ti_example.py``\nor in parallel as ``mpirun -n 9 python ./ti_example.py``, where 3 * 3 = 9\n(volume, temperature) grid points are parallelized over.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itertools import product\nfrom pathlib import Path\n\nimport numpy as np\nfrom ase.build import bulk\nfrom ase.parallel import parprint\n\nimport directupsampling\nfrom directupsampling.calculators.emt import EMT\nfrom directupsampling.effqh.effqh import EffQH\nfrom directupsampling.plot_tools import plot\nfrom directupsampling.ti import ThermoInt\n\n# from directupsampling.calculators.mtp import MTP" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get pretrained EffQH and the target calculator.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "resource_path = Path(directupsampling.__file__).parents[1] / \"tests/resources\"\ncalc = EMT()\n\n# calc = MTP.read_potential(resource_path / \"full_test/twostep_emt_4g_with_hs.mtp\")\n# calc.species = (13,) # motep\n# calc.species = (\"Al\",) # mlippy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make grid.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "vmin = 13.0\nvmax = 17.0\nnvolumes = 2\ntmin = 298.0\ntmax = 500.0\nntemperatures = 2\ngrid_fah = product(\n np.linspace(vmin, vmax, nvolumes),\n np.linspace(tmin, tmax, ntemperatures),\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make an initial ASE Atoms object.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "atoms = bulk(\"Al\", \"fcc\", cubic=True) * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make EffQH\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "effqh = EffQH.read_fc_fit(resource_path / \"full_test/fc_fit.hdf5\", atoms)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run thermodynamic integration.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nlambdas = 5\nti = ThermoInt(\n atoms,\n effqh.calc,\n calc,\n grid_fah,\n lambdas=np.linspace(0.0, 1.0, nlambdas),\n sampling_setting={\"sampling_offset\": 0},\n)\nti.run_sampling(nsnapshots=5, file=\"test.db\")\nfdiff_qh_to_mtp = ti.free_energy_differences" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the TI snapshots to an ASE db format and print and save *F*\\ :sup:`ah`.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "ti.snapshot_container.write(\"thermodynamic_integration.db\")\nparprint(\"Results in meV/atom:\")\nparprint(fdiff_qh_to_mtp * 1e3)\nfdiff_qh_to_mtp.to_csv(\"fah.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot \u0394\\ *U* as a function of \u03bb.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot(ti).show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.13" } }, "nbformat": 4, "nbformat_minor": 0 }