First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!
1 | initial version |
If you are running just one simulation the lines of code you mentioned would probably be sufficient. Although in my experience if you are running multiple studies (e.g. using a for loop) then the saved case may not get closed properly prior to the next one being opened etc.
In case you are running many studies in your script then you may also have to re-write the same lines of code again. Here is an alternative way using Python's with
statement and a context manager function from the built-in contextlib
library to start and finish each run appropriately.
from contextlib import contextmanager
@contextmanager
def open_savedcase(filepath):
try:
psspy.case(filepath)
yield
finally:
psspy.delete_all_plot_channels()
psspy.dynamicsmode(0)
ierr_close_line = psspy.close_powerflow()
ierr_del_tmpfiles = psspy.deltmpfiles()
ierr_halt = psspy.pssehalt_2()
Then to run a study you can type
with open_savedcase(filepath):
# Study code here
Once you switch back to the initial indentation level the code to finalise the study will get run automatically. You can also run multiple studies with the following code
case_filepaths = ['case1.sav',' case2.sav', 'case3.sav']
for case in case_filepaths:
with open_savedcase(case):
# Study code here
Another benefit to using a context manager is that in case an error or exception occurs in your script part way through running a simulation then the finish code will still run because of Python's finally
statement.