Skip to content
Snippets Groups Projects
Commit 6e3705f3 authored by Richard Berger's avatar Richard Berger
Browse files

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
parent 717e719b
No related branches found
No related tags found
No related merge requests found
...@@ -454,6 +454,7 @@ class PyLammps(object): ...@@ -454,6 +454,7 @@ class PyLammps(object):
else: else:
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 = []
def __del__(self): def __del__(self):
if self.lmp: self.lmp.close() if self.lmp: self.lmp.close()
...@@ -469,8 +470,15 @@ class PyLammps(object): ...@@ -469,8 +470,15 @@ class PyLammps(object):
def file(self,file): def file(self,file):
self.lmp.file(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): def command(self,cmd):
self.lmp.command(cmd) self.lmp.command(cmd)
self._cmd_history.append(cmd)
@property @property
def atoms(self): def atoms(self):
...@@ -643,7 +651,7 @@ class PyLammps(object): ...@@ -643,7 +651,7 @@ class PyLammps(object):
cmd_args = [name] + [str(x) for x in args] cmd_args = [name] + [str(x) for x in args]
with OutputCapture() as capture: with OutputCapture() as capture:
self.lmp.command(' '.join(cmd_args)) self.command(' '.join(cmd_args))
output = capture.output output = capture.output
if 'verbose' in kwargs and kwargs['verbose']: if 'verbose' in kwargs and kwargs['verbose']:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment