Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Python script for dynamics run - does it need to be improved?

def ContingencyFiles(idvfiles): idev = [a for a in listdir(idvfiles) if isfile(join(idvfiles, a))] idevinfile = [] for i in idev: idevinfile.append(os.path.splitext(i)[0]) return idev_infile

def ConvertedFiles(cnvfiles): cnv = [b for b in listdir(cnvfiles) if isfile(join(cnvfiles, b))] cnvinfile = [] for j in cnv: cnvinfile.append(os.path.splitext(j)[0]) return cnv_infile

def SnapshotFiles(snpfiles): snp = [c for c in listdir(snpfiles) if isfile(join(snpfiles, c))] snpinfile = [] for k in snp: snpinfile.append(os.path.splitext(k)[0]) return snp_infile

def log_out(directory): try: if not os.path.exists(directory): os.makedirs(directory) except OSError: print ('Error: Creating directory. ' + directory)

*******Main Program********

*******************

if name == 'main': import os, sys from os import listdir from os.path import isfile, join from os.path import splitext import time

*******Set PSS/E Path******

pssedir = r"""C:\Program Files (x86)\PTI\PSSE32\PSSBIN"""

sys.path.append (pssedir)
os.environ["PATH"] = os.environ["PATH"] + ";" + pssedir

*****Define Working Directory******

study_folder = r"""D:/Studies/BBB/"""
idv_files = r"""D:/Studies/BBB/contingency/"""
cnv_files = r"""D:/Studies/BBB/converted/"""
snp_files = r"""D:/Studies/BBB/snapshot/"""

******Import psspy modules*****

import psspy
import redirect
_i = psspy.getdefaultint()
_f = psspy.getdefaultreal()
_c = psspy.getdefaultchar()

****Redirect PSS/E Output to Python****

redirect.psse2py()

*******Initialize PSS/E******

psspy.psseinit(150000)

******Run Dynamic Simulation*****

for x, y in zip(Converted_Files(cnv_files),Snapshot_Files(snp_files)):
    log_out(x)
    for z in Contingency_Files(idv_files):
        print 'Simulation executed at time ', time.ctime()
        print 'The snapshot file is ', y + '.'
        print 'The case file is ', x + '.'
        print 'The Fault ID is ', z + '.'
        print 'Processing dynamic simulation for Fault ID ', z + '...'
        psspy.progress_output(2, study_folder + x + "/" + z + '.pdev', [0,0])
        psspy.prompt_output(2, study_folder + x + "/" + z + '.odev', [0,0])
        try:
            psspy.rstr(snp_files + y + '.snp')
        except:
            print 'Error while opening the file ', y
            sys.exit(0)
        psspy.powerflowmode()
        try: 
            psspy.case(cnv_files + x + '.cnv')
        except:
            print 'Error while opening the file ', x
            sys.exit(0)
        psspy.ordr(0)
        psspy.fact()
        psspy.tysl(0)
        psspy.dynamicsmode(0)
        psspy.strt(1, study_folder + x + "/" + z + '.out')
        psspy.runrspnsfile(idv_files + z + '.idv')
        psspy.run(1,15,60,3,0)
        psspy.progress_output(1,'', [0,0])
        psspy.prompt_output(1,'', [0,0])
        print 'Simulation finished at time ', time.ctime()
        print 'Dynamic simulation for Fault ID ', z + ' complete! \n\n'

print 'Total Simulation finished at time ', time.ctime()
print '\nEnd of Dynamic Simulation \n'
click to hide/show revision 2
No.2 Revision

Python script for dynamics run - does it need to be improved?

 def ContingencyFiles(idvfiles):
Contingency_Files(idv_files):
    idev = [a for a in listdir(idvfiles) listdir(idv_files) if isfile(join(idvfiles, isfile(join(idv_files, a))]
    idevinfile idev_infile = []
    for i in idev:
        idevinfile.append(os.path.splitext(i)[0])
idev_infile.append(os.path.splitext(i)[0])
    return idev_infile

idev_infile def ConvertedFiles(cnvfiles): Converted_Files(cnv_files): cnv = [b for b in listdir(cnvfiles) listdir(cnv_files) if isfile(join(cnvfiles, isfile(join(cnv_files, b))] cnvinfile cnv_infile = [] for j in cnv: cnvinfile.append(os.path.splitext(j)[0]) cnv_infile.append(os.path.splitext(j)[0]) return cnv_infile

cnv_infile def SnapshotFiles(snpfiles): Snapshot_Files(snp_files): snp = [c for c in listdir(snpfiles) listdir(snp_files) if isfile(join(snpfiles, isfile(join(snp_files, c))] snpinfile snp_infile = [] for k in snp: snpinfile.append(os.path.splitext(k)[0]) snp_infile.append(os.path.splitext(k)[0]) return snp_infile

snp_infile def log_out(directory): try: if not os.path.exists(directory): os.makedirs(directory) except OSError: print ('Error: Creating directory. ' + directory)

*******Main Program********

*******************

directory) #*************************Main Program************************# #*************************************************************# if name __name__ == 'main': '__main__': import os, sys from os import listdir from os.path import isfile, join from os.path import splitext import time

*******Set time #************************Set PSS/E Path******

Path***********************#
pssedir = r"""C:\Program Files (x86)\PTI\PSSE32\PSSBIN"""

 sys.path.append (pssedir)
os.environ["PATH"] = os.environ["PATH"] + ";" + pssedir

*****Define #*******************Define Working Directory******

Directory******************#

study_folder = r"""D:/Studies/BBB/"""
idv_files = r"""D:/Studies/BBB/contingency/"""
cnv_files = r"""D:/Studies/BBB/converted/"""
snp_files = r"""D:/Studies/BBB/snapshot/"""

******Import #*********************Import psspy modules*****

modules********************#

import psspy
import redirect
_i = psspy.getdefaultint()
_f = psspy.getdefaultreal()
_c = psspy.getdefaultchar()

****Redirect #***************Redirect PSS/E Output to Python****

Python***************#

redirect.psse2py()

*******Initialize PSS/E******


#************************Initialize PSS/E*********************#

psspy.psseinit(150000)

******Run #*********************Run Dynamic Simulation*****

Simulation******************#

for x, y in zip(Converted_Files(cnv_files),Snapshot_Files(snp_files)):
    log_out(x)
    for z in Contingency_Files(idv_files):
        print 'Simulation executed at time ', time.ctime()
        print 'The snapshot file is ', y + '.'
        print 'The case file is ', x + '.'
        print 'The Fault ID is ', z + '.'
        print 'Processing dynamic simulation for Fault ID ', z + '...'
        psspy.progress_output(2, study_folder + x + "/" + z + '.pdev', [0,0])
        psspy.prompt_output(2, study_folder + x + "/" + z + '.odev', [0,0])
        try:
            psspy.rstr(snp_files + y + '.snp')
        except:
            print 'Error while opening the file ', y
            sys.exit(0)
        psspy.powerflowmode()
        try: 
            psspy.case(cnv_files + x + '.cnv')
        except:
            print 'Error while opening the file ', x
            sys.exit(0)
        psspy.ordr(0)
        psspy.fact()
        psspy.tysl(0)
        psspy.dynamicsmode(0)
        psspy.strt(1, study_folder + x + "/" + z + '.out')
        psspy.runrspnsfile(idv_files + z + '.idv')
        psspy.run(1,15,60,3,0)
        psspy.progress_output(1,'', [0,0])
        psspy.prompt_output(1,'', [0,0])
        print 'Simulation finished at time ', time.ctime()
        print 'Dynamic simulation for Fault ID ', z + ' complete! \n\n'

print 'Total Simulation finished at time ', time.ctime()
print '\nEnd of Dynamic Simulation \n'