.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/ase_interface.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_examples_ase_interface.py: Structure minimization and constant temperature MD using ASE interface ====================================================================== This example is modified from the official `home page` and `Constant temperature MD`_ to use the ASE interface of TorchANI as energy calculator. .. _home page: https://wiki.fysik.dtu.dk/ase/ .. _Constant temperature MD: https://wiki.fysik.dtu.dk/ase/tutorials/md/md.html#constant-temperature-md .. GENERATED FROM PYTHON SOURCE LINES 18-19 To begin with, let's first import the modules we will use: .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: default from ase.lattice.cubic import Diamond from ase.md.langevin import Langevin from ase.optimize import BFGS from ase import units import torchani .. GENERATED FROM PYTHON SOURCE LINES 27-28 Now let's set up a crystal .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: default atoms = Diamond(symbol="C", pbc=True) print(len(atoms), "atoms in the cell") .. rst-class:: sphx-glr-script-out .. code-block:: none 8 atoms in the cell .. GENERATED FROM PYTHON SOURCE LINES 32-33 Now let's create a calculator from builtin models: .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: default calculator = torchani.models.ANI1ccx().ase() .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/torchani-2.2.3-py3.8.egg/torchani/resources/ Downloading ANI model parameters ... .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. note:: Regardless of the dtype you use in your model, when converting it to ASE calculator, it always automatically the dtype to ``torch.float64``. The reason for this behavior is, at many cases, the rounding error is too large for structure minimization. If you insist on using ``torch.float32``, do the following instead: .. code-block:: python calculator = torchani.models.ANI1ccx().ase(dtype=torch.float32) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Now let's set the calculator for ``atoms``: .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: default atoms.set_calculator(calculator) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Now let's minimize the structure: .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: default print("Begin minimizing...") opt = BFGS(atoms) opt.run(fmax=0.001) print() .. rst-class:: sphx-glr-script-out .. code-block:: none Begin minimizing... Step Time Energy fmax BFGS: 0 18:14:04 -8311.226180 0.0000 .. GENERATED FROM PYTHON SOURCE LINES 60-61 Now create a callback function that print interesting physical quantities: .. GENERATED FROM PYTHON SOURCE LINES 61-69 .. code-block:: default def printenergy(a=atoms): """Function to print the potential, kinetic and total energy.""" epot = a.get_potential_energy() / len(a) ekin = a.get_kinetic_energy() / len(a) print('Energy per atom: Epot = %.3feV Ekin = %.3feV (T=%3.0fK) ' 'Etot = %.3feV' % (epot, ekin, ekin / (1.5 * units.kB), epot + ekin)) .. GENERATED FROM PYTHON SOURCE LINES 70-73 We want to run MD with constant energy using the Langevin algorithm with a time step of 1 fs, the temperature 300K and the friction coefficient to 0.02 atomic units. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: default dyn = Langevin(atoms, 1 * units.fs, 300 * units.kB, 0.2) dyn.attach(printenergy, interval=50) .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.8.16/x64/lib/python3.8/site-packages/ase/md/md.py:48: FutureWarning: Specify the temperature in K using the 'temperature_K' argument warnings.warn(FutureWarning(w)) .. GENERATED FROM PYTHON SOURCE LINES 77-78 Now run the dynamics: .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: default print("Beginning dynamics...") printenergy() dyn.run(500) .. rst-class:: sphx-glr-script-out .. code-block:: none Beginning dynamics... Energy per atom: Epot = -1038.903eV Ekin = 0.000eV (T= 0K) Etot = -1038.903eV Energy per atom: Epot = -1038.903eV Ekin = 0.000eV (T= 0K) Etot = -1038.903eV Energy per atom: Epot = -1038.885eV Ekin = 0.029eV (T=223K) Etot = -1038.856eV Energy per atom: Epot = -1038.878eV Ekin = 0.018eV (T=141K) Etot = -1038.859eV Energy per atom: Epot = -1038.875eV Ekin = 0.025eV (T=194K) Etot = -1038.850eV Energy per atom: Epot = -1038.875eV Ekin = 0.024eV (T=187K) Etot = -1038.850eV Energy per atom: Epot = -1038.863eV Ekin = 0.026eV (T=202K) Etot = -1038.837eV Energy per atom: Epot = -1038.859eV Ekin = 0.024eV (T=186K) Etot = -1038.835eV Energy per atom: Epot = -1038.881eV Ekin = 0.023eV (T=180K) Etot = -1038.858eV Energy per atom: Epot = -1038.871eV Ekin = 0.054eV (T=416K) Etot = -1038.817eV Energy per atom: Epot = -1038.877eV Ekin = 0.059eV (T=458K) Etot = -1038.818eV Energy per atom: Epot = -1038.866eV Ekin = 0.039eV (T=301K) Etot = -1038.827eV True .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 14.111 seconds) .. _sphx_glr_download_examples_ase_interface.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ase_interface.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ase_interface.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_