From 18e6c14b7d71dfc258c431375f4a11e977dc58c8 Mon Sep 17 00:00:00 2001 From: Ovi T Date: Fri, 3 May 2024 20:53:55 +0200 Subject: [PATCH 1/3] expose time in SIL's broker --- vessim/sil.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vessim/sil.py b/vessim/sil.py index e87bfa3..518e6df 100644 --- a/vessim/sil.py +++ b/vessim/sil.py @@ -35,6 +35,7 @@ def __init__(self, data_pipe_out: Connection, events_pipe_in: Connection): self._actor_infos_ts: List[Tuple[DatetimeLike, Dict]] = [] self._p_delta_ts: List[Tuple[DatetimeLike, float]] = [] self._e_delta_ts: List[Tuple[DatetimeLike, float]] = [] + self._time: Optional[DatetimeLike] = None self._microgrid: Optional[Microgrid] = None self._actor_infos: Dict[str, Dict] = {} self._p_delta: float = 0 @@ -45,6 +46,7 @@ def __init__(self, data_pipe_out: Connection, events_pipe_in: Connection): def _recv_data(self) -> None: while True: time, data = self._data_pipe_out.recv() + self._time = time self._microgrid = data["microgrid"] self._actor_infos = data["actor_infos"] self._p_delta = data["p_delta"] @@ -66,6 +68,10 @@ def set_event(self, category: str, value: Any) -> None: } ) + @property + def current_time(self) -> DatetimeLike | None: + return self._time + @property def microgrid(self) -> Microgrid | None: return self._microgrid From 1466d42dc3c1edc336650efb640860c88a87d85c Mon Sep 17 00:00:00 2001 From: Marvin Steinke <48684343+marvin-steinke@users.noreply.github.com> Date: Mon, 13 May 2024 15:45:38 +0200 Subject: [PATCH 2/3] `current_time` -> `time` --- vessim/sil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vessim/sil.py b/vessim/sil.py index 518e6df..4850b45 100644 --- a/vessim/sil.py +++ b/vessim/sil.py @@ -69,7 +69,7 @@ def set_event(self, category: str, value: Any) -> None: ) @property - def current_time(self) -> DatetimeLike | None: + def time(self) -> DatetimeLike | None: return self._time @property From 248d391c4515e50f9b6559bb1498ab5b0656f709 Mon Sep 17 00:00:00 2001 From: "marvin.steinke" Date: Wed, 31 Jul 2024 16:36:20 +0200 Subject: [PATCH 3/3] sil refactor --- vessim/sil.py | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/vessim/sil.py b/vessim/sil.py index 63bdea5..5aed89a 100644 --- a/vessim/sil.py +++ b/vessim/sil.py @@ -32,11 +32,11 @@ def __init__(self, data_pipe_out: Connection, events_pipe_in: Connection): self._data_pipe_out = data_pipe_out self._events_pipe_in = events_pipe_in self._microgrid_ts: list[tuple[DatetimeLike, Microgrid]] = [] - self._actor_infos_ts: list[tuple[DatetimeLike, dict]] = [] + self._state_ts: list[tuple[DatetimeLike, dict]] = [] self._p_delta_ts: list[tuple[DatetimeLike, float]] = [] self._e_delta_ts: list[tuple[DatetimeLike, float]] = [] self._microgrid: Optional[Microgrid] = None - self._actor_infos: dict[str, dict] = {} + self._state: dict[str, dict] = {} self._p_delta: float = 0 self._e_delta: float = 0 self._ts_lock: Lock = Lock() @@ -45,14 +45,14 @@ def _recv_data(self) -> None: while True: time, data = self._data_pipe_out.recv() self._microgrid = data["microgrid"] - self._actor_infos = data["actor_infos"] + self._state = data["state"] self._p_delta = data["p_delta"] self._e_delta = data["e_delta"] with self._ts_lock: assert isinstance(time, (str, datetime, np.datetime64)) assert self._microgrid is not None self._microgrid_ts.append((time, self._microgrid)) - self._actor_infos_ts.append((time, self._actor_infos)) + self._state_ts.append((time, self._state)) self._p_delta_ts.append((time, self._p_delta)) self._e_delta_ts.append((time, self._e_delta)) @@ -77,46 +77,38 @@ def p_delta(self) -> float: def e_delta(self) -> float: return self._e_delta + @property + def state(self) -> dict[str, dict]: + return self._state + def _get_ts_range( - self, - series: list[tuple[DatetimeLike, Any]], - start_time: Optional[DatetimeLike], - end_time: Optional[DatetimeLike], + self, ts_attr: str, start_time: Optional[DatetimeLike], end_time: Optional[DatetimeLike] ) -> list[tuple[DatetimeLike, Any]]: - start_idx = 0 if start_time is None else bisect_left(series, (start_time,)) - end_idx = len(series) if end_time is None else bisect_right(series, (end_time,)) - return series[start_idx:end_idx] + with self._ts_lock: + ts = getattr(self, ts_attr).copy() + start_idx = 0 if start_time is None else bisect_left(ts, (start_time,)) + end_idx = len(ts) if end_time is None else bisect_right(ts, (end_time,)) + return ts[start_idx:end_idx] def get_microgrid_ts( self, start_time: Optional[DatetimeLike] = None, end_time: Optional[DatetimeLike] = None ) -> list[tuple[DatetimeLike, Microgrid]]: - with self._ts_lock: - ts = self._microgrid_ts.copy() - return self._get_ts_range(ts, start_time, end_time) + return self._get_ts_range("_microgrid_ts", start_time, end_time) - def get_actor_infos(self, actor: str) -> dict: - return self._actor_infos[actor] - - def get_actors_infos_ts( + def get_state_ts( self, start_time: Optional[DatetimeLike] = None, end_time: Optional[DatetimeLike] = None ) -> list[tuple[DatetimeLike, dict[str, dict]]]: - with self._ts_lock: - ts = self._actor_infos_ts.copy() - return self._get_ts_range(ts, start_time, end_time) + return self._get_ts_range("_state_ts", start_time, end_time) def get_p_delta_ts( self, start_time: Optional[DatetimeLike] = None, end_time: Optional[DatetimeLike] = None ) -> list[tuple[DatetimeLike, float]]: - with self._ts_lock: - ts = self._p_delta_ts.copy() - return self._get_ts_range(ts, start_time, end_time) + return self._get_ts_range("_p_delta_ts", start_time, end_time) def get_e_delta_ts( self, start_time: Optional[DatetimeLike] = None, end_time: Optional[DatetimeLike] = None ) -> list[tuple[DatetimeLike, float]]: - with self._ts_lock: - ts = self._e_delta_ts.copy() - return self._get_ts_range(ts, start_time, end_time) + return self._get_ts_range("_e_delta_ts", start_time, end_time) class SilController(Controller): @@ -163,14 +155,14 @@ def start(self, microgrid: Microgrid) -> None: Thread(target=self._collect_set_requests_loop, daemon=True).start() - def step(self, time: datetime, p_delta: float, e_delta: float, actor_infos: dict) -> None: + def step(self, time: datetime, p_delta: float, e_delta: float, state: dict) -> None: assert self.microgrid is not None self.data_pipe_in.send( ( time, { "microgrid": self.microgrid, - "actor_infos": actor_infos, + "state": state, "p_delta": p_delta, "e_delta": e_delta, },