Using LAMMPS with iPython and Jupyter

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

Installation

  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 PNG support

    cd $LAMMPS_DIR/src
    python2 Make.py -m mpi -png -a file
    make mode=shlib auto
    
  3. Create a python virtualenv

    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

    (testing) pip install ipython jupyter
    
  6. Run jupyter notebook

    (testing) jupyter notebook
    

Example


In [1]:
from lammps import IPyLammps

In [2]:
L = IPyLammps()


LAMMPS output is captured by PyLammps wrapper

In [3]:
# 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)


Out[3]:
['Neighbor list info ...',
 '  1 neighbor list requests',
 '  update every 20 steps, delay 0 steps, check no',
 '  max neighbors/atom: 2000, page size: 100000',
 '  master list distance cutoff = 2.8',
 '  ghost atom cutoff = 2.8',
 '  binsize = 1.4, bins = 5 5 5',
 'Setting up Verlet run ...',
 '  Unit style    : lj',
 '  Current step  : 0',
 '  Time step     : 0.005',
 'Memory usage per processor = 2.04486 Mbytes',
 'Step Temp E_pair E_mol TotEng Press ',
 '       0         1.44   -6.7733681            0   -4.6218056   -5.0244179 ',
 '      10    1.1298532   -6.3095502            0   -4.6213906   -2.6058175 ',
 'Loop time of 0.001452 on 1 procs for 10 steps with 256 atoms',
 '',
 'Performance: 2975206.618 tau/day, 6887.052 timesteps/s',
 '0.0% CPU use with 1 MPI tasks x no OpenMP threads',
 '',
 'MPI task timing breakdown:',
 'Section |  min time  |  avg time  |  max time  |%varavg| %total',
 '---------------------------------------------------------------',
 'Pair    | 0.001326   | 0.001326   | 0.001326   |   0.0 | 91.32',
 'Neigh   | 0          | 0          | 0          |   0.0 |  0.00',
 'Comm    | 6.9e-05    | 6.9e-05    | 6.9e-05    |   0.0 |  4.75',
 'Output  | 8e-06      | 8e-06      | 8e-06      |   0.0 |  0.55',
 'Modify  | 3.2e-05    | 3.2e-05    | 3.2e-05    |   0.0 |  2.20',
 'Other   |            | 1.7e-05    |            |       |  1.17',
 '',
 'Nlocal:    256 ave 256 max 256 min',
 'Histogram: 1 0 0 0 0 0 0 0 0 0',
 'Nghost:    1431 ave 1431 max 1431 min',
 'Histogram: 1 0 0 0 0 0 0 0 0 0',
 'Neighs:    9984 ave 9984 max 9984 min',
 'Histogram: 1 0 0 0 0 0 0 0 0 0',
 '',
 'Total # of neighbors = 9984',
 'Ave neighs/atom = 39',
 'Neighbor list builds = 0',
 'Dangerous builds not checked']

In [4]:
L.image(zoom=1)


Out[4]:

Queries about LAMMPS simulation


In [5]:
L.system


Out[5]:
System(zlo=0.0, atom_style='atomic', boundaries='p,p p,p p,p', kspace_style='none', ntypes=1, dimensions=3, units='lj', orthogonal_box=[6.71838, 6.71838, 6.71838], yhi=6.71838, style='lj/cut', ylo=0.0, natoms=256, zhi=6.71838, atom_map='array', xhi=6.71838, xlo=0.0)

In [6]:
L.system.natoms


Out[6]:
256

In [7]:
L.communication


Out[7]:
Communication(comm_style='brick', nthreads=1, proc_grid=[1, 1, 1], mpi_version='MPI v3.0', nprocs=1, ghost_velocity=False, comm_layout='uniform')

In [8]:
L.fixes


Out[8]:
[{'group': 'all', 'name': '1', 'style': 'nve'}]

In [9]:
L.computes


Out[9]:
[{'group': 'all', 'name': 'thermo_temp', 'style': 'temp'},
 {'group': 'all', 'name': 'thermo_press', 'style': 'pressure'},
 {'group': 'all', 'name': 'thermo_pe', 'style': 'pe'}]

In [10]:
L.dumps


Out[10]:
[]

In [11]:
L.groups


Out[11]:
[{'name': 'all', 'type': 'static'}]

Working with LAMMPS Variables


In [12]:
L.variable("a index 2")

In [13]:
L.variables


Out[13]:
{'a': <lammps.Variable at 0x7ffbbe8407f0>,
 'fx': <lammps.Variable at 0x7ffbbe8409e8>}

In [14]:
L.variable("t equal temp")

In [15]:
L.variables


Out[15]:
{'a': <lammps.Variable at 0x7ffbbe840940>,
 'fx': <lammps.Variable at 0x7ffbbe840a90>,
 't': <lammps.Variable at 0x7ffbbe840a20>}

In [16]:
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[16]:
2.0

In [17]:
L.variables['t'].value


Out[17]:
1.12985322128803

In [18]:
L.eval("v_t/2.0")


Out[18]:
0.564926610644015

In [19]:
L.variable("b index a b c")

In [20]:
L.variables['b'].value


Out[20]:
'a'

In [21]:
L.eval("v_b")


Out[21]:
0.0

In [22]:
L.variables['b'].definition


Out[22]:
['a', 'b', 'c']

In [23]:
L.variable("i loop 10")

In [24]:
L.variables['i'].value


Out[24]:
1.0

In [25]:
L.next("i")
L.variables['i'].value


Out[25]:
2.0

In [26]:
L.eval("ke")


Out[26]:
1.6881595982135622

Accessing Atom data


In [27]:
L.atoms[0]


Out[27]:
<lammps.Atom at 0x7ffbbe83a828>

In [28]:
[x for x in dir(L.atoms[0]) if not x.startswith('__')]


Out[28]:
['charge',
 'force',
 'id',
 'index',
 'lmp',
 'mass',
 'mol',
 'position',
 'type',
 'velocity']

In [29]:
L.atoms[0].position


Out[29]:
(-0.01214313575723994, -0.030471417344883317, -0.09285770282156164)

In [30]:
L.atoms[0].id


Out[30]:
1

In [31]:
L.atoms[0].velocity


Out[31]:
(-0.2400638079872712, -0.5469382338431744, -1.6999922351145154)

In [32]:
L.atoms[0].force


Out[32]:
(-0.255291613596607, 4.609419196795491, 11.488556813439391)

In [33]:
L.atoms[0].type


Out[33]:
1

In [34]:
L.variables['fx'].value


Out[34]:
[-0.255291613596607,
 16.37266096862701,
 4.159322561393292,
 -10.358734928539349,
 -0.9411205246407262,
 2.938717474528063,
 -4.466552742048257,
 3.300931493929359,
 7.504103848449664,
 0.32879193591527645,
 -0.9406476961963371,
 -4.245722284654334,
 6.018469438850848,
 6.224548675112736,
 -1.3057190929343863,
 -4.54408845092879,
 4.302630565338362,
 -1.7752937195364038,
 -0.3537035449320157,
 -1.3938874511858526,
 -3.2776639119871187,
 3.160948290623288,
 -2.790188852792064,
 -0.9421802510034946,
 -13.724814879107763,
 -6.790155378420829,
 17.396062702331758,
 -5.771329649495489,
 -3.5023908236078265,
 7.496961111326418,
 -14.794500895776752,
 -14.720706631690351,
 -11.222160684284093,
 -17.894954250497932,
 8.404340282980602,
 -5.645941702083396,
 24.30811488965594,
 4.642599675775289,
 4.729539604038768,
 -0.4323819470652316,
 4.429779896132127,
 6.291952145215263,
 0.4284419246044248,
 -1.3300758523850174,
 2.714350256153217,
 -6.096739470326565,
 -0.5583077705790587,
 1.4115895642030756,
 17.316303747789764,
 12.123094224551103,
 6.520722209593786,
 -17.68459440341121,
 -23.744743068321586,
 -6.046503331470429,
 -3.892946532289541,
 1.9824465478702231,
 2.1510901134880336,
 -21.457945483950954,
 -16.011060818843365,
 10.889879999299424,
 10.953581565497077,
 -10.500586583044381,
 7.261088472660403,
 11.771997854536748,
 -0.6923053428387291,
 -11.71992686359301,
 10.161122642269508,
 -10.998509349838962,
 7.762360982853242,
 -0.39669551447512674,
 4.704847032648839,
 1.9525374247998633,
 -1.9996359541569817,
 5.205417057028795,
 0.6514801200578717,
 2.6509735204951146,
 -17.194397304879793,
 9.615345441194968,
 15.97002988281231,
 -3.3083188642938164,
 1.079294935322761,
 -2.9159102982605685,
 8.339297765197136,
 -3.959256903323581,
 2.874519411544632,
 -8.035228252740698,
 -14.03231374489075,
 2.4699091593746414,
 12.451219709891634,
 5.257386554469884,
 4.871163799433557,
 6.481746305583672,
 -1.236500768727892,
 16.497903799989224,
 -3.2737818963838174,
 -7.729520697941898,
 6.983322280758721,
 -2.1783280707695516,
 -40.295683406889985,
 14.060933848332962,
 2.3173930526619286,
 -2.046819216934295,
 -5.575438681868664,
 0.051326704791966424,
 8.719879836383923,
 -10.449448588532217,
 -0.12770355595051575,
 -11.59085661910744,
 -1.2611793341907793,
 -1.6398395331461924,
 -16.543263269385008,
 6.946375245763642,
 2.6458297416802523,
 -10.025322437003299,
 4.163259032034149,
 -10.984017942787375,
 0.0189726292183942,
 11.047577918499082,
 6.636325674666171,
 -3.559549874028051,
 -3.7556182807178997,
 0.027033528848196417,
 10.127352215962617,
 1.7697881024264828,
 20.985782565194718,
 2.1010189398183607,
 2.5666116097092524,
 -16.99457029010233,
 9.481467066173956,
 -5.0148231799442655,
 -9.95818988004336,
 9.866111938538626,
 -13.377742489960198,
 -3.2000295963348737,
 4.221680371419796,
 6.6302163287475135,
 0.009615374538821303,
 5.935463079394487,
 6.744551723461018,
 -9.64051876692669,
 -7.248950200244623,
 21.67197587791844,
 -23.8217504755792,
 4.597217761709829,
 -7.3169739101387385,
 -8.714043619104718,
 4.276550340709232,
 -1.8682033065758508,
 2.3693051462352748,
 1.8634844311192382,
 2.700781615273778,
 -3.138893562685977,
 -7.838876642135683,
 7.105203286115525,
 -1.083149867355154,
 -8.230419208731144,
 -12.233603659735682,
 10.833640166542056,
 9.721449220701018,
 -1.877609728717964,
 6.949261765399482,
 4.333500197216083,
 -1.8425880411098108,
 15.518947781103941,
 43.730190585283616,
 -2.1799812023845067,
 19.718357414093497,
 3.3198524110110186,
 2.136021263737773,
 -22.822582785055513,
 10.584653305299993,
 -5.9440297813706735,
 -7.6460245439162735,
 -4.180198871509111,
 -15.206132091014798,
 -3.039031521362778,
 -4.08036797778845,
 15.77348833812475,
 -7.999055949825264,
 -8.352682108827025,
 -10.629465258967437,
 6.9086941795359555,
 7.811955849517036,
 9.648408899213731,
 -1.3137506877264686,
 4.136163787610965,
 16.07136107010353,
 -22.199319830558398,
 19.00507586305845,
 10.27178610721686,
 13.228301605295682,
 -14.665041029548497,
 7.120484977464804,
 -13.014879391562197,
 7.016429546425751,
 0.8514431074593181,
 7.076572599898325,
 5.862896580033373,
 10.198650097607718,
 -5.217161885689705,
 1.841119452808099,
 23.5916329013764,
 -8.679317739549196,
 -9.082715536688122,
 4.622153070057964,
 -5.874112058912108,
 -4.317367532903269,
 -7.524652255692546,
 7.045622469998213,
 -6.215660840266272,
 0.5747623660814143,
 4.153063384795775,
 14.564757922330578,
 0.07090910607276352,
 1.4106570324329886,
 -1.2051371465387875,
 -19.453922015562327,
 2.158901789908676,
 -1.7127770369716167,
 13.941756348756183,
 -11.407906832323556,
 -12.643375679433458,
 3.8913549718334743,
 -0.12892051724766565,
 -3.8354255637031587,
 3.593509155189163,
 2.3291099495398613,
 0.4476595712151561,
 -16.190835039283897,
 -16.273156965726642,
 -44.64517813872991,
 -0.7695530880869927,
 33.99022078707833,
 -0.4197585361810044,
 0.30699564709585314,
 -10.994998378422109,
 1.7245510624229585,
 -2.8257173859741753,
 -1.1216135118661077,
 -0.3196709164205188,
 0.9628747760085039,
 -13.512075985764719,
 -0.13854498486337197,
 6.335139515373413,
 -0.3014898864186137,
 -2.969691585007467,
 26.20545138119855,
 -12.312340606934061,
 11.195269213941275,
 -0.2729563418493044,
 2.1363235059731647,
 3.5188084767077807,
 -5.536023143253481,
 9.718400670863252,
 -12.352077461650115,
 -1.4659962575175773]

In [ ]: