Add PyLammps.atoms test and fix bug with -echo screen/both

This commit is contained in:
Richard Berger
2020-09-16 12:48:20 -04:00
parent 9cdd35e625
commit 2270d8f4ec
2 changed files with 20 additions and 2 deletions

View File

@ -1974,6 +1974,14 @@ class PyLammps(object):
""" """
def __init__(self, name="", cmdargs=None, ptr=None, comm=None): def __init__(self, name="", cmdargs=None, ptr=None, comm=None):
self.has_echo = False
if cmdargs:
if '-echo' in cmdargs:
idx = cmdargs.index('-echo')
# ensures that echo line is ignored during output capture
self.has_echo = idx+1 < len(cmdargs) and cmdargs[idx+1] in ('screen', 'both')
if ptr: if ptr:
if isinstance(ptr,PyLammps): if isinstance(ptr,PyLammps):
self.lmp = ptr.lmp self.lmp = ptr.lmp
@ -2323,7 +2331,8 @@ 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.command(' '.join(cmd_args)) cmd = ' '.join(cmd_args)
self.command(cmd)
output = capture.output output = capture.output
if 'verbose' in kwargs and kwargs['verbose']: if 'verbose' in kwargs and kwargs['verbose']:
@ -2331,6 +2340,9 @@ class PyLammps(object):
lines = output.splitlines() lines = output.splitlines()
if self.has_echo:
lines = lines[1:]
if len(lines) > 1: if len(lines) > 1:
return lines return lines
elif len(lines) == 1: elif len(lines) == 1:

View File

@ -6,7 +6,10 @@ class PythonPyLammps(unittest.TestCase):
machine = None machine = None
if 'LAMMPS_MACHINE_NAME' in os.environ: if 'LAMMPS_MACHINE_NAME' in os.environ:
machine=os.environ['LAMMPS_MACHINE_NAME'] machine=os.environ['LAMMPS_MACHINE_NAME']
self.pylmp = PyLammps(name=machine, cmdargs=['-nocite', '-log','none', '-echo','screen']) self.pylmp = PyLammps(name=machine, cmdargs=['-nocite', '-log','none', '-echo', 'screen'])
self.pylmp.units("lj")
self.pylmp.atom_style("atomic")
self.pylmp.atom_modify("map array")
if 'LAMMPS_CMAKE_CACHE' in os.environ: if 'LAMMPS_CMAKE_CACHE' in os.environ:
self.cmake_cache = {} self.cmake_cache = {}
@ -45,6 +48,9 @@ class PythonPyLammps(unittest.TestCase):
self.assertEqual(self.pylmp.lmp.create_atoms(2, id=None, type=types, x=x), 2) self.assertEqual(self.pylmp.lmp.create_atoms(2, id=None, type=types, x=x), 2)
self.assertEqual(self.pylmp.system.natoms, 2) self.assertEqual(self.pylmp.system.natoms, 2)
self.assertEqual(len(self.pylmp.atoms), 2)
self.assertEqual(self.pylmp.atoms[0].position, tuple(x[0:3]))
self.assertEqual(self.pylmp.atoms[1].position, tuple(x[3:6]))
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()