{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2: Using the PyLammps interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prerequisites\n", "\n", "Before running this example, make sure your Python environment can find the LAMMPS shared library (`liblammps.so`) and the LAMMPS Python package is installed. If you followed the [README](README.md) in this folder, this should already be the case. You can also find more information about how to compile LAMMPS and install the LAMMPS Python package in the [LAMMPS manual](https://lammps.sandia.gov/doc/Python_install.html). There is also a dedicated [PyLammps HowTo](https://lammps.sandia.gov/doc/Howto_pylammps.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup system" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from lammps import IPyLammps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L = IPyLammps()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 3d Lennard-Jones melt\n", "L.units(\"lj\")\n", "L.atom_style(\"atomic\")\n", "L.atom_modify(\"map array\")\n", "\n", "L.lattice(\"fcc\", 0.8442)\n", "L.region(\"box block\", 0, 4, 0, 4, 0, 4)\n", "L.create_box(1, \"box\")\n", "L.create_atoms(1, \"box\")\n", "L.mass(1, 1.0)\n", "\n", "L.velocity(\"all create\", 1.44, 87287, \"loop geom\")\n", "\n", "L.pair_style(\"lj/cut\", 2.5)\n", "L.pair_coeff(1, 1, 1.0, 1.0, 2.5)\n", "\n", "L.neighbor(0.3, \"bin\")\n", "L.neigh_modify(\"delay 0 every 20 check no\")\n", "\n", "L.fix(\"1 all nve\")\n", "\n", "L.variable(\"fx atom fx\")\n", "\n", "L.run(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualize the initial state" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.image(zoom=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Queries about LAMMPS simulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.system" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.system.natoms" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.communication" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.fixes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.computes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.dumps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.groups" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with LAMMPS Variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variable(\"a index 2\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variable(\"t equal temp\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "if sys.version_info < (3, 0):\n", " # In Python 2 'print' is a restricted keyword, which is why you have to use the lmp_print function instead.\n", " x = float(L.lmp_print('\"${a}\"'))\n", "else:\n", " # In Python 3 the print function can be redefined.\n", " # x = float(L.print('\"${a}\"')\")\n", " \n", " # To avoid a syntax error in Python 2 executions of this notebook, this line is packed into an eval statement\n", " x = float(eval(\"L.print('\\\"${a}\\\"')\"))\n", "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables['t'].value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.eval(\"v_t/2.0\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variable(\"b index a b c\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables['b'].value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.eval(\"v_b\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables['b'].definition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.lmp.command('variable i loop 10')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variable(\"i loop 10\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables['i'].value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.next(\"i\")\n", "L.variables['i'].value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.eval(\"ke\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing Atom data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.atoms[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(L.atoms[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.atoms[0].position" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.atoms[0].id" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.atoms[0].velocity" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.atoms[0].force" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.atoms[0].type" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.variables['fx'].value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing thermo data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.runs" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.runs[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.runs[0].thermo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.runs[0].thermo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(L.runs[0].thermo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Saving session to as LAMMPS input file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PyLammps can keep track of all LAMMPS commands that are executed. This allows you to prototype a script and then later on save it as a regular input script:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L = IPyLammps()\n", "\n", "# enable command history\n", "L.enable_cmd_history = True\n", "\n", "# 3d Lennard-Jones melt\n", "L.units(\"lj\")\n", "L.atom_style(\"atomic\")\n", "L.atom_modify(\"map array\")\n", "\n", "L.lattice(\"fcc\", 0.8442)\n", "L.region(\"box block\", 0, 4, 0, 4, 0, 4)\n", "L.create_box(1, \"box\")\n", "L.create_atoms(1, \"box\")\n", "L.mass(1, 1.0)\n", "\n", "L.velocity(\"all create\", 1.44, 87287, \"loop geom\")\n", "\n", "L.pair_style(\"lj/cut\", 2.5)\n", "L.pair_coeff(1, 1, 1.0, 1.0, 2.5)\n", "\n", "L.neighbor(0.3, \"bin\")\n", "L.neigh_modify(\"delay 0 every 20 check no\")\n", "\n", "L.fix(\"1 all nve\")\n", "\n", "L.run(10)\n", "\n", "# write LAMMPS input script with all commands executed so far (including implicit ones)\n", "L.write_script(\"in.output\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat in.output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9.2" } }, "nbformat": 4, "nbformat_minor": 1 }