Skip to content

Commit 69517fb

Browse files
committed
fix issue with EOS-activity coeff vs pressure
1 parent cf10f21 commit 69517fb

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

thermosteam/equilibrium/activity_coefficients.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def fill_group_psis(group_psis, psis, group_mask):
131131
@njit(cache=True)
132132
def gamma_UNIFAC(x, T, interactions,
133133
group_psis, group_mask, qs, rs, Qs,
134-
chemgroups, chem_Qfractions, index):
134+
chemgroups, chem_Qfractions, index, *, P=None):
135135
N_chemicals = index.size
136136
gamma = np.ones(x.size)
137137
if N_chemicals > 1:
@@ -157,7 +157,7 @@ def gamma_UNIFAC(x, T, interactions,
157157
@njit(cache=True)
158158
def gamma_modified_UNIFAC(x, T, interactions,
159159
group_psis, group_mask, qs, rs, Qs,
160-
chemgroups, chem_Qfractions, index):
160+
chemgroups, chem_Qfractions, index, *, P=None):
161161
N_chemicals = index.size
162162
gamma = np.ones(x.size)
163163
if N_chemicals > 1:
@@ -225,11 +225,12 @@ class IdealActivityCoefficients(ActivityCoefficients):
225225
226226
"""
227227
__slots__ = ()
228+
P_dependent = False
228229

229230
def __init__(self, chemicals):
230231
self._chemicals = tuple(chemicals)
231232

232-
def __call__(self, xs, T):
233+
def __call__(self, xs, T, P=101325):
233234
return np.ones(len(xs))
234235

235236

@@ -244,6 +245,7 @@ class GroupActivityCoefficients(ActivityCoefficients):
244245
chemicals : Iterable[:class:`~thermosteam.Chemical`]
245246
246247
"""
248+
P_dependent = False
247249
__slots__ = ('_rs', '_qs', '_Qs','_chemgroups',
248250
'_group_psis', '_chem_Qfractions',
249251
'_group_mask', '_interactions',
@@ -304,7 +306,7 @@ def args(self):
304306
self._chemgroups, self._chem_Qfractions,
305307
self._index)
306308

307-
def activity_coefficients(self, x, T):
309+
def activity_coefficients(self, x, T, P=101325):
308310
"""
309311
Return activity coefficients of chemicals with defined functional groups.
310312
@@ -324,7 +326,7 @@ def activity_coefficients(self, x, T):
324326
self._chem_Qfractions,
325327
self._group_psis)
326328

327-
def __call__(self, x, T):
329+
def __call__(self, x, T, P=101325):
328330
"""
329331
Return activity coefficients.
330332
@@ -485,6 +487,7 @@ class GCEOSActivityCoefficients(ActivityCoefficients):
485487
486488
"""
487489
__slots__ = ('_chemicals', '_eos')
490+
P_dependent = True
488491
EOS = None # type[GCEOSMIX] Subclasses must implement this attribute.
489492
chemsep_db = None # Optional[str] Name of chemsep data base for interaction parameters.
490493
cache = None # [dict] Subclasses must implement this attribute.
@@ -553,6 +556,7 @@ def __call__(self, x, T, P=101325):
553556
activity_coefficient_classes.append(cls)
554557
dct[clsname] = cls
555558

556-
dct['PRActivityCoefficients'].chemsep_db = 'ChemSep PR'
559+
for i in ('PR', 'PR78'):
560+
dct[i + 'ActivityCoefficients'].chemsep_db = 'ChemSep PR'
557561
__all__ = (*__all__, *clsnames, 'activity_coefficient_classes')
558562
del dct, clsnames

thermosteam/equilibrium/bubble_point.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _T_error(self, T, P, z_over_P, z_norm, y):
146146
Psats = np.array([i(T) for i in self.Psats], dtype=float)
147147
y_phi = (z_over_P
148148
* Psats
149-
* self.gamma(z_norm, T)
149+
* self.gamma(z_norm, T, P)
150150
* self.pcf(T, P, Psats))
151151
y[:] = solve_y(y_phi, self.phi, T, P, y)
152152
return 1. - y.sum()
@@ -157,6 +157,12 @@ def _P_error(self, P, T, z_Psat_gamma, Psats, y):
157157
y[:] = solve_y(y_phi, self.phi, T, P, y)
158158
return 1. - y.sum()
159159

160+
def _P_error_dep(self, P, T, z, Psats, z_Psats, y):
161+
if P <= 0: raise InfeasibleRegion('negative pressure')
162+
y_phi = z_Psats * self.gamma(z, T, P) * self.pcf(T, P, Psats) / P
163+
y[:] = solve_y(y_phi, self.phi, T, P, y)
164+
return 1. - y.sum()
165+
160166
def _T_error_reactive(self, T, P, z, dz, y, x, liquid_conversion):
161167
if T <= 0: raise InfeasibleRegion('negative temperature')
162168
dz[:] = liquid_conversion(z, T, P, 'l')
@@ -165,7 +171,7 @@ def _T_error_reactive(self, T, P, z, dz, y, x, liquid_conversion):
165171
Psats = np.array([i(T) for i in self.Psats], dtype=float)
166172
y_phi = (x / P
167173
* Psats
168-
* self.gamma(x, T)
174+
* self.gamma(x, T, P)
169175
* self.pcf(T, P, Psats))
170176
y[:] = solve_y(y_phi, self.phi, T, P, y)
171177
return 1. - y.sum()
@@ -175,7 +181,7 @@ def _P_error_reactive(self, P, T, Psats, z, dz, y, x, liquid_conversion):
175181
dz[:] = liquid_conversion(z, T, P, 'l')
176182
x[:] = z + dz
177183
x /= x.sum()
178-
z_Psat_gamma = x * Psats * self.gamma(x, T)
184+
z_Psat_gamma = x * Psats * self.gamma(x, T, P)
179185
y_phi = z_Psat_gamma * self.pcf(T, P, Psats) / P
180186
y[:] = solve_y(y_phi, self.phi, T, P, y)
181187
return 1. - y.sum()
@@ -341,10 +347,16 @@ def solve_Py(self, z, T, liquid_conversion=None):
341347
elif T < self.Tmin: T = self.Tmin
342348
Psats = np.array([i(T) for i in self.Psats])
343349
z_norm = z / z.sum()
344-
z_Psat_gamma = z_norm * Psats * self.gamma(z_norm, T)
345-
f = self._P_error
346-
P_guess, y = self._Py_ideal(z_Psat_gamma)
347-
args = (T, z_Psat_gamma, Psats, y)
350+
if self.gamma.P_dependent:
351+
f = self._P_error_dep
352+
z_Psats = z_norm * Psats
353+
P_guess, y = self._Py_ideal(z_Psats)
354+
args = (T, z_norm, Psats, z_Psats, y)
355+
else:
356+
z_Psat_gamma = z_norm * Psats * self.gamma(z_norm, T, 101325)
357+
P_guess, y = self._Py_ideal(z_Psat_gamma)
358+
f = self._P_error
359+
args = (T, z_Psat_gamma, Psats, y)
348360
try:
349361
P = flx.aitken_secant(f, P_guess, P_guess-1, self.P_tol, 1e-9,
350362
args, checkiter=False, maxiter=self.maxiter)
@@ -362,7 +374,7 @@ def solve_Py(self, z, T, liquid_conversion=None):
362374
Psats = np.array([i(T) for i in self.Psats])
363375
x = z_norm.copy()
364376
dz = z_norm.copy()
365-
z_Psat_gamma = z * Psats * self.gamma(z_norm, T)
377+
z_Psat_gamma = z * Psats * self.gamma(z_norm, T, 101325)
366378
P_guess, y = self._Py_ideal(z_Psat_gamma)
367379
args = (T, Psats, z_norm, dz, y, x, liquid_conversion)
368380
try:

thermosteam/equilibrium/dew_point.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ def gamma_iter(gamma, x_gamma, T, P, f_gamma, gamma_args):
3030
mask = x < 1e-32
3131
x[mask] = 1e-32
3232
x = fn.normalize(x)
33-
return f_gamma(x, T, *gamma_args)
33+
return f_gamma(x, T, *gamma_args, P=P)
3434

3535
def solve_x(x_guess, x_gamma, T, P, f_gamma, gamma_args):
3636
mask = x_guess < 1e-32
3737
x_guess[mask] = 1e-32
3838
x_guess = fn.normalize(x_guess)
39-
gamma = f_gamma(x_guess, T, *gamma_args)
39+
gamma = f_gamma(x_guess, T, *gamma_args, P=P)
4040
args = (x_gamma, T, P, f_gamma, gamma_args)
4141
gamma = flx.wegstein(
4242
gamma_iter, gamma, 1e-12, args=args, checkiter=False,

thermosteam/equilibrium/fugacities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, chemicals, thermo=None):
5353

5454
def unweighted(self, x, T, P=101325.):
5555
Psats = np.array([i.Psat(T) for i in self.chemicals], dtype=float)
56-
return self.gamma(x, T) * self.pcf(T, P, Psats) * Psats
56+
return self.gamma(x, T, P) * self.pcf(T, P, Psats) * Psats
5757

5858
def __call__(self, x, T, P=101325., reduce=False):
5959
f_reduced = x * self.gamma(x, T)

thermosteam/equilibrium/vle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def xVlogK_iter(
8787
x = xVlogK[:n]
8888
Ks = np.exp(xVlogK[n+1:])
8989
x, y = xy(x, Ks)
90-
Ks[:] = pcf_Psat_over_P * f_gamma(x, T, *gamma_args) / f_phi(y, T, P)
90+
Ks[:] = pcf_Psat_over_P * f_gamma(x, T, *gamma_args, P=P) / f_phi(y, T, P)
9191
V = xVlogK[n]
9292
if V < 0.: V = 0.
9393
elif V > 1.: V = 1.

0 commit comments

Comments
 (0)