Repository: oemof/tespy

Issue:
The Ref class imposes negative value for delta
... which is unintuitive.

Comments:

Patch:
diff --git a/docs/whats_new.rst b/docs/whats_new.rst
index f03b829f..7f5718e0 100644
--- a/docs/whats_new.rst
+++ b/docs/whats_new.rst
@@ -3,6 +3,7 @@ What's New
 
 Discover noteable new features and improvements in each release
 
+.. include::  whats_new/v0-7-2.rst
 .. include::  whats_new/v0-7-1.rst
 .. include::  whats_new/v0-7-0.rst
 .. include::  whats_new/v0-6-3.rst
diff --git a/docs/whats_new/v0-7-2.rst b/docs/whats_new/v0-7-2.rst
new file mode 100644
index 00000000..ca1c1c59
--- /dev/null
+++ b/docs/whats_new/v0-7-2.rst
@@ -0,0 +1,12 @@
+v0.7.2 - Under development
+++++++++++++++++++++++++++
+
+Bug Fixes
+#########
+- The `delta` value of the :py:class:`tespy.connections.connection.Ref` class
+  was oriented with the wrong sign. A positive delta lead to a negative value.
+  Fixed in (`PR #459 <https://github.com/oemof/tespy/pull/459>`__).
+
+Contributors
+############
+- Francesco Witte (`@fwitte <https://github.com/fwitte>`__)
diff --git a/src/tespy/connections/connection.py b/src/tespy/connections/connection.py
index ceb64a66..459bcd14 100644
--- a/src/tespy/connections/connection.py
+++ b/src/tespy/connections/connection.py
@@ -726,7 +726,7 @@ class Connection:
         ref = self.get_attr(f"{variable}_ref").ref
         self.residual[k] = (
             self.get_attr(variable).val_SI
-            - ref.obj.get_attr(variable).val_SI * ref.factor + ref.delta_SI
+            - (ref.obj.get_attr(variable).val_SI * ref.factor + ref.delta_SI)
         )
 
     def primary_ref_deriv(self, k, **kwargs):
@@ -761,7 +761,7 @@ class Connection:
     def T_ref_func(self, k, **kwargs):
         ref = self.T_ref.ref
         self.residual[k] = (
-            self.calc_T() - ref.obj.calc_T() * ref.factor + ref.delta_SI
+            self.calc_T() - (ref.obj.calc_T() * ref.factor + ref.delta_SI)
         )
 
     def T_ref_deriv(self, k, **kwargs):
@@ -810,7 +810,7 @@ class Connection:
         ref = self.v_ref.ref
         self.residual[k] = (
             self.calc_vol(T0=self.T.val_SI) * self.m.val_SI
-            - ref.obj.calc_vol(T0=ref.obj.T.val_SI) * ref.obj.m.val_SI * ref.factor + ref.delta_SI
+            - (ref.obj.calc_vol(T0=ref.obj.T.val_SI) * ref.obj.m.val_SI * ref.factor + ref.delta_SI)
         )
 
     def v_ref_deriv(self, k, **kwargs):
diff --git a/src/tespy/tools/helpers.py b/src/tespy/tools/helpers.py
index e177474e..d92ebf04 100644
--- a/src/tespy/tools/helpers.py
+++ b/src/tespy/tools/helpers.py
@@ -628,7 +628,7 @@ def newton(func, deriv, params, y, **kwargs):
             logger.debug(msg)
 
             break
-        if tol_mode == 'abs':
+        if tol_mode == 'abs' or y == 0:
             expr = abs(res) >= tol_abs
         elif tol_mode == 'rel':
             expr = abs(res / y) >= tol_rel
@@ -680,7 +680,7 @@ def newton_with_kwargs(
             logger.debug(msg)
 
             break
-        if tol_mode == 'abs':
+        if tol_mode == 'abs' or target_value == 0:
             expr = abs(residual) >= tol_abs
         elif tol_mode == 'rel':
             expr = abs(residual / target_value) >= tol_rel


Test patch:
diff --git a/tests/test_connections.py b/tests/test_connections.py
index acc620a2..6d35bdf8 100644
--- a/tests/test_connections.py
+++ b/tests/test_connections.py
@@ -58,7 +58,7 @@ class TestConnections:
         c2.set_attr(v=Ref(c1, 2, 10))
         self.nw.solve('design')
 
-        v_expected = round(c1.v.val * 2 - 10, 4)
+        v_expected = round(c1.v.val * 2 + 10, 4)
         v_is = round(c2.v.val, 4)
         msg = (
             'The mass flow of the connection 2 should be equal to '
@@ -87,7 +87,7 @@ class TestConnections:
         c2.set_attr(T=Ref(c1, 1.5, -75))
         self.nw.solve('design')
 
-        T_expected = round(convert_from_SI("T", c1.T.val_SI * 1.5, c1.T.unit) + 75, 4)
+        T_expected = round(convert_from_SI("T", c1.T.val_SI * 1.5, c1.T.unit) - 75, 4)
         T_is = round(c2.T.val, 4)
         msg = (
             'The temperature of the connection 2 should be equal to '
@@ -116,7 +116,7 @@ class TestConnections:
         c2.set_attr(m=Ref(c1, 2, -0.5))
         self.nw.solve('design')
 
-        m_expected = round(convert_from_SI("m", c1.m.val_SI * 2, c1.m.unit) + 0.5, 4)
+        m_expected = round(convert_from_SI("m", c1.m.val_SI * 2, c1.m.unit) - 0.5, 4)
         m_is = round(c2.m.val, 4)
         msg = (
             'The mass flow of the connection 2 should be equal to '