From d76088bf675d24ca70353657b754acff7101a551 Mon Sep 17 00:00:00 2001 From: Francesco Witte Date: Sun, 10 Sep 2023 12:21:21 +0200 Subject: [PATCH] Fix a couple of bus derivatives --- src/tespy/components/combustion/base.py | 16 ++++++---- src/tespy/components/component.py | 32 ------------------- src/tespy/components/heat_exchangers/base.py | 19 ++++++++--- .../components/heat_exchangers/simple.py | 19 ++++++++--- 4 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/tespy/components/combustion/base.py b/src/tespy/components/combustion/base.py index b9af1420c..00e65559a 100644 --- a/src/tespy/components/combustion/base.py +++ b/src/tespy/components/combustion/base.py @@ -1139,13 +1139,17 @@ def bus_deriv(self, bus): deriv : ndarray Matrix of partial derivatives. """ - deriv = np.zeros((1, 3, self.num_nw_vars)) f = self.calc_bus_value - for i in range(3): - deriv[0, i, 0] = self.numeric_deriv(f, 'm', i, bus=bus) - deriv[0, i, 3:] = self.numeric_deriv(f, 'fluid', i, bus=bus) - - return deriv + for c in self.inl + self.outl: + if c.m.is_var: + if c.m.J_col not in bus.jacobian: + bus.jacobian[c.m.J_col] = 0 + bus.jacobian[c.m.J_col] = -self.numeric_deriv(f, 'm', c, bus=bus) + + for fluid in c.fluid.is_var: + if c.fluid.J_col[fluid] not in bus.jacobian: + bus.jacobian[c.fluid.J_col[fluid]] = 0 + bus.jacobian[c.fluid.J_col[fluid]] = -self.numeric_deriv(f, fluid, c, bus=bus) def initialise_fluids(self): """Calculate reaction balance for generic starting values at outlet.""" diff --git a/src/tespy/components/component.py b/src/tespy/components/component.py index 5197d44b0..cd6d87877 100644 --- a/src/tespy/components/component.py +++ b/src/tespy/components/component.py @@ -620,38 +620,6 @@ def bus_func(self, bus): """ return 0 - def bus_func_doc(self, bus): - r""" - Base method for LaTeX equation generation of the bus function. - - Parameters - ---------- - bus : tespy.connections.bus.Bus - TESPy bus object. - - Returns - ------- - latex : str - Bus function in LaTeX format. - """ - return None - - def bus_deriv(self, bus): - r""" - Base method for partial derivatives of the bus function. - - Parameters - ---------- - bus : tespy.connections.bus.Bus - TESPy bus object. - - Returns - ------- - deriv : ndarray - Matrix of partial derivatives. - """ - return np.zeros((1, self.num_i + self.num_o, self.num_nw_vars)) - def calc_bus_expr(self, bus): r""" Return the busses' characteristic line input expression. diff --git a/src/tespy/components/heat_exchangers/base.py b/src/tespy/components/heat_exchangers/base.py index b16e7d056..38a2ec7e4 100644 --- a/src/tespy/components/heat_exchangers/base.py +++ b/src/tespy/components/heat_exchangers/base.py @@ -734,12 +734,21 @@ def bus_deriv(self, bus): deriv : ndarray Matrix of partial derivatives. """ - deriv = np.zeros((1, 4, self.num_nw_vars)) f = self.calc_bus_value - deriv[0, 0, 0] = self.numeric_deriv(f, 'm', 0, bus=bus) - deriv[0, 0, 2] = self.numeric_deriv(f, 'h', 0, bus=bus) - deriv[0, 2, 2] = self.numeric_deriv(f, 'h', 2, bus=bus) - return deriv + if self.inl[0].m.is_var: + if self.inl[0].m.J_col not in bus.jacobian: + bus.jacobian[self.inl[0].m.J_col] = 0 + bus.jacobian[self.inl[0].m.J_col] -= self.numeric_deriv(f, 'm', self.inl[0], bus=bus) + + if self.inl[0].h.is_var: + if self.inl[0].h.J_col not in bus.jacobian: + bus.jacobian[self.inl[0].h.J_col] = 0 + bus.jacobian[self.inl[0].h.J_col] -= self.numeric_deriv(f, 'h', self.inl[0], bus=bus) + + if self.outl[0].h.is_var: + if self.outl[0].h.J_col not in bus.jacobian: + bus.jacobian[self.outl[0].h.J_col] = 0 + bus.jacobian[self.outl[0].h.J_col] -= self.numeric_deriv(f, 'h', self.outl[0], bus=bus) def initialise_source(self, c, key): r""" diff --git a/src/tespy/components/heat_exchangers/simple.py b/src/tespy/components/heat_exchangers/simple.py index aa4dd636c..23ffdcafc 100644 --- a/src/tespy/components/heat_exchangers/simple.py +++ b/src/tespy/components/heat_exchangers/simple.py @@ -794,12 +794,21 @@ def bus_deriv(self, bus): deriv : ndarray Matrix of partial derivatives. """ - deriv = np.zeros((1, 2, self.num_nw_vars)) f = self.calc_bus_value - deriv[0, 0, 0] = self.numeric_deriv(f, 'm', 0, bus=bus) - deriv[0, 0, 2] = self.numeric_deriv(f, 'h', 0, bus=bus) - deriv[0, 1, 2] = self.numeric_deriv(f, 'h', 1, bus=bus) - return deriv + if self.inl[0].m.is_var: + if self.inl[0].m.J_col not in bus.jacobian: + bus.jacobian[self.inl[0].m.J_col] = 0 + bus.jacobian[self.inl[0].m.J_col] -= self.numeric_deriv(f, 'm', self.inl[0], bus=bus) + + if self.inl[0].h.is_var: + if self.inl[0].h.J_col not in bus.jacobian: + bus.jacobian[self.inl[0].h.J_col] = 0 + bus.jacobian[self.inl[0].h.J_col] -= self.numeric_deriv(f, 'h', self.inl[0], bus=bus) + + if self.outl[0].h.is_var: + if self.outl[0].h.J_col not in bus.jacobian: + bus.jacobian[self.outl[0].h.J_col] = 0 + bus.jacobian[self.outl[0].h.J_col] -= self.numeric_deriv(f, 'h', self.outl[0], bus=bus) def initialise_source(self, c, key): r"""