31
31
from pymatgen .util .string import Stringify
32
32
33
33
if TYPE_CHECKING :
34
- from typing import Any , Literal
34
+ from typing import Any , Literal , ClassVar
35
35
36
36
import matplotlib .pyplot as plt
37
37
from numpy .typing import NDArray
@@ -410,17 +410,18 @@ def ion_or_solid_comp_object(formula: str) -> Composition | Ion:
410
410
Returns:
411
411
Composition/Ion object
412
412
"""
413
+ # Formula for ion
413
414
if re .match (r"\[([^\[\]]+)\]|\(aq\)" , formula ):
414
415
comp_obj = Ion .from_formula (formula )
416
+
417
+ # Formula for solid
415
418
elif re .search (r"\(s\)" , formula ):
416
419
comp_obj = Composition (formula [:- 3 ])
420
+
417
421
else :
418
422
comp_obj = Composition (formula )
419
- return comp_obj
420
-
421
-
422
- ELEMENTS_HO : set [Element ] = {Element ("H" ), Element ("O" )}
423
423
424
+ return comp_obj
424
425
425
426
# TODO: the solids filter breaks some of the functionality of the
426
427
# heatmap plotter, because the reference states for decomposition
@@ -433,6 +434,9 @@ def ion_or_solid_comp_object(formula: str) -> Composition | Ion:
433
434
class PourbaixDiagram (MSONable ):
434
435
"""Create a Pourbaix diagram from entries."""
435
436
437
+
438
+ elements_ho : ClassVar [set [Element ]] = {Element ("H" ), Element ("O" )}
439
+
436
440
def __init__ (
437
441
self ,
438
442
entries : list [PourbaixEntry ] | list [MultiEntry ],
@@ -466,7 +470,7 @@ def __init__(
466
470
467
471
# Get non-OH elements
468
472
self .pbx_elts = list (
469
- set (itertools .chain .from_iterable ([entry .composition .elements for entry in entries ])) - ELEMENTS_HO
473
+ set (itertools .chain .from_iterable ([entry .composition .elements for entry in entries ])) - self . elements_ho
470
474
)
471
475
self .dim = len (self .pbx_elts ) - 1
472
476
@@ -478,7 +482,7 @@ def __init__(
478
482
self ._unprocessed_entries = single_entries
479
483
self ._filtered_entries = single_entries
480
484
self ._conc_dict = None
481
- self ._elt_comp = {k : v for k , v in entries [0 ].composition .items () if k not in ELEMENTS_HO }
485
+ self ._elt_comp = {k : v for k , v in entries [0 ].composition .items () if k not in self . elements_ho }
482
486
self ._multi_element = True
483
487
484
488
# Process single entry inputs
@@ -498,7 +502,7 @@ def __init__(
498
502
499
503
# If a conc_dict is specified, override individual entry concentrations
500
504
for entry in ion_entries :
501
- ion_elts = list (set (entry .elements ) - ELEMENTS_HO )
505
+ ion_elts = list (set (entry .elements ) - self . elements_ho )
502
506
# TODO: the logic here for ion concentration setting is in two
503
507
# places, in PourbaixEntry and here, should be consolidated
504
508
if len (ion_elts ) == 1 :
@@ -869,7 +873,7 @@ def get_decomposition_energy(
869
873
# Check composition consistency between entry and Pourbaix diagram:
870
874
pbx_comp = Composition (self ._elt_comp ).fractional_composition
871
875
entry_pbx_comp = Composition (
872
- {elt : coeff for elt , coeff in entry .composition .items () if elt not in ELEMENTS_HO }
876
+ {elt : coeff for elt , coeff in entry .composition .items () if elt not in self . elements_ho }
873
877
).fractional_composition
874
878
if entry_pbx_comp != pbx_comp :
875
879
raise ValueError ("Composition of stability entry does not match Pourbaix Diagram" )
0 commit comments