LAMMPS/python/mpi4py interface with conda installation

Hi all,

I am trying to get a project working that involves using a python script to call a LAMMPS input script. To do this, I used conda to set up a conda environment for lammps using the command:

conda create -n “lammps” -c conda-forge lammps mpi4py numpy scipy

I can then activate this environment by sourcing my .bashrc and then using the “conda activate lammps” command. I find that I can run simple mpi4py scripts in this environment. I also find that I can run simple lammps scripts (such as those in the “Using PyLammps and mpi4py (Experimental)” section on the website) on a single mpi processor, i.e. I can run mpirun -np 1 python melt.py), but when I try to run to use multiple processors, i.e. run mpirun -np 2 python melt.py, the job runs more slowly than before! The log file seems to indicate that the 2 processors are actually running independently on the same input script.

I’m wondering if this strange behavior is somehow due to two separate mpi’s being installed (one for LAMMPs and one for mpi4py) that are having trouble communicating? Any suggestions for what I should do to get my project working in the near-term is much appreciated.

Best,
Robert

I do know that you have to be sure that you have build mpi4py and also LAMMPS with
the same MPI, otherwise you’ll have problems. Section 12.5 of the LAMMPS manual
has a note about this. Possibly Richard (CCd) will have another idea.

Steve

If both lammps and mpi4py use a different MPI this will not work.

If you insist using the conda build, remove mpi4py from conda and try installing mpi4py manually to make sure it uses the same MPI

https://mpi4py.readthedocs.io/en/stable/install.html

Otherwise, it’s better to compile LAMMPS on your own, e.g. with CMake, and use virtualenv and pip for the Python dependencies

git clone https://github.com/lammps/lammps.git
cd lammps
mkdir build
cd build/
virtualenv myenv
source myenv/bin/activate
pip install numpy scipy mpi4py
cmake -C …/cmake/presets/most.cmake -D CMAKE_INSTALL_PREFIX=$VIRTUAL_ENV …/cmake
make -j 4
make install
export LD_LIBRARY_PATH=$VIRTUAL_ENV/lib:$LD_LIBRARY_PATH
cd python/examples/pylammps/mpi4py/
mpirun -np 1 python melt.py
mpirun -np 2 python melt.py

Best,
Richard