Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Below I present a function (getlevelsapart) that returns the number of levels between two buses. For example, using the savnw network, call the function to calculate the levels between bus 3011 and 3005:

n = getlevelsapart(3011,3005)

Here, the function will return 3.

Similarly, the levels between bus 3011 and bus 206 is 7.

n = getlevelsapart(3011,206)

Definition of function getlevelsapart:

def getlevelsapart(ibus,jbus,flag=2,ms=True):
    """ Returns number of levels between ibus and jbus
    connected with branch, 2W or 3W.
    ibus: number of startnode
    jbus: number of endnode
    flag: 1= use also connection out of service, 2= only in-service connection
    ms: True= use multisection line definition, False = ignore multi-section line 

    Returns None if there is no AC network between ibus and jbus
    """
    if psspy.busexs(ibus) == 1:
        print 'Error getlevelsapart: Start bus %s does not exist' % ibus
        return None
    if psspy.busexs(jbus) == 1:
        print 'Error getlevelsapart: End bus %s does not exist' % jbus
        return None
    busdict = {ibus:0}  # dict with level for each analysed bus
    nextbuses = [ibus] # start search at ibus
    # For each level out into the grid
    for level in range(99999):
        searchbuses = list(nextbuses) # buses for next level search
        nextbuses = {}  # New buses at next level
        # For each start node at this level
        for bus in searchbuses:
            if ms: ierr = psspy.inibrx(bus, 2)
            else: ierr = psspy.inibrn(bus, 2)
            while ierr==0: # for every connection to bus
                ierr,tbus,kbus,ickt = psspy.nxtbrn3(bus)
                if ierr>0: break
                if kbus==0: # branch or 2W
                    ier2,st = psspy.brnint(bus,tbus,ickt,'STATUS')
                else: # 3W
                    ier2, st = psspy.tr3int(bus, tbus, kbus, ickt, 'STATUS')
                if flag==1 or st==1: 
                    if tbus not in busdict: # tbus at next level
                        busdict[tbus] = level+1
                        nextbuses[tbus] = level+1
                    if kbus>0:
                        if kbus not in busdict:  # kbus at next level
                            busdict[kbus] = level+1
                            nextbuses[kbus] = level+1

            if jbus in nextbuses: return level+1   # jbus found

        if len(nextbuses) == 0:  # No more levels found
            print 'No AC connection between bus %s [%s] and bus %s [%s]' % (ibus,psspy.notona(ibus)[1],jbus,psspy.notona(jbus)[1])
            return None