LAMMPS can be run interactively using iPython easily. This tutorial shows how to set this up.
$LAMMPS_DIR from now on)Compile LAMMPS as a shared library and enable PNG support
cd $LAMMPS_DIR/src
make yes-molecule
python2 Make.py -m mpi -png -a file
make mode=shlib auto
Create a python virtualenv
virtualenv testing
source testing/bin/activate
Inside the virtualenv install the lammps package
(testing) cd $LAMMPS_DIR/python
(testing) python install.py
(testing) cd # move to your working directory
Install jupyter and ipython in the virtualenv
(testing) pip install ipython jupyter
Run jupyter notebook
(testing) jupyter notebook
In [1]:
from lammps import IPyLammps
In [2]:
L = IPyLammps()
In [3]:
# 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.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")
In [4]:
L.image(zoom=1.8)
Out[4]:
In [5]:
L.thermo_style("custom step temp epair press")
L.thermo(100)
output = L.run(40000)
L.image(zoom=1.8)
Out[5]:
In [6]:
L.system
Out[6]:
In [7]:
L.system.natoms
Out[7]:
In [8]:
L.system.nbonds
Out[8]:
In [9]:
L.system.nbondtypes
Out[9]:
In [10]:
L.communication
Out[10]:
In [11]:
L.fixes
Out[11]:
In [12]:
L.computes
Out[12]:
In [13]:
L.dumps
Out[13]:
In [14]:
L.groups
Out[14]:
In [15]:
L.variable("a index 2")
In [16]:
L.variables
Out[16]:
In [17]:
L.variable("t equal temp")
In [18]:
L.variables
Out[18]:
In [19]:
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
Out[19]:
In [20]:
L.variables['t'].value
Out[20]:
In [21]:
L.eval("v_t/2.0")
Out[21]:
In [22]:
L.variable("b index a b c")
In [23]:
L.variables['b'].value
Out[23]:
In [24]:
L.eval("v_b")
Out[24]:
In [25]:
L.variables['b'].definition
Out[25]:
In [26]:
L.variable("i loop 10")
In [27]:
L.variables['i'].value
Out[27]:
In [28]:
L.next("i")
L.variables['i'].value
Out[28]:
In [29]:
L.eval("ke")
Out[29]:
In [30]:
L.atoms[0]
Out[30]:
In [31]:
[x for x in dir(L.atoms[0]) if not x.startswith('__')]
Out[31]:
In [32]:
L.atoms[0].position
Out[32]:
In [33]:
L.atoms[0].id
Out[33]:
In [34]:
L.atoms[0].velocity
Out[34]:
In [35]:
L.atoms[0].force
Out[35]:
In [36]:
L.atoms[0].type
Out[36]:
In [ ]: