From 7b7a8ea777917b6ba4c04a39658e421e55586822 Mon Sep 17 00:00:00 2001 From: Yoel Date: Mon, 14 Oct 2024 10:12:08 -0500 Subject: [PATCH] all around improvements --- thermosteam/mixture/mixture.py | 31 ++++++++++++--------- thermosteam/network.py | 10 ++++++- thermosteam/thermo/tp_dependent_property.py | 3 ++ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/thermosteam/mixture/mixture.py b/thermosteam/mixture/mixture.py index 44cdd5c7..e0fd6c13 100644 --- a/thermosteam/mixture/mixture.py +++ b/thermosteam/mixture/mixture.py @@ -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. ) @@ -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 ) @@ -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. ) @@ -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. ) @@ -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] diff --git a/thermosteam/network.py b/thermosteam/network.py index cd072ce1..e591fef6 100644 --- a/thermosteam/network.py +++ b/thermosteam/network.py @@ -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 diff --git a/thermosteam/thermo/tp_dependent_property.py b/thermosteam/thermo/tp_dependent_property.py index 2b121efb..d4f5142b 100644 --- a/thermosteam/thermo/tp_dependent_property.py +++ b/thermosteam/thermo/tp_dependent_property.py @@ -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: @@ -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