Update simply.ipynb

This commit is contained in:
Richard Berger
2021-03-16 14:22:26 -04:00
parent 644b61cd1f
commit e9e1a96335

View File

@ -4,74 +4,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using LAMMPS with iPython and Jupyter"
"# Using LAMMPS with PyLammps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LAMMPS can be run interactively using iPython easily. This tutorial shows how to set this up."
"The LAMMPS Python package provides multiple interfaces. The `PyLammps` interface is a high-level abstration of the low-level `lammps` interface. `IPyLammps` further extends this interface with functions that are useful for Jupyter notebooks to enable embedding generated graphics and videos."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation"
"## 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": [
"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",
"## Example\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from lammps import IPyLammps"
"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:"
]
},
{
@ -80,22 +38,53 @@
"metadata": {},
"outputs": [],
"source": [
"from lammps import IPyLammps\n",
"L = IPyLammps()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With `PyLammps`/`IPyLammps` you can write LAMMPS simulations similar to the input script language. Take the following LAMMPS input script:\n",
"\n",
"```bash\n",
"# 3d Lennard-Jones melt\n",
"\n",
"units lj\n",
"atom_style atomic\n",
"\n",
"lattice fcc 0.8442\n",
"region box block 0 4 0 4 0 4\n",
"create_box 1 box\n",
"create_atoms 1 box\n",
"mass 1 1.0\n",
"\n",
"velocity all create 1.44 87287 loop geom\n",
"\n",
"pair_style lj/cut 2.5\n",
"pair_coeff 1 1 1.0 1.0 2.5\n",
"\n",
"neighbor 0.3 bin\n",
"neigh_modify delay 0 every 20 check no\n",
"\n",
"fix 1 all nve\n",
"\n",
"thermo 50\n",
"```\n",
"The equivalent can be written with `PyLammps`/`IPyLammps`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"\n",
"# 3d Lennard-Jones melt\n",
"\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",
@ -111,13 +100,18 @@
"L.neighbor(0.3, \"bin\")\n",
"L.neigh_modify(\"delay\", 0, \"every\", 20, \"check no\")\n",
"\n",
"L.fix(\"1 all nve\")\n",
"L.fix(\"1\", \"all\", \"nve\")\n",
"\n",
"L.variable(\"fx atom fx\")\n",
"L.thermo(50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualizing the initial state\n",
"\n",
"L.info(\"all\")\n",
"\n",
"L.run(10)"
"`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."
]
},
{
@ -128,6 +122,170 @@
"source": [
"L.image(zoom=1.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.run(150)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can suppress it by adding a semicolon `;`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.run(100);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualizing the system will now show us how the atoms have moved."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.image(zoom=1.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(L.runs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each entry contains information about the simulation run, including the thermo output for the printed out time steps.\n",
"\n",
"```bash\n",
"# thermo output of a LAMMPS simulation run\n",
"Step Temp E_pair E_mol TotEng Press\n",
" 0 1.44 -6.7733681 0 -4.6218056 -5.0244179\n",
" 50 0.70303849 -5.6796164 0 -4.629178 0.50453907\n",
" 100 0.72628044 -5.7150774 0 -4.6299123 0.29765862\n",
" 150 0.78441711 -5.805142 0 -4.6331125 -0.086709661\n",
"```\n",
"\n",
"`PyLammps` already parses this information and makes it available as dictionaries and arrays."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.runs[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.runs[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, the first run was 150 time steps, with printing out a line every 50 steps. You can access the list of time steps using `{entry}.thermo.Step`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.runs[0].thermo.Step"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The corresponding values of each thermo quantity are also accessed this way:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.runs[0].thermo.TotEng"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Together you can use this information to run post-processing on these values or even plot it using `matplotlib`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"\n",
"plt.xlabel('time step')\n",
"plt.ylabel('Total Energy')\n",
"plt.plot(L.runs[0].thermo.Step, L.runs[0].thermo.TotEng)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -146,7 +304,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.9.2"
}
},
"nbformat": 4,