First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
0

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

asked Jul 18 '18

lmcqueen gravatar image

updated Oct 11 '4

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

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

def Snapshot_Files(snp_files):
    snp = [c for c in listdir(snp_files) if isfile(join(snp_files, c))]
    snp_infile = []
    for k in snp:
        snp_infile.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

#*******************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/"""

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

pssedir = r"""C:\Program Files (x86)\PTI\PSSE32\PSSBIN""" 
sys.path.append (pssedir)
os.environ["PATH"] = os.environ["PATH"] + ";" + pssedir

#*********************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'

1 answer

Sort by » oldest newest most voted
0

answered Jul 18 '18

jconto gravatar image

The code works fine.

replace "sys.exit(0)" with "break" to exit the inner loop when the case or snp file is bad.

Potential improvements:
- separate data input from code.  Minimize hard-coded data input.  Even the simulation time could become an input.
- snp filenames do not have to match same sequence as cnv filenames.  Better, read a pair (cnv, snp) of filenames per run.
- for over tens of runs, it pays to use parallel processing (one run per CPU)
link

Comments

This is invaluable. Thanks jconto! Can I also use the break if the idev file causes the program to crash during run?

lmcqueen gravatar imagelmcqueen (Jul 19 '18)

Yeah, put the fault idv inside a try/except but use a 'continue' instead of break to bypass only the 'bad' idv.

jconto gravatar imagejconto (Jul 20 '18)

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: Jul 18 '18

Seen: 986 times

Last updated: Oct 11 '24