From d8557ac8027837958921d6a25b73a9babc40615d Mon Sep 17 00:00:00 2001 From: Han Wang Date: Thu, 13 Aug 2020 19:01:32 +0800 Subject: [PATCH] fix bug of append systems with different formula --- dpdata/system.py | 13 ++++++++++++- tests/poscars/POSCAR.h4o3 | 15 +++++++++++++++ tests/test_system_append.py | 10 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/poscars/POSCAR.h4o3 diff --git a/dpdata/system.py b/dpdata/system.py index b3d4c0b9..656563f2 100644 --- a/dpdata/system.py +++ b/dpdata/system.py @@ -331,7 +331,8 @@ def append(self, system) : # this system is non-converged but the system to append is converged self.data = system.data return False - assert(system.formula == self.formula) + if system.uniq_formula != self.uniq_formula: + raise RuntimeError('systems with inconsistent formula could not be append: %s v.s. %s' % (self.uniq_formula, system.uniq_formula)) if system.data['atom_names'] != self.data['atom_names']: # allow to append a system with different atom_names order system.sort_atom_names() @@ -418,6 +419,16 @@ def formula(self): return ''.join(["{}{}".format(symbol,numb) for symbol,numb in zip(self.data['atom_names'], self.data['atom_numbs'])]) + @property + def uniq_formula(self): + """ + Return the uniq_formula of this system. + The uniq_formula sort the elements in formula by names. + Systems with the same uniq_formula can be append together. + """ + return ''.join(["{}{}".format(symbol,numb) for symbol,numb in sorted( + zip(self.data['atom_names'], self.data['atom_numbs']))]) + def extend(self, systems): """ diff --git a/tests/poscars/POSCAR.h4o3 b/tests/poscars/POSCAR.h4o3 new file mode 100644 index 00000000..acd73713 --- /dev/null +++ b/tests/poscars/POSCAR.h4o3 @@ -0,0 +1,15 @@ +POSCAR file written by OVITO +1 +10 0.0 0.0 +-0.011409 10 0.0 +0.1411083 -0.0595569 10 + H O + 4 3 +d +.428 .424 .520 +.428 .424 .520 +.230 .628 .113 +.458 .352 .458 +.389 .384 .603 +.137 .626 .150 +.231 .589 .021 diff --git a/tests/test_system_append.py b/tests/test_system_append.py index 2a3d21cc..a166398b 100644 --- a/tests/test_system_append.py +++ b/tests/test_system_append.py @@ -6,6 +6,16 @@ from comp_sys import CompLabeledSys from comp_sys import IsPBC, IsNoPBC + +class TestFailedAppend(unittest.TestCase): + def test_failed_append(self): + sys1 = dpdata.System('poscars/POSCAR.h2o.md', fmt='vasp/poscar') + sys2 = dpdata.System('poscars/POSCAR.h4o3', fmt='vasp/poscar') + with self.assertRaises(Exception) as c: + sys1.append(sys2) + self.assertTrue("systems with inconsistent formula could not be append" in str(c.exception)) + + class TestVaspXmlAppend(unittest.TestCase, CompLabeledSys, IsPBC): def setUp (self) : self.places = 6