Doubt using read_data command multiple times in the same code

First off, please see Please Read This First: Guidelines and Suggestions for posting LAMMPS questions for guidelines on how to correctly format quoted inputs and also about the uselessness of statement like “it does not work”.

Second, this is a long-winded explanation about something that is very simple. You are just not thinking like a programmer. The situation is:

  • there is no “undelete” for atoms, so you must restore the previous state
  • you must use “clear” to read a previous state from a restart with “read_restart”
  • the same is true for “read_data” only that less information is contained in a data file (it is far more portable to make up for it).
  • you lose all previous settings like groups, regions, etc. in the LAMMPS instance with “clear” since that literally destroys the LAMMPS simulations state and creates a fresh one. Only information that is contained in the restart is recreated

So the logical conclusion is that within your loops you must issue a clear command before resetting the system with read_restart or read_data. At that point you will also have to recreate all relevant settings that were in the LAMMPS instance before. Since you created them from your python script, you can easily recreate them since you do not lose the information of the python instance.

This is all very straightforward and just requires reviewing the documentation. You may need to adjust the logic and workflow of your script accordingly, but that is a common thing when programming: you cannot force your way of thinking on a computer, but you have to work with its limitations and its own logic.

P.S.: I would recommend against using PyLammps and instead use the regular lammps python module. PyLammps is by construction more fragile and the nature of its implementation has a significant negative impact on performance. For a larger programming project, it is straightforward to use wrapper functions that utilize the available introspection features to extract the information that PyLammps conveniently collects.