@@ -56,36 +56,14 @@ def _aux_runpf_pp(self, is_dc: bool):
5656 warnings .filterwarnings ("ignore" , category = RuntimeWarning )
5757 warnings .filterwarnings ("ignore" , category = DeprecationWarning )
5858 self ._pf_init = "dc"
59- # nb_bus = self.get_nb_active_bus()
60- # if self._nb_bus_before is None:
61- # self._pf_init = "dc"
62- # elif nb_bus == self._nb_bus_before:
63- # self._pf_init = "results"
64- # else:
65- # self._pf_init = "auto"
66-
67- if (~ self ._grid .load ["in_service" ]).any ():
68- # TODO see if there is a better way here -> do not handle this here, but rather in Backend._next_grid_state
69- raise pp .powerflow .LoadflowNotConverged ("Disconnected load: for now grid2op cannot handle properly"
70- " disconnected load. If you want to disconnect one, say it"
71- " consumes 0. instead. Please check loads: "
72- f"{ (~ self ._grid .load ['in_service' ].values ).nonzero ()[0 ]} "
73- )
74- if (~ self ._grid .gen ["in_service" ]).any ():
75- # TODO see if there is a better way here -> do not handle this here, but rather in Backend._next_grid_state
76- raise pp .powerflow .LoadflowNotConverged ("Disconnected gen: for now grid2op cannot handle properly"
77- " disconnected generators. If you want to disconnect one, say it"
78- " produces 0. instead. Please check generators: "
79- f"{ (~ self ._grid .gen ['in_service' ].values ).nonzero ()[0 ]} "
80- )
8159 try :
8260 if is_dc :
83- #TODO: Call pm function here too
61+ # TODO: Could call PwMd function here too, but Ben says DC will be same
8462 pp .rundcpp (self ._grid , check_connectivity = True , init = "flat" )
8563
8664 # if I put check_connectivity=False then the test AAATestBackendAPI.test_22_islanded_grid_make_divergence
8765 # does not pass
88-
66+
8967 # if dc i start normally next time i call an ac powerflow
9068 self ._nb_bus_before = None
9169 else :
@@ -130,14 +108,20 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
130108 in case of "do nothing" action applied.
131109 """
132110 try :
111+ # as pandapower does not modify the topology or the status of
112+ # powerline, then we can compute the topology (and the line status)
113+ # at the beginning
114+ # This is also interesting in case of divergence :-)
115+ self ._get_line_status ()
116+ self ._get_topo_vect ()
133117 self ._aux_runpf_pp (is_dc )
134- cls = type (self )
118+ cls = type (self )
135119 # if a connected bus has a no voltage, it's a divergence (grid was not connected)
136120 if self ._grid .res_bus .loc [self ._grid .bus ["in_service" ]]["va_degree" ].isnull ().any ():
137121 buses_ko = self ._grid .res_bus .loc [self ._grid .bus ["in_service" ]]["va_degree" ].isnull ()
138122 buses_ko = buses_ko .values .nonzero ()[0 ]
139123 raise pp .powerflow .LoadflowNotConverged (f"Isolated bus, check buses { buses_ko } with `env.backend._grid.res_bus.iloc[{ buses_ko } , :]`" )
140-
124+
141125 (
142126 self .prod_p [:],
143127 self .prod_q [:],
@@ -150,13 +134,8 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
150134 self .load_v [:],
151135 self .load_theta [:],
152136 ) = self ._loads_info ()
153-
154- if not is_dc :
155- if not np .isfinite (self .load_v ).all ():
156- # TODO see if there is a better way here
157- # some loads are disconnected: it's a game over case!
158- raise pp .powerflow .LoadflowNotConverged (f"Isolated load: check loads { np .isfinite (self .load_v ).nonzero ()[0 ]} " )
159- else :
137+
138+ if is_dc :
160139 # fix voltages magnitude that are always "nan" for dc case
161140 # self._grid.res_bus["vm_pu"] is always nan when computed in DC
162141 self .load_v [:] = self .load_pu_to_kv # TODO
@@ -175,8 +154,8 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
175154 ):
176155 self .load_v [l_id ] = self .prod_v [g_id ]
177156 break
178-
179- self . line_status [:] = self . _get_line_status ()
157+ self . load_v [ ~ self . _grid . load [ "in_service" ]] = 0.
158+
180159 # I retrieve the data once for the flows, so has to not re read multiple dataFrame
181160 self .p_or [:] = self ._aux_get_line_info ("p_from_mw" , "p_hv_mw" )
182161 self .q_or [:] = self ._aux_get_line_info ("q_from_mvar" , "q_hv_mvar" )
@@ -222,19 +201,21 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
222201 self .storage_theta [:],
223202 ) = self ._storages_info ()
224203 deact_storage = ~ np .isfinite (self .storage_v )
225- if (np .abs (self .storage_p [deact_storage ]) > self .tol ).any ():
226- raise pp .powerflow .LoadflowNotConverged (
227- "Isolated storage set to absorb / produce something"
228- )
229204 self .storage_p [deact_storage ] = 0.0
230205 self .storage_q [deact_storage ] = 0.0
231206 self .storage_v [deact_storage ] = 0.0
232207 self ._grid .storage ["in_service" ].values [deact_storage ] = False
233208
234- self ._topo_vect [:] = self ._get_topo_vect ()
235209 if getattr (self ._grid , 'converged' , False ) and getattr (self ._grid , 'OPF_converged' , False ):
236210 raise pp .powerflow .LoadflowNotConverged ("Divergence without specific reason (self._grid.converged and self._grid.OPF_converged both are False)" )
237211 self .div_exception = None
212+ if is_dc :
213+ # pandapower apparently does not set 0 for q in DC...
214+ self .prod_q [:] = 0.
215+ self .load_q [:] = 0.
216+ self .storage_q [:] = 0.
217+ self .q_or [:] = 0.
218+ self .q_ex [:] = 0.
238219 return True , None
239220
240221 except pp .powerflow .LoadflowNotConverged as exc_ :
0 commit comments