From fef2f80d9398b675978b663007241b86580362e8 Mon Sep 17 00:00:00 2001 From: Francesco Witte Date: Tue, 16 Jan 2024 16:31:02 +0100 Subject: [PATCH 1/2] Set the default value for fluid property data to nan --- src/tespy/tools/data_containers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tespy/tools/data_containers.py b/src/tespy/tools/data_containers.py index a9e559626..4dd647fd4 100644 --- a/src/tespy/tools/data_containers.py +++ b/src/tespy/tools/data_containers.py @@ -12,8 +12,6 @@ SPDX-License-Identifier: MIT """ -import collections - import numpy as np from tespy.tools import logger @@ -487,7 +485,7 @@ def attr(): 'design': np.nan, 'val': np.nan, 'val0': np.nan, - 'val_SI': 0, + 'val_SI': np.nan, 'unit': None, 'is_set': False, "is_var": False, From 03ddc6db021f9633df4a6c05e09d498c53eb22a1 Mon Sep 17 00:00:00 2001 From: Francesco Witte Date: Sun, 21 Jan 2024 13:01:19 +0100 Subject: [PATCH 2/2] Delegate T0 value passing to connection object where possible and check for 0 or nan T0 values --- src/tespy/components/heat_exchangers/base.py | 16 ++++++++-------- .../components/heat_exchangers/condenser.py | 8 ++++---- .../heat_exchangers/parabolic_trough.py | 2 +- src/tespy/components/heat_exchangers/simple.py | 8 ++++---- .../heat_exchangers/solar_collector.py | 2 +- src/tespy/components/nodes/separator.py | 4 ++-- .../components/turbomachinery/compressor.py | 4 ++-- src/tespy/connections/connection.py | 2 ++ src/tespy/tools/data_containers.py | 2 +- src/tespy/tools/fluid_properties/helpers.py | 2 +- 10 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/tespy/components/heat_exchangers/base.py b/src/tespy/components/heat_exchangers/base.py index f1685f639..5ad36b254 100644 --- a/src/tespy/components/heat_exchangers/base.py +++ b/src/tespy/components/heat_exchangers/base.py @@ -381,10 +381,10 @@ def calculate_td_log(self): o2 = self.outl[1] # temperature value manipulation for convergence stability - T_i1 = i1.calc_T(T0=i1.T.val_SI) - T_i2 = i2.calc_T(T0=i2.T.val_SI) - T_o1 = o1.calc_T(T0=o1.T.val_SI) - T_o2 = o2.calc_T(T0=o2.T.val_SI) + T_i1 = i1.calc_T() + T_i2 = i2.calc_T() + T_o1 = o1.calc_T() + T_o2 = o2.calc_T() if T_i1 <= T_o2: T_i1 = T_o2 + 0.01 @@ -581,8 +581,8 @@ def ttd_u_func(self): """ i = self.inl[0] o = self.outl[1] - T_i1 = i.calc_T(T0=i.T.val_SI) - T_o2 = o.calc_T(T0=o.T.val_SI) + T_i1 = i.calc_T() + T_o2 = o.calc_T() return self.ttd_u.val - T_i1 + T_o2 def ttd_u_func_doc(self, label): @@ -636,8 +636,8 @@ def ttd_l_func(self): """ i = self.inl[1] o = self.outl[0] - T_i2 = i.calc_T(T0=i.T.val_SI) - T_o1 = o.calc_T(T0=o.T.val_SI) + T_i2 = i.calc_T() + T_o1 = o.calc_T() return self.ttd_l.val - T_o1 + T_i2 def ttd_l_func_doc(self, label): diff --git a/src/tespy/components/heat_exchangers/condenser.py b/src/tespy/components/heat_exchangers/condenser.py index a87316095..377660332 100644 --- a/src/tespy/components/heat_exchangers/condenser.py +++ b/src/tespy/components/heat_exchangers/condenser.py @@ -327,9 +327,9 @@ def calculate_td_log(self): o2 = self.outl[1] T_i1 = i1.calc_T_sat() - T_i2 = i2.calc_T(T0=i2.T.val_SI) - T_o1 = o1.calc_T(T0=o1.T.val_SI) - T_o2 = o2.calc_T(T0=o2.T.val_SI) + T_i2 = i2.calc_T() + T_o1 = o1.calc_T() + T_o2 = o2.calc_T() if T_i1 <= T_o2 and not i1.T.is_set: T_i1 = T_o2 + 0.5 @@ -452,7 +452,7 @@ def ttd_u_func(self): i = self.inl[0] o = self.outl[1] T_i1 = i.calc_T_sat() - T_o2 = o.calc_T(T0=self.outl[1].T.val_SI) + T_o2 = o.calc_T() return self.ttd_u.val - T_i1 + T_o2 def ttd_u_func_doc(self, label): diff --git a/src/tespy/components/heat_exchangers/parabolic_trough.py b/src/tespy/components/heat_exchangers/parabolic_trough.py index 169d164fd..1b948ae7e 100644 --- a/src/tespy/components/heat_exchangers/parabolic_trough.py +++ b/src/tespy/components/heat_exchangers/parabolic_trough.py @@ -274,7 +274,7 @@ def energy_group_func(self): i = self.inl[0] o = self.outl[0] - T_m = 0.5 * (i.calc_T(T0=i.T.val_SI) + o.calc_T(T0=o.T.val_SI)) + T_m = 0.5 * (i.calc_T() + o.calc_T()) iam = ( 1 - self.iam_1.val * abs(self.aoi.val) diff --git a/src/tespy/components/heat_exchangers/simple.py b/src/tespy/components/heat_exchangers/simple.py index 57623e758..a9e555079 100644 --- a/src/tespy/components/heat_exchangers/simple.py +++ b/src/tespy/components/heat_exchangers/simple.py @@ -535,8 +535,8 @@ def kA_group_func(self): i = self.inl[0] o = self.outl[0] - ttd_1 = i.calc_T(T0=i.T.val_SI) - self.Tamb.val_SI - ttd_2 = o.calc_T(T0=o.T.val_SI) - self.Tamb.val_SI + ttd_1 = i.calc_T() - self.Tamb.val_SI + ttd_2 = o.calc_T() - self.Tamb.val_SI # For numerical stability: If temperature differences have # different sign use mean difference to avoid negative logarithm. @@ -651,8 +651,8 @@ def kA_char_group_func(self): # For numerical stability: If temperature differences have # different sign use mean difference to avoid negative logarithm. - ttd_1 = i.calc_T(T0=i.T.val_SI) - self.Tamb.val_SI - ttd_2 = o.calc_T(T0=o.T.val_SI) - self.Tamb.val_SI + ttd_1 = i.calc_T() - self.Tamb.val_SI + ttd_2 = o.calc_T() - self.Tamb.val_SI if (ttd_1 / ttd_2) < 0: td_log = (ttd_2 + ttd_1) / 2 diff --git a/src/tespy/components/heat_exchangers/solar_collector.py b/src/tespy/components/heat_exchangers/solar_collector.py index 9a940e12c..7129bd5c6 100644 --- a/src/tespy/components/heat_exchangers/solar_collector.py +++ b/src/tespy/components/heat_exchangers/solar_collector.py @@ -231,7 +231,7 @@ def energy_group_func(self): i = self.inl[0] o = self.outl[0] - T_m = 0.5 * (i.calc_T(T0=i.T.val_SI) + o.calc_T(T0=o.T.val_SI)) + T_m = 0.5 * (i.calc_T() + o.calc_T()) return ( i.m.val_SI * (o.h.val_SI - i.h.val_SI) diff --git a/src/tespy/components/nodes/separator.py b/src/tespy/components/nodes/separator.py index c826f022c..bf8a7be85 100644 --- a/src/tespy/components/nodes/separator.py +++ b/src/tespy/components/nodes/separator.py @@ -299,9 +299,9 @@ def energy_balance_func(self): \forall j \in \text{outlets} """ residual = [] - T_in = self.inl[0].calc_T(T0=300) + T_in = self.inl[0].calc_T() for o in self.outl: - residual += [T_in - o.calc_T(T0=300)] + residual += [T_in - o.calc_T()] return residual def energy_balance_func_doc(self, label): diff --git a/src/tespy/components/turbomachinery/compressor.py b/src/tespy/components/turbomachinery/compressor.py index a28d0d344..5c7668d92 100644 --- a/src/tespy/components/turbomachinery/compressor.py +++ b/src/tespy/components/turbomachinery/compressor.py @@ -374,7 +374,7 @@ def char_map_pr_func(self): i = self.inl[0] o = self.outl[0] - x = np.sqrt(i.T.design / i.calc_T(T0=i.T.val_SI)) + x = np.sqrt(i.T.design / i.calc_T()) y = (i.m.val_SI * i.p.design) / (i.m.design * i.p.val_SI * x) yarr, zarr = self.char_map_pr.char_func.evaluate_x(x) @@ -477,7 +477,7 @@ def char_map_eta_s_func(self): i = self.inl[0] o = self.outl[0] - x = np.sqrt(i.T.design / i.calc_T(T0=i.T.val_SI)) + x = np.sqrt(i.T.design / i.calc_T()) y = (i.m.val_SI * i.p.design) / (i.m.design * i.p.val_SI * x) yarr, zarr = self.char_map_eta_s.char_func.evaluate_x(x) diff --git a/src/tespy/connections/connection.py b/src/tespy/connections/connection.py index 3c58eeafe..bd788fc6a 100644 --- a/src/tespy/connections/connection.py +++ b/src/tespy/connections/connection.py @@ -737,6 +737,8 @@ def primary_ref_deriv(self, k, **kwargs): self.jacobian[k, ref.obj.get_attr(variable).J_col] = -ref.factor def calc_T(self, T0=None): + if T0 is None: + T0 = self.T.val_SI return T_mix_ph(self.p.val_SI, self.h.val_SI, self.fluid_data, self.mixing_rule, T0=T0) def T_func(self, k, **kwargs): diff --git a/src/tespy/tools/data_containers.py b/src/tespy/tools/data_containers.py index 4dd647fd4..61375b466 100644 --- a/src/tespy/tools/data_containers.py +++ b/src/tespy/tools/data_containers.py @@ -485,7 +485,7 @@ def attr(): 'design': np.nan, 'val': np.nan, 'val0': np.nan, - 'val_SI': np.nan, + 'val_SI': 0, 'unit': None, 'is_set': False, "is_var": False, diff --git a/src/tespy/tools/fluid_properties/helpers.py b/src/tespy/tools/fluid_properties/helpers.py index 27740208a..ff36cfa00 100644 --- a/src/tespy/tools/fluid_properties/helpers.py +++ b/src/tespy/tools/fluid_properties/helpers.py @@ -67,7 +67,7 @@ def get_molar_fractions(fluid_data): def inverse_temperature_mixture(p=None, target_value=None, fluid_data=None, T0=None, f=None): # calculate the fluid properties for fluid mixtures valmin, valmax = get_mixture_temperature_range(fluid_data) - if T0 is None: + if T0 is None or T0 == 0 or np.isnan(T0): T0 = (valmin + valmax) / 2.0 function_kwargs = {