[lammps-users] Too Many Files Open

I am trying to calculate energies of small molecules using a python script driving lammps.
I have more than 60000 molecules, each molecule is in a separate data file.
My python script is something like this:

def runlammps(datafile,ffield):
typeorder = ’ C H O N ’
lmp = lammps() #cmdargs=["-screen",“none”])
lmp.command(“units real”)
lmp.command(“boundary p p p”)
lmp.command(“atom_style charge”)
lmp.command("read_data s " datafile)
lmp.command(“pair_style reax/c NULL safezone 1.8 mincap 400”)
lmp.command("pair_coeff * * %s s" (ffield, typeorder))
lmp.command(“neighbor 2 bin”)
lmp.command(“neigh_modify every 10 delay 0 check no”)
lmp.command(“fix fqeq all qeq/reax 1 0.0 10.0 1e-6 reax/c”)
lmp.command(“timestep 0.25”)
lmp.command(‘run 1’)
en = lmp.get_thermo(‘etotal’)
return lmp,en

and calling runlammps function from main():

for i in range(2000):
filename = filenames[i]
lmp,en = runlammps(filename,ffield)
del lmp

After calculating energies I close lammps instance and even deleting it however after reading 1000 molecules it throws this error:

LAMMPS (14 Dec 2021)
OMP_NUM_THREADS environment is not set. Defaulting to 1 thread. (src/comm.cpp:98)
using 1 OpenMP thread(s) per MPI task
ERROR: Cannot open file reax_14098196.lmp: Too many open files (src/read_data.cpp:333)
Last command: read_data reax_14098196.lmp

The operating system does not allow more than 1024 files open at the same time. However lmp.close() should also close any open files right?
I checked the read_data.cpp:333 , it checks platform:isfile_open but that function returns false. Apparently lammps.close() is not closing open files or am I missing something?

Dundar Yilmaz, Ph.D.

Associate Research Professor
Department of Mechanical Engineering
138 Research East Building
The Pennsylvania State University
University Park, PA 16802

Yes, you are missing something. File handle leaks (and memory leaks) can happen everywhere, so to be able to tell you need to check each pair of open/close (malloc/free). However, that is not always trivial to do. There are specific memory checker tools like valgrind that can help, but require practice and report many false positives when checking python scripts.

For example, there was a file handle (and memory) leak inside the REAXFF package that was detected and fixed only after a few days ago, and that could be the cause for your problems.
It is a simple one line change. Please apply, recompile and check again.


BTW: on most Linux boxes you can raise the limit of available file handles from the default of 1024 to something like 524288 using ulimit -n.