Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass proof of concept #615

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/tespy/components/basics/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from tespy.components.component import Component
from tespy.components.component import component_registry
from tespy.tools.data_containers import SimpleDataContainer as dc_simple


@component_registry
Expand Down Expand Up @@ -78,6 +79,11 @@ def propagate_wrapper_to_target(self, branch):
branch["components"] += [self]
return

def get_parameters(self):
return {
'status': dc_simple(val="active")
}

def exergy_balance(self, T0):
r"""Exergy balance calculation method of a sink.

Expand Down
6 changes: 6 additions & 0 deletions src/tespy/components/basics/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from tespy.components.component import Component
from tespy.components.component import component_registry
from tespy.tools.data_containers import SimpleDataContainer as dc_simple


@component_registry
Expand Down Expand Up @@ -71,6 +72,11 @@ def outlets():
def is_branch_source():
return True

def get_parameters(self):
return {
'status': dc_simple(val="active")
}

def start_branch(self):
outconn = self.outl[0]
branch = {
Expand Down
150 changes: 80 additions & 70 deletions src/tespy/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ def preprocess(self, num_nw_vars):
self.num_eq = 0
self.vars = {}
self.num_vars = 0
self.constraints = self.get_mandatory_constraints().copy()

if self.get_attr("status").val == "active":
self.constraints = self.get_mandatory_constraints().copy()
else:
self.constraints = self.get_bypass_constraints().copy()

self.prop_specifications = {}
self.var_specifications = {}
self.group_specifications = {}
Expand All @@ -349,68 +354,69 @@ def preprocess(self, num_nw_vars):
for constraint in self.constraints.values():
self.num_eq += constraint['num_eq']

for key, val in self.parameters.items():
data = self.get_attr(key)
if isinstance(val, dc_cp):
if data.is_var:
data.J_col = num_nw_vars + self.num_vars
self.num_vars += 1
self.vars[data] = key

self.prop_specifications[key] = val.is_set
self.var_specifications[key] = val.is_var

# component characteristics
elif isinstance(val, dc_cc):
if data.func is not None:
self.char_specifications[key] = val.is_set
if data.char_func is None:
try:
data.char_func = ldc(
self.component(), key, 'DEFAULT', CharLine)
except KeyError:
data.char_func = CharLine(x=[0, 1], y=[1, 1])

# component characteristics
elif isinstance(val, dc_cm):
if data.func is not None:
self.char_specifications[key] = val.is_set
if data.char_func is None:
try:
data.char_func = ldc(
self.component(), key, 'DEFAULT', CharMap)
except KeyError:
data.char_func = CharLine(x=[0, 1], y=[1, 1])

# grouped component properties
elif isinstance(val, dc_gcp):
is_set = True
for e in data.elements:
if not self.get_attr(e).is_set:
is_set = False

if is_set:
data.set_attr(is_set=True)
elif data.is_set:
start = (
'All parameters of the component group have to be '
'specified! This component group uses the following '
'parameters: '
)
end = f" at {self.label}. Group will be set to False."
logger.warning(start + ', '.join(val.elements) + end)
val.set_attr(is_set=False)
else:
val.set_attr(is_set=False)
self.group_specifications[key] = val.is_set
if self.get_attr("status").val == "active":
for key, val in self.parameters.items():
data = self.get_attr(key)
if isinstance(val, dc_cp):
if data.is_var:
data.J_col = num_nw_vars + self.num_vars
self.num_vars += 1
self.vars[data] = key

self.prop_specifications[key] = val.is_set
self.var_specifications[key] = val.is_var

# component characteristics
elif isinstance(val, dc_cc):
if data.func is not None:
self.char_specifications[key] = val.is_set
if data.char_func is None:
try:
data.char_func = ldc(
self.component(), key, 'DEFAULT', CharLine)
except KeyError:
data.char_func = CharLine(x=[0, 1], y=[1, 1])

# component characteristics
elif isinstance(val, dc_cm):
if data.func is not None:
self.char_specifications[key] = val.is_set
if data.char_func is None:
try:
data.char_func = ldc(
self.component(), key, 'DEFAULT', CharMap)
except KeyError:
data.char_func = CharLine(x=[0, 1], y=[1, 1])

# grouped component properties
elif isinstance(val, dc_gcp):
is_set = True
for e in data.elements:
if not self.get_attr(e).is_set:
is_set = False

if is_set:
data.set_attr(is_set=True)
elif data.is_set:
start = (
'All parameters of the component group have to be '
'specified! This component group uses the following '
'parameters: '
)
end = f" at {self.label}. Group will be set to False."
logger.warning(start + ', '.join(val.elements) + end)
val.set_attr(is_set=False)
else:
val.set_attr(is_set=False)
self.group_specifications[key] = val.is_set

# grouped component characteristics
elif isinstance(val, dc_gcc):
self.group_specifications[key] = val.is_set
# grouped component characteristics
elif isinstance(val, dc_gcc):
self.group_specifications[key] = val.is_set

# component properties
if data.is_set and data.func is not None:
self.num_eq += data.num_eq
# component properties
if data.is_set and data.func is not None:
self.num_eq += data.num_eq

self.jacobian = {}
self.residual = np.zeros(self.num_eq)
Expand All @@ -432,6 +438,9 @@ def get_parameters(self):
def get_mandatory_constraints(self):
return {}

def get_bypass_constraints(self):
return {}

@staticmethod
def inlets():
return []
Expand Down Expand Up @@ -592,14 +601,15 @@ def solve(self, increment_filter):
constraint['deriv'](increment_filter, sum_eq)
sum_eq += num_eq

for data in self.parameters.values():
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.deriv(increment_filter, sum_eq, **data.func_params)
if self.get_attr("status") == "active":
for data in self.parameters.values():
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.deriv(increment_filter, sum_eq, **data.func_params)

sum_eq += data.num_eq
sum_eq += data.num_eq

def bus_func(self, bus):
r"""
Expand Down Expand Up @@ -919,7 +929,7 @@ def pressure_equality_func_doc(self, label):
r'\; \forall i \in [' + indices + r']')
return generate_latex_eq(self, latex, label)

def pressure_equality_deriv(self, k):
def pressure_equality_deriv(self, increment_filter, k):
r"""
Calculate partial derivatives for all mass flow balance equations.

Expand Down Expand Up @@ -978,7 +988,7 @@ def enthalpy_equality_func_doc(self, label):
)
return generate_latex_eq(self, latex, label)

def enthalpy_equality_deriv(self, k):
def enthalpy_equality_deriv(self, increment_filter, k):
r"""
Calculate partial derivatives for all mass flow balance equations.

Expand Down
17 changes: 17 additions & 0 deletions src/tespy/components/heat_exchangers/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def component():

def get_parameters(self):
return {
'status': dc_simple(val="active"),
'Q': dc_cp(
deriv=self.energy_balance_deriv,
latex=self.energy_balance_func_doc, num_eq=1,
Expand Down Expand Up @@ -255,6 +256,22 @@ def preprocess(self, num_nw_vars):

self.Tamb.val_SI = convert_to_SI('T', self.Tamb.val, self.inl[0].T.unit)

def get_bypass_constraints(self):
return {
'pressure_equality_constraints': {
'func': self.pressure_equality_func,
'deriv': self.pressure_equality_deriv,
'constant_deriv': False,
'latex': self.pressure_equality_func_doc,
'num_eq': 1},
'enthalpy_equality_constraints': {
'func': self.enthalpy_equality_func,
'deriv': self.enthalpy_equality_deriv,
'constant_deriv': False,
'latex': self.enthalpy_equality_func_doc,
'num_eq': 1}
}

def energy_balance_func(self):
r"""
Equation for pressure drop calculation.
Expand Down
Loading