The LAMMPS Python module enables calling the LAMMPS C library API from Python by dynamically loading functions in the LAMMPS shared library through the Python ctypes module. Because of the dynamic loading, it is required that LAMMPS is compiled in “shared” mode. It is also recommended to compile LAMMPS with C++ exceptions enabled.
Two components are necessary for Python to be able to invoke LAMMPS code:
The LAMMPS Python Package (
lammps) from the
The LAMMPS Shared Library (
liblammps.dll) from the folder where you compiled LAMMPS.
2.2.2. Extending Python to run in parallel¶
If you wish to run LAMMPS in parallel from Python, you need to extend your Python with an interface to MPI. This also allows you to make MPI calls directly from Python in your script, if you desire.
We have tested this with MPI for Python (aka mpi4py) and you will find installation instruction for it below.
Installation of mpi4py (version 3.0.3 as of Sep 2020) can be done as follows:
pipinto a local user folder with:
pip install --user mpi4py
dnfinto a system folder for RedHat/Fedora systems:
# for use with OpenMPI sudo dnf install python3-mpi4py-openmpi # for use with MPICH sudo dnf install python3-mpi4py-openmpi
pipinto a virtual environment (see above):
$ source $HOME/myenv/activate (myenv)$ pip install mpi4py
pipinto a system folder (not recommended):
sudo pip install mpi4py
For more detailed installation instructions and additional options, please see the mpi4py installation page.
mpi4py and LAMMPS in parallel from Python, you must make
certain that both are using the same implementation and version
of MPI library. If you only have one MPI library installed on your
system this is not an issue, but it can be if you have multiple MPI
installations (e.g. on an HPC cluster to be selected through environment
modules). Your LAMMPS build is explicit about which MPI it is using,
since it is either detected during CMake configuration or in the
traditional make build system you specify the details in your low-level
src/MAKE/Makefile.foo file. The installation process of
mpicc command to find information about the MPI it uses to
build against. And it tries to load “libmpi.so” from the
LD_LIBRARY_PATH. This may or may not find the MPI library that
LAMMPS is using. If you have problems running both mpi4py and LAMMPS
together, this is an issue you may need to address, e.g. by loading the
module for different MPI installation so that mpi4py finds the right
If you have successfully installed mpi4py, you should be able to run Python and type
from mpi4py import MPI
without error. You should also be able to run Python in parallel on a simple test script
$ mpirun -np 4 python3 test.py
test.py contains the lines
from mpi4py import MPI comm = MPI.COMM_WORLD print("Proc %d out of %d procs" % (comm.Get_rank(),comm.Get_size()))
and see one line of output for each processor you run on.
# NOTE: the line order is not deterministic $ mpirun -np 4 python3 test.py Proc 0 out of 4 procs Proc 1 out of 4 procs Proc 2 out of 4 procs Proc 3 out of 4 procs