From f467832e0f48d6aeb1c14fcbd3a30b27af3d537f Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Mon, 22 Feb 2021 01:29:50 -0500 Subject: [PATCH] Make PyLammps command history feature optional PyLammps so far has been saving a history for every executed command. This was originally added to allow writing out the commands of interactive PyLammps sessions as regular input scripts. This commit disables this history by default, which avoids the small, but rising memory consumption over time. It can be enabled and disabled with the enable_cmd_history property. There is also now a method to clear the history at any time. --- python/lammps/pylammps.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/python/lammps/pylammps.py b/python/lammps/pylammps.py index 47a2a5a6ab..4bba9f5e94 100644 --- a/python/lammps/pylammps.py +++ b/python/lammps/pylammps.py @@ -400,6 +400,7 @@ class PyLammps(object): self.lmp = lammps(name=name,cmdargs=cmdargs,ptr=None,comm=comm) print("LAMMPS output is captured by PyLammps wrapper") self._cmd_history = [] + self._enable_cmd_history = False self.runs = [] def __del__(self): @@ -434,6 +435,24 @@ class PyLammps(object): """ self.lmp.file(file) + @property + def enable_cmd_history(self): + """ + :getter: Return whether command history is saved + :setter: Set if command history should be saved + :type: bool + """ + return self._enable_cmd_history + + @enable_cmd_history.setter + def enable_cmd_history(self, value): + """ + :getter: Return whether command history is saved + :setter: Set if command history should be saved + :type: bool + """ + self._enable_cmd_history = (value == True) + def write_script(self, filepath): """ Write LAMMPS script file containing all commands executed up until now @@ -445,18 +464,28 @@ class PyLammps(object): for cmd in self._cmd_history: print(cmd, file=f) + def clear_cmd_history(self): + """ + Clear LAMMPS command history up to this point + """ + self._cmd_history = [] + def command(self, cmd): """ Execute LAMMPS command - All commands executed will be stored in a command history which can be - written to a file using :py:meth:`PyLammps.write_script()` + If :py:attr:`PyLammps.enable_cmd_history` is set to ``True``, commands executed + will be recorded. The entire command history can be written to a file using + :py:meth:`PyLammps.write_script()`. To clear the command history, use + :py:meth:`PyLammps.clear_cmd_history()`. :param cmd: command string that should be executed :type: cmd: string """ self.lmp.command(cmd) - self._cmd_history.append(cmd) + + if self.enable_cmd_history: + self._cmd_history.append(cmd) def run(self, *args, **kwargs): """