Skip to content

Commit

Permalink
all around improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelcortes committed Oct 14, 2024
1 parent 19de1d8 commit 7b7a8ea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
31 changes: 18 additions & 13 deletions thermosteam/mixture/mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,12 @@ def solve_T_at_HP(self, phase, mol, H, T_guess, P):
self._load_free_energy_args(phase, mol, T_guess, P)
try:
args = (H, self.H, phase, mol, P, self.Cn, [0, None])
T_guess = flx.aitken(iter_T_at_HP, T_guess, self.T_tol, args, self.maxiter, checkiter=False)
T = iter_T_at_HP(T_guess, *args)
if abs(T - T_guess) < self.T_tol: return T
T_guess = flx.aitken(iter_T_at_HP, T, self.T_tol, args, self.maxiter, checkiter=False)
T = iter_T_at_HP(T_guess, *args)
return (
flx.aitken_secant(
flx.secant(
lambda T: self.H(phase, mol, T, P) - H,
x0=T_guess, x1=T, xtol=self.T_tol, ytol=0.
)
Expand All @@ -231,12 +233,14 @@ def xsolve_T_at_HP(self, phase_mol, H, T_guess, P):
self._load_xfree_energy_args(phase_mol, T_guess, P)
try:
args = (H, self.xH, phase_mol, P, self.xCn, [0, None])
T_guess = flx.aitken(xiter_T_at_HP, T_guess, self.T_tol, args, self.maxiter, checkiter=False)
T = xiter_T_at_HP(T_guess, *args)
if abs(T - T_guess) < self.T_tol: return T
T_guess = flx.aitken(xiter_T_at_HP, T, self.T_tol, args, self.maxiter, checkiter=False)
T = xiter_T_at_HP(T_guess, *args)
return (
flx.aitken_secant(
flx.secant(
lambda T: self.xH(phase_mol, T, P) - H,
x0=T_guess, x1=T, xtol=self.T_tol, ytol=0.
x0=T_guess, x1=T, xtol=self.T_tol, ytol=0., checkiter=False
)
if abs(T - T_guess) > self.T_tol else T
)
Expand All @@ -248,10 +252,12 @@ def solve_T_at_SP(self, phase, mol, S, T_guess, P):
self._load_free_energy_args(phase, mol, T_guess, P)
try:
args = (S, self.S, phase, mol, P, self.Cn, [0, None])
T_guess = flx.aitken(iter_T_at_SP, T_guess, self.T_tol, args, self.maxiter, checkiter=False)
T = iter_T_at_SP(T_guess, *args)
if abs(T - T_guess) < self.T_tol: return T
T_guess = flx.aitken(iter_T_at_SP, T, self.T_tol, args, self.maxiter, checkiter=False)
T = iter_T_at_SP(T_guess, *args)
return (
flx.aitken_secant(
flx.secant(
lambda T: self.S(phase, mol, T, P) - S,
x0=T_guess, x1=T, xtol=self.T_tol, ytol=0.
)
Expand All @@ -266,10 +272,12 @@ def xsolve_T_at_SP(self, phase_mol, S, T_guess, P):
self._load_xfree_energy_args(phase_mol, T_guess, P)
try:
args = (S, self.xS, phase_mol, P, self.xCn, [0, None])
T_guess = flx.aitken(xiter_T_at_SP, T_guess, self.T_tol, args, self.maxiter, checkiter=False)
T = xiter_T_at_SP(T_guess, *args)
if abs(T - T_guess) < self.T_tol: return T
T_guess = flx.aitken(xiter_T_at_SP, T, self.T_tol, args, self.maxiter, checkiter=False)
T = xiter_T_at_SP(T_guess, *args)
return (
flx.aitken_secant(
flx.secant(
lambda T: self.xS(phase_mol, T, P) - S,
x0=T_guess, x1=T, xtol=self.T_tol, ytol=0.
)
Expand Down Expand Up @@ -458,10 +466,7 @@ def from_chemicals(cls, chemicals,
"""
isa = isinstance
if isa(chemicals, CompiledChemicals):
try:
MWs = chemicals.MW
except:
breakpoint()
MWs = chemicals.MW
chemicals = chemicals.tuple
else:
chemicals = [(i if isa(i, Chemical) else Chemical(i, cache=cache)) for i in chemicals]
Expand Down
10 changes: 9 additions & 1 deletion thermosteam/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,15 @@ def replace(self, stream, other_stream):
self[index] = other_stream

def index(self, stream):
return self._streams.index(stream)
try:
return self._streams.index(stream)
except Exception as e:
imol = stream._imol
for n, i in enumerate(self._streams):
if i and i._imol is imol:
return n
else:
raise e from None

def pop(self, index):
streams = self._streams
Expand Down
3 changes: 3 additions & 0 deletions thermosteam/thermo/tp_dependent_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

# Remove cache from call
def __call__(self, T, P):
if self.hook:
return self.hook(T, P)
if self._method_P:
return self.TP_dependent_property(T, P)
else:
Expand All @@ -49,6 +51,7 @@ def __call__(self, T, P):
def has_method(self):
return bool(self._method or self._method_P and self._method_P != NEGLECT_P)

TPDependentProperty.hook = None
TPDependentProperty.__bool__ = has_method

# Handling methods
Expand Down

0 comments on commit 7b7a8ea

Please sign in to comment.