Ask Your Question
0

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

asked 2018-07-18 00:22:07 -0500

ffl gravatar image

updated 2018-07-18 10:21:50 -0500

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

#************************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'
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2018-07-18 12:07:31 -0500

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)
edit flag offensive delete link more

Comments

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

ffl gravatar imageffl ( 2018-07-19 03:09:04 -0500 )edit

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 ( 2018-07-19 21:26:14 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: 2018-07-18 00:22:07 -0500

Seen: 800 times

Last updated: Jul 18 '18