Skip to content

Commit

Permalink
Adding makeSg, makeSd and makeYsh functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwl committed Jun 29, 2015
1 parent 516b5f6 commit 51c48a8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
68 changes: 54 additions & 14 deletions pypf/makeSbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"""Builds the vector of complex bus power injections.
"""

from numpy import array, ones, flatnonzero as find
from numpy import zeros, ones, flatnonzero as find
import numpy as np
from scipy.sparse import csr_matrix as sparse


def makeSbus(baseMVA, bus, gen):
def makeSbus(baseMVA, bus, load, gen):
"""Builds the vector of complex bus power injections.
Returns the vector of complex bus power injections, that is, generation
Expand All @@ -19,18 +20,57 @@ def makeSbus(baseMVA, bus, gen):
@author: Ray Zimmerman (PSERC Cornell)
"""
## generator info
on = find(array(gen.status) > 0) ## which generators are on?
gbus = array(gen.bus)[on] ## what buses are they at?

## form net complex bus power injection vector
nb = bus.n
ngon = on.shape[0]
## connection matrix, element i, j is 1 if gen on(j) at bus i is ON
Sg = makeSg(bus.n, gen)
Sd = makeSd(bus.n, load)

# Form net complex bus power injection vector.
return (Sg - Sd) / baseMVA


def makeSg(nb, gen):
on = find(gen.status > 0)
gbus = gen.bus[on]
ngon = len(on)
if ngon == 0:
return zeros(nb, dtype='complex')

# Connection matrix, element i, j is 1 if gen on(j) at bus i is ON.
Cg = sparse((ones(ngon), (gbus, range(ngon))), (nb, ngon))

## power injected by gens plus power injected by loads converted to p.u.
Sbus = ( Cg * (gen.Pg[on] + 1j * gen.Qg[on]) -
(bus.Pd + 1j * bus.Qd) ) / baseMVA
Pg = gen.pg[on]
Qg = gen.qg[on]
Sg = np.complex(Pg, Qg);

return Cg * Sg # power injected by generators


def makeSd(nb, load):
on = find(load.status > 0)
ldbus = load.bus[on]
non = len(on)
if non == 0:
return zeros(nb, dtype='complex')

Cd = sparse((ones(non), (ldbus, range(non))), (nb, non))

Pd = load.Pd[on]
Qd = load.Qd[on]
Sd = np.complex(Pd, Qd)

return Cd * Sd


def makeYsh(nb, shunt):
on = find(shunt.status > 0)
shbus = shunt.bus[on]
non = len(on)
if non == 0:
return zeros(nb, dtype='complex')

Csh = sparse((ones(non), (shbus, range(non))), (nb, non))

Gs = shunt.Gs[on]
Bs = shunt.Bs[on]
Ysh = np.complex(Gs, Bs)

return Sbus
return Csh * Ysh
5 changes: 3 additions & 2 deletions pypf/makeYbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

from numpy import ones, conj, nonzero, exp, pi, r_
from scipy.sparse import csr_matrix
from makeSbus import makeYsh


def makeYbus(baseMVA, bus, branch):
def makeYbus(baseMVA, bus, shunt, branch):
"""Builds the bus admittance matrix and branch admittance matrices.
Returns the full bus admittance matrix (i.e. for all buses) and the
Expand Down Expand Up @@ -55,7 +56,7 @@ def makeYbus(baseMVA, bus, branch):
## then Psh - j Qsh = V * conj(Ysh * V) = conj(Ysh) = Gs - j Bs,
## i.e. Ysh = Psh + j Qsh, so ...
## vector of shunt admittances
Ysh = (bus.Gs + 1j * bus.Bs) / baseMVA
Ysh = makeYsh(nb, shunt) / baseMVA

## build connection matrices
f = branch.f_bus ## list of "from" buses
Expand Down

0 comments on commit 51c48a8

Please sign in to comment.