{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Equilibrating a box of acetonitrile with MD\n\n## Goals\n\nIn this tutorial we learn how to perform a thermal equilibration of a box\nof acetonitrile molecules using ASE. We will:\n\n* build a coarse-grained CH\u2083\u2013C\u2261N triatomic model.\n* set up a periodic box at the experimental density (298 K).\n* apply rigid-molecule constraints.\n* use the ACN force field.\n* equilibrate with Langevin dynamics.\n* scale small box of 27 molecules to 216 molecules.\n\n## ACN model\n\nThe acetonitrile force field implemented in ASE (:mod:`ase.calculators.acn`)\nis an interaction potential between three-site linear molecules. The atoms\nof the methyl group are treated as a single site centered on the methyl\ncarbon (hydrogens are not explicit). Therefore:\n\n* assign the **methyl mass** to the outer carbon (``m_me``),\n* use the atomic sequence **Me\u2013C\u2013N** repeated for all molecules,\n* keep molecules **rigid** during MD with :class:`FixLinearTriatomic`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport ase.units as units\nfrom ase import Atoms\nfrom ase.calculators.acn import ACN, m_me, r_cn, r_mec\nfrom ase.constraints import FixLinearTriatomic\nfrom ase.io import Trajectory\nfrom ase.md import Langevin\nfrom ase.visualize.plot import plot_atoms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Molecule\nBuild one CH3\u2013C\u2261N as a linear triatomic \u201cC\u2013C\u2013N\u201d. The first carbon is\nthe methyl site; we assign it the CH3 mass. Rotate slightly to avoid\nperfect alignment with the cell axes.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pos = [[0, 0, -r_mec], [0, 0, 0], [0, 0, r_cn]]\natoms = Atoms('CCN', positions=pos)\natoms.rotate(30, 'x')\n\nmasses = atoms.get_masses()\nmasses[0] = m_me\natoms.set_masses(masses)\n\nmol = atoms.copy()\nmol.set_pbc(False)\nmol.set_cell([20, 20, 20])\nmol.center()\n\nfig, ax = plt.subplots(figsize=(4, 4))\nplot_atoms(\n mol,\n ax=ax,\n rotation='30x,30y,0z',\n show_unit_cell=0,\n radii=0.75,\n)\nax.set_axis_off()\nplt.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Set up small box of 27-molecules\nMatch density 0.776 g/cm^3 at 298 K. Compute cubic box length L from\nmass and density. Build 3\u00d73\u00d73 supercell and enable PBC.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "density = 0.776 / 1e24 # g / \u00c5^3\nL = ((masses.sum() / units.mol) / density) ** (1 / 3.0)\n\natoms.set_cell((L, L, L))\natoms.center()\natoms = atoms.repeat((3, 3, 3))\natoms.set_pbc(True)\n\nbox27 = atoms.copy()\nfig, ax = plt.subplots(figsize=(5, 5))\nplot_atoms(\n box27,\n ax=ax,\n rotation='35x,35y,0z',\n show_unit_cell=2,\n radii=0.75,\n)\nax.set_axis_off()\nplt.tight_layout()\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Set constraints\nKeep each \u201cC\u2013C\u2013N\u201d rigid during MD using FixLinearTriatomic.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nm = 27\ntriples = [(3 * i, 3 * i + 1, 3 * i + 2) for i in range(nm)]\natoms.constraints = FixLinearTriatomic(triples=triples)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: MD run for 27-molecules system\nAssign ACN with cutoff = half the smallest box edge. Langevin MD at\n300 K, 2 fs timestep. Save a frame every step.\n\n
This example uses a relatively large timestep to demonstrate\n the usage of the code. In general, a smaller timestep\n should be used for this molecular dynamics simulation.\n Generally, the timestep depends on the chemical system,\n its constraints\n and is in the order of 0.5-2~fs.