Skip to content

Commit

Permalink
improvements for PO-sim
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelcortes committed Aug 21, 2024
1 parent 498b00e commit 23fb77a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions thermosteam/_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ def _create_material_balance_equations(self):
def _create_energy_departure_equations(self):
return [i() for i in self.energy_equations]

def _update_energy_departure_coefficient(self, coefficients):
def _update_energy_departure_coefficient(self, coefficients, temperature_only):
source = self.source
if source is None: return
if not source._get_energy_departure_coefficient:
raise NotImplementedError(f'{source!r} has no method `_get_energy_departure_coefficient`')
coeff = source._get_energy_departure_coefficient(self)
coeff = source._get_energy_departure_coefficient(self, temperature_only)
if coeff is None: return
key, value = coeff
coefficients[key] = value
Expand Down
29 changes: 19 additions & 10 deletions thermosteam/equilibrium/bubble_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def solve_y(y_phi, phi, T, P, y_guess):
return flx.wegstein(y_iter, y_phi, 1e-9, args=(y_phi, phi, T, P),
checkiter=False,
checkconvergence=False,
convergenceiter=3)
convergenceiter=5,
maxiter=BubblePoint.maxiter)


# %% Bubble point values container
Expand Down Expand Up @@ -103,6 +104,7 @@ class BubblePoint:
__slots__ = ('chemicals', 'IDs', 'gamma', 'phi', 'pcf',
'Psats', 'Tmin', 'Tmax', 'Pmin', 'Pmax')
_cached = {}
maxiter = 50
T_tol = 1e-9
P_tol = 1e-3
def __new__(cls, chemicals=(), thermo=None):
Expand Down Expand Up @@ -184,7 +186,8 @@ def _Ty_ideal(self, z_over_P):
T = flx.IQ_interpolation(f, Tmin, Tmax, fmax, fmin,
None, self.T_tol, 5e-12, args,
checkiter=False,
checkbounds=False)
checkbounds=False,
maxiter=self.maxiter)
return T, y

def _Py_ideal(self, z_Psat_gamma_pcf):
Expand Down Expand Up @@ -253,13 +256,15 @@ def solve_Ty(self, z, P, liquid_conversion=None):
try:
T = flx.aitken_secant(f, T_guess, T_guess + 1e-3,
self.T_tol, 5e-12, args,
checkiter=False)
checkiter=False,
maxiter=self.maxiter)
except RuntimeError:
Tmin = self.Tmin; Tmax = self.Tmax
T = flx.IQ_interpolation(f, Tmin, Tmax,
f(Tmin, *args), f(Tmax, *args),
T_guess, self.T_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return T, fn.normalize(y)
else:
f = self._T_error_reactive
Expand All @@ -272,13 +277,14 @@ def solve_Ty(self, z, P, liquid_conversion=None):
try:
T = flx.aitken_secant(f, T_guess, T_guess + 1e-3,
self.T_tol, 5e-12, args,
checkiter=False)
checkiter=False, maxiter=self.maxiter)
except RuntimeError:
Tmin = self.Tmin; Tmax = self.Tmax
T = flx.IQ_interpolation(f, Tmin, Tmax,
f(Tmin, *args), f(Tmax, *args),
T_guess, self.T_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return T, dz, fn.normalize(y), x

def solve_Py(self, z, T, liquid_conversion=None):
Expand Down Expand Up @@ -330,13 +336,14 @@ def solve_Py(self, z, T, liquid_conversion=None):
args = (T, z_Psat_gamma, Psats, y)
try:
P = flx.aitken_secant(f, P_guess, P_guess-1, self.P_tol, 1e-9,
args, checkiter=False)
args, checkiter=False, maxiter=self.maxiter)
except RuntimeError:
Pmin = self.Pmin; Pmax = self.Pmax
P = flx.IQ_interpolation(f, Pmin, Pmax,
f(Pmin, *args), f(Pmax, *args),
P_guess, self.P_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return P, fn.normalize(y)
else:
f = self._P_error_reactive
Expand All @@ -349,13 +356,15 @@ def solve_Py(self, z, T, liquid_conversion=None):
args = (T, Psats, z_norm, dz, y, x, liquid_conversion)
try:
P = flx.aitken_secant(f, P_guess, P_guess-1, self.P_tol, 1e-9,
args, checkiter=False)
args, checkiter=False,
maxiter=self.maxiter)
except RuntimeError:
Pmin = self.Pmin; Pmax = self.Pmax
P = flx.IQ_interpolation(f, Pmin, Pmax,
f(Pmin, *args), f(Pmax, *args),
P_guess, self.P_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return P, dz, fn.normalize(y), x

def __repr__(self):
Expand Down
19 changes: 13 additions & 6 deletions thermosteam/equilibrium/dew_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ def x_iter(x, x_gamma, T, P, f_gamma, gamma_args):
# @njit(cache=True)
def solve_x(x_guess, x_gamma, T, P, f_gamma, gamma_args):
args = (x_gamma, T, P, f_gamma, gamma_args)
x = flx.wegstein(x_iter, x_guess, 1e-10, args=args, checkiter=False,
checkconvergence=False, convergenceiter=3)
x = flx.wegstein(
x_iter, x_guess, 1e-10, args=args, checkiter=False,
checkconvergence=False, convergenceiter=5, maxiter=DewPoint.maxiter
)
return x

# %% Dew point values container
Expand Down Expand Up @@ -112,6 +114,7 @@ class DewPoint:
__slots__ = ('chemicals', 'phi', 'gamma', 'IDs',
'pcf', 'Psats', 'Tmin', 'Tmax', 'Pmin', 'Pmax')
_cached = {}
maxiter = 50
T_tol = 1e-9
P_tol = 1e-3
def __new__(cls, chemicals=(), thermo=None):
Expand Down Expand Up @@ -197,7 +200,8 @@ def _Tx_ideal(self, zP):
if fmax < 0.: return Tmax, x
T = flx.IQ_interpolation(f, Tmin, Tmax, fmin, fmax,
None, self.T_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return T, x

def _Px_ideal(self, z_over_Psats):
Expand Down Expand Up @@ -266,14 +270,16 @@ def solve_Tx(self, z, P, gas_conversion=None):
try:
T = flx.aitken_secant(f, T_guess, T_guess + 1e-3,
self.T_tol, 5e-12, args,
maxiter=self.maxiter,
checkiter=False)
except RuntimeError:
Tmin = self.Tmin
Tmax = self.Tmax
T = flx.IQ_interpolation(f, Tmin, Tmax,
f(Tmin, *args), f(Tmax, *args),
T_guess, self.T_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return T, fn.normalize(x)
else:
f = self._T_error_reactive
Expand Down Expand Up @@ -342,14 +348,15 @@ def solve_Px(self, z, T, gas_conversion=None):
f = self._P_error
try:
P = flx.aitken_secant(f, P_guess, P_guess-10, self.P_tol, 5e-12, args,
checkiter=False)
checkiter=False, maxiter=self.maxiter)
except RuntimeError:
Pmin = self.Pmin
Pmax = self.Pmax
P = flx.IQ_interpolation(f, Pmin, Pmax,
f(Pmin, *args), f(Pmax, *args),
P_guess, self.P_tol, 5e-12, args,
checkiter=False, checkbounds=False)
checkiter=False, checkbounds=False,
maxiter=self.maxiter)
return P, fn.normalize(x)
else:
f = self._P_error_reactive
Expand Down

0 comments on commit 23fb77a

Please sign in to comment.