From 3970942028d6964957e556d11bb5e8d9870649c8 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 7 Apr 2022 02:43:39 -0400 Subject: [PATCH] add test for yaml dump style --- examples/yaml/in.yaml | 8 ++--- python/lammps/formats.py | 2 +- unittest/python/python-formats.py | 55 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/examples/yaml/in.yaml b/examples/yaml/in.yaml index 28660751c8..f682f39776 100644 --- a/examples/yaml/in.yaml +++ b/examples/yaml/in.yaml @@ -29,9 +29,9 @@ fix 1 all nve thermo_style yaml thermo 10 -dump 1 all yaml 25 dump.yaml id type x y z ix iy iz vx vy vz -dump_modify 1 sort id thermo yes units yes time yes format 1 %5d format float "% 12.8e" format int %2d +#dump 1 all yaml 25 dump.yaml id type x y z ix iy iz vx vy vz +#dump_modify 1 sort id thermo yes units yes time yes format 1 %5d format float "% 12.8e" format int %2d -run 100 +run 100 post no -run 100 +run 100 post no diff --git a/python/lammps/formats.py b/python/lammps/formats.py index 83d05dd9f7..a311867253 100644 --- a/python/lammps/formats.py +++ b/python/lammps/formats.py @@ -21,7 +21,7 @@ import re, yaml try: from yaml import CSafeLoader as Loader, CSafeDumper as Dumper except ImportError: - from yaml import SafeLoader as Loader, SafeDumper as Dumper + from yaml import SafeLoader as Loader, SafeDumper as Dumper class LogFile: """Reads LAMMPS log files and extracts the thermo information diff --git a/unittest/python/python-formats.py b/unittest/python/python-formats.py index 9e7863e198..4d6aa7e2cd 100644 --- a/unittest/python/python-formats.py +++ b/unittest/python/python-formats.py @@ -2,6 +2,12 @@ import os import unittest from lammps.formats import LogFile, AvgChunkFile +import yaml +try: + from yaml import CSafeLoader as Loader, CSafeDumper as Dumper +except ImportError: + from yaml import SafeLoader, SafeDumper + EXAMPLES_DIR=os.path.abspath(os.path.join(__file__, '..', '..', '..', 'examples')) DEFAULT_STYLE_EXAMPLE_LOG="melt/log.8Apr21.melt.g++.1" @@ -109,5 +115,54 @@ class AvgChunkFiles(unittest.TestCase): self.assertEqual(len(chunk['coord'][0]), 1) +from lammps import lammps +has_full = False +try: + machine=None + if 'LAMMPS_MACHINE_NAME' in os.environ: + machine=os.environ['LAMMPS_MACHINE_NAME'] + lmp=lammps(name=machine) + has_full = lmp.has_style("atom","full") + lmp.close() +except: + pass + +@unittest.skipIf(not has_full, "atom_style full is not available") +class PythonDump(unittest.TestCase): + def setUp(self): + machine = None + if 'LAMMPS_MACHINE_NAME' in os.environ: + machine=os.environ['LAMMPS_MACHINE_NAME'] + self.lmp = lammps(name=machine, cmdargs=['-nocite', '-log','none', '-echo','screen']) + + def tearDown(self): + del self.lmp + + def testDumpYaml(self): + dumpfile = os.path.join(os.path.abspath('.'), 'dump.yaml') + self.lmp.command('shell cd ' + os.environ['TEST_INPUT_DIR']) + self.lmp.command("newton on on") + self.lmp.file("in.fourmol") + self.lmp.command("dump 1 all yaml 2 " + dumpfile + " id type mol q x y z vx vy vz") + self.lmp.command("dump_modify 1 time yes sort id units yes") + self.lmp.command("run 4 post no") + with open(dumpfile) as d: + traj = tuple(yaml.load_all(d, Loader=Loader)) + self.assertEqual(len(traj), 3) + self.assertEqual(traj[0]['timestep'], 0) + self.assertEqual(traj[0]['time'], 0) + self.assertEqual(traj[0]['natoms'], 29) + self.assertEqual(traj[0]['units'], 'real') + self.assertEqual(len(traj[0]['boundary']), 6) + self.assertEqual(traj[0]['boundary'][0], 'p') + self.assertEqual(traj[1]['timestep'], 2) + self.assertEqual(traj[1]['time'], 0.2) + self.assertEqual(traj[2]['timestep'], 4) + self.assertEqual(traj[2]['time'], 0.4) + self.assertEqual(traj[0]['keywords'],['id', 'type', 'mol', 'q', 'x', 'y', 'z', + 'vx', 'vy', 'vz']) + self.assertEqual(traj[0]['data'][0],[1, 3, 1, -0.47, -0.279937, 2.47266, -0.172009, + 0.000778678, 0.000589703, -0.000221795]) + if __name__ == "__main__": unittest.main()