(batch: HeteroData)
| 175 | |
| 176 | |
| 177 | def create_Ybus(batch: HeteroData): |
| 178 | homo_batch = batch.to_homogeneous().detach() |
| 179 | bus = homo_batch.x |
| 180 | index_diff = homo_batch.edge_index[1, :] - homo_batch.edge_index[0, :] |
| 181 | # to index bigger than from index |
| 182 | edge_attr = homo_batch.edge_attr[index_diff > 0, :] |
| 183 | edge_index_ori = homo_batch.edge_index[:, index_diff > 0] |
| 184 | device = batch['PQ'].x.device |
| 185 | with torch.no_grad(): |
| 186 | edge_mask = torch.isnan(edge_attr[:,0]) |
| 187 | edge_attr = edge_attr[~edge_mask] |
| 188 | edge_index = torch.vstack([edge_index_ori[0][~edge_mask],edge_index_ori[1][~edge_mask]]) |
| 189 | # makeYbus, reference to pypower makeYbus |
| 190 | nb = bus.shape[0] # number of buses |
| 191 | nl = edge_index.shape[1] # number of edges |
| 192 | Vm, Va, P_net, Q_net, Gs, Bs = 0, 1, 2, 3, 4, 5 |
| 193 | BR_R, BR_X, BR_B, TAP, SHIFT = 0, 1, 2, 3, 4 |
| 194 | |
| 195 | Ys = 1.0 / (edge_attr[:, BR_R] + 1j * edge_attr[:, BR_X]) |
| 196 | Bc = edge_attr[:, BR_B] |
| 197 | tap = torch.ones(nl).to(device) |
| 198 | i = torch.nonzero(edge_attr[:, TAP]) |
| 199 | tap[i] = edge_attr[i, TAP] |
| 200 | tap = tap * torch.exp(1j * edge_attr[:, SHIFT]) |
| 201 | |
| 202 | Ytt = Ys + 1j * Bc / 2 |
| 203 | Yff = Ytt / (tap * torch.conj(tap)) |
| 204 | Yft = - Ys / torch.conj(tap) |
| 205 | Ytf = - Ys / tap |
| 206 | |
| 207 | Ysh = bus[:, Gs] + 1j * bus[:, Bs] |
| 208 | |
| 209 | # build connection matrices |
| 210 | f = edge_index[0] |
| 211 | t = edge_index[1] |
| 212 | Cf = torch.sparse_coo_tensor( |
| 213 | torch.vstack([torch.arange(nl).to(device), f]), |
| 214 | torch.ones(nl).to(device), |
| 215 | (nl, nb) |
| 216 | ).to(torch.complex64) |
| 217 | Ct = torch.sparse_coo_tensor( |
| 218 | torch.vstack([torch.arange(nl).to(device), t]), |
| 219 | torch.ones(nl).to(device), |
| 220 | (nl, nb) |
| 221 | ).to(torch.complex64) |
| 222 | |
| 223 | i_nl = torch.cat([torch.arange(nl), torch.arange(nl)], dim=0).to(device) |
| 224 | i_ft = torch.cat([f, t], dim=0) |
| 225 | |
| 226 | Yf = torch.sparse_coo_tensor( |
| 227 | torch.vstack([i_nl, i_ft]), |
| 228 | torch.cat([Yff, Yft], dim=0), |
| 229 | (nl, nb), |
| 230 | dtype=torch.complex64 |
| 231 | ) |
| 232 | |
| 233 | Yt = torch.sparse_coo_tensor( |
| 234 | torch.vstack([i_nl, i_ft]), |
no test coverage detected