Skip to content

Commit

Permalink
Merge pull request #112 from deepmodeling/devel
Browse files Browse the repository at this point in the history
Fix bug of append systems with different formula
  • Loading branch information
amcadmus authored Aug 13, 2020
2 parents b0cb213 + 6f53839 commit f150dd1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion dpdata/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,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()
Expand Down Expand Up @@ -419,6 +420,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):
"""
Expand Down
15 changes: 15 additions & 0 deletions tests/poscars/POSCAR.h4o3
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions tests/test_system_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f150dd1

Please sign in to comment.