def branchDataZone(zone):
	toBus = []
	fromBus = []
	busInZone = []
	
	busZones = defaultdict(list)
	ierr, array = psspy.aflowint(-1, -1, -1, 2, 'FROMNUMBER')
	ierr, array2 = psspy.aflowint(-1, -1, -1, 2, 'TONUMBER')
	#for x in range(0, len(array[0])):
		#print array[0][x]
		#print array2[0][x]
	ierr, busNumber = psspy.abusint(-1, 2, 'NUMBER')
	ierr, busZone = psspy.abusint(-1, 2, 'ZONE')
	#print busNumber
	
	for y in range(0, len(busNumber[0])):
		busZones[busNumber[0][y]].append(busZone[0][y])
	
	for a in range(0, len(busZones)):
		if busZones[busNumber[0][a]][0] == zone:
			busInZone.append(busNumber[0][a])
	
	for z in range(0, len(array[0])):
		if busZones[array[0][z]] == busZones[array2[0][z]]:
			pass
		else:
			#print 'Branch between different zones'
			if busZones[array[0][z]][0] == zone:
				toBus.append(array[0][z])
				fromBus.append(array2[0][z])
				#for i in range(0, len(busInZone)):
				
				if array[0][z] not in busInZone:
					busInZone.append(array[0][z])
				if array2[0][z] not in busInZone:
					busInZone.append(array2[0][z])		
				
			
			if busZones[array2[0][z]][0] == zone:
				toBus.append(array[0][z])
				fromBus.append(array2[0][z])	

				
				if array[0][z] not in busInZone:
					busInZone.append(array[0][z])
				if array2[0][z] not in busInZone:
					busInZone.append(array2[0][z])		
				



	
	return toBus, fromBus, busInZone, busZones, array, array2
Beispiel #2
0
    def loadBusesPF(self, ibus, entries):
        ''' retrieve the information of power flow values associated to each branch. Information of branches, 
        pair of buses (fromBus,toBus) is used.
        ibus bus number to start searching
        entries - int number to retrieve either 1) only forward branches or 2) forward and backward branches
        '''
        sid = -1
        flag = 2
        string = ['NUMBER', 'TYPE']
        #         print 'buses', buses
        ierr, buslist = psspy.abusint(sid, flag, string)
        #         print 'buslist: ', buslist
        branchlist = []
        for frombus in buslist:
            psspy.inibrn(frombus[0], entries)
            while True:  #branch loop
                ierr, tobus, ckt = psspy.nxtbrn(frombus[0])
                if ierr != 0:
                    break
                branchlist.append((frombus[0], tobus, ckt))
#         print 'branchlist: ', branchlist
        for branch in branchlist:  #get branch data
            frombus, tobus, ckt = branch
            ierr, pflow = psspy.brnflo(frombus, tobus, ckt)
            if ierr != 0:
                break
#             print 'pflow: ', pflow
            self.busPowerFlow[frombus] = pflow
        print 'busPowerFlow: ', self.busPowerFlow
Beispiel #3
0
    def readBusNumbers(self):
        '''
        Stores all bus numbers in self.busNumbers
        '''

        ierr, self.busNumbers = psspy.abusint(-1, 2, 'NUMBER')

        assert ierr == 0, 'Error with reading bus numbers'

        return self.busNumbers
Beispiel #4
0
    def readBusNumbers(self):
        '''
        Stores all bus numbers in self.busNumbers
        '''

        ierr, self.busNumbers = psspy.abusint(-1, 2, 'NUMBER')

        assert ierr == 0, 'Error with reading bus numbers'

        return self.busNumbers
Beispiel #5
0
def findAllBusType(bus_num):
    psspy.bsys(sid=1, numbus=len(bus_num), buses=bus_num)
    ierr,bus_type = psspy.abusint(1,1,'type')
    bus_type = bus_type[0]
    pq = []
    pv = []
    slackBus = []
    for index,bus in enumerate(bus_num):
        if bus_type[index] == 1:
            pq.append(bus)
        elif bus_type[index] == 2:
            pv.append(bus)
        elif bus_type[index] == 3:
            slackBus.append(bus)
    return pq,pv,slackBus
Beispiel #6
0
    def loadBranchZ(self, ibus, entries):
        ''' retrieve the information of inductances and admittances of branches of the model
        ibus - bus number to start searching
        entries - int number to retrieve either 1) only forward branches or 2) forward and backward branches
        '''
        sid = -1
        flag = 2
        string = ['NUMBER', 'TYPE']
        ierr, buslist = psspy.abusint(sid, flag, string)
        ''' retrive info from the branch starting at bus ibus '''
        branchlist = []
        psspy.inibrn(ibus, entries)
        ierr, tobus, ckt = psspy.nxtbrn(ibus)
        branchlist.append((ibus, tobus, ckt))
        #         print 'branchlist: ', branchlist
        ''' retrieve info from the branch model 
        Resistance, reactance, admittance sending end, admittance receiving end'''
        branchParam = []
        for branch in branchlist:  #get branch data
            frombus, tobus, ckt = branch
            '''load impedance Z'''
            ierr, cmprx = psspy.brndt2(frombus, tobus, ckt, 'RX')
            #             print 'cmprx: ', cmprx, 'ierr: ', ierr
            branchParam.append(cmprx)
            '''load conductance G'''
            ierr, cmpishunt = psspy.brndt2(frombus, tobus, ckt, 'ISHNT')
            #             print 'cmpishunt: ', cmpishunt, 'ierr: ', ierr
            branchParam.append(cmpishunt)
            '''load susceptance B'''
            ierr, cmpjshunt = psspy.brndt2(frombus, tobus, ckt, 'JSHNT')
            #             print 'cmpjshunt: ', cmpjshunt, 'ierr: ', ierr
            branchParam.append(cmpjshunt)
            if ierr != 0:
                break
#             print 'pflow: ', pflow
#             print 'branchParam: ', branchParam
            self.branchData[branch] = branchParam
        print 'branchData: ', self.branchData
##ierr = psspy.branch_data(44109, 44119, "4", intgar1=1, realar1=0.002781434,
##                         realar2=0.008661488, realar3=0.001763027)
##ierr = psspy.seq_branch_data(44109, 44119, "4", realar1=0.007021062,
##                             realar2=0.028150285, realar3=0.001087962)

#---------------------------------
# CTPS1 generation off
ierr = psspy.machine_data_2(44015, intgar1=0)  #CTPS gen1
ierr = psspy.machine_data_2(44016, intgar1=0)  #CTPS gen2
ierr = psspy.machine_data_2(44017, intgar1=0)  #CTPS gen3
ierr = psspy.load_data_3(44106, realar1=184.5)  #CHANDRAPURA
ierr = psspy.load_data_3(44202, realar1=300.)  #CTPS2

###-------------------------------
ierr = psspy.bsys(sid=1, numzone=1, zones=44)
ierr, all_buses = psspy.abusint(sid=1, string=["NUMBER"], flag=2)
all_buses = all_buses[0]
ierr, in_buses = psspy.abusint(sid=1, string=['NUMBER'], flag=1)
in_buses = in_buses[0]
out_buses = list(set(all_buses) - set(in_buses))
print out_buses
ierr = psspy.bsys(sid=2, numbus=len(out_buses), buses=out_buses)
ierr = psspy.extr(sid=2, all=0, status2=0)

##for i in range(len(out_buses)):
##     psspy.load_data_3(i, intgar1=0)
##
##ierr = psspy.bsys(sid=2, numbus=len(out_buses), buses=out_buses)
##ierr, in_branches = psspy.abrnint(sid=2, string=['FROMNUMBER', 'TONUMBER'],
##                                  flag=1)
##for i in range(len(in_branches[0])):
    # choose current percentage
    current_percentage = percentage_set[index]

    # choose the case
    case_name_constant = "IEEE_118_%s.sav"
    PSSE_CASE = case_name_constant % current_percentage
    psspy.case(PSSE_CASE)

    # silent the output
    output = StringIO.StringIO()
    with silence(output):

        #pick the bus in Dominion Area 345 is the index of Dominion
        psspy.case(savecase)
        # psspy.bsys(0,0,[ 0.2, 999.],1,[],0,[],0,[],0,[])
        ierr,all_bus = psspy.abusint(-1,1,['NUMBER'])
        bus_num = all_bus[0]

        #Load Bus
        psspy.bsys(sid = 1,numbus = len(bus_num), buses = bus_num)
        ierr,load_bus = psspy.alodbusint(1,1,['NUMBER'])
        load_bus = load_bus[0]

        # Gen Bus
        psspy.bsys(sid=1, numbus=len(bus_num), buses=bus_num)
        ierr, gen_bus = psspy.agenbusint(1, 1, ['NUMBER'])
        gen_bus = gen_bus[0]

        #change the load and the generation
        percentage = 1-(current_percentage-5)/current_percentage
        pssepylib.change_load(load_bus, percentage)
Beispiel #9
0
redirect.psse2py()
psspy.psseinit(buses=80000)
psspy.report_output(2, psse_log, [0, 0])
psspy.progress_output(2, psse_log, [0, 0])
psspy.alert_output(2, psse_log, [0, 0])
psspy.prompt_output(2, psse_log, [0, 0])
_i = psspy.getdefaultint()
_f = psspy.getdefaultreal()
_s = psspy.getdefaultchar()
print "\n Reading raw file:", settings['filename']
#ierr = psspy.read(0, settings['filename'])

# read raw file and get bus list
#ierr = psspy.read(0, settings['filename'])
ierr, buslist = psspy.abusint(-1, 1,
                              'NUMBER')  # getting bus numbers from raw file
buslist = buslist[0]

# loop to simulate all line outages
for line in list(HVLineSet):

    lineElements = line.split(',')
    Bus1 = int(lineElements[0])
    Bus2 = int(lineElements[1])
    cktID = lineElements[2]

    # read the original raw file and try to solve power flow
    ierr = psspy.read(0, settings['filename'])
    ierr = psspy.branch_chng(
        Bus1, Bus2, cktID, [0, _i, _i, _i, _i, _i],
        [_f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f
Beispiel #10
0
def pout_excel(savfile='savnw.sav', outpath=None, show=True):
    '''Exports power flow results to Excel Spreadsheet.
    When 'savfile' is not provided, it uses Network Data from PSS(R)E memory.
    When 'xlsfile' is provided and exists, power flow results are saved in 'next sheet#' of 'xlsfile'.
    When 'xlsfile' is provided and does not exists, power flow results are saved in 'Sheet1' of 'xlsfile'.
    When 'xlsfile' is not provided, power flow results are saved in 'Sheet1' of 'Book#.xls' file.
    '''

    import psspy

    psspy.psseinit()

    if savfile:
        ierr = psspy.case(savfile)
        if ierr != 0: return
        fpath, fext = os.path.splitext(savfile)
        if not fext: savfile = fpath + '.sav'
        #ierr = psspy.fnsl([0,0,0,1,1,0,0,0])
        #if ierr != 0: return
    else:  # saved case file not provided, check if working case is in memory
        ierr, nbuses = psspy.abuscount(-1, 2)
        if ierr != 0:
            print '\n No working case in memory.'
            print ' Either provide a Saved case file name or open Saved case in PSS(R)E.'
            return
        savfile, snapfile = psspy.sfiles()

    # ================================================================================================
    # PART 1: Get the required results data
    # ================================================================================================

    # Select what to report
    if psspy.bsysisdef(0):
        sid = 0
    else:  # Select subsytem with all buses
        sid = -1

    flag_bus = 1  # in-service
    flag_plant = 1  # in-service
    flag_load = 1  # in-service
    flag_swsh = 1  # in-service
    flag_brflow = 1  # in-service
    owner_brflow = 1  # bus, ignored if sid is -ve
    ties_brflow = 5  # ignored if sid is -ve

    # ------------------------------------------------------------------------------------------------
    # Case Title
    titleline1, titleline2 = psspy.titldt()

    # ------------------------------------------------------------------------------------------------
    # Bus Data
    # Bus Data - Integer
    istrings = ['number', 'type', 'area', 'zone', 'owner', 'dummy']
    ierr, idata = psspy.abusint(sid, flag_bus, istrings)
    if ierr:
        print '(1) psspy.abusint error = %d' % ierr
        return
    ibuses = array2dict(istrings, idata)
    # Bus Data - Real
    rstrings = [
        'base', 'pu', 'kv', 'angle', 'angled', 'mismatch', 'o_mismatch'
    ]
    ierr, rdata = psspy.abusreal(sid, flag_bus, rstrings)
    if ierr:
        print '(1) psspy.abusreal error = %d' % ierr
        return
    rbuses = array2dict(rstrings, rdata)
    # Bus Data - Complex
    xstrings = [
        'voltage', 'shuntact', 'o_shuntact', 'shuntnom', 'o_shuntnom',
        'mismatch', 'o_mismatch'
    ]
    ierr, xdata = psspy.abuscplx(sid, flag_bus, xstrings)
    if ierr:
        print '(1) psspy.abuscplx error = %d' % ierr
        return
    xbuses = array2dict(xstrings, xdata)
    # Bus Data - Character
    cstrings = ['name', 'exname']
    ierr, cdata = psspy.abuschar(sid, flag_bus, cstrings)
    if ierr:
        print '(1) psspy.abuschar error = %d' % ierr
        return
    cbuses = array2dict(cstrings, cdata)

    # Store bus data for all buses
    ibusesall = {}
    rbusesall = {}
    xbusesall = {}
    cbusesall = {}
    if sid == -1:
        ibusesall = ibuses
        rbusesall = rbuses
        xbusesall = xbuses
        cbusesall = cbuses
    else:
        ierr, idata = psspy.abusint(-1, flag_bus, istrings)
        if ierr:
            print '(2) psspy.abusint error = %d' % ierr
            return
        ibusesall = array2dict(istrings, idata)

        ierr, rdata = psspy.abusreal(-1, flag_bus, rstrings)
        if ierr:
            print '(2) psspy.abusreal error = %d' % ierr
            return
        rbusesall = array2dict(rstrings, rdata)

        ierr, xdata = psspy.abuscplx(-1, flag_bus, xstrings)
        if ierr:
            print '(2) psspy.abuscplx error = %d' % ierr
            return
        xbusesall = array2dict(xstrings, xdata)

        ierr, cdata = psspy.abuschar(-1, flag_bus, cstrings)
        if ierr:
            print '(2) psspy.abuschar error = %d' % ierr
            return
        cbusesall = array2dict(cstrings, cdata)

    # ------------------------------------------------------------------------------------------------
    # Plant Bus Data
    # Plant Bus Data - Integer
    istrings = [
        'number', 'type', 'area', 'zone', 'owner', 'dummy', 'status', 'ireg'
    ]
    ierr, idata = psspy.agenbusint(sid, flag_plant, istrings)
    if ierr:
        print 'psspy.agenbusint error = %d' % ierr
        return
    iplants = array2dict(istrings, idata)
    # Plant Bus Data - Real
    rstrings = [
        'base', 'pu', 'kv', 'angle', 'angled', 'iregbase', 'iregpu', 'iregkv',
        'vspu', 'vskv', 'rmpct', 'pgen', 'qgen', 'mva', 'percent', 'pmax',
        'pmin', 'qmax', 'qmin', 'mismatch', 'o_pgen', 'o_qgen', 'o_mva',
        'o_pmax', 'o_pmin', 'o_qmax', 'o_qmin', 'o_mismatch'
    ]
    ierr, rdata = psspy.agenbusreal(sid, flag_plant, rstrings)
    if ierr:
        print 'psspy.agenbusreal error = %d' % ierr
        return
    rplants = array2dict(rstrings, rdata)
    # Plant Bus Data - Complex
    xstrings = ['voltage', 'pqgen', 'mismatch', 'o_pqgen', 'o_mismatch']
    ierr, xdata = psspy.agenbuscplx(sid, flag_plant, xstrings)
    if ierr:
        print 'psspy.agenbusreal error = %d' % ierr
        return
    xplants = array2dict(xstrings, xdata)
    # Plant Bus Data - Character
    cstrings = ['name', 'exname', 'iregname', 'iregexname']
    ierr, cdata = psspy.agenbuschar(sid, flag_plant, cstrings)
    if ierr:
        print 'psspy.agenbuschar error = %d' % ierr
        return
    cplants = array2dict(cstrings, cdata)

    # ------------------------------------------------------------------------------------------------
    # Load Data - based on Individual Loads Zone/Area/Owner subsystem
    # Load Data - Integer
    istrings = ['number', 'area', 'zone', 'owner', 'status']
    ierr, idata = psspy.aloadint(sid, flag_load, istrings)
    if ierr:
        print 'psspy.aloadint error = %d' % ierr
        return
    iloads = array2dict(istrings, idata)
    # Load Data - Real
    rstrings = [
        'mvaact', 'mvanom', 'ilact', 'ilnom', 'ylact', 'ylnom', 'totalact',
        'totalnom', 'o_mvaact', 'o_mvanom', 'o_ilact', 'o_ilnom', 'o_ylact',
        'o_ylnom', 'o_totalact', 'o_totalnom'
    ]
    ierr, rdata = psspy.aloadreal(sid, flag_load, rstrings)
    if ierr:
        print 'psspy.aloadreal error = %d' % ierr
        return
    rloads = array2dict(rstrings, rdata)
    # Load Data - Complex
    xstrings = rstrings
    ierr, xdata = psspy.aloadcplx(sid, flag_load, xstrings)
    if ierr:
        print 'psspy.aloadcplx error = %d' % ierr
        return
    xloads = array2dict(xstrings, xdata)
    # Load Data - Character
    cstrings = ['id', 'name', 'exname']
    ierr, cdata = psspy.aloadchar(sid, flag_load, cstrings)
    if ierr:
        print 'psspy.aloadchar error = %d' % ierr
        return
    cloads = array2dict(cstrings, cdata)

    # ------------------------------------------------------------------------------------------------
    # Total load on a bus
    totalmva = {}
    totalil = {}
    totalyl = {}
    totalys = {}
    totalysw = {}
    totalload = {}
    busmsm = {}
    for b in ibuses['number']:
        ierr, ctmva = psspy.busdt2(b, 'MVA', 'ACT')
        if ierr == 0: totalmva[b] = ctmva

        ierr, ctil = psspy.busdt2(b, 'IL', 'ACT')
        if ierr == 0: totalil[b] = ctil

        ierr, ctyl = psspy.busdt2(b, 'YL', 'ACT')
        if ierr == 0: totalyl[b] = ctyl

        ierr, ctys = psspy.busdt2(b, 'YS', 'ACT')
        if ierr == 0: totalys[b] = ctys

        ierr, ctysw = psspy.busdt2(b, 'YSW', 'ACT')
        if ierr == 0: totalysw[b] = ctysw

        ierr, ctld = psspy.busdt2(b, 'TOTAL', 'ACT')
        if ierr == 0: totalload[b] = ctld

        #Bus mismstch
        ierr, msm = psspy.busmsm(b)
        if ierr != 1: busmsm[b] = msm

    # ------------------------------------------------------------------------------------------------
    # Switched Shunt Data
    # Switched Shunt Data - Integer
    istrings = [
        'number', 'type', 'area', 'zone', 'owner', 'dummy', 'mode', 'ireg',
        'blocks', 'stepsblock1', 'stepsblock2', 'stepsblock3', 'stepsblock4',
        'stepsblock5', 'stepsblock6', 'stepsblock7', 'stepsblock8'
    ]
    ierr, idata = psspy.aswshint(sid, flag_swsh, istrings)
    if ierr:
        print 'psspy.aswshint error = %d' % ierr
        return
    iswsh = array2dict(istrings, idata)
    # Switched Shunt Data - Real (Note: Maximum allowed NSTR are 50. So they are split into 2)
    rstrings = [
        'base', 'pu', 'kv', 'angle', 'angled', 'vswhi', 'vswlo', 'rmpct',
        'bswnom', 'bswmax', 'bswmin', 'bswact', 'bstpblock1', 'bstpblock2',
        'bstpblock3', 'bstpblock4', 'bstpblock5', 'bstpblock6', 'bstpblock7',
        'bstpblock8', 'mismatch'
    ]
    rstrings1 = [
        'o_bswnom', 'o_bswmax', 'o_bswmin', 'o_bswact', 'o_bstpblock1',
        'o_bstpblock2', 'o_bstpblock3', 'o_bstpblock4', 'o_bstpblock5',
        'o_bstpblock6', 'o_bstpblock7', 'o_bstpblock8', 'o_mismatch'
    ]
    ierr, rdata = psspy.aswshreal(sid, flag_swsh, rstrings)
    if ierr:
        print '(1) psspy.aswshreal error = %d' % ierr
        return
    rswsh = array2dict(rstrings, rdata)
    ierr, rdata1 = psspy.aswshreal(sid, flag_swsh, rstrings1)
    if ierr:
        print '(2) psspy.aswshreal error = %d' % ierr
        return
    rswsh1 = array2dict(rstrings1, rdata1)
    for k, v in rswsh1.iteritems():
        rswsh[k] = v
    # Switched Shunt Data - Complex
    xstrings = ['voltage', 'yswact', 'mismatch', 'o_yswact', 'o_mismatch']
    ierr, xdata = psspy.aswshcplx(sid, flag_swsh, xstrings)
    if ierr:
        print 'psspy.aswshcplx error = %d' % ierr
        return
    xswsh = array2dict(xstrings, xdata)
    # Switched Shunt Data - Character
    cstrings = ['vscname', 'name', 'exname', 'iregname', 'iregexname']
    ierr, cdata = psspy.aswshchar(sid, flag_swsh, cstrings)
    if ierr:
        print 'psspy.aswshchar error = %d' % ierr
        return
    cswsh = array2dict(cstrings, cdata)

    # ------------------------------------------------------------------------------------------------
    # Branch Flow Data
    # Branch Flow Data - Integer
    istrings = [
        'fromnumber', 'tonumber', 'status', 'nmeternumber', 'owners', 'own1',
        'own2', 'own3', 'own4'
    ]
    ierr, idata = psspy.aflowint(sid, owner_brflow, ties_brflow, flag_brflow,
                                 istrings)
    if ierr:
        print 'psspy.aflowint error = %d' % ierr
        return
    iflow = array2dict(istrings, idata)
    # Branch Flow Data - Real
    rstrings = [
        'amps',
        'pucur',
        'pctrate',
        'pctratea',
        'pctrateb',
        'pctratec',
        'pctmvarate',
        'pctmvaratea',
        'pctmvarateb',  #'pctmvaratec','fract1','fract2','fract3',
        'fract4',
        'rate',
        'ratea',
        'rateb',
        'ratec',
        'p',
        'q',
        'mva',
        'ploss',
        'qloss',
        'o_p',
        'o_q',
        'o_mva',
        'o_ploss',
        'o_qloss'
    ]
    ierr, rdata = psspy.aflowreal(sid, owner_brflow, ties_brflow, flag_brflow,
                                  rstrings)
    if ierr:
        print 'psspy.aflowreal error = %d' % ierr
        return
    rflow = array2dict(rstrings, rdata)
    # Branch Flow Data - Complex
    xstrings = ['pq', 'pqloss', 'o_pq', 'o_pqloss']
    ierr, xdata = psspy.aflowcplx(sid, owner_brflow, ties_brflow, flag_brflow,
                                  xstrings)
    if ierr:
        print 'psspy.aflowcplx error = %d' % ierr
        return
    xflow = array2dict(xstrings, xdata)
    # Branch Flow Data - Character
    cstrings = [
        'id', 'fromname', 'fromexname', 'toname', 'toexname', 'nmetername',
        'nmeterexname'
    ]
    ierr, cdata = psspy.aflowchar(sid, owner_brflow, ties_brflow, flag_brflow,
                                  cstrings)
    if ierr:
        print 'psspy.aflowchar error = %d' % ierr
        return
    cflow = array2dict(cstrings, cdata)

    # ================================================================================================
    # PART 2: Export acquired results to Excel
    # ================================================================================================
    p, nx = os.path.split(savfile)
    n, x = os.path.splitext(nx)
    # Require path otherwise Excel stores file in My Documents directory
    xlsfile = get_output_filename(outpath, 'pout_' + n + '.xlsx')

    if os.path.exists(xlsfile):
        xlsfileExists = True
    else:
        xlsfileExists = False

    # Excel Specifications, Worksheet Size: 65,536 rows by 256 columns
    # Limit maximum data that can be exported to meet above Worksheet Size.
    maxrows, maxcols = 65530, 256
    # if required, validate number of rows and columns against these values

    # Start Excel, add a new workbook, fill it with acquired data
    xlApp = win32com.client.Dispatch("Excel.Application")

    # DisplayAlerts = True is important in order to save changed data.
    # DisplayAlerts = False suppresses all POP-UP windows, like File Overwrite Yes/No/Cancel.
    xlApp.DisplayAlerts = False

    # set this to True if want see Excel file, False if just want to save
    xlApp.Visible = show

    if xlsfileExists:  # file exist, open it and add worksheet
        xlApp.Workbooks.Open(xlsfile)
        xlBook = xlApp.ActiveWorkbook
        xlSheet = xlBook.Worksheets.Add()
    else:  # file does not exist, add workbook and select sheet (=1, default)
        xlApp.Workbooks.Add()
        xlBook = xlApp.ActiveWorkbook
        xlSheet = xlBook.ActiveSheet
        try:
            xlBook.Sheets("Sheet2").Delete()
            xlBook.Sheets("Sheet3").Delete()
        except:
            pass

    # Format Excel Sheet
    xlSheet.Columns.WrapText = False
    xlSheet.Columns.Font.Name = 'Courier New'
    xlSheet.Columns.Font.Size = 10

    nclns, rowvars, xlsclnsdict = exportedvalues()

    xlSheet.Columns(
        eval('"' + xlsclnsdict['DESC'] + ':' + xlsclnsdict['BUS'] +
             '"')).ColumnWidth = 6
    xlSheet.Columns(
        eval('"' + xlsclnsdict['BUSNAME'] + ':' + xlsclnsdict['BUSNAME'] +
             '"')).ColumnWidth = 18
    xlSheet.Columns(
        eval('"' + xlsclnsdict['CKT'] + ':' + xlsclnsdict['CKT'] +
             '"')).ColumnWidth = 3
    xlSheet.Columns(
        eval('"' + xlsclnsdict['MW'] + ':' + xlsclnsdict['MVA'] +
             '"')).ColumnWidth = 10
    xlSheet.Columns(
        eval('"' + xlsclnsdict['%I'] + ':' + xlsclnsdict['%I'] +
             '"')).ColumnWidth = 6
    xlSheet.Columns(
        eval('"' + xlsclnsdict['VOLTAGE'] + ':' + xlsclnsdict['MVARLOSS'] +
             '"')).ColumnWidth = 10
    xlSheet.Columns(
        eval('"' + xlsclnsdict['AREA'] + ':' + xlsclnsdict['ZONE'] +
             '"')).ColumnWidth = 4

    xlSheet.Columns(
        eval('"' + xlsclnsdict['MW'] + ':' + xlsclnsdict['MVA'] +
             '"')).NumberFormat = "0.00"
    xlSheet.Columns(
        eval('"' + xlsclnsdict['%I'] + ':' + xlsclnsdict['%I'] +
             '"')).NumberFormat = "0.00"
    xlSheet.Columns(
        eval('"' + xlsclnsdict['VOLTAGE'] + ':' + xlsclnsdict['MVARLOSS'] +
             '"')).NumberFormat = "0.00"

    xlSheet.Columns(
        eval('"' + xlsclnsdict['CKT'] + ':' + xlsclnsdict['CKT'] +
             '"')).HorizontalAlignment = -4108
    xlSheet.Columns(
        eval('"' + xlsclnsdict['AREA'] + ':' + xlsclnsdict['ZONE'] +
             '"')).HorizontalAlignment = -4108
    # Integer value -4108 is for setting alignment to "center"

    # Page steup
    xlSheet.PageSetup.Orientation = 2  #1: Portrait, 2:landscape
    xlSheet.PageSetup.LeftMargin = xlApp.InchesToPoints(0.5)
    xlSheet.PageSetup.RightMargin = xlApp.InchesToPoints(0.5)
    xlSheet.PageSetup.TopMargin = xlApp.InchesToPoints(0.25)
    xlSheet.PageSetup.BottomMargin = xlApp.InchesToPoints(0.5)
    xlSheet.PageSetup.HeaderMargin = xlApp.InchesToPoints(0.25)
    xlSheet.PageSetup.FooterMargin = xlApp.InchesToPoints(0.25)

    # ColorIndex Constants
    # BLACK       --> ColorIndex = 1
    # WHITE       --> ColorIndex = 2
    # RED         --> ColorIndex = 3
    # GREEN       --> ColorIndex = 4
    # BLUE        --> ColorIndex = 5
    # PURPLE      --> ColorIndex = 7
    # LIGHT GREEN --> ColorIndex = 43

    # ------------------------------------------------------------------------------------------------
    # Report Title
    colstart = 1
    row = 1
    col = colstart
    xlSheet.Cells(row, col).Value = "POWER FLOW OUTPUT REPORT"
    xlSheet.Cells(row, col).Font.Bold = True
    xlSheet.Cells(row, col).Font.Size = 14
    xlSheet.Cells(row, col).Font.ColorIndex = 7

    row += 1
    xlSheet.Cells(row, col).Value = savfile

    row += 1
    xlSheet.Cells(row, col).Value = titleline1

    row += 1
    xlSheet.Cells(row, col).Value = titleline2

    row += 2
    tr, lc, br, rc = row, 1, row, nclns  #toprow, leftcolumn, bottomrow, rightcolumn
    xlSheet.Range(xlSheet.Cells(tr, lc + 1),
                  xlSheet.Cells(br, rc)).Value = rowvars[1:]
    xlSheet.Range(xlSheet.Cells(tr, lc), xlSheet.Cells(br,
                                                       rc)).Font.Bold = True
    xlSheet.Range(xlSheet.Cells(tr, lc), xlSheet.Cells(br,
                                                       rc)).Font.ColorIndex = 3
    xlSheet.Range(xlSheet.Cells(tr, lc),
                  xlSheet.Cells(br, rc)).VerticalAlignment = -4108
    xlSheet.Range(xlSheet.Cells(tr, lc),
                  xlSheet.Cells(br, rc)).HorizontalAlignment = -4108

    clnlabelrow = row
    row += 1  # add blank row after lables

    # Worksheet Headers and Footer
    # Put Title and ColumnHeads on top of each page
    rows2repeat = "$" + str(1) + ":$" + str(row)
    xlSheet.PageSetup.PrintTitleRows = rows2repeat

    xlSheet.PageSetup.LeftFooter = "PF Results: " + savfile
    xlSheet.PageSetup.RightFooter = "&P of &N"

    # ------------------------------------------------------------------------------------------------
    for i, bus in enumerate(ibuses['number']):

        # select bus and put bus data in a row

        rd = initdict(rowvars)
        rd['BUS'] = bus
        rd['BUSNAME'] = cbuses['exname'][i]
        rd['VOLTAGE'] = rbuses['pu'][i]
        rd['AREA'] = ibuses['area'][i]
        rd['ZONE'] = ibuses['zone'][i]

        row += 1
        rowvalues = [rd[each] for each in rowvars]
        xlSheet.Range(xlSheet.Cells(row, col),
                      xlSheet.Cells(row, nclns)).Value = rowvalues
        xlSheet.Range(xlSheet.Cells(row, col),
                      xlSheet.Cells(row, nclns)).Font.Bold = True
        xlSheet.Range(xlSheet.Cells(row, col),
                      xlSheet.Cells(row, nclns)).Font.ColorIndex = 5

        # check generation on selected bus
        plantbusidxes = busindexes(bus, iplants['number'])

        for idx in plantbusidxes:
            pcti = rplants['percent'][idx]
            if pcti == 0.0: pcti = ''
            rd = initdict(rowvars)
            rd['DESC'] = 'FROM'
            rd['BUSNAME'] = 'GENERATION'
            rd['MW'] = rplants['pgen'][idx]
            rd['MVAR'] = rplants['qgen'][idx]
            rd['MVA'] = rplants['mva'][idx]
            rd['%I'] = pcti
            rd['VOLTAGE'] = rplants['kv'][idx]

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues

        # check total load on selected bus
        if bus in totalmva:
            rd = initdict(rowvars)
            rd['DESC'] = 'TO'
            rd['BUSNAME'] = 'LOAD-PQ'
            rd['MW'] = totalmva[bus].real
            rd['MVAR'] = totalmva[bus].imag
            rd['MVA'] = abs(totalmva[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues

        if bus in totalil:
            rd = initdict(rowvars)
            rd['DESC'] = 'TO'
            rd['BUSNAME'] = 'LOAD-I'
            rd['MW'] = totalil[bus].real
            rd['MVAR'] = totalil[bus].imag
            rd['MVA'] = abs(totalil[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues

        if bus in totalyl:
            rd = initdict(rowvars)
            rd['DESC'] = 'TO'
            rd['BUSNAME'] = 'LOAD-Y'
            rd['MW'] = totalyl[bus].real
            rd['MVAR'] = totalyl[bus].imag
            rd['MVA'] = abs(totalyl[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues
        '''
        if bus in totalload:
            rd             = initdict(rowvars)
            rd['DESC']     = 'TO'
            rd['BUSNAME']  = 'LOAD-TOTAL'
            rd['MW']       = totalload[bus].real
            rd['MVAR']     = totalload[bus].imag
            rd['MVA']      = abs(totalload[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row,col),xlSheet.Cells(row,nclns)).Value = rowvalues
        '''

        if bus in totalys:
            rd = initdict(rowvars)
            rd['DESC'] = 'TO'
            rd['BUSNAME'] = 'SHUNT'
            rd['MW'] = totalys[bus].real
            rd['MVAR'] = totalys[bus].imag
            rd['MVA'] = abs(totalys[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues

        if bus in totalysw:
            rd = initdict(rowvars)
            rd['DESC'] = 'TO'
            rd['BUSNAME'] = 'SWITCHED SHUNT'
            rd['MW'] = totalysw[bus].real
            rd['MVAR'] = totalysw[bus].imag
            rd['MVA'] = abs(totalysw[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues
        """
        # Sometimes load/shunt/switch shunt area/owner/zone's could be different than the bus
        # to which it is connected. So when producing subsystem based reports, these equipment
        # might get excluded.

        # check loads on selected bus
        loadbusidxes=busindexes(bus,iloads['number'])
        pq_p = 0; pq_q = 0
        il_p = 0; il_q = 0
        yl_p = 0; yl_q = 0
        for idx in loadbusidxes:
            pq_p += xloads['mvaact'][idx].real
            pq_q += xloads['mvaact'][idx].imag
            il_p += xloads['ilact'][idx].real
            il_q += xloads['ilact'][idx].imag
            yl_p += xloads['ylact'][idx].real
            yl_q += xloads['ylact'][idx].imag

        pq_mva = abs(complex(pq_p,pq_q))
        il_mva = abs(complex(il_p,il_q))
        yl_mva = abs(complex(yl_p,yl_q))

        if pq_mva:  #PQ Loads
            rd             = initdict(rowvars)
            rd['DESC']     = 'TO'
            rd['BUSNAME']  = 'LOAD-PQ'
            rd['MW']       = pq_p
            rd['MVAR']     = pq_q
            rd['MVA']      = pq_mva

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row,col),xlSheet.Cells(row,nclns)).Value = rowvalues

        if il_mva:   #I Loads
            rd             = initdict(rowvars)
            rd['DESC']     = 'TO'
            rd['BUSNAME']  = 'LOAD-I'
            rd['MW']       = il_p
            rd['MVAR']     = il_q
            rd['MVA']      = il_mva

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row,col),xlSheet.Cells(row,nclns)).Value = rowvalues

        if yl_mva:   #Y Loads
            rd             = initdict(rowvars)
            rd['DESC']     = 'TO'
            rd['BUSNAME']  = 'LOAD-Y'
            rd['MW']       = yl_p
            rd['MVAR']     = yl_q
            rd['MVA']      = yl_mva

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row,col),xlSheet.Cells(row,nclns)).Value = rowvalues

        # check shunts on selected bus
        if abs(xbuses['shuntact'][i]):
            rd             = initdict(rowvars)
            rd['DESC']     = 'TO'
            rd['BUSNAME']  = 'SHUNT'
            rd['MW']       = xbuses['shuntact'][i].real
            rd['MVAR']     = xbuses['shuntact'][i].imag
            rd['MVA']      = abs(xbuses['shuntact'][i])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row,col),xlSheet.Cells(row,nclns)).Value = rowvalues

        # check switched shunts on selected bus
        swshbusidxes=busindexes(bus,iswsh['number'])
        pswsh = 0; qswsh = 0
        for idx in swshbusidxes:
            pswsh += xswsh['yswact'][idx].real
            qswsh += xswsh['yswact'][idx].imag
        mvaswsh = abs(complex(pswsh,qswsh))
        if mvaswsh:
            rd             = initdict(rowvars)
            rd['DESC']     = 'TO'
            rd['BUSNAME']  = 'SWITCHED SHUNT'
            rd['MW']       = pswsh
            rd['MVAR']     = qswsh
            rd['MVA']      = mvaswsh

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row,col),xlSheet.Cells(row,nclns)).Value = rowvalues
        """

        # check connected branches to selected bus
        flowfrombusidxes = busindexes(bus, iflow['fromnumber'])
        for idx in flowfrombusidxes:
            if iflow['tonumber'][
                    idx] < 10000000:  #don't process 3-wdg xmer star-point buses
                tobusidx = busindexes(iflow['tonumber'][idx],
                                      ibusesall['number'])
                tobusVpu = rbusesall['pu'][tobusidx[0]]
                tobusarea = ibusesall['area'][tobusidx[0]]
                tobuszone = ibusesall['zone'][tobusidx[0]]
                pcti = rflow['pctrate'][idx]
                if pcti == 0.0: pcti = ''

                rd = initdict(rowvars)
                rd['DESC'] = 'TO'
                rd['BUS'] = iflow['tonumber'][idx]
                rd['BUSNAME'] = cflow['toexname'][idx]
                rd['CKT'] = cflow['id'][idx]
                rd['MW'] = rflow['p'][idx]
                rd['MVAR'] = rflow['q'][idx]
                rd['MVA'] = rflow['mva'][idx]
                rd['%I'] = pcti
                rd['VOLTAGE'] = tobusVpu
                rd['MWLOSS'] = rflow['ploss'][idx]
                rd['MVARLOSS'] = rflow['qloss'][idx]
                rd['AREA'] = tobusarea
                rd['ZONE'] = tobuszone

                row += 1
                rowvalues = [rd[each] for each in rowvars]
                xlSheet.Range(xlSheet.Cells(row, col),
                              xlSheet.Cells(row, nclns)).Value = rowvalues

        # Bus Mismatch
        if bus in busmsm:
            rd = initdict(rowvars)
            rd['BUSNAME'] = 'BUS MISMATCH'
            rd['MW'] = busmsm[bus].real
            rd['MVAR'] = busmsm[bus].imag
            rd['MVA'] = abs(busmsm[bus])

            row += 1
            rowvalues = [rd[each] for each in rowvars]
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Value = rowvalues
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Font.ColorIndex = 10
            xlSheet.Range(xlSheet.Cells(row, col),
                          xlSheet.Cells(row, nclns)).Font.Bold = True

            # xlEdgeTop border set to xlThin
            xlSheet.Range(xlSheet.Cells(row, col + 4),
                          xlSheet.Cells(row, col + 6)).Borders(8).Weight = 2

        # Insert EdgeBottom border(Borders(9)) with xlThin weight(2)
        xlSheet.Range(xlSheet.Cells(row, col),
                      xlSheet.Cells(row, nclns)).Borders(9).Weight = 2

        # Insert blank row
        row += 1

    # ------------------------------------------------------------------------------------------------
    # Draw borders
    # Column Lable Row
    # xlEdgeTop border set to xlThin
    xlSheet.Range(xlSheet.Cells(clnlabelrow, 1),
                  xlSheet.Cells(clnlabelrow, nclns)).Borders(8).Weight = 2
    # xlEdgeBottom border set to xlThin
    xlSheet.Range(xlSheet.Cells(clnlabelrow, 2),
                  xlSheet.Cells(clnlabelrow, nclns)).Borders(9).Weight = 2

    # Remaining WorkSheet
    # xlEdgeLeft border set to xlThinline
    xlSheet.Range(xlSheet.Cells(clnlabelrow, 1),
                  xlSheet.Cells(row - 1, nclns)).Borders(7).Weight = 2
    # xlEdgeRight border set to xlThinline
    xlSheet.Range(xlSheet.Cells(clnlabelrow, 1),
                  xlSheet.Cells(row - 1, nclns)).Borders(10).Weight = 2
    # xlInsideVertical border set to xlHairline
    xlSheet.Range(xlSheet.Cells(clnlabelrow, 1),
                  xlSheet.Cells(row - 1, nclns)).Borders(11).Weight = 1

    # ------------------------------------------------------------------------------------------------
    # Save the workbook and close the Excel application

    if xlsfile:  # xls file provided
        xlBook.SaveAs(Filename=xlsfile)
    else:
        xlsbookfilename = os.path.join(
            os.getcwd(), xlBook.Name)  # xlBook.Name returns without '.xls'
        xlBook.SaveAs(Filename=xlsbookfilename)
        xlsbookfilename = os.path.join(
            os.getcwd(), xlBook.Name)  # xlBook.Name returns '.xls' extn.

    if not show:
        xlBook.Close()
        xlApp.Quit()
        txt = '\n Power Flow Results saved to file %s' % xlsfile
        sys.stdout.write(txt)
Beispiel #11
0
def getbus(region):
    region = region - 1
    psspy.bsys(0, 0, [0.6, 345.], 1, [region], 0, [], 0, [], 0, [])
    ierr, busInterestedRegion = psspy.abusint(1, 1, ['number'])
    busInterestedRegion = busInterestedRegion[0]
    return busInterestedRegion
##ierr = psspy.branch_data(44109, 44119, "4", intgar1=1, realar1=0.002781434,
##                         realar2=0.008661488, realar3=0.001763027)
##ierr = psspy.seq_branch_data(44109, 44119, "4", realar1=0.007021062,
##                             realar2=0.028150285, realar3=0.001087962)

#---------------------------------
# CTPS1 generation off
ierr = psspy.machine_data_2(44015, intgar1=0) #CTPS gen1
ierr = psspy.machine_data_2(44016, intgar1=0) #CTPS gen2
ierr = psspy.machine_data_2(44017, intgar1=0) #CTPS gen3
ierr = psspy.load_data_3(44106, realar1=184.5) #CHANDRAPURA
ierr = psspy.load_data_3(44202, realar1=300.) #CTPS2

###-------------------------------
ierr = psspy.bsys(sid=1, numzone=1, zones=44)
ierr, all_buses = psspy.abusint(sid=1, string=["NUMBER"], flag=2)
all_buses = all_buses[0]
ierr, in_buses = psspy.abusint(sid=1, string=['NUMBER'], flag=1)
in_buses = in_buses[0]
out_buses = list(set(all_buses)-set(in_buses))
print out_buses
ierr = psspy.bsys(sid=2, numbus=len(out_buses), buses=out_buses)
ierr = psspy.extr(sid=2, all=0, status2=0)

##for i in range(len(out_buses)):
##     psspy.load_data_3(i, intgar1=0)
##
##ierr = psspy.bsys(sid=2, numbus=len(out_buses), buses=out_buses)
##ierr, in_branches = psspy.abrnint(sid=2, string=['FROMNUMBER', 'TONUMBER'],
##                                  flag=1)
##for i in range(len(in_branches[0])):
Beispiel #13
0
def findBusesArea(buses):
    psspy.bsys(sid=1, numbus=len(buses), buses=buses)
    ierr,buses_area = psspy.abusint(1, 1, ['AREA'])
    buses_area = buses_area[0]
    return buses_area
Beispiel #14
0
def findAllBuses():
    psspy.bsys(0, 0, [0.0, 0.0], 1, [1], 0, [], 0, [], 0, [])
    ierr, all_bus = psspy.abusint(0, 1, ['number'])
    all_bus= all_bus[0]
    return all_bus
        psspy.psseinit(1000000)

        case = address + '\\' + caseX + '.raw'
        #case = address + '\\' + caseX + '.sav'

        #psspy.case(case) #this is for sav file
        psspy.read(0, case)  #this is for raw file
        psspy.fnsl([0, 0, 0, 1, 1, 0, 0, 0])
        #psspy.rawd_2(0,1,[1,1,1,0,0,0,0],0, address + '\\' + caseX +'.raw')

        # parse the con file to make sure there is no swing bus generator contingency,
        # and construct a dictionary for the generator contingencies
        print(
            '------------------start checking swing bus in con file -------------------'
        )
        ierr, iarray = psspy.abusint(-1, 1, 'NUMBER')
        vtmpbusno = iarray[0]
        ierr, iarray = psspy.abusint(-1, 1, 'TYPE')
        vtmpbustype = iarray[0]
        swingbus_str = []
        swingbus_tmp = []
        for ibus in range(0, len(vtmpbusno)):
            if vtmpbustype[ibus] == 3:
                swingbus_str.append(str(vtmpbusno[ibus]))
                swingbus_tmp.append(vtmpbusno[ibus])

        fcon = open(fileCon)
        conlines = fcon.readlines()
        cont_con_array = []
        cont_gen_dict = {}
        cont_genbus_array = []
Beispiel #16
0
        sys.stdout = file_object
        yield
    finally:
        sys.stdout = old_stdout


output = StringIO.StringIO()
with silence(output):

    psspy.psseinit(80000)  # initialize PSS\E in python
    savecase = 'ieee118bus_PSSEcorrected_rq.sav'
    psspy.case(savecase)

    # find all the buses
    psspy.bsys(0, 0, [0.0, 0.0], 1, [1], 0, [], 0, [], 0, [])
    ierr, all_bus = psspy.abusint(0, 1, ['number'])
    bus_num = all_bus[0]

    #List of all machines
    psspy.bsys(sid=1, numbus=len(bus_num), buses=bus_num)
    ierr, machine_bus = psspy.amachint(1, 1, ['NUMBER'])
    machine_bus = machine_bus[0]
    ierr, machine_id = psspy.amachchar(1, 1, ['ID'])
    machine_id = machine_id[0]

    #List of all load
    psspy.bsys(sid=1, numbus=len(bus_num), buses=bus_num)
    ierr, load_bus = psspy.alodbusint(1, 1, ['NUMBER'])
    load_bus = load_bus[0]
    ierr, load_id = psspy.aloadchar(1, 1, ['ID'])
    load_id = load_id[0]
Beispiel #17
0
import psspy
import redirect
redirect.psse2py()
psspy.psseinit(10000)
psspy.case(r'C:\Documents and Settings\xw0419\Mes documents\Mon Projet\Simulation\HQ_PSSmodel\HQ1_shuntadded.sav')
psspy.fnsl(
    options1=0,
    options5=0
    )
ierr, volt = psspy.abusreal(-1, string="PU")        #voltage at buses in normal condition

ierr=psspy.load_data_3(i=303,
                       REALAR2=600
                       )
ierr, buses = psspy.abusint(-1, string="NUMBER")
psspy.fnsl(
    options1=0,
    options5=0
    )
ierr, voltd = psspy.abusreal(-1, string="PU")
n=len(volt[0][:])
dv=[[0]*n]
for i in range(0,n-1):
    dv[0][i]=voltd[0][i]-volt[0][i]

import excelpy
x1=excelpy.workbook(r'C:\Documents and Settings\xw0419\Mes documents\Mon Projet\Simulation\IREQ\PythonProgs\inoutdata.xls',sheet="Feuil1",overwritesheet=True, mode='w')
x1.show()
x1.show_alerts(False)
x1.set_cell('a1','Bus No.')
    # choose current percentage
    current_percentage = percentage_set[index]

    # choose the case
    case_name_constant = "IEEE_118_%s.sav"
    PSSE_CASE = case_name_constant % current_percentage
    psspy.case(PSSE_CASE)

    # silent the output
    output = StringIO.StringIO()
    with silence(output):

        #pick the bus in Dominion Area 345 is the index of Dominion
        psspy.case(savecase)
        # psspy.bsys(0,0,[ 0.2, 999.],1,[],0,[],0,[],0,[])
        ierr, all_bus = psspy.abusint(-1, 1, ['NUMBER'])
        bus_num = all_bus[0]

        #Load Bus
        psspy.bsys(sid=1, numbus=len(bus_num), buses=bus_num)
        ierr, load_bus = psspy.alodbusint(1, 1, ['NUMBER'])
        load_bus = load_bus[0]

        # Gen Bus
        psspy.bsys(sid=1, numbus=len(bus_num), buses=bus_num)
        ierr, gen_bus = psspy.agenbusint(1, 1, ['NUMBER'])
        gen_bus = gen_bus[0]

        #change the load and the generation
        percentage = 1 - (current_percentage - 5) / current_percentage
        pssepylib.change_load(load_bus, percentage)
Beispiel #19
0
def randdist(argin):

    filename = argin[0]  # initializing input arguments
    maxdist = argin[1]
    mode = argin[2]
    shuntctrlmode = argin[3]
    capbus = argin[4]
    capstep = argin[5]
    capQ = argin[6]
    pilot = argin[7]
    ndist = argin[8]
    nswitch = argin[9]
    reloadfile = 1
    # By default the network data file will be reloaded at each iteration
    if len(argin) == 11:
        reloadfile = argin[10]
    psspy.case(filename)
    ierr, Qload = psspy.aloadcplx(
        -1, string="MVAACT"
    )  # Qload is the amount of apparent power (P+jQ) for all loads
    Qload = [
        x.imag for x in Qload[0]
    ]  # We just need Reactive power (Q) so we find imaginary part of apparent power
    ierr, tlbuses = psspy.aloadint(
        string='NUMBER'
    )  # tlbuses is all loads' bus nomber, it includes also the compensators that we have added as loads to the network
    nbus = len(tlbuses[0])  # nbus is No. of all load buses
    ncap = len(capbus)
    ierr, busn = psspy.abusint(-1, string='NUMBER')

    npn = len(pilot)

    volt = []
    voltd = []
    vpn = [[0 for x in range(npn)] for y in range(ndist * nswitch)]
    vpnd = [[0 for x in range(npn)] for y in range(ndist * nswitch)]
    dvpn = [[0 for x in range(npn)] for y in range(ndist * nswitch)]
    switch = [[0 for x in range(ncap)] for y in range(ndist * nswitch)]
    for i in range(
            ndist
    ):  # in this loop we generate ndist random distrurbane cases and apply nswitch control actions on each case
        if reloadfile == 1:
            psspy.case(filename)
        percentage = random.random(
        ) * maxdist  # choose randomly the amount of disturbance with maximum of maxdist
        zoseq = [0, 1] * nbus
        if mode == 'random':
            sb = random.sample(
                zoseq, nbus)  # choose randomly loads to apply the disturbance
        if mode == 'all': sb = [1] * nbus
        capQd = [0 for x in range(ncap)]
        for j in range(nbus):  # applying the disturbance
            if sb[j] == 1:
                ##                if not(tlbuses[0][j] in capbus):          # we make sure that no dist. applied on capacitor buses (which are considered also as loads)
                Qd = Qload[j] + percentage / 100.0 * abs(Qload[j])
                ierr = psspy.load_data_3(i=tlbuses[0][j], REALAR2=Qd)
            if tlbuses[0][j] in capbus:
                capidx = capbus.index(tlbuses[0][j])
                if sb[j] == 1:
                    capQd[capidx] = Qd
                else:
                    capQd[capidx] = Qload[j]
        if shuntctrlmode == 1:  # by this option we can unlock all compensators over the network
            for j in tlbuses[0]:
                ierr = psspy.switched_shunt_data_3(j, intgar9=1)

        print '###### DIST(' + str(i) + ') ########'
        psspy.fdns(OPTIONS1=0, OPTIONS5=0,
                   OPTIONS6=1)  # do power flow on the disturbed system
        ierr, v = psspy.abusreal(
            -1, string="PU"
        )  # and measure voltage at all buses after disturbance (there is no option in PSS to measure the voltage of just one bus)

        for j in range(
                nswitch):  # now we apply random cap. switchings nswitch times
            [scb, ss, qss] = capselect(capbus, capstep, capQ)

            print '### ADD CAP ###'
            for k in range(len(scb)):
                scbidx = capbus.index(scb[k])
                switch[i * nswitch + j][scbidx] = ss[k]

                capQd[scbidx] = capQd[scbidx] - ss[k] * qss[k]
                ierr = psspy.load_data_3(i=scb[k], REALAR2=capQd[scbidx])

            print '###### DIST(' + str(i) + ') , ' + 'SWITCHCASE(' + str(
                i * nswitch + j) + ') ########'
            psspy.fdns(OPTIONS1=0, OPTIONS5=0,
                       OPTIONS6=1)  # do power flow on the disturbed system
            ierr, vd = psspy.abusreal(
                -1, string="PU"
            )  # and measure voltage at all buses after disturbance (there is no option in PSS to measure the voltage of just one bus)
            voltd.append(vd)
            volt.append(v)
            for k in range(npn):  # measuring vpn, and vpnd as outputs
                pnidx = busn[0].index(pilot[k])
                vpn[i * nswitch + j][k] = v[0][pnidx]
                vpnd[i * nswitch + j][k] = vd[0][pnidx]
                dvpn[i * nswitch +
                     j][k] = vpnd[i * nswitch + j][k] - vpn[i * nswitch + j][k]
            print '### REMOVE CAP ###'
            for k in range(
                    len(scb)
            ):  # after cap switchings we remove their effect for next switching
                scbidx = capbus.index(scb[k])
                capQd[scbidx] = capQd[scbidx] + ss[k] * qss[k]
                ierr = psspy.load_data_3(i=scb[k], REALAR2=capQd[scbidx])

    return volt, voltd, vpn, vpnd, dvpn, switch
Beispiel #20
0
)  # load psse model defined by casestudy. since ardat function give areal losses we need these IDs to sum up all areal losses to find overall network loss
ierr, areas = psspy.aareaint(-1, 1, 'NUMBER')  # id of areas in the network.
psspy.fdns(
    OPTIONS1=0, OPTIONS5=0,
    OPTIONS6=1)  # run power flow in psse model using decoupled newton method
PLOSS1 = 0
for i in areas[
        0]:  # evaluating sum of losses in all areas, before compensation
    ierr, area_loss = psspy.ardat(iar=i, string='LOSS')
    PLOSS1 = PLOSS1 + area_loss.real

ierr, vpu1 = psspy.abusreal(
    -1, string="PU")  # voltage at all buses, before compensation
vpu01 = vpu1[0]

ierr, busidx = psspy.abusint(-1, string='NUMBER')  # find All buses
busidx0 = []
nbus = len(busidx0)  # No. of all buses
ierr, PVidx = psspy.agenbusint(-1, 1, 'NUMBER')  # find PV buses
for idx in busidx[0]:  # find PQ buses by removing PV buses from All buses
    if idx not in PVidx[0]:
        busidx0.append(idx)

Vmin = 0.98  # here you can define your desired Vmin and Vmax
Vmax = 1.02

# next three line import methods from deap class to define genetic algorithm. to find out more go to deap library documentation
from deap import base
from deap import creator
from deap import tools
Beispiel #21
0
psspy.case(CASOsav)
#psspy.fnsl([0,0,0,1,1,1,99,0])
#psspy.save(CASOsav)

#Volvemos a simular
psspy.fdns([0, 0, 0, 1, 1, 1, 99, 0])
U = psspy.solv()

#bus_i = 798
#bus_f = 734
#ierr, line_rx = psspy.brndt2( bus_i, bus_f, '1', 'RXZ')

##############################################################################
## Lee los buses del modelo
##############################################################################
ierr, busnumbers = psspy.abusint(sid=-1, string="NUMBER")
ierr, busnames = psspy.abuschar(sid=-1, string="NAME")
ierr, busvoltages = psspy.abusreal(sid=-1, string="BASE")
#ierr, bus_shunts = psspy.abuscplx(sid=-1, string="SHUNTACT")

##############################################################################
## Instrucciones para recorrer todos los buses y almacenar aquellos que
## verifiquen el criterio de tension base = 132 kV
##############################################################################
BUSLIST = []
BUSNAME = []
#Nbus = len( busnumbers[0])
for nb in busnumbers[0]:
    bus_index = busnumbers[0].index(nb)
    voltage = busvoltages[0][bus_index]
    name = busnames[0][bus_index]
Beispiel #22
0
    #os.remove(savefile)
    with open(savefile, 'a') as f1:
        for index in range(0, stepNumber):

            record_inspected_bus_current = []
            record_inspected_bus_line_bus_info = []
            inspected_bus_number = 64

            # silent the output
            output = StringIO.StringIO()
            with silence(output):

                # pick the bus in Dominion Area 345 is the index of Dominion
                psspy.case(savecase)
                ierr, all_bus = psspy.abusint(-1, 1, ['number'])
                bus_num = all_bus[0]

                # Load Bus
                psspy.bsys(sid=-1, numbus=len(bus_num), buses=bus_num)
                ierr, load_bus = psspy.alodbusint(-1, 1, ['NUMBER'])
                load_bus = load_bus[0]

                # Gen Bus
                psspy.bsys(sid=-1, numbus=len(bus_num), buses=bus_num)
                ierr, gen_bus = psspy.agenbusint(-1, 1, ['NUMBER'])
                gen_bus = gen_bus[0]

                # Choose the proper case
                current_percentage = (1 + percent_set[index]) * 100
                level = math.ceil(current_percentage / 5)
Beispiel #23
0
    def read_raw(self):
        ''' Read the raw file.'''

        # Read bus numbers
        ierr, bus_numbers = psspy.abusint(-1, 2, 'NUMBER')

        assert ierr == 0, 'Error with reading bus numbers'

        # Reads voltage levels at buses stored in self.busNumbers
        ierr, voltage_levels = psspy.abusreal(-1, 2, 'PU')

        assert ierr == 0, 'Error reading voltage levels'

        # Reads voltage levels at buses stored in self.busNumbers
        ierr, voltage_angles = psspy.abusreal(-1, 2, 'ANGLED')

        assert ierr == 0, 'Error reading voltage angles'

        # Creates a Python dictionary containing bus numbers as keys and associates
        # a dictionary with voltage and angle to each of the keys
        for bus, voltage, angle in zip(bus_numbers[0], voltage_levels[0],
                                       voltage_angles[0]):
            self.buses[bus] = {'voltage': voltage, 'angle': angle}

        # Reads and stores bus numbers where generators are connected
        ierr, [machine_bus_numbers] = psspy.amachint(-1, 4, 'NUMBER')
        ierr, [machine_ids] = psspy.amachchar(-1, 4, 'ID')
        assert ierr == 0, 'Error reading generator bus numbers'

        # Reads and stores active and reactive powers of each generator
        ierr1, [machine_power_p] = psspy.amachreal(-1, 4, 'PGEN')
        ierr2, [machine_power_q] = psspy.amachreal(-1, 4, 'QGEN')

        assert ierr1 == 0 and ierr2 == 0, 'Error with reading active and reactive powers'

        # Creates a Python dictionary containing keys in form of
        # "BUSNUMBER_MACHINEID" and associates a dictionary with active and
        # reactive powers to each of the keys
        for k in range(0, len(machine_ids)):
            self.machines[(str(machine_bus_numbers[k]) + '_' +
                           machine_ids[k][:-1])] = {
                               'bus': machine_bus_numbers[k],
                               'P': machine_power_p[k],
                               'Q': machine_power_q[k]
                           }

        # Reads and stores bus numbers where loads are connected
        ierr, [load_bus_numbers] = psspy.aloadint(-1, 4, 'NUMBER')
        ierr, [load_ids] = psspy.aloadchar(-1, 4, 'ID')
        assert ierr == 0, 'Error reading load bus numbers'

        # Reads and stores active and reactive powers of each load
        ierr1, [load] = psspy.aloadcplx(-1, 4, 'TOTALACT')
        load_power_p = []
        load_power_q = []
        for cplxload in load:
            load_power_p.append(cplxload.real)
            load_power_q.append(cplxload.imag)

        assert ierr1 == 0, 'Error with reading active and reactive powers'

        # Creates a Python dictionary containing keys in form of
        # "BUSNUMBER_LOADID" and associates a dictionary with active and
        # reactive powers to each of the keys

        for load, bus, active, reactive in zip(load_ids, load_bus_numbers,
                                               load_power_p, load_power_q):
            self.loads[(str(bus) + '_' + load[:-1])] = {
                'bus': bus,
                'P': active,
                'Q': reactive
            }

        # Reads and stores bus numbers where 2WindingTrafos are connected
        ierr1, [two_w_trafo_from] = psspy.atrnint(-1, 1, 1, 2, 1, 'FROMNUMBER')
        ierr2, [two_w_trafo_to] = psspy.atrnint(-1, 1, 1, 2, 1, 'TONUMBER')

        assert ierr1 == 0 and ierr2 == 0, 'Error reading trafo bus numbers'

        # Reads and stores 2WindingTrafo ratios taking into account the primary side
        ierr1, [two_w_trafo_ratio1] = psspy.atrnreal(-1, 1, 1, 2, 1, 'RATIO')
        ierr2, [two_w_trafo_ratio2] = psspy.atrnreal(-1, 1, 1, 2, 1, 'RATIO2')

        assert ierr1 == 0 and ierr2 == 0, 'Error reading trafo bus numbers'

        # Creates a Python dictionary containing keys in form of
        # "BUSNUMBER_LOADID" and associates a dictionary with active and
        # reactive powers to each of the keys
        for f_bus, to_bus, ratio1, ratio2 in zip(two_w_trafo_from,
                                                 two_w_trafo_to,
                                                 two_w_trafo_ratio1,
                                                 two_w_trafo_ratio2):
            self.trafos[(str(f_bus) + '_' + str(to_bus))] = {
                'fromBus': f_bus,
                'toBus': to_bus,
                't1': ratio1,
                't2': ratio2
            }
        sys.stdout = file_object
        yield
    finally:
        sys.stdout = old_stdout

output = StringIO.StringIO()
with silence(output):

    psspy.psseinit(80000)             # initialize PSS\E in python
    savecase = 'IEEE 57 bus.sav'
    psspy.case(savecase)


    # find all the buses
    psspy.bsys(0,0,[0.0,0.0],1,[1],0,[],0,[],0,[])
    ierr,all_bus = psspy.abusint(0,1,['number'])
    bus_num = all_bus[0]

    #List of all machines
    psspy.bsys(sid = 1,numbus = len(bus_num), buses = bus_num)
    ierr,machine_bus = psspy.amachint(1,1,['NUMBER'])
    machine_bus = machine_bus[0]
    ierr,machine_id =  psspy.amachchar(1,1,['ID'])
    machine_id = machine_id[0]

    #List of all Gen
    psspy.bsys(sid = 1,numbus = len(bus_num), buses = bus_num)
    ierr,gen_bus = psspy.agenbusint(1,1,['NUMBER'])
    gen_bus = gen_bus[0]

    #List of all load