Repository: e2nIEE/pandapower

Issue:
Bug in rundcpp failed to initialize result table
```python
import pandapower as pp
import pandapower.networks as nw

net = nw.case57()
pp.rundcpp(net)    
net.pop("res_gen")

# Problem by executing this
pp.rundcpp(net)

# No problem doing this
pp.runpp(net) 
```

Can anyone recreate this? I tries pp 2.2.2, pp 2.3.0.


Discussion:

Patch:
diff --git a/pandapower/results.py b/pandapower/results.py
index 99b424df..5a6a5d97 100644
--- a/pandapower/results.py
+++ b/pandapower/results.py
@@ -70,7 +70,15 @@ def verify_results(net, mode="pf"):
     suffix = suffix_mode.get(mode, None)
     for element in elements:
         res_element, res_empty_element = get_result_tables(element, suffix)
-        if len(net[element]) != len(net[res_element]):
+
+        index_equal = False if res_element not in net else net[element].index.equals(net[res_element].index)
+        if not index_equal:
+            if net["_options"]["init_results"] and element == "bus":
+                # if the indices of bus and res_bus are not equal, but init_results is set, the voltage vector
+                # is wrong. A UserWarning is raised in this case. For all other elements the result table is emptied.
+                raise UserWarning("index of result table '{}' is not equal to the element table '{}'. The init result"
+                                  " option may lead to a non-converged power flow.".format(res_element, element))
+            # init result table for
             init_element(net, element)
             if element == "bus":
                 net._options["init_vm_pu"] = "auto"


Test patch:
diff --git a/pandapower/test/loadflow/test_rundcpp.py b/pandapower/test/loadflow/test_rundcpp.py
index 9625993b..7b720379 100644
--- a/pandapower/test/loadflow/test_rundcpp.py
+++ b/pandapower/test/loadflow/test_rundcpp.py
@@ -8,6 +8,8 @@ import numpy as np
 import pytest
 
 import pandapower as pp
+import pandapower.networks as nw
+import copy
 from pandapower.auxiliary import _check_connectivity, _add_ppc_options
 from pandapower.pd2ppc import _pd2ppc
 from pandapower.test.consistency_checks import rundcpp_with_consistency_checks
@@ -100,5 +102,14 @@ def test_single_bus_network():
     assert net.converged
 
 
+def test_missing_gen():
+    net = nw.case4gs()
+    pp.rundcpp(net)
+    res_gen = copy.deepcopy(net.res_gen.values)
+    net.pop("res_gen")
+    pp.rundcpp(net)
+    assert np.allclose(net.res_gen.values, res_gen, equal_nan=True)
+
+
 if __name__ == "__main__":
     pytest.main([__file__, "-xs"])
diff --git a/pandapower/test/loadflow/test_runpp.py b/pandapower/test/loadflow/test_runpp.py
index 60e4e1c4..e0f3bbb5 100644
--- a/pandapower/test/loadflow/test_runpp.py
+++ b/pandapower/test/loadflow/test_runpp.py
@@ -865,7 +865,11 @@ def test_add_element_and_init_results():
     pp.create_bus(net, vn_kv=20.)
     pp.create_line(net, from_bus=2, to_bus=3, length_km=1, name="new line" + str(1),
                    std_type="NAYY 4x150 SE")
-    pp.runpp(net, init="results")
+    try:
+        pp.runpp(net, init="results")
+        assert False
+    except UserWarning:
+        pass
 
 
 def test_pp_initialization():