|
| 1 | +""" |
| 2 | +Module for Connectivity Manager. |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from collections import OrderedDict |
| 7 | + |
| 8 | +from andes.utils.func import list_flatten |
| 9 | +from andes.shared import np |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +# connectivity dependencies of `Bus` |
| 15 | +# NOTE: only include PFlow models and measurements models |
| 16 | +# cause online status of dynamic models are expected to be handled by their |
| 17 | +# corresponding static models |
| 18 | +# TODO: DC Topologies are not included yet, `Node`, etc |
| 19 | +bus_deps = OrderedDict([ |
| 20 | + ('ACLine', ['bus1', 'bus2']), |
| 21 | + ('ACShort', ['bus1', 'bus2']), |
| 22 | + ('FreqMeasurement', ['bus']), |
| 23 | + ('Interface', ['bus']), |
| 24 | + ('Motor', ['bus']), |
| 25 | + ('PhasorMeasurement', ['bus']), |
| 26 | + ('StaticACDC', ['bus']), |
| 27 | + ('StaticGen', ['bus']), |
| 28 | + ('StaticLoad', ['bus']), |
| 29 | + ('StaticShunt', ['bus']), |
| 30 | +]) |
| 31 | + |
| 32 | + |
| 33 | +class ConnMan: |
| 34 | + """ |
| 35 | + Define a Connectivity Manager class for System. |
| 36 | +
|
| 37 | + Connectivity Manager is used to automatically **turn off** |
| 38 | + attached devices when a ``Bus`` is turned off **after** system |
| 39 | + setup and **before** TDS initializtion. |
| 40 | +
|
| 41 | + Attributes |
| 42 | + ---------- |
| 43 | + system: system object |
| 44 | + System object to manage the connectivity. |
| 45 | + busu0: ndarray |
| 46 | + Last recorded bus connection status. |
| 47 | + is_needed: bool |
| 48 | + Flag to indicate if connectivity update is needed. |
| 49 | + changes: dict |
| 50 | + Dictionary to record bus connectivity changes ('on' and 'off'). |
| 51 | + 'on' means the bus is previous offline and now online. |
| 52 | + 'off' means the bus is previous online and now offline. |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__(self, system=None): |
| 56 | + """ |
| 57 | + Initialize the connectivity manager. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + system: system object |
| 62 | + System object to manage the connectivity. |
| 63 | + """ |
| 64 | + self.system = system |
| 65 | + self.busu0 = None # placeholder for Bus.u.v |
| 66 | + self.is_needed = False # flag to indicate if check is needed |
| 67 | + self.changes = {'on': None, 'off': None} # dict of bus connectivity changes |
| 68 | + |
| 69 | + def init(self): |
| 70 | + """ |
| 71 | + Initialize the connectivity. |
| 72 | +
|
| 73 | + `ConnMan` is initialized in `System.setup()`, where all buses are considered online |
| 74 | + by default. This method records the initial bus connectivity. |
| 75 | + """ |
| 76 | + # NOTE: here, we expect all buses are online before the system setup |
| 77 | + self.busu0 = np.ones(self.system.Bus.n, dtype=int) |
| 78 | + self.changes['on'] = np.zeros(self.system.Bus.n, dtype=int) |
| 79 | + self.changes['off'] = np.logical_and(self.busu0 == 1, self.system.Bus.u.v == 0).astype(int) |
| 80 | + |
| 81 | + if np.any(self.changes['off']): |
| 82 | + self.is_needed = True |
| 83 | + |
| 84 | + self.act() |
| 85 | + |
| 86 | + return True |
| 87 | + |
| 88 | + def _update(self): |
| 89 | + """ |
| 90 | + Helper function for in-place update of bus connectivity. |
| 91 | + """ |
| 92 | + self.changes['on'][...] = np.logical_and(self.busu0 == 0, self.system.Bus.u.v == 1) |
| 93 | + self.changes['off'][...] = np.logical_and(self.busu0 == 1, self.system.Bus.u.v == 0) |
| 94 | + self.busu0[...] = self.system.Bus.u.v |
| 95 | + |
| 96 | + def record(self): |
| 97 | + """ |
| 98 | + Record the bus connectivity in-place. |
| 99 | +
|
| 100 | + This method should be called if `Bus.set()` or `Bus.alter()` is called. |
| 101 | + """ |
| 102 | + self._update() |
| 103 | + |
| 104 | + if np.any(self.changes['on']): |
| 105 | + onbus_idx = [self.system.Bus.idx.v[i] for i in np.nonzero(self.changes["on"])[0]] |
| 106 | + logger.warning(f'Bus turned on: {onbus_idx}') |
| 107 | + self.is_needed = True |
| 108 | + if len(onbus_idx) > 0: |
| 109 | + raise NotImplementedError('Turning on bus after system setup is not supported yet!') |
| 110 | + |
| 111 | + if np.any(self.changes['off']): |
| 112 | + offbus_idx = [self.system.Bus.idx.v[i] for i in np.nonzero(self.changes["off"])[0]] |
| 113 | + logger.warning(f'Bus turned off: {offbus_idx}') |
| 114 | + self.is_needed = True |
| 115 | + |
| 116 | + return self.changes |
| 117 | + |
| 118 | + def act(self): |
| 119 | + """ |
| 120 | + Update the connectivity. |
| 121 | + """ |
| 122 | + if not self.is_needed: |
| 123 | + logger.debug('No need to update connectivity.') |
| 124 | + return True |
| 125 | + |
| 126 | + if self.system.TDS.initialized: |
| 127 | + raise NotImplementedError('Bus connectivity update during TDS is not supported yet!') |
| 128 | + |
| 129 | + # --- action --- |
| 130 | + offbus_idx = [self.system.Bus.idx.v[i] for i in np.nonzero(self.changes["off"])[0]] |
| 131 | + |
| 132 | + # skip if no bus is turned off |
| 133 | + if len(offbus_idx) == 0: |
| 134 | + return True |
| 135 | + |
| 136 | + logger.warning('Entering connectivity update.') |
| 137 | + logger.warning(f'Following bus(es) are turned off: {offbus_idx}') |
| 138 | + |
| 139 | + logger.warning('-> System connectivity update results:') |
| 140 | + for grp_name, src_list in bus_deps.items(): |
| 141 | + devices = [] |
| 142 | + for src in src_list: |
| 143 | + grp_devs = self.system.__dict__[grp_name].find_idx(keys=src, values=offbus_idx, |
| 144 | + allow_none=True, allow_all=True, |
| 145 | + default=None) |
| 146 | + grp_devs_flat = list_flatten(grp_devs) |
| 147 | + if grp_devs_flat != [None]: |
| 148 | + devices.append(grp_devs_flat) |
| 149 | + |
| 150 | + devices_flat = list_flatten(devices) |
| 151 | + |
| 152 | + if len(devices_flat) > 0: |
| 153 | + self.system.__dict__[grp_name].set(src='u', attr='v', |
| 154 | + idx=devices_flat, value=0) |
| 155 | + logger.warning(f'In <{grp_name}>, turn off {devices_flat}') |
| 156 | + |
| 157 | + self.is_needed = False # reset the action flag |
| 158 | + self._update() # update but not record |
| 159 | + self.system.connectivity(info=True) |
| 160 | + return True |
0 commit comments