[lammps-users] Explanation of lammps in python

Hello everyone,

I would like to share the use of python as a controller for the lammps
library. This is for anyone who is interested in coupling lammps (with
MPI) to a python code, I can explain how I have done it on my x86
machine running ubuntu 9.10.

The important disclaimer is that unless you will be completely
satisfied with the cpp functions in library.cpp, which I will
demonstrate how to use from python, you will still need to write in
cpp to interact with lammps. My personal motivation for using python
and lammps in this way is that I want to use python's dictionary
(hashing) functionality.

I will explain how I ran the attached python code c_driver.py , which
is a translation of c_driver.c from the lammps/examples/couple
directory. Please treat this an open discussion, I have only been
using python for a couple of months and swig for even less, so will
look forward to comments and criticisms.

First of all I made a lammps library according to the manual, it is
called lmp_openmpi here and in the attached files.

Then I installed SWIG and mpi4py:
http://www.swig.org
http://mpi4py.scipy.org/

To help us start using the lammps library, there is a starting
interface file in the lammps/src directory called library.cpp, with
header library.h. To use these functions from python, the .cpp file
needs to be built as a python extension module, for which we use swig.

As can be seen from the attached files, the only function from
library.cpp which I did not manage to interface with correctly was
lammps_open() , since I did not manage to set the void ptr as an
output/return value. Thus, the extra function "my_lammps_open()" is
present.

The only file which needs to be written is a swig interface file, such
as "library.i" which is attached. The "%include" statements in the
file call swig libraries that allow us to speak MPI and C/C++.

To compile it all, it just needs to be correctly linked and compiled,
as in the simple makefile "Makefile" attached. Edit the makefile to
match the location of your lammps library.

make

When it has been compiled, add the location of the extension module to
your python path:

export PYTHONPATH=$PYTHONPATH:[location of the extension module, e.g. _library.so on linux].

Then the module (which I imaginatively called "library") should be
importable to python. If this works, you might want to try
"c_driver.py" , which should replicate the results from c_driver.c.

mpiexec -n 8 python c_driver.py 8 in.lj

I also copied and pasted the C code from c_driver.c to library.cpp, so
that you can test it directly by calling "main(sys.argv)" from python,
or remove the comments from the relevant line in c_driver.py.

As you can see from the python code, I don't know what to do with the
MPI broadcast statement in line 46 (the equivalent of the c-code
MPI_Bcast() ). It works for me using my multi-core desktop with shared
memory, but I will have to study up a little bit more before using it
on our cluster.

Hope this can be of assistance to somebody. I also hope there are not
too many mistakes, but if there are let me know.
Andy Ballard (the one from Tokyo not from Maryland!!!)

c_driver.py (1.81 KB)

library.cpp (9.67 KB)

library.h (1.85 KB)

library.i (1.14 KB)

Makefile (1.42 KB)

Thanks for posting the details and the files Andy,

Steve