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

Refactor: presolving based on structural matrix #620

Open
wants to merge 15 commits into
base: v0.8
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
45 changes: 13 additions & 32 deletions src/tespy/components/basics/cycle_closer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from tespy.components.component import Component
from tespy.components.component import component_registry
from tespy.tools.data_containers import ComponentProperties as dc_cp
from tespy.tools.data_containers import ComponentMandatoryConstraints as dc_cmc


@component_registry
Expand Down Expand Up @@ -105,18 +106,16 @@ def get_parameters():

def get_mandatory_constraints(self):
return {
'pressure_equality_constraints': {
'func': self.pressure_equality_func,
'deriv': self.pressure_equality_deriv,
'constant_deriv': True,
'latex': self.pressure_equality_func_doc,
'num_eq': 1},
'enthalpy_equality_constraints': {
'func': self.enthalpy_equality_func,
'deriv': self.enthalpy_equality_deriv,
'constant_deriv': True,
'latex': self.enthalpy_equality_func_doc,
'num_eq': 1}
'pressure_equality_constraint': dc_cmc(**{
'num_eq': 1,
'structure_matrix': self.variable_equality_structure_matrix,
'func_params': {'variable': 'p'}
}),
'enthalpy_equality_constraint': dc_cmc(**{
'num_eq': 1,
'structure_matrix': self.variable_equality_structure_matrix,
'func_params': {'variable': 'h'}
})
}

@staticmethod
Expand All @@ -127,21 +126,6 @@ def inlets():
def outlets():
return ['out1']

@staticmethod
def is_branch_source():
return True

def start_branch(self):
outconn = self.outl[0]
branch = {
"connections": [outconn],
"components": [self, outconn.target],
"subbranches": {}
}
outconn.target.propagate_to_target(branch)

return {outconn.label: branch}

def start_fluid_wrapper_branch(self):
outconn = self.outl[0]
branch = {
Expand All @@ -152,15 +136,12 @@ def start_fluid_wrapper_branch(self):

return {outconn.label: branch}

def propagate_to_target(self, branch):
return

def propagate_wrapper_to_target(self, branch):
branch["components"] += [self]
return

def preprocess(self, num_nw_vars):
super().preprocess(num_nw_vars)
def preprocess(self, row_idx):
super().preprocess(row_idx)
self._propagation_start = False

def calc_parameters(self):
Expand Down
3 changes: 0 additions & 3 deletions src/tespy/components/basics/sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ def inlets():
def get_mandatory_constraints():
return {}

def propagate_to_target(self, branch):
return

def propagate_wrapper_to_target(self, branch):
branch["components"] += [self]
return
Expand Down
15 changes: 0 additions & 15 deletions src/tespy/components/basics/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@ def component():
def outlets():
return ['out1']

@staticmethod
def is_branch_source():
return True

def start_branch(self):
outconn = self.outl[0]
branch = {
"connections": [outconn],
"components": [self, outconn.target],
"subbranches": {}
}
outconn.target.propagate_to_target(branch)

return {outconn.label: branch}

def start_fluid_wrapper_branch(self):
outconn = self.outl[0]
branch = {
Expand Down
44 changes: 14 additions & 30 deletions src/tespy/components/combustion/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,26 +179,33 @@ def get_mandatory_constraints(self):
self.fluid_eqs_list = list(self.fluid_eqs)
return {
'mass_flow_constraints': {
'func': self.mass_flow_func, 'deriv': self.mass_flow_deriv,
'constant_deriv': True, 'latex': self.mass_flow_func_doc,
'num_eq': 1},
'func': self.mass_flow_func,
'deriv': self.mass_flow_deriv,
'constant_deriv': True,
'latex': self.mass_flow_func_doc,
'num_eq': 1
},
'reactor_pressure_constraints': {
'func': self.combustion_pressure_func,
'deriv': self.combustion_pressure_deriv,
'constant_deriv': True,
'latex': self.combustion_pressure_func_doc,
'num_eq': 2},
'num_eq': 2
},
'stoichiometry_constraints': {
'func': self.stoichiometry_func,
'deriv': self.stoichiometry_deriv,
'constant_deriv': False,
'latex': self.stoichiometry_func_doc,
'num_eq': len(self.fluid_eqs)},
'num_eq': len(self.fluid_eqs)
},
'energy_balance_constraints': {
'func': self.energy_balance_func,
'deriv': self.energy_balance_deriv,
'constant_deriv': False, 'latex': self.energy_balance_func_doc,
'num_eq': 1}
'constant_deriv': False,
'latex': self.energy_balance_func_doc,
'num_eq': 1
}
}

@staticmethod
Expand All @@ -209,29 +216,6 @@ def inlets():
def outlets():
return ['out1']

@staticmethod
def is_branch_source():
return True

def start_branch(self):
_, outl = self._get_combustion_connections()
outconn = outl[0]
for f in ["H2O", "CO2"]:
if f not in outconn.fluid.val:
outconn.fluid.val[f] = 0

branch = {
"connections": [outconn],
"components": [self, outconn.target],
"subbranches": {}
}
outconn.target.propagate_to_target(branch)

return {outconn.label: branch}

def propagate_to_target(self, branch):
return

def propagate_wrapper_to_target(self, branch):
if self in branch["components"]:
return
Expand Down
34 changes: 10 additions & 24 deletions src/tespy/components/combustion/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,6 @@ def inlets():
def outlets():
return ['out1', 'out2', 'out3']

def propagate_to_target(self, branch):
inl, outl = self._get_combustion_connections()
inconn = branch["connections"][-1]
if inconn in inl:
return

conn_idx = self.inl.index(inconn)
outconn = self.outl[conn_idx]

branch["connections"] += [outconn]
branch["components"] += [outconn.target]

outconn.target.propagate_to_target(branch)

def propagate_wrapper_to_target(self, branch):
inl, outl = self._get_combustion_connections()
inconn = branch["connections"][-1]
Expand Down Expand Up @@ -494,9 +480,9 @@ def energy_balance_deriv(self, increment_filter, k):

# power and heat loss
if self.P.is_var:
self.jacobian[k, self.P.J_col] = 1
self.jacobian[k, self.p.J_col] = 1
if self.Qloss.is_var:
self.jacobian[k, self.Qloss.J_col] = 1
self.jacobian[k, self.QlossJ_col()] = 1

def Q1_func(self):
r"""
Expand Down Expand Up @@ -688,7 +674,7 @@ def tiP_char_deriv(self, increment_filter, k):
self.jacobian[k, c.fluid.J_col[fl]] = self.numeric_deriv(f, fl, c)

if self.P.is_var:
self.jacobian[k, self.P.J_col] = self.numeric_deriv(f, 'P', None)
self.jacobian[k, self.p.J_col] = self.numeric_deriv(f, 'P', None)

def Q1_char_func(self):
r"""
Expand Down Expand Up @@ -783,7 +769,7 @@ def Q1_char_deriv(self, increment_filter, k):
self.jacobian[k, c.fluid.J_col[fl]] = self.numeric_deriv(f, fl, c)

if self.P.is_var:
self.jacobian[k, self.P.J_col] = self.numeric_deriv(f, 'P', None)
self.jacobian[k, self.p.J_col] = self.numeric_deriv(f, 'P', None)

def Q2_char_func(self):
r"""
Expand Down Expand Up @@ -880,7 +866,7 @@ def Q2_char_deriv(self, increment_filter, k):
self.jacobian[k, c.fluid.J_col[fl]] = self.numeric_deriv(f, fl, c)

if self.P.is_var:
self.jacobian[k, self.P.J_col] = self.numeric_deriv(f, 'P', None)
self.jacobian[k, self.p.J_col] = self.numeric_deriv(f, 'P', None)

def Qloss_char_func(self):
r"""
Expand Down Expand Up @@ -956,9 +942,9 @@ def Qloss_char_deriv(self, increment_filter, k):
self.jacobian[k, c.fluid.J_col[fl]] = self.numeric_deriv(f, fl, c)

if self.P.is_var:
self.jacobian[k, self.P.J_col] = self.numeric_deriv(f, 'P', None)
self.jacobian[k, self.p.J_col] = self.numeric_deriv(f, 'P', None)
if self.Qloss.is_var:
self.jacobian[k, self.Qloss.J_col] = self.numeric_deriv(f, 'Qloss', None)
self.jacobian[k, self.QlossJ_col()] = self.numeric_deriv(f, 'Qloss', None)

def calc_P(self):
r"""
Expand Down Expand Up @@ -1185,9 +1171,9 @@ def bus_deriv(self, bus):

# variable power
if self.P.is_var:
if self.P.J_col not in bus.jacobian:
bus.jacobian[self.P.J_col] = 0
bus.jacobian[self.P.J_col] -= self.numeric_deriv(f, 'P', None, bus=bus)
if self.p.J_col not in bus.jacobian:
bus.jacobian[self.p.J_col] = 0
bus.jacobian[self.p.J_col] -= self.numeric_deriv(f, 'P', None, bus=bus)

######################################################################
# derivatives for bus parameter of total heat production (Q)
Expand Down
Loading