/
apo.jl
401 lines (286 loc) · 16.3 KB
/
apo.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
"apo models ignore reactive power flows"
function variable_mc_generator_power_imaginary(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"apo models ignore reactive power flows"
function variable_mc_generator_power_imaginary_on_off(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"on/off constraint for generators"
function constraint_mc_gen_power_on_off(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, connections::Vector{Int}, pmin::Vector{<:Real}, pmax::Vector{<:Real}, qmin::Vector{<:Real}, qmax::Vector{<:Real})
z = var(pm, nw, :z_gen, i)
pg = var(pm, nw, :pg_bus)[i] = var(pm, nw, :pg, i)
for (idx, c) in enumerate(connections)
if isfinite(pmax[idx])
JuMP.@constraint(pm.model, pg[c] .<= pmax[idx].*z)
end
if isfinite(pmin[idx])
JuMP.@constraint(pm.model, pg[c] .>= pmin[idx].*z)
end
end
end
"nothing to do"
function constraint_mc_voltage_magnitude_only(::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, vm_ref::Vector{<:Real})
end
"apo models ignore reactive power flows"
function variable_mc_storage_power_imaginary(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"apo models ignore reactive power flows"
function variable_mc_storage_power_control_imaginary(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"apo models ignore reactive power flows"
function variable_mc_storage_power_imaginary_on_off(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"apo models ignore reactive power flows"
function variable_mc_branch_power_imaginary(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"apo models ignore reactive power flows"
function variable_mc_branch_flow_ne_reactive(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"apo models ignore reactive power flows"
function variable_mc_switch_power_imaginary(::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"no voltage variables"
function variable_mc_oltc_transformer_tap(::AbstractUnbalancedNFAModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
""
function constraint_mc_switch_state_open(pm::AbstractUnbalancedActivePowerModel, nw::Int, f_idx::Tuple{Int,Int,Int})
psw = var(pm, nw, :psw, f_idx)
JuMP.@constraint(pm.model, psw .== 0.0)
end
"nothing to do, these models do not have complex voltage variables"
function variable_mc_bus_voltage(pm::AbstractUnbalancedNFAModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
"nothing to do, these models do not have complex voltage variables"
function variable_mc_capcontrol(pm::AbstractUnbalancedNFAModel; nw::Int=nw_id_default, relax::Bool=false, report::Bool=true)
end
"nothing to do"
function constraint_mc_switch_state_closed(pm::AbstractUnbalancedNFAModel, nw::Int, f_bus::Int, t_bus::Int, f_connections::Vector{Int}, t_connections::Vector{Int})
end
"nothing to do, these models do not have angle difference constraints"
function constraint_mc_voltage_angle_difference(pm::AbstractUnbalancedNFAModel, nw::Int, f_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, t_connections::Vector{Int}, angmin::Vector{<:Real}, angmax::Vector{<:Real})
end
"apo models ignore reactive power flows"
function variable_mc_transformer_power_imaginary(pm::AbstractUnbalancedActivePowerModel; nw::Int=nw_id_default, bounded::Bool=true, report::Bool=true)
end
@doc raw"""
constraint_mc_power_balance(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, terminals::Vector{Int}, grounded::Vector{Bool}, bus_arcs::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_arcs_sw::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_arcs_trans::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_gens::Vector{Tuple{Int,Vector{Int}}}, bus_storage::Vector{Tuple{Int,Vector{Int}}}, bus_loads::Vector{Tuple{Int,Vector{Int}}}, bus_shunts::Vector{Tuple{Int,Vector{Int}}})
power balanace constraint with line shunts and transformers, active power only
```math
p_{br} + p_{tr} + p_{sw} == p_{g} - p_{s} - p_{d} - G
```
"""
function constraint_mc_power_balance(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, terminals::Vector{Int}, grounded::Vector{Bool}, bus_arcs::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_arcs_sw::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_arcs_trans::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_gens::Vector{Tuple{Int,Vector{Int}}}, bus_storage::Vector{Tuple{Int,Vector{Int}}}, bus_loads::Vector{Tuple{Int,Vector{Int}}}, bus_shunts::Vector{Tuple{Int,Vector{Int}}})
p = get(var(pm, nw), :p, Dict()); _check_var_keys(p, bus_arcs, "active power", "branch")
psw = get(var(pm, nw), :psw, Dict()); _check_var_keys(psw, bus_arcs_sw, "active power", "switch")
pt = get(var(pm, nw), :pt, Dict()); _check_var_keys(pt, bus_arcs_trans, "active power", "transformer")
pg = get(var(pm, nw), :pg, Dict()); _check_var_keys(pg, bus_gens, "active power", "generator")
ps = get(var(pm, nw), :ps, Dict()); _check_var_keys(ps, bus_storage, "active power", "storage")
pd = get(var(pm, nw), :pd_bus, Dict()); _check_var_keys(pd, bus_loads, "active power", "load")
Gt, Bt = _build_bus_shunt_matrices(pm, nw, terminals, bus_shunts)
cstr_p = []
ungrounded_terminals = [(idx,t) for (idx,t) in enumerate(terminals) if !grounded[idx]]
for (idx,t) in ungrounded_terminals
cp = JuMP.@constraint(pm.model,
sum(p[a][t] for (a, conns) in bus_arcs if t in conns)
+ sum(psw[a_sw][t] for (a_sw, conns) in bus_arcs_sw if t in conns)
+ sum(pt[a_trans][t] for (a_trans, conns) in bus_arcs_trans if t in conns)
==
sum(pg[g][t] for (g, conns) in bus_gens if t in conns)
- sum(ps[s][t] for (s, conns) in bus_storage if t in conns)
- sum(pd[d][t] for (d, conns) in bus_loads if t in conns)
- LinearAlgebra.diag(Gt)[idx]
)
push!(cstr_p, cp)
end
# omit reactive constraint
con(pm, nw, :lam_kcl_r)[i] = cstr_p
con(pm, nw, :lam_kcl_i)[i] = []
if _IM.report_duals(pm)
sol(pm, nw, :bus, i)[:lam_kcl_r] = cstr_p
sol(pm, nw, :bus, i)[:lam_kcl_i] = []
end
end
"nothing to do in capc, no complex voltage variables in these models"
constraint_mc_power_balance_capc(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, terminals::Vector{Int}, grounded::Vector{Bool}, bus_arcs::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_arcs_sw::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_arcs_trans::Vector{Tuple{Tuple{Int,Int,Int},Vector{Int}}}, bus_gens::Vector{Tuple{Int,Vector{Int}}}, bus_storage::Vector{Tuple{Int,Vector{Int}}}, bus_loads::Vector{Tuple{Int,Vector{Int}}}, bus_shunts::Vector{Tuple{Int,Vector{Int}}}) = constraint_mc_power_balance(pm, nw, i, terminals, grounded, bus_arcs, bus_arcs_sw, bus_arcs_trans, bus_gens, bus_storage, bus_loads, bus_shunts)
######## Lossless Models ########
""
function constraint_mc_network_power_balance(pm::AbstractUnbalancedAPLossLessModels, nw::Int, i, comp_gen_ids, comp_pd, comp_qd, comp_gs, comp_bs, comp_branch_g, comp_branch_b)
pg = var(pm, nw, :pg)
for (idx, t) in enumerate(ref(pm, nw, :bus, i, "terminals"))
JuMP.@constraint(pm.model, sum(pg[g][t] for (g, conns) in comp_gen_ids if t in conns) == sum(pd[findfirst(isequal(t), conns)] for (i,conns,pd) in values(comp_pd) if t in conns) + sum(gs[findfirst(isequal(t), conns)]*1.0^2 for (i,conns,gs) in values(comp_gs) if t in conns))
# omit reactive constraint
end
end
"Do nothing, this model is symmetric"
function constraint_mc_ohms_yt_to(pm::AbstractUnbalancedAPLossLessModels, nw::Int, f_bus::Int, t_bus::Int, f_idx::Tuple{Int,Int,Int}, t_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, t_connections::Vector{Int}, G::Matrix{<:Real}, B::Matrix{<:Real}, G_fr::Matrix{<:Real}, B_fr::Matrix{<:Real})
end
### Network Flow Approximation ###
"nothing to do, no voltage angle variables"
function constraint_mc_theta_ref(pm::AbstractUnbalancedNFAModel, i::Int; nw::Int=nw_id_default)
end
"nothing to do, no voltage angle variables"
function constraint_mc_ohms_yt_from(pm::AbstractUnbalancedNFAModel, i::Int; nw::Int=nw_id_default)
end
"nothing to do, this model is symmetric"
function constraint_mc_ohms_yt_to(pm::NFAUPowerModel, nw::Int, f_bus::Int, t_bus::Int, f_idx::Tuple{Int,Int,Int}, t_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, t_connections::Vector{Int}, G::Matrix{<:Real}, B::Matrix{<:Real}, G_fr::Matrix{<:Real}, B_fr::Matrix{<:Real})
end
@doc raw"""
constraint_mc_transformer_power(pm::NFAUPowerModel, i::Int; nw::Int=nw_id_default)
transformer active power only constraint pf=-pt
```math
p_f[fc] == -pt[tc]
```
"""
function constraint_mc_transformer_power(pm::NFAUPowerModel, i::Int; nw::Int=nw_id_default, fix_taps::Bool=false)
transformer = ref(pm, nw, :transformer, i)
pf = var(pm, nw, :pt, (i, transformer["f_bus"], transformer["t_bus"]))
pt = var(pm, nw, :pt, (i, transformer["t_bus"], transformer["f_bus"]))
for (fc, tc) in zip(transformer["f_connections"],transformer["t_connections"])
JuMP.@constraint(pm.model, pf[fc] == -pt[tc])
end
end
"`-rate_a <= p[f_idx] <= rate_a`"
function constraint_mc_thermal_limit_from(pm::AbstractUnbalancedActivePowerModel, nw::Int, f_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, rate_a::Vector{<:Real})
mu_sm_fr = JuMP.ConstraintRef[]
for (idx, c) in enumerate(f_connections)
p_fr = var(pm, nw, :p, f_idx)[c]
if isa(p_fr, JuMP.VariableRef) && JuMP.has_lower_bound(p_fr)
push!(mu_sm_fr,JuMP.LowerBoundRef(p_fr))
JuMP.lower_bound(p_fr) < -rate_a[idx] && set_lower_bound(p_fr, -rate_a[idx])
if JuMP.has_upper_bound(p_fr)
JuMP.upper_bound(p_fr) > rate_a[idx] && set_upper_bound(p_fr, rate_a[idx])
end
else
if rate_a[idx] < Inf
push!(mu_sm_fr, JuMP.@constraint(pm.model, p_fr <= rate_a[idx]))
end
end
end
con(pm, nw, :mu_sm_branch)[f_idx] = mu_sm_fr
if _IM.report_duals(pm)
sol(pm, nw, :branch, f_idx[1])[:mu_sm_fr] = mu_sm_fr
end
end
""
function constraint_mc_thermal_limit_to(pm::AbstractUnbalancedActivePowerModel, nw::Int, t_idx::Tuple{Int,Int,Int}, t_connections::Vector{Int}, rate_a::Vector{<:Real})
mu_sm_to = JuMP.ConstraintRef[]
for (idx,c) in enumerate(t_connections)
p_to = var(pm, nw, :p, t_idx)[c]
if isa(p_to, JuMP.VariableRef) && JuMP.has_lower_bound(p_to)
push!(mu_sm_to, JuMP.LowerBoundRef(p_to))
JuMP.lower_bound(p_to) < -rate_a[idx] && set_lower_bound(p_to, -rate_a[idx])
if JuMP.has_upper_bound(p_to)
JuMP.upper_bound(p_to) > rate_a[idx] && set_upper_bound(p_to, rate_a[idx])
end
else
if rate_a[idx] < Inf
push!(mu_sm_to, JuMP.@constraint(pm.model, p_to <= rate_a[idx]))
end
end
end
con(pm, nw, :mu_sm_branch)[t_idx] = mu_sm_to
if _IM.report_duals(pm)
sol(pm, nw, :branch, t_idx[1])[:mu_sm_to] = mu_sm_to
end
end
@doc raw"""
constraint_mc_switch_thermal_limit(pm::AbstractUnbalancedActivePowerModel, nw::Int, f_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, rating::Vector{<:Real})::Nothing
Active power only switch thermal limit constraint
math```
-S_{max} \leq p_{fr} \leq S_{max}
```
"""
function constraint_mc_switch_thermal_limit(pm::AbstractUnbalancedActivePowerModel, nw::Int, f_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, rating::Vector{<:Real})::Nothing
psw = var(pm, nw, :psw, f_idx)
mu_sm_fr = JuMP.ConstraintRef[]
for (idx, c) in enumerate(f_connections)
if rating[idx] < Inf
JuMP.lower_bound(psw[c]) < -rating[idx] && set_lower_bound(psw[c], -rating[idx])
JuMP.upper_bound(psw[c]) > rating[idx] && set_upper_bound(psw[c], rating[idx])
end
end
con(pm, nw, :mu_sm_switch)[f_idx] = mu_sm_fr
nothing
end
""
function constraint_mc_storage_thermal_limit(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, connections::Vector{Int}, rating::Real)
ps = var(pm, nw, :ps, i)
for (idx,c) in enumerate(connections)
JuMP.has_lower_bound(ps[c]) && JuMP.lower_bound(ps[c]) < -rating && set_lower_bound(ps[c], -rating)
JuMP.has_upper_bound(ps[c]) && JuMP.upper_bound(ps[c]) > rating && set_upper_bound(ps[c], rating)
end
end
""
function constraint_mc_storage_current_limit(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, bus::Int, connections::Vector{Int}, rating::Real)
ps = var(pm, nw, :ps, i)
for (idx,c) in enumerate(connections)
JuMP.lower_bound(ps[c]) < -rating && set_lower_bound(ps[c], -rating)
JuMP.upper_bound(ps[c]) > rating && set_upper_bound(ps[c], rating)
end
end
"active power only"
function constraint_mc_storage_losses(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, bus::Int, connections::Vector{Int}, r::Real, ::Real, p_loss::Real, ::Real)
ps = var(pm, nw, :ps, i)
sc = var(pm, nw, :sc, i)
sd = var(pm, nw, :sd, i)
JuMP.@constraint(pm.model,
sum(ps[c] for c in connections) + (sd - sc)
==
p_loss + r * sum(ps[c]^2 for (idx,c) in enumerate(connections))
)
end
"active power only, lossless model"
function constraint_mc_storage_losses(pm::AbstractUnbalancedAPLossLessModels, nw::Int, i::Int, bus::Int, connections::Vector{Int}, r::Real, ::Real, p_loss::Real, ::Real)
ps = var(pm, nw, :ps, i)
sc = var(pm, nw, :sc, i)
sd = var(pm, nw, :sd, i)
JuMP.@constraint(pm.model,
sum(ps[c] for c in connections) + (sd - sc)
==
0.0
)
end
""
function constraint_mc_storage_on_off(pm::AbstractUnbalancedActivePowerModel, nw::Int, i::Int, connections::Vector{Int}, pmin::Vector{<:Real}, pmax::Vector{<:Real}, qmin::Vector{<:Real}, qmax::Vector{<:Real}, charge_ub, discharge_ub)
z_storage = var(pm, nw, :z_storage, i)
ps = [var(pm, nw, :ps, i)[c] for c in connections]
JuMP.@constraint(pm.model, ps .<= pmax.*z_storage)
JuMP.@constraint(pm.model, ps .>= pmin.*z_storage)
end
"Only support wye-connected generators."
function constraint_mc_generator_power(pm::AbstractUnbalancedActivePowerModel, id::Int; nw::Int=nw_id_default, report::Bool=true)
var(pm, nw, :pg_bus)[id] = var(pm, nw, :pg, id)
end
# load variables
"""
Create a dictionary with values of type Any for the load.
Depending on the load model, this can be a parameter or a NLexpression.
These will be inserted into KCL.
"""
function variable_mc_load_power(pm::AbstractUnbalancedActivePowerModel; nw=nw_id_default, bounded::Bool=true, report::Bool=true)
var(pm, nw)[:pd] = Dict{Int, Any}()
var(pm, nw)[:pd_bus] = Dict{Int, Any}()
end
"Only support wye-connected, constant-power loads."
function constraint_mc_load_power(pm::AbstractUnbalancedActivePowerModel, id::Int; nw::Int=nw_id_default, report::Bool=true)
load = ref(pm, nw, :load, id)
connections = load["connections"]
pd = load["pd"]
var(pm, nw, :pd)[id] = JuMP.Containers.DenseAxisArray(pd, connections)
var(pm, nw, :pd_bus)[id] = var(pm, nw, :pd, id)
if report
sol(pm, nw, :load, id)[:pd] = var(pm, nw, :pd, id)
sol(pm, nw, :load, id)[:pd_bus] = var(pm, nw, :pd_bus, id)
end
end
"nothing to do, no voltage variables"
function constraint_mc_ampacity_from(pm::AbstractUnbalancedActivePowerModel, nw::Int, f_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, rating::Vector{Float64})::Nothing
end
"nothing to do, no voltage variables"
function constraint_mc_ampacity_to(pm::AbstractUnbalancedActivePowerModel, nw::Int, t_idx::Tuple{Int,Int,Int}, t_connections::Vector{Int}, rating::Vector{Float64})::Nothing
end
"nothing to do, no voltage variables"
function constraint_mc_switch_ampacity(pm::AbstractUnbalancedActivePowerModel, nw::Int, f_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, rating::Vector{Float64})::Nothing
end
"Already handled by variable_mc_branch_power_real"
function constraint_mc_branch_flow(pm::AbstractUnbalancedAPLossLessModels, nw::Int, f_idx::Tuple{Int,Int,Int}, t_idx::Tuple{Int,Int,Int}, f_connections::Vector{Int}, t_connections::Vector{Int})
end