[lammps-users] Python printing according to number of processors in mpi


LAMMPS version 3March20

I am calling python script from lammps while running it through mpi. This python script has to print some command in the lammps input itself. It is working and printing the command.

The problem is that it is printing the command according to the number of processors I am selecting. However, I just need to print this command only once. It is working fine when I am running it on 1 processor.

For example it is printing the command ‘x’ times if I use the command “mpirun -np x …”

Please suggest a solution. Thanks

Can you explain in more detail what you mean by ‘printing a command’ and provide a simple example or demonstration so we can see how it goes wrong.

Thanks, Axel.

For example, my python script read some data of coordinates and attach a string “create_atoms 6 single” to them and then prints it into input file (in.input).

Python script includes:

string_to_add = "create_atoms 6 single "

with open(‘coordinates.txt’, ‘r’) as p:
file_lines = [’’.join([string_to_add, x.strip(), ‘\n’]) for x in p.readlines()]
with open(‘coordinates.txt’, ‘w’) as p:

with open (“coordinates.txt”) as f1: #reading data from coordinates file
content= f1.readlines()

with open(“in.input”, “r+”) as f: #inserting the coordinates into lammps file at some #placeholder
a = [x.rstrip() for x in f]
index = 0
for item in a:
if item.startswith("#placeholder"):
a.insert(index, “”.join(map(str, content)) + ‘\n’)
index += 1

Go to start of file and clear it


Write each line back

for lines in a:
f.write(lines + “\n”)

Sorry, but this inot is not helping much. This is only a demonstration of how to do something in a very complicated way in python.

It does not show how this involves LAMMPS.