Error in running lammps using python file

Hi,

I am trying to run lammps using python using the following script:

from lammps import lammps
lmp = lammps()
lmp.file("+input.cntMS")

But i get the following error:
Note: The lammps input file “input.cntMS” is in the same directory as the poython script “cnt.py”

 /bin/python3 /home/vinayak/Documents/vinayak-phd/codes_vinayak/lammps/cnt/MS/cnt.py
LAMMPS (23 Jun 2022)
OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:98)
  using 1 OpenMP thread(s) per MPI task
ERROR on proc 0: Cannot open input script input.cntMS: No such file or directory (src/input.cpp:334)
Last command: (unknown)
Traceback (most recent call last):
  File "/home/vinayak/Documents/vinayak-phd/codes_vinayak/lammps/cnt/MS/cnt.py", line 4, in <module>
    lmp.file("input.cntMS")
  File "/home/vinayak/.local/lib/python3.8/site-packages/lammps/core.py", line 564, in file
    self.lib.lammps_file(self.lmp, path)
  File "/home/vinayak/.local/lib/python3.8/site-packages/lammps/core.py", line 49, in __exit__
    raise self.lmp._lammps_exception
Exception: ERROR on proc 0: Cannot open input script input.cntMS: No such file or directory (src/input.cpp:334
Total wall time: 0:00:00

I don’t get this error if i specify the full path of the lammps input file in the python script as follows:

from lammps import lammps
lmp = lammps()
lmp.file("/home/vinayak/Documents/vinayak-phd/codes_vinayak/lammps/cnt/MS/input.cntMS")

What is the reason for this? How can i get away with specifying the complete path even when the files are in the same directory?

Thanks
Vinayak

The inputs you provide and the error messages are not consistent.
If you have a file “input.cntMS” in your current folder and execute lmp.file("+input.cntMS")
you should get an error, namely this (because the filename is misspelled):

[[email protected] test]$ ls
input.cntMS  log.lammps  test.py
[[email protected] test]$ rm log.lammps 
[[email protected] test]$ ls
input.cntMS  test.py
[[email protected] test]$ cat input.cntMS 
print "hello!"
[[email protected] test]$ cat test.py 
from lammps import lammps
lmp = lammps()
lmp.file("+input.cntMS")
[[email protected] test]$ python test.py
LAMMPS (23 Jun 2022)
OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:98)
  using 1 OpenMP thread(s) per MPI task
ERROR on proc 0: Cannot open input script +input.cntMS: No such file or directory (src/input.cpp:334)
Last command: (unknown)
Traceback (most recent call last):
  File "/home/akohlmey/compile/lammps/build-test/test/test.py", line 3, in <module>
    lmp.file("+input.cntMS")
  File "/home/akohlmey/.local/lib/python3.10/site-packages/lammps/core.py", line 563, in file
    with ExceptionCheck(self):
  File "/home/akohlmey/.local/lib/python3.10/site-packages/lammps/core.py", line 49, in __exit__
    raise self.lmp._lammps_exception
Exception: ERROR on proc 0: Cannot open input script +input.cntMS: No such file or directory (src/input.cpp:33
Total wall time: 0:00:00

However, if I change the python code to refer to the input file correctly, it does execute as expected:

[[email protected] test]$ cat test.py
from lammps import lammps
lmp = lammps()
lmp.file("input.cntMS")
[[email protected] test]$ python test.py
LAMMPS (23 Jun 2022)
OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:98)
  using 1 OpenMP thread(s) per MPI task
hello!
Total wall time: 0:00:00

So the reason why you don’t get the expected result is between your chair and your computer.

1 Like