<commit_before>#!/usr/bin/env python

"""
Module which defines basic entries for each ion and oxide to compute a
Pourbaix diagram
"""

from __future__ import division

__author__ = "<NAME>"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.0"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"
__date__ = "December 10, 2012"

import re
import math

from pymatgen.core.periodic_table import Element
from pymatgen.core.structure import Composition
from pymatgen.serializers.json_coders import MSONable
from pymatgen.core.ion import Ion
from pymatgen.phasediagram.entries import PDEntry

PREFAC = 0.0591


class PourbaixEntry(MSONable):
    """
    An object encompassing all data relevant to an ion in a pourbaix diagram.
    Each bulk solid/ion has a free energy g of the form:
    g = g0_ref + 0.0591 log10(conc) - nO mu_H2O + (nH - 2nO) pH
        + phi (-nH + 2nO + q)
    """
    def __init__(self, entry, correction=0.0, entry_id=None):
        """
        Args:
            entry:
                An entry object
                (ComputedEntry/ComputedStructureEntry/PDEntry/IonEntry)
            energy:
                Energy of entry
        """
        if isinstance(entry, IonEntry):
            self._entry = entry
            self._conc = 1.0e-6
            self._phase_type = "Ion"
            self._charge = entry.composition.charge
        else:
            self._entry = entry
            self._conc = 1.0
            self._phase_type = "Solid"
            self._charge = 0.0
        self._npH = None
        self._nPhi = None
        self._nH2O = None
        self._nM = None
        self.uncorrected_energy = entry.energy
        self.correction = correction
        self._calc_coeff_terms()
        self._name = self._entry.composition.reduced_formula
        if self._phase_type == "Solid":
            self._name += "(s)"
        try:
            self.entry_id = entry.entry_id
        except AttributeError:
            self.entry_id = entry_id

    @property
    def _g0(self):
        return self.energy

    @property
    def energy(self):
        return self.uncorrected_energy + self.correction

    @property
    def name(self):
        """
        Returns the entry's name
        """
        return self._name

    def set_name(self, string):
        """
        Set name of entry
        args:
            string: Input string
        """
        self._name = string

    @property
    def npH(self):
        """
        Returns value of npH, the coefficient of pH
        """
        return self._npH

    @property
    def nH2O(self):
        """
        Returns coefficient of Mu_H2O
        """
        return self._nH2O

    @property
    def nPhi(self):
        """
        Returns nPhi, the coefficient of Phi
        """
        return self._nPhi

    @property
    def g0(self):
        """
        Return g0 for the entry
        Legacy function.
        """
        return self._g0

    @property
    def conc(self):
        """
        Return concentration of the entry. Returns 1 if solid.
        """
        return self._conc
    
    @property
    def conc_term(self):
        """
        Returns the concentration contribution to the free energy
        """
        return self.normalization_factor * PREFAC * math.log10(self._conc)

    @property
    def phase_type(self):
        """
        Returns whether the entry is a solid/ion
        """
        return self._phase_type

    def g0_add(self, term):
        """
        Add a correction term to g0
        args:
            term:
                Correction term to add to g0
        """
        self.correction += term

    def g0_replace(self, term):
        """
        Replace g0 by a different value
        args: 
            term: 
                New value for g0
        """
        self.uncorrected_energy = term
        self.correction = 0.0

    @property
    def to_dict(self):
        """
        Returns dict which contains Pourbaix Entry data.
        Note that the pH, voltage, H2O factors are always calculated when
        constructing a PourbaixEntry object.
        """
        d = {"@module": self.__class__.__module__,
             "@class": self.__class__.__name__}
        if isinstance(self._entry, "IonEntry"):
            d["entry type"] = "Ion"
        else:
            d["entry type"] = "Solid"
        d["entry"] = self._entry.to_dict
        d["pH factor"] = self._npH
        d["voltage factor"] = self._nPhi
        d["concentration"] = self._conc
        d["H2O factor"] = self._nH2O
        d["energy"] = self.energy
        d["correction"] = self.correction
        d["entry_id"] = self.entry_id
        return d

    @classmethod
    def from_dict(cls, d):<commit_msg>Fix bug in cleanup.<commit_after>#!/usr/bin/env python

"""
Module which defines basic entries for each ion and oxide to compute a
Pourbaix diagram
"""

from __future__ import division

__author__ = "<NAME>"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.0"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"
__date__ = "December 10, 2012"

import re
import math

from pymatgen.core.periodic_table import Element
from pymatgen.core.structure import Composition
from pymatgen.serializers.json_coders import MSONable
from pymatgen.core.ion import Ion
from pymatgen.phasediagram.entries import PDEntry

PREFAC = 0.0591


class PourbaixEntry(MSONable):
    """
    An object encompassing all data relevant to an ion in a pourbaix diagram.
    Each bulk solid/ion has a free energy g of the form:
    g = g0_ref + 0.0591 log10(conc) - nO mu_H2O + (nH - 2nO) pH
        + phi (-nH + 2nO + q)
    """
    def __init__(self, entry, correction=0.0, entry_id=None):
        """
        Args:
            entry:
                An entry object
                (ComputedEntry/ComputedStructureEntry/PDEntry/IonEntry)
            energy:
                Energy of entry
        """
        if isinstance(entry, IonEntry):
            self._entry = entry
            self._conc = 1.0e-6
            self._phase_type = "Ion"
            self._charge = entry.composition.charge
        else:
            self._entry = entry
            self._conc = 1.0
            self._phase_type = "Solid"
            self._charge = 0.0
        self._npH = None
        self._nPhi = None
        self._nH2O = None
        self._nM = None
        self.uncorrected_energy = entry.energy
        self.correction = correction
        self._calc_coeff_terms()
        self._name = self._entry.composition.reduced_formula
        if self._phase_type == "Solid":
            self._name += "(s)"
        try:
            self.entry_id = entry.entry_id
        except AttributeError:
            self.entry_id = entry_id

    @property
    def _g0(self):
        return self.energy

    @property
    def energy(self):
        return self.uncorrected_energy + self.correction

    @property
    def name(self):
        """
        Returns the entry's name
        """
        return self._name

    def set_name(self, string):
        """
        Set name of entry
        args:
            string: Input string
        """
        self._name = string

    @property
    def npH(self):
        """
        Returns value of npH, the coefficient of pH
        """
        return self._npH

    @property
    def nH2O(self):
        """
        Returns coefficient of Mu_H2O
        """
        return self._nH2O

    @property
    def nPhi(self):
        """
        Returns nPhi, the coefficient of Phi
        """
        return self._nPhi

    @property
    def g0(self):
        """
        Return g0 for the entry
        Legacy function.
        """
        return self._g0

    @property
    def conc(self):
        """
        Return concentration of the entry. Returns 1 if solid.
        """
        return self._conc
    
    @property
    def conc_term(self):
        """
        Returns the concentration contribution to the free energy
        """
        return self.normalization_factor * PREFAC * math.log10(self._conc)

    @property
    def phase_type(self):
        """
        Returns whether the entry is a solid/ion
        """
        return self._phase_type

    def g0_add(self, term):
        """
        Add a correction term to g0
        args:
            term:
                Correction term to add to g0
        """
        self.correction += term

    def g0_replace(self, term):
        """
        Replace g0 by a different value
        args: 
            term: 
                New value for g0
        """
        self.uncorrected_energy = term
        self.correction = 0.0

    @property
    def to_dict(self):
        """
        Returns dict which contains Pourbaix Entry data.
        Note that the pH, voltage, H2O factors are always calculated when
        constructing a PourbaixEntry object.
        """
        d = {"@module": self.__class__.__module__,
             "@class": self.__class__.__name__}
        if isinstance(self._entry, IonEntry):
            d["entry type"] = "Ion"
        else:
            d["entry type"] = "Solid"
        d["entry"] = self._entry.to_dict
        d["pH factor"] = self._npH
        d["voltage factor"] = self._nPhi
        d["concentration"] = self._conc
        d["H2O factor"] = self._nH2O
        d["energy"] = self.energy
        d["correction"] = self.correction
        d["entry_id"] = self.entry_id
        return d

    @classmethod
    def from_dict(cls, d):
{'fraction_non_alphanumeric': 0.09315269901281244, 'fraction_numerical': 0.012392354547364, 'mean_word_length': 1.770730288041897, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7302185', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2736, 'n_words': 782}