Skip to content

polykin.thermo.acm¤

MolecularACM ¤

Abstract base class for activity coefficient models for molecular (i.e., non-polymeric) systems.

A molecular ACM is defined in terms of a molar excess Gibbs energy model,

\[ g^{E} = g^{E}(T, x_i) \]

typically evaluated at constant pressure. All other thermodynamic solution properties are derived from \(g^{E}\).

To implement a specific molecular ACM, subclasses must:

  • Implement the gE method.
  • Preferably override the gamma method for efficiency.
Source code in src/polykin/thermo/acm/base.py
 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
class MolecularACM(ACM):
    """Abstract base class for activity coefficient models for molecular
    (i.e., non-polymeric) systems.

    A molecular ACM is defined in terms of a molar excess Gibbs energy model,

    $$ g^{E} = g^{E}(T, x_i) $$

    typically evaluated at constant pressure. All other thermodynamic
    solution properties are derived from $g^{E}$.

    To implement a specific molecular ACM, subclasses must:

    * Implement the `gE` method.
    * Preferably override the `gamma` method for efficiency.
    """

    @abstractmethod
    def gE(self, T: Number, x: FloatVector) -> Number:
        r"""Calculate the molar excess Gibbs energy.

        $$ g^{E} \equiv g - g^{id} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        float
            Molar excess Gibbs energy [J/mol].
        """

    def gamma(self, T: float, x: FloatVector) -> FloatVector:
        r"""Calculate the activity coefficients based on mole fraction.

        $$ \ln \gamma_i = \frac{1}{RT}
           \left( \frac{\partial (n g^E)}{\partial n_i} \right)_{T,P,n_j} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        FloatVector (N)
            Activity coefficients of all components.
        """

        def GE(n: np.ndarray):
            """Total excess Gibbs energy."""
            nT = n.sum()
            return nT * self.gE(T, n / nT)

        dGEdn = jacobian_forward(GE, x)

        return exp(dGEdn / (R * T))

    def Dgmix(self, T: float, x: FloatVector) -> float:
        r"""Calculate the molar Gibbs energy of mixing.

        $$ \Delta_{mix} g = g^E + R T \sum_i {x_i \ln{x_i}} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        float
            Molar Gibbs energy of mixing [J/mol].
        """
        return self.gE(T, x) - T * self._Dsmix_ideal(T, x)

    def Dhmix(self, T: float, x: FloatVector) -> float:
        r"""Calculate the molar enthalpy of mixing.

        $$ \Delta_{mix} h = h^{E} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        float
            Molar enthalpy of mixing [J/mol].
        """
        return self.hE(T, x)

    def Dsmix(self, T: float, x: FloatVector) -> float:
        r"""Calculate the molar entropy of mixing.

        $$ \Delta_{mix} s = s^{E} - R \sum_i {x_i \ln{x_i}} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        float
            Molar entropy of mixing [J/(mol·K)].
        """
        return self.sE(T, x) + self._Dsmix_ideal(T, x)

    def hE(self, T: float, x: FloatVector) -> float:
        r"""Calculate the molar excess enthalpy.

        $$ h^{E} = g^{E} + T s^{E} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        float
            Molar excess enthalpy [J/mol].
        """
        return self.gE(T, x) + T * self.sE(T, x)

    def sE(self, T: float, x: FloatVector) -> float:
        r"""Calculate the molar excess entropy.

        $$ s^{E} = -\left(\frac{\partial g^{E}}{\partial T}\right)_{P,x_i} $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        float
            Molar excess entropy [J/(mol·K)].
        """
        return -1 * derivative_complex(lambda T_: self.gE(T_, x), T)[0]

    def activity(self, T: float, x: FloatVector) -> FloatVector:
        r"""Calculate the activities.

        $$ a_i = x_i \gamma_i $$

        Parameters
        ----------
        T : float
            Temperature [K].
        x : FloatVector (N)
            Mole fractions of all components [mol/mol].

        Returns
        -------
        FloatVector (N)
            Activities of all components.
        """
        return x * self.gamma(T, x)

Dgmix ¤

Dgmix(T: float, x: FloatVector) -> float

Calculate the molar Gibbs energy of mixing.

\[ \Delta_{mix} g = g^E + R T \sum_i {x_i \ln{x_i}} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
float

Molar Gibbs energy of mixing [J/mol].

Source code in src/polykin/thermo/acm/base.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def Dgmix(self, T: float, x: FloatVector) -> float:
    r"""Calculate the molar Gibbs energy of mixing.

    $$ \Delta_{mix} g = g^E + R T \sum_i {x_i \ln{x_i}} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    float
        Molar Gibbs energy of mixing [J/mol].
    """
    return self.gE(T, x) - T * self._Dsmix_ideal(T, x)

Dhmix ¤

Dhmix(T: float, x: FloatVector) -> float

Calculate the molar enthalpy of mixing.

\[ \Delta_{mix} h = h^{E} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
float

Molar enthalpy of mixing [J/mol].

Source code in src/polykin/thermo/acm/base.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def Dhmix(self, T: float, x: FloatVector) -> float:
    r"""Calculate the molar enthalpy of mixing.

    $$ \Delta_{mix} h = h^{E} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    float
        Molar enthalpy of mixing [J/mol].
    """
    return self.hE(T, x)

Dsmix ¤

Dsmix(T: float, x: FloatVector) -> float

Calculate the molar entropy of mixing.

\[ \Delta_{mix} s = s^{E} - R \sum_i {x_i \ln{x_i}} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
float

Molar entropy of mixing [J/(mol·K)].

Source code in src/polykin/thermo/acm/base.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def Dsmix(self, T: float, x: FloatVector) -> float:
    r"""Calculate the molar entropy of mixing.

    $$ \Delta_{mix} s = s^{E} - R \sum_i {x_i \ln{x_i}} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    float
        Molar entropy of mixing [J/(mol·K)].
    """
    return self.sE(T, x) + self._Dsmix_ideal(T, x)

N property ¤

N: int

Number of components.

activity ¤

activity(T: float, x: FloatVector) -> FloatVector

Calculate the activities.

\[ a_i = x_i \gamma_i \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
FloatVector(N)

Activities of all components.

Source code in src/polykin/thermo/acm/base.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def activity(self, T: float, x: FloatVector) -> FloatVector:
    r"""Calculate the activities.

    $$ a_i = x_i \gamma_i $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    FloatVector (N)
        Activities of all components.
    """
    return x * self.gamma(T, x)

gE abstractmethod ¤

gE(T: Number, x: FloatVector) -> Number

Calculate the molar excess Gibbs energy.

\[ g^{E} \equiv g - g^{id} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
float

Molar excess Gibbs energy [J/mol].

Source code in src/polykin/thermo/acm/base.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@abstractmethod
def gE(self, T: Number, x: FloatVector) -> Number:
    r"""Calculate the molar excess Gibbs energy.

    $$ g^{E} \equiv g - g^{id} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    float
        Molar excess Gibbs energy [J/mol].
    """

gamma ¤

gamma(T: float, x: FloatVector) -> FloatVector

Calculate the activity coefficients based on mole fraction.

\[ \ln \gamma_i = \frac{1}{RT} \left( \frac{\partial (n g^E)}{\partial n_i} \right)_{T,P,n_j} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
FloatVector(N)

Activity coefficients of all components.

Source code in src/polykin/thermo/acm/base.py
 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
def gamma(self, T: float, x: FloatVector) -> FloatVector:
    r"""Calculate the activity coefficients based on mole fraction.

    $$ \ln \gamma_i = \frac{1}{RT}
       \left( \frac{\partial (n g^E)}{\partial n_i} \right)_{T,P,n_j} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    FloatVector (N)
        Activity coefficients of all components.
    """

    def GE(n: np.ndarray):
        """Total excess Gibbs energy."""
        nT = n.sum()
        return nT * self.gE(T, n / nT)

    dGEdn = jacobian_forward(GE, x)

    return exp(dGEdn / (R * T))

hE ¤

hE(T: float, x: FloatVector) -> float

Calculate the molar excess enthalpy.

\[ h^{E} = g^{E} + T s^{E} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
float

Molar excess enthalpy [J/mol].

Source code in src/polykin/thermo/acm/base.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def hE(self, T: float, x: FloatVector) -> float:
    r"""Calculate the molar excess enthalpy.

    $$ h^{E} = g^{E} + T s^{E} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    float
        Molar excess enthalpy [J/mol].
    """
    return self.gE(T, x) + T * self.sE(T, x)

sE ¤

sE(T: float, x: FloatVector) -> float

Calculate the molar excess entropy.

\[ s^{E} = -\left(\frac{\partial g^{E}}{\partial T}\right)_{P,x_i} \]
PARAMETER DESCRIPTION
T

Temperature [K].

TYPE: float

x

Mole fractions of all components [mol/mol].

TYPE: FloatVector(N)

RETURNS DESCRIPTION
float

Molar excess entropy [J/(mol·K)].

Source code in src/polykin/thermo/acm/base.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def sE(self, T: float, x: FloatVector) -> float:
    r"""Calculate the molar excess entropy.

    $$ s^{E} = -\left(\frac{\partial g^{E}}{\partial T}\right)_{P,x_i} $$

    Parameters
    ----------
    T : float
        Temperature [K].
    x : FloatVector (N)
        Mole fractions of all components [mol/mol].

    Returns
    -------
    float
        Molar excess entropy [J/(mol·K)].
    """
    return -1 * derivative_complex(lambda T_: self.gE(T_, x), T)[0]