From 59dc83eadbde4521cb3076cccb4f5e2963a70d4b Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Tue, 9 Aug 2016 04:08:01 -0400 Subject: [PATCH 1/5] Fix resource leak --- python/lammps.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/lammps.py b/python/lammps.py index 0a996d0235..9f7e02e8e4 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -304,6 +304,9 @@ class OutputCapture(object): def __exit__(self, type, value, tracebac): os.dup2(self.stdout, self.stdout_fd) + os.close(self.stdout) + os.close(self.stdout_pipe_read) + os.close(self.stdout_pipe_write) # check if we have more to read from the pipe def more_data(self, pipe): From 33a87a470a2bdd9791c742be20810b7193e4db3b Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Tue, 9 Aug 2016 04:11:05 -0400 Subject: [PATCH 2/5] Return 2D vectors in 2D cases --- python/lammps.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/python/lammps.py b/python/lammps.py index 9f7e02e8e4..285596caa6 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -348,8 +348,11 @@ class AtomList(object): def __init__(self, lammps_wrapper_instance): self.lmp = lammps_wrapper_instance self.natoms = self.lmp.system.natoms + self.dimensions = self.lmp.system.dimensions def __getitem__(self, index): + if self.dimensions == 2: + return Atom2D(self.lmp, index + 1) return Atom(self.lmp, index + 1) @@ -397,6 +400,26 @@ class Atom(object): return self.lmp.eval("q[%d]" % self.index) +class Atom2D(Atom): + def __init__(self, lammps_wrapper_instance, index): + super(Atom2D, self).__init__(lammps_wrapper_instance, index) + + @property + def position(self): + return (self.lmp.eval("x[%d]" % self.index), + self.lmp.eval("y[%d]" % self.index)) + + @property + def velocity(self): + return (self.lmp.eval("vx[%d]" % self.index), + self.lmp.eval("vy[%d]" % self.index)) + + @property + def force(self): + return (self.lmp.eval("fx[%d]" % self.index), + self.lmp.eval("fy[%d]" % self.index)) + + class PyLammps(object): """ More Python-like wrapper for LAMMPS (e.g., for iPython) From 691de01b33839ffc77590e668db5115116c16e7c Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Tue, 9 Aug 2016 04:11:42 -0400 Subject: [PATCH 3/5] Allow setting the position of atoms --- python/lammps.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/python/lammps.py b/python/lammps.py index 285596caa6..c2e9fa983b 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -383,6 +383,12 @@ class Atom(object): self.lmp.eval("y[%d]" % self.index), self.lmp.eval("z[%d]" % self.index)) + @position.setter + def position(self, value): + self.lmp.set("atom", self.index, "x", value[0]) + self.lmp.set("atom", self.index, "y", value[1]) + self.lmp.set("atom", self.index, "z", value[2]) + @property def velocity(self): return (self.lmp.eval("vx[%d]" % self.index), @@ -409,6 +415,11 @@ class Atom2D(Atom): return (self.lmp.eval("x[%d]" % self.index), self.lmp.eval("y[%d]" % self.index)) + @position.setter + def position(self, value): + self.lmp.set("atom", self.index, "x", value[0]) + self.lmp.set("atom", self.index, "y", value[1]) + @property def velocity(self): return (self.lmp.eval("vx[%d]" % self.index), From a08cf7a4b6a0dc123ceb4b81570827d97a968b4f Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Wed, 10 Aug 2016 14:29:07 -0400 Subject: [PATCH 4/5] Add verbose option in PyLammps methods This option allows forcing LAMMPS output to be printed to the console. --- python/lammps.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/lammps.py b/python/lammps.py index c2e9fa983b..6eba1b73c0 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -640,6 +640,10 @@ class PyLammps(object): with OutputCapture() as capture: self.lmp.command(' '.join(cmd_args)) output = capture.output + + if 'verbose' in kwargs and kwargs['verbose']: + print(output) + lines = output.splitlines() if len(lines) > 1: From 6cbdad7a97a702c28c2f805e3b52e1be968eec3f Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Wed, 10 Aug 2016 14:35:48 -0400 Subject: [PATCH 5/5] Add utility function to IPyLammps for embedding videos --- python/lammps.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/lammps.py b/python/lammps.py index 6eba1b73c0..472f27ce29 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -698,3 +698,7 @@ class IPyLammps(PyLammps): self.write_dump(*cmd_args) from IPython.core.display import Image return Image('snapshot.png') + + def video(self, filename): + from IPython.display import HTML + return HTML("")