File: pourbaix.py

package info (click to toggle)
python-ase 3.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (945 lines) | stat: -rw-r--r-- 30,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# fmt: off

import functools
import re
from collections import Counter
from dataclasses import dataclass
from fractions import Fraction
from itertools import chain, combinations, product
from typing import List, Tuple, Union

import numpy as np
from scipy.linalg import null_space

from ase.formula import Formula
from ase.units import kB

CONST = kB * np.log(10)  # Nernst constant

PREDEF_ENERGIES = {      # Default chemical potentials
    'H+': 0.0,           # for water, protons and electrons
    'e-': 0.0,
    'H2O': -2.4583
}

U_STD_AGCL = 0.222  # Standard redox potential of AgCl electrode
U_STD_SCE = 0.244   # Standard redox potential of SCE electrode


def parse_formula(formula: str, fmt: str = 'metal'):
    aq = formula.endswith('(aq)')
    charge = formula.count('+') - formula.count('-')
    formula_strip = formula.replace('(aq)', '').rstrip('+-')
    formula_obj = Formula(formula_strip, format=fmt)
    return formula_obj, charge, aq


def initialize_refs(refs_dct, reduce=False, fmt='metal'):
    """Convert dictionary entries to Species instances"""
    refs = {}
    for label, energy in refs_dct.items():
        spec = Species.from_string(label, energy, reduce, fmt)
        refs[label] = spec
    return refs


def get_product_combos(reactant, refs):
    """Obtain all possible combinations of products.

    Obtain - from the available references and based
    on the stoichiometry of the target material (reactant) -
    different combinations of products to be inserted
    in (electro)chemical reactions.
    """
    ref_elem = reactant._main_elements
    allcombos = [[] for _ in range(len(ref_elem))]
    for ref in refs.values():
        contained = ref.contains(ref_elem)
        for w in np.argwhere(contained).flatten():
            allcombos[w].append(ref)
    return [np.unique(combo) for combo in product(*allcombos)]


def get_phases(reactant, refs, T, conc, reference, tol=1e-3):
    """Obtain all the possible decomposition pathways
       for a given reactant as a collection of RedOx objects.
    """
    phases = []
    phase_matrix = []

    for products in get_product_combos(reactant, refs):
        phase = RedOx.from_species(
            reactant, products, T, conc, reference, tol
        )
        if phase is not None:
            phases.append(phase)
            phase_matrix.append(phase._vector)

    if len(phase_matrix) == 0:
        raise ValueError(
            "No valid decomposition pathways have been found" +
            " given this set of references."
        )

    return phases, np.array(phase_matrix).astype('float64')


def get_main_products(count):
    """Obtain the reaction products excluded protons,
       water and electrons.
    """
    return [spec for spec, coef in count.items()
            if coef > 0 and spec not in ['H+', 'H2O', 'e-']]


def format_label(count) -> str:
    """Obtain phase labels formatted in LaTeX math style."""
    formatted = []
    for prod in get_main_products(count):
        label = re.sub(r'(\S)([+-]+)', r'\1$^{\2}$', prod)
        label = re.sub(r'(\d+)', r'$_{\1}$', label)
        for symbol in ['+', '-']:
            count = label.count(symbol)
            if count > 1:
                label = label.replace(count * symbol, f'{count}{symbol}')
            if count == 1:
                label = label.replace(count * symbol, symbol)
        formatted.append(label)
    return ', '.join(f for f in formatted)


def make_coeff_nice(coeff, max_denom) -> str:
    """Convert a fraction into a string while limiting the denominator"""
    frac = abs(Fraction(coeff).limit_denominator(max_denom))
    if frac.numerator == frac.denominator:
        return ''
    return str(frac)


def add_numbers(ax, text) -> None:
    """Add number identifiers to the different domains of a Pourbaix diagram."""
    import matplotlib.patheffects as pfx
    for i, (x, y, _) in enumerate(text):
        txt = ax.text(
            y, x, f'{i}',
            fontsize=20,
            horizontalalignment='center'
        )
        txt.set_path_effects([pfx.withStroke(linewidth=2.0, foreground='w')])


def add_labels(ax, text) -> None:
    """Add phase labels to the different domains of a Pourbaix diagram."""
    import matplotlib.patheffects as pfx
    for i, (x, y, species) in enumerate(text):
        label = format_label(species)
        annotation = ax.annotate(
            label, xy=(y, x), color='w',
            fontsize=16, horizontalalignment='center'
        )
        annotation.set_path_effects(
            [pfx.withStroke(linewidth=2.0, foreground='k')]
        )
        annotation.draggable()
        ax.add_artist(annotation)


def wrap_text(text) -> str:
    import textwrap

    textlines = []
    for i, (_, _, species) in enumerate(text):
        label = format_label(species)
        textlines.append(
            textwrap.fill(
                f'({i})  {label}',
                width=40,
                subsequent_indent='      '
            )
        )
    return '\n'.join(textlines)


def add_phase_labels(fig, text, offset=0.0):
    """Add phase labels to the right of the diagram"""

    fig.text(
        0.75 + offset, 0.5,
        wrap_text(text),
        fontsize=16,
        va='center',
        ha='left')


def add_redox_lines(axes, pH, reference, color='k') -> None:
    """Add water redox potentials to a Pourbaix diagram"""
    const = -0.5 * PREDEF_ENERGIES['H2O']
    corr = {
        'SHE': 0,
        'RHE': 0,
        'Pt': 0,
        'AgCl': -U_STD_AGCL,
        'SCE': -U_STD_SCE,
    }
    kwargs = {
        'c': color,
        'ls': '--',
        'zorder': 2
    }
    if reference in ['SHE', 'AgCl', 'SCE']:
        slope = -59.2e-3
        axes.plot(pH, slope * pH + corr[reference], **kwargs)
        axes.plot(pH, slope * pH + const + corr[reference], **kwargs)
    elif reference in ['Pt', 'RHE']:
        axes.axhline(0 + corr[reference], **kwargs)
        axes.axhline(const + corr[reference], **kwargs)
    else:
        raise ValueError('The specified reference electrode doesnt exist')


@functools.total_ordering
class Species:
    """Class representing an individual chemical species,
       grouping relevant properties.

    Initialization
    --------------
    name: str
        A label representing the species

    formula: Formula

    charge: int
        the electric charge of the species, if ionic

    aq: bool
        whether the species is solid (False)
        or acqueous (True)

    energy: float
        the chemical potential of the species
    """
    def __init__(self,
                 name: str,
                 formula: Formula,
                 charge: int,
                 aq: bool,
                 energy: float):

        self.name = name
        self.formula = formula
        self.energy = energy
        self.charge = charge
        self.aq = aq

        self.count = formula.count()
        self.natoms = sum(self.count.values())
        self._main_elements = [
            e for e in self.count.keys() if e not in ['H', 'O']
        ]

    def __eq__(self, other):
        if not isinstance(other, Species):
            return NotImplemented
        return self.name == other.name

    def __lt__(self, other):
        if not isinstance(other, Species):
            return NotImplemented
        return self.name < other.name

    @classmethod
    def from_string(cls, string: str, energy: float,
                    reduce: bool = True, fmt: str = 'metal'):
        """Initialize the class provided a formula and an energy.

        string: str
            The chemical formula of the species (e.g. ``ZnO``).
            For solid species, the formula is automatically reduced to the
            unit formula, and the chemical potential normalized accordingly.
            Acqueous species are specified by expliciting
            all the positive or negative charges and by appending ``(aq)``.
            Parentheses for grouping functional groups are acceptable.
                e.g.
                    Be3(OH)3[3+]    ➜  wrong
                    Be3(OH)3+++     ➜  wrong
                    Be3(OH)3+++(aq) ➜  correct
                    Be3O3H3+++(aq)  ➜  correct

        energy: float
            the energy (chemical potential) associated with the species.

        reduce: bool
            reduce to the unit formula and normalize the energy accordingly.
            Formulae and energies of acqueous species are never reduced.

        fmt: str
            Formula formatting according to the available options in
            ase.formula.Formula
        """
        formula, charge, aq = parse_formula(string, fmt=fmt)
        if not aq:
            if reduce:
                formula, n_fu = formula.reduce()
                energy /= n_fu
            name = str(formula)
        else:
            name = string
        return cls(name, formula, charge, aq, energy)

    def get_chemsys(self):
        """Get the possible combinations of elements based
           on the stoichiometry. Useful for database queries.
        """
        elements = set(self.count.keys())
        elements.update(['H', 'O'])
        chemsys = list(
            chain.from_iterable(
                [combinations(elements, i + 1)
                 for i in range(len(elements))]
            )
        )
        return chemsys

    def balance_electrochemistry(self):
        """Obtain number of H2O, H+, e- "carried" by the species
           in electrochemical reactions.
        """
        n_H2O = -self.count.get('O', 0)
        n_H = -2 * n_H2O - self.count.get('H', 0)
        n_e = n_H + self.charge
        return n_H2O, n_H, n_e

    def _count_array(self, elements):
        return np.array([self.count.get(e, 0) for e in elements])

    def contains(self, elements):
        return [elem in self._main_elements for elem in elements]

    def get_fractional_composition(self, elements):
        """Obtain the fractional content of each element."""
        N_all = sum(self.count.values())
        N_elem = sum([self.count.get(e, 0) for e in elements])
        return N_elem / N_all

    def __repr__(self):
        return f'Species({self.name})'


class RedOx:
    def __init__(self, species, coeffs,
                 T=298.15, conc=1e-6,
                 reference='SHE'):
        """RedOx class representing an (electro)chemical reaction.

        Initialization
        --------------

        species: list[Species]
            The reactant and products excluded H2O, protons and electrons.

        coeffs: list[float]
            The stoichiometric coefficients of the above species.
            Positive coefficients are associated with the products,
            negative coefficients with the reactants.

        T: float
            The temperature in Kelvin. Default: 298.15 K.

        conc: float
            The concentration of ionic species. Default: 1.0e-6 M.

        reference: str
            The reference electrode. Default: SHE.
            available options: SHE, RHE, AgCl, Pt.


        Relevant methods
        ----------------

        from_species(reactant, products, T, conc, reference)
            Initialize the class from the reactant as a Species object
            and the product(s) a list of Species objects.

        equation()
            Print the chemical equation of the reaction.

        get_free_energy(U, pH):
            Obtain the reaction free energy at a given
            applied potential (U) and pH.

        """
        self.species = species
        self.coeffs = coeffs
        self.T = T
        self.conc = conc
        self.reference = reference
        self.count = Counter()

        alpha = CONST * T   # 0.059 eV @ T=298.15K
        const_term = 0
        pH_term = 0
        U_term = 0

        for spec, coef in zip(species, coeffs):
            self.count[spec.name] = coef
            amounts = spec.balance_electrochemistry()

            const_term += coef * (
                spec.energy + alpha * (spec.aq * np.log10(conc))
            )
            pH_term += - coef * alpha * amounts[1]
            U_term += - coef * amounts[2]

            for name, n in zip(['H2O', 'H+', 'e-'], amounts):
                const_term += coef * n * PREDEF_ENERGIES[name]
                self.count[name] += coef * n

        const_corr, pH_corr = self.get_ref_correction(reference, alpha)
        self._vector = [
            float(const_term + const_corr),
            float(U_term),
            float(pH_term + pH_corr)
        ]

    @classmethod
    def from_species(cls, reactant, products,
                     T: float = 298.15, conc: float = 1e-6,
                     reference: str = 'SHE', tol: float = 1e-3):
        """Initialize the class from a combination of
           reactant and products. The stoichiometric
           coefficients are automatically determined.

           reactant: Species
           products: list[Species]
        """
        reac_elem = reactant._main_elements
        reac_count = [-reactant._count_array(reac_elem)]
        prod_count = [p._count_array(reac_elem) for p in products]
        elem_matrix = np.array(reac_count + prod_count).T
        coeffs = null_space(elem_matrix).flatten()

        if len(coeffs) > 0 and all(coeffs > tol):
            coeffs /= coeffs[0]
            coeffs[0] = -coeffs[0]
            species = (reactant, *products)
            phase = cls(species, coeffs, T, conc, reference)
            return phase

        return None

    def get_ref_correction(self, reference: str,
                           alpha: float) -> Tuple[float, float]:
        """Correct the constant and pH contributions to the reaction free energy
           based on the reference electrode of choice and the temperature
           (alpha=k_B*T*ln(10))
        """
        n_e = self.count['e-']
        gibbs_corr = 0.0
        pH_corr = 0.0
        if reference in ['RHE', 'Pt']:
            pH_corr += n_e * alpha
            if reference == 'Pt' and n_e < 0:
                gibbs_corr += n_e * 0.5 * PREDEF_ENERGIES['H2O']
        if reference == 'AgCl':
            gibbs_corr -= n_e * U_STD_AGCL
        if reference == 'SCE':
            gibbs_corr -= n_e * U_STD_SCE

        return gibbs_corr, pH_corr

    def equation(self, max_denom: int = 50) -> str:
        """Print the chemical reaction."""

        reactants = []
        products = []
        for s, n in self.count.items():
            if abs(n) <= 1e-6:
                continue
            nice_coeff = make_coeff_nice(n, max_denom)
            substr = f'{nice_coeff}{s}'
            if n > 0:
                products.append(substr)
            else:
                reactants.append(substr)

        return "  ➜  ".join([" + ".join(reactants), " + ".join(products)])

    def get_free_energy(self, U: float, pH: float) -> float:
        """Evaluate the reaction free energy
           at a given applied potential U and pH"""
        return self._vector[0] + self._vector[1] * U + self._vector[2] * pH


class Pourbaix:
    """Pourbaix class for acqueous stability evaluations.

    Allows to determine the most stable phase in a given set
    of pH and potential conditions and to evaluate a complete diagram.

    Initialization
    --------------

    material_name: str
        The formula of the target material. It is preferrable
        to provide the reduced formula (e.g. RuO2 instad of Ru2O4).

    refs_dct: dict
        A dictionary containing the formulae of the target material
        and its competing phases (solid and/or ionic) as keys,
        and their formation energies as values.

    T: float
        Temperature in Kelvin. Default: 298.15 K.

    conc: float
        Concentration of the ionic species. Default: 1e-6 mol/L.

    reference: str
        The reference electrode. Default: SHE.
        available options: SHE, RHE, AgCl, Pt.


    Relevant methods
    ----------------

    get_pourbaix_energy(U, pH)
        obtain the energy of the target material
        relative to the most stable phase at a given potential U and pH.
        If negative, the target material can be regarded as stable.
    plot(...)
        plot a complete Pourbaix diagram in a given pH and potential window.


    Relevant attributes
    -------------------

    material: Species
        the target material as a Species object

    phases: list[RedOx]
        the available decomposition reactions of the target material
        into its competing phases as a list of RedOx objects.

    """
    def __init__(self,
                 material_name: str,
                 refs_dct: dict,
                 T: float = 298.15,
                 conc: float = 1.0e-6,
                 reference: str = 'SHE'):

        self.material_name = material_name
        self.refs = refs_dct
        self.T = T
        self.conc = conc
        self.reference = reference

        refs = initialize_refs(refs_dct)
        self.material = refs.pop(material_name)
        self.phases, phase_matrix = get_phases(
            self.material, refs, T, conc, reference
        )
        self._const = phase_matrix[:, 0]
        self._var = phase_matrix[:, 1:]

    def _decompose(self, U, pH):
        """Evaluate the reaction energy for decomposing
           the target material into each of the available products
           at a given pH and applied potential.
        """
        return self._const + np.dot(self._var, [U, pH])

    def _get_pourbaix_energy(self, U, pH):
        """Evaluate the Pourbaix energy"""
        energies = self._decompose(U, pH)
        i_min = np.argmin(energies)
        return -energies[i_min], i_min

    def get_pourbaix_energy(self, U, pH, verbose=False):
        """Evaluate the Pourbaix energy, print info about
        the most stable phase and the corresponding energy at
        a given potential U and pH.

        The Pourbaix energy represents the energy of the target material
        relative to the most stable competing phase. If negative,
        the target material can be considered as stable.
        """
        energy, index = self._get_pourbaix_energy(U, pH)
        phase = self.phases[index]
        if verbose:
            if energy <= 0.0:
                print(f'{self.material.name} is stable.')
            else:
                print(f'Stable phase: \n{phase.equation()}')
            print(f'Energy: {energy:.3f} eV')
        return energy, phase

    def get_equations(self, contains: Union[str, None] = None):
        """Print the chemical reactions of the available phases.

        the argument `contains' allows to filter for phases containing a
        particular species
            e.g. get_equations(contains='ZnO')
        """
        equations = []
        for i, p in enumerate(self.phases):
            if contains is not None and contains not in p.count:
                continue
            equations.append(f'{i}) {p.equation()}')
        return equations

    def diagram(self, U=None, pH=None):
        """Actual evaluation of the complete diagram

        Returns
        -------

        pour:
            The stability domains of the diagram on the pH vs. U grid.
            domains are represented by indexes (as integers)
            that map to Pourbaix.phases
            the target material is stable (index=-1)

        meta:
            The Pourbaix energy on the pH vs. U grid.

        text:
            The coordinates and phases information for
            text placement on the diagram.

        domains:
            ...

        """
        pour = np.zeros((len(U), len(pH)))
        meta = pour.copy()

        for i, u in enumerate(U):
            for j, p in enumerate(pH):
                meta[i, j], pour[i, j] = self._get_pourbaix_energy(u, p)

        # Identifying the region where the target material
        # is stable and updating the diagram accordingly
        where_stable = (meta <= 0)
        pour[where_stable] = -1

        text = []
        domains = [int(i) for i in np.unique(pour)]
        for phase_id in domains:
            if phase_id == -1:
                where = where_stable
                txt = {self.material.name: 1}
            else:
                where = (pour == phase_id)
                phase = self.phases[int(phase_id)]
                txt = phase.count
            x = np.dot(where.sum(1), U) / where.sum()
            y = np.dot(where.sum(0), pH) / where.sum()
            text.append((x, y, txt))

        return PourbaixDiagram(self, U, pH, pour, meta, text, domains)

    def get_phase_boundaries(self, phmin, phmax, umin, umax, domains, tol=1e-6):
        """Plane intersection method for finding
           the boundaries between phases seen in the final plot.

        Returns a list of tuples, each representing a single boundary,
        of the form ([[x1, x2], [y1, y2]], [id1, id2]), namely the
        x and y coordinates of the simplices connected by the boundary
        and the id's of the phases at each side of the boundary.
        """
        from collections import defaultdict
        from itertools import combinations

        # Planes identifying the diagram frame
        planes = [(np.array([0.0, 1.0, 0.0]), umin, 'bottom'),
                  (np.array([0.0, 1.0, 0.0]), umax, 'top'),
                  (np.array([1.0, 0.0, 0.0]), phmin, 'left'),
                  (np.array([1.0, 0.0, 0.0]), phmax, 'right')]

        # Planes associated with the stable domains of the diagram.
        # Given x=pH, y=U, z=E_pbx=-DeltaG, each plane has expression:
        # _vector[2]*x + _vector[1]*y + z = -_vector[0]
        # The region where the target material is stable
        # (id=-1, if present) is delimited by the xy plane (Epbx=0)
        for d in domains:
            if d == -1:
                plane = np.array([0.0, 0.0, 1.0])
                const = 0.0
            else:
                pvec = self.phases[d]._vector
                plane = np.array([pvec[2], pvec[1], 1])
                const = -pvec[0]
            planes.append((plane, const, d))

        # The simplices are found from the intersection points between
        # all possible plane triplets. If the z coordinate of the point
        # matches the corresponding pourbaix energy,
        # then the point is a simplex.
        simplices = []
        for (p1, c1, id1), (p2, c2, id2), (p3, c3, id3) in \
                combinations(planes, 3):
            A = np.vstack((p1, p2, p3))
            c = np.array([c1, c2, c3])
            ids = (id1, id2, id3)

            try:
                # triplets containing parallel planes raise a LinAlgError
                # and are automatically excluded.
                invA = np.linalg.inv(A)
            except np.linalg.LinAlgError:
                continue

            pt = np.dot(invA, c)
            Epbx = self._get_pourbaix_energy(pt[1], pt[0])[0]
            if pt[2] >= -tol and \
               np.isclose(Epbx, pt[2], rtol=0, atol=tol) and \
               phmin - tol <= pt[0] <= phmax + tol and \
               umin - tol <= pt[1] <= umax + tol:
                simplex = np.round(pt[:2], 3)
                simplices.append((simplex, ids))

        # The final segments to plot on the diagram are found from
        # the pairs of unique simplices that have two neighboring phases
        # in common, diagram frame excluded.
        duplicate_filter = defaultdict(int)

        def are_boundaries_there(comm):
            bounds = ['bottom', 'top', 'left', 'right']
            for c in comm:
                if c in bounds:
                    return True
            return False

        for (s1, id1), (s2, id2) in combinations(simplices, 2):

            # common neighboring phases, diagram frame excluded
            common = {*id1} & {*id2}

            simplices_are_distinct = not np.allclose(s1, s2, rtol=0, atol=tol)
            only_two_phases_in_common = (len(common) == 2)
            diagram_frame_excluded = not are_boundaries_there(common)

            # Filtering out duplicates
            if all([simplices_are_distinct, only_two_phases_in_common,
                    diagram_frame_excluded]):
                testarray = sorted([s1, s2], key=lambda s: (s[0], s[1]))
                testarray.append(sorted(common))
                testarray = np.array(testarray).flatten()
                duplicate_filter[tuple(testarray)] += 1

        segments = []
        for segment in duplicate_filter:
            coords, phases = np.split(np.array(segment), [4])
            segments.append((
                coords.reshape(2, 2).T,
                list(phases.astype(int))
            ))
        return segments


@dataclass
class PourbaixDiagram:
    pbx: Pourbaix
    U: np.ndarray
    pH: np.ndarray
    pour: np.ndarray
    meta: np.ndarray
    text: List[Tuple[float, float, str]]
    domains: List[int]

    def __post_init__(self):
        def _issorted(array):
            return all(np.diff(array) > 0)

        # We might as well require the input domains to be sorted:
        if not _issorted(self.U):
            raise ValueError('U must be sorted')

        if not _issorted(self.pH):
            raise ValueError('pH must be sorted')

    @property
    def pHrange(self):
        return (self.pH[0], self.pH[-1])

    @property
    def Urange(self):
        return (self.U[0], self.U[-1])

    def _draw_diagram_axes(
            self,
            cap,
            normalize,
            include_text,
            include_water,
            labeltype, cmap, *,
            ax):
        """Backend for drawing Pourbaix diagrams."""

        meta = self.meta.copy()
        if normalize:
            meta /= self.pbx.material.natoms
            cbarlabel = r'$\Delta G_{pbx}$ (eV/atom)'
        else:
            cbarlabel = r'$\Delta G_{pbx}$ (eV)'

        fig = ax.get_figure()
        extent = [*self.pHrange, *self.Urange]

        fig.subplots_adjust(
            left=0.1, right=0.97,
            top=0.97, bottom=0.14
        )

        if isinstance(cap, list):
            vmin = cap[0]
            vmax = cap[1]
        else:
            vmin = -cap
            vmax = cap

        colorplot = ax.imshow(
            meta, cmap=cmap,
            extent=extent,
            vmin=vmin, vmax=vmax,
            origin='lower', aspect='auto',
            interpolation='gaussian'
        )

        cbar = fig.colorbar(
            colorplot,
            ax=ax,
            pad=0.02
        )

        bounds = self.pbx.get_phase_boundaries(
            *self.pHrange, *self.Urange, self.domains
        )
        for coords, _ in bounds:
            ax.plot(coords[0], coords[1], '-', c='k', lw=1.0)

        if labeltype == 'numbers':
            add_numbers(ax, self.text)
        elif labeltype == 'phases':
            add_labels(ax, self.text)
        elif labeltype is None:
            pass
        else:
            raise ValueError("The provided label type doesn't exist")

        if include_water:
            add_redox_lines(ax, self.pH, self.pbx.reference, 'w')

        ax.set_xlim(*self.pHrange)
        ax.set_ylim(*self.Urange)
        ax.set_xlabel('pH', fontsize=22)
        ax.set_ylabel(r'$\it{U}$' + f' vs. {self.pbx.reference} (V)',
                      fontsize=22)
        ax.set_xticks(np.arange(self.pHrange[0], self.pHrange[1] + 1, 2))
        ax.set_yticks(np.arange(self.Urange[0], self.Urange[1] + 1, 1))
        ax.xaxis.set_tick_params(width=1.5, length=5)
        ax.yaxis.set_tick_params(width=1.5, length=5)
        ax.tick_params(axis='both', labelsize=22)

        ticks = np.linspace(vmin, vmax, num=5)
        cbar.set_ticks(ticks)
        cbar.set_ticklabels(ticks)
        cbar.outline.set_linewidth(1.5)
        cbar.ax.tick_params(labelsize=20, width=1.5, length=5)
        cbar.ax.set_ylabel(cbarlabel, fontsize=20)

        for axis in ['top', 'bottom', 'left', 'right']:
            ax.spines[axis].set_linewidth(1.5)

        if include_text:
            fig.subplots_adjust(right=0.75)
            add_phase_labels(fig, self.text, offset=0.05)
            return ax, cbar

        fig.tight_layout()
        return cbar

    def plot(self,
             cap=1.0,
             # figsize=[12, 6],
             normalize=True,
             include_text=True,
             include_water=False,
             labeltype='numbers',
             cmap="RdYlGn_r",
             filename=None,
             ax=None,
             show=False):
        """Plot a complete Pourbaix diagram.

        Keyword arguments
        -----------------

        Urange: list
            The potential range onto which to draw the diagram.

        pHrange: list
            The pH range onto which to draw the diagram.

        npoints: int
            The resolution of the diagram. Higher values
            mean higher resolution and thus higher compute times.

        cap: float/list
            If float, the limit (in both the positive and negative direction)
            of the Pourbaix energy colormap.
            If list, the first and second value determine the colormap limits.

        figsize: list
            The horizontal and vertical size of the graph.

        normalize: bool
            Normalize energies by the number of
            atoms in the target material unit formula.

        include_text: bool
            Report to the right of the diagram the main products
            associated with the stability domains.

        include_water: bool
            Include in the diagram the stability domain of water.

        labeltype: str/None
            The labeling style of the diagram domains. Options:
                'numbers': just add numbers associated with the
                           different phases, the latter shown
                           on the right if include_text=True.
                'phases':  Write the main products directly on the diagram.
                           These labels can be dragged around if the placement
                           is unsatisfactory. Redundant if include_text=True.
                 None:     Don't draw any labels.

        filename: str/None
            If passed as a string, the figure will be saved with that name.

        show: bool
            Spawn a window showing the diagram.

        """
        import matplotlib.pyplot as plt

        if ax is None:
            ax = plt.gca()

        fig = ax.get_figure()

        self._draw_diagram_axes(
            cap,
            normalize,
            include_text,
            include_water,
            labeltype, cmap,
            ax=ax)

        if filename is not None:
            fig.savefig(filename)

        if show:
            plt.show()