-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathpsse.jl
More file actions
2382 lines (2130 loc) · 96.6 KB
/
Copy pathpsse.jl
File metadata and controls
2382 lines (2130 loc) · 96.6 KB
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
# Parse PSS(R)E data from PTI file into PowerModels data format
"""
_init_bus!(bus, id)
Initializes a `bus` of id `id` with default values given in the PSS(R)E
specification.
"""
function _init_bus!(bus::Dict{String, Any}, id::Int)
bus["bus_i"] = id
bus["bus_type"] = 1
bus["area"] = 1
bus["vm"] = 1.0
bus["va"] = 0.0
bus["base_kv"] = 1.0
bus["zone"] = 1
bus["name"] = " "
bus["vmax"] = 1.1
bus["vmin"] = 0.9
bus["index"] = id
return
end
function _find_bus_value(bus_i::Int, field::String, pm_bus_data::Array)
for bus in pm_bus_data
if bus["index"] == bus_i
return bus[field]
end
end
@info("Could not find bus $bus_i, returning 0 for field $field")
return 0
end
function _find_bus_value(bus_i::Int, field::String, pm_bus_data::Dict)
if !haskey(pm_bus_data, bus_i)
@info("Could not find bus $bus_i, returning 0 for field $field")
return 0
else
return pm_bus_data[bus_i][field]
end
end
"""
_get_bus_value(bus_i, field, pm_data)
Returns the value of `field` of `bus_i` from the PowerModels data. Requires
"bus" Dict to already be populated.
"""
function _get_bus_value(bus_i::Int, field::String, pm_data::Dict{String, Any})
return _find_bus_value(bus_i, field, pm_data["bus"])
end
"""
_find_max_bus_id(pm_data)
Returns the maximum bus id in `pm_data`
"""
function _find_max_bus_id(pm_data::Dict)::Int
max_id = 0
for bus in values(pm_data["bus"])
if bus["index"] > max_id && !endswith(bus["name"], "starbus")
max_id = bus["index"]
end
end
return max_id
end
"""
create_starbus(pm_data, transformer)
Creates a starbus from a given three-winding `transformer`. "source_id" is given
by `["bus_i", "name", "I", "J", "K", "CKT"]` where "bus_i" and "name" are the
modified names for the starbus, and "I", "J", "K" and "CKT" come from the
originating transformer, in the PSS(R)E transformer specification.
"""
function _create_starbus_from_transformer(
pm_data::Dict,
transformer::Dict,
starbus_id::Int,
)::Dict
starbus = Dict{String, Any}()
_init_bus!(starbus, starbus_id)
starbus["name"] = "starbus_$(transformer["I"])_$(transformer["J"])_$(transformer["K"])_$(strip(transformer["CKT"]))"
bus_type = 1
starbus["vm"] = transformer["VMSTAR"]
starbus["va"] = transformer["ANSTAR"]
starbus["bus_type"] = bus_type
if transformer["STAT"] != 0
starbus["bus_status"] = true
else
starbus["bus_status"] = false
end
starbus["area"] = _get_bus_value(transformer["I"], "area", pm_data)
starbus["zone"] = _get_bus_value(transformer["I"], "zone", pm_data)
starbus["hidden"] = true
starbus["source_id"] = push!(
["transformer", starbus["bus_i"], starbus["name"]],
transformer["I"],
transformer["J"],
transformer["K"],
transformer["CKT"],
)
return starbus
end
"Imports remaining top level component lists from `data_in` into `data_out`, excluding keys in `exclude`"
function _import_remaining_comps!(data_out::Dict, data_in::Dict; exclude = [])
for (comp_class, v) in data_in
if !(comp_class in exclude)
comps_out = Dict{String, Any}()
if isa(v, Array)
for (n, item) in enumerate(v)
if isa(item, Dict)
comp_out = Dict{String, Any}()
_import_remaining_keys!(comp_out, item)
if !("index" in keys(item))
comp_out["index"] = n
end
comps_out["$(n)"] = comp_out
else
@error("psse data parsing error, please post an issue")
end
end
elseif isa(v, Dict)
comps_out = Dict{String, Any}()
_import_remaining_keys!(comps_out, v)
else
@error("psse data parsing error, please post an issue")
end
data_out[lowercase(comp_class)] = comps_out
end
end
end
"Imports remaining keys from a source component into detestation component, excluding keys in `exclude`"
function _import_remaining_keys!(comp_dest::Dict, comp_src::Dict; exclude = [])
for (k, v) in comp_src
if !(k in exclude)
key = lowercase(k)
if !haskey(comp_dest, key)
comp_dest[key] = v
else
if key != "index"
@warn("duplicate key $(key), please post an issue")
end
end
end
end
end
"""
_psse2pm_branch!(pm_data, pti_data)
Parses PSS(R)E-style Branch data into a PowerModels-style Dict. "source_id" is
given by `["I", "J", "CKT"]` in PSS(R)E Branch specification.
"""
function _psse2pm_branch!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Branch data into a PowerModels Dict..."
pm_data["branch"] = []
if haskey(pti_data, "BRANCH")
for branch in pti_data["BRANCH"]
if !haskey(branch, "I") || !haskey(branch, "J")
@error "Bus Data Incomplete for $(branch). Skipping branch creation"
continue
end
if first(branch["CKT"]) != '@' && first(branch["CKT"]) != '*'
sub_data = Dict{String, Any}()
sub_data["f_bus"] = pop!(branch, "I")
sub_data["t_bus"] = pop!(branch, "J")
bus_from = pm_data["bus"][sub_data["f_bus"]]
sub_data["base_voltage_from"] = bus_from["base_kv"]
bus_to = pm_data["bus"][sub_data["t_bus"]]
sub_data["base_voltage_to"] = bus_to["base_kv"]
if pm_data["has_isolated_type_buses"]
if !(bus_from["bus_type"] == 4 || bus_to["bus_type"] == 4)
push!(pm_data["connected_buses"], sub_data["f_bus"])
push!(pm_data["connected_buses"], sub_data["t_bus"])
end
end
sub_data["br_r"] = pop!(branch, "R")
sub_data["br_x"] = pop!(branch, "X")
sub_data["g_fr"] = pop!(branch, "GI")
# Evenly split the susceptance between the `from` and `to` ends
sub_data["b_fr"] = (branch["B"] / 2) + pop!(branch, "BI")
sub_data["g_to"] = pop!(branch, "GJ")
sub_data["b_to"] = (branch["B"] / 2) + pop!(branch, "BJ")
sub_data["ext"] = Dict{String, Any}(
"LEN" => pop!(branch, "LEN"),
)
if pm_data["source_version"] ∈ ("32", "33")
sub_data["rate_a"] = pop!(branch, "RATEA")
sub_data["rate_b"] = pop!(branch, "RATEB")
sub_data["rate_c"] = pop!(branch, "RATEC")
elseif pm_data["source_version"] == "35"
sub_data["rate_a"] = pop!(branch, "RATE1")
sub_data["rate_b"] = pop!(branch, "RATE2")
sub_data["rate_c"] = pop!(branch, "RATE3")
for i in 4:12
rate_key = "RATE$i"
if haskey(branch, rate_key)
sub_data["ext"][rate_key] = pop!(branch, rate_key)
end
end
else
error(
"Unsupported PSS(R)E source version: $(pm_data["source_version"])",
)
end
sub_data["tap"] = 1.0
sub_data["shift"] = 0.0
sub_data["br_status"] = pop!(branch, "ST")
sub_data["angmin"] = 0.0
sub_data["angmax"] = 0.0
sub_data["transformer"] = false
sub_data["source_id"] =
["branch", sub_data["f_bus"], sub_data["t_bus"], pop!(branch, "CKT")]
sub_data["index"] = length(pm_data["branch"]) + 1
if import_all
_import_remaining_keys!(sub_data, branch; exclude = ["B", "BI", "BJ"])
end
if sub_data["rate_a"] == 0.0
delete!(sub_data, "rate_a")
end
if sub_data["rate_b"] == 0.0
delete!(sub_data, "rate_b")
end
if sub_data["rate_c"] == 0.0
delete!(sub_data, "rate_c")
end
branch_isolated_bus_modifications!(pm_data, sub_data)
push!(pm_data["branch"], sub_data)
else
from_bus = branch["I"]
to_bus = branch["J"]
ckt = branch["CKT"]
@info "Branch $from_bus -> $to_bus with CKT=$ckt will be parsed as DiscreteControlledACBranch"
end
end
end
return
end
function branch_isolated_bus_modifications!(pm_data::Dict, branch_data::Dict)
bus_data = pm_data["bus"]
from_bus_no = branch_data["f_bus"]
to_bus_no = branch_data["t_bus"]
from_bus = bus_data[from_bus_no]
to_bus = bus_data[to_bus_no]
status_field = haskey(branch_data, "br_status") ? "br_status" : "state"
if (from_bus["bus_type"] == 4 || to_bus["bus_type"] == 4) &&
branch_data[status_field] == 1
@warn "Branch connected between buses $(from_bus_no) -> $(to_bus_no) is connected to an isolated bus. Setting branch status to 0."
branch_data[status_field] = 0
end
if from_bus["bus_type"] == 4
push!(pm_data["candidate_isolated_to_pq_buses"], from_bus_no)
end
if to_bus["bus_type"] == 4
push!(pm_data["candidate_isolated_to_pq_buses"], to_bus_no)
end
return
end
function transformer3W_isolated_bus_modifications!(pm_data::Dict, branch_data::Dict)
bus_data = pm_data["bus"]
primary_bus_number = branch_data["bus_primary"]
secondary_bus_number = branch_data["bus_secondary"]
tertiary_bus_number = branch_data["bus_tertiary"]
primary_bus = bus_data[primary_bus_number]
secondary_bus = bus_data[secondary_bus_number]
tertiary_bus = bus_data[tertiary_bus_number]
if branch_data["available"] == 1
if primary_bus["bus_type"] == 4
branch_data["available_primary"] = 0
@warn "Three winding transformer primary bus $(primary_bus_number) is isolated. Setting primary winding status to 0."
end
if secondary_bus["bus_type"] == 4
branch_data["available_secondary"] = 0
@warn "Three winding transformer secondary bus $(secondary_bus_number) is isolated. Setting secondary winding status to 0."
end
if tertiary_bus["bus_type"] == 4
branch_data["available_tertiary"] = 0
@warn "Three winding transformer tertiary bus $(tertiary_bus_number) is isolated. Setting tertiary winding status to 0."
end
if (
branch_data["available_primary"] == 0 &&
branch_data["available_secondary"] == 0 &&
branch_data["available_tertiary"] == 0
)
branch_data["available"] = 0
@warn "All three windings are unavailable. Setting overall transformer availability to 0"
end
end
if primary_bus["bus_type"] == 4
push!(pm_data["candidate_isolated_to_pq_buses"], primary_bus_number)
end
if secondary_bus["bus_type"] == 4
push!(pm_data["candidate_isolated_to_pq_buses"], secondary_bus_number)
end
if tertiary_bus["bus_type"] == 4
push!(pm_data["candidate_isolated_to_pq_buses"], tertiary_bus_number)
end
return
end
"""
_is_synch_condenser(sub_data, pm_data)
Returns `true` if the generator described by `sub_data` and `pm_data` meets the criteria for a synchronous condenser.
"""
function _is_synch_condenser(sub_data::Dict{String, Any}, pm_data::Dict{String, Any})
is_zero_pg = sub_data["pg"] == 0.0
has_q_limits = (sub_data["qmax"] != 0.0 || sub_data["qmin"] != 0.0)
has_zero_p_limits = (sub_data["pmax"] == 0.0 && sub_data["pmin"] == 0.0)
zero_control_mode = sub_data["m_control_mode"] == 0
is_pv_bus = pm_data["bus"][sub_data["gen_bus"]]["bus_type"] == 2
if is_zero_pg && has_q_limits && has_zero_p_limits && zero_control_mode
if !is_pv_bus
@warn "Generator $(sub_data["gen_bus"]) is likely a synchronous condenser but not connected to a PV bus."
end
return true
end
return false
end
function _determine_injector_status(
sub_data::Dict{String, Any},
pm_data::Dict{String, Any},
gen_bus::Int,
status_key::String,
bus_conversion_list::String,
)
# Special case for FACTS: MODE = 0 -> Unavailable, MODE = 1 -> Normal mode, MODE = 2 -> Link bypassed
if status_key == "MODE"
device_status = pop!(sub_data, status_key) != 0 ? true : false
else
device_status = pop!(sub_data, status_key) == 1 ? true : false
end
# If device is off keep it off.
if !device_status
return false
end
# If device is on check the topology and status of the bus it is connected to.
if pm_data["bus"][gen_bus]["bus_type"] == 4
gen_bus_connected = gen_bus ∈ pm_data["connected_buses"]
if gen_bus_connected && device_status
@warn "Device connected to bus $(gen_bus) is marked as available, but the bus is set isolated and not topologically isolated. Setting device status to 1 and the bus added to candidate for conversion."
push!(pm_data[bus_conversion_list], gen_bus)
pm_data["bus"][gen_bus]["bus_status"] = true
return true
elseif !gen_bus_connected && device_status
@warn "Device connected to bus $(gen_bus) is marked as available, but the bus is set isolated. Setting device status to 0."
pm_data["bus"][gen_bus]["bus_status"] = false
return false
else
error("Unrecognized generator and bus status combination.")
end
else
sub_data["gen_status"] = true
return true
end
end
"""
_psse2pm_generator!(pm_data, pti_data)
Parses PSS(R)E-style Generator data in a PowerModels-style Dict. "source_id" is
given by `["I", "ID"]` in PSS(R)E Generator specification.
"""
function _psse2pm_generator!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Generator data into a PowerModels Dict..."
if haskey(pti_data, "GENERATOR")
pm_data["gen"] = Vector{Dict{String, Any}}(undef, length(pti_data["GENERATOR"]))
for (ix, gen) in enumerate(pti_data["GENERATOR"])
sub_data = Dict{String, Any}()
sub_data["gen_bus"] = pop!(gen, "I")
sub_data["gen_status"] =
_determine_injector_status(
gen,
pm_data,
sub_data["gen_bus"],
"STAT",
"candidate_isolated_to_pv_buses",
)
sub_data["pg"] = pop!(gen, "PG")
sub_data["qg"] = pop!(gen, "QG")
sub_data["vg"] = pop!(gen, "VS")
sub_data["mbase"] = pop!(gen, "MBASE")
sub_data["pmin"] = pop!(gen, "PB")
sub_data["pmax"] = pop!(gen, "PT")
sub_data["qmin"] = pop!(gen, "QB")
sub_data["qmax"] = pop!(gen, "QT")
sub_data["rt_source"] = pop!(gen, "RT")
sub_data["xt_source"] = pop!(gen, "XT")
sub_data["r_source"] = pop!(gen, "ZR")
sub_data["x_source"] = pop!(gen, "ZX")
sub_data["m_control_mode"] = pop!(gen, "WMOD")
if _is_synch_condenser(sub_data, pm_data)
sub_data["fuel"] = "SYNC_COND"
sub_data["type"] = "SYNC_COND"
end
if pm_data["source_version"] == "35"
sub_data["ext"] = Dict{String, Any}(
"NREG" => pop!(gen, "NREG"),
"BASLOD" => pop!(gen, "BASLOD"),
)
elseif pm_data["source_version"] ∈ ("32", "33")
sub_data["ext"] = Dict{String, Any}(
"IREG" => pop!(gen, "IREG"),
"WPF" => pop!(gen, "WPF"),
"WMOD" => sub_data["m_control_mode"],
"GTAP" => pop!(gen, "GTAP"),
"RMPCT" => pop!(gen, "RMPCT"),
)
else
error("Unsupported PSS(R)E source version: $(pm_data["source_version"])")
end
# Default Cost functions
sub_data["model"] = 2
sub_data["startup"] = 0.0
sub_data["shutdown"] = 0.0
sub_data["ncost"] = 2
sub_data["cost"] = [1.0, 0.0]
sub_data["source_id"] =
["generator", string(sub_data["gen_bus"]), pop!(gen, "ID")]
sub_data["index"] = ix
if import_all
_import_remaining_keys!(sub_data, gen)
end
pm_data["gen"][ix] = sub_data
end
else
pm_data["gen"] = Vector{Dict{String, Any}}()
end
end
function _psse2pm_area_interchange!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E AreaInterchange data into a PowerModels Dict..."
pm_data["area_interchange"] = []
if haskey(pti_data, "AREA INTERCHANGE")
for area_int in pti_data["AREA INTERCHANGE"]
sub_data = Dict{String, Any}()
sub_data["area_name"] = pop!(area_int, "ARNAME")
sub_data["area_number"] = pop!(area_int, "I")
sub_data["bus_number"] = pop!(area_int, "ISW")
sub_data["net_interchange"] = pop!(area_int, "PDES")
sub_data["tol_interchange"] = pop!(area_int, "PTOL")
sub_data["index"] = length(pm_data["area_interchange"]) + 1
if import_all
_import_remaining_keys!(sub_data, area_int)
end
push!(pm_data["area_interchange"], sub_data)
end
end
end
function _psse2pm_interarea_transfer!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E InterAreaTransfer data into a PowerModels Dict..."
pm_data["interarea_transfer"] = []
if haskey(pti_data, "INTER-AREA TRANSFER")
for interarea in pti_data["INTER-AREA TRANSFER"]
sub_data = Dict{String, Any}()
sub_data["area_from"] = pop!(interarea, "ARFROM")
sub_data["area_to"] = pop!(interarea, "ARTO")
sub_data["transfer_id"] = pop!(interarea, "TRID")
sub_data["power_transfer"] = pop!(interarea, "PTRAN")
sub_data["index"] = length(pm_data["interarea_transfer"]) + 1
if import_all
_import_remaining_keys!(sub_data, interarea)
end
push!(pm_data["interarea_transfer"], sub_data)
end
end
end
function _psse2pm_zone!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Zone data into a PowerModels Dict..."
pm_data["zone"] = []
if haskey(pti_data, "ZONE")
for zone in pti_data["ZONE"]
sub_data = Dict{String, Any}()
sub_data["zone_number"] = pop!(zone, "I")
sub_data["zone_name"] = pop!(zone, "ZONAME")
sub_data["index"] = length(pm_data["zone"]) + 1
if import_all
_import_remaining_keys!(sub_data, zone)
end
push!(pm_data["zone"], sub_data)
end
end
end
"""
_psse2pm_bus!(pm_data, pti_data)
Parses PSS(R)E-style Bus data into a PowerModels-style Dict. "source_id" is given
by ["I", "NAME"] in PSS(R)E Bus specification.
"""
function _psse2pm_bus!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Bus data into a PowerModels Dict..."
pm_data["has_isolated_type_buses"] = false
pm_data["bus"] = Dict{Int, Any}()
if haskey(pti_data, "BUS")
for bus in pti_data["BUS"]
sub_data = Dict{String, Any}()
sub_data["bus_i"] = bus["I"]
sub_data["bus_type"] = pop!(bus, "IDE")
if sub_data["bus_type"] == 4
@warn "The PSS(R)E data contains buses designated as isolated. The parser will check if the buses are connected or topologically isolated."
pm_data["has_isolated_type_buses"] = true
sub_data["bus_status"] = false
pm_data["connected_buses"] = Set{Int}()
pm_data["candidate_isolated_to_pq_buses"] = Set{Int}()
pm_data["candidate_isolated_to_pv_buses"] = Set{Int}()
else
sub_data["bus_status"] = true
end
sub_data["area"] = pop!(bus, "AREA")
sub_data["vm"] = pop!(bus, "VM")
sub_data["va"] = pop!(bus, "VA")
sub_data["base_kv"] = pop!(bus, "BASKV")
sub_data["zone"] = pop!(bus, "ZONE")
sub_data["name"] = pop!(bus, "NAME")
sub_data["vmax"] = pop!(bus, "NVHI")
sub_data["vmin"] = pop!(bus, "NVLO")
sub_data["hidden"] = false
sub_data["source_id"] = ["bus", "$(bus["I"])"]
sub_data["index"] = pop!(bus, "I")
if import_all
_import_remaining_keys!(sub_data, bus)
end
if haskey(pm_data["bus"], sub_data["bus_i"])
error("Repeated $(sub_data["bus_i"])")
end
pm_data["bus"][sub_data["bus_i"]] = sub_data
end
end
return
end
"""
_psse2pm_load!(pm_data, pti_data)
Parses PSS(R)E-style Load data into a PowerModels-style Dict. "source_id" is given
by `["I", "ID"]` in the PSS(R)E Load specification.
"""
function _psse2pm_load!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Load data into a PowerModels Dict..."
pm_data["load"] = []
if haskey(pti_data, "LOAD")
for load in pti_data["LOAD"]
sub_data = Dict{String, Any}()
sub_data["load_bus"] = pop!(load, "I")
dgenp = 0.0
dgenq = 0.0
dgenm = 0.0
if pm_data["source_version"] == "35"
dgenp = pop!(load, "DGENP", 0.0)
dgenq = pop!(load, "DGENQ", 0.0)
dgenm = pop!(load, "DGENM", 0.0)
end
# PSS(R)E models distributed generation as negative demand on the load record.
# Net active/reactive demand seen by PF should be gross load minus DGEN.
sub_data["pd"] = pop!(load, "PL") - dgenp
sub_data["qd"] = pop!(load, "QL") - dgenq
sub_data["pi"] = pop!(load, "IP")
sub_data["qi"] = pop!(load, "IQ")
sub_data["py"] = pop!(load, "YP")
# Reactive power component of constant Y load.
# Positive for an inductive load (consumes Q)
# Negative for a capacitive load (injects Q)
sub_data["qy"] = -pop!(load, "YQ")
sub_data["conformity"] = pop!(load, "SCALE")
sub_data["source_id"] = ["load", sub_data["load_bus"], pop!(load, "ID")]
sub_data["interruptible"] = pop!(load, "INTRPT")
sub_data["ext"] = Dict{String, Any}()
if pm_data["source_version"] ∈ ("32", "33")
sub_data["ext"]["LOADTYPE"] = ""
elseif pm_data["source_version"] == "35"
sub_data["ext"]["LOADTYPE"] = pop!(load, "LOADTYPE", "")
sub_data["ext"]["DGENP"] = dgenp
sub_data["ext"]["DGENQ"] = dgenq
sub_data["ext"]["DGENM"] = dgenm
else
error("Unsupported PSS(R)E source version: $(pm_data["source_version"])")
end
sub_data["status"] =
_determine_injector_status(
load,
pm_data,
sub_data["load_bus"],
"STATUS",
"candidate_isolated_to_pq_buses",
)
sub_data["index"] = length(pm_data["load"]) + 1
if import_all
_import_remaining_keys!(sub_data, load)
end
push!(pm_data["load"], sub_data)
end
end
end
"""
_psse2pm_shunt!(pm_data, pti_data)
Parses PSS(R)E-style Fixed and Switched Shunt data into a PowerModels-style
Dict. "source_id" is given by `["I", "ID"]` for Fixed Shunts, and `["I", "SWREM"]`
for Switched Shunts, as given by the PSS(R)E Fixed and Switched Shunts
specifications.
"""
function _psse2pm_shunt!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Fixed & Switched Shunt data into a PowerModels Dict..."
pm_data["shunt"] = []
if haskey(pti_data, "FIXED SHUNT")
for shunt in pti_data["FIXED SHUNT"]
sub_data = Dict{String, Any}()
sub_data["shunt_bus"] = pop!(shunt, "I")
sub_data["gs"] = pop!(shunt, "GL")
sub_data["bs"] = pop!(shunt, "BL")
sub_data["status"] = _determine_injector_status(
shunt,
pm_data,
sub_data["shunt_bus"],
"STATUS",
"candidate_isolated_to_pq_buses",
)
sub_data["source_id"] =
["fixed shunt", sub_data["shunt_bus"], pop!(shunt, "ID")]
sub_data["index"] = length(pm_data["shunt"]) + 1
if import_all
_import_remaining_keys!(sub_data, shunt)
end
push!(pm_data["shunt"], sub_data)
end
end
pm_data["switched_shunt"] = []
if haskey(pti_data, "SWITCHED SHUNT")
for switched_shunt in pti_data["SWITCHED SHUNT"]
sub_data = Dict{String, Any}()
sub_data["shunt_bus"] = pop!(switched_shunt, "I")
sub_data["gs"] = 0.0
sub_data["bs"] = pop!(switched_shunt, "BINIT")
sub_data["status"] = _determine_injector_status(
switched_shunt,
pm_data,
sub_data["shunt_bus"],
"STAT",
"candidate_isolated_to_pq_buses",
)
sub_data["admittance_limits"] =
(pop!(switched_shunt, "VSWLO"), pop!(switched_shunt, "VSWHI"))
step_numbers = Dict(
k => v for
(k, v) in switched_shunt if startswith(k, "N") && isdigit(last(k))
)
step_numbers_sorted =
sort(collect(keys(step_numbers)); by = x -> parse(Int, x[2:end]))
sub_data["step_number"] = [step_numbers[k] for k in step_numbers_sorted]
sub_data["step_number"] = sub_data["step_number"][sub_data["step_number"] .!= 0]
modsw = switched_shunt["MODSW"]
sub_data["ext"] = Dict{String, Any}(
"MODSW" => modsw,
"ADJM" => switched_shunt["ADJM"],
"RMPCT" => switched_shunt["RMPCT"],
"RMIDNT" => switched_shunt["RMIDNT"],
)
y_increment = Dict(
k => v for
(k, v) in switched_shunt if startswith(k, "B") && isdigit(last(k))
)
y_increment_sorted =
sort(collect(keys(y_increment)); by = x -> parse(Int, x[2:end]))
sub_data["y_increment"] = [y_increment[k] for k in y_increment_sorted]im
sub_data["y_increment"] = sub_data["y_increment"][sub_data["y_increment"] .!= 0]
if pm_data["source_version"] == "35"
sub_data["sw_id"] = pop!(switched_shunt, "ID")
initial_ss_status = Dict(
k => v for
(k, v) in switched_shunt if startswith(k, "S") && isdigit(last(k))
)
initial_ss_status_sorted =
sort(collect(keys(initial_ss_status)); by = x -> parse(Int, x[2:end]))
sub_data["initial_status"] =
[initial_ss_status[k] for k in initial_ss_status_sorted]
sub_data["initial_status"] =
sub_data["initial_status"][1:length(sub_data["step_number"])]
sub_data["ext"]["NREG"] = pop!(switched_shunt, "NREG")
elseif pm_data["source_version"] ∈ ("32", "33")
sub_data["ext"]["SWREM"] = switched_shunt["SWREM"]
sub_data["initial_status"] = ones(Int, length(sub_data["y_increment"]))
else
error("Unsupported PSS(R)E source version: $(pm_data["source_version"])")
end
if modsw ∈ (0, 1, 2)
# For fixed/discrete/continuous modes used for PF comparison,
# BINIT is treated as the total shunt admittance.
# Keep Y_increase but zero all initial states to avoid double counting.
sub_data["initial_status"] = zeros(Int, length(sub_data["y_increment"]))
end
sub_data["index"] = length(pm_data["switched_shunt"]) + 1
sub_data["source_id"] =
["switched shunt", sub_data["shunt_bus"], sub_data["index"]]
if import_all
_import_remaining_keys!(sub_data, switched_shunt)
end
push!(pm_data["switched_shunt"], sub_data)
end
end
end
function apply_tap_correction!(
windv_value::Float64,
transformer::Dict{String, Any},
cod_key::String,
rmi_key::String,
rma_key::String,
ntp_key::String,
cw_value::Int64,
winding_name::String,
)
if abs(transformer[cod_key]) ∈ [1, 2] && cw_value ∈ [1, 2, 3]
tap_positions = collect(
range(
transformer[rmi_key],
transformer[rma_key];
length = Int(transformer[ntp_key]),
),
)
closest_tap_ix = argmin(abs.(tap_positions .- windv_value))
if !isapprox(
windv_value,
tap_positions[closest_tap_ix];
atol = PARSER_TAP_RATIO_CORRECTION_TOL,
)
@warn "Transformer $winding_name winding tap setting is not on a step; $windv_value set to $(tap_positions[closest_tap_ix])"
return tap_positions[closest_tap_ix]
end
end
return windv_value
end
# Base Power has a different key in sub_data depending on the number of windings
function _transformer_mag_pu_conversion(
transformer::Dict,
sub_data::Dict,
base_power::Float64,
)
if isapprox(transformer["MAG1"], ZERO_IMPEDANCE_REACTANCE_THRESHOLD) &&
isapprox(transformer["MAG2"], ZERO_IMPEDANCE_REACTANCE_THRESHOLD)
@warn "Transformer $(sub_data["f_bus"]) -> $(sub_data["t_bus"]) has zero MAG1 and MAG2 values."
return 0.0, 0.0
else
G_pu = 1e-6 * transformer["MAG1"] / base_power
mag_diff = transformer["MAG2"]^2 - G_pu^2
@assert mag_diff >= -ZERO_IMPEDANCE_REACTANCE_THRESHOLD
B_pu = sqrt(max(0.0, mag_diff))
return G_pu, B_pu
end
end
"""
_psse2pm_transformer!(pm_data, pti_data)
Parses PSS(R)E-style Transformer data into a PowerModels-style Dict. "source_id"
is given by `["I", "J", "K", "CKT", "winding"]`, where "winding" is 0 if
transformer is two-winding, and 1, 2, or 3 for three-winding, and the remaining
keys are defined in the PSS(R)E Transformer specification.
"""
function _psse2pm_transformer!(pm_data::Dict, pti_data::Dict, import_all::Bool)
@info "Parsing PSS(R)E Transformer data into a PowerModels Dict..."
if !haskey(pm_data, "branch")
pm_data["branch"] = []
end
if haskey(pti_data, "TRANSFORMER")
starbus_id = 10^ceil(Int, log10(abs(_find_max_bus_id(pm_data)))) + 1
for transformer in pti_data["TRANSFORMER"]
if !(transformer["CZ"] in [1, 2, 3])
@warn(
"transformer CZ value outside of valid bounds assuming the default value of 1. Given $(transformer["CZ"]), should be 1, 2 or 3",
)
transformer["CZ"] = 1
end
if !(transformer["CW"] in [1, 2, 3])
@warn(
"transformer CW value outside of valid bounds assuming the default value of 1. Given $(transformer["CW"]), should be 1, 2 or 3",
)
transformer["CW"] = 1
end
if !(transformer["CM"] in [1, 2])
@warn(
"transformer CM value outside of valid bounds assuming the default value of 1. Given $(transformer["CM"]), should be 1 or 2",
)
transformer["CM"] = 1
end
if transformer["K"] == 0 # Two-winding Transformers
sub_data = Dict{String, Any}()
sub_data["f_bus"] = transformer["I"]
sub_data["t_bus"] = transformer["J"]
if pm_data["has_isolated_type_buses"]
bus_from = pm_data["bus"][sub_data["f_bus"]]
bus_to = pm_data["bus"][sub_data["t_bus"]]
if !(bus_from["bus_type"] == 4 || bus_to["bus_type"] == 4)
push!(pm_data["connected_buses"], sub_data["f_bus"])
push!(pm_data["connected_buses"], sub_data["t_bus"])
end
end
# Store base_power
if transformer["SBASE1-2"] < 0.0
throw(
IS.InvalidValue(
"Transformer $(sub_data["f_bus"]) -> $(sub_data["t_bus"]) has non-positive base power SBASE1-2: $(transformer["SBASE1-2"])",
),
)
end
if iszero(transformer["SBASE1-2"])
sub_data["base_power"] = pm_data["baseMVA"]
else
sub_data["base_power"] = transformer["SBASE1-2"]
end
if iszero(transformer["NOMV1"])
sub_data["base_voltage_from"] =
_get_bus_value(transformer["I"], "base_kv", pm_data)
else
sub_data["base_voltage_from"] = transformer["NOMV1"]
end
if iszero(transformer["NOMV2"])
sub_data["base_voltage_to"] =
_get_bus_value(transformer["J"], "base_kv", pm_data)
else
sub_data["base_voltage_to"] = transformer["NOMV2"]
end
# Unit Transformations
# Data must be stored in the DEVICE_BASE
# Z_base_device = (V_device)^2 / S_device, Z_base_sys = (V_device)^2 / S_sys
# Z_ohms = Z_pu_sys * Z_base_sys, Z_pu_device = Z_ohms / Z_device = Z_pu_sys * S_device / S_sys
mva_ratio = sub_data["base_power"] / pm_data["baseMVA"]
Z_base_device = sub_data["base_voltage_from"]^2 / sub_data["base_power"]
Z_base_sys = sub_data["base_voltage_from"]^2 / pm_data["baseMVA"]
#_get_bus_value(transformer["I"], "base_kv", pm_data)^2 /
#pm_data["baseMVA"]
if transformer["CZ"] == 2 # "for resistance and reactance in pu on system MVA base and winding voltage base"
# Compute br_r and br_x in pu of device base
br_r, br_x = transformer["R1-2"], transformer["X1-2"]
else # NOT "for resistance and reactance in pu on system MVA base and winding voltage base"
if transformer["CZ"] == 3 # "for transformer load loss in watts and impedance magnitude in pu on a specified MVA base and winding voltage base."
br_r = 1e-6 * transformer["R1-2"] / sub_data["base_power"] # device pu
br_x = sqrt(transformer["X1-2"]^2 - br_r^2) # device pu
else # "CZ" = 1 in system base pu
@assert transformer["CZ"] == 1
br_r, br_x = transformer["R1-2"], transformer["X1-2"] # sys pu
if iszero(Z_base_device) # NOMV1 = 0.0: use the power ratios
br_r = transformer["R1-2"] * mva_ratio
br_x = transformer["X1-2"] * mva_ratio
else # NOMV1 could potentially be different than the bus_voltage, use impedance ratios
br_r = (transformer["R1-2"] * Z_base_sys) / Z_base_device
br_x = (transformer["X1-2"] * Z_base_sys) / Z_base_device
end
end
end
# Zeq scaling for tap2 (see eq (4.21b) in PROGRAM APPLICATION GUIDE 1 in PSSE installation folder)
# Unit Transformations
if transformer["CW"] == 1 # "for off-nominal turns ratio in pu of winding bus base voltage"
br_r *= transformer["WINDV2"]^2
br_x *= transformer["WINDV2"]^2
# NOT "for off-nominal turns ratio in pu of winding bus base voltage"
elseif transformer["CW"] == 2 # "for winding voltage in kV"
br_r *=
(
transformer["WINDV2"] /
_get_bus_value(transformer["J"], "base_kv", pm_data)
)^2
br_x *=
(
transformer["WINDV2"] /
_get_bus_value(transformer["J"], "base_kv", pm_data)
)^2
elseif transformer["CW"] == 3 # "for off-nominal turns ratio in pu of nominal winding voltage, NOMV1, NOMV2 and NOMV3."
#The nominal (rated) Winding 2 voltage base in kV, or zero to indicate
# that nominal Winding 2 voltage is assumed to be identical to the base
# voltage of bus J. NOMV2 is used in converting tap ratio data between values
# in per unit of nominal Winding 2 voltage and values in per unit of Winding 2
#bus base voltage when CW is 3. NOMV2 = 0.0 by default.
if iszero(transformer["NOMV2"])
nominal_voltage_ratio = 1.0
else
nominal_voltage_ratio =
transformer["NOMV2"] /
_get_bus_value(transformer["J"], "base_kv", pm_data)
end
br_r *= (transformer["WINDV2"] * (nominal_voltage_ratio))^2
br_x *= (transformer["WINDV2"] * (nominal_voltage_ratio))^2
else
error("invalid transformer $(transformer["CW"])")
end
if transformer["X1-2"] < 0.0 && br_x < 0.0
@warn "Transformer $(sub_data["f_bus"]) -> $(sub_data["t_bus"]) has negative impedance values X1-2: $(transformer["X1-2"]), br_x: $(br_x)"
end
sub_data["br_r"] = br_r
sub_data["br_x"] = br_x
if transformer["CM"] == 1
# Transform admittance to device per unit
mva_ratio_12 = sub_data["base_power"] / pm_data["baseMVA"]
sub_data["g_fr"] = transformer["MAG1"] / mva_ratio_12
sub_data["b_fr"] = transformer["MAG2"] / mva_ratio_12
else # CM=2: MAG1 are no load loss in Watts and MAG2 is the exciting current in pu, in device base.
@assert transformer["CM"] == 2
G_pu, B_pu = _transformer_mag_pu_conversion(
transformer,
sub_data,
sub_data["base_power"],
)
sub_data["g_fr"] = G_pu
sub_data["b_fr"] = B_pu
end
sub_data["g_to"] = 0.0
sub_data["b_to"] = 0.0
sub_data["ext"] = Dict{String, Any}(
"psse_name" => transformer["NAME"],
"CW" => transformer["CW"],
"CZ" => transformer["CZ"],
"CM" => transformer["CM"],
"COD1" => transformer["COD1"],
"CONT1" => transformer["CONT1"],
"NOMV1" => transformer["NOMV1"],
"NOMV2" => transformer["NOMV2"],
"WINDV1" => transformer["WINDV1"],
"WINDV2" => transformer["WINDV2"],
"SBASE1-2" => transformer["SBASE1-2"],
"RMI1" => transformer["RMI1"],
"RMA1" => transformer["RMA1"],
"NTP1" => transformer["NTP1"],
"R1-2" => transformer["R1-2"],
"X1-2" => transformer["X1-2"],
"MAG1" => transformer["MAG1"],
"MAG2" => transformer["MAG2"],
)