Skip to content

Commit f150dd1

Browse files
authored
Merge pull request #112 from deepmodeling/devel
Fix bug of append systems with different formula
2 parents b0cb213 + 6f53839 commit f150dd1

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

dpdata/system.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ def append(self, system) :
332332
# this system is non-converged but the system to append is converged
333333
self.data = system.data
334334
return False
335-
assert(system.formula == self.formula)
335+
if system.uniq_formula != self.uniq_formula:
336+
raise RuntimeError('systems with inconsistent formula could not be append: %s v.s. %s' % (self.uniq_formula, system.uniq_formula))
336337
if system.data['atom_names'] != self.data['atom_names']:
337338
# allow to append a system with different atom_names order
338339
system.sort_atom_names()
@@ -419,6 +420,16 @@ def formula(self):
419420
return ''.join(["{}{}".format(symbol,numb) for symbol,numb in
420421
zip(self.data['atom_names'], self.data['atom_numbs'])])
421422

423+
@property
424+
def uniq_formula(self):
425+
"""
426+
Return the uniq_formula of this system.
427+
The uniq_formula sort the elements in formula by names.
428+
Systems with the same uniq_formula can be append together.
429+
"""
430+
return ''.join(["{}{}".format(symbol,numb) for symbol,numb in sorted(
431+
zip(self.data['atom_names'], self.data['atom_numbs']))])
432+
422433

423434
def extend(self, systems):
424435
"""

tests/poscars/POSCAR.h4o3

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
POSCAR file written by OVITO
2+
1
3+
10 0.0 0.0
4+
-0.011409 10 0.0
5+
0.1411083 -0.0595569 10
6+
H O
7+
4 3
8+
d
9+
.428 .424 .520
10+
.428 .424 .520
11+
.230 .628 .113
12+
.458 .352 .458
13+
.389 .384 .603
14+
.137 .626 .150
15+
.231 .589 .021

tests/test_system_append.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
from comp_sys import CompLabeledSys
77
from comp_sys import IsPBC, IsNoPBC
88

9+
10+
class TestFailedAppend(unittest.TestCase):
11+
def test_failed_append(self):
12+
sys1 = dpdata.System('poscars/POSCAR.h2o.md', fmt='vasp/poscar')
13+
sys2 = dpdata.System('poscars/POSCAR.h4o3', fmt='vasp/poscar')
14+
with self.assertRaises(Exception) as c:
15+
sys1.append(sys2)
16+
self.assertTrue("systems with inconsistent formula could not be append" in str(c.exception))
17+
18+
919
class TestVaspXmlAppend(unittest.TestCase, CompLabeledSys, IsPBC):
1020
def setUp (self) :
1121
self.places = 6

0 commit comments

Comments
 (0)