From 6e3705f3809d6c2bb188ddfce6f93b1e6b74c887 Mon Sep 17 00:00:00 2001 From: Richard Berger <richard.berger@temple.edu> Date: Wed, 24 Aug 2016 00:44:15 -0400 Subject: [PATCH] Allow writing input script from Python session This implements the requested feature in issue #145. The `write_script` method now gives you a way of dumping out all used commands into a LAMMPS input script file. Note: this also dumps all commands which are indirectly issued by PyLammps --- python/lammps.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/python/lammps.py b/python/lammps.py index 7926fa5e4a..daf526f0e9 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -454,6 +454,7 @@ class PyLammps(object): else: self.lmp = lammps(name=name,cmdargs=cmdargs,ptr=None,comm=comm) print("LAMMPS output is captured by PyLammps wrapper") + self._cmd_history = [] def __del__(self): if self.lmp: self.lmp.close() @@ -469,8 +470,15 @@ class PyLammps(object): def file(self,file): self.lmp.file(file) + def write_script(self,filename): + """ Write LAMMPS script file containing all commands executed up until now """ + with open(filename, "w") as f: + for cmd in self._cmd_history: + f.write("%s\n" % cmd) + def command(self,cmd): self.lmp.command(cmd) + self._cmd_history.append(cmd) @property def atoms(self): @@ -643,7 +651,7 @@ class PyLammps(object): cmd_args = [name] + [str(x) for x in args] with OutputCapture() as capture: - self.lmp.command(' '.join(cmd_args)) + self.command(' '.join(cmd_args)) output = capture.output if 'verbose' in kwargs and kwargs['verbose']: -- GitLab