/
utils.jl
903 lines (748 loc) · 30.8 KB
/
utils.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
"Helper to convert "
_missing2false(a::Union{Missing,Bool}) = ismissing(a) ? false : a
"""
iseng(data::Dict{String,Any})
Helper function to check is data is ENGINEERING model
"""
iseng(data::Dict{String,<:Any}) = _missing2false(get(data, "data_model", missing) == ENGINEERING)
iseng(data::InfrastructureModel) = false
"""
ismath(data::Dict{String,Any})
Helper function to check if data is MATHEMATICAL model
"""
ismath(data::Dict{String,<:Any}) = _missing2false(get(data, "data_model", missing) == MATHEMATICAL)
ismath(data::InfrastructureModel) = false
"""
make_multiconductor!(data::Dict{String,<:Any}, conductors::Int)
**This function is not meant to be an officially supported method for creating reasonable multiconductor data sets.**
**Hacky** helper function to transform single-conductor network data, from, e.g., matpower/psse, into multi-conductor data.
"""
function make_multiconductor!(data::Dict{String,<:Any}, conductors::Int)
@info "This function is not meant to be an officially supported method for creating reasonable multiconductor data sets"
if ismultinetwork(data)
for (i,nw_data) in data["nw"]
_make_multiconductor!(nw_data, conductors)
end
else
_make_multiconductor!(data, conductors)
end
end
"Hacky helper function to transform single-conductor network data, from, e.g., matpower/psse, into multi-conductor data"
function _make_multiconductor!(data::Dict{String,<:Any}, conductors::Real)
if haskey(data, "conductor_ids")
@warn "skipping network that is already multiconductor"
return
end
data["conductor_ids"] = collect(1:conductors)
data["data_model"] = MATHEMATICAL
data["settings"] = get(
data,
"settings",
Dict{String,Any}(
"sbase_default" => get(data, "baseMVA", 1e6)
)
)
for (key, item) in data
if isa(item, Dict{String,Any})
for (item_id, item_data) in item
if isa(item_data, Dict{String,Any})
item_ref_data = Dict{String,Any}()
for (param, value) in item_data
if param in _conductorless
item_ref_data[param] = value
else
if param in _conductor_matrix
item_ref_data[param] = LinearAlgebra.diagm(0=>fill(value, conductors))
else
item_ref_data[param] = fill(value, conductors)
end
end
end
item[item_id] = item_ref_data
end
end
else
#root non-dict items
end
end
for (_, load) in data["load"]
load["model"] = POWER
load["configuration"] = WYE
end
for (_, gen) in data["gen"]
gen["configuration"] = WYE
end
for type in ["load", "gen", "storage", "shunt"]
if haskey(data, type)
for (_,obj) in data[type]
obj["connections"] = collect(1:conductors)
end
end
end
for type in ["branch", "transformer", "switch"]
if haskey(data, type)
for (_,obj) in data[type]
obj["f_connections"] = collect(1:conductors)
obj["t_connections"] = collect(1:conductors)
end
end
end
for (_,bus) in data["bus"]
bus["terminals"] = collect(1:conductors)
bus["grounded"] = fill(false, conductors)
end
end
"initializes the base math object of any type, and copies any one-to-one mappings"
function _init_math_obj(obj_type::String, eng_id::Any, eng_obj::Dict{String,<:Any}, index::Int; pass_props::Vector{String}=String[])::Dict{String,Any}
math_obj = Dict{String,Any}(
"name" => "$eng_id",
"source_id" => "$obj_type.$eng_id"
)
for key in [get(_1to1_maps, obj_type, String[]); pass_props]
if haskey(eng_obj, key)
if key in ["status", "dispatchable"]
math_obj[key] = Int(eng_obj[key])
else
math_obj[key] = eng_obj[key]
end
end
end
math_obj["index"] = index
return math_obj
end
"initializes the base components that are expected by powermodelsdistribution in the mathematical model"
function _init_base_components!(data_math::Dict{String,<:Any})
for key in pmd_math_asset_types
if !haskey(data_math, key)
data_math[key] = Dict{String,Any}()
end
end
end
"function for applying a scale to a paramter"
function _scale(dict::Dict{String,<:Any}, key::String, scale::Real)
if haskey(dict, key)
dict[key] *= scale
end
end
"""
Converts a set of short-circuit tests to an equivalent reactance network.
Reference:
R. C. Dugan, “A perspective on transformer modeling for distribution system analysis,”
in 2003 IEEE Power Engineering Society General Meeting (IEEE Cat. No.03CH37491), 2003, vol. 1, pp. 114-119 Vol. 1.
"""
function _sc2br_impedance(Zsc::Dict{Tuple{Int,Int},Complex{Float64}})::Dict{Tuple{Int,Int},Complex}
N = maximum([maximum(k) for k in keys(Zsc)])
# check whether no keys are missing
# Zsc should contain tupples for upper triangle of NxN
for i in 1:N
for j in i+1:N
if !haskey(Zsc, (i,j))
if haskey(Zsc, (j,i))
# Zsc is symmetric; use value of lower triangle if defined
Zsc[(i,j)] = Zsc[(j,i)]
else
error("Short-circuit impedance between winding $i and $j is missing.")
end
end
end
end
# if all zero, return all zeros
if all(values(Zsc).==0.0)
return Zsc
end
# make Zb
Zb = zeros(Complex{Float64}, N-1,N-1)
for i in 1:N-1
Zb[i,i] = Zsc[(1,i+1)]
end
for i in 1:N-1
for j in 1:i-1
Zb[i,j] = (Zb[i,i]+Zb[j,j]-Zsc[(j+1,i+1)])/2
Zb[j,i] = Zb[i,j]
end
end
# get Ybus
Y = LinearAlgebra.pinv(Zb)
Y = [-Y*ones(N-1) Y]
Y = [-ones(1,N-1)*Y; Y]
# extract elements
Zbr = Dict{Tuple{Int,Int},Complex}()
for k in keys(Zsc)
Zbr[k] = (abs(Y[k...])==0) ? Inf : -1/Y[k...]
end
return Zbr
end
"loss model builder for transformer decomposition"
function _build_loss_model!(
data_math::Dict{String,<:Any},
transformer_name::String,
to_map::Vector{String},
r_s::Vector{Float64},
zsc::Dict{Tuple{Int,Int},Complex{Float64}},
ysh::Complex{Float64},
connections::Vector{Int};
nphases::Int=3,
status::Int=1,
)::Vector{Int}
# precompute the minimal set of buses and lines
N = length(r_s)
tr_t_bus = collect(1:N)
buses = Set(1:2*N)
zbr = _sc2br_impedance(zsc)
edges = [[[i,i+N] for i in 1:N]..., [[i+N,j+N] for (i,j) in keys(zbr)]...]
lines = Dict(enumerate(edges))
z = Dict(enumerate([r_s..., values(zbr)...]))
shunts = Dict(2=>ysh)
# remove Inf lines
for (l,edge) in lines
if real(z[l])==Inf || imag(z[l])==Inf
delete!(lines, l)
delete!(z, l)
end
end
# merge short circuits
stack = Set(keys(lines))
while !isempty(stack)
l = pop!(stack)
if z[l] == 0
(i,j) = lines[l]
# remove line
delete!(lines, l)
# remove bus j
delete!(buses, j)
# update lines
for (k,(edge)) in lines
if edge[1] == j
edge[1] = i
end
if edge[2] == j
edge[2] = i
end
if edge[1]==edge[2]
delete!(lines, k)
delete!(stack, k)
end
end
# move shunts
if haskey(shunts, j)
if haskey(shunts, i)
shunts[i] += shunts[j]
else
shunts[i] = shunts[j]
end
end
# update transformer buses
for w in 1:N
if tr_t_bus[w] == j
tr_t_bus[w] = i
end
end
end
end
bus_ids = Dict{Int,Int}()
for bus in buses
bus_obj = Dict{String,Any}(
"name" => "_virtual_bus.transformer.$(transformer_name)_$(bus)",
"bus_i" => length(data_math["bus"])+1,
"vmin" => fill(0.0, nphases),
"vmax" => fill(Inf, nphases),
"vm_pair_lb" => Tuple{Any,Any,Real}[],
"vm_pair_ub" => Tuple{Any,Any,Real}[],
"terminals" => connections[collect(1:nphases)],
"grounded" => fill(false, nphases),
"base_kv" => 1.0,
"bus_type" => status == 0 ? 4 : 1,
"source_id" => "transformer.$(transformer_name)",
"index" => length(data_math["bus"])+1,
)
if !get(data_math, "is_kron_reduced", false)
if bus in tr_t_bus
bus_obj["terminals"] = collect(1:nphases+1)
bus_obj["vmin"] = fill(0.0, nphases+1)
bus_obj["vmax"] = fill(Inf, nphases+1)
bus_obj["grounded"] = [fill(false, nphases)..., true]
bus_obj["rg"] = [0.0]
bus_obj["xg"] = [0.0]
else
bus_obj["terminals"] = collect(1:nphases)
bus_obj["vmin"] = fill(0.0, nphases)
bus_obj["vmax"] = fill(Inf, nphases)
end
end
data_math["bus"]["$(bus_obj["index"])"] = bus_obj
bus_ids[bus] = bus_obj["bus_i"]
push!(to_map, "bus.$(bus_obj["index"])")
end
for (l,(i,j)) in lines
# merge the shunts into the shunts of the pi model of the line
g_fr = b_fr = g_to = b_to = 0
if haskey(shunts, i)
g_fr = real(shunts[i])
b_fr = imag(shunts[i])
delete!(shunts, i)
end
if haskey(shunts, j)
g_to = real(shunts[j])
b_to = imag(shunts[j])
delete!(shunts, j)
end
branch_obj = Dict{String,Any}(
"name" => "_virtual_branch.transformer.$(transformer_name)_$(l)",
"source_id" => "_virtual_branch.transformer.$(transformer_name)_$(l)",
"index" => length(data_math["branch"])+1,
"br_status"=>status,
"f_bus"=>bus_ids[i],
"t_bus"=>bus_ids[j],
"f_connections"=>data_math["bus"]["$(bus_ids[i])"]["terminals"][collect(1:nphases)],
"t_connections"=>data_math["bus"]["$(bus_ids[j])"]["terminals"][collect(1:nphases)],
"br_r" => LinearAlgebra.diagm(0=>fill(real(z[l]), nphases)),
"br_x" => LinearAlgebra.diagm(0=>fill(imag(z[l]), nphases)),
"g_fr" => LinearAlgebra.diagm(0=>fill(g_fr, nphases)),
"b_fr" => LinearAlgebra.diagm(0=>fill(b_fr, nphases)),
"g_to" => LinearAlgebra.diagm(0=>fill(g_to, nphases)),
"b_to" => LinearAlgebra.diagm(0=>fill(b_to, nphases)),
"angmin" => fill(-10.0, nphases),
"angmax" => fill( 10.0, nphases),
"c_rating_a" => fill(Inf, nphases),
"shift" => zeros(nphases),
"tap" => ones(nphases),
"switch" => false,
"transformer" => false,
)
data_math["branch"]["$(branch_obj["index"])"] = branch_obj
push!(to_map, "branch.$(branch_obj["index"])")
end
return Vector{Int}([bus_ids[bus] for bus in tr_t_bus])
end
"performs kron reduction on branch"
function _kron_reduce_branch!(object::Dict{String,<:Any}, Zs_keys::Vector{String}, Ys_keys::Vector{String}, terminals::Vector{Int}, neutral::Int)::Vector{Int}
Zs = Vector{Matrix}([object[k] for k in Zs_keys])
Ys = Vector{Matrix}([object[k] for k in Ys_keys])
Zs_kr, Ys_kr, terminals_kr = _kron_reduce_branch(Zs, Ys, terminals, neutral)
for (i,k) in enumerate(Zs_keys)
object[k] = Zs_kr[i]
end
for (i,k) in enumerate(Ys_keys)
object[k] = Ys_kr[i]
end
return _get_idxs(terminals, terminals_kr)
end
"get locations of terminal in connections list"
function _get_ilocs(vec::Vector{<:Any}, loc::Any)::Vector{Int}
return collect(1:length(vec))[vec.==loc]
end
"performs kron reduction on branch - helper function"
function _kron_reduce_branch(Zs::Vector{Matrix}, Ys::Vector{Matrix}, terminals::Vector{Int}, neutral::Int)::Tuple{Vector{Matrix}, Vector{Matrix}, Vector{Int}}
Zs_kr = Vector{Matrix}([deepcopy(Z) for Z in Zs])
Ys_kr = Vector{Matrix}([deepcopy(Y) for Y in Ys])
terminals_kr = deepcopy(terminals)
while neutral in terminals_kr
n = _get_ilocs(terminals_kr, neutral)[1]
P = setdiff(collect(1:length(terminals_kr)), n)
if all(size(Z) == (length(terminals_kr), length(terminals_kr)) for Z in Zs_kr)
Zs_kr = Vector{Matrix}([Z[P,P]-(1/Z[n,n])*Z[P,[n]]*Z[[n],P] for Z in Zs_kr])
Ys_kr = Vector{Matrix}([Y[P,P] for Y in Ys_kr])
end
terminals_kr = terminals_kr[P]
end
return Zs_kr, Ys_kr, terminals_kr
end
"pads properties to have the total number of conductors for the whole system"
function _pad_properties!(object::Dict{String,<:Any}, properties::Vector{String}, connections::Vector{Int}, phases::Vector{Int}; pad_value::Real=0.0)
@assert(all(c in phases for c in connections))
inds = _get_idxs(phases, connections)
for property in properties
if haskey(object, property)
if isa(object[property], Vector)
tmp = fill(pad_value, length(phases))
tmp[inds] = object[property]
object[property] = tmp
elseif isa(object[property], Matrix)
tmp = fill(pad_value, length(phases), length(phases))
tmp[inds, inds] = object[property]
object[property] = tmp
end
end
end
end
"pads properties to have the total number of conductors for the whole system (transformer winding variant)"
function _pad_properties!(object::Dict{String,<:Any}, properties::Vector{String}, wdg::Int, connections::Vector{Int}, phases::Vector{Int}; pad_value::Real=0.0)
@assert(all(c in phases for c in connections))
inds = _get_idxs(phases, connections)
for property in properties
if haskey(object, property)
if isa(object[property][wdg], Vector)
tmp = fill(pad_value, length(phases))
tmp[inds] = object[property][wdg]
object[property][wdg] = tmp
elseif isa(object[property][wdg], Matrix)
tmp = fill(pad_value, length(phases), length(phases))
tmp[inds, inds] = object[property][wdg]
object[property][wdg] = tmp
end
end
end
end
"pads properties to have the total number of conductors for the whole system - delta connection variant"
function _pad_properties_delta!(object::Dict{String,<:Any}, properties::Vector{String}, connections::Vector{Int}, phases::Vector{Int}; invert::Bool=false)
@assert(all(c in phases for c in connections))
@assert(length(connections) in [2, 3], "A delta configuration has to have at least 2 or 3 connections!")
@assert(length(phases)==3, "Padding only possible to a |phases|==3!")
for property in properties
if haskey(object, property)
val = object[property]
val_length = length(connections)==2 ? 1 : length(connections)
@assert(isa(val, Vector) && length(val)==val_length)
# build tmp
tmp = Dict()
sign = invert ? -1 : 1
if val_length==1
tmp[(connections[1], connections[2])] = val[1]
tmp[(connections[2], connections[1])] = sign*val[1]
else
tmp[(connections[1], connections[2])] = val[1]
tmp[(connections[2], connections[3])] = val[2]
tmp[(connections[3], connections[1])] = val[3]
end
merge!(tmp, Dict((k[2], k[1])=>sign*v for (k,v) in tmp))
get_val(x,y) = haskey(tmp, (x,y)) ? tmp[(x,y)] : 0.0
object[property] = val_length==1 ? [get_val(connections[1], connections[2]), 0.0,0.0] : [get_val(phases[1], phases[2]), get_val(phases[2], phases[3]), get_val(phases[3], phases[1])]
end
end
end
"pads properties to have the total number of conductors for the whole system - delta connection variant"
function _pad_properties_delta!(object::Dict{String,<:Any}, properties::Vector{String}, connections::Vector{Int}, wdg::Int, phases::Vector{Int}; invert::Bool=false)
@assert(all(c in phases for c in connections))
@assert(length(connections) in [2, 3], "A delta configuration has to have at least 2 or 3 connections!")
@assert(length(phases)==3, "Padding only possible to a |phases|==3!")
for property in properties
if haskey(object, property)
val = object[property][wdg]
val_length = length(connections)==2 ? 1 : length(connections)
@assert(isa(val, Vector) && length(val)==val_length)
# build tmp
tmp = Dict()
sign = invert ? -1 : 1
if val_length==1
tmp[(connections[1], connections[2])] = val[1]
tmp[(connections[2], connections[1])] = sign*val[1]
else
tmp[(connections[1], connections[2])] = val[1]
tmp[(connections[2], connections[3])] = val[2]
tmp[(connections[3], connections[1])] = val[3]
end
merge!(tmp, Dict((k[2], k[1])=>sign*v for (k,v) in tmp))
get_val(x,y) = haskey(tmp, (x,y)) ? tmp[(x,y)] : 0.0
object[property][wdg] = [get_val(phases[1], phases[2]), get_val(phases[2], phases[3]), get_val(phases[3], phases[1])]
end
end
end
"Filters out values of a vector or matrix for certain properties"
function _apply_filter!(obj::Dict{String,<:Any}, properties::Vector{String}, filter::Union{Array,BitArray})
for property in properties
if haskey(obj, property)
if isa(obj[property], Vector)
obj[property] = obj[property][filter]
elseif isa(obj[property], Matrix)
obj[property] = obj[property][filter, filter]
else
error("The property $property is not a Vector or a Matrix!")
end
end
end
end
"Filters out values of a vector or matrix for certain properties (transformer winding variant)"
function _apply_filter!(obj::Dict{String,<:Any}, properties::Vector{String}, wdg::Int, filter::Union{Array,BitArray})
for property in properties
if haskey(obj, property)
if isa(obj[property], Vector)
obj[property][wdg] = obj[property][wdg][filter]
elseif isa(obj[property], Matrix)
obj[property][wdg] = obj[property][wdg][filter, filter]
else
error("The property $property is not a Vector or a Matrix!")
end
end
end
end
"""
Given a set of addmittances 'y' connected from the conductors 'f_cnds' to the
conductors 't_cnds', this method will return a list of conductors 'cnd' and a
matrix 'Y', which will satisfy I[cnds] = Y*V[cnds].
"""
function _calc_shunt(f_cnds::Vector{Int}, t_cnds::Vector{Int}, y::Vector{<:Union{Real,Vector{<:Real}}})::Tuple{Vector{Int}, Matrix{Real}}
cnds = unique([f_cnds..., t_cnds...])
e(f,t) = reshape([c==f ? 1 : c==t ? -1 : 0 for c in cnds], length(cnds), 1)
Y = sum([e(f_cnds[i], t_cnds[i])*y[i]*e(f_cnds[i], t_cnds[i])' for (i,_y) in enumerate(y)])
return (cnds, Y)
end
"""
Given a set of terminals 'cnds' with associated shunt admittance 'Y', this
method will calculate the reduced admittance matrix if terminal 'ground' is
grounded.
"""
function _calc_ground_shunt_admittance_matrix(cnds::Vector{Int}, Y::Matrix{T}, ground::Int)::Tuple{Vector{Int}, Matrix{T}} where T <: Number
if ground in cnds
cndsr = setdiff(cnds, ground)
cndsr_inds = _get_idxs(cnds, cndsr)
Yr = Y[cndsr_inds, cndsr_inds]
return (cndsr, Yr)
else
return cnds, Y
end
end
"initialization actions for unmapping"
function _init_unmap_eng_obj!(data_eng::Dict{String,<:Any}, eng_obj_type::String, map::Dict{String,<:Any})::Dict{String,Any}
if !haskey(data_eng, eng_obj_type)
data_eng[eng_obj_type] = Dict{String,Any}()
end
eng_obj = Dict{String,Any}()
return eng_obj
end
"returns component from the mathematical data model"
function _get_math_obj(data_math::Dict{String,<:Any}, to_id::String)::Dict{String,Any}
math_type, math_id = split(to_id, '.')
return haskey(data_math, math_type) && haskey(data_math[math_type], math_id) ? data_math[math_type][math_id] : Dict{String,Any}()
end
"convert cost model names"
function _add_gen_cost_model!(math_obj::Dict{String,<:Any}, eng_obj::Dict{String,<:Any})
math_obj["model"] = get(eng_obj, "cost_pg_model", 2)
math_obj["startup"] = 0.0
math_obj["shutdown"] = 0.0
math_obj["cost"] = get(eng_obj, "cost_pg_parameters", [1.0, 0.0])
math_obj["ncost"] = length(math_obj["cost"])
end
"applies a xfmrcode to a transformer in preparation for converting to mathematical model"
function _apply_xfmrcode!(eng_obj::Dict{String,<:Any}, data_eng::Dict{String,<:Any})
if haskey(eng_obj, "xfmrcode") && haskey(data_eng, "xfmrcode") && haskey(data_eng["xfmrcode"], eng_obj["xfmrcode"])
xfmrcode = data_eng["xfmrcode"][eng_obj["xfmrcode"]]
for (k, v) in xfmrcode
if !haskey(eng_obj, k)
eng_obj[k] = deepcopy(v)
elseif haskey(eng_obj, k) && k in ["vm_nom", "sm_nom", "tm_set", "rw"]
for (w, vw) in enumerate(eng_obj[k])
if ismissing(vw)
eng_obj[k][w] = deepcopy(v[w])
end
end
end
end
end
end
"applies a linecode to a line in preparation for converting to mathematical model"
function _apply_linecode!(eng_obj::Dict{String,<:Any}, data_eng::Dict{String,<:Any})
if haskey(eng_obj, "linecode") && haskey(data_eng, "linecode") && haskey(data_eng["linecode"], eng_obj["linecode"])
linecode = data_eng["linecode"][eng_obj["linecode"]]
for property in ["rs", "xs", "g_fr", "g_to", "b_fr", "b_to", "cm_ub", "sm_ub"]
if !haskey(eng_obj, property) && haskey(linecode, property)
eng_obj[property] = deepcopy(linecode[property])
end
end
end
end
"converts impendance in Ohm/m by multiplying by length"
function _impedance_conversion(data_eng::Dict{String,<:Any}, eng_obj::Dict{String,<:Any}, key::String)
eng_obj[key] .* get(eng_obj, "length", 1.0)
end
"converts admittance by multiplying by 2πωl"
function _admittance_conversion(data_eng::Dict{String,<:Any}, eng_obj::Dict{String,<:Any}, key::String)
2.0 .* pi .* data_eng["settings"]["base_frequency"] .* eng_obj[key] .* get(eng_obj, "length", 1.0) ./ 1e9
end
"lossy grounding to perfect grounding and shunts"
function _convert_grounding(terminals, grounded, rg, xg)
grouped = Dict(t=>[] for t in unique(grounded))
for (i,t) in enumerate(grounded)
push!(grouped[t], rg[i]+im*xg[i])
end
t_lookup = Dict(t=>i for (i,t) in enumerate(terminals))
grounded_lossless = fill(false, length(terminals))
shunts = []
for (t, zgs) in grouped
if any(iszero.(zgs))
grounded_lossless[t_lookup[t]] = true
else
ygs = 1 ./zgs
yg = sum(ygs)
push!(shunts, ([t], [yg]))
end
end
return grounded_lossless, shunts
end
"slices branches based on connected terminals"
function _slice_branches!(data_math::Dict{String,<:Any})
for (_, branch) in data_math["branch"]
if haskey(branch, "f_connections")
N = length(branch["f_connections"])
for prop in ["br_r", "br_x", "g_fr", "g_to", "b_fr", "b_to"]
branch[prop] = branch[prop][1:N,1:N]
end
end
end
end
"finds maximal set of ungrounded phases"
function _get_complete_conductor_set(data::Dict{String,<:Any})
conductors = Set([])
for (_, obj) in data["bus"]
for t in obj["terminals"]
push!(conductors, t)
end
end
return sort([c for c in conductors])
end
"checks if data structures are equivalent, and if not, will enumerate the differences"
function _check_equal(data1::Dict{String,<:Any}, data2::Dict{String,<:Any}; context::String="", ignore::Vector{String}=Vector{String}(["connections", "f_connections", "t_connections", "terminals"]))
lines = []
for (i, obj) in data1
if !haskey(data2, i)
push!(lines, " $i missing in $data2")
else
if obj != data2[i]
if isa(obj, Dict)
push!(lines, _check_equal(obj, data2[i]; context=" $context $i"))
else
if !(i in ignore)
push!(lines, " $context $i: $obj != $(data2[i])")
end
end
end
end
end
return lines
end
"adds conductors to connections during padding process"
function _pad_connections!(eng_obj::Dict{String,<:Any}, connection_key::String, conductors::Union{Vector{Int},Vector{String}})
for cond in conductors
if !(cond in eng_obj[connection_key])
push!(eng_obj[connection_key], cond)
end
end
return eng_obj[connection_key]
end
"adds conductors to connections during padding process, transformer winding variant"
function _pad_connections!(eng_obj::Dict{String,<:Any}, connection_key::String, wdg::Int, conductors::Union{Vector{Int},Vector{String}})
for cond in conductors
if !(cond in eng_obj[connection_key][wdg])
push!(eng_obj[connection_key][wdg], cond)
end
end
end
"helper function to map non integer conductor ids into integers"
function _map_conductor_ids!(data_math::Dict{String,<:Any})
if all(typeof(c) <: Int for c in data_math["conductor_ids"])
cnd_map = Dict{Any,Int}(c => c for c in data_math["conductor_ids"])
else
cnd_map = Dict{Any,Int}(c => idx for (idx, c) in enumerate(data_math["conductor_ids"]))
end
data_math["conductor_ids"] = Vector{Int}([cnd_map[c] for c in data_math["conductor_ids"]])
for type in ["branch", "switch", "transformer"]
if haskey(data_math, type)
for (_,obj) in data_math[type]
obj["f_connections"] = Vector{Int}([cnd_map[c] for c in obj["f_connections"]])
obj["t_connections"] = Vector{Int}([cnd_map[c] for c in obj["t_connections"]])
end
end
end
for type in ["load", "shunt", "gen", "storage"]
if haskey(data_math, type)
for (_,obj) in data_math[type]
obj["connections"] = Vector{Int}([cnd_map[c] for c in obj["connections"]])
end
end
end
for (_,bus) in data_math["bus"]
bus["terminals"] = Vector{Int}([cnd_map[t] for t in bus["terminals"]])
end
end
"""
Returns the tightest set of pairwise voltage magnitude bounds,
removing looser bounds which are implied by the tighter ones.
"""
function _get_tight_pairwise_voltage_magnitude_bounds(bus::Dict)
lb_pairs = Tuple{Any,Any,Real}[]
ub_pairs = Tuple{Any,Any,Real}[]
haskey(bus, "vm_pair_lb") && append!(lb_pairs, bus["vm_pair_lb"])
haskey(bus, "vm_pair_ub") && append!(ub_pairs, bus["vm_pair_ub"])
haskey(bus, "vm_pn_lb") && append!(lb_pairs, [(p, bus["neutral"], bus["vm_pn_lb"]) for p in bus["phases"]])
haskey(bus, "vm_pn_ub") && append!(ub_pairs, [(p, bus["neutral"], bus["vm_pn_ub"]) for p in bus["phases"]])
haskey(bus, "vm_pp_lb") && append!(lb_pairs, [(bus["phases"][i], bus["phases"][j], bus["vm_pp_lb"]) for i in 1:length(bus["phases"]) for j in i+1:length(bus["phases"])])
haskey(bus, "vm_pp_ub") && append!(ub_pairs, [(bus["phases"][i], bus["phases"][j], bus["vm_pp_ub"]) for i in 1:length(bus["phases"]) for j in i+1:length(bus["phases"])])
lb_pairs_tight = Tuple{Any,Any,Real}[]
for (c,d) in unique([(min(n,m), max(n,m)) for (n,m,bound) in lb_pairs])
bound = maximum([bound for (n,m,bound) in lb_pairs if (n==c&&m==d) || (n==d&&m==c)])
push!(lb_pairs_tight, (c, d, bound))
end
ub_pairs_tight = Tuple{Any,Any,Real}[]
for (c,d) in unique([(min(n,m), max(n,m)) for (n,m,b) in ub_pairs])
bound = minimum([bound for (n,m,bound) in ub_pairs if (n==c&&m==d) || (n==d&&m==c)])
push!(ub_pairs_tight, (c, d, bound))
end
return lb_pairs_tight, ub_pairs_tight
end
"""
Returns the tightest set of absolute voltage magnitude bounds,
removing looser bounds which are implied by the tighter ones.
"""
function _get_tight_absolute_voltage_magnitude_bounds(bus::Dict)
N = length(bus["terminals"])
vmin = haskey(bus, "vm_lb") ? bus["vm_lb"] : fill(0.0, N)
vmax = haskey(bus, "vm_ub") ? bus["vm_ub"] : fill(Inf, N)
if haskey(bus, "vm_ng_ub")
idx = findfirst(bus["terminals"].==bus["neutral"])
vmax[idx] = min(vmax[idx], bus["vm_ng_ub"])
end
return vmin, vmax
end
"""
When a terminal is grounded, any pairwise bounds it occurs in
imply an absolute bound for the other terminal in the pair.
This method converts such pairwise bounds to absolute ones.
"""
function _add_implicit_absolute_bounds!(bus_math, terminals::Vector)
grounded_terminals = terminals[bus_math["grounded"]]
vm_pair_lb = bus_math["vm_pair_lb"]
vm_pair_ub = bus_math["vm_pair_ub"]
vmin = bus_math["vmin"]
vmax = bus_math["vmax"]
lb_keep_idx = []
for (i,(a,b,lb)) in enumerate(vm_pair_lb)
if a in grounded_terminals
b_idx = findfirst(terminals.==b)
vmin[b_idx] = max(vmin[b_idx],lb)
elseif b in grounded_terminals
a_idx = findfirst(terminals.==a)
vmin[a_idx] = max(vmin[a_idx],lb)
else
push!(lb_keep_idx, i)
end
end
ub_keep_idx = []
for (i,(a,b,ub)) in enumerate(vm_pair_ub)
if a in grounded_terminals
b_idx = findfirst(terminals.==b)
vmax[b_idx] = min(vmax[b_idx],ub)
elseif b in grounded_terminals
a_idx = findfirst(terminals.==a)
vmax[a_idx] = min(vmax[a_idx],ub)
else
push!(ub_keep_idx, i)
end
end
bus_math["vmin"] = vmin
bus_math["vmax"] = vmax
bus_math["vm_pair_lb"] = vm_pair_lb[lb_keep_idx]
bus_math["vm_pair_ub"] = vm_pair_ub[ub_keep_idx]
end
"computes the bus type based on existing bus_type, the status of the generation object, and the control_mode"
function _compute_bus_type(existing_bus_type::Int, gen_status::Int, control_mode::Int)::Int
bus_type = existing_bus_type
if gen_status == Int(ENABLED) && bus_type != pmd_math_component_status_inactive["bus"]
if control_mode == Int(ISOCHRONOUS)
bus_type = 3
else
if bus_type != 3
bus_type = 2
end
end
end
return bus_type
end