How to execute Python in LAMMPS just 1 time?

Dear Lammps users,

I want to execute a python script in Lammps, and assume I have a Lammps script name: in.LammpsInput contain the command like this:

python myFUNC source pythonScript.py

The problem is that when I run in.LammpsInput script using: mpirun -np nCores …
then the pythonScript.py in Lammps is execute multiple times (= nCores).
Does any way to just execute pythonScript.py only 1 time?
(for example just exec on processor 0)

Thank you so much

Yes, but it requires the use of the LAMMPS python module and passing the LAMMPS object pointer to your function.

With the pointer you initialize the LAMMPS python module and through the python module you can query LAMMPS for the (world) MPI rank and then selectively execute the remainder of the function for specific MPI ranks.

dear Prof. akohlmey,

Does it have any to access MPI-rank number in lammps?
So that we can choose to execute python command on a specific rank.

Thank

Yes, I just said that. Please study the documentation for the library interface and the python module.

dear Prof. akohlmey,

Thank so much for your reply.
But you said that we can access MPI-rank inside “lammps module of python”
And my question is whether we can access MPI-rank in Lammps itself (without python package)?
I took many time to search in Lammps document but not yet found, or I did not know where it is call in lammps.

Thank you so much for your help.

No you cannot.

All input script is processed on all MPI ranks and all commands are global. That must be since the parallelization of LAMMPS requires that the global state is the same across all MPI ranks except for the data that is explicitly distributed without requiring explicit synchronization at each access to global data. Any such non-parallel/non-replicated operation thus would seriously interfere with parallel efficiency. LAMMPS parallelization is via domain decomposition (plus threads and/or GPUs within subdomains on top of that) and not a client/server or replicated data model, both of which do not scale so well to the extremely large number of MPI ranks that LAMMPS can scale to and was designed for.

It is not very difficult to get access to the rank. Here is a simple example.

Python source code “my_functions.py”:

def print_info(lmpptr):
    from lammps import lammps
    lmp = lammps(ptr=lmpptr)
    rank = lmp.extract_setting('world_rank')
    print("LAMMPS Version:", lmp.version(), " on MPI rank: ", rank)

LAMMPS input “in.pyfunc”:

python load_code source my_functions.py
python print_info input 1 SELF format p exists
python print_info invoke

when run with 4 MPI ranks, e.g. from within my LAMMPS development folder with:
mpirun -np 4 env PYTHONPATH=$HOME/compile/lammps/python ./lmp -in in.pyfunc
I get the following output:

  using 1 OpenMP thread(s) per MPI task
LAMMPS Version: 20210730  on MPI rank:  0
LAMMPS Version: 20210730  on MPI rank:  1
LAMMPS Version: 20210730  on MPI rank:  2
LAMMPS Version: 20210730  on MPI rank:  3
Total wall time: 0:00:00

It would be trivial to use an if statement to have this function only do something on MPI rank 0 and still have the python function by executed globally on all ranks.

Perhaps it would be more helpful, if you would first explain what exactly you need to run some python code on one MPI rank only and what you want to achieve by it that cannot be done otherwise in LAMMPS, instead of searching and asking for a feature that cannot be easily and efficiently implemented because of the principal design of the software.

dear Prof. akohlmey,

Thank you so much for your instruction. That is really helpful.
I use conda to manage packages on my python envs. And I found a lammp repo here: https://anaconda.org/conda-forge/lammps

But this repo can not be found when installing with either conda or pip. I dont know why?

%%%%%%%%%%
The reason I ask about this function becaue I need a minimized structure be written in *.pdb format to be used as a reference structure in some latter lammps commands in the same script.
But, because Lammps cannot produce *.pdb format, so I write a python script to convert from LammpsDUMP to PDB. And the problem comes when many MPI-ranks writte on the same PDB file, it produces unexpected data (1 data is written many times in an inordered manner). So that I need to execute python script only 1 time.

Now I know why? Since this repo does not support Windows

The LAMMPS packages for windows provided by the LAMMPS developers do contain PYTHON package support, if you download the installer with Python in the name: LAMMPS Windows Installer Repository

For this feature to work you need both, the PYTHON package and the Python module. The Python module is included in all LAMMPS installer packages from lammps.org.

That is not correct. LAMMPS includes the MOLFILE package with interfaces to VMD molfile plugins to output files in any format that the VMD molfile library supports: 6.2. Package details — LAMMPS documentation
dump molfile command — LAMMPS documentation

Besides, converting LAMMPS dump files from one format to another can be done just as well after the fact, e.g. with VMD scripting, and much more conveniently. Please also keep in mind, that doing serial tasks while running in parallel can be quite damaging to parallel efficiency.

Please note that the LAMMPS package in conda forge is not maintained by the LAMMPS developers (as is the case with most pre-packaged LAMMPS binaries).

dear Prof. akohlmey,

Thank you so much for your instruction.
I will search for the MOLFILE package