add test for yaml dump style

This commit is contained in:
Axel Kohlmeyer
2022-04-07 02:43:39 -04:00
parent 98b908387f
commit 3970942028
3 changed files with 60 additions and 5 deletions

View File

@ -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

View File

@ -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()