Merge branch 'improved_python_interface' of https://github.com/rbberger/lammps into merge-pull-116

This commit is contained in:
Axel Kohlmeyer
2016-07-18 18:25:24 -04:00
5 changed files with 105 additions and 52 deletions

View File

@ -58,6 +58,11 @@ It also gives you an easier way to query the system state, evaluate variable
expressions and access atom data. See examples/ipython/interface_usage.ipynb
for more details.
NOTE: this alternative wrapper was primarily developed for Python 3. While it
is mostly compatible to Python 2, not all functionality is available due to
language restrictions. E.g., 'print' is a reserved keyword in Python 2, which
is why the LAMMPS 'print' command is only available as 'lmp_print'.
-------------------------------------------------------------------
Once you have successfully wrapped LAMMPS, you can run the Python

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -302,7 +302,7 @@ class OutputCapture(object):
def read_pipe(self, pipe):
out = ""
while self.more_data(pipe):
out += str(os.read(pipe, 1024), 'utf-8')
out += os.read(pipe, 1024).decode()
return out
@property
@ -322,7 +322,7 @@ class Variable(object):
if self.style == 'atom':
return list(self.wrapper.lmp.extract_variable(self.name, "all", 1))
else:
value = self.wrapper.print('"${%s}"' % self.name).strip()
value = self.wrapper.lmp_print('"${%s}"' % self.name).strip()
try:
return float(value)
except ValueError:
@ -388,7 +388,8 @@ class PyLammps(object):
See examples/ipython for usage
"""
def __init__(self, lmp):
def __init__(self, lmp=lammps()):
print("LAMMPS output is captured by PyLammps wrapper")
self.lmp = lmp
@property
@ -436,7 +437,7 @@ class PyLammps(object):
return vars
def eval(self, expr):
value = self.print('"$(%s)"' % expr).strip()
value = self.lmp_print('"$(%s)"' % expr).strip()
try:
return float(value)
except ValueError:
@ -553,6 +554,10 @@ class PyLammps(object):
groups.append(group)
return groups
def lmp_print(self, s):
""" needed for Python2 compatibility, since print is a reserved keyword """
return self.__getattr__("print")(s)
def __getattr__(self, name):
def handler(*args, **kwargs):
cmd_args = [name] + [str(x) for x in args]
@ -576,7 +581,7 @@ class IPyLammps(PyLammps):
iPython wrapper for LAMMPS which adds embedded graphics capabilities
"""
def __init__(self, lmp):
def __init__(self, lmp=lammps()):
super(IPyLammps, self).__init__(lmp)
def image(self, filename="snapshot.png", group="all", color="type", diameter="type",