Skip to content

Commit

Permalink
Revert attribute changes by implementing @proprety and fix a couple o…
Browse files Browse the repository at this point in the history
…f components
  • Loading branch information
fwitte committed Mar 2, 2025
1 parent a982041 commit 2a49e37
Show file tree
Hide file tree
Showing 30 changed files with 1,038 additions and 830 deletions.
10 changes: 5 additions & 5 deletions docs/modules/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -713,22 +713,22 @@ equation, too. The Valve's dp_char parameter methods are the following.
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)
self.jacobian[k, i.m.J_col] = self.numeric_deriv(f, 'm', i)
if self.dp_char.param == 'v':
if self.is_variable(i.p, increment_filter):
self.jacobian[k, i.p.J_col()] = self.numeric_deriv(
self.jacobian[k, i.p.J_col] = self.numeric_deriv(
self.dp_char_func, 'p', i
)
if self.is_variable(i.h, increment_filter):
self.jacobian[k, i.h.J_col()] = self.numeric_deriv(
self.jacobian[k, i.h.J_col] = self.numeric_deriv(
self.dp_char_func, 'h', i
)
else:
if self.is_variable(i.p, increment_filter):
self.jacobian[k, i.p.J_col()] = 1
self.jacobian[k, i.p.J_col] = 1
if self.is_variable(o.p):
self.jacobian[k, o.p.J_col()] = -1
self.jacobian[k, o.p.J_col] = -1
For the Jacobian, the partial derivatives to all variables of the network
are required. This means, that you have to calculate the partial derivatives
Expand Down
30 changes: 15 additions & 15 deletions docs/modules/ude.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ inside the Jacobian of the :code:`UserDefinedEquation` and returns it:
connection required by the :code:`UserDefinedEquation`.
- derivatives are referred to with the :code:`J_col` attribute of the
variables of a :code:`Connection` object, i.e.
:code:`c.m.J_col()` for mass flow, :code:`c.p.J_col()` for pressure,
:code:`c.h.J_col()` for enthalpy, and :code:`c.fluid.J_col()[fluid_name]` for the
:code:`c.m.J_col` for mass flow, :code:`c.p.J_col` for pressure,
:code:`c.h.J_col` for enthalpy, and :code:`c.fluid.J_col[fluid_name]` for the
derivative of the fluid composition towards a specific fluid
:code:`fluid_name`.

Expand All @@ -116,9 +116,9 @@ derivatives to mass flow are not zero.
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.m.is_var:
... ude.jacobian[c1.m.J_col()] = 1
... ude.jacobian[c1.m.J_col] = 1
... if c2.m.is_var:
... ude.jacobian[c2.m.J_col()] = -2 * ude.conns[1].m.val_SI
... ude.jacobian[c2.m.J_col] = -2 * ude.conns[1].m.val_SI
Now we can create our instance of the :code:`UserDefinedEquation` and add it to
the network. The class requires four mandatory arguments to be passed:
Expand Down Expand Up @@ -187,17 +187,17 @@ respectively to calculate the partial derivatives.
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.m.is_var:
... ude.jacobian[c1.m.J_col()] = 1 / ude.conns[0].m.val_SI
... ude.jacobian[c1.m.J_col] = 1 / ude.conns[0].m.val_SI
... if c1.p.is_var:
... ude.jacobian[c1.p.J_col()] = - 2 / ude.conns[0].p.val_SI
... ude.jacobian[c1.p.J_col] = - 2 / ude.conns[0].p.val_SI
... T = c2.calc_T()
... if c2.p.is_var:
... ude.jacobian[c2.p.J_col()] = (
... ude.jacobian[c2.p.J_col] = (
... dT_mix_dph(c2.p.val_SI, c2.h.val_SI, c2.fluid_data, c2.mixing_rule)
... * 0.5 / (T ** 0.5)
... )
... if c2.h.is_var:
... ude.jacobian[c2.h.J_col()] = (
... ude.jacobian[c2.h.J_col] = (
... dT_mix_pdh(c2.p.val_SI, c2.h.val_SI, c2.fluid_data, c2.mixing_rule)
... * 0.5 / (T ** 0.5)
... )
Expand All @@ -215,13 +215,13 @@ for the above derivatives would therefore look like this:
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.m.is_var:
... ude.jacobian[c1.m.J_col()] = ude.numeric_deriv('m', c1)
... ude.jacobian[c1.m.J_col] = ude.numeric_deriv('m', c1)
... if c1.p.is_var:
... ude.jacobian[c1.p.J_col()] = ude.numeric_deriv('p', c1)
... ude.jacobian[c1.p.J_col] = ude.numeric_deriv('p', c1)
... if c2.p.is_var:
... ude.jacobian[c2.p.J_col()] = ude.numeric_deriv('p', c2)
... ude.jacobian[c2.p.J_col] = ude.numeric_deriv('p', c2)
... if c2.h.is_var:
... ude.jacobian[c2.h.J_col()] = ude.numeric_deriv('h', c2)
... ude.jacobian[c2.h.J_col] = ude.numeric_deriv('h', c2)
>>> ude = UserDefinedEquation('ude numerical', my_ude, my_ude_deriv, [c1, c2])
>>> nw.add_ude(ude)
Expand Down Expand Up @@ -279,11 +279,11 @@ instance must therefore be changed as below.
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.p.is_var:
... ude.jacobian[c1.p.J_col()] = dh_mix_dpQ(c1.p.val_SI, b, c1.fluid_data)
... ude.jacobian[c1.p.J_col] = dh_mix_dpQ(c1.p.val_SI, b, c1.fluid_data)
... if c1.h.is_var:
... ude.jacobian[c1.h.J_col()] = -a
... ude.jacobian[c1.h.J_col] = -a
... if c2.p.is_var:
... ude.jacobian[c2.p.J_col()] = a - 1
... ude.jacobian[c2.p.J_col] = a - 1
>>> ude = UserDefinedEquation(
... 'my ude', my_ude, my_ude_deriv, [c1, c2], params={'a': 0.5, 'b': 1}
Expand Down
48 changes: 24 additions & 24 deletions src/tespy/components/combustion/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,10 @@ def mass_flow_deriv(self, k):
inl, outl = self._get_combustion_connections()
for i in inl:
if i.m.is_var:
self.jacobian[k, i.m.J_col()] = 1
self.jacobian[k, i.m.J_col] = 1

if outl[0].m.is_var:
self.jacobian[k, outl[0].m.J_col()] = -1
self.jacobian[k, outl[0].m.J_col] = -1

def combustion_pressure_func(self):
r"""
Expand Down Expand Up @@ -452,14 +452,14 @@ def combustion_pressure_deriv(self, k):
"""
inl, outl = self._get_combustion_connections()
if inl[0].p.is_var:
self.jacobian[k, inl[0].p.J_col()] = 1
self.jacobian[k, inl[0].p.J_col] = 1
if outl[0].p.is_var:
self.jacobian[k, outl[0].p.J_col()] = -1
self.jacobian[k, outl[0].p.J_col] = -1

if inl[1].p.is_var:
self.jacobian[k + 1, inl[1].p.J_col()] = 1
self.jacobian[k + 1, inl[1].p.J_col] = 1
if outl[0].p.is_var:
self.jacobian[k + 1, outl[0].p.J_col()] = -1
self.jacobian[k + 1, outl[0].p.J_col] = -1

def stoichiometry_func(self):
r"""
Expand Down Expand Up @@ -779,11 +779,11 @@ def stoichiometry_deriv(self, increment_filter, k):
for fluid, conn in itertools.product(self.fluid_eqs_list, conns):
eq_num = self.fluid_eqs_list.index(fluid)
if self.is_variable(conn.m, increment_filter):
self.jacobian[k + eq_num, conn.m.J_col()] = self.numeric_deriv(
self.jacobian[k + eq_num, conn.m.J_col] = self.numeric_deriv(
f, 'm', conn, fluid=fluid
)
for fluid_name in conn.fluid.is_var:
self.jacobian[k + eq_num, conn.fluid.J_col()[fluid_name]] = self.numeric_deriv(
self.jacobian[k + eq_num, conn.fluid.J_col[fluid_name]] = self.numeric_deriv(
f, fluid_name, conn, fluid=fluid
)

Expand Down Expand Up @@ -883,14 +883,14 @@ def energy_balance_deriv(self, increment_filter, k):
f = self.energy_balance_func
for c in self.inl + self.outl:
if self.is_variable(c.m, increment_filter):
self.jacobian[k, c.m.J_col()] = self.numeric_deriv(f, 'm', c)
self.jacobian[k, c.m.J_col] = self.numeric_deriv(f, 'm', c)
if self.is_variable(c.p, increment_filter):
self.jacobian[k, c.p.J_col()] = self.numeric_deriv(f, 'p', c)
self.jacobian[k, c.p.J_col] = self.numeric_deriv(f, 'p', c)
if self.is_variable(c.h, increment_filter):
if c == self.outl[0]:
self.jacobian[k, c.h.J_col()] = -c.m.val_SI
self.jacobian[k, c.h.J_col] = -c.m.val_SI
else:
self.jacobian[k, c.h.J_col()] = c.m.val_SI
self.jacobian[k, c.h.J_col] = c.m.val_SI

def lambda_func(self):
r"""
Expand Down Expand Up @@ -953,9 +953,9 @@ def lambda_deriv(self, increment_filter, k):
f = self.lambda_func
for conn in inl:
if self.is_variable(conn.m, increment_filter):
self.jacobian[k, conn.m.J_col()] = self.numeric_deriv(f, 'm', conn)
self.jacobian[k, conn.m.J_col] = self.numeric_deriv(f, 'm', conn)
for fluid in conn.fluid.is_var:
self.jacobian[k, conn.fluid.J_col()[fluid]] = self.numeric_deriv(f, fluid, conn)
self.jacobian[k, conn.fluid.J_col[fluid]] = self.numeric_deriv(f, fluid, conn)

def calc_lambda(self):
r"""
Expand Down Expand Up @@ -1057,18 +1057,18 @@ def ti_deriv(self, increment_filter, k):
deriv = 0
for f in self.fuel_list:
deriv -= i.fluid.val[f] * self.fuels[f]['LHV']
self.jacobian[k, i.m.J_col()] = deriv
self.jacobian[k, i.m.J_col] = deriv
for f in (self.fuel_list & i.fluid.is_var):
self.jacobian[k, i.fluid.J_col()[f]] = -i.m.val_SI * self.fuels[f]['LHV']
self.jacobian[k, i.fluid.J_col[f]] = -i.m.val_SI * self.fuels[f]['LHV']

o = outl[0]
if o.m.is_var:
deriv = 0
for f in self.fuel_list:
deriv += o.fluid.val[f] * self.fuels[f]['LHV']
self.jacobian[k, o.m.J_col()] = deriv
self.jacobian[k, o.m.J_col] = deriv
for f in (self.fuel_list & o.fluid.is_var):
self.jacobian[k, o.fluid.J_col()[f]] = o.m.val_SI * self.fuels[f]['LHV']
self.jacobian[k, o.fluid.J_col[f]] = o.m.val_SI * self.fuels[f]['LHV']

def calc_ti(self):
r"""
Expand Down Expand Up @@ -1162,14 +1162,14 @@ def bus_deriv(self, bus):
f = self.calc_bus_value
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)
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)
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 convergence_check(self):
r"""
Expand Down
4 changes: 2 additions & 2 deletions src/tespy/components/combustion/diabatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ def pr_deriv(self, increment_filter, k):
i = self.inl[0]
o = self.outl[0]
if self.is_variable(i.p):
self.jacobian[k, i.p.J_col()] = self.pr.val
self.jacobian[k, i.p.J_col] = self.pr.val
if self.is_variable(o.p):
self.jacobian[k, o.p.J_col()] = -1
self.jacobian[k, o.p.J_col] = -1

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

0 comments on commit 2a49e37

Please sign in to comment.