Skip to content

Commit 60441ea

Browse files
afonariSasha Fonaripre-commit-ci[bot]
authored
Allow more than 20 terminal groups in CompoundPhaseDiagram (#4081)
* Allow more than 20 terminal groups in CompoundPhaseDiagram * Allow more than 20 terminal groups in CompoundPhaseDiagram * pre-commit auto-fixes --------- Co-authored-by: Sasha Fonari <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d699678 commit 60441ea

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/pymatgen/analysis/phase_diagram.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,34 @@ def __init__(self, entries, terminal_compositions, normalize_terminal_compositio
14711471
self.species_mapping = species_mapping
14721472
super().__init__(p_entries, elements=species_mapping.values())
14731473

1474+
@staticmethod
1475+
def num2str(num):
1476+
"""
1477+
Convert number to a list of letter(s). First letter must be `f`.
1478+
1479+
:param num int: Number to convert
1480+
1481+
:rtype: str
1482+
:return Converted string
1483+
"""
1484+
1485+
# `f` letter was hard coded, do not modify. Alphabet consists of letters
1486+
# between f and z.
1487+
letter = "f"
1488+
code, z_code = ord(letter), ord("z") + 1
1489+
1490+
rest_num_letters = z_code - code
1491+
mult = num // rest_num_letters
1492+
1493+
ret = letter * mult if mult else ""
1494+
1495+
remainder = num % rest_num_letters + 1
1496+
1497+
start = code - 1
1498+
ret += chr(start + remainder)
1499+
1500+
return ret
1501+
14741502
def transform_entries(self, entries, terminal_compositions):
14751503
"""
14761504
Method to transform all entries to the composition coordinate in the
@@ -1493,7 +1521,7 @@ def transform_entries(self, entries, terminal_compositions):
14931521
# Map terminal compositions to unique dummy species.
14941522
sp_mapping = {}
14951523
for idx, comp in enumerate(terminal_compositions):
1496-
sp_mapping[comp] = DummySpecies("X" + chr(102 + idx))
1524+
sp_mapping[comp] = DummySpecies("X" + self.num2str(idx))
14971525

14981526
for entry in entries:
14991527
if getattr(entry, "attribute", None) is None:

tests/analysis/test_phase_diagram.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,18 @@ def test_get_formation_energy(self):
701701
def test_str(self):
702702
assert str(self.pd) == "Xf-Xg phase diagram\n4 stable phases: \nLiFeO2, Li2O, Li5FeO4, Fe2O3"
703703

704+
def test_num2str(self):
705+
ret = set()
706+
707+
num = 100
708+
for idx in range(num):
709+
val = CompoundPhaseDiagram.num2str(idx)
710+
assert val.isalpha()
711+
assert ord(val[0]) >= ord("f")
712+
ret.add(val)
713+
714+
assert len(ret) == num
715+
704716

705717
class TestPatchedPhaseDiagram(TestCase):
706718
def setUp(self):

0 commit comments

Comments
 (0)