Skip to content

Commit

Permalink
should fix all issues, need to make the AAA test suite better now
Browse files Browse the repository at this point in the history
Signed-off-by: DONNOT Benjamin <[email protected]>
  • Loading branch information
BDonnot committed Nov 26, 2024
1 parent 09d1a65 commit d6bf9a8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
6 changes: 3 additions & 3 deletions grid2op/Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def __init__(self,
#: You should not worry about the class attribute of the backend in :func:`Backend.apply_action`
self.n_busbar_per_sub: int = DEFAULT_N_BUSBAR_PER_SUB

# .. versionadded: 1.11.0
self._missing_detachment_support:bool = True
self.detachment_is_allowed:bool = DEFAULT_ALLOW_DETACHMENT
#: .. versionadded: 1.11.0
self._missing_detachment_support : bool = True
self.detachment_is_allowed : bool = DEFAULT_ALLOW_DETACHMENT

def can_handle_more_than_2_busbar(self):
"""
Expand Down
37 changes: 31 additions & 6 deletions grid2op/Backend/pandaPowerBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ def reset(self,
warnings.simplefilter("ignore", FutureWarning)
self._grid = copy.deepcopy(self.__pp_backend_initial_grid)
self._reset_all_nan()
self._get_line_status()
self._get_topo_vect()
self.line_status[:] = self._get_line_status()
self.comp_time = 0.0

def load_grid(self,
Expand Down Expand Up @@ -771,7 +771,6 @@ def _init_private_attrs(self) -> None:
self.q_ex = np.full(self.n_line, dtype=dt_float, fill_value=np.NaN)
self.v_ex = np.full(self.n_line, dtype=dt_float, fill_value=np.NaN)
self.a_ex = np.full(self.n_line, dtype=dt_float, fill_value=np.NaN)
self.line_status = np.full(self.n_line, dtype=dt_bool, fill_value=np.NaN)
self.load_p = np.full(self.n_load, dtype=dt_float, fill_value=np.NaN)
self.load_q = np.full(self.n_load, dtype=dt_float, fill_value=np.NaN)
self.load_v = np.full(self.n_load, dtype=dt_float, fill_value=np.NaN)
Expand All @@ -782,6 +781,9 @@ def _init_private_attrs(self) -> None:
self.storage_q = np.full(self.n_storage, dtype=dt_float, fill_value=np.NaN)
self.storage_v = np.full(self.n_storage, dtype=dt_float, fill_value=np.NaN)
self._nb_bus_before = None

self.line_status = np.full(self.n_line, dtype=dt_bool, fill_value=np.NaN)
self.line_status.flags.writeable = False

# store the topoid -> objid
self._init_topoid_objid()
Expand Down Expand Up @@ -1084,8 +1086,14 @@ 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
# as pandapower does not modify the topology or the status of
# powerline, then we can compute the topology (and the line status)
# at the beginning
# This is also interesting in case of divergence :-)
self._get_line_status()
self._get_topo_vect()
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)
if self._grid.res_bus.loc[self._grid.bus["in_service"]]["va_degree"].isnull().any():
Expand Down Expand Up @@ -1131,7 +1139,6 @@ 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")
self.q_or[:] = self._aux_get_line_info("q_from_mvar", "q_hv_mvar")
Expand Down Expand Up @@ -1225,8 +1232,9 @@ def _reset_all_nan(self) -> None:
self._topo_vect.flags.writeable = True
self._topo_vect[:] = -1
self._topo_vect.flags.writeable = False
self.line_status.flags.writeable = True
self.line_status[:] = False

self.line_status.flags.writeable = False
def copy(self) -> "PandaPowerBackend":
"""
INTERNAL
Expand Down Expand Up @@ -1372,12 +1380,15 @@ def get_line_status(self) -> np.ndarray:
return self.line_status

def _get_line_status(self):
return np.concatenate(
self.line_status.flags.writeable = True
self.line_status[:] = np.concatenate(
(
self._grid.line["in_service"].values,
self._grid.trafo["in_service"].values,
)
).astype(dt_bool)
self.line_status.flags.writeable = False
return self.line_status

def get_line_flow(self) -> np.ndarray:
return self.a_or
Expand All @@ -1391,19 +1402,33 @@ def _disconnect_line(self, id_):
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.flags.writeable = True
self.line_status[id_] = False
self.line_status.flags.writeable = False

def _reconnect_line(self, id_):
if id_ < self._number_true_line:
self._grid.line.iloc[id_, self._in_service_line_col_id] = True
else:
self._grid.trafo.iloc[id_ - self._number_true_line, self._in_service_trafo_col_id] = True
self.line_status.flags.writeable = True
self.line_status[id_] = True
self.line_status.flags.writeable = False

def get_topo_vect(self) -> np.ndarray:
return self._topo_vect

def _get_topo_vect(self):
"""
.. danger::
you should have called `self._get_line_status` before otherwise it might
not behave correctly !
Returns
-------
_type_
_description_
"""
cls = type(self)

# lines / trafo
Expand Down
26 changes: 21 additions & 5 deletions grid2op/tests/aaa_test_backend_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from grid2op.dtypes import dt_int
from grid2op.tests.helper_path_test import HelperTests, MakeBackend, PATH_DATA
from grid2op.Exceptions import BackendError, Grid2OpException
from grid2op.Space import DEFAULT_ALLOW_DETACHMENT, DEFAULT_N_BUSBAR_PER_SUB


class AAATestBackendAPI(MakeBackend):
Expand All @@ -39,21 +40,36 @@ def aux_get_env_name(self):
"""do not run nor modify ! (used for this test class only)"""
return "BasicTest_load_grid_" + type(self).__name__

def aux_make_backend(self, n_busbar=2) -> Backend:
def aux_make_backend(self,
n_busbar=DEFAULT_N_BUSBAR_PER_SUB,
allow_detachment=DEFAULT_ALLOW_DETACHMENT,
extra_name=None) -> Backend:
"""do not run nor modify ! (used for this test class only)"""
backend = self.make_backend_with_glue_code(n_busbar=n_busbar)

if extra_name is None:
extra_name = self.aux_get_env_name()
backend = self.make_backend_with_glue_code(n_busbar=n_busbar,
allow_detachment=allow_detachment,
extra_name=extra_name)
backend.load_grid(self.get_path(), self.get_casefile())
backend.load_redispacthing_data("tmp") # pretend there is no generator
backend.load_storage_data(self.get_path())
env_name = self.aux_get_env_name()
backend.env_name = env_name
backend.assert_grid_correct()
backend.assert_grid_correct()
return backend

def test_00create_backend(self):
"""Tests the backend can be created (not integrated in a grid2op environment yet)"""
self.skip_if_needed()
backend = self.make_backend_with_glue_code()
if not backend._missing_two_busbars_support_info:
warnings.warn("You should call either `self.can_handle_more_than_2_busbar()` "
"or `self.cannot_handle_more_than_2_busbar()` in the `load_grid` "
"method of your backend. Please refer to documentation for more information.")

if not backend._missing_detachment_support:
warnings.warn("You should call either `self.can_handle_detachment()` "
"or `self.cannot_handle_detachment()` in the `load_grid` "
"method of your backend. Please refer to documentation for more information.")

def test_01load_grid(self):
"""Tests the grid can be loaded (supposes that your backend can read the grid.json in educ_case14_storage)*
Expand Down
2 changes: 1 addition & 1 deletion grid2op/tests/helper_path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def make_backend_with_glue_code(self,
type(bk)._clear_grid_dependant_class_attributes()
type(bk).set_env_name(type(self).__name__ + extra_name)
type(bk).set_n_busbar_per_sub(n_busbar)
type(bk)._allow_detachment = allow_detachment
type(bk).set_detachment_is_allowed(allow_detachment)
return bk

def get_path(self) -> str:
Expand Down

0 comments on commit d6bf9a8

Please sign in to comment.