-
Notifications
You must be signed in to change notification settings - Fork 1
/
dcopf.jl
450 lines (347 loc) · 16.9 KB
/
dcopf.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
# DC OPF setup
"""
setup_dcopf!(config, data, model)
Set up a DC OPF problem
"""
function setup_dcopf!(config, data, model)
# define tables
bus = get_table(data, :bus)
years = get_years(data)
rep_hours = get_table(data, :hours) # weight of representative time chunks (hours)
hours_per_year = sum(get_hour_weights(data))
gen = get_table(data, :gen)
branch = get_table(data, :branch)
nbus = nrow(bus)
nyear = get_num_years(data)
nhour = get_num_hours(data)
nbranch = nrow(branch)
ngen = nrow(gen)
## Variables
@info "Creating Variables"
# Voltage Angle
@variable(model,
θ_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour],
start=0.0,
lower_bound = -1e6, # Lower value from MATLAB E4ST minimum(res.base.bus(:, VA)) was ~-2.5e5
upper_bound = 1e6 # Upper value from MATLAB E4ST maximum(res.base.bus(:, VA)) was ~200
)
# Capacity
@variable(model,
pcap_gen[gen_idx in 1:ngen, year_idx in 1:nyear],
start = get_table_num(data, :gen, :pcap0, gen_idx, year_idx, :),
lower_bound = get_pcap_min(data, gen_idx, year_idx),
upper_bound = get_pcap_max(data, gen_idx, year_idx),
)
# Power Generation
@variable(model,
pgen_gen[gen_idx in 1:ngen, year_idx in 1:nyear, hour_idx in 1:nhour],
start = get_table_num(data, :gen, :pcap0, gen_idx, year_idx, :) * get_cf_max(config, data, gen_idx, year_idx, hour_idx),
lower_bound = 0.0,
upper_bound = get_pcap_max(data, gen_idx, year_idx) * 1.1, # 10% buffer here to allow cons_pgen_max to always be binding
)
# Power Curtailed
@variable(model,
plcurt_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour],
start=0.0,
lower_bound = 0.0,
upper_bound = get_plnom(data, bus_idx, year_idx, hour_idx),
)
## Expressions to be used later
@info "Creating Expressions"
# Power flowing through a given branch
@expression(model, pflow_branch[branch_idx in 1:nbranch, year_idx in 1:nyear, hour_idx in 1:nhour], get_pflow_branch(data, model, branch_idx, year_idx, hour_idx))
# Power flowing out of a given bus
@expression(model, pflow_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour], get_pflow_bus(data, model, bus_idx, year_idx, hour_idx))
# Power flowing in/out of buses, only necessary if modeling line losses from pflow.
if config[:line_loss_type] == "pflow"
# Make variables for positive and negative power flowing out of the bus.
@variable(model, pflow_out_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour], lower_bound = 0)
@variable(model, pflow_in_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour], lower_bound = 0)
end
# Served power of a given bus
@expression(model, plserv_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour], get_plnom(data, bus_idx, year_idx, hour_idx) - plcurt_bus[bus_idx, year_idx, hour_idx])
# Generated power of a given bus
@expression(model, pgen_bus[bus_idx in 1:nbus, year_idx in 1:nyear, hour_idx in 1:nhour], get_pgen_bus(data, model, bus_idx, year_idx, hour_idx))
## Constraints
@info "Creating Constraints"
# Constrain Power Generation
if hasproperty(gen, :cf_min)
cf_min = gen.cf_min
@constraint(model,
cons_pgen_min[gen_idx in 1:ngen, yr_idx in 1:nyear, hr_idx in 1:nhour],
pgen_gen[gen_idx, yr_idx, hr_idx] >=
cf_min[gen_idx][yr_idx, hr_idx] * pcap_gen[gen_idx, yr_idx]
)
end
pgen_scalar = Float64(config[:pgen_scalar])
@constraint(model,
cons_pgen_max[gen_idx in 1:ngen, yr_idx in 1:nyear, hr_idx in 1:nhour],
pgen_scalar * pgen_gen[gen_idx, yr_idx, hr_idx] <= # Scale by pgen_scalar in this constraint to improve matrix coefficient range. Some af values are very small.
pgen_scalar * get_cf_max(config, data, gen_idx, yr_idx, hr_idx) * pcap_gen[gen_idx, yr_idx]
)
# Constrain Reference Bus
for ref_bus_idx in get_ref_bus_idxs(data), yr_idx in 1:nyear, hr_idx in 1:nhour
fix(model[:θ_bus][ref_bus_idx, yr_idx, hr_idx], 0.0, force=true)
end
# Constrain Transmission Lines, positive and negative
@constraint(model,
cons_branch_pflow_pos[
branch_idx in 1:nbranch,
year_idx in 1:nyear,
hour_idx in 1:nhour;
get_pflow_branch_max(data, branch_idx, year_idx, hour_idx) > 0 # Only constrain for branches with nonzero pflow_max
],
pflow_branch[branch_idx, year_idx, hour_idx] <= get_pflow_branch_max(data, branch_idx, year_idx, hour_idx)
)
@constraint(model,
cons_branch_pflow_neg[
branch_idx in 1:nbranch,
year_idx in 1:nyear,
hour_idx in 1:nhour;
get_pflow_branch_max(data, branch_idx, year_idx, hour_idx) > 0 # Only constrain for branches with nonzero pflow_max
],
-pflow_branch[branch_idx, year_idx, hour_idx] <= get_pflow_branch_max(data, branch_idx, year_idx, hour_idx)
)
add_build_constraints!(data, model, :gen, :pcap_gen)
## Objective Function
@info "Building Objective"
@expression(model, obj, 0*model[:θ_bus][1,1,1])
# needed to be defined as an GenericAffExp instead of an Int64 so multiplied by an arbitrary var
# This keeps track of the expressions added to the obj and their signs
data[:obj_vars] = OrderedDict{Symbol, Any}()
# This is written as a cost minimization where costs are added to the obj
# Power System Costs
add_obj_term!(data, model, PerMWhGen(), :vom, oper = +)
# Only add fuel cost if included and non-zero.
if hasproperty(gen, :fuel_price) && anyany(!=(0), gen.fuel_price)
add_obj_term!(data, model, PerMMBtu(), :fuel_price, oper = +)
end
add_obj_term!(data, model, PerMWCap(), :fom, oper = +)
add_obj_term!(data, model, PerMWCap(), :routine_capex, oper = +)
@expression(model,
pcap_gen_inv_sim[gen_idx in axes(gen,1)],
AffExpr(0.0)
)
for (gen_idx,g) in enumerate(eachrow(gen))
g.build_status in ("unbuilt", "unretrofitted") || continue
# Retrieve the investment year (either the retrofit year or the build year)
year_retrofit = get(g, :year_retrofit, "")
year_invest = isempty(year_retrofit) ? g.year_on : year_retrofit
year_invest > last(years) && continue
yr_idx_on = findfirst(>=(year_invest), years)
add_to_expression!(pcap_gen_inv_sim[gen_idx], pcap_gen[gen_idx, yr_idx_on])
end
add_obj_term!(data, model, PerMWCapInv(), :capex_obj, oper = +)
add_obj_term!(data, model, PerMWCapInv(), :transmission_capex_obj, oper = +)
# Curtailment Cost
add_obj_term!(data, model, PerMWhCurtailed(), :curtailment_cost, oper = +)
# @objective() goes in the setup after modifications have been made
return model
end
export setup_dcopf!
################################################################################
# Helper Functions
################################################################################
# Accessor Functions
################################################################################
### Get Model Variables Functions
"""
get_pgen_bus(data, model, bus_idx, year_idx, hour_idx)
Returns total power generation for a bus at a time
* To use this to retieve the variable values after the model has been optimized, wrap the function with value() like this: value.(get_pgen_bus).
"""
function get_pgen_bus(data, model, bus_idx, year_idx, hour_idx)
bus_gens = get_bus_gens(data, bus_idx)
sum(model[:pgen_gen][bus_gens, year_idx, hour_idx])
end
export get_pgen_bus
"""
get_pflow_bus(data, model, f_bus_idx, year_idx, hour_idx)
Returns net power flow out of the bus
* To use this to retieve the variable values after the model has been optimized, wrap the function with value() like this: value.(get_pflow_bus).
"""
function get_pflow_bus(data, model, bus_idx, year_idx, hour_idx)
branch_idxs = get_table(data, :bus)[bus_idx, :connected_branch_idxs] #vector of the connecting branches with positive values for branches going out (branch f_bus = bus_idx) and negative values for branches coming in (branch t_bus = bus_idx)
isempty(branch_idxs) && return AffExpr(0.0)
return sum(get_pflow_branch(data, model, branch_idx, year_idx, hour_idx) for branch_idx in branch_idxs)
end
export get_pflow_bus
"""
get_pflow_branch(data, model, branch_idx, year_idx, hour_idx)
Return total power flow on a branch.
* If branch_idx_signed is positive then positive power flow is in the direction f_bus -> t_bus listed in the branch table. It is measuring the power flow out of f_bus.
* If branch_idx_signed is negative then positive power flow is in the opposite direction, t_bus -> f_bus listed in the branch table. It is measuring the power flow out of t_bus.
* To use this to retieve the variable values after the model has been optimized, wrap the function with value() like this: value.(get_pflow_branch).
"""
function get_pflow_branch(data, model, branch_idx_signed, year_idx, hour_idx)
direction = sign(branch_idx_signed)
branch_idx = abs(branch_idx_signed)
f_bus_idx = data[:branch].f_bus_idx[branch_idx]
t_bus_idx = data[:branch].t_bus_idx[branch_idx]
x = get_table_num(data, :branch, :x, branch_idx, year_idx, hour_idx)
Δθ = direction * (model[:θ_bus][f_bus_idx, year_idx, hour_idx] - model[:θ_bus][t_bus_idx, year_idx, hour_idx]) #positive for power flow out(f_bus to t_bus)
return Δθ / x
end
export get_pflow_branch
### Contraint/Expression Info Functions
"""
get_cf_max(data, gen_idx, year_idx, hour_idx)
Returns max capacity factor at a given time. It is based on the lower of gen properties `af` (availability factor) and optional `cf_max` (capacity factor). If it is below `config[:cf_threshold]`, it is rounded to zero.
"""
function get_cf_max(config, data, gen_idx, year_idx, hour_idx)
cf_threshold = config[:cf_threshold]::Float64
af = get_table_num(data, :gen, :af, gen_idx, year_idx, hour_idx)
gen = get_table(data, :gen)
if hasproperty(gen, :cf_max)
cf = get_table_num(data, :gen, :cf_max, gen_idx, year_idx, hour_idx)
else
cf = 1.0
end
cf_max = min(af, cf)
cf_max < cf_threshold && return 0.0
return cf_max
end
export get_cf_max
"""
get_egen_gen(data, model, gen_idx)
Returns the total energy generation from a gen summed over all rep time.
get_egen_gen(data, model, gen_idx, year_idx)
Returns the total energy generation from a gen summed over rep time for the given year.
get_egen_gen(data, model, gen_idx, year_idx, hour_idx)
Returns the total energy generation from a gen for the given year and hour. This is pgen_gen multiplied by the number of hours spent at that representative hour. See [`get_hour_weight`](@ref)
* To use this to retieve the variable values after the model has been optimized, wrap the function with `value()` like this: `value.(get_egen_gen(args...))`.
"""
function get_egen_gen(data, model, gen_idx)
rep_hours = get_table(data, :hours)
years = get_years(data)
return sum(rep_hours.hours[hour_idx] .* model[:pgen_gen][gen_idx, year_idx, hour_idx] for hour_idx in 1:nrow(rep_hours), year_idx in 1:length(years))
end
function get_egen_gen(data, model, gen_idx, year_idx)
rep_hours = get_table(data, :hours)
return sum(rep_hours.hours[hour_idx] .* model[:pgen_gen][gen_idx, year_idx, hour_idx] for hour_idx in 1:nrow(rep_hours))
end
function get_egen_gen(data, model, gen_idx, year_idx, hour_idx)
return model[:pgen_gen][gen_idx, year_idx, hour_idx] * get_hour_weight(data, hour_idx)
end
export get_egen_gen
"""
get_pcap_gen(data, model, gen_idx, year_idx)
Returns the capacity (pcap_gen) for the gen_idx and year_idx given.
"""
function get_pcap_gen(data, model, gen_idx, year_idx)
return model[:pcap_gen][gen_idx, year_idx]
end
export get_pcap_gen
"""
add_obj_term!(data, model, ::Term, s::Symbol; oper)
Adds or subtracts cost/revenue `s` to the objective function of the `model` based on the operator `oper`. Adds the cost/revenue to the objective variables list in data.
"""
function add_obj_term!(data, model, term::Term, s::Symbol; oper) end
function add_obj_term!(data, model, ::PerMWhGen, s::Symbol; oper)
#Check if s has already been added to obj
Base.@assert s ∉ keys(data[:obj_vars]) "$s has already been added to the objective function"
#write expression for the term
pgen_gen = model[:pgen_gen]::Array{VariableRef, 3}
gen = get_table(data, :gen)
col = gen[!,s]
nhr = get_num_hours(data)
nyr = get_num_years(data)
hour_weights = get_hour_weights(data)
model[s] = @expression(model,
[gen_idx in axes(gen,1), yr_idx in 1:nyr],
sum(col[gen_idx][yr_idx,hr_idx] * pgen_gen[gen_idx, yr_idx, hr_idx] * hour_weights[hr_idx] for hr_idx in 1:nhr)
)
# add or subtract the expression from the objective function
add_obj_exp!(data, model, PerMWhGen(), s; oper = oper)
end
function add_obj_term!(data, model, ::PerMMBtu, s::Symbol; oper)
#Check if s has already been added to obj
Base.@assert s ∉ keys(data[:obj_vars]) "$s has already been added to the objective function"
#write expression for the term
gen = get_table(data, :gen)
pgen_gen = model[:pgen_gen]::Array{VariableRef, 3}
col = gen[!,s]
hr = gen[!,:heat_rate]
nhr = get_num_hours(data)
nyr = get_num_years(data)
hour_weights = get_hour_weights(data)
model[s] = @expression(model,
[gen_idx in axes(gen,1), yr_idx in 1:nyr],
sum(col[gen_idx][yr_idx,hr_idx] * hr[gen_idx][yr_idx, hr_idx] * pgen_gen[gen_idx, yr_idx, hr_idx] * hour_weights[hr_idx] for hr_idx in 1:nhr)
)
# add or subtract the expression from the objective function
add_obj_exp!(data, model, PerMWhGen(), s; oper = oper)
end
function add_obj_term!(data, model, ::PerMWCap, s::Symbol; oper)
#Check if s has already been added to obj
Base.@assert s ∉ keys(data[:obj_vars]) "$s has already been added to the objective function"
#write expression for the term
gen = get_table(data, :gen)
years = get_years(data)
hours_per_year = sum(get_hour_weights(data))
pcap_gen = model[:pcap_gen]
model[s] = @expression(model,
[gen_idx in 1:nrow(gen), year_idx in 1:length(years)],
get_table_num(data, :gen, s, gen_idx, year_idx, :) .*
pcap_gen[gen_idx, year_idx] *
hours_per_year
)
# add or subtract the expression from the objective function
add_obj_exp!(data, model, PerMWCap(), s; oper = oper)
end
function add_obj_term!(data, model, ::PerMWCapInv, s::Symbol; oper)
#Check if s has already been added to obj
Base.@assert s ∉ keys(data[:obj_vars]) "$s has already been added to the objective function"
#write expression for the term
gen = get_table(data, :gen)
years = get_years(data)
hours_per_year = sum(get_hour_weights(data))
pcap_gen_inv_sim = model[:pcap_gen_inv_sim]
model[s] = @expression(model,
[gen_idx in 1:nrow(gen), year_idx in 1:length(years)],
get_table_num(data, :gen, s, gen_idx, year_idx, :) .*
pcap_gen_inv_sim[gen_idx] *
hours_per_year
)
# add or subtract the expression from the objective function
add_obj_exp!(data, model, PerMWCapInv(), s; oper = oper)
end
function add_obj_term!(data, model, ::PerMWhCurtailed, s::Symbol; oper)
#Check if s has already been added to obj
Base.@assert s ∉ keys(data[:obj_vars]) "$s has already been added to the objective function"
#write expression for the term
bus = get_table(data, :bus)
rep_hours = get_table(data, :hours)
years = get_years(data)
# Use this expression for single VOLL
model[s] = @expression(model, [bus_idx in 1:nrow(bus)],
sum(get_voll(data, bus_idx, year_idx, hour_idx) .* rep_hours.hours[hour_idx] .* model[:plcurt_bus][bus_idx, year_idx, hour_idx] for year_idx in 1:length(years), hour_idx in 1:nrow(rep_hours)))
# add or subtract the expression from the objective function
add_obj_exp!(data, model, PerMWhCurtailed(), s; oper = oper)
end
"""
function add_obj_exp!(data, model, term::Term, s::Symbol; oper)
Adds expression s (already defined in model) to the objective expression model[:obj].
Adds the name, oper, and type of the term to data[:obj_vars].
"""
function add_obj_exp!(data, model, term::Term, s::Symbol; oper)
expr = model[s]
if oper == +
for new_term in expr
add_to_expression!(model[:obj], new_term)
end
elseif oper == -
for new_term in expr
add_to_expression!(model[:obj], -1, new_term)
end
else
Base.error("The entered operator isn't valid, oper must be + or -")
end
#Add s to array of variables included obj
data[:obj_vars][s] = OrderedDict{Symbol, Any}(
:term_sign => oper,
:term_type => typeof(term)
)
end
export add_obj_term!
export add_obj_exp!