Skip to content

Commit

Permalink
Merge branch 'docs/#549-fix-old-jacobian-indices' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
fwitte committed Oct 13, 2024
2 parents 5e809bd + 56ea7a1 commit 9cd7c56
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
86 changes: 42 additions & 44 deletions docs/modules/ude.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,12 @@ inside the Jacobian of the :code:`UserDefinedEquation` and returns it:

- :code:`ude.jacobian` is a dictionary containing numpy arrays for every
connection required by the :code:`UserDefinedEquation`.
- derivatives to **mass flow** are placed in the first element of the numpy
array (**index 0**)
- derivatives to **pressure** are placed in the second element of the numpy
array (**index 1**)
- derivatives to **enthalpy** are placed in the third element of the numpy
array (**index 2**)
- derivatives to **fluid composition** are placed in the remaining elements
beginning at the fourth element of the numpy array (**indices 3:**)
- 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
derivative of the fluid composition towards a specific fluid
:code:`fluid_name`.

If we calculate the derivatives of our equation, it is easy to find, that only
derivatives to mass flow are not zero.
Expand All @@ -115,12 +113,12 @@ derivatives to mass flow are not zero.
.. code-block:: python
>>> def my_ude_deriv(ude):
... c0 = ude.conns[0]
... c1 = ude.conns[1]
... if c0.m.is_var:
... ude.jacobian[c0.m.J_col] = 1
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.m.is_var:
... ude.jacobian[c1.m.J_col] = -2 * ude.conns[1].m.val_SI
... 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
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 @@ -186,21 +184,21 @@ respectively to calculate the partial derivatives.
>>> from tespy.tools.fluid_properties import dT_mix_pdh
>>> def my_ude_deriv(ude):
... c0 = ude.conns[0]
... c1 = ude.conns[1]
... if c0.m.is_var:
... ude.jacobian[c0.m.J_col] = 1 / ude.conns[0].m.val_SI
... if c0.p.is_var:
... ude.jacobian[c0.p.J_col] = - 2 / ude.conns[0].p.val_SI
... T = c1.calc_T()
... 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
... if c1.p.is_var:
... ude.jacobian[c1.p.J_col] = (
... dT_mix_dph(c1.p.val_SI, c1.h.val_SI, c1.fluid_data, c1.mixing_rule)
... 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] = (
... dT_mix_dph(c2.p.val_SI, c2.h.val_SI, c2.fluid_data, c2.mixing_rule)
... * 0.5 / (T ** 0.5)
... )
... if c1.h.is_var:
... ude.jacobian[c1.h.J_col] = (
... dT_mix_pdh(c1.p.val_SI, c1.h.val_SI, c1.fluid_data, c1.mixing_rule)
... if c2.h.is_var:
... 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 @@ -214,16 +212,16 @@ for the above derivatives would therefore look like this:
.. code-block:: python
>>> def my_ude_deriv(ude):
... c0 = ude.conns[0]
... c1 = ude.conns[1]
... if c0.m.is_var:
... ude.jacobian[c0.m.J_col] = ude.numeric_deriv('m', c0)
... if c0.p.is_var:
... ude.jacobian[c0.p.J_col] = ude.numeric_deriv('p', c0)
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.m.is_var:
... 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)
... if c1.h.is_var:
... ude.jacobian[c1.h.J_col] = ude.numeric_deriv('h', c1)
... if c2.p.is_var:
... 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 = UserDefinedEquation('ude numerical', my_ude, my_ude_deriv, [c1, c2])
>>> nw.add_ude(ude)
Expand Down Expand Up @@ -268,24 +266,24 @@ instance must therefore be changed as below.
>>> def my_ude(ude):
... a = ude.params['a']
... b = ude.params['b']
... c0 = ude.conns[0]
... c1 = ude.conns[1]
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... return (
... a * (c1.h.val_SI - c0.h.val_SI) -
... (c1.h.val_SI - h_mix_pQ(c0.p.val_SI, b, c0.fluid_data))
... a * (c2.h.val_SI - c1.h.val_SI) -
... (c2.h.val_SI - h_mix_pQ(c1.p.val_SI, b, c1.fluid_data))
... )
>>> def my_ude_deriv(ude):
... a = ude.params['a']
... b = ude.params['b']
... c0 = ude.conns[0]
... c1 = ude.conns[1]
... if c0.p.is_var:
... ude.jacobian[c0.p.J_col] = dh_mix_dpQ(c0.p.val_SI, b, c0.fluid_data)
... if c0.h.is_var:
... ude.jacobian[c0.h.J_col] = -a
... c1 = ude.conns[0]
... c2 = ude.conns[1]
... if c1.p.is_var:
... ude.jacobian[c1.p.J_col] = a - 1
... 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
... if c2.p.is_var:
... 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
6 changes: 6 additions & 0 deletions docs/whats_new/v0-7-7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Bug Fixes
available in the namespace via the :code:`@component_registry` decorator
(`PR #536 <https://github.com/oemof/tespy/pull/536>`__).

Documentation
#############
- Update deprecated information on the indices of variables in the Jacobian of
a :code:`UserDefinedEquation`
(`PR #552 <https://github.com/oemof/tespy/pull/552>`__).

Contributors
############
- Francesco Witte (`@fwitte <https://github.com/fwitte>`__)

0 comments on commit 9cd7c56

Please sign in to comment.