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.
This commit is contained in:
@ -400,6 +400,7 @@ class PyLammps(object):
|
|||||||
self.lmp = lammps(name=name,cmdargs=cmdargs,ptr=None,comm=comm)
|
self.lmp = lammps(name=name,cmdargs=cmdargs,ptr=None,comm=comm)
|
||||||
print("LAMMPS output is captured by PyLammps wrapper")
|
print("LAMMPS output is captured by PyLammps wrapper")
|
||||||
self._cmd_history = []
|
self._cmd_history = []
|
||||||
|
self._enable_cmd_history = False
|
||||||
self.runs = []
|
self.runs = []
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
@ -434,6 +435,24 @@ class PyLammps(object):
|
|||||||
"""
|
"""
|
||||||
self.lmp.file(file)
|
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):
|
def write_script(self, filepath):
|
||||||
"""
|
"""
|
||||||
Write LAMMPS script file containing all commands executed up until now
|
Write LAMMPS script file containing all commands executed up until now
|
||||||
@ -445,17 +464,27 @@ class PyLammps(object):
|
|||||||
for cmd in self._cmd_history:
|
for cmd in self._cmd_history:
|
||||||
print(cmd, file=f)
|
print(cmd, file=f)
|
||||||
|
|
||||||
|
def clear_cmd_history(self):
|
||||||
|
"""
|
||||||
|
Clear LAMMPS command history up to this point
|
||||||
|
"""
|
||||||
|
self._cmd_history = []
|
||||||
|
|
||||||
def command(self, cmd):
|
def command(self, cmd):
|
||||||
"""
|
"""
|
||||||
Execute LAMMPS command
|
Execute LAMMPS command
|
||||||
|
|
||||||
All commands executed will be stored in a command history which can be
|
If :py:attr:`PyLammps.enable_cmd_history` is set to ``True``, commands executed
|
||||||
written to a file using :py:meth:`PyLammps.write_script()`
|
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
|
:param cmd: command string that should be executed
|
||||||
:type: cmd: string
|
:type: cmd: string
|
||||||
"""
|
"""
|
||||||
self.lmp.command(cmd)
|
self.lmp.command(cmd)
|
||||||
|
|
||||||
|
if self.enable_cmd_history:
|
||||||
self._cmd_history.append(cmd)
|
self._cmd_history.append(cmd)
|
||||||
|
|
||||||
def run(self, *args, **kwargs):
|
def run(self, *args, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user