From d0d572036c81b68f82d96b8465fc867352b73bb1 Mon Sep 17 00:00:00 2001 From: Francesco Witte Date: Sun, 13 Oct 2024 08:52:50 +0200 Subject: [PATCH 1/3] Correct the derivative placement description --- docs/modules/ude.rst | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/modules/ude.rst b/docs/modules/ude.rst index cf6ed80d9..0c3865402 100644 --- a/docs/modules/ude.rst +++ b/docs/modules/ude.rst @@ -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. From e10ad1600ad0e2391e78fee19ead978b1f5b3f51 Mon Sep 17 00:00:00 2001 From: Francesco Witte Date: Sun, 13 Oct 2024 08:53:06 +0200 Subject: [PATCH 2/3] Make variable naming consistent with text --- docs/modules/ude.rst | 72 ++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/docs/modules/ude.rst b/docs/modules/ude.rst index 0c3865402..ab85d49c5 100644 --- a/docs/modules/ude.rst +++ b/docs/modules/ude.rst @@ -113,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: @@ -184,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) ... ) @@ -212,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) @@ -266,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} From 56ea7a1ed3fc1b76914f74f9b6524a99a1e3092b Mon Sep 17 00:00:00 2001 From: Francesco Witte Date: Sun, 13 Oct 2024 08:56:08 +0200 Subject: [PATCH 3/3] Update whats new --- docs/whats_new/v0-7-7.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/whats_new/v0-7-7.rst b/docs/whats_new/v0-7-7.rst index 03b191424..d162ae9c9 100644 --- a/docs/whats_new/v0-7-7.rst +++ b/docs/whats_new/v0-7-7.rst @@ -8,6 +8,12 @@ Bug Fixes available in the namespace via the :code:`@component_registry` decorator (`PR #536 `__). +Documentation +############# +- Update deprecated information on the indices of variables in the Jacobian of + a :code:`UserDefinedEquation` + (`PR #552 `__). + Contributors ############ - Francesco Witte (`@fwitte `__)