overreact/thermo/__init__.py
def equilibrium_constant(
    delta_freeenergy: Union[float, np.ndarray],
    delta_moles: Optional[Union[int, np.ndarray]] = None,
    temperature: Union[float, np.ndarray] = 298.15,
    pressure: float = constants.atm,
    volume: Optional[float] = None,
):
    r"""Calculate an equilibrium constant from a reaction [Gibbs free energy](https://en.wikipedia.org/wiki/Gibbs_free_energy).

    This function uses the usual `relationship between reaction Gibbs energy
    and equilibrium constant
    <https://en.wikipedia.org/wiki/Equilibrium_constant>`_:

    .. math::
        K(T) = \exp\left(-\frac{\Delta_\text{r} G^\circ}{R T}\right)

    If `delta_moles` is given, the above will be multiplied by a term
    :math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
    transforms a :math:`K_p` equilibrium constant into a :math:`K_c`
    (see below).

    Parameters
    ----------
    delta_freeenergy : array-like
        Delta Gibbs reaction free energies. **This assumes values were already
        corrected for a one molar reference state.**
    delta_moles : array-like, optional
        Difference in moles between products and reactants. If set, this
        multiplies the end result by
        :math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
        calculates a solution equilibrium constant for gas phase data. You
        should set this to `None` if your free energies were already adjusted
        for solution Gibbs free energies.
    temperature : array-like, optional
        Absolute temperature in Kelvin.
    pressure : array-like, optional
        Reference gas pressure.
    volume : float, optional
        Molar volume. This substitutes :math:`\frac{R T}{p}` if given. See
        `delta_moles`.

    Returns
    -------
    K : array-like
        Equilibrium constant.

    Notes
    -----
    If you want to calculate a solution equilibrium constant from gas phase data, set
    `delta_moles` to the difference in moles between products and reactants.
    Alternatively, convert your energies to solution Gibbs free energies and
    set `delta_moles` to `None`.

    Examples
    --------
    The following is an example from the
    [LibreTexts Chemistry Library](https://chem.libretexts.org/Bookshelves/Physical_and_Theoretical_Chemistry_Textbook_Maps/Supplemental_Modules_(Physical_and_Theoretical_Chemistry)/Equilibria/Chemical_Equilibria/The_Equilibrium_Constant).
    Consider the following equilibrium:

    .. math::
        \ce{2 SO2(g) + O2(g) <=> 2 SO3(g)}

    with concentrations :math:`c_{\ce{SO2(g)}} = 0.2 M`,
    :math:`c_{\ce{O2(g)}} = 0.5 M` and :math:`c_{\ce{SO3(g)}} = 0.7 M` (room
    temperature). Its :math:`K_c` is given by:

    >>> Kc = 0.7**2 / (0.2**2 * 0.5)
    >>> Kc
    24.5

    You could use `equilibrium_constant` to reach the same result using the
    Gibbs reaction free energy (which can easily be obtained from
    :math:`K_c`):

    >>> temperature = 298.15
    >>> dG = -constants.R * temperature * np.log(Kc)
    >>> equilibrium_constant(dG)
    array([24.5])

    By giving a `delta_moles` value (in this case, :math:`2 - 2 - 1 = -1`),
    we can calculate the corresponding `K_p`:

    >>> equilibrium_constant(dG, delta_moles=-1)
    array([1.002])

    (As expected, it makes sense for gases to favor the most entropic side of
    the equilibrium.) The example above clearly used "solution-based" data
    (our :math:`K_c` was calculated using molar quantities, which means
    reference volumes of one liter). You could convert it to gas phase data
    to get the same result, by changing the reference state (in this case,
    from one molar to one atmosphere using `change_reference_state`):

    >>> dG += temperature * rx.change_reference_state()
    >>> equilibrium_constant(dG)
    array([1.002])

    Having gas phase information, the inverse path can be taken just by
    inverting the sign of `delta_moles`:

    >>> equilibrium_constant(dG, delta_moles=1)
    array([24.5])

    The following example is from
    [Wikipedia](https://en.wikipedia.org/wiki/Stability_constants_of_complexes#The_chelate_effect).
    The reactions are two copper complex forming equilibria with two
    different ligands. Since this reaction takes place in solution, it is the
    solution standard Gibbs reaction free energy that is given:

    >>> dG1 = -37.4e3
    >>> np.log10(equilibrium_constant(dG1))
    array([6.55])
    >>> dG2 = -60.67e3
    >>> np.log10(equilibrium_constant(dG2))
    array([10.62])

    The above are thus :math:`\log_{10}(K_c)`. Since we are talking about a
    mono- and a bidendate ligands, the `delta_moles` are -4 and -2,
    respectively, and we could obtain the :math:`\log_{10}(K_p)` the following
    way:

    >>> np.log10(equilibrium_constant(dG1, delta_moles=-4))
    array([0.998])
    >>> np.log10(equilibrium_constant(dG2, delta_moles=-2))
    array([7.85])

    You can easily check that the above values match the values given
    [here](https://en.wikipedia.org/wiki/Stability_constants_of_complexes#The_chelate_effect).
    """
    temperature = np.asarray(temperature)

    equilibrium_constant = np.exp(
        -np.atleast_1d(delta_freeenergy) / (constants.R * temperature)
    )

    if delta_moles is not None:
        if volume is None:
            volume = molar_volume(temperature, pressure) * constants.kilo

        equilibrium_constant *= volume ** delta_moles

    logger.info(f"equilibrium constant = {equilibrium_constant}")
    return equilibrium_constant
r"""Calculate an equilibrium constant from a reaction [Gibbs free energy](https://en.wikipedia.org/wiki/Gibbs_free_energy).

    This function uses the usual `relationship between reaction Gibbs energy
    and equilibrium constant
    <https://en.wikipedia.org/wiki/Equilibrium_constant>`_:

    .. math::
        K(T) = \exp\left(-\frac{\Delta_\text{r} G^\circ}{R T}\right)

    If `delta_moles` is given, the above will be multiplied by a term
    :math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
    transforms a :math:`K_p` equilibrium constant into a :math:`K_c`
    (see below).

    Parameters
    ----------
    delta_freeenergy : array-like
        Delta Gibbs reaction free energies. **This assumes values were already
        corrected for a one molar reference state.**
    delta_moles : array-like, optional
        Difference in moles between products and reactants. If set, this
        multiplies the end result by
        :math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
        calculates a solution equilibrium constant for gas phase data. You
        should set this to `None` if your free energies were already adjusted
        for solution Gibbs free energies.
    temperature : array-like, optional
        Absolute temperature in Kelvin.
    pressure : array-like, optional
        Reference gas pressure.
    volume : float, optional
        Molar volume. This substitutes :math:`\frac{R T}{p}` if given. See
        `delta_moles`.

    Returns
    -------
    K : array-like
        Equilibrium constant.

    Notes
    -----
    If you want to calculate a solution equilibrium constant from gas phase data, set
    `delta_moles` to the difference in moles between products and reactants.
    Alternatively, convert your energies to solution Gibbs free energies and
    set `delta_moles` to `None`.

    Examples
    --------
    The following is an example from the
    [LibreTexts Chemistry Library](https://chem.libretexts.org/Bookshelves/Physical_and_Theoretical_Chemistry_Textbook_Maps/Supplemental_Modules_(Physical_and_Theoretical_Chemistry)/Equilibria/Chemical_Equilibria/The_Equilibrium_Constant).
    Consider the following equilibrium:

    .. math::
        \ce{2 SO2(g) + O2(g) <=> 2 SO3(g)}

    with concentrations :math:`c_{\ce{SO2(g)}} = 0.2 M`,
    :math:`c_{\ce{O2(g)}} = 0.5 M` and :math:`c_{\ce{SO3(g)}} = 0.7 M` (room
    temperature). Its :math:`K_c` is given by:

    >>> Kc = 0.7**2 / (0.2**2 * 0.5)
    >>> Kc
    24.5

    You could use `equilibrium_constant` to reach the same result using the
    Gibbs reaction free energy (which can easily be obtained from
    :math:`K_c`):

    >>> temperature = 298.15
    >>> dG = -constants.R * temperature * np.log(Kc)
    >>> equilibrium_constant(dG)
    array([24.5])

    By giving a `delta_moles` value (in this case, :math:`2 - 2 - 1 = -1`),
    we can calculate the corresponding `K_p`:

    >>> equilibrium_constant(dG, delta_moles=-1)
    array([1.002])

    (As expected, it makes sense for gases to favor the most entropic side of
    the equilibrium.) The example above clearly used "solution-based" data
    (our :math:`K_c` was calculated using molar quantities, which means
    reference volumes of one liter). You could convert it to gas phase data
    to get the same result, by changing the reference state (in this case,
    from one molar to one atmosphere using `change_reference_state`):

    >>> dG += temperature * rx.change_reference_state()
    >>> equilibrium_constant(dG)
    array([1.002])

    Having gas phase information, the inverse path can be taken just by
    inverting the sign of `delta_moles`:

    >>> equilibrium_constant(dG, delta_moles=1)
    array([24.5])

    The following example is from
    [Wikipedia](https://en.wikipedia.org/wiki/Stability_constants_of_complexes#The_chelate_effect).
    The reactions are two copper complex forming equilibria with two
    different ligands. Since this reaction takes place in solution, it is the
    solution standard Gibbs reaction free energy that is given:

    >>> dG1 = -37.4e3
    >>> np.log10(equilibrium_constant(dG1))
    array([6.55])
    >>> dG2 = -60.67e3
    >>> np.log10(equilibrium_constant(dG2))
    array([10.62])

    The above are thus :math:`\log_{10}(K_c)`. Since we are talking about a
    mono- and a bidendate ligands, the `delta_moles` are -4 and -2,
    respectively, and we could obtain the :math:`\log_{10}(K_p)` the following
    way:

    >>> np.log10(equilibrium_constant(dG1, delta_moles=-4))
    array([0.998])
    >>> np.log10(equilibrium_constant(dG2, delta_moles=-2))
    array([7.85])

    You can easily check that the above values match the values given
    [here](https://en.wikipedia.org/wiki/Stability_constants_of_complexes#The_chelate_effect).
    
r"""Calculate an equilibrium constant from a reaction [Gibbs free energy].
This function uses the usual `relationship between reaction Gibbs energy
and equilibrium constant
`_.



If `delta_moles` is given, the above will be multiplied by a term
r " " " Calculate an equilibrium constant from a reaction [ Gibbs free energy ] . This function uses the usual ` relationship between reaction Gibbs energy and equilibrium constant ` _ . If ` delta_moles ` is given the above will be multiplied by a term
def equilibrium_constant(
    delta_freeenergy: Union[float, np.ndarray],
    delta_moles: Optional[Union[int, np.ndarray]] = None,
    temperature: Union[float, np.ndarray] = 298.15,
    pressure: float = constants.atm,
    volume: Optional[float] = None,
):
    temperature = np.asarray(temperature)
    equilibrium_constant = np.exp(
        -np.atleast_1d(delta_freeenergy) / (constants.R * temperature)
    )
    if delta_moles is not None:
        if volume is None:
            volume = molar_volume(temperature, pressure) * constants.kilo
        equilibrium_constant *= volume ** delta_moles
    logger.info(f"equilibrium constant = {equilibrium_constant}")
    return equilibrium_constant
def equilibrium_constant ( delta_freeenergy : Union [ float , np . ndarray ] , delta_moles : Optional [ Union [ int , np . ndarray ] ] = None , temperature : Union [ float , np . ndarray ] = 298.15 , pressure : float = constants . atm , volume : Optional [ float ] = None , ) : temperature = np . asarray ( temperature ) equilibrium_constant = np . exp ( - np . atleast_1d ( delta_freeenergy ) / ( constants . R * temperature ) ) if delta_moles is not None : if volume is None : volume = molar_volume ( temperature , pressure ) * constants . kilo equilibrium_constant *= volume ** delta_moles logger . info ( f"equilibrium constant = {equilibrium_constant}" ) return equilibrium_constant
r"""Calculate an equilibrium constant from a reaction [Gibbs free energy](https://en.wikipedia.org/wiki/Gibbs_free_energy).
r " " " Calculate an equilibrium constant from a reaction [ Gibbs free energy ] ( https : // en . wikipedia . org / wiki / Gibbs_free_energy ) .
r"""Calculate an equilibrium constant from a reaction [Gibbs free energy](https://en.wikipedia.org/wiki/Gibbs_free_energy).

    This function uses the usual `relationship between reaction Gibbs energy
    and equilibrium constant
    <https://en.wikipedia.org/wiki/Equilibrium_constant>`_:

    .. math::
        K(T) = \exp\left(-\frac{\Delta_\text{r} G^\circ}{R T}\right)

    If `delta_moles` is given, the above will be multiplied by a term
    :math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
    transforms a :math:`K_p` equilibrium constant into a :math:`K_c`
    (see below).

    Parameters
    ----------
    delta_freeenergy : array-like
        Delta Gibbs reaction free energies. **This assumes values were already
        corrected for a one molar reference state.**
    delta_moles : array-like, optional
        Difference in moles between products and reactants. If set, this
        multiplies the end result by
        :math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
        calculates a solution equilibrium constant for gas phase data. You
        should set this to `None` if your free energies were already adjusted
        for solution Gibbs free energies.
    temperature : array-like, optional
        Absolute temperature in Kelvin.
    pressure : array-like, optional
        Reference gas pressure.
    volume : float, optional
        Molar volume. This substitutes :math:`\frac{R T}{p}` if given. See
        `delta_moles`.

    Returns
    -------
    K : array-like
        Equilibrium constant.

    Notes
    -----
    If you want to calculate a solution equilibrium constant from gas phase data, set
    `delta_moles` to the difference in moles between products and reactants.
    Alternatively, convert your energies to solution Gibbs free energies and
    set `delta_moles` to `None`.

    Examples
    --------
    The following is an example from the
    [LibreTexts Chemistry Library](https://chem.libretexts.org/Bookshelves/Physical_and_Theoretical_Chemistry_Textbook_Maps/Supplemental_Modules_(Physical_and_Theoretical_Chemistry)/Equilibria/Chemical_Equilibria/The_Equilibrium_Constant).
    Consider the following equilibrium:

    .. math::
        \ce{2 SO2(g) + O2(g) <=> 2 SO3(g)}

    with concentrations :math:`c_{\ce{SO2(g)}} = 0.2 M`,
    :math:`c_{\ce{O2(g)}} = 0.5 M` and :math:`c_{\ce{SO3(g)}} = 0.7 M` (room
    temperature). Its :math:`K_c` is given by:

    >>> Kc = 0.7**2 / (0.2**2 * 0.5)
    >>> Kc
    24.5

    You could use `equilibrium_constant` to reach the same result using the
    Gibbs reaction free energy (which can easily be obtained from
    :math:`K_c`):

    >>> temperature = 298.15
    >>> dG = -constants.R * temperature * np.log(Kc)
    >>> equilibrium_constant(dG)
    array([24.5])

    By giving a `delta_moles` value (in this case, :math:`2 - 2 - 1 = -1`),
    we can calculate the corresponding `K_p`:

    >>> equilibrium_constant(dG, delta_moles=-1)
    array([1.002])

    (As expected, it makes sense for gases to favor the most entropic side of
    the equilibrium.) The example above clearly used "solution-based" data
    (our :math:`K_c` was calculated using molar quantities, which means
    reference volumes of one liter). You could convert it to gas phase data
    to get the same result, by changing the reference state (in this case,
    from one molar to one atmosphere using `change_reference_state`):

    >>> dG += temperature * rx.change_reference_state()
    >>> equilibrium_constant(dG)
    array([1.002])

    Having gas phase information, the inverse path can be taken just by
    inverting the sign of `delta_moles`:

    >>> equilibrium_constant(dG, delta_moles=1)
    array([24.5])

    The following example is from
    [Wikipedia](https://en.wikipedia.org/wiki/Stability_constants_of_complexes#The_chelate_effect).
    The reactions are two copper complex forming equilibria with two
    different ligands. Since this reaction takes place in solution, it is the
    solution standard Gibbs reaction free energy that is given:

    >>> dG1 = -37.4e3
    >>> np.log10(equilibrium_constant(dG1))
    array([6.55])
    >>> dG2 = -60.67e3
    >>> np.log10(equilibrium_constant(dG2))
    array([10.62])

    The above are thus :math:`\log_{10}(K_c)`. Since we are talking about a
    mono- and a bidendate ligands, the `delta_moles` are -4 and -2,
    respectively, and we could obtain the :math:`\log_{10}(K_p)` the following
    way:

    >>> np.log10(equilibrium_constant(dG1, delta_moles=-4))
    array([0.998])
    >>> np.log10(equilibrium_constant(dG2, delta_moles=-2))
    array([7.85])

    You can easily check that the above values match the values given
    [here](https://en.wikipedia.org/wiki/Stability_constants_of_complexes#The_chelate_effect).
    """
delta_freeenergy
Union[float, np.ndarray]
delta_moles
Optional[Union[int, np.ndarray]]
temperature
Union[float, np.ndarray]
pressure
float
volume
Optional[float]
math
`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
transforms a :math:`K_p` equilibrium constant into a :math:`K_c`
.

Parameters

delta_freeenergy : array-like
Delta Gibbs reaction free energies. **This assumes values were already
corrected for a one molar reference state.
delta_moles : array-like, optional
Difference in moles between products and reactants. If set, this
multiplies the end result by
:math:`\left( \frac{R T}{p} \right)^{-\Delta n}`, which effectively
calculates a solution equilibrium constant for gas phase data. You
should set this to `None` if your free energies were already adjusted
for solution Gibbs free energies.
temperature : array-like, optional
Absolute temperature in Kelvin.
pressure : array-like, optional
Reference gas pressure.
volume : float, optional
Molar volume. This substitutes :math:`\frac{R T}{p}` if given. See
`delta_moles`.

Returns

K : array-like
Equilibrium constant.

Notes

If you want to calculate a solution equilibrium constant from gas phase data, set
`delta_moles` to the difference in moles between products and reactants.
Alternatively, convert your energies to solution Gibbs free energies and
set `delta_moles` to `None`.

Examples

The following is an example from the
[LibreTexts Chemistry Library]/Equilibria/Chemical_Equilibria/The_Equilibrium_Constant).
Consider the following equilibrium.




` \ left ( \ frac { R T } { p } \ right ) ^ { - \ Delta n } ` which effectively transforms a : math : ` K_p ` equilibrium constant into a : math : ` K_c ` . Parameters delta_freeenergy : array - like Delta Gibbs reaction free energies . ** This assumes values were already corrected for a one molar reference state . delta_moles : array - like optional Difference in moles between products and reactants . If set this multiplies the end result by : math : ` \ left ( \ frac { R T } { p } \ right ) ^ { - \ Delta n } ` which effectively calculates a solution equilibrium constant for gas phase data . You should set this to ` None ` if your free energies were already adjusted for solution Gibbs free energies . temperature : array - like optional Absolute temperature in Kelvin . pressure : array - like optional Reference gas pressure . volume : float optional Molar volume . This substitutes : math : ` \ frac { R T } { p } ` if given . See ` delta_moles ` . Returns K : array - like Equilibrium constant . Notes If you want to calculate a solution equilibrium constant from gas phase data set ` delta_moles ` to the difference in moles between products and reactants . Alternatively convert your energies to solution Gibbs free energies and set ` delta_moles ` to ` None ` . Examples The following is an example from the [ LibreTexts Chemistry Library ] / Equilibria / Chemical_Equilibria / The_Equilibrium_Constant ) . Consider the following equilibrium .
math




You could use `equilibrium_constant` to reach the same result using the
Gibbs reaction free energy (which can easily be obtained from
You could use ` equilibrium_constant ` to reach the same result using the Gibbs reaction free energy ( which can easily be obtained from
math


By giving a `delta_moles` value (in this case, :math:`2 - 2 - 1 = -1`),
we can calculate the corresponding `K_p`.



(As expected, it makes sense for gases to favor the most entropic side of
the equilibrium.) The example above clearly used "solution-based" data
(our :math:`K_c` was calculated using molar quantities, which means
reference volumes of one liter). You could convert it to gas phase data
to get the same result, by changing the reference state (in this case,
from one molar to one atmosphere using `change_reference_state`).

>>> dG += temperature * rx.change_reference_state()
>>> equilibrium_constant(dG)
array([1.002])

Having gas phase information, the inverse path can be taken just by
inverting the sign of `delta_moles`.

>>> equilibrium_constant(dG, delta_moles=1)
array([24.5])

The following example is from
[Wikipedia].
The reactions are two copper complex forming equilibria with two
different ligands. Since this reaction takes place in solution, it is the
solution standard Gibbs reaction free energy that is given.







You can easily check that the above values match the values given
[here].
By giving a ` delta_moles ` value ( in this case : math : ` 2 - 2 - 1 = - 1 ` ) we can calculate the corresponding ` K_p ` . ( As expected it makes sense for gases to favor the most entropic side of the equilibrium . ) The example above clearly used " solution - based " data ( our : math : ` K_c ` was calculated using molar quantities which means reference volumes of one liter ) . You could convert it to gas phase data to get the same result by changing the reference state ( in this case from one molar to one atmosphere using ` change_reference_state ` ) . >>> dG + = temperature * rx . change_reference_state () >>> equilibrium_constant ( dG ) array ( [ 1 . 002 ] ) Having gas phase information the inverse path can be taken just by inverting the sign of ` delta_moles ` . >>> equilibrium_constant ( dG delta_moles = 1 ) array ( [ 24 . 5 ] ) The following example is from [ Wikipedia ] . The reactions are two copper complex forming equilibria with two different ligands . Since this reaction takes place in solution it is the solution standard Gibbs reaction free energy that is given . You can easily check that the above values match the values given [ here ] .