-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_nx.py
1255 lines (964 loc) · 48.3 KB
/
setup_nx.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# In[22]:
## THIS SCRIPT STORES THE FUNCTIONS AND CLASSES NECESSARY TO CREATE A 'FEEDER' OBJECT FROM AN EPHASORSIM MODEL
#SPBC-HIL files moved 7/15/19
# In[23]:
import pandas as pd
import numpy as np
import string
import networkx as nx
from copy import deepcopy
import pdb
import matplotlib.pyplot as plt
import time
# In[24]:
# Adds traceback to warning messages, which is sometimes useful
import traceback
import warnings
import sys
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
log = file if hasattr(file,'write') else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))
warnings.showwarning = warn_with_traceback
# In[25]:
## CLASS DEFINITIONS/HELPER FUNCTIONS
# In[26]:
class feeder:
# The main class. Creating this from an ePHASORsim model and set of loads stores all the information about your network
# Buses, loads, lines, actuators, etc. are all stored as dictionaries within this object
#[HIL] - added ICDI vars and phasor reference vars
def __init__(self,modelpath,loadfolder,loadpath,actpath,timesteps,timestepcur,subkVbase_phg,subkVAbase,refphasor,Psat_nodes,Qsat_nodes,PVforecast, depth_dict, leaf_list):
#def __init__(self,modelpath,loadfolder,loadpath,timesteps,subkVbase_phg,subkVAbase,depth_dict,leaf_list):
# import data
self.modeldata = pd.ExcelFile(modelpath)
# self.loadfolder = loadfolder
# self.loadpath = loadpath
self.actpath = actpath
self.timesteps = timesteps
self.timestepcur = timestepcur
self.subkVbase_phg = subkVbase_phg
self.subkVAbase = subkVAbase
self.refphasor = refphasor
self.Psat_nodes = Psat_nodes
self.Qsat_nodes = Qsat_nodes
self.PVforecast = PVforecast
# groom data (note that the functions below are not separable...
# ...e.g. buses are not completely defined until all these functions have been run)
self.busdict = busbuilder(self.modeldata, subkVbase_phg, subkVAbase, timesteps)
#self.Vsrcdict = Vsrcbuilder(self.modeldata, self.busdict)
#****** NOTE: Uncomment this after formatting has been changed
#self.shuntdict = shuntbuilder(self.modeldata, self.busdict,timesteps)
self.loaddict = loadbuilderPQ(self.modeldata, self.busdict, loadpath, timesteps, timestepcur)
# self.actdict = actbuilder(self.modeldata, self.busdict, loadpath, timesteps, timestepcur)
self.linedict = linebuilder(self.modeldata, self.busdict, timesteps)
self.transdict = transbuilder(self.modeldata, self.busdict, self.subkVAbase, timesteps)
self.switchdict = switchbuilder(self.modeldata, self.busdict, timesteps)
self.network = network_mapper(self.modeldata,self.busdict,self.linedict,self.transdict,self.switchdict, depth_dict, leaf_list)
set_per_unit(self.linedict)
class bus:
def __init__(self,desig,subkVbase_phg,subkVAbase,timesteps):
self.name = 'bus_' + str(desig)
self.phases = list()
self.phasevec = np.zeros((3,1))
self.type = ''
self.kVbase_phg = subkVbase_phg
self.kVAbase = subkVAbase
self.Zbase = subkVbase_phg*subkVbase_phg*1000/subkVAbase
self.loads = list()
self.cap = list()
self.actuators = list()
self.edges_out = list()
self.edges_in = list()
self.Vsrcs = list()
#self.Vmagsq_linopt = cp.Variable((3,timesteps), name=self.name + '_Vmagsq')
#self.Vang_linopt = cp.Variable((3,timesteps), self.name + '_Vang')
self.Vmag_NL = np.ones((3,timesteps))
self.Vang_NL = np.array([[0.],[240.],[120.]])
for idx in range(timesteps-1): #[HIL] - delete -1? or set timesteps=2 and just use first value
self.Vang_NL = np.concatenate((self.Vang_NL,np.array([[0.],[240.],[120.]])), axis=1)
class connector:
# Includes lines, transformers, and switches as subclasses
def __init__(self,timesteps):
self.phasevec = np.zeros((3,1))
self.Imag_NL = np.zeros((3,timesteps))
self.Iang_NL = np.array([[0.],[0.],[0.]])
for idx in range(timesteps-1):
self.Iang_NL = np.concatenate((self.Iang_NL,np.array([[0.],[0.],[0.]])), axis=1)
class Vsrc:
# These are defined and populated, but as of 7/31/2018 they are not used.
def __init__(self,desig):
self.name = 'Vsrc_' + str(desig)
self.id = ''
self.phases = list()
self.kV = complex(0)
self.angle_phA = complex(0)
self.phasevec = np.zeros((3,1))
self.Vvec_phg = np.zeros((3,1))
self.Vangvec = np.zeros((3,1))
class shunt:
# As of 7/31/2018, this class only supports cap banks.
def __init__(self,desig,timesteps):
self.name = 'cap_' + str(desig)
self.id = ''
self.node = None
self.phases = list()
self.phasevec = np.zeros((3,1))
self.kV_phg = complex(0)
self.phasevec = np.zeros((3,1))
self.Pvec = np.zeros((3,1))
self.Qvec = np.zeros((3,1))
self.statusvec = np.zeros((3,1))
self.P_NL = np.zeros((3,timesteps))
self.Q_NL = np.zeros((3,timesteps))
class load:
def __init__(self, desig, timesteps):
self.name = 'load_' + str(desig)
self.id = ''
self.type = ''
self.node = None
self.phases = list()
self.phasevec = np.zeros((3,1))
self.kV_phph = complex(0)
self.bandwidth = complex(0)
self.conn = ''
self.constZ = complex(0)
self.constI = complex(0)
self.constP = complex(0)
self.status = list()
self.Rsched = np.zeros((3,timesteps))
self.Xsched = np.zeros((3,timesteps))
self.Psched = np.zeros((3,timesteps))
self.Qsched = np.zeros((3,timesteps))
self.P_NL = np.zeros((3,timesteps))
self.Q_NL = np.zeros((3,timesteps))
class actuator:
# As of 7/31/2018, only set up to handle one actuator per node
def __init__(self, desig, timesteps):
self.name = 'act_' + str(desig)
self.node = None
self.phases = list()
self.phasevec = np.zeros((3,1))
self.Psched = np.zeros((3,timesteps))
self.Ssched = np.zeros((3,timesteps))
#self.Pgen = cp.Variable((3,timesteps), name=self.name + '_actP')
#self.Qgen = cp.Variable((3,timesteps), name=self.name + '_actQ')
class line(connector):
def __init__(self, desig, timesteps):
connector.__init__(self,timesteps)
self.name = 'line_' + str(desig)
self.id = ''
self.from_node = None
self.from_phases = list()
self.to_node = None
self.to_phases = list()
self.length = complex(0)
self.kVbase_phg = complex(0)
self.kVAbase = complex(0)
self.Zbase = complex(0)
self.R = np.zeros((3, 3))
self.X = np.zeros((3, 3))
self.shuntB = np.zeros((3, 3), dtype=np.complex_)
self.Z = np.zeros((3, 3), dtype=np.complex_)
self.Y = np.zeros((3, 3), dtype=np.complex_)
self.Zpu = np.zeros((3, 3), dtype=np.complex_)
self.Ypu = np.zeros((3, 3), dtype=np.complex_)
#self.P_linopt = cp.Variable((3,timesteps), self.name + '_P')
#self.Q_linopt = cp.Variable((3,timesteps), self.name + '_Q')
class switch(connector):
def __init__(self,desig,timesteps):
connector.__init__(self,timesteps)
self.name = 'switch_' + str(desig)
self.from_phases = list()
self.to_phases = list()
self.from_node = None
self.to_node = None
self.status = list()
self.Z = np.zeros((3, 3), dtype=np.complex_)
self.Y = np.zeros((3, 3), dtype=np.complex_)
self.Zpu = np.zeros((3, 3), dtype=np.complex_)
self.Ypu = np.zeros((3, 3), dtype=np.complex_)
#self.P_linopt = cp.Variable((3,timesteps), self.name + '_P')
#self.Q_linopt = cp.Variable((3,timesteps), self.name + '_Q')
class transformer(connector):
def __init__(self,desig,timesteps):
connector.__init__(self,timesteps)
self.name = 'transformer_' + str(desig)
self.id = ''
self.w0_node = None
self.w0_phases = list()
self.w0_kVbase_phg = complex(0)
self.w0_kVAbase = complex(0)
self.w0_rpu = complex(0)
self.w0_conn = ''
self.w1_node = None
self.w1_phases = list()
self.w1_kVbase_phg = complex(0)
self.w1_kVAbase = complex(0)
self.w1_rpu = complex(0)
self.w1_conn = ''
self.xpu = complex(0)
self.tappos = list()
self.taprange_high = complex(0)
self.taprange_low = complex(0)
self.taprangepct_high = complex(0)
self.taprangepct_low = complex(0)
self.Zpu = np.zeros((3, 3), dtype=np.complex_)
self.Ypu = np.zeros((3, 3), dtype=np.complex_)
#self.P_linopt = cp.Variable((3,timesteps), self.name + '_P')
#self.Q_linopt = cp.Variable((3,timesteps), self.name + '_Q')
# In[27]:
def phase2vec(letter):
# Converts 'a','b','c' to elementary vectors e1, e2, or e3
possible_letters = ['a', 'b', 'c', 'A', 'B', 'C']
assert (letter in possible_letters), "Bad call to phase2vec"
vecout = np.zeros((3,1))
if letter == 'a' or letter == 'A':
vecout = vecout + np.array([[1],[0],[0]])
elif letter == 'b' or letter == 'B':
vecout = vecout + np.array([[0],[1],[0]])
elif letter == 'c' or letter == 'C':
vecout = vecout + np.array([[0],[0],[1]])
return vecout
def ZtoY(Zmat):
# Converts an impedance matrix to an admittance matrix
# Treats '0' impedances on the diagonal as 'line not present' (i.e. temporarily assigns them Z=infinite before inverting)
tempZ = deepcopy(Zmat)
for idx in range(0,3):
if Zmat[idx,idx] == 0:
tempZ[idx,idx] = np.inf
return np.linalg.inv(tempZ)
def fixconnections(tree,headnode):
#Iterative approach to prevent maximum recursion depth exceeded error from occuring
current_level = [headnode]
while len(current_level) != 0:
next_level = []
for current_level_node in current_level:
for successor in tree.successors(current_level_node):
if (successor, current_level_node) in tree.edges():
tree.remove_edge(successor, current_level_node)
next_level.append(successor)
current_level = next_level
'''
# Removes all upstream-facing connections from a networkx digraph
# This will break when we start moving to mesh networks
succlist = list()
for inode in tree.successors(headnode):
succlist.append(inode)
for inode in succlist:
if (inode, headnode) in tree.edges():
tree.remove_edge(inode,headnode)
#old line causing recursive error
#for inode2 in tree.successors(inode):
#this next line used to be indented for for loop
fixconnections(tree,inode)
'''
def propogatebasesup(tree,currnode,kVbase_phg):
# Sets the voltage bases of every node on a path upstream from a given node. Stops if it hits a transformer.
# 'tree' should be a networkx graph object
predlist = list()
currnode.kVbase_phg = kVbase_phg
currnode.Zbase = kVbase_phg*kVbase_phg*1000/currnode.kVAbase
for inode in tree.predecessors(currnode):
predlist.append(inode)
for inode in predlist:
if isinstance(tree[inode][currnode]['connector'],transformer):
return
else:
propogatebasesup(tree,inode,kVbase_phg)
def propogatebasesdown(tree,currnode,kVbase_phg):
# Sets the voltage bases of every node on a path downtream from a given node. Stops if it hits a transformer.
# 'tree' should be a networkx graph object
succlist = list()
currnode.kVbase_phg = kVbase_phg
currnode.Zbase = kVbase_phg*kVbase_phg*1000/currnode.kVAbase
for inode in tree.successors(currnode):
succlist.append(inode)
for inode in succlist:
propogatebasesdown(tree,inode,kVbase_phg)
def propogatetrans(tree,currnode):
# Uses propogatebasesup and down to set appropriate voltage bases throughout a networkx graph based on transformer ratings.
edgelist = list()
for iedge in tree.out_edges(currnode): #2to3 out_edges_iter to out_edges
edgelist.append(iedge)
for iedge in edgelist:
conn = tree[iedge[0]][iedge[1]]['connector']
if isinstance(conn,transformer):
if currnode == conn.w0_node:
propogatebasesup(tree,currnode,conn.w0_kVbase_phg)
propogatebasesdown(tree,iedge[1],conn.w1_kVbase_phg)
elif currnode == conn.w1_node:
propogatebasesup(tree,currnode,conn.w1_kVbase_phg)
propogatebasesdown(tree,iedge[1],conn.w0_kVbase_phg)
succlist = list()
for inode in tree.successors(currnode):
succlist.append(inode)
for inode in succlist:
propogatetrans(tree,inode)
# In[28]:
## DATA IMPORT FUNCTIONS
# In[29]:
# CREATING NODES AND LOAD OBJECTS
# In[30]:
# Create bus dictionary from dataframe
def busbuilder(modeldata,subkVbase_phg,subkVAbase,timesteps):
bussheet = modeldata.parse('Bus') # parse Bus tab of impedance excel
busdict=dict()
for idx, row in bussheet.iterrows():
# row['Bus'] is for example 'N_300216892_B'
indkey = row['Bus'][:len(row['Bus'])-2] # all except the last 2
indphase = row['Bus'][len(row['Bus'])-1] # pulling out A/B/C substring from each cell in "Bus" column
if indkey in busdict.keys(): # if bus already in dict, add on the phase
busdict[indkey].phases.append(indphase)
else: # if bus not already in dict, add it
busdict[indkey] = bus(indkey,subkVbase_phg,subkVAbase, timesteps) # create a bus object
busdict[indkey].type = row['Type'] # add whether it's PQ,slack, or PV bus
busdict[indkey].phases.append(indphase)
for key, obj in busdict.items(): # for each multiphase bus
for idx2 in range(len(obj.phases)): # for each phase
if obj.phases[idx2] == 'a':
obj.phasevec = obj.phasevec + np.array([[1],[0],[0]])
elif obj.phases[idx2] == 'b':
obj.phasevec = obj.phasevec + np.array([[0],[1],[0]])
elif obj.phases[idx2] == 'c':
obj.phasevec = obj.phasevec + np.array([[0],[0],[1]])
return busdict
# In[31]:
# Create Vsource dictionary from dataframe
# This is a superfluous object since the present (July 2018) code assumes that there is only one Vsrc (the substation)
# So, the current code doesn't use it for anything, but if later models have multiple Vsrcs, we might find it useful
def Vsrcbuilder(modeldata, busdict):
Vsrcsheet = modeldata.parse('Vsource 3-phase')
Vsrcdict=dict()
# Initiation
for idx, row in Vsrcsheet.iterrows():
if row['bus A']:
indkey = row['bus A'][:len(row['bus A'])-2]
elif row['bus B']:
indkey = row['bus B'][:len(row['bus B'])-2]
elif row[' bus C']:
indkey = row['bus C'][:len(row[' bus C'])-2] # Note that there is a leading space in the Excel sheet
Vsrcdict[indkey] = Vsrc(indkey)
# Check phases
if row['bus A'] and isinstance(row['bus A'],str):
Vsrcdict[indkey].phases.append('a')
if row['bus B'] and isinstance(row['bus B'],str):
Vsrcdict[indkey].phases.append('b')
if row[' bus C'] and isinstance(row[' bus C'],str): # Note that there is a leading space in the Excel sheet
Vsrcdict[indkey].phases.append('c')
# Set angles. This assumes that "Angle A" is the only one populated, (the only form I've seen so far)
angle_phA = row[' Angle_a (Degree)']
Vsrcdict[indkey].Vangvec[0,0] = angle_phA
Vsrcdict[indkey].Vangvec[1,0] = angle_phA + 240.
Vsrcdict[indkey].Vangvec[2,0] = angle_phA + 120.
# Misc field, likely not useful
Vsrcdict[indkey].id = row['ID']
# Set voltages
Vsrcdict[indkey].kV_phph = row['kV (ph-ph RMS)']
Vsrcdict[indkey].Vvec_phg[0,0] = Vsrcdict[indkey].kV_phph/np.sqrt(3)
Vsrcdict[indkey].Vvec_phg[1,0] = Vsrcdict[indkey].kV_phph/np.sqrt(3)
Vsrcdict[indkey].Vvec_phg[2,0] = Vsrcdict[indkey].kV_phph/np.sqrt(3)
# Append object to buslist
busdict[indkey].Vsrcs.append(Vsrcdict[indkey])
# Populate phase vectors
for key, obj in Vsrcdict.items():
for idx2 in range(len(obj.phases)):
obj.phasevec = obj.phasevec + phase2vec(obj.phases[idx2])
return Vsrcdict
# In[32]:
# Create shunt elements dictionary from dataframe
# Currently (July 2018) this object handles constant power capacitors only
def shuntbuilder(modeldata, busdict,timesteps):
shuntsheet = modeldata.parse('Multiphase Shunt')
shuntdict = dict()
for idx, row in shuntsheet.iterrows():
if row['Bus1'] and isinstance(row['Bus1'],str):
indkey = row['Bus1'][:len(row['Bus1'])-2]
elif row['Bus2'] and isinstance(row['Bus2'],str):
indkey = row['Bus2'][:len(row['Bus2'])-2]
elif row['Bus3'] and isinstance(row['Bus3'],str):
indkey = row['Bus3'][:len(row['Bus3'])-2]
shuntdict[indkey] = shunt(indkey,timesteps)
if row['Bus1'] and isinstance(row['Bus1'],str):
indphase = row['Bus1'][len(row['Bus1'])-1]
shuntdict[indkey].phases.append(indphase)
if row['Bus2'] and isinstance(row['Bus2'],str):
indphase = row['Bus2'][len(row['Bus2'])-1]
shuntdict[indkey].phases.append(indphase)
if row['Bus3'] and isinstance(row['Bus3'],str):
indphase = row['Bus3'][len(row['Bus3'])-1]
shuntdict[indkey].phases.append(indphase)
shuntdict[indkey].id = row['ID']
shuntdict[indkey].node = busdict[indkey]
shuntdict[indkey].kV_phg = row['kV (ph-gr RMS)']
# 2to3 long to int in (isinstance(row['P1(kW)'],long)
if (row['P1(kW)']) and (not np.isnan(row['P1(kW)'])) and (isinstance(row['P1(kW)'],int) or isinstance(row['P1(kW)'],float)):
# Note that spacings in P,Q headings are very inconsistent
shuntdict[indkey].Pvec = shuntdict[indkey].Pvec + row['P1(kW)'] * phase2vec(shuntdict[indkey].phases[0])
if row['P2 (kW)'] and (not np.isnan(row['P2 (kW)'])) and (isinstance(row['P2 (kW)'],int) or isinstance(row['P2 (kW)'],float)):
shuntdict[indkey].Pvec = shuntdict[indkey].Pvec + row['P2 (kW)'] * phase2vec(shuntdict[indkey].phases[1])
if row['P3(kW)'] and (not np.isnan(row['P3(kW)'])) and (isinstance(row['P3(kW)'],int) or isinstance(row['P3(kW)'],float)):
shuntdict[indkey].Pvec = shuntdict[indkey].Pvec + row['P3(kW)'] * phase2vec(shuntdict[indkey].phases[2])
if row['Q1(kVAr)'] and (not np.isnan(row['Q1(kVAr)'])) and (isinstance(row['Q1(kVAr)'],int) or isinstance(row['Q1(kVAr)'],float)):
shuntdict[indkey].Qvec = shuntdict[indkey].Qvec + row['Q1(kVAr)'] * phase2vec(shuntdict[indkey].phases[0])
if row['Q2 (kVAr)'] and (not np.isnan(row['Q2 (kVAr)'])) and (isinstance(row['Q2 (kVAr)'],int) or isinstance(row['Q2 (kVAr)'],float)):
shuntdict[indkey].Qvec = shuntdict[indkey].Qvec + row['Q2 (kVAr)'] * phase2vec(shuntdict[indkey].phases[1])
if row['Q3 (kVAr)'] and (not np.isnan(row['Q3 (kVAr)'])) and (isinstance(row['Q3 (kVAr)'],int) or isinstance(row['Q3 (kVAr)'],float)):
shuntdict[indkey].Qvec = shuntdict[indkey].Qvec + row['Q3 (kVAr)'] * phase2vec(shuntdict[indkey].phases[2])
if row['Status1'] and row['Status1'] == 1:
shuntdict[indkey].statusvec = shuntdict[indkey].statusvec + phase2vec(shuntdict[indkey].phases[0])
if row['Status2'] and row['Status2'] == 1:
assert(len(shuntdict[indkey].phases) >= 2)
shuntdict[indkey].statusvec = shuntdict[indkey].statusvec + phase2vec(shuntdict[indkey].phases[1])
if row['Status3'] and row['Status3'] == 1:
assert(len(shuntdict[indkey].phases) == 3)
shuntdict[indkey].statusvec = shuntdict[indkey].statusvec + phase2vec(shuntdict[indkey].phases[2])
busdict[indkey].cap.append(shuntdict[indkey])
for key, obj in shuntdict.items():
for idx2 in range(len(obj.phases)):
obj.phasevec = obj.phasevec + phase2vec(obj.phases[idx2])
return shuntdict
# In[33]:
# Create load dictionary from ePHASORsim model and Gridbright load files
def loadbuilderPQ(modeldata, busdict, loadpath, timesteps, timestepcur):
# This creates the load objects from the ePHASORsim model, but ignores the specified values (values are set with a Gridbright load file)
loadsheet = modeldata.parse('Multiphase Load')
loaddict = dict()
for idx, row in loadsheet.iterrows():
if row['Bus1']:
indkey = row['Bus1'][:len(row['Bus1'])-2]
loadname = row['Bus1']
elif row['Bus2']:
indkey = row['Bus2'][:len(row['Bus2'])-2]
loadname = row['Bus2']
elif row['Bus3']:
indkey = row['Bus3'][:len(row['Bus3'])-2]
loadname = row['Bus3']
loaddict[indkey] = load(indkey, timesteps)
if row['Bus1'] and isinstance(row['Bus1'],str):
indphase = row['Bus1'][len(row['Bus1'])-1]
loaddict[indkey].phases.append(indphase)
if row['Bus2'] and isinstance(row['Bus2'],str):
indphase = row['Bus2'][len(row['Bus2'])-1]
loaddict[indkey].phases.append(indphase)
if row['Bus3'] and isinstance(row['Bus3'],str):
indphase = row['Bus3'][len(row['Bus3'])-1]
loaddict[indkey].phases.append(indphase)
loaddict[indkey].id = row['ID']
loaddict[indkey].node = busdict[indkey]
loaddict[indkey].type = row['Type']
loaddict[indkey].kV_phph = row['V (kV)']
loaddict[indkey].bandwidth = row['Bandwidth (pu)']
loaddict[indkey].conn = row['Conn. type']
loaddict[indkey].constZ = row['K_z']
loaddict[indkey].constI = row['K_i']
loaddict[indkey].constP = row['K_p']
loaddict[indkey].status = row['Status']
busdict[indkey].loads.append(loaddict[indkey])
#loadfile = pd.read_csv(loadpath)
#loaddf = loadfile.parse('Time_Series_data')
#[HIL] - parse out current timestep
# jaimie removed these lines 10/19/21:
# loaddf = pd.read_csv(loadpath)
# loaddf = loaddf[timestepcur:timestepcur+timesteps]
# loaddf.index -= timestepcur
# Populate the load dictionary's P and Q schedules with a Gridbright load file
#JPEDIT START
# Jaimie removed this 10/19/21
# multiph = ['1','2','3'] ##generalize to 1,2,3 instead of first second third
# for key, iload in loaddict.items():
# Pkey = []
# Qkey = []
# phkey = []
# for header in loaddf.columns:
# if key in header:
# #for mph in multiph:
# if '/P' in header:
# Pkey.append(header)
# if '/Q' in header:
# Qkey.append(header)
# for ph in iload.phases:
# phkey.append(ph)
# for idx in range(len(phkey)):
# for ts in range(0,timesteps): #write with dict??
# if idx+1 > len(Pkey):
# kW = 0
# kVar = 0
# else:
# kW = loaddf[Pkey[idx]][ts]
# kVAR = loaddf[Qkey[idx]][ts]
# S = kW + 1j*kVAR
# if not (S==0):
# Z = np.conj(np.square(iload.node.kVbase_phg)*1000/S)
# else:
# Z = 0
# if phkey[idx] == 'a':
# iload.Psched[0,ts] = kW
# iload.Qsched[0,ts] = kVAR
# iload.Rsched[0,ts] = np.real(Z)
# iload.Xsched[0,ts] = np.imag(Z)
# if phkey[idx] == 'b':
# iload.Psched[1,ts] = kW
# iload.Qsched[1,ts] = kVAR
# iload.Rsched[1,ts] = np.real(Z)
# iload.Xsched[1,ts] = np.imag(Z)
# if phkey[idx] == 'c':
# iload.Psched[2,ts] = kW
# iload.Qsched[2,ts] = kVAR
# iload.Rsched[2,ts] = np.real(Z)
# iload.Xsched[2,ts] = np.imag(Z)
#JPEDIT END
#EDITED SECTION START
'''
for key, iload in loaddict.items():
for ph in iload.phases:
for ts in range(0,timesteps):
#loadname_kW = 'LD_KW_' + key + '_' + ph
#loadname_kVAR = 'LD_KV_' + key + '_' + ph
#Jaimie
loadname_kW = 'LD_' + key + '/P_' + ph
loadname_kVAR = 'LD_' + key + '/Q_' + ph
if loadname_kW in list(loaddf.columns.values):
kW = loaddf[loadname_kW][ts]
else:
kW = 0
if loadname_kVAR in list(loaddf.columns.values):
kVAR = loaddf[loadname_kVAR][ts]
else:
kVAR = 0
S = kW + 1j*kVAR
if not (S==0):
Z = np.conj(np.square(iload.node.kVbase_phg)*1000/S)
else:
Z = 0
if ph == 'a':
iload.Psched[0,ts] = kW
iload.Qsched[0,ts] = kVAR
iload.Rsched[0,ts] = np.real(Z)
iload.Xsched[0,ts] = np.imag(Z)
if ph == 'b':
iload.Psched[1,ts] = kW
iload.Qsched[1,ts] = kVAR
iload.Rsched[1,ts] = np.real(Z)
iload.Xsched[1,ts] = np.imag(Z)
if ph == 'c':
iload.Psched[2,ts] = kW
iload.Qsched[2,ts] = kVAR
iload.Rsched[2,ts] = np.real(Z)
iload.Xsched[2,ts] = np.imag(Z)
'''
#EDITED SECTION END
for key, iload in loaddict.items():
for idx in range(len(iload.phases)):
if iload.phases[idx] == 'a':
iload.phasevec = iload.phasevec + np.array([[1],[0],[0]])
elif iload.phases[idx] == 'b':
iload.phasevec = iload.phasevec + np.array([[0],[1],[0]])
elif iload.phases[idx] == 'c':
iload.phasevec = iload.phasevec + np.array([[0],[0],[1]])
return loaddict
# In[34]:
# Create actuator dictionary from Gridbright load file
# This interprets all 'PV_KW_' entries in the load file as controllable PV.
# If the PV is uncontrollable, it should be included as negative load in the load P and Q schedules
def actbuilder(modeldata, busdict, actpath, timesteps, timestepcur):
actdict = dict()
# Jaimie removing actuators on 10/19/21
# #actfile = pd.read_csv(actpath)
# actdf = pd.read_csv(actpath)
# #[HIL] - parse current timestep
# actdf = actdf[timestepcur:timestepcur+timesteps]
# actdf.index -= timestepcur
# for key,ibus in busdict.items():
# for ph in ibus.phases:
# if 'act_kVA_' + key + '_' + ph in list(actdf.columns.values):
# if not (key in actdict):
# actdict[key] = actuator(key, timesteps)
# actdict[key].node = busdict[key]
# ibus.actuators.append(actdict[key])
# actdict[key].phases.append(ph)
# for key, iact in actdict.items():
# for ph in iact.phases:
# for ts in range(0,timesteps):
# actname_kVA = 'act_kVA_' + key + '_' + ph
# act = actdf[actname_kVA][ts]
# if ph == 'a':
# iact.Psched[0,ts] = act
# iact.Ssched[0,ts] = act
# if ph == 'b':
# iact.Psched[1,ts] = act
# iact.Ssched[1,ts] = act
# if ph == 'c':
# iact.Psched[2,ts] = act
# iact.Ssched[2,ts] = act
# for key, iact in actdict.items():
# for idx in range(len(iact.phases)):
# if iact.phases[idx] == 'a':
# iact.phasevec = iact.phasevec + np.array([[1],[0],[0]])
# elif iact.phases[idx] == 'b':
# iact.phasevec = iact.phasevec + np.array([[0],[1],[0]])
# elif iact.phases[idx] == 'c':
# iact.phasevec = iact.phasevec + np.array([[0],[0],[1]])
return actdict
# In[35]:
# LINES AND CONNECTOR OBJECTS
# In[36]:
# Create line dictionary
# Note that this isn't set up to handle cross-phase connections: i.e. please don't connect phase A at node 1 to phase B at node 2
def linebuilder(modeldata, busdict, timesteps):
linesheet = modeldata.parse('Multiphase Line')
# Create line dictionary from dataframe
linedict = dict()
for idx, row in linesheet.iterrows():
if row['From1']:
indkeyfrom = row['From1'][:len(row['From1'])-2]
elif row['From2']:
indkeyfrom = row['From2'][:len(row['From2'])-2]
elif row['From3']:
indkeyfrom = row['From3'][:len(row['From3'])-2]
if row['To1']:
indkeyto = row['To1'][:len(row['To1'])-2]
elif row['To2']:
indkeyto = row['To2'][:len(row['To2'])-2]
elif row['To3']:
indkeyto = row['To3'][:len(row['To3'])-2]
indkey = indkeyfrom + 'to' + indkeyto
linedict[indkey] = line(indkey, timesteps)
busdict[indkeyfrom].edges_out.append(linedict[indkey])
busdict[indkeyto].edges_in.append(linedict[indkey])
linedict[indkey].id = row['ID']
length_unit = ' (length_unit)'
ohm_per_lu = ' (ohm/length_unit)'
us_per_lu = ' (uS/length_unit)'
if 'Length (length_unit)' not in row:
length_unit = ' (Mile)'
ohm_per_lu = ' (ohm/Mile)'
us_per_lu = ' (uS/Mile)'
linedict[indkey].length = row['Length' + length_unit]
linedict[indkey].from_node = busdict[indkeyfrom]
linedict[indkey].to_node = busdict[indkeyto]
if row['From1'] and isinstance(row['From1'],str):
linedict[indkey].from_phases.append(row['From1'][len(row['From1'])-1].lower())
if row['From2'] and isinstance(row['From2'],str):
linedict[indkey].from_phases.append(row['From2'][len(row['From2'])-1].lower())
if row['From3'] and isinstance(row['From3'],str):
linedict[indkey].from_phases.append(row['From3'][len(row['From3'])-1].lower())
if row['To1'] and isinstance(row['To1'],str):
linedict[indkey].to_phases.append(row['To1'][len(row['To1'])-1].lower())
if row['To2'] and isinstance(row['To2'],str):
linedict[indkey].to_phases.append(row['To2'][len(row['To2'])-1].lower())
if row['To3'] and isinstance(row['To3'],str):
linedict[indkey].to_phases.append(row['To3'][len(row['To3'])-1].lower())
for idx in range(0,len(linedict[indkey].to_phases)):
linedict[indkey].phasevec = linedict[indkey].phasevec + phase2vec(linedict[indkey].to_phases[idx])
# Build R, X, shunt Y matrices (shunt Y are not included in LUPFM)
rdict = dict()
rdict['11'] = row['r11' + ohm_per_lu]
rdict['21'] = row['r21' + ohm_per_lu]
rdict['22'] = row['r22' + ohm_per_lu]
rdict['31'] = row['r31' + ohm_per_lu]
rdict['32'] = row['r32' + ohm_per_lu]
rdict['33'] = row['r33' + ohm_per_lu]
rdict['12'] = rdict['21']
rdict['13'] = rdict['31']
rdict['23'] = rdict['32']
xdict = dict()
xdict['11'] = row['x11' + ohm_per_lu]
xdict['21'] = row['x21' + ohm_per_lu]
xdict['22'] = row['x22' + ohm_per_lu]
xdict['31'] = row['x31' + ohm_per_lu]
xdict['32'] = row['x32' + ohm_per_lu]
xdict['33'] = row['x33' + ohm_per_lu]
xdict['12'] = xdict['21']
xdict['13'] = xdict['31']
xdict['23'] = xdict['32']
bdict = dict()
bdict['11'] = row['b11' + us_per_lu]
bdict['21'] = row['b21' + us_per_lu]
bdict['22'] = row['b22' + us_per_lu]
bdict['31'] = row['b31' + us_per_lu]
bdict['32'] = row['b32' + us_per_lu]
bdict['33'] = row['b33' + us_per_lu]
bdict['12'] = bdict['21']
bdict['13'] = bdict['31']
bdict['23'] = bdict['32']
frm_ph_lst = list(map(string.ascii_lowercase.index,linedict[indkey].from_phases))
to_ph_lst = list(map(string.ascii_lowercase.index,linedict[indkey].to_phases))
for fromidx in range(0, len(frm_ph_lst)):
for toidx in range(0, len(to_ph_lst)):
linedict[indkey].R[frm_ph_lst[fromidx], to_ph_lst[toidx]] = rdict[str(fromidx + 1) + str(toidx + 1)]
linedict[indkey].R[to_ph_lst[toidx], frm_ph_lst[fromidx]] = rdict[str(fromidx + 1) + str(toidx + 1)]
linedict[indkey].X[to_ph_lst[toidx], frm_ph_lst[fromidx]] = xdict[str(fromidx + 1) + str(toidx + 1)]
linedict[indkey].X[frm_ph_lst[fromidx], to_ph_lst[toidx]] = xdict[str(fromidx + 1) + str(toidx + 1)]
linedict[indkey].R = np.nan_to_num(np.multiply(linedict[indkey].R,linedict[indkey].length))
linedict[indkey].X = np.nan_to_num(np.multiply(linedict[indkey].X,linedict[indkey].length))
linedict[indkey].Z = linedict[indkey].R + 1j*linedict[indkey].X
linedict[indkey].Y = ZtoY(linedict[indkey].Z)
# Line per-unit quantities are set later in this script, once voltage bases have been established.
return linedict
# In[37]:
# Create switch dictionary
def switchbuilder(modeldata, busdict, timesteps):
switchsheet = modeldata.parse('Switch')
switchdict = dict()
for idx, row in switchsheet.iterrows():
if row['Status'] != 0: #only create node/edge if switch is closed
#uscoreposfrom = row['From Bus'].find('_')
#endfrom = len(row['From Bus'])-2 if uscoreposfrom == len(row['From Bus'])-2 else len(row['From Bus'])-1
indkeyfrom = row['From Bus'][:len(row['From Bus'])-2]
#uscoreposto = row['To Bus'].find('_')
#ndto = len(row['To Bus'])-2 if uscoreposto == len(row['To Bus'])-2 else len(row['To Bus'])-1
indkeyto = row['To Bus'][:len(row['To Bus'])-2]
indphasefrom = row['From Bus'][len(row['From Bus'])-1].lower()
indphaseto = row['To Bus'][len(row['To Bus'])-1].lower()
indkey = indkeyfrom + 'to' + indkeyto
if indkey in switchdict.keys(): # if already in switch list
switchdict[indkey].from_phases.append(indphasefrom)
switchdict[indkey].to_phases.append(indphaseto)
else:
switchdict[indkey] = switch(indkey, timesteps)
switchdict[indkey].from_phases.append(indphasefrom)
switchdict[indkey].to_phases.append(indphaseto)
switchdict[indkey].from_node = busdict[indkeyfrom]
switchdict[indkey].to_node = busdict[indkeyto]
switchdict[indkey].status.append(row['Status'])
busdict[indkeyfrom].edges_out.append(switchdict[indkey])
busdict[indkeyto].edges_in.append(switchdict[indkey])
# This next loop is ugly, but it works (it's the result of fixing a bug)
for indkey, entry in switchdict.items():
for idx in range(0,len(switchdict[indkey].to_phases)):
switchdict[indkey].phasevec = switchdict[indkey].phasevec + phase2vec(switchdict[indkey].to_phases[idx])
# Build Z, Y matrices (per unit)
assert(switchdict[indkey].from_node.Zbase == switchdict[indkey].to_node.Zbase)
Zbase = switchdict[indkey].from_node.Zbase
tempz = (1e-4)/Zbase # Based off of the DSS switch approximation: r1=r0=1e-4
tempy = 1/tempz
from_ph_lst = list(map(string.ascii_lowercase.index,switchdict[indkey].from_phases))
to_ph_lst = list(map(string.ascii_lowercase.index,switchdict[indkey].to_phases))
for idx in range(0, len(from_ph_lst)): # set impedance over switch to be tiny value, not zero
switchdict[indkey].Z[from_ph_lst[idx], to_ph_lst[idx]] = 1e-4
switchdict[indkey].Y[from_ph_lst[idx], to_ph_lst[idx]] = 1e4
switchdict[indkey].Zpu[from_ph_lst[idx], to_ph_lst[idx]] = tempz
switchdict[indkey].Ypu[from_ph_lst[idx], to_ph_lst[idx]] = tempy
return switchdict
# In[38]:
#Phase is a character among {'a', 'b', 'c'}
#Accomodates cross-phsae connections
def trans_helper(indkeyw0, indkeyw1, transdict, busdict, timesteps, subkVAbase, row):
'''
These are all of the following cases for a transformer:
'''
'''
bus_a_w0_side = None
bus_a_w0_side_phase = None
bus_b_w0_side = None
bus_b_w0_side_phase = None
bus_c_w0_side = None
bus_c_w0_side_phase = None
w0_phase_dict = {}
if row['w0_bus a']:
bus_a_w0_side = row['w0_bus a'][:len(row['w0_bus a'])-2]
bus_a_w0_side_phase = row['w0_bus a'][len(row['w0_bus a'])-1].lower()
w0_phase_dict[bus_a_w0_side] = [bus_a_w0_side_phase]
if row['w0_bus b']:
bus_b_w0_side = row['w0_bus b'][:len(row['w0_bus b'])-2]
bus_b_w0_side_phase = row['w0_bus b'][len(row['w0_bus b'])-1].lower()
if bus_b_w0_side in w0_phase_dict.keys():
w0_phase_dict[bus_b_w0_side].append(bus_b_w0_side_phase)
else:
w0_phase_dict[bus_b_w0_side] = [bus_b_w0_side_phase]
if row['w0_bus c']:
bus_c_w0_side = row['w0_bus c'][:len(row['w0_bus c'])-2]
bus_c_w0_side_phase = row['w0_bus c'][len(row['w0_bus c'])-1].lower()
if bus_c_w0_side in w0_phase_dict.keys():
w0_phase_dict[bus_c_w0_side].append(bus_c_w0_side_phase)
else:
w0_phase_dict[bus_c_w0_side] = [bus_c_w0_side_phase]
bus_a_w1_side = None
bus_a_w1_side_phase = None
bus_b_w1_side = None