|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import numpy as np |
| 6 | + |
6 | 7 | from andes.core import (ModelData, IdxParam, NumParam, DataParam, |
7 | 8 | Model, ExtAlgeb, ConstService) |
| 9 | +from andes.shared import spmatrix |
8 | 10 |
|
9 | 11 |
|
10 | 12 | class LineData(ModelData): |
@@ -241,3 +243,168 @@ def get_tf_idx(self): |
241 | 243 | """ |
242 | 244 |
|
243 | 245 | return np.array(self.idx.v)[self.istf] |
| 246 | + |
| 247 | + def build_y(self): |
| 248 | + """ |
| 249 | + Build bus admittance matrix. Store the matrix in ``self.Y``. |
| 250 | +
|
| 251 | + Returns |
| 252 | + ------- |
| 253 | + Y : spmatrix |
| 254 | + Bus admittance matrix. |
| 255 | + """ |
| 256 | + |
| 257 | + nb = self.system.Bus.n |
| 258 | + |
| 259 | + y1 = self.u.v * (self.g1.v + self.b1.v * 1j) |
| 260 | + y2 = self.u.v * (self.g2.v + self.b2.v * 1j) |
| 261 | + y12 = self.u.v / (self.r.v + self.x.v * 1j) |
| 262 | + m = self.tap.v * np.exp(1j * self.phi.v) |
| 263 | + m2 = self.tap.v**2 |
| 264 | + mconj = np.conj(m) |
| 265 | + |
| 266 | + # build self and mutual admittances into Y |
| 267 | + self.Y = spmatrix((y12 + y1 / m2), self.a1.a, self.a1.a, (nb, nb), 'z') |
| 268 | + self.Y -= spmatrix(y12 / mconj, self.a1.a, self.a2.a, (nb, nb), 'z') |
| 269 | + self.Y -= spmatrix(y12 / m, self.a2.a, self.a1.a, (nb, nb), 'z') |
| 270 | + self.Y += spmatrix(y12 + y2, self.a2.a, self.a2.a, (nb, nb), 'z') |
| 271 | + |
| 272 | + return self.Y |
| 273 | + |
| 274 | + def build_b(self, method='fdpf'): |
| 275 | + """ |
| 276 | + build Bp and Bpp matrices for fast decoupled power flow and DC power flow. |
| 277 | +
|
| 278 | + Results are saved to ``self.Bp`` and ``self.Bpp`` without return. |
| 279 | + """ |
| 280 | + self.build_Bp(method) |
| 281 | + self.build_Bpp(method) |
| 282 | + |
| 283 | + def build_Bp(self, method='fdpf'): |
| 284 | + """ |
| 285 | + Function for building B' matrix. |
| 286 | +
|
| 287 | + The result is saved to ``self.Bp`` and returned |
| 288 | +
|
| 289 | + Parameters |
| 290 | + ---------- |
| 291 | + method : str |
| 292 | + Method for building B' matrix. Choose from 'fdpf', 'fdbx', 'dcpf'. |
| 293 | + Returns |
| 294 | + ------- |
| 295 | + Bp : spmatrix |
| 296 | + B' matrix. |
| 297 | +
|
| 298 | + """ |
| 299 | + nb = self.system.Bus.n |
| 300 | + |
| 301 | + if method not in ("fdpf", "fdbx", "dcpf"): |
| 302 | + raise ValueError(f"Invalid method {method}; choose from 'fdpf', 'fdbx', 'dcpf'") |
| 303 | + |
| 304 | + # Build B prime matrix -- FDPF |
| 305 | + # `y1`` neglects line charging shunt, and g1 is usually 0 in HV lines |
| 306 | + # `y2`` neglects line charging shunt, and g2 is usually 0 in HV lines |
| 307 | + y1 = self.u.v * self.g1.v |
| 308 | + y2 = self.u.v * self.g2.v |
| 309 | + |
| 310 | + # `m` neglected tap ratio |
| 311 | + m = np.exp(self.phi.v * 1j) |
| 312 | + mconj = np.conj(m) |
| 313 | + m2 = np.ones(self.n) |
| 314 | + |
| 315 | + if method in ('fdxb', 'dcpf'): |
| 316 | + # neglect line resistance in Bp in XB method |
| 317 | + y12 = self.u.v / (self.x.v * 1j) |
| 318 | + else: |
| 319 | + y12 = self.u.v / (self.r.v + self.x.v * 1j) |
| 320 | + |
| 321 | + self.Bdc = spmatrix((y12 + y1) / m2, self.a1.a, self.a1.a, (nb, nb), 'z') |
| 322 | + self.Bdc -= spmatrix(y12 / mconj, self.a1.a, self.a2.a, (nb, nb), 'z') |
| 323 | + self.Bdc -= spmatrix(y12 / m, self.a2.a, self.a1.a, (nb, nb), 'z') |
| 324 | + self.Bdc += spmatrix(y12 + y2, self.a2.a, self.a2.a, (nb, nb), 'z') |
| 325 | + self.Bdc = self.Bdc.imag() |
| 326 | + |
| 327 | + for item in range(nb): |
| 328 | + if abs(self.Bdc[item, item]) == 0: |
| 329 | + self.Bdc[item, item] = 1e-6 + 0j |
| 330 | + |
| 331 | + return self.Bdc |
| 332 | + |
| 333 | + def build_Bpp(self, method='fdpf'): |
| 334 | + """ |
| 335 | + Function for building B'' matrix. |
| 336 | +
|
| 337 | + The result is saved to ``self.Bpp`` and returned |
| 338 | +
|
| 339 | + Parameters |
| 340 | + ---------- |
| 341 | + method : str |
| 342 | + Method for building B'' matrix. Choose from 'fdpf', 'fdbx', 'dcpf'. |
| 343 | +
|
| 344 | + Returns |
| 345 | + ------- |
| 346 | + Bpp : spmatrix |
| 347 | + B'' matrix. |
| 348 | + """ |
| 349 | + |
| 350 | + nb = self.system.Bus.n |
| 351 | + |
| 352 | + if method not in ("fdpf", "fdbx", "dcpf"): |
| 353 | + raise ValueError(f"Invalid method {method}; choose from 'fdpf', 'fdbx', 'dcpf'") |
| 354 | + |
| 355 | + # Build B double prime matrix |
| 356 | + # y1 neglected line charging shunt, and g1 is usually 0 in HV lines |
| 357 | + # y2 neglected line charging shunt, and g2 is usually 0 in HV lines |
| 358 | + # m neglected phase shifter |
| 359 | + y1 = self.u.v * (self.g1.v + self.b1.v * 1j) |
| 360 | + y2 = self.u.v * (self.g2.v + self.b2.v * 1j) |
| 361 | + |
| 362 | + m = self.tap.v |
| 363 | + m2 = abs(m)**2 |
| 364 | + |
| 365 | + if method in ('fdbx', 'fdpf', 'dcpf'): |
| 366 | + # neglect line resistance in Bpp in BX method |
| 367 | + y12 = self.u.v / (self.x.v * 1j) |
| 368 | + else: |
| 369 | + y12 = self.u.v / (self.r.v + self.x.v * 1j) |
| 370 | + |
| 371 | + self.Bpp = spmatrix((y12 + y1) / m2, self.a1.a, self.a1.a, (nb, nb), 'z') |
| 372 | + self.Bpp -= spmatrix(y12 / np.conj(m), self.a1.a, self.a2.a, (nb, nb), 'z') |
| 373 | + self.Bpp -= spmatrix(y12 / m, self.a2.a, self.a1.a, (nb, nb), 'z') |
| 374 | + self.Bpp += spmatrix(y12 + y2, self.a2.a, self.a2.a, (nb, nb), 'z') |
| 375 | + self.Bpp = self.Bpp.imag() |
| 376 | + |
| 377 | + for item in range(nb): |
| 378 | + if abs(self.Bpp[item, item]) == 0: |
| 379 | + self.Bpp[item, item] = 1e-6 + 0j |
| 380 | + |
| 381 | + return self.Bpp |
| 382 | + |
| 383 | + def build_Bdc(self): |
| 384 | + """ |
| 385 | + The MATPOWER-flavor Bdc matrix for DC power flow. Saves results to `self.Bdc`. |
| 386 | +
|
| 387 | + The method neglects line charging and line resistance. It retains tap ratio. |
| 388 | +
|
| 389 | + Returns |
| 390 | + ------- |
| 391 | + Bdc : spmatrix |
| 392 | + Bdc matrix. |
| 393 | + """ |
| 394 | + |
| 395 | + nb = self.system.Bus.n |
| 396 | + |
| 397 | + y12 = self.u.v / (self.x.v * 1j) |
| 398 | + y12 = y12 / self.tap.v |
| 399 | + |
| 400 | + self.Bdc = spmatrix(y12, self.a1.a, self.a1.a, (nb, nb), 'z') |
| 401 | + self.Bdc -= spmatrix(y12, self.a1.a, self.a2.a, (nb, nb), 'z') |
| 402 | + self.Bdc -= spmatrix(y12, self.a2.a, self.a1.a, (nb, nb), 'z') |
| 403 | + self.Bdc += spmatrix(y12, self.a2.a, self.a2.a, (nb, nb), 'z') |
| 404 | + self.Bdc = self.Bdc.imag() |
| 405 | + |
| 406 | + for item in range(nb): |
| 407 | + if abs(self.Bdc[item, item]) == 0: |
| 408 | + self.Bdc[item, item] = 1e-6 |
| 409 | + |
| 410 | + return self.Bdc |
0 commit comments