Problem writing python with Lammps to output

Hi Lammps community,

I have a quick question while running Lammps with Python in our HPC system. I in stalled Lammps via conda cloud.

This is supposed to be a very simple question but I really don’t know anything about Lammps code.

I have a code with running Lammps in Python: let say a.py.

If I run the code using command:

$ python a.py

This code works fine and write the output to screen

However if I want to write the output to a file, for example output.txt, I run the command:

$ python a.py > output.txt

I have an error:

Traceback (most recent call last):

File “a.py”, line 10, in

a0 = L.eval(“a”)

File “usr/.conda/envs/LammpsPy/lib/python3.7/site-packages/lammps.py”, line 931, in eval

value = self.lmp_print(’"$(s)"' expr).strip()

AttributeError: ‘NoneType’ object has no attribute ‘strip’

It would be useful to know, which version of LAMMPS this is with (the date printed when you launch LAMMPS).
it also would be helpful, if you would provide a simple and complete example that demonstrates the issue reliably and quickly.

there are some known difficulties with line-buffering vs block-buffering when redirecting output, since PyLammps captures and interprets LAMMPS output and thus is dependent on line-buffering as far as i know.
if you want to play it safe, use the more low-level “lammps” python class, which is not affected by this.
i am copying richard berger, the author of the PyLammps class, who might be able to comment further.

axel.

This is related to the following issues:

https://github.com/lammps/lammps/issues/666

and

https://github.com/lammps/lammps/issues/871

Redirecting the output of LAMMPS when using PyLammps is problematic. The solution is to use stdbuf, using python3 or setting the environment variable PYTHONUNBUFFERED=1

stdbuf -oL python3 bug.py > lmp.log

Best,

Richard

Hi Richard,

Thanks for jumping in to help.

That’s perfect answer.

I know there is something wrong in writing the Lammps output but I could not figure it out.

I am able to run the program now.

Regards,

Tue