From 45b01aba0c821f2683ff8401161de9721f2f2736 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Tue, 16 Mar 2021 15:18:07 -0400 Subject: [PATCH] Update Jupyter notebooks --- .../pylammps/dihedrals/dihedral.ipynb | 18 +-- .../examples/pylammps/interface_usage.ipynb | 142 +++++++++--------- .../pylammps/interface_usage_bonds.ipynb | 93 ++++-------- python/examples/pylammps/simple.ipynb | 10 +- 4 files changed, 112 insertions(+), 151 deletions(-) diff --git a/python/examples/pylammps/dihedrals/dihedral.ipynb b/python/examples/pylammps/dihedrals/dihedral.ipynb index 6b919816d7..240e3e8bd6 100644 --- a/python/examples/pylammps/dihedrals/dihedral.ipynb +++ b/python/examples/pylammps/dihedrals/dihedral.ipynb @@ -19,9 +19,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt" @@ -30,9 +28,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from lammps import IPyLammps" @@ -194,9 +190,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "K = 80.0\n", @@ -232,9 +226,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -255,7 +247,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.9.2" } }, "nbformat": 4, diff --git a/python/examples/pylammps/interface_usage.ipynb b/python/examples/pylammps/interface_usage.ipynb index dbaae5a209..36cc7c77db 100644 --- a/python/examples/pylammps/interface_usage.ipynb +++ b/python/examples/pylammps/interface_usage.ipynb @@ -4,71 +4,29 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Using LAMMPS with iPython and Jupyter" + "# Example 2: Using the PyLammps interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "LAMMPS can be run interactively using iPython easily. This tutorial shows how to set this up." + "## 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": [ - "## Installation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "1. Download the latest version of LAMMPS into a folder (we will calls this `$LAMMPS_DIR` from now on)\n", - "2. Compile LAMMPS as a shared library and enable exceptions and PNG support\n", - " ```bash\n", - " cd $LAMMPS_DIR/src\n", - " make mpi mode=shlib LMP_INC=\"-DLAMMPS_PNG -DLAMMPS_EXCEPTIONS\" JPG_LIB=\"-lpng\"\n", - " ```\n", - "\n", - "3. Create a python virtualenv\n", - " ```bash\n", - " virtualenv testing\n", - " source testing/bin/activate\n", - " ```\n", - "\n", - "4. Inside the virtualenv install the lammps package\n", - " ```\n", - " (testing) cd $LAMMPS_DIR/python\n", - " (testing) python install.py\n", - " (testing) cd # move to your working directory\n", - " ```\n", - "\n", - "5. Install jupyter and ipython in the virtualenv\n", - " ```bash\n", - " (testing) pip install ipython jupyter\n", - " ```\n", - "\n", - "6. Run jupyter notebook\n", - " ```bash\n", - " (testing) jupyter notebook\n", - " ```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example" + "## Setup system" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from lammps import IPyLammps" @@ -90,7 +48,6 @@ "outputs": [], "source": [ "# 3d Lennard-Jones melt\n", - "\n", "L.units(\"lj\")\n", "L.atom_style(\"atomic\")\n", "L.atom_modify(\"map array\")\n", @@ -116,6 +73,13 @@ "L.run(10)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Visualize the initial state" + ] + }, { "cell_type": "code", "execution_count": null, @@ -205,9 +169,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"a index 2\")" @@ -225,9 +187,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"t equal temp\")" @@ -283,9 +243,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"b index a b c\")" @@ -321,9 +279,16 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, + "outputs": [], + "source": [ + "L.lmp.command('variable i loop 10')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"i loop 10\")" @@ -379,7 +344,7 @@ "metadata": {}, "outputs": [], "source": [ - "[x for x in dir(L.atoms[0]) if not x.startswith('__')]" + "dir(L.atoms[0])" ] }, { @@ -479,6 +444,15 @@ "L.runs[0].thermo" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dir(L.runs[0].thermo)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -486,14 +460,48 @@ "## 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": { - "collapsed": true - }, + "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\")" ] }, @@ -503,7 +511,7 @@ "metadata": {}, "outputs": [], "source": [ - "dir(L.runs[0].thermo)" + "!cat in.output" ] }, { @@ -530,7 +538,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.9.2" } }, "nbformat": 4, diff --git a/python/examples/pylammps/interface_usage_bonds.ipynb b/python/examples/pylammps/interface_usage_bonds.ipynb index 2ee9899b80..54df488de9 100644 --- a/python/examples/pylammps/interface_usage_bonds.ipynb +++ b/python/examples/pylammps/interface_usage_bonds.ipynb @@ -4,72 +4,29 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Using LAMMPS with iPython and Jupyter" + "# Example 3: 2D circle of particles inside of box with LJ walls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "LAMMPS can be run interactively using iPython easily. This tutorial shows how to set this up." + "## 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": [ - "## Installation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "1. Download the latest version of LAMMPS into a folder (we will calls this `$LAMMPS_DIR` from now on)\n", - "2. Compile LAMMPS as a shared library and enable exceptions and PNG support\n", - " ```bash\n", - " cd $LAMMPS_DIR/src\n", - " make yes-molecule\n", - " make mpi mode=shlib LMP_INC=\"-DLAMMPS_PNG -DLAMMPS_EXCEPTIONS\" JPG_LIB=\"-lpng\"\n", - " ```\n", - "\n", - "3. Create a python virtualenv\n", - " ```bash\n", - " virtualenv testing\n", - " source testing/bin/activate\n", - " ```\n", - "\n", - "4. Inside the virtualenv install the lammps package\n", - " ```\n", - " (testing) cd $LAMMPS_DIR/python\n", - " (testing) python install.py\n", - " (testing) cd # move to your working directory\n", - " ```\n", - "\n", - "5. Install jupyter and ipython in the virtualenv\n", - " ```bash\n", - " (testing) pip install ipython jupyter\n", - " ```\n", - "\n", - "6. Run jupyter notebook\n", - " ```bash\n", - " (testing) jupyter notebook\n", - " ```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Example" + "## Setup system" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from lammps import IPyLammps" @@ -135,6 +92,13 @@ "L.fix(3, \"all wall/lj93 ylo 0.0 1 1 2.5 yhi\", y, \"1 1 2.5\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Visualize initial state" + ] + }, { "cell_type": "code", "execution_count": null, @@ -144,6 +108,13 @@ "L.image(zoom=1.8)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Run simulation and visualize new state" + ] + }, { "cell_type": "code", "execution_count": null, @@ -254,9 +225,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"a index 2\")" @@ -274,9 +243,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"t equal temp\")" @@ -332,9 +299,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"b index a b c\")" @@ -370,9 +335,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "L.variable(\"i loop 10\")" @@ -428,7 +391,7 @@ "metadata": {}, "outputs": [], "source": [ - "[x for x in dir(L.atoms[0]) if not x.startswith('__')]" + "dir(L.atoms[0])" ] }, { @@ -479,9 +442,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -502,7 +463,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.9.2" } }, "nbformat": 4, diff --git a/python/examples/pylammps/simple.ipynb b/python/examples/pylammps/simple.ipynb index ea44b93631..aecf89d3d9 100644 --- a/python/examples/pylammps/simple.ipynb +++ b/python/examples/pylammps/simple.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Using LAMMPS with PyLammps" + "# Example 1: Using LAMMPS with PyLammps" ] }, { @@ -27,7 +27,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Example\n", + "## Creating a new simulation\n", "\n", "Once the LAMMPS shared library and the LAMMPS Python package are installed, you can create a new LAMMMPS instance in your Python interpreter as follows:" ] @@ -109,7 +109,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Visualizing the initial state\n", + "## Visualizing the initial state\n", "\n", "`IPyLammps` allows you to visualize the current simulation state with the [image](https://lammps.sandia.gov/doc/Python_module.html#lammps.IPyLammps.image) command. Here we use it to create an image of the initial state of the system." ] @@ -127,7 +127,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Running simulations\n", + "## Running simulations\n", "\n", "Use the `run` command to start the simulation. In Jupyter the return value of the last command will be displayed. The `run` command will return the output of the simulation." ] @@ -177,7 +177,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Post-processing thermo output\n", + "## Post-processing thermo output\n", "\n", "Independent of whether or not you suppress or show the output of the `run` command, `PyLammps` will record the output. Each `run` command creates a new entry in the `L.runs` list. So far our PyLammps instance `L` executed two `run` commands:" ]