Commit e84b6d80 authored by sjplimp's avatar sjplimp Committed by GitHub
Browse files

Merge pull request #602 from rbberger/pylammps_fixes

PyLammps corrections
parents 6d0a2286 96d37128
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -48,21 +48,17 @@ System-wide Installation :h3
Step 1: Building LAMMPS as a shared library :h4

To use LAMMPS inside of Python it has to be compiled as shared library. This
library is then loaded by the Python interface. In this example, we use the
Make.py utility to create a Makefile with C++ exceptions, PNG, JPEG and FFMPEG
output support enabled. Finally, we also enable the MOLECULE package and compile
using the generated {auto} Makefile.
library is then loaded by the Python interface. In this example we enable the
MOLECULE package and compile LAMMPS with C++ exceptions, PNG, JPEG and FFMPEG
output support enabled.

cd $LAMMPS_DIR/src :pre

# generate custom Makefile
python Make.py -jpg -png -s ffmpeg exceptions -m mpi -a file :pre

# add packages if necessary
make yes-MOLECULE :pre

# compile shared library using Makefile
make mode=shlib auto :pre
make mpi mode=shlib LMP_INC="-DLAMMPS_PNG -DLAMMPS_JPEG -DLAMMPS_FFMPEG -DLAMMPS_EXCEPTIONS" JPG_LIB="-lpng -ljpeg" :pre

Step 2: Installing the LAMMPS Python package :h4

+3 −0
Original line number Diff line number Diff line
*.orig
*-checkpoint.ipynb
*.png
*.mp4
+50 −955

File changed.

Preview size limit exceeded, changes collapsed.

+8 −3
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Using LAMMPS with iPython and Jupyter

%% Cell type:markdown id: tags:

LAMMPS can be run interactively using iPython easily. This tutorial shows how to set this up.

%% Cell type:markdown id: tags:

## Installation

%% Cell type:markdown id: tags:

1. Download the latest version of LAMMPS into a folder (we will calls this `$LAMMPS_DIR` from now on)
2. Compile LAMMPS as a shared library and enable exceptions and PNG support
   ```bash
   cd $LAMMPS_DIR/src
   python Make.py -m mpi -png -s exceptions -a file
   make mode=shlib auto
   make mpi mode=shlib LMP_INC="-DLAMMPS_PNG -DLAMMPS_EXCEPTIONS" JPG_LIB="-lpng"
   ```

3. Create a python virtualenv
   ```bash
   virtualenv testing
   source testing/bin/activate
   ```

4. Inside the virtualenv install the lammps package
   ```
   (testing) cd $LAMMPS_DIR/python
   (testing) python install.py
   (testing) cd   # move to your working directory
   ```

5. Install jupyter and ipython in the virtualenv
   ```bash
   (testing) pip install ipython jupyter
   ```

6. Run jupyter notebook
   ```bash
   (testing) jupyter notebook
   ```

%% Cell type:markdown id: tags:

## Example

%% Cell type:code id: tags:

``` python
from lammps import IPyLammps
```

%% Cell type:code id: tags:

``` python
L = IPyLammps()
```

%% Cell type:code id: tags:

``` python
# 3d Lennard-Jones melt

L.units("lj")
L.atom_style("atomic")
L.atom_modify("map array")

L.lattice("fcc", 0.8442)
L.region("box block", 0, 4, 0, 4, 0, 4)
L.create_box(1, "box")
L.create_atoms(1, "box")
L.mass(1, 1.0)

L.velocity("all create", 1.44, 87287, "loop geom")

L.pair_style("lj/cut", 2.5)
L.pair_coeff(1, 1, 1.0, 1.0, 2.5)

L.neighbor(0.3, "bin")
L.neigh_modify("delay 0 every 20 check no")

L.fix("1 all nve")

L.variable("fx atom fx")

L.run(10)
```

%% Cell type:code id: tags:

``` python
L.image(zoom=1)
```

%% Cell type:markdown id: tags:

## Queries about LAMMPS simulation

%% Cell type:code id: tags:

``` python
L.system
```

%% Cell type:code id: tags:

``` python
L.system.natoms
```

%% Cell type:code id: tags:

``` python
L.communication
```

%% Cell type:code id: tags:

``` python
L.fixes
```

%% Cell type:code id: tags:

``` python
L.computes
```

%% Cell type:code id: tags:

``` python
L.dumps
```

%% Cell type:code id: tags:

``` python
L.groups
```

%% Cell type:markdown id: tags:

## Working with LAMMPS Variables

%% Cell type:code id: tags:

``` python
L.variable("a index 2")
```

%% Cell type:code id: tags:

``` python
L.variables
```

%% Cell type:code id: tags:

``` python
L.variable("t equal temp")
```

%% Cell type:code id: tags:

``` python
L.variables
```

%% Cell type:code id: tags:

``` python
import sys

if sys.version_info < (3, 0):
    # In Python 2 'print' is a restricted keyword, which is why you have to use the lmp_print function instead.
    x = float(L.lmp_print('"${a}"'))
else:
    # In Python 3 the print function can be redefined.
    # x = float(L.print('"${a}"')")

    # To avoid a syntax error in Python 2 executions of this notebook, this line is packed into an eval statement
    x = float(eval("L.print('\"${a}\"')"))
x
```

%% Cell type:code id: tags:

``` python
L.variables['t'].value
```

%% Cell type:code id: tags:

``` python
L.eval("v_t/2.0")
```

%% Cell type:code id: tags:

``` python
L.variable("b index a b c")
```

%% Cell type:code id: tags:

``` python
L.variables['b'].value
```

%% Cell type:code id: tags:

``` python
L.eval("v_b")
```

%% Cell type:code id: tags:

``` python
L.variables['b'].definition
```

%% Cell type:code id: tags:

``` python
L.variable("i loop 10")
```

%% Cell type:code id: tags:

``` python
L.variables['i'].value
```

%% Cell type:code id: tags:

``` python
L.next("i")
L.variables['i'].value
```

%% Cell type:code id: tags:

``` python
L.eval("ke")
```

%% Cell type:markdown id: tags:

## Accessing Atom data

%% Cell type:code id: tags:

``` python
L.atoms[0]
```

%% Cell type:code id: tags:

``` python
[x for x in dir(L.atoms[0]) if not x.startswith('__')]
```

%% Cell type:code id: tags:

``` python
L.atoms[0].position
```

%% Cell type:code id: tags:

``` python
L.atoms[0].id
```

%% Cell type:code id: tags:

``` python
L.atoms[0].velocity
```

%% Cell type:code id: tags:

``` python
L.atoms[0].force
```

%% Cell type:code id: tags:

``` python
L.atoms[0].type
```

%% Cell type:code id: tags:

``` python
L.variables['fx'].value
```

%% Cell type:markdown id: tags:

## Accessing thermo data

%% Cell type:code id: tags:

``` python
L.runs
```

%% Cell type:code id: tags:

``` python
L.runs[0]
```

%% Cell type:code id: tags:

``` python
L.runs[0].thermo
```

%% Cell type:code id: tags:

``` python
L.runs[0].thermo.Temp
L.runs[0].thermo
```

%% Cell type:markdown id: tags:

## Saving session to as LAMMPS input file

%% Cell type:code id: tags:

``` python
L.write_script("in.output")
```

%% Cell type:code id: tags:

``` python
dir(L.runs[0].thermo)
```

%% Cell type:code id: tags:

``` python
```
+2 −3
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Using LAMMPS with iPython and Jupyter

%% Cell type:markdown id: tags:

LAMMPS can be run interactively using iPython easily. This tutorial shows how to set this up.

%% Cell type:markdown id: tags:

## Installation

%% Cell type:markdown id: tags:

1. Download the latest version of LAMMPS into a folder (we will calls this `$LAMMPS_DIR` from now on)
2. Compile LAMMPS as a shared library and enable exceptions and PNG support
   ```bash
   cd $LAMMPS_DIR/src
   make yes-molecule
   python Make.py -m mpi -png -s exceptions -a file
   make mode=shlib auto
   make mpi mode=shlib LMP_INC="-DLAMMPS_PNG -DLAMMPS_EXCEPTIONS" JPG_LIB="-lpng"
   ```

3. Create a python virtualenv
   ```bash
   virtualenv testing
   source testing/bin/activate
   ```

4. Inside the virtualenv install the lammps package
   ```
   (testing) cd $LAMMPS_DIR/python
   (testing) python install.py
   (testing) cd   # move to your working directory
   ```

5. Install jupyter and ipython in the virtualenv
   ```bash
   (testing) pip install ipython jupyter
   ```

6. Run jupyter notebook
   ```bash
   (testing) jupyter notebook
   ```

%% Cell type:markdown id: tags:

## Example

%% Cell type:code id: tags:

``` python
from lammps import IPyLammps
```

%% Cell type:code id: tags:

``` python
L = IPyLammps()
```

%% Cell type:code id: tags:

``` python
# 2d circle of particles inside a box with LJ walls
import math

b = 0
x = 50
y = 20
d = 20

# careful not to slam into wall too hard

v = 0.3
w = 0.08

L.units("lj")
L.dimension(2)
L.atom_style("bond")
L.boundary("f f p")

L.lattice("hex", 0.85)
L.region("box", "block", 0, x, 0, y, -0.5, 0.5)
L.create_box(1, "box", "bond/types", 1, "extra/bond/per/atom", 6)
L.region("circle", "sphere", d/2.0+1.0, d/2.0/math.sqrt(3.0)+1, 0.0, d/2.0)
L.create_atoms(1, "region", "circle")
L.mass(1, 1.0)

L.velocity("all create 0.5 87287 loop geom")
L.velocity("all set", v, w, 0, "sum yes")

L.pair_style("lj/cut", 2.5)
L.pair_coeff(1, 1, 10.0, 1.0, 2.5)

L.bond_style("harmonic")
L.bond_coeff(1, 10.0, 1.2)

L.create_bonds("all", "all", 1, 1.0, 1.5)
L.create_bonds("many", "all", "all", 1, 1.0, 1.5)

L.neighbor(0.3, "bin")
L.neigh_modify("delay", 0, "every", 1, "check yes")

L.fix(1, "all", "nve")

L.fix(2, "all wall/lj93 xlo 0.0 1 1 2.5 xhi", x, "1 1 2.5")
L.fix(3, "all wall/lj93 ylo 0.0 1 1 2.5 yhi", y, "1 1 2.5")
```

%% Cell type:code id: tags:

``` python
L.image(zoom=1.8)
```

%% Cell type:code id: tags:

``` python
L.thermo_style("custom step temp epair press")
L.thermo(100)
output = L.run(40000)
L.image(zoom=1.8)
```

%% Cell type:markdown id: tags:

## Queries about LAMMPS simulation

%% Cell type:code id: tags:

``` python
L.system
```

%% Cell type:code id: tags:

``` python
L.system.natoms
```

%% Cell type:code id: tags:

``` python
L.system.nbonds
```

%% Cell type:code id: tags:

``` python
L.system.nbondtypes
```

%% Cell type:code id: tags:

``` python
L.communication
```

%% Cell type:code id: tags:

``` python
L.fixes
```

%% Cell type:code id: tags:

``` python
L.computes
```

%% Cell type:code id: tags:

``` python
L.dumps
```

%% Cell type:code id: tags:

``` python
L.groups
```

%% Cell type:markdown id: tags:

## Working with LAMMPS Variables

%% Cell type:code id: tags:

``` python
L.variable("a index 2")
```

%% Cell type:code id: tags:

``` python
L.variables
```

%% Cell type:code id: tags:

``` python
L.variable("t equal temp")
```

%% Cell type:code id: tags:

``` python
L.variables
```

%% Cell type:code id: tags:

``` python
import sys

if sys.version_info < (3, 0):
    # In Python 2 'print' is a restricted keyword, which is why you have to use the lmp_print function instead.
    x = float(L.lmp_print('"${a}"'))
else:
    # In Python 3 the print function can be redefined.
    # x = float(L.print('"${a}"')")

    # To avoid a syntax error in Python 2 executions of this notebook, this line is packed into an eval statement
    x = float(eval("L.print('\"${a}\"')"))
x
```

%% Cell type:code id: tags:

``` python
L.variables['t'].value
```

%% Cell type:code id: tags:

``` python
L.eval("v_t/2.0")
```

%% Cell type:code id: tags:

``` python
L.variable("b index a b c")
```

%% Cell type:code id: tags:

``` python
L.variables['b'].value
```

%% Cell type:code id: tags:

``` python
L.eval("v_b")
```

%% Cell type:code id: tags:

``` python
L.variables['b'].definition
```

%% Cell type:code id: tags:

``` python
L.variable("i loop 10")
```

%% Cell type:code id: tags:

``` python
L.variables['i'].value
```

%% Cell type:code id: tags:

``` python
L.next("i")
L.variables['i'].value
```

%% Cell type:code id: tags:

``` python
L.eval("ke")
```

%% Cell type:markdown id: tags:

## Accessing Atom data

%% Cell type:code id: tags:

``` python
L.atoms[0]
```

%% Cell type:code id: tags:

``` python
[x for x in dir(L.atoms[0]) if not x.startswith('__')]
```

%% Cell type:code id: tags:

``` python
L.atoms[0].position
```

%% Cell type:code id: tags:

``` python
L.atoms[0].id
```

%% Cell type:code id: tags:

``` python
L.atoms[0].velocity
```

%% Cell type:code id: tags:

``` python
L.atoms[0].force
```

%% Cell type:code id: tags:

``` python
L.atoms[0].type
```

%% Cell type:code id: tags:

``` python
```
Loading