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:
@FlorianShepherd can you try this case? Is this reproducible?
@wangzhenassd I can confirm. I get this error:
```
  File "/home/florian/python_script/temp/dcbug.py", line 9, in <module>
    pp.rundcpp(net)
  File "/home/florian/pandapower/pandapower/run.py", line 267, in rundcpp
    _powerflow(net, **kwargs)
  File "/home/florian/pandapower/pandapower/powerflow.py", line 52, in _powerflow
    verify_results(net)
  File "/home/florian/pandapower/pandapower/results.py", line 73, in verify_results
    if len(net[element]) != len(net[res_element]):
KeyError: 'res_gen'
```
I'll try to find a fix

Resolving patch:
diff --git a/pandapower/results.py b/pandapower/results.py
--- 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
--- a/pandapower/test/loadflow/test_rundcpp.py
+++ b/pandapower/test/loadflow/test_rundcpp.py
@@ -8,6 +8,8 @@
 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
--- 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():