Skip to content

Commit

Permalink
fix issue 125
Browse files Browse the repository at this point in the history
Signed-off-by: DONNOT Benjamin <[email protected]>
  • Loading branch information
BDonnot committed Nov 25, 2024
1 parent 4cf73c6 commit dbba375
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
10 changes: 4 additions & 6 deletions grid2op/Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,10 +724,10 @@ def get_line_status(self) -> np.ndarray:
:return: an array with the line status of each powerline
:rtype: np.array, dtype:bool
"""
cls = type(self)
topo_vect = self.get_topo_vect()
return (topo_vect[self.line_or_pos_topo_vect] >= 0) & (
topo_vect[self.line_ex_pos_topo_vect] >= 0
)
return ((topo_vect[cls.line_or_pos_topo_vect] >= 0) &
(topo_vect[cls.line_ex_pos_topo_vect] >= 0))

def get_line_flow(self) -> np.ndarray:
"""
Expand Down Expand Up @@ -1090,10 +1090,8 @@ def _runpf_with_diverging_exception(self, is_dc : bool) -> Optional[Exception]:

# Check if loads/gens have been detached and if this is allowed, otherwise raise an error
# .. versionadded:: 1.11.0
topo_vect = self.get_topo_vect()

topo_vect = self.get_topo_vect()
load_buses = topo_vect[self.load_pos_topo_vect]

if not self.detachment_is_allowed and (load_buses == -1).any():
raise Grid2OpException(f"One or more loads were detached before powerflow in Backend {type(self).__name__}"
"but this is not allowed or not supported (Game Over)")
Expand Down
50 changes: 30 additions & 20 deletions grid2op/Backend/pandaPowerBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ def reset(self,
warnings.simplefilter("ignore", FutureWarning)
self._grid = copy.deepcopy(self.__pp_backend_initial_grid)
self._reset_all_nan()
self._topo_vect[:] = self._get_topo_vect()
self._get_topo_vect()
self.line_status[:] = self._get_line_status()
self.comp_time = 0.0

def load_grid(self,
Expand Down Expand Up @@ -717,6 +718,8 @@ def _init_private_attrs(self) -> None:
)

self._compute_pos_big_topo()

self._topo_vect = np.full(self.dim_topo, fill_value=-1, dtype=dt_int)

# utilities for imeplementing apply_action
self._corresp_name_fun = {}
Expand Down Expand Up @@ -805,7 +808,7 @@ def _init_private_attrs(self) -> None:
self.gen_theta = np.full(self.n_gen, fill_value=np.NaN, dtype=dt_float)
self.storage_theta = np.full(self.n_storage, fill_value=np.NaN, dtype=dt_float)

self._topo_vect = self._get_topo_vect()
self._get_topo_vect()
self.tol = 1e-5 # this is NOT the pandapower tolerance !!!! this is used to check if a storage unit
# produce / absorbs anything

Expand All @@ -824,7 +827,7 @@ def storage_deact_for_backward_comaptibility(self) -> None:
self.storage_p = np.full(cls.n_storage, dtype=dt_float, fill_value=np.NaN)
self.storage_q = np.full(cls.n_storage, dtype=dt_float, fill_value=np.NaN)
self.storage_v = np.full(cls.n_storage, dtype=dt_float, fill_value=np.NaN)
self._topo_vect = self._get_topo_vect()
self._get_topo_vect()

def _convert_id_topo(self, id_big_topo):
"""
Expand Down Expand Up @@ -1083,6 +1086,7 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
in case of "do nothing" action applied.
"""
try:
self._get_topo_vect() # do that before any possible divergence
self._aux_runpf_pp(is_dc)
cls = type(self)
# if a connected bus has a no voltage, it's a divergence (grid was not connected)
Expand Down Expand Up @@ -1128,7 +1132,7 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
):
self.load_v[l_id] = self.prod_v[g_id]
break

self.line_status[:] = self._get_line_status()
# I retrieve the data once for the flows, so has to not re read multiple dataFrame
self.p_or[:] = self._aux_get_line_info("p_from_mw", "p_hv_mw")
Expand Down Expand Up @@ -1183,11 +1187,10 @@ def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
self.storage_q[deact_storage] = 0.0
self.storage_v[deact_storage] = 0.0
self._grid.storage["in_service"].values[deact_storage] = False

self._topo_vect[:] = self._get_topo_vect()
if not self._grid.converged:
raise pp.powerflow.LoadflowNotConverged("Divergence without specific reason (self._grid.converged is False)")
self.div_exception = None
self._get_topo_vect() # do that after (maybe useless)
return True, None

except pp.powerflow.LoadflowNotConverged as exc_:
Expand Down Expand Up @@ -1222,6 +1225,10 @@ def _reset_all_nan(self) -> None:
self.load_theta[:] = np.NaN
self.gen_theta[:] = np.NaN
self.storage_theta[:] = np.NaN
self._topo_vect.flags.writeable = True
self._topo_vect[:] = -1
self._topo_vect.flags.writeable = False
self.line_status[:] = False

def copy(self) -> "PandaPowerBackend":
"""
Expand Down Expand Up @@ -1383,8 +1390,10 @@ def _disconnect_line(self, id_):
self._grid.line.iloc[id_, self._in_service_line_col_id] = False
else:
self._grid.trafo.iloc[id_ - self._number_true_line, self._in_service_trafo_col_id] = False
self._topo_vect.flags.writeable = True
self._topo_vect[self.line_or_pos_topo_vect[id_]] = -1
self._topo_vect[self.line_ex_pos_topo_vect[id_]] = -1
self._topo_vect.flags.writeable = False
self.line_status[id_] = False

def _reconnect_line(self, id_):
Expand All @@ -1399,29 +1408,30 @@ def get_topo_vect(self) -> np.ndarray:

def _get_topo_vect(self):
cls = type(self)
res = np.full(cls.dim_topo, fill_value=np.iinfo(dt_int).max, dtype=dt_int)


# lines / trafo
line_status = self.get_line_status()
self._topo_vect.flags.writeable = True
glob_bus_or = np.concatenate((self._grid.line["from_bus"].values, self._grid.trafo["hv_bus"].values))
res[cls.line_or_pos_topo_vect] = cls.global_bus_to_local(glob_bus_or, cls.line_or_to_subid)
res[cls.line_or_pos_topo_vect[~line_status]] = -1
self._topo_vect[cls.line_or_pos_topo_vect] = cls.global_bus_to_local(glob_bus_or, cls.line_or_to_subid)
self._topo_vect[cls.line_or_pos_topo_vect[~line_status]] = -1
glob_bus_ex = np.concatenate((self._grid.line["to_bus"].values, self._grid.trafo["lv_bus"].values))
res[cls.line_ex_pos_topo_vect] = cls.global_bus_to_local(glob_bus_ex, cls.line_ex_to_subid)
res[cls.line_ex_pos_topo_vect[~line_status]] = -1
self._topo_vect[cls.line_ex_pos_topo_vect] = cls.global_bus_to_local(glob_bus_ex, cls.line_ex_to_subid)
self._topo_vect[cls.line_ex_pos_topo_vect[~line_status]] = -1
# load, gen
load_status = self._grid.load["in_service"].values
res[cls.load_pos_topo_vect] = cls.global_bus_to_local(self._grid.load["bus"].values, cls.load_to_subid)
res[cls.load_pos_topo_vect[~load_status]] = -1
self._topo_vect[cls.load_pos_topo_vect] = cls.global_bus_to_local(self._grid.load["bus"].values, cls.load_to_subid)
self._topo_vect[cls.load_pos_topo_vect[~load_status]] = -1
gen_status = self._grid.gen["in_service"].values
res[cls.gen_pos_topo_vect] = cls.global_bus_to_local(self._grid.gen["bus"].values, cls.gen_to_subid)
res[cls.gen_pos_topo_vect[~gen_status]] = -1
self._topo_vect[cls.gen_pos_topo_vect] = cls.global_bus_to_local(self._grid.gen["bus"].values, cls.gen_to_subid)
self._topo_vect[cls.gen_pos_topo_vect[~gen_status]] = -1
# storage
if cls.n_storage:
storage_status = self._grid.storage["in_service"].values
res[cls.storage_pos_topo_vect] = cls.global_bus_to_local(self._grid.storage["bus"].values, cls.storage_to_subid)
res[cls.storage_pos_topo_vect[~storage_status]] = -1
return res
storage_status = 1 * self._grid.storage["in_service"].values
self._topo_vect[cls.storage_pos_topo_vect] = cls.global_bus_to_local(self._grid.storage["bus"].values, cls.storage_to_subid)
self._topo_vect[cls.storage_pos_topo_vect[~storage_status]] = -1
self._topo_vect.flags.writeable = False
return self._topo_vect

def _gens_info(self):
prod_p = self.cst_1 * self._grid.res_gen["p_mw"].values.astype(dt_float)
Expand Down
7 changes: 5 additions & 2 deletions grid2op/tests/helper_path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ class MakeBackend(ABC, HelperTests):
def make_backend(self, detailed_infos_for_cascading_failures=False) -> Backend:
pass

def make_backend_with_glue_code(self, detailed_infos_for_cascading_failures=False, extra_name="",
n_busbar=2, allow_detachment=False) -> Backend:
def make_backend_with_glue_code(self,
detailed_infos_for_cascading_failures=False,
extra_name="",
n_busbar=2,
allow_detachment=False) -> Backend:
Backend._clear_class_attribute()
bk = self.make_backend(detailed_infos_for_cascading_failures=detailed_infos_for_cascading_failures)
type(bk)._clear_grid_dependant_class_attributes()
Expand Down
7 changes: 5 additions & 2 deletions grid2op/tests/test_PandaPowerBackendDefaultFunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def get_topo_vect(self):
"""
otherwise there are some infinite recursions
"""
res = np.full(self.dim_topo, fill_value=-1, dtype=dt_int)
self._topo_vect.flags.writeable = True
res = self._topo_vect

line_status = np.concatenate(
(
Expand Down Expand Up @@ -112,13 +113,14 @@ def get_topo_vect(self):
for bus_id in self._grid.gen["bus"].values:
res[self.gen_pos_topo_vect[i]] = 1 if bus_id == self.gen_to_subid[i] else 2
i += 1

res[self.gen_pos_topo_vect[~self._grid.gen["in_service"]]] = -1
i = 0
for bus_id in self._grid.load["bus"].values:
res[self.load_pos_topo_vect[i]] = (
1 if bus_id == self.load_to_subid[i] else 2
)
i += 1
res[self.load_pos_topo_vect[~self._grid.load["in_service"]]] = -1

# do not forget storage units !
i = 0
Expand All @@ -127,6 +129,7 @@ def get_topo_vect(self):
1 if bus_id == self.storage_to_subid[i] else 2
)
i += 1
self._topo_vect.flags.writeable = False
return res


Expand Down

0 comments on commit dbba375

Please sign in to comment.