{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Structure optimization: :mol:`H_2O`\n\nLet's calculate the structure of the :mol:`H_2O` molecule.\nPart of this tutorial is exercise-based, so refer back to what you learnt\nfrom `introductionexample` and `atomscalculators`.\nSuggested solutions to the exercises are found after each exercise,\nbut try solving them youself first!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: Exercise\n\n Create an :class:`~ase.Atoms` object representing an :mol:`H_2O`\n molecule by providing chemical symbols and a guess for the positions.\n Visualize it, making sure the molecule is V shaped.\n\nSolution:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfrom ase import Atoms\nfrom ase.visualize.plot import plot_atoms\n\natoms = Atoms('HOH', positions=[[0, 0, -1], [0, 1, 0], [0, 0, 1]])\natoms.center(vacuum=3.0)\n\nfig, ax = plt.subplots()\nplot_atoms(atoms, ax, rotation='10x,60y,0z')\nax.set_axis_off()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: Exercise\n\n Run a self-consistent calculation of the approximate :mol:`H_2O` molecule\n using GPAW.\n\nSolution:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from gpaw import GPAW\n\ncalc = GPAW(mode='lcao', basis='dzp', txt='gpaw.txt')\natoms.calc = calc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Optimizers\nWe will next want to optimize the geometry.\nASE provides :mod:`several optimization algorithms `\nthat can run on top of :class:`~ase.Atoms` equipped with a calculator:\n\n.. admonition:: Exercise\n\n Run a structure optimization, thus calculating the equilibrium\n geometry of :mol:`H_2O`.\n\nSolution:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ase.optimize import BFGS\n\nopt = BFGS(atoms, trajectory='opt.traj', logfile='opt.log')\nopt.run(fmax=0.05)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``trajectory`` keyword above ensures that the trajectory of intermediate\ngeometries is written to :file:`opt.traj`.\n\n.. admonition:: Exercise\n\n Visualize the output trajectory and play it as an animation.\n Use the mouse to drag a box around and select the three atoms \u2014\n this will display the angles between them.\n What is H\u2013O\u2013H angle of :mol:`H_2O`?\n\nSolution:\n\n```python\nfrom ase.io import read\nfrom ase.visualize import view\n\natoms = read('opt.traj', ':')\nview(atoms)\n```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the above will open in a separate graphical window.\nAs always in ASE, we can do things programmatically, too,\nif we know the right incantations:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ase.io import read\n\natoms = read('opt.traj', ':')\nprint(atoms[-1].get_angle(0, 1, 2))\nprint(atoms[-1].get_angle(2, 0, 1))\nprint(atoms[-1].get_angle(1, 2, 0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The documentation on the :class:`~ase.Atoms` object provides\na long list of methods.\n\n## G2 molecule dataset\n\nASE knows many common molecules, so we did not really need to type in\nall the molecular coordinates ourselves. As luck would have it, the\n:func:`ase.build.molecule` function does exactly what we need:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ase.build import molecule\n\natoms = molecule('H2O', vacuum=3.0)\nfig, ax = plt.subplots()\nplot_atoms(atoms, ax, rotation='10x,60y,0z')\nax.set_axis_off()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function returns a molecule from the G2 test set, which is nice\nif we remember the exact name of that molecule, in this case :mol:`'H_2O'`.\nIn case we don't have all the molecule names memorized, we can work\nwith the G2 test set using the more general :mod:`ase.collections.g2`\nmodule:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ase.collections import g2\n\nprint(g2.names) # These are the molecule names\natoms = g2['CH3CH2OH']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To visualize the selected molecule as well as all 162 systems, run\n\n```python\nview(atoms)\nview(g2)\n```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use another calculator\n\nWe could equally well substitute\nanother calculator, often accessed through imports like ``from\nase.calculators.emt import EMT`` or ``from ase.calculators.aims import\nAims``. For a list, see :mod:`ase.calculators` or run::\n\n $ ase info --calculators\n\n" ] } ], "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.13.7" } }, "nbformat": 4, "nbformat_minor": 0 }