-
Notifications
You must be signed in to change notification settings - Fork 7
/
execute_pscad.py
126 lines (100 loc) · 4.28 KB
/
execute_pscad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'''
Executes the Powerplant model testbench in PSCAD.
'''
from __future__ import annotations
import os
#Ensure right working directory
executePath = os.path.abspath(__file__)
executeFolder = os.path.dirname(executePath)
os.chdir(executeFolder)
from configparser import ConfigParser
class readConfig:
def __init__(self) -> None:
self.cp = ConfigParser(allow_no_value=True)
self.cp.read('config.ini')
self.parsedConf = self.cp['config']
self.sheetPath = str(self.parsedConf['Casesheet path'])
self.pythonPath = str(self.parsedConf['Python path'])
self.volley = int(self.parsedConf['Volley'])
config = readConfig()
import sys
sys.path.append(config.pythonPath)
print(sys.version)
from datetime import datetime
import shutil
from typing import List
import sim_interface as si
import case_setup as cs
import mhi.pscad
def outToCsv(srcPath : str, dstPath : str):
"""
Converts PSCAD .out file into .csv file
"""
with open(srcPath) as out, \
open(dstPath, 'w') as csv:
csv.writelines(','.join(line.split())+'\n' for line in out)
def moveFiles(srcPath : str, dstPath : str, types : List[str]) -> None:
'''
Moves files of the specified types from srcPath to dstPath.
'''
for file in os.listdir(srcPath):
_, typ = os.path.splitext(file)
if typ in types:
shutil.move(os.path.join(srcPath, file), os.path.join(dstPath, file))
def cleanUpOutFiles(buildPath : str, projectName : str):
'''
Cleans up the build folder by moving .out and .csv files to an 'Output' folder in the current working directory.
'''
# Converting .out files to .csv files
for file in os.listdir(buildPath):
_, fileName = os.path.split(file)
root, typ = os.path.splitext(fileName)
if fileName.startswith(projectName) and typ == '.out':
print('Converting {} to .csv'.format(file))
outToCsv(os.path.join(buildPath, fileName), os.path.join(buildPath, f'{root}.csv'))
# Move desired files to an 'Output' folder in the current working directory
outputFolder = 'output'
if not os.path.exists(outputFolder):
os.mkdir(outputFolder)
resultsFolder = f'MTB_{datetime.now().strftime(r"%d%m%Y%H%M%S")}'
#Move .csv and .inf files away from build folder into output folder
csvFolder = os.path.join(outputFolder, resultsFolder)
os.mkdir(csvFolder)
moveFiles(buildPath, csvFolder, ['.csv', '.inf'])
#Move .out file away from build folder
outFolder = os.path.join(buildPath, resultsFolder)
os.mkdir(outFolder)
moveFiles(buildPath, outFolder, ['.out'])
def cleanBuildfolder(buildPath : str):
'''
"Cleans" the build folder by trying to delete it.
'''
try:
shutil.rmtree(buildPath)
except FileNotFoundError:
pass
def main():
print('execute_pscad.py started at:', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
pscad = mhi.pscad.application()
plantSettings, channels, _, maxRank = cs.setup(config.sheetPath, pscad = True, pfEncapsulation = None)
si.renderFortran('interface.f', channels)
#Print key plant settings
for setting in plantSettings.__dict__:
print(f'{setting} : {plantSettings.__dict__[setting]}')
project = pscad.project(plantSettings.PSCAD_Namespace)
buildFolder : str = project.temp_folder #type: ignore
cleanBuildfolder(buildFolder) #type: ignore
project.parameters(time_duration = 999, time_step = plantSettings.PSCAD_Timestep, sample_step = '1000') #type: ignore
project.parameters(PlotType = '1', output_filename = f'{plantSettings.Projectname}.out') #type: ignore
project.parameters(SnapType='0', SnapTime='2', snapshot_filename='pannatest5us.snp') #type: ignore
pscad.remove_all_simulation_sets()
pmr = pscad.create_simulation_set('MTB')
pmr.add_tasks(plantSettings.PSCAD_Namespace)
project_pmr = pmr.task(plantSettings.PSCAD_Namespace)
project_pmr.parameters(ammunition = maxRank, volley = config.volley, affinity_type = '2') #type: ignore
pscad.run_simulation_sets('MTB') #type: ignore ??? By sideeffect changes current working directory ???
os.chdir(executeFolder)
cleanUpOutFiles(buildFolder, plantSettings.Projectname)
print('execute.py finished at:', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if __name__ == '__main__':
main()