Skip to content

Commit

Permalink
Adapt concept for offdesign calculations and fix turbomachinery imple…
Browse files Browse the repository at this point in the history
…mentations
  • Loading branch information
fwitte committed Sep 4, 2023
1 parent 2458117 commit 30b4cc2
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 346 deletions.
60 changes: 34 additions & 26 deletions src/tespy/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,15 @@ def get_char_expr(self, param, type='rel', inconn=0, outconn=0):
"""
if type == 'rel':
if param == 'm':
return (
self.inl[inconn].m.val_SI / self.inl[inconn].m.design)
return self.inl[inconn].m.val_SI / self.inl[inconn].m.design
elif param == 'm_out':
return (
self.outl[outconn].m.val_SI /
self.outl[outconn].m.design)
return self.outl[outconn].m.val_SI / self.outl[outconn].m.design
elif param == 'v':
v = self.inl[inconn].m.val_SI * v_mix_ph(
self.inl[inconn].get_flow(),
T0=self.inl[inconn].T.val_SI)
self.inl[inconn].p.val_SI, self.inl[inconn].h.val_SI,
self.inl[inconn].fluid_data, self.inl[inconn].mixing_rule,
T0=self.inl[inconn].T.val_SI
)
return v / self.inl[inconn].v.design
elif param == 'pr':
return (
Expand All @@ -501,12 +500,12 @@ def get_char_expr(self, param, type='rel', inconn=0, outconn=0):
return self.outl[outconn].m.val_SI
elif param == 'v':
return self.inl[inconn].m.val_SI * v_mix_ph(
self.inl[inconn].get_flow(),
T0=self.inl[inconn].T.val_SI)
self.inl[inconn].p.val_SI, self.inl[inconn].h.val_SI,
self.inl[inconn].fluid_data, self.inl[inconn].mixing_rule,
T0=self.inl[inconn].T.val_SI
)
elif param == 'pr':
return (
self.outl[outconn].p.val_SI /
self.inl[inconn].p.val_SI)
return self.outl[outconn].p.val_SI / self.inl[inconn].p.val_SI
else:
return False

Expand Down Expand Up @@ -592,7 +591,8 @@ def solve(self, increment_filter):
for parameter, data in self.parameters.items():
if data.is_set and data.func is not None:
self.residual[sum_eq:sum_eq + data.num_eq] = data.func(
**data.func_params)
**data.func_params
)
data.deriv(increment_filter, sum_eq, **data.func_params)

sum_eq += data.num_eq
Expand Down Expand Up @@ -1064,7 +1064,7 @@ def enthalpy_equality_deriv(self, k):
if self.outl[i].h.is_var:
self.jacobian[k + i, self.outl[i].h.J_col] = -1

def numeric_deriv(self, func, dx, conn, **kwargs):
def numeric_deriv(self, func, dx, conn=None, **kwargs):
r"""
Calculate partial derivative of the function func to dx.
Expand All @@ -1091,7 +1091,19 @@ def numeric_deriv(self, func, dx, conn, **kwargs):
\frac{\partial f}{\partial x} = \frac{f(x + d) + f(x - d)}{2 d}
"""
if dx in conn.fluid.is_var:
if conn is None:
d = self.get_attr(dx).d
exp = 0
self.get_attr(dx).val += d
exp += func(**kwargs)

self.get_attr(dx).val -= 2 * d
exp -= func(**kwargs)
deriv = exp / (2 * d)

self.get_attr(dx).val += d

elif dx in conn.fluid.is_var:
d = 1e-5

val = conn.fluid.val[dx]
Expand Down Expand Up @@ -1131,17 +1143,13 @@ def numeric_deriv(self, func, dx, conn, **kwargs):
conn.get_attr(dx).val_SI += d

else:
d = self.get_attr(dx).d
exp = 0
self.get_attr(dx).val += d
exp += func(**kwargs)

self.get_attr(dx).val -= 2 * d
exp -= func(**kwargs)
deriv = exp / (2 * d)

self.get_attr(dx).val += d

msg = (
"Your variable specification for the numerical derivative "
"calculation seems to be wrong. It has to be a fluid name, m, "
"p, h or the name of a component variable."
)
logger.exception(msg)
raise ValueError(msg)
return deriv

def get_conn_var_pos(self, connection_number, variable):
Expand Down
115 changes: 69 additions & 46 deletions src/tespy/components/turbomachinery/compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,19 @@ def eta_s_char_func(self):
i = self.inl[0]
o = self.outl[0]
return (
self.eta_s.design * self.eta_s_char.char_func.evaluate(expr) *
(o.h.val_SI - i.h.val_SI) - (isentropic(
i.get_flow(), o.get_flow(), T0=self.inl[0].T.val_SI) -
i.h.val_SI))
(o.h.val_SI - i.h.val_SI)
* self.eta_s.design * self.eta_s_char.char_func.evaluate(expr)
- (
isentropic(
i.p.val_SI,
i.h.val_SI,
o.p.val_SI,
i.fluid_data,
i.mixing_rule,
T0=None
) - i.h.val_SI
)
)

def eta_s_char_func_doc(self, label):
r"""
Expand Down Expand Up @@ -324,16 +333,18 @@ def eta_s_char_deriv(self, increment_filter, k):
Position of derivatives in Jacobian matrix (k-th equation).
"""
f = self.eta_s_char_func
if not increment_filter[0, 0]:
self.jacobian[k, 0, 0] = self.numeric_deriv(f, 'm', 0)
if not increment_filter[0, 1]:
self.jacobian[k, 0, 1] = self.numeric_deriv(f, 'p', 0)
if not increment_filter[1, 1]:
self.jacobian[k, 1, 1] = self.numeric_deriv(f, 'p', 1)
if not increment_filter[0, 2]:
self.jacobian[k, 0, 2] = self.numeric_deriv(f, 'h', 0)
if not increment_filter[1, 2]:
self.jacobian[k, 1, 2] = self.numeric_deriv(f, 'h', 1)
i = self.inl[0]
o = self.outl[0]
if self.is_variable(i.m, increment_filter):
self.jacobian[k, i.m.J_col] = self.numeric_deriv(f, 'm', i)
if self.is_variable(i.p, increment_filter):
self.jacobian[k, i.p.J_col] = self.numeric_deriv(f, 'p', i)
if self.is_variable(i.h, increment_filter):
self.jacobian[k, i.h.J_col] = self.numeric_deriv(f, 'h', i)
if self.is_variable(o.p, increment_filter):
self.jacobian[k, o.p.J_col] = self.numeric_deriv(f, 'p', o)
if self.is_variable(o.h, increment_filter):
self.jacobian[k, o.h.J_col] = self.numeric_deriv(f, 'h', o)

def char_map_pr_func(self):
r"""
Expand Down Expand Up @@ -364,7 +375,7 @@ def char_map_pr_func(self):
"""
i = self.inl[0]
o = self.outl[0]
T = T_mix_ph(i.get_flow(), T0=i.T.val_SI)
T = T_mix_ph(i.p.val_SI, i.h.val_SI, i.fluid_data, i.mixing_rule, T0=i.T.val_SI)

x = np.sqrt(i.T.design / T)
y = (i.m.val_SI * i.p.design) / (i.m.design * i.p.val_SI * x)
Expand All @@ -375,7 +386,7 @@ def char_map_pr_func(self):
zarr *= (1 - self.igva.val / 100)
pr = self.char_map_pr.char_func.evaluate_y(y, yarr, zarr)

return (o.p.val_SI / i.p.val_SI) / self.pr.design - pr
return (o.p.val_SI / i.p.val_SI) - pr * self.pr.design

def char_map_pr_func_doc(self, label):
r"""
Expand Down Expand Up @@ -422,21 +433,23 @@ def char_map_pr_deriv(self, increment_filter, k):
Position of derivatives in Jacobian matrix (k-th equation).
"""
f = self.char_map_pr_func
if not increment_filter[0, 0]:
self.jacobian[k, 0, 0] = self.numeric_deriv(f, 'm', 0)
if not increment_filter[0, 1]:
self.jacobian[k, 0, 1] = self.numeric_deriv(f, 'p', 0)
if not increment_filter[0, 2]:
self.jacobian[k, 0, 2] = self.numeric_deriv(f, 'h', 0)

if not increment_filter[1, 1]:
self.jacobian[k, 1, 1] = self.numeric_deriv(f, 'p', 1)
if not increment_filter[1, 2]:
self.jacobian[k, 1, 2] = self.numeric_deriv(f, 'h', 1)
i = self.inl[0]
o = self.outl[0]
if self.is_variable(i.m, increment_filter):
self.jacobian[k, i.m.J_col] = self.numeric_deriv(f, 'm', i)
if self.is_variable(i.p, increment_filter):
self.jacobian[k, i.p.J_col] = self.numeric_deriv(f, 'p', i)
if self.is_variable(i.h, increment_filter):
self.jacobian[k, i.h.J_col] = self.numeric_deriv(f, 'h', i)
if self.is_variable(o.p, increment_filter):
self.jacobian[k, o.p.J_col] = 1 / i.p.val_SI
if self.is_variable(o.h, increment_filter):
self.jacobian[k, o.h.J_col] = self.numeric_deriv(f, 'h', o)

if self.igva.is_var:
self.jacobian[k, 2 + self.igva.var_pos, 0] = self.numeric_deriv(
f, 'igva', 1)
self.jacobian[k, self.igva.J_col] = self.numeric_deriv(
f, 'igva', None
)

def char_map_eta_s_func(self):
r"""
Expand Down Expand Up @@ -466,7 +479,7 @@ def char_map_eta_s_func(self):
"""
i = self.inl[0]
o = self.outl[0]
T = T_mix_ph(i.get_flow(), T0=i.T.val_SI)
T = T_mix_ph(i.p.val_SI, i.h.val_SI, i.fluid_data, i.mixing_rule, T0=i.T.val_SI)

x = np.sqrt(i.T.design / T)
y = (i.m.val_SI * i.p.design) / (i.m.design * i.p.val_SI * x)
Expand All @@ -478,9 +491,17 @@ def char_map_eta_s_func(self):
eta = self.char_map_eta_s.char_func.evaluate_y(y, yarr, zarr)

return (
(isentropic(i.get_flow(), o.get_flow(), T0=T) -
i.h.val_SI) / (o.h.val_SI - i.h.val_SI) / self.eta_s.design -
eta)
(
isentropic(
i.p.val_SI,
i.h.val_SI,
o.p.val_SI,
i.fluid_data,
i.mixing_rule,
T0=i.T.val_SI
) - i.h.val_SI)
/ (o.h.val_SI - i.h.val_SI) - eta * self.eta_s.design
)

def char_map_eta_s_func_doc(self, label):
r"""
Expand Down Expand Up @@ -527,21 +548,23 @@ def char_map_eta_s_deriv(self, increment_filter, k):
Position of derivatives in Jacobian matrix (k-th equation).
"""
f = self.char_map_eta_s_func
if not increment_filter[0, 0]:
self.jacobian[k, 0, 0] = self.numeric_deriv(f, 'm', 0)
if not increment_filter[0, 1]:
self.jacobian[k, 0, 1] = self.numeric_deriv(f, 'p', 0)
if not increment_filter[0, 2]:
self.jacobian[k, 0, 2] = self.numeric_deriv(f, 'h', 0)

if not increment_filter[1, 1]:
self.jacobian[k, 1, 1] = self.numeric_deriv(f, 'p', 1)
if not increment_filter[1, 2]:
self.jacobian[k, 1, 2] = self.numeric_deriv(f, 'h', 1)
i = self.inl[0]
o = self.outl[0]
if self.is_variable(i.m, increment_filter):
self.jacobian[k, i.m.J_col] = self.numeric_deriv(f, 'm', i)
if self.is_variable(i.p, increment_filter):
self.jacobian[k, i.p.J_col] = self.numeric_deriv(f, 'p', i)
if self.is_variable(i.h, increment_filter):
self.jacobian[k, i.h.J_col] = self.numeric_deriv(f, 'h', i)
if self.is_variable(o.p, increment_filter):
self.jacobian[k, o.p.J_col] = self.numeric_deriv(f, 'p', o)
if self.is_variable(o.h, increment_filter):
self.jacobian[k, o.h.J_col] = self.numeric_deriv(f, 'h', o)

if self.igva.is_var:
self.jacobian[k, 2 + self.igva.var_pos, 0] = self.numeric_deriv(
f, 'igva', 1)
self.jacobian[k, self.igva.J_col] = self.numeric_deriv(
f, 'igva', None
)

def convergence_check(self):
r"""
Expand Down
Loading

0 comments on commit 30b4cc2

Please sign in to comment.