-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathpti.jl
1756 lines (1457 loc) · 66.3 KB
/
pti.jl
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
#####################################################################
# #
# This file provides functions for interfacing with pti .raw files #
# #
#####################################################################
"""
A list of data file sections in the order that they appear in a PTI v33 file
"""
const _pti_sections = ["CASE IDENTIFICATION", "BUS", "LOAD", "FIXED SHUNT",
"GENERATOR", "BRANCH", "TRANSFORMER", "AREA INTERCHANGE",
"TWO-TERMINAL DC", "VOLTAGE SOURCE CONVERTER", "IMPEDANCE CORRECTION",
"MULTI-TERMINAL DC", "MULTI-SECTION LINE", "ZONE", "INTER-AREA TRANSFER",
"OWNER", "FACTS CONTROL DEVICE", "SWITCHED SHUNT", "GNE DEVICE",
"INDUCTION MACHINE"]
const _transaction_dtypes = [("IC", Int), ("SBASE", Float64), ("REV", Int),
("XFRRAT", Int), ("NXFRAT", Int), ("BASFRQ", Float64)]
const _bus_dtypes = [("I", Int), ("NAME", String), ("BASKV", Float64),
("IDE", Int), ("AREA", Int), ("ZONE", Int), ("OWNER", Int),
("VM", Float64), ("VA", Float64), ("NVHI", Float64), ("NVLO", Float64),
("EVHI", Float64), ("EVLO", Float64)]
const _load_dtypes = [("I", Int), ("ID", String), ("STATUS", Int),
("AREA", Int), ("ZONE", Int), ("PL", Float64), ("QL", Float64),
("IP", Float64), ("IQ", Float64), ("YP", Float64), ("YQ", Float64),
("OWNER", Int), ("SCALE", Int), ("INTRPT", Int)]
const _fixed_shunt_dtypes = [("I", Int), ("ID", String), ("STATUS", Int),
("GL", Float64), ("BL", Float64)]
const _generator_dtypes = [("I", Int), ("ID", String), ("PG", Float64),
("QG", Float64), ("QT", Float64), ("QB", Float64), ("VS", Float64),
("IREG", Int), ("MBASE", Float64), ("ZR", Float64), ("ZX", Float64),
("RT", Float64), ("XT", Float64), ("GTAP", Float64), ("STAT", Int),
("RMPCT", Float64), ("PT", Float64), ("PB", Float64), ("O1", Int),
("F1", Float64), ("O2", Int), ("F2", Float64), ("O3", Int),
("F3", Float64), ("O4", Int), ("F4", Float64), ("WMOD", Int),
("WPF", Float64)]
const _branch_dtypes = [("I", Int), ("J", Int), ("CKT", String),
("R", Float64), ("X", Float64), ("B", Float64), ("RATEA", Float64),
("RATEB", Float64), ("RATEC", Float64), ("GI", Float64), ("BI", Float64),
("GJ", Float64), ("BJ", Float64), ("ST", Int), ("MET", Int),
("LEN", Float64), ("O1", Int), ("F1", Float64), ("O2", Int),
("F2", Float64), ("O3", Int), ("F3", Float64), ("O4", Int),
("F4", Float64)]
const _transformer_dtypes = [("I", Int), ("J", Int), ("K", Int),
("CKT", String), ("CW", Int), ("CZ", Int), ("CM", Int),
("MAG1", Float64), ("MAG2", Float64), ("NMETR", Int), ("NAME", String),
("STAT", Int), ("O1", Int), ("F1", Float64), ("O2", Int),
("F2", Float64), ("O3", Int), ("F3", Float64), ("O4", Int),
("F4", Float64), ("VECGRP", String)]
const _transformer_3_1_dtypes = [("R1-2", Float64), ("X1-2", Float64),
("SBASE1-2", Float64), ("R2-3", Float64), ("X2-3", Float64),
("SBASE2-3", Float64), ("R3-1", Float64), ("X3-1", Float64),
("SBASE3-1", Float64), ("VMSTAR", Float64), ("ANSTAR", Float64)]
const _transformer_3_2_dtypes = [("WINDV1", Float64), ("NOMV1", Float64),
("ANG1", Float64), ("RATA1", Float64), ("RATB1", Float64),
("RATC1", Float64), ("COD1", Int), ("CONT1", Int), ("RMA1", Float64),
("RMI1", Float64), ("VMA1", Float64), ("VMI1", Float64), ("NTP1", Float64),
("TAB1", Int), ("CR1", Float64), ("CX1", Float64), ("CNXA1", Float64)]
const _transformer_3_3_dtypes = [("WINDV2", Float64), ("NOMV2", Float64),
("ANG2", Float64), ("RATA2", Float64), ("RATB2", Float64),
("RATC2", Float64), ("COD2", Int), ("CONT2", Int), ("RMA2", Float64),
("RMI2", Float64), ("VMA2", Float64), ("VMI2", Float64), ("NTP2", Float64),
("TAB2", Int), ("CR2", Float64), ("CX2", Float64), ("CNXA2", Float64)]
const _transformer_3_4_dtypes = [("WINDV3", Float64), ("NOMV3", Float64),
("ANG3", Float64), ("RATA3", Float64), ("RATB3", Float64),
("RATC3", Float64), ("COD3", Int), ("CONT3", Int), ("RMA3", Float64),
("RMI3", Float64), ("VMA3", Float64), ("VMI3", Float64), ("NTP3", Float64),
("TAB3", Int), ("CR3", Float64), ("CX3", Float64), ("CNXA3", Float64)]
const _transformer_2_1_dtypes = [("R1-2", Float64), ("X1-2", Float64),
("SBASE1-2", Float64)]
const _transformer_2_2_dtypes = [("WINDV1", Float64), ("NOMV1", Float64),
("ANG1", Float64), ("RATA1", Float64), ("RATB1", Float64),
("RATC1", Float64), ("COD1", Int), ("CONT1", Int), ("RMA1", Float64),
("RMI1", Float64), ("VMA1", Float64), ("VMI1", Float64), ("NTP1", Int),
("TAB1", Int), ("CR1", Float64), ("CX1", Float64), ("CNXA1", Float64)]
const _transformer_2_3_dtypes = [("WINDV2", Float64), ("NOMV2", Float64)]
const _area_interchange_dtypes = [("I", Int), ("ISW", Int),
("PDES", Float64), ("PTOL", Float64), ("ARNAME", String)]
const _two_terminal_line_dtypes = [("NAME", String), ("MDC", Int),
("RDC", Float64), ("SETVL", Float64), ("VSCHD", Float64),
("VCMOD", Float64), ("RCOMP", Float64), ("DELTI", Float64),
("METER", String), ("DCVMIN", Float64), ("CCCITMX", Int),
("CCCACC", Float64), ("IPR", Int), ("NBR", Int), ("ANMXR", Float64),
("ANMNR", Float64), ("RCR", Float64), ("XCR", Float64), ("EBASR", Float64),
("TRR", Float64), ("TAPR", Float64), ("TMXR", Float64), ("TMNR", Float64),
("STPR", Float64), ("ICR", Int), ("IFR", Int), ("ITR", Int),
("IDR", String), ("XCAPR", Float64), ("IPI", Int), ("NBI", Int),
("ANMXI", Float64), ("ANMNI", Float64), ("RCI", Float64), ("XCI", Float64),
("EBASI", Float64), ("TRI", Float64), ("TAPI", Float64), ("TMXI", Float64),
("TMNI", Float64), ("STPI", Float64), ("ICI", Int), ("IFI", Int),
("ITI", Int), ("IDI", String), ("XCAPI", Float64)]
const _vsc_line_dtypes = [("NAME", String), ("MDC", Int), ("RDC", Float64),
("O1", Int), ("F1", Float64), ("O2", Int), ("F2", Float64),
("O3", Int), ("F3", Float64), ("O4", Int), ("F4", Float64)]
const _vsc_subline_dtypes = [("IBUS", Int), ("TYPE", Int), ("MODE", Int),
("DCSET", Float64), ("ACSET", Float64), ("ALOSS", Float64),
("BLOSS", Float64), ("MINLOSS", Float64), ("SMAX", Float64),
("IMAX", Float64), ("PWF", Float64), ("MAXQ", Float64), ("MINQ", Float64),
("REMOT", Int), ("RMPCT", Float64)]
const _impedance_correction_dtypes = [("I", Int), ("T1", Float64),
("F1", Float64), ("T2", Float64), ("F2", Float64), ("T3", Float64),
("F3", Float64), ("T4", Float64), ("F4", Float64), ("T5", Float64),
("F5", Float64), ("T6", Float64), ("F6", Float64), ("T7", Float64),
("F7", Float64), ("T8", Float64), ("F8", Float64), ("T9", Float64),
("F9", Float64), ("T10", Float64), ("F10", Float64), ("T11", Float64),
("F11", Float64)]
const _multi_term_main_dtypes = [("NAME", String), ("NCONV", Int),
("NDCBS", Int), ("NDCLN", Int), ("MDC", Int), ("VCONV", Int),
("VCMOD", Float64), ("VCONVN", Float64)]
const _multi_term_nconv_dtypes = [("IB", Int), ("N", Int),
("ANGMX", Float64), ("ANGMN", Float64), ("RC", Float64), ("XC", Float64),
("EBAS", Float64), ("TR", Float64), ("TAP", Float64), ("TPMX", Float64),
("TPMN", Float64), ("TSTP", Float64), ("SETVL", Float64),
("DCPF", Float64), ("MARG", Float64), ("CNVCOD", Int)]
const _multi_term_ndcbs_dtypes = [("IDC", Int), ("IB", Int),
("AREA", Int), ("ZONE", Int), ("DCNAME", String), ("IDC2", Int),
("RGRND", Float64), ("OWNER", Int)]
const _multi_term_ndcln_dtypes = [("IDC", Int), ("JDC", Int),
("DCCKT", String), ("MET", Int), ("RDC", Float64), ("LDC", Float64)]
const _multi_section_dtypes = [("I", Int), ("J", Int), ("ID", String),
("MET", Int), ("DUM1", Int), ("DUM2", Int), ("DUM3", Int),
("DUM4", Int), ("DUM5", Int), ("DUM6", Int), ("DUM7", Int),
("DUM8", Int), ("DUM9", Int)]
const _zone_dtypes = [("I", Int), ("ZONAME", String)]
const _interarea_dtypes = [("ARFROM", Int), ("ARTO", Int),
("TRID", String), ("PTRAN", Float64)]
const _owner_dtypes = [("I", Int), ("OWNAME", String)]
const _FACTS_dtypes = [("NAME", String), ("I", Int), ("J", Int),
("MODE", Int), ("PDES", Float64), ("QDES", Float64), ("VSET", Float64),
("SHMX", Float64), ("TRMX", Float64), ("VTMN", Float64), ("VTMX", Float64),
("VSMX", Float64), ("IMX", Float64), ("LINX", Float64), ("RMPCT", Float64),
("OWNER", Int), ("SET1", Float64), ("SET2", Float64), ("VSREF", Int),
("REMOT", Int), ("MNAME", String)]
const _switched_shunt_dtypes = [("I", Int), ("MODSW", Int),
("ADJM", Int), ("STAT", Int), ("VSWHI", Float64), ("VSWLO", Float64),
("SWREM", Int), ("RMPCT", Float64), ("RMIDNT", String), ("BINIT", Float64),
("N1", Int), ("B1", Float64), ("N2", Int), ("B2", Float64),
("N3", Int), ("B3", Float64), ("N4", Int), ("B4", Float64),
("N5", Int), ("B5", Float64), ("N6", Int), ("B6", Float64),
("N7", Int), ("B7", Float64), ("N8", Int), ("B8", Float64)]
# TODO: Account for multiple lines in GNE Device entries
const _gne_device_dtypes = [("NAME", String), ("MODEL", String),
("NTERM", Int), ("BUSi", Int), ("NREAL", Int), ("NINTG", Int),
("NCHAR", Int), ("STATUS", Int), ("OWNER", Int), ("NMETR", Int),
("REALi", Float64), ("INTGi", Int), ("CHARi", String)]
const _induction_machine_dtypes = [("I", Int), ("ID", String),
("STAT", Int), ("SCODE", Int), ("DCODE", Int), ("AREA", Int),
("ZONE", Int), ("OWNER", Int), ("TCODE", Int), ("BCODE", Int),
("MBASE", Float64), ("RATEKV", Float64), ("PCODE", Int),
("PSET", Float64), ("H", Float64), ("A", Float64), ("B", Float64),
("D", Float64), ("E", Float64), ("RA", Float64), ("XA", Float64),
("XM", Float64), ("R1", Float64), ("X1", Float64), ("R2", Float64),
("X2", Float64), ("X3", Float64), ("E1", Float64), ("SE1", Float64),
("E2", Float64), ("SE2", Float64), ("IA1", Float64), ("IA2", Float64),
("XAMULT", Float64)]
"""
lookup array of data types for PTI file sections given by
`field_name`, as enumerated by PSS/E Program Operation Manual.
"""
const _pti_dtypes = Dict{String,Array}(
"BUS" => _bus_dtypes,
"LOAD" => _load_dtypes,
"FIXED SHUNT" => _fixed_shunt_dtypes,
"GENERATOR" => _generator_dtypes,
"BRANCH" => _branch_dtypes,
"TRANSFORMER" => _transformer_dtypes,
"TRANSFORMER TWO-WINDING LINE 1" => _transformer_2_1_dtypes,
"TRANSFORMER TWO-WINDING LINE 2" => _transformer_2_2_dtypes,
"TRANSFORMER TWO-WINDING LINE 3" => _transformer_2_3_dtypes,
"TRANSFORMER THREE-WINDING LINE 1" => _transformer_3_1_dtypes,
"TRANSFORMER THREE-WINDING LINE 2" => _transformer_3_2_dtypes,
"TRANSFORMER THREE-WINDING LINE 3" => _transformer_3_3_dtypes,
"TRANSFORMER THREE-WINDING LINE 4" => _transformer_3_4_dtypes,
"AREA INTERCHANGE" => _area_interchange_dtypes,
"TWO-TERMINAL DC" => _two_terminal_line_dtypes,
"VOLTAGE SOURCE CONVERTER" => _vsc_line_dtypes,
"VOLTAGE SOURCE CONVERTER SUBLINES" => _vsc_subline_dtypes,
"IMPEDANCE CORRECTION" => _impedance_correction_dtypes,
"MULTI-TERMINAL DC" => _multi_term_main_dtypes,
"MULTI-TERMINAL DC NCONV" => _multi_term_nconv_dtypes,
"MULTI-TERMINAL DC NDCBS" => _multi_term_ndcbs_dtypes,
"MULTI-TERMINAL DC NDCLN" => _multi_term_ndcln_dtypes,
"MULTI-SECTION LINE" => _multi_section_dtypes,
"ZONE" => _zone_dtypes,
"INTER-AREA TRANSFER" => _interarea_dtypes,
"OWNER" => _owner_dtypes,
"FACTS CONTROL DEVICE" => _FACTS_dtypes,
"SWITCHED SHUNT" => _switched_shunt_dtypes,
"CASE IDENTIFICATION" => _transaction_dtypes,
"GNE DEVICE" => _gne_device_dtypes,
"INDUCTION MACHINE" => _induction_machine_dtypes
)
const _default_case_identification = Dict("IC" => 0, "SBASE" => 100.0,
"REV" => 33, "XFRRAT" => 0, "NXFRAT" => 0, "BASFRQ" => 60)
const _default_bus = Dict("BASKV" => 0.0, "IDE" => 1, "AREA" => 1, "ZONE" => 1,
"OWNER" => 1, "VM" => 1.0, "VA" => 0.0, "NVHI" => 1.1, "NVLO" => 0.9,
"EVHI" => 1.1, "EVLO" => 0.9, "NAME" => " ")
const _default_load = Dict("ID" => "1", "STATUS" => 1, "PL" => 0.0, "QL" => 0.0,
"IP" => 0.0, "IQ" => 0.0, "YP" => 0.0, "YQ" => 0.0, "SCALE" => 1,
"INTRPT" => 0, "AREA" => nothing, "ZONE" => nothing, "OWNER" => nothing)
const _default_fixed_shunt = Dict("ID" => "1", "STATUS" => 1, "GL" => 0.0, "BL" => 0.0)
const _default_generator = Dict("ID" => "1", "PG" => 0.0, "QG" => 0.0, "QT" => 9999.0,
"QB" => -9999.0, "VS" => 1.0, "IREG" => 0, "MBASE" => nothing, "ZR" => 0.0,
"ZX" => 1.0, "RT" => 0.0, "XT" => 0.0, "GTAP" => 1.0, "STAT" => 1,
"RMPCT" => 100.0, "PT" => 9999.0, "PB" => -9999.0, "O1" => nothing,
"O2" => 0, "O3" => 0, "O4" => 0, "F1" => 1.0,"F2" => 1.0, "F3" => 1.0,
"F4" => 1.0, "WMOD" => 0, "WPF" => 1.0)
const _default_branch = Dict("CKT" => "1", "B" => 0.0, "RATEA" => 0.0,
"RATEB" => 0.0, "RATEC" => 0.0, "GI" => 0.0, "BI" => 0.0, "GJ" => 0.0,
"BJ" => 0.0, "ST" => 1, "MET" => 1, "LEN" => 0.0, "O1" => nothing,
"O2" => 0, "O3" => 0, "O4" => 0, "F1" => 1.0, "F2" => 1.0, "F3" => 1.0,
"F4" => 1.0)
const _default_transformer = Dict("K" => 0, "CKT" => "1", "CW" => 1, "CZ" => 1,
"CM" => 1, "MAG1" => 0.0, "MAG2" => 0.0, "NMETR" => 2,
"NAME" => " ", "STAT" => 1, "O1" => nothing, "O2" => 0,
"O3" => 0, "O4" => 0, "F1" => 1.0, "F2" => 1.0, "F3" => 1.0, "F4" => 1.0,
"VECGRP" => " ", "R1-2" => 0.0, "SBASE1-2" => nothing,
"R2-3" => 0.0, "SBASE2-3" => nothing, "R3-1" => 0.0, "SBASE3-1" => nothing,
"VMSTAR" => 1.0, "ANSTAR" => 0.0,
"WINDV1" => nothing,
"NOMV1" => 0.0, "ANG1" => 0.0, "RATA1" => 0.0,
"RATB1" => 0.0, "RATC1" => 0.0, "COD1" => 0,
"CONT1" => 0, "RMA1" => 1.1, "RMI1" => 0.9,
"VMA1" => 1.1, "VMI1" => 0.9, "NTP1" => 33,
"TAB1" => 0, "CR1" => 0.0, "CX1" => 0.0, "CNXA1" => 0.0,
"WINDV2" => nothing,
"NOMV2" => 0.0, "ANG2" => 0.0, "RATA2" => 0.0,
"RATB2" => 0.0, "RATC2" => 0.0, "COD2" => 0,
"CONT2" => 0, "RMA2" => 1.1, "RMI2" => 0.9,
"VMA2" => 1.1, "VMI2" => 0.9, "NTP2" => 33,
"TAB2" => 0, "CR2" => 0.0, "CX2" => 0.0,
"CNXA2" => 0.0,
"WINDV3" => nothing,
"NOMV3" => 0.0, "ANG3" => 0.0, "RATA3" => 0.0,
"RATB3" => 0.0, "RATC3" => 0.0, "COD3" => 0,
"CONT3" => 0, "RMA3" => 1.1, "RMI3" => 0.9,
"VMA3" => 1.1, "VMI3" => 0.9, "NTP3" => 33,
"TAB3" => 0, "CR3" => 0.0, "CX3" => 0.0,
"CNXA3" => 0.0)
const _default_area_interchange = Dict("ISW" => 0, "PDES" => 0.0,
"PTOL" => 10.0, "ARNAME" => " ")
const _default_two_terminal_dc = Dict("MDC" => 0, "VCMOD" => 0.0,
"RCOMP" => 0.0, "DELTI" => 0.0, "METER" => "I", "DCVMIN" => 0.0,
"CCCITMX" => 20, "CCCACC" => 1.0, "TRR" => 1.0, "TAPR" => 1.0,
"TMXR" => 1.5, "TMNR" => 0.51, "STPR" => 0.00625, "ICR" => 0, "IFR" => 0,
"ITR" => 0, "IDR" => "1", "XCAPR" => 0.0, "TRI" => 1.0, "TAPI" => 1.0,
"TMXI" => 1.5, "TMNI" => 0.51, "STPI" => 0.00625, "ICI" => 0, "IFI" => 0,
"ITI" => 0, "IDI" => "1", "XCAPI" => 0.0)
const _default_vsc_dc = Dict("MDC" => 1, "O1" => nothing, "O2" => 0, "O3" => 0,
"O4" => 0, "F1" => 1.0, "F2" => 1.0, "F3" => 1.0, "F4" => 1.0,
"CONVERTER BUSES" => Dict("MODE" => 1, "ACSET" => 1.0, "ALOSS" => 1.0,
"BLOSS" => 0.0, "MINLOSS" => 0.0, "SMAX" => 0.0, "IMAX" => 0.0,
"PWF" => 1.0, "MAXQ" => 9999.0, "MINQ" => -9999.0, "REMOT" => 0,
"RMPCT" => 100.0)
)
const _default_impedance_correction = Dict("T1" => 0.0, "T2" => 0.0, "T3" => 0.0,
"T4" => 0.0, "T5" => 0.0, "T6" => 0.0, "T7" => 0.0, "T8" => 0.0, "T9" => 0.0,
"T10" => 0.0, "T11" => 0.0, "F1" => 0.0, "F2" => 0.0, "F3" => 0.0,
"F4" => 0.0, "F5" => 0.0, "F6" => 0.0, "F7" => 0.0, "F8" => 0.0,
"F9" => 0.0, "F10" => 0.0, "F11" => 0.0)
const _default_multi_term_dc = Dict("MDC" => 0, "VCMOD" => 0.0, "VCONVN" => 0,
"CONV" => Dict("TR" => 1.0, "TAP" => 1.0, "TPMX" => 1.5, "TPMN" => 0.51,
"TSTP" => 0.00625,"DCPF" => 1, "MARG" => 0.0, "CNVCOD" => 1),
"DCBS" => Dict("IB" => 0.0, "AREA" => 1, "ZONE" => 1,
"DCNAME" => " ", "IDC2" => 0, "RGRND" => 0.0, "OWNER" => 1),
"DCLN" => Dict("DCCKT" => 1, "MET" => 1, "LDC" => 0.0)
)
const _default_multi_section = Dict("ID" => "&1", "MET" => 1)
const _default_zone = Dict("ZONAME" => " ")
const _default_interarea = Dict("TRID" => 1, "PTRAN" => 0.0)
const _default_owner = Dict("OWNAME" => " ")
const _default_facts = Dict("J" => 0, "MODE" => 1, "PDES" => 0.0, "QDES" => 0.0,
"VSET" => 1.0, "SHMX" => 9999.0, "TRMX" => 9999.0, "VTMN" => 0.9,
"VTMX" => 1.1, "VSMX" => 1.0, "IMX" => 0.0, "LINX" => 0.05,
"RMPCT" => 100.0, "OWNER" => 1, "SET1" => 0.0, "SET2" => 0.0, "VSREF" => 0,
"REMOT" => 0, "MNAME" => "")
const _default_switched_shunt = Dict("MODSW" => 1, "ADJM" => 0, "STAT" => 1,
"VSWHI" => 1.0, "VSWLO" => 1.0, "SWREM" => 0, "RMPCT" => 100.0,
"RMIDNT" => "", "BINIT" => 0.0,
"N1" => 0, "N2" => 0, "N3" => 0, "N4" => 0, "N5" => 0,
"N6" => 0, "N7" => 0, "N8" => 0,
"B1" => 0.0, "B2" => 0.0, "B3" => 0.0, "B4" => 0.0, "B5" => 0.0,
"B6" => 0.0, "B7" => 0.0, "B8" => 0.0)
const _default_gne_device = Dict("NTERM" => 1, "NREAL" => 0, "NINTG" => 0,
"NCHAR" => 0, "STATUS" => 1, "OWNER" => nothing, "NMETR" => nothing,
"REAL" => 0, "INTG" => nothing,
"CHAR" => "1")
const _default_induction_machine = Dict("ID" => 1, "STAT" => 1, "SCODE" => 1,
"DCODE" => 2, "AREA" => nothing, "ZONE" => nothing, "OWNER" => nothing,
"TCODE" => 1, "BCODE" => 1, "MBASE" => nothing, "RATEKV" => 0.0,
"PCODE" => 1, "H" => 1.0, "A" => 1.0, "B" => 1.0, "D" => 1.0, "E" => 1.0,
"RA" => 0.0, "XA" => 0.0, "XM" => 2.5, "R1" => 999.0, "X1" => 999.0,
"R2" => 999.0, "X2" => 999.0, "X3" => 0.0, "E1" => 1.0, "SE1" => 0.0,
"E2" => 1.2, "SE2" => 0.0, "IA1" => 0.0, "IA2" => 0.0, "XAMULT" => 1)
const _pti_defaults = Dict("BUS" => _default_bus,
"LOAD" => _default_load,
"FIXED SHUNT" => _default_fixed_shunt,
"GENERATOR" => _default_generator,
"BRANCH" => _default_branch,
"TRANSFORMER" => _default_transformer,
"AREA INTERCHANGE" => _default_area_interchange,
"TWO-TERMINAL DC" => _default_two_terminal_dc,
"VOLTAGE SOURCE CONVERTER" => _default_vsc_dc,
"IMPEDANCE CORRECTION" => _default_impedance_correction,
"MULTI-TERMINAL DC" => _default_multi_term_dc,
"MULTI-SECTION LINE" => _default_multi_section,
"ZONE" => _default_zone,
"INTER-AREA TRANSFER" => _default_interarea,
"OWNER" => _default_owner,
"FACTS CONTROL DEVICE" => _default_facts,
"SWITCHED SHUNT" => _default_switched_shunt,
"CASE IDENTIFICATION" => _default_case_identification,
"GNE DEVICE" => _default_gne_device,
"INDUCTION MACHINE" => _default_induction_machine
)
function _correct_nothing_values!(data::Dict)
if !haskey(data, "BUS")
return
end
sbase = data["CASE IDENTIFICATION"][1]["SBASE"]
bus_lookup = Dict(bus["I"] => bus for bus in data["BUS"])
if haskey(data, "LOAD")
for load in data["LOAD"]
load_bus = bus_lookup[load["I"]]
if load["AREA"] == nothing
load["AREA"] = load_bus["AREA"]
end
if load["ZONE"] == nothing
load["ZONE"] = load_bus["ZONE"]
end
if load["OWNER"] == nothing
load["OWNER"] = load_bus["OWNER"]
end
end
end
if haskey(data, "GENERATOR")
for gen in data["GENERATOR"]
gen_bus = bus_lookup[gen["I"]]
if haskey(gen, "OWNER") && gen["OWNER"] == nothing
gen["OWNER"] = gen_bus["OWNER"]
end
if gen["MBASE"] == nothing
gen["MBASE"] = sbase
end
end
end
if haskey(data, "BRANCH")
for branch in data["BRANCH"]
branch_bus = bus_lookup[branch["I"]]
if haskey(branch, "OWNER") && branch["OWNER"] == nothing
branch["OWNER"] = branch_bus["OWNER"]
end
end
end
if haskey(data, "TRANSFORMER")
for transformer in data["TRANSFORMER"]
transformer_bus = bus_lookup[transformer["I"]]
for base_id in ["SBASE1-2", "SBASE2-3", "SBASE3-1"]
if haskey(transformer, base_id) && transformer[base_id] == nothing
transformer[base_id] = sbase
end
end
for winding_id in ["WINDV1", "WINDV2", "WINDV3"]
if haskey(transformer, winding_id) && transformer[winding_id] == nothing
if transformer["CW"] == 2
transformer[winding_id] = transformer_bus["BASKV"]
else
transformer[winding_id] = 1.0
end
end
end
end
end
#=
# TODO update this default value
if haskey(data, "VOLTAGE SOURCE CONVERTER")
for mdc in data["VOLTAGE SOURCE CONVERTER"]
mdc["O1"] = Expr(:call, :_get_component_property, data["BUS"], "OWNER", "I", get(get(component, "CONVERTER BUSES", [Dict()])[1], "IBUS", 0))
end
end
=#
if haskey(data, "GNE DEVICE")
for gne in data["GNE DEVICE"]
gne_bus = bus_lookup[gne["I"]]
if haskey(gne, "OWNER") && gne["OWNER"] == nothing
gne["OWNER"] = gne_bus["OWNER"]
end
if haskey(gne, "NMETR") && gne["NMETR"] == nothing
gne["NMETR"] = gne_bus["NTERM"]
end
end
end
if haskey(data, "INDUCTION MACHINE")
for indm in data["INDUCTION MACHINE"]
indm_bus = bus_lookup[indm["I"]]
if indm["AREA"] == nothing
indm["AREA"] = indm_bus["AREA"]
end
if indm["ZONE"] == nothing
indm["ZONE"] = indm_bus["ZONE"]
end
if indm["OWNER"] == nothing
indm["OWNER"] = indm_bus["OWNER"]
end
if indm["MBASE"] == nothing
indm["MBASE"] = sbase
end
end
end
end
"""
This is an experimental method for parsing elements and setting defaults at the same time.
It is not currently working but would reduce memory allocations if implemented correctly.
"""
function _parse_elements(elements::Array, dtypes::Array, defaults::Dict, section::AbstractString)
data = Dict{String,Any}()
if length(elements) > length(dtypes)
Memento.warn(_LOGGER, "ignoring $(length(elements) - length(dtypes)) extra values in section $section, only $(length(dtypes)) items are defined")
elements = elements[1:length(dtypes)]
end
for (i,element) in enumerate(elements)
field, dtype = dtypes[i]
element = strip(element)
if dtype == String
if startswith(element, "'") && endswith(element, "'")
data[field] = element[2:end-1]
else
data[field] = element
end
else
if length(element) <= 0
# this will be set to a default in the cleanup phase
data[field] = nothing
else
try
data[field] = parse(dtype, element)
catch message
if isa(message, Meta.ParseError)
data[field] = element
else
Memento.error(_LOGGER, "value '$element' for $field in section $section is not of type $dtype.")
end
end
end
end
end
if length(elements) < length(dtypes)
for (field, dtype) in dtypes[length(elements):end]
data[field] = defaults[field]
#=
if length(missing_fields) > 0
for field in missing_fields
data[field] = ""
end
missing_str = join(missing_fields, ", ")
if !(section == "SWITCHED SHUNT" && startswith(missing_str, "N")) &&
!(section == "MULTI-SECTION LINE" && startswith(missing_str, "DUM")) &&
!(section == "IMPEDANCE CORRECTION" && startswith(missing_str, "T"))
Memento.warn(_LOGGER, "The following fields in $section are missing: $missing_str")
end
end
=#
end
end
return data
end
"""
_parse_line_element!(data, elements, section)
Internal function. Parses a single "line" of data elements from a PTI file, as
given by `elements` which is an array of the line, typically split at `,`.
Elements are parsed into data types given by `section` and saved into `data::Dict`.
"""
function _parse_line_element!(data::Dict, elements::Array, section::AbstractString)
missing_fields = []
for (i, (field, dtype)) in enumerate(_pti_dtypes[section])
if i > length(elements)
Memento.debug(_LOGGER, "Have run out of elements in $section at $field")
push!(missing_fields, field)
continue
else
element = strip(elements[i])
end
try
if dtype != String && element != ""
data[field] = parse(dtype, element)
else
if dtype == String && startswith(element, "'") && endswith(element, "'")
data[field] = chop(element[nextind(element,1):end])
else
data[field] = element
end
end
catch message
if isa(message, Meta.ParseError)
data[field] = element
else
Memento.error(_LOGGER, "value '$element' for $field in section $section is not of type $dtype.")
end
end
end
if length(missing_fields) > 0
for field in missing_fields
data[field] = ""
end
missing_str = join(missing_fields, ", ")
if !(section == "SWITCHED SHUNT" && startswith(missing_str, "N")) &&
!(section == "MULTI-SECTION LINE" && startswith(missing_str, "DUM")) &&
!(section == "IMPEDANCE CORRECTION" && startswith(missing_str, "T"))
Memento.warn(_LOGGER, "The following fields in $section are missing: $missing_str")
end
end
end
const _comment_split = r"(?!\B[\'][^\']*)[\/](?![^\']*[\']\B)"
const _split_string = r",(?=(?:[^']*'[^']*')*[^']*$)"
"""
_get_line_elements(line)
Internal function. Uses regular expressions to extract all separate data
elements from a line of a PTI file and populate them into an `Array{String}`.
Comments, typically indicated at the end of a line with a `'/'` character,
are also extracted separately, and `Array{Array{String}, String}` is returned.
"""
function _get_line_elements(line::AbstractString)
if count(i->(i=="'"), line) % 2 == 1
throw(Memento.error(_LOGGER, "There are an uneven number of single-quotes in \"{line}\", the line cannot be parsed."))
end
line_comment = split(line, _comment_split, limit=2)
line = strip(line_comment[1])
comment = length(line_comment) > 1 ? strip(line_comment[2]) : ""
elements = split(line, _split_string)
return (elements, comment)
end
"""
_parse_pti_data(data_string, sections)
Internal function. Parse a PTI raw file into a `Dict`, given the
`data_string` of the file and a list of the `sections` in the PTI
file (typically given by default by `get_pti_sections()`.
"""
function _parse_pti_data(data_io::IO)
sections = deepcopy(_pti_sections)
data_lines = readlines(data_io)
skip_lines = 0
skip_sublines = 0
subsection = ""
pti_data = Dict{String,Array{Dict}}()
section = popfirst!(sections)
section_data = Dict{String,Any}()
for (line_number, line) in enumerate(data_lines)
(elements, comment) = _get_line_elements(line)
first_element = strip(elements[1])
if line_number > 3 && length(elements) != 0 && first_element == "Q"
break
elseif line_number > 3 && length(elements) != 0 && first_element == "0"
if line_number == 4
section = popfirst!(sections)
end
if length(elements) > 1
Memento.warn(_LOGGER, "At line $line_number, new section started with '0', but additional non-comment data is present. Pattern '^\\s*0\\s*[/]*.*' is reserved for section start/end.")
elseif length(comment) > 0
Memento.debug(_LOGGER, "At line $line_number, switched to $section")
end
if !isempty(sections)
section = popfirst!(sections)
end
continue
else
if line_number == 4
section = popfirst!(sections)
section_data = Dict{String,Any}()
end
if skip_lines > 0
skip_lines -= 1
continue
end
Memento.debug(_LOGGER, join(["Section:", section], " "))
if !(section in ["CASE IDENTIFICATION","TRANSFORMER","VOLTAGE SOURCE CONVERTER","MULTI-TERMINAL DC","TWO-TERMINAL DC","GNE DEVICE"])
section_data = Dict{String,Any}()
try
_parse_line_element!(section_data, elements, section)
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
elseif section == "CASE IDENTIFICATION"
if line_number == 1
try
_parse_line_element!(section_data, elements, section)
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
if section_data["REV"] != "" && section_data["REV"] < 33
Memento.warn(_LOGGER, "Version $(section_data["REV"]) of PTI format is unsupported, parser may not function correctly.")
end
else
section_data["Comment_Line_$(line_number - 1)"] = line
end
if line_number < 3
continue
end
elseif section == "TRANSFORMER"
section_data = Dict{String,Any}()
if parse(Int, _get_line_elements(line)[1][3]) == 0 # two winding transformer
winding = "TWO-WINDING"
skip_lines = 3
elseif parse(Int, _get_line_elements(line)[1][3]) != 0 # three winding transformer
winding = "THREE-WINDING"
skip_lines = 4
else
Memento.error(_LOGGER, "Cannot detect type of Transformer")
end
try
for transformer_line in 0:4
if transformer_line == 0
temp_section = section
else
temp_section = join([section, winding, "LINE", transformer_line], " ")
end
if winding == "TWO-WINDING" && transformer_line == 4
break
else
elements = _get_line_elements(data_lines[line_number + transformer_line])[1]
_parse_line_element!(section_data, elements, temp_section)
end
end
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
elseif section == "VOLTAGE SOURCE CONVERTER"
if length(_get_line_elements(line)[1]) == 11
section_data = Dict{String,Any}()
try
_parse_line_element!(section_data, elements, section)
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
skip_sublines = 2
continue
elseif skip_sublines > 0
skip_sublines -= 1
subsection_data = Dict{String,Any}()
for (field, dtype) in _pti_dtypes["$section SUBLINES"]
element = popfirst!(elements)
if element != ""
subsection_data[field] = parse(dtype, element)
else
subsection_data[field] = ""
end
end
if haskey(section_data, "CONVERTER BUSES")
push!(section_data["CONVERTER BUSES"], subsection_data)
else
section_data["CONVERTER BUSES"] = [subsection_data]
continue
end
end
elseif section == "TWO-TERMINAL DC"
section_data = Dict{String,Any}()
if length(_get_line_elements(line)[1]) == 12
(elements, comment) = _get_line_elements(join(data_lines[line_number:line_number + 2], ','))
skip_lines = 2
end
try
_parse_line_element!(section_data, elements, section)
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
elseif section == "MULTI-TERMINAL DC"
if skip_sublines == 0
section_data = Dict{String,Any}()
try
_parse_line_element!(section_data, elements, section)
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
if section_data["NCONV"] > 0
skip_sublines = section_data["NCONV"]
subsection = "NCONV"
continue
elseif section_data["NDCBS"] > 0
skip_sublines = section_data["NDCBS"]
subsection = "NDCBS"
continue
elseif section_data["NDCLN"] > 0
skip_sublines = section_data["NDCLN"]
subsection = "NDCLN"
continue
end
end
if skip_sublines > 0
skip_sublines -= 1
subsection_data = Dict{String,Any}()
try
_parse_line_element!(subsection_data, elements, "$section $subsection")
catch message
throw(Memento.error(_LOGGER, "Parsing failed at line $line_number: $(sprint(showerror, message))"))
end
if haskey(section_data, "$(subsection[2:end])")
section_data["$(subsection[2:end])"] = push!(section_data["$(subsection[2:end])"], subsection_data)
if skip_sublines > 0 && subsection != "NDCLN"
continue
end
else
section_data["$(subsection[2:end])"] = [subsection_data]
if skip_sublines > 0 && subsection != "NDCLN"
continue
end
end
if skip_sublines == 0 && subsection != "NDCLN"
if subsection == "NDCBS"
skip_sublines = section_data["NDCLN"]
subsection = "NDCLN"
continue
elseif subsection == "NCONV"
skip_sublines = section_data["NDCBS"]
subsection = "NDCBS"
continue
end
elseif skip_sublines == 0 && subsection == "NDCLN"
subsection = ""
else
continue
end
end
elseif section == "GNE DEVICE"
# TODO: handle multiple lines of GNE Device
Memento.warn(_LOGGER, "GNE DEVICE parsing is not supported.")
end
end
if subsection != ""
Memento.debug(_LOGGER, "appending data")
end
if haskey(pti_data, section)
push!(pti_data[section], section_data)
else
pti_data[section] = [section_data]
end
end
_populate_defaults!(pti_data)
_correct_nothing_values!(pti_data)
return pti_data
end
"""
parse_pti(filename::String)
Open PTI raw file given by `filename`, returning a `Dict` of the data parsed
into the proper types.
"""
function parse_pti(filename::String)::Dict
pti_data = open(filename) do f
parse_pti(f)
end
return pti_data
end
"""
parse_pti(io::IO)
Reads PTI data in `io::IO`, returning a `Dict` of the data parsed into the
proper types.
"""
function parse_pti(io::IO)::Dict
pti_data = _parse_pti_data(io)
try
pti_data["CASE IDENTIFICATION"][1]["NAME"] = match(r"^\<file\s[\/\\]*(?:.*[\/\\])*(.*)\.raw\>$", lowercase(io.name)).captures[1]
catch
Memento.info(_LOGGER, "unable to recover case name from io file name in parse_pti")
end
return pti_data
end
"""
_populate_defaults!(pti_data)
Internal function. Populates empty fields with PSS(R)E PTI v33 default values
"""
function _populate_defaults!(data::Dict)
for section in _pti_sections
if haskey(data, section)
component_defaults = _pti_defaults[section]
for component in data[section]
for (field, field_value) in component
if isa(field_value, Array)
sub_component_defaults = component_defaults[field]
for sub_component in field_value
for (sub_field, sub_field_value) in sub_component
if sub_field_value == ""
try
sub_component[sub_field] = sub_component_defaults[sub_field]
catch msg
if isa(msg, KeyError)
Memento.warn(_LOGGER, "'$sub_field' in '$field' in '$section' has no default value")
else
rethrow(msg)
end
end
end
end
end
elseif field_value == "" && !(field in ["Comment_Line_1", "Comment_Line_2"]) && !startswith(field, "DUM")
try
component[field] = component_defaults[field]
catch msg
if isa(msg, KeyError)
Memento.warn(_LOGGER, "'$field' in '$section' has no default value")
else
rethrow(msg)
end
end
end
end
end
end
end
end
"Export power network data in the pti format"
function export_pti(data::Dict{String,Any})
return sprint(export_pti, data)
end
"Export power network data to a file in the pti format"
function export_pti(file::AbstractString, data::Dict{String,Any})
open(file, "w") do io
export_pti(io, data)
end
end
"""
export_pti(io::IO, data::Dict{String, Any})
Export PowerModels network data dictionary to the as a power flow raw data
acording to the pti format `RAW V33`.
It is highly recommend to export the PowerModel data dictionary in the same
format as the source data.
The `export_pti` function exports the essential components of a network:
- Buses
- Loads
- Fixed Shunts
- Generators
- Non Tansformers Branchs
- Transformers (Two-Windings and Three-Windings)
- Switched Shunts (aproximate)
If the PowerModels was parsed from a pti file with the `import_all=true` parameter:
`data = parse_file(case3.raw, import_all=true)`
It will export these aditionals items:
- Header Options
- Comment Lines
- Zone Data
- Area Data
- Owner Data
- Switched Shunts (with block steps)
Things that are not exported:
- TNEP network specification
- Generation Cost Data
- Storage
- Switches
- DC Lines (future work, #754)
Things that are not exported if you use `import_all = true` to make the PowerModel data dict:
- FACTS (Maybe in future work)
- GNE (No intentions to export it)
- Inter Area Transfer Data (No intentions to export it)
"""
function export_pti(io::IO, data::Dict{String,Any})