2.2. Installation

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 python folder

  • The LAMMPS Shared Library (liblammps.so, liblammps.dylib or liblammps.dll) from the folder where you compiled LAMMPS.

2.2.1. Installing the LAMMPS Python Module and Shared Library

Making LAMMPS usable within Python and vice versa requires putting the LAMMPS Python package (lammps) into a location where the Python interpreter can find it and installing the LAMMPS shared library into a folder that the dynamic loader searches or inside of the installed lammps package folder. There are multiple ways to achieve this.

  1. Do a full LAMMPS installation of libraries, executables, selected headers, documentation (if enabled), and supporting files (only available via CMake), which can also be either system-wide or into user specific folders.

  2. Install both components into a Python site-packages folder, either system-wide or in the corresponding user-specific folder. This way no additional environment variables need to be set, but the shared library is otherwise not accessible.

  3. Do an installation into a virtual environment. This can either be an installation of the Python package only or a full installation of LAMMPS.

  4. Leave the files where they are in the source/development tree and adjust some environment variables.

Build the LAMMPS executable and library with -DBUILD_SHARED_LIBS=on, -DLAMMPS_EXCEPTIONS=on and -DPKG_PYTHON=on (The first option is required, the other two are optional by recommended). The exact file name of the shared library depends on the platform (Unix/Linux, MacOS, Windows) and the build configuration being used. The installation base folder is already set by default to the $HOME/.local directory, but it can be changed to a custom location defined by the CMAKE_INSTALL_PREFIX CMake variable. This uses a folder called build to store files generated during compilation.

# create build folder
mkdir build
cd build

# configure LAMMPS compilation
cmake -C ../cmake/presets/basic.cmake -D BUILD_SHARED_LIBS=on \
      -D LAMMPS_EXCEPTIONS=on -D PKG_PYTHON=on ../cmake

# compile LAMMPS
cmake --build .

# install LAMMPS into $HOME/.local
cmake --install .

This leads to an installation to the following locations:

File

Location

Notes

LAMMPS Python package

  • $HOME/.local/lib/pythonX.Y/site-packages/lammps (32bit)

  • $HOME/.local/lib64/pythonX.Y/site-packages/lammps (64bit)

X.Y depends on the installed Python version

LAMMPS shared library

  • $HOME/.local/lib/ (32bit)

  • $HOME/.local/lib64/ (64bit)

Set shared loader environment variable to this path (see below for more info on this)

LAMMPS executable

  • $HOME/.local/bin/

LAMMPS potential files

  • $HOME/.local/share/lammps/potentials/

Set LAMMPS_POTENTIALS environment variable to this path

For a system-wide installation you need to set CMAKE_INSTALL_PREFIX to a system folder like /usr (or /usr/local); the default is ${HOME}/.local. The installation step for a system folder installation (not the configuration/compilation) needs to be done with superuser privilege, e.g. by using sudo cmake --install .. The installation folders will then be changed to (assuming /usr as prefix):

File

Location

Notes

LAMMPS Python package

  • /usr/lib/pythonX.Y/site-packages/lammps (32bit)

  • /usr/lib64/pythonX.Y/site-packages/lammps (64bit)

X.Y depends on the installed Python version

LAMMPS shared library

  • /usr/lib/ (32bit)

  • /usr/lib64/ (64bit)

LAMMPS executable

  • /usr/bin/

LAMMPS potential files

  • /usr/share/lammps/potentials/

To be able to use the “user” installation you have to ensure that the folder containing the LAMMPS shared library is either included in a path searched by the shared linker (e.g. like /usr/lib64/) or part of the LD_LIBRARY_PATH environment variable (or DYLD_LIBRARY_PATH on MacOS). Otherwise you will get an error when trying to create a LAMMPS object through the Python module.

# Unix/Linux
export LD_LIBRARY_PATH=$HOME/.local/lib:$LD_LIBRARY_PATH

# MacOS
export DYLD_LIBRARY_PATH=$HOME/.local/lib:$DYLD_LIBRARY_PATH

If you plan to use the LAMMPS executable (e.g., lmp), you may also need to adjust the PATH environment variable (but many newer Linux distributions already have $HOME/.local/bin included). Example:

export PATH=$HOME/.local/bin:$PATH

To make those changes permanent, you can add the commands to your $HOME/.bashrc file. For a system-wide installation is is not necessary due to files installed in system folders that are loaded automatically when a login shell is started.

To verify if LAMMPS can be successfully started from Python, start the Python interpreter, load the lammps Python module and create a LAMMPS instance. This should not generate an error message and produce output similar to the following:

$ python
Python 3.8.5 (default, Sep  5 2020, 10:50:12)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lammps
>>> lmp = lammps.lammps()
LAMMPS (18 Sep 2020)
using 1 OpenMP thread(s) per MPI task
>>>

Note

Unless you opted for “In place use”, you will have to rerun the installation any time you recompile LAMMPS to ensure the latest Python package and shared library are installed and used.

Note

If you want Python to be able to load different versions of the LAMMPS shared library with different settings, you will need to manually copy the files under different names (e.g. liblammps_mpi.so or liblammps_gpu.so) into the appropriate folder as indicated above. You can then select the desired library through the name argument of the LAMMPS object constructor (see Creating or deleting a LAMMPS object).

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:

  • Via pip into a local user folder with:

    pip install --user mpi4py
    
  • Via dnf into 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
    
  • Via pip into a virtual environment (see above):

    $ source $HOME/myenv/activate
    (myenv)$ pip install mpi4py
    
  • Via pip into a system folder (not recommended):

    sudo pip install mpi4py
    

For more detailed installation instructions and additional options, please see the mpi4py installation page.

To use 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 mpi4py uses the 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 one.

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

where 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