Skip to content

Commit 3809f76

Browse files
committed
Refactor: generalize the rule to switch the io backend of abacuslite
1 parent bba6408 commit 3809f76

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

  • interfaces/ASE_interface/abacuslite

interfaces/ASE_interface/abacuslite/core.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@
6060
)
6161

6262
__LEGACYIO__ = True
63+
def switch_io_backend_version(version: str) -> bool:
64+
'''determine if the i/o is in legacy format by the version number,
65+
for detailed discussion, see issue #7260
66+
'''
67+
global __LEGACYIO__
68+
m = re.match(r'^v(\d+)\.(\d+)\.(\d+)(\.\d+|\-(alpha|beta|rc)\.\d+)?$', version)
69+
assert m, f'Invalid format of version number, please check file version.h'
70+
assert int(m.group(1)) >= 3, f'ABACUS v2.x is not supported'
71+
if int(m.group(2)) >= 11:
72+
__LEGACYIO__ = False
73+
elif int(m.group(2)) == 9:
74+
if m.group(4) is not None:
75+
# it is also possible to divide the version more carefully,
76+
# but because 3.9.0.x are all on the develop branch, up to
77+
# now, there is no user submit issue to request such a careful
78+
# division
79+
__LEGACYIO__ = False
80+
else:
81+
__LEGACYIO__ = True
82+
return __LEGACYIO__
6383

6484
class AbacusProfile(BaseProfile):
6585
'''AbacusProfile for interacting the ASE with ABACUS that installed in
@@ -397,11 +417,9 @@ def __init__(self,
397417
# not recommended :(
398418
profile = AbacusProfile('abacus') if profile is None else profile
399419

400-
# does not support ABACUS version series v3.9.0.x and v3.11.0-beta.x
401-
version = profile.version()
402-
if re.match(r'v3\.9\.0\.\d+', version) or re.match(r'v3\.11\.0-beta\.\d+', version):
403-
global __LEGACYIO__
404-
__LEGACYIO__ = False
420+
# to be compatible with both the legacy and latest format of i/o, the
421+
# switch is needed.
422+
_ = switch_io_backend_version(profile.version())
405423

406424
# because ABACUS run job in folders, based on the assumption that
407425
# there is only one job in the folder. Therefore once there are already
@@ -632,5 +650,18 @@ def test_restart(self):
632650
e2 = silicon.get_potential_energy()
633651
self.assertAlmostEqual(e2, e)
634652

653+
def test_version_number_check(self):
654+
# not a version number
655+
with self.assertRaises(AssertionError):
656+
switch_io_backend_version('not-a-version-number')
657+
# too old version
658+
with self.assertRaises(AssertionError):
659+
switch_io_backend_version('v2.2.2')
660+
self.assertTrue(switch_io_backend_version('v3.8.4'))
661+
self.assertFalse(switch_io_backend_version('v3.9.0.25'))
662+
self.assertTrue(switch_io_backend_version('v3.10.0'))
663+
self.assertFalse(switch_io_backend_version('v3.11.0-beta.2'))
664+
self.assertFalse(switch_io_backend_version('v3.11.0'))
665+
635666
if __name__ == '__main__':
636667
unittest.main()

0 commit comments

Comments
 (0)