[lammps-users] Python script crashes when reading restart files in a loop


I need to read multiple restart files in order to get the value of some variables at given time steps. I need it because I have overwritten my output files when restarting the calculation. I know, I should have included a time-step variable in the filename.

I have performed GCMC calculation and I want to plot the number of molecules absorbed in a pore with respect to the time-step. So I have written a small Python script that is supposed to read all the restart files and get the number of molecules. Here is the first test :

import os
from lammps import IPyLammps

L = IPyLammps()

files = os.listdir(".")


L.variable(“nO equal count(oxygen)”)

It worked. Then I tried to add a loop :

for filename in files:

It crashed systematically. I tried with one iteration “for i in range(1)” and by replacing filename by one of the filename. It crashed too. I tried with “from lammps import lammps”. It crashed too. I tried with a time.sleep(10) before and after the read. It crashed. Is it a bug or did I do something wrong ?

Thanks for your help,


commands like “read_restart” or “read_data” or “create_box” will create the system cell and thus may only be called where a system cell does not (yet) exist.
to execute a loop like you describe you first have to wipe out everything and initialize LAMMPS to a clean state with the “clear” command, which should translate to L.clear() in the PyLammps interface.


Thanks a lot ! you saved me :slight_smile: