Skip to content

Commit

Permalink
Allow more than 20 terminal groups in CompoundPhaseDiagram (#4081)
Browse files Browse the repository at this point in the history
* 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 <fonari@schrodinger.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent d699678 commit 60441ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/pymatgen/analysis/phase_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,34 @@ def __init__(self, entries, terminal_compositions, normalize_terminal_compositio
self.species_mapping = species_mapping
super().__init__(p_entries, elements=species_mapping.values())

@staticmethod
def num2str(num):
"""
Convert number to a list of letter(s). First letter must be `f`.
:param num int: Number to convert
:rtype: str
:return Converted string
"""

# `f` letter was hard coded, do not modify. Alphabet consists of letters
# between f and z.
letter = "f"
code, z_code = ord(letter), ord("z") + 1

rest_num_letters = z_code - code
mult = num // rest_num_letters

ret = letter * mult if mult else ""

remainder = num % rest_num_letters + 1

start = code - 1
ret += chr(start + remainder)

return ret

def transform_entries(self, entries, terminal_compositions):
"""
Method to transform all entries to the composition coordinate in the
Expand All @@ -1493,7 +1521,7 @@ def transform_entries(self, entries, terminal_compositions):
# Map terminal compositions to unique dummy species.
sp_mapping = {}
for idx, comp in enumerate(terminal_compositions):
sp_mapping[comp] = DummySpecies("X" + chr(102 + idx))
sp_mapping[comp] = DummySpecies("X" + self.num2str(idx))

for entry in entries:
if getattr(entry, "attribute", None) is None:
Expand Down
12 changes: 12 additions & 0 deletions tests/analysis/test_phase_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,18 @@ def test_get_formation_energy(self):
def test_str(self):
assert str(self.pd) == "Xf-Xg phase diagram\n4 stable phases: \nLiFeO2, Li2O, Li5FeO4, Fe2O3"

def test_num2str(self):
ret = set()

num = 100
for idx in range(num):
val = CompoundPhaseDiagram.num2str(idx)
assert val.isalpha()
assert ord(val[0]) >= ord("f")
ret.add(val)

assert len(ret) == num


class TestPatchedPhaseDiagram(TestCase):
def setUp(self):
Expand Down

0 comments on commit 60441ea

Please sign in to comment.