-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAGGanalysis.py
374 lines (243 loc) · 11.1 KB
/
AGGanalysis.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import sys
import os
import time
import statistics
import matplotlib.pyplot as plt
import functions.aggregationstudy.aggfunctions as AGF
import functions.convcompar.datamanager as DTM
import numpy as np
import functions.convcompar.PFmanager as PFM
import datetime
import pandas as pd
if os.getlogin() != 'Pedro':
sys.path.append(r'C:\Program Files\DIgSILENT\PowerFactory 2021 SP1\Python\3.8')
import powerfactory as pf
start = time.asctime()
print(start)
#Initiate powerfactory. In case there is an error, will give the error code number
try:
app = pf.GetApplicationExt(None, None, r"/ini C:\Users\ge25bod\config.ini")
# ... some calculations ...
except pf.ExitError as error:
print(error)
print('error.code = %d' % error.code)
print('error.code = %d' % error.code)
# mode selection
mode = 'multievent' # RXanaylsis, RMSE, multievent, enveloping
escens = ['hPV', 'hW', 'lPV', 'lW']
#available analysis to be made
if mode == 'RXanalysis':
submode = 'boxplot'
setup = 'GFMdepth'
if submode == 'barplot':
vlevel = 'MV'
controller = 'constphi'
Rdf, Xdf, Cdf = AGF.RXanalysis(app, vlevel, controller, escens)
DTM.DFplot([Rdf,Xdf, Cdf], 'aggRX',
savefigures=True,
figurefolder='grid aggregation\line_parameter/' + vlevel + controller)
elif submode == 'boxplot':
vlevel = 'MV'
controllers = ['constphi', 'constv']
R = [None]*2
X = [None]*2
C = [None]*2
for controller in controllers:
i = controllers.index(controller)
Rdf, Xdf, Cdf = AGF.RXanalysis(app, vlevel, controller, escens)
R[i] = Rdf
X[i] = Xdf
C[i] = Cdf
parameters = {'R': R, 'X': X, 'C': C}
if setup == 'controller':
p = 'C'
Rconcv = list(parameters[p][0].loc[:, 'hPV']) + list(parameters[p][0].loc[:, 'hW']) + list(
parameters[p][0].loc[:, 'lPV']) + list(parameters[p][0].loc[:, 'lW'])
Rconcphi = list(parameters[p][1].loc[:, 'hPV']) + list(parameters[p][1].loc[:, 'hW']) + list(
parameters[p][1].loc[:, 'lPV']) + list(parameters[p][1].loc[:, 'lW'])
plt.figure(0)
plt.boxplot([Rconcv,Rconcphi])
plt.show()
#elif setup == 'GFMdepth':
elif mode == 'RMSE':
submode = 'boxplot'
print('Obtaining all csv for RMSE calculation')
# selecte noramlization mode and var to plot
var2plot = 'I1'
normmode = 'commonscenario' # 'commonscenario' or 'individual'
#select controller folder
basepath = r"C:\Users\Usuario\Documents\Universidad\TUM\Subjects\5th semester\Masterarbeit\Code_and_data\aggresults/"
controller = ['constv', 'constphi']
cont = controller[0]
if submode == 'barplot':
normRMSE = AGF.RMSEanalysis(var2plot, normmode, basepath, cont, escens)
DTM.DFplot([normRMSE], 'aggeval',
savefigures=True,
figurefolder= 'grid aggregation\Vdip_0,5_RMSE/' + mode + '_' + var2plot + '_' + cont + '.pdf')
elif submode == 'boxplot':
normRMSEv = AGF.RMSEanalysis(var2plot, normmode, basepath, controller[0], escens)
normRMSEphi = AGF.RMSEanalysis(var2plot, normmode, basepath, controller[1], escens)
setup = 'converter' #constvconstphi, GFMdepth, scenario
plt.figure(0)
fontsize = 9
params = {'legend.fontsize': fontsize,
'figure.titlesize': fontsize,
'axes.labelsize': fontsize,
'axes.titlesize': fontsize,
'xtick.labelsize': fontsize,
'ytick.labelsize': fontsize,
'font.family': 'Times New Roman',
'legend.frameon': '0',
'legend.columnspacing': '1'}
for attr, val in params.items():
plt.rcParams[attr] = val
if setup == 'constvconstphi':
concv = list(normRMSEv.loc[:, 'hPV']) + list(normRMSEv.loc[:, 'hW']) + list(
normRMSEv.loc[:, 'lPV']) + list(normRMSEv.loc[:, 'lW'])
concphi = list(normRMSEphi.loc[:, 'hPV']) + list(normRMSEphi.loc[:, 'hW']) + list(
normRMSEphi.loc[:, 'lPV']) + list(normRMSEphi.loc[:, 'lW'])
box = plt.boxplot([concv, concphi], patch_artist=True)
plt.violinplot([concv, concphi], )
colors = ['c', 'c']
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)
plt.grid(axis='y')
plt.xticks([1, 2], controller)
plt.xlabel('Local controller')
plt.ylabel('RMSE/%')
plt.show()
elif setup == 'GFMdepth':
alllist = []
onelist = []
mvlist = []
lvlist = []
dtfs = [normRMSEv, normRMSEphi]
for dtf in dtfs:
for index,row in dtf.iterrows():
if index == 'GFLAll':
continue
elif ('All' in index) and (index != 'GFLAll'):
alllist = alllist + row.to_list()
elif 'MV' in index:
mvlist = mvlist + row.to_list()
elif 'LV' in index:
lvlist = lvlist + row.to_list()
else:
onelist = onelist + row.to_list()
plt.boxplot([alllist, lvlist,mvlist, onelist])
plt.violinplot([alllist, lvlist,mvlist, onelist])
plt.xticks([1, 2, 3, 4], ['GFMAll', 'GFMLV', 'GMFMV', 'GFM1'])
plt.xlabel('Depth of GFM integration')
plt.ylabel('RMSE/%')
plt.grid(axis='y')
plt.show()
elif setup == 'scenario':
dtfs = [normRMSEv, normRMSEphi]
hPV = []
hW = []
lPV = []
lW = []
for dtf in dtfs:
hPV = hPV + list(dtf.loc[:,'hPV'])
hW = hW + list(dtf.loc[:, 'hW'])
lPV = lPV + list(dtf.loc[:,'lPV'])
lW = lW + list(dtf.loc[:, 'lW'])
plt.boxplot([hPV,hW,lPV,lW])
violing = plt.violinplot([hPV, hW, lPV, lW])
for pc in violing['bodies']:
pc.set_facecolor('cyan')
pc.set_edgecolor('red')
plt.xticks([1,2,3,4], escens)
plt.xlabel('Scenario')
plt.ylabel('RMSE/%')
plt.grid(axis='y')
plt.show()
elif setup == 'converter':
dtfs = [normRMSEv, normRMSEphi]
dro = []
syn = []
vsm = []
for dtf in dtfs:
for index,row in dtf.iterrows():
if index == 'GFLAll':
continue
elif 'Dro' in index:
dro = dro + row.to_list()
elif 'Syn' in index:
syn = syn + row.to_list()
elif 'VSM' in index:
vsm = vsm + row.to_list()
plt.boxplot([dro,syn,vsm])
plt.xticks([1,2,3], ['Dro', 'Syn', 'VSM'])
plt.show()
elif mode == 'multievent':
eventype = 'vdip' # 'vdip' or 'freqramp'
controller = 'constphi'
pathmode = 'Read'
print('Creating or reading results folder')
if pathmode == 'Create':
basepath = DTM.ReadorCreatePath('Create', folder=datetime.datetime.now().strftime("\\%d.%m.%Y_%H-%M-%S") + '_' + eventype + '_' + controller + '_AG\\')
elif pathmode == 'Read':
basepath = DTM.ReadorCreatePath('Read', readmode='userfile', folder='/14.06.2021_07-38-00_vdip_constphi_AG/', filename='')
else:
sys.exit('Wrong path mode')
if eventype == 'vdip':
values = list(np.arange(-0.9, -0.05, 0.1))
values.extend((-0.05, 0.05, 0.1))
values = [round(val, 2) for val in values]
else:
sys.exit('Wrong event type name')
print('Running ' + str(len(values)) + ' events for ' + eventype)
# get all projects
allprj = app.GetCurrentUser().GetContents('*.IntPrj')
# get relevant projects
prjlist = [s for s in allprj if '_eq_' + controller in s.GetFullName()]
prjlist = prjlist
escens = escens
# for every project of the specified controller
for prj in prjlist:
print('PROJECT: Activating ' + prj.GetAttribute('loc_name') + ' project')
prj.Activate() # activate
# for each of the for scenarios
for scen in escens:
# get active study case
actcase = app.GetProjectFolder('study').GetContents('MV_'+scen+'.IntCase')[0]
print('STUDY CASE: Activating MV_' + scen + ' study case')
actcase.Activate()
#get VS from detailed and equivalent grids for given scenario
detVS = app.GetProjectFolder('netdat').GetContents('*rural*.ElmNet')[0].GetContents('*.ElmVac')[0]
eqVS = app.GetProjectFolder('netdat').GetContents('MV_'+scen+'.ElmNet')[0].GetContents('*.ElmVac')[0]
# for each of the values of the event selected
for val in values:
print('Applying values ' + str(val) + ' to VS signal')
#if this event is a voltage dip
if eventype == 'vdip':
detVS.GetAttribute('c_pmod').GetAttribute('sig_conditioning').SetAttribute('Vldip', val)
eqVS.GetAttribute('c_pmod').GetAttribute('sig_conditioning').SetAttribute('Vldip', val)
# run simulation and save csv in Results folder
print('Running simulation and saving results')
PFM.RunNSave(app, True, tstop=1, path=basepath + '{project}_{scenario}_{eventype}_{value}.csv'.format(project=prj.GetAttribute('loc_name'), scenario=scen, eventype=eventype, value=val))
elif mode == 'enveloping':
submode = None
grid = '1-MVLV-rural-all-0-sw_SynAll_constv'
var2plot = ['u1', 'I1P', 'I1Q']
units = ['pu', 'kA', 'kA']
var = var2plot[1]
label = var + '/' + units[var2plot.index(var)]
if submode == 'Createcsv':
project = app.GetCurrentUser().GetContents(grid)[0]
project.Activate()
app.GetProjectFolder('scen').GetContents('hW')[0].Activate()
basepath = DTM.ReadorCreatePath('Create', folder=datetime.datetime.now().strftime("\\%d.%m.%Y_%H-%M-%S") + '_EV\\')
PFM.RunNSave(app, True, tstop=1, path=basepath + 'enveloping_ubus.csv')
else:
df = DTM.importData(r"C:\Users\Usuario\Documents\Universidad\TUM\Subjects\5th semester\Masterarbeit\Code_and_data\aggresults\07.06.2021_11-19-02_EV\enveloping_ubus1-MVLV-rural-all-0-sw_VSMAll_constvhPV.csv")
enveldf = pd.DataFrame(columns=['max', 'min'])
col2plot = [s for s in list(df.columns) if var in s]
allvaluesdf = df.loc[:, col2plot]
allvaluesdf.insert(0, df.columns[0], df.loc[:, df.columns[0]])
for i in range(df.shape[0]):
enveldf.loc[i, 'max'] = max(df.loc[i, col2plot])
enveldf.loc[i, 'min'] = min(df.loc[i, col2plot])
dflist = [enveldf, allvaluesdf]
DTM.DFplot(dflist,'enveloping', ylabel=label)