Hi all - we just added a new capability to LAMMPS, that I think is quite
powerful. It adds a new PYTHON package that will embed the Python
interpreter in LAMMPS, so that Python code can be invoked from an input
script. This is in the 14Mar patch, which was backdated to coincide
with Pi day …
There are just 2 added commands and their syntax is pretty
An overview is here: http://lammps.sandia.gov/doc/Section_python.html
with examples in example/python.
The python command allows you to write a Python function and ask the
input script to execute it. Either directly, or by assigning it to a
python-style variable, so that it is executed whenever the variable is
evaluated, either in the input script, or when some other LAMMPS
command uses it. Arguments to the Python function can be mapped to
input script variables, and the function can return a value to a
The code for the Python function can be included in the input script,
or in a separate file you provide. The loaded code can contain other
Python functions or classes or global variables to hold state between
calls from LAMMPS.
The Python function can do any normal Python operation, including load
modules, call other functions or class methods, etc. It can also
“import” the LAMMPS library wrapper (see python/lammps.py in the
distro) so it can make callbacks to LAMMPS itself. This means the
Python function can invoke LAMMPS input script commands or query/alter
internal LAMMPS state.
One thing this enables is writing a loop or branching operation in
Python similar to what you might like to do in an input script with
the LAMMPS next and if commands, but much more complex than their
simple syntax allows.
Note that when you use the python command from a parallel simulation,
every MPI process is loading the Python interpreter and executing the
Python function locally.
I’m hoping creative users can think of cool things to do with this.
And of course new ways to shoot your LAMMPS simulation in the foot.