Skip to content

Commit 0e98a81

Browse files
committed
restuting all unit tests and minor cleanup in tnep
1 parent f937b80 commit 0e98a81

File tree

3 files changed

+36
-72
lines changed

3 files changed

+36
-72
lines changed

src/core/variable.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function variable_complex_voltage_product_on_off{T}(pm::GenericPowerModel{T})
140140
bi_bp = Dict([(i, (b["f_bus"], b["t_bus"])) for (i,b) in pm.set.branches])
141141

142142
@variable(pm.model, min(0, wr_min[bi_bp[b]]) <= wr[b in pm.set.branch_indexes] <= max(0, wr_max[bi_bp[b]]), start = getstart(pm.set.buspairs, bi_bp[b], "wr_start", 1.0))
143-
@variable(pm.model, min(0, wi_min[bi_bp[b]]) <= wi[b in pm.set.branch_indexes] <= max(0, wi_max[bi_bp[b]]), start = getstart(pm.set.buspairs, bi_bp[b], "wr_start"))
143+
@variable(pm.model, min(0, wi_min[bi_bp[b]]) <= wi[b in pm.set.branch_indexes] <= max(0, wi_max[bi_bp[b]]), start = getstart(pm.set.buspairs, bi_bp[b], "wi_start"))
144144

145145
return wr, wi
146146
end

src/prob/tnep.jl

+29-65
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ function post_tnep{T}(pm::GenericPowerModel{T})
5555
end
5656

5757

58+
59+
##### TNEP specific base and solution code
60+
5861
type TNEPDataSets
5962
branches
6063
branch_indexes
@@ -69,42 +72,22 @@ end
6972
#### create some tnep specific sets
7073
function build_ne_sets(data::Dict{AbstractString,Any})
7174
bus_lookup = Dict([(Int(bus["index"]), bus) for bus in data["bus"]])
72-
#gen_lookup = Dict([(Int(gen["index"]), gen) for gen in data["gen"]])
73-
#for gencost in data["gencost"]
74-
# i = Int(gencost["index"])
75-
# gen_lookup[i] = merge(gen_lookup[i], gencost)
76-
#end
7775
branch_lookup = Dict([(Int(branch["index"]), branch) for branch in data["branch_ne"]])
7876

7977
# filter turned off stuff
8078
bus_lookup = filter((i, bus) -> bus["bus_type"] != 4, bus_lookup)
81-
#gen_lookup = filter((i, gen) -> gen["gen_status"] == 1 && gen["gen_bus"] in keys(bus_lookup), gen_lookup)
8279
branch_lookup = filter((i, branch) -> branch["br_status"] == 1 && branch["f_bus"] in keys(bus_lookup) && branch["t_bus"] in keys(bus_lookup), branch_lookup)
8380

8481
arcs_from = [(i,branch["f_bus"],branch["t_bus"]) for (i,branch) in branch_lookup]
8582
arcs_to = [(i,branch["t_bus"],branch["f_bus"]) for (i,branch) in branch_lookup]
8683
arcs = [arcs_from; arcs_to]
8784

88-
#bus_gens = Dict([(i, []) for (i,bus) in bus_lookup])
89-
#for (i,gen) in gen_lookup
90-
# push!(bus_gens[gen["gen_bus"]], i)
91-
#end
92-
9385
bus_branches = Dict([(i, []) for (i,bus) in bus_lookup])
9486
for (l,i,j) in arcs_from
9587
push!(bus_branches[i], (l,i,j))
9688
push!(bus_branches[j], (l,j,i))
9789
end
9890

99-
#ref_bus = [i for (i,bus) in bus_lookup | bus["bus_type"] == 3][1]
100-
#ref_bus = Union{}
101-
#for (k,v) in bus_lookup
102-
# if v["bus_type"] == 3
103-
# ref_bus = k
104-
# break
105-
# end
106-
#end
107-
10891
bus_idxs = collect(keys(bus_lookup))
10992
#gen_idxs = collect(keys(gen_lookup))
11093
branch_idxs = collect(keys(branch_lookup))
@@ -115,7 +98,6 @@ function build_ne_sets(data::Dict{AbstractString,Any})
11598
return TNEPDataSets(branch_lookup, branch_idxs, arcs_from, arcs_to, arcs, bus_branches, buspairs, buspair_indexes)
11699
end
117100

118-
119101
function process_raw_mp_ne_data(data::Dict{AbstractString,Any})
120102
# TODO, see if there is a clean way of reusing 'process_raw_mp_data'
121103
# would be fine, except for on/off phase angle calc
@@ -141,10 +123,6 @@ function process_raw_mp_ne_data(data::Dict{AbstractString,Any})
141123
return data, sets, ext
142124
end
143125

144-
145-
146-
147-
148126
function get_tnep_solution{T}(pm::GenericPowerModel{T})
149127
sol = Dict{AbstractString,Any}()
150128
add_bus_voltage_setpoint(sol, pm)
@@ -155,6 +133,23 @@ function get_tnep_solution{T}(pm::GenericPowerModel{T})
155133
return sol
156134
end
157135

136+
function add_branch_ne_setpoint{T}(sol, pm::GenericPowerModel{T})
137+
add_setpoint(sol, pm, "branch_ne", "index", "built", :line_ne; default_value = (item) -> 1)
138+
end
139+
140+
function add_branch_flow_setpoint_ne{T}(sol, pm::GenericPowerModel{T})
141+
# check the line flows were requested
142+
if haskey(pm.setting, "output") && haskey(pm.setting["output"], "line_flows") && pm.setting["output"]["line_flows"] == true
143+
mva_base = pm.data["baseMVA"]
144+
145+
add_setpoint(sol, pm, "branch_ne", "index", "p_from", :p_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["f_bus"], item["t_bus"])])
146+
add_setpoint(sol, pm, "branch_ne", "index", "q_from", :q_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["f_bus"], item["t_bus"])])
147+
add_setpoint(sol, pm, "branch_ne", "index", "p_to", :p_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["t_bus"], item["f_bus"])])
148+
add_setpoint(sol, pm, "branch_ne", "index", "q_to", :q_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["t_bus"], item["f_bus"])])
149+
end
150+
end
151+
152+
158153
#### TNEP specific variables
159154

160155
## Variables associated with building new lines
@@ -192,39 +187,26 @@ function variable_complex_voltage_product_ne{T}(pm::GenericPowerModel{T})
192187
wr_min, wr_max, wi_min, wi_max = compute_voltage_product_bounds(pm.ext[:ne].buspairs, pm.ext[:ne].buspair_indexes)
193188
bi_bp = Dict([(i, (b["f_bus"], b["t_bus"])) for (i,b) in pm.ext[:ne].branches])
194189
@variable(pm.model, min(0, wr_min[bi_bp[b]]) <= wr_ne[b in pm.ext[:ne].branch_indexes] <= max(0, wr_max[bi_bp[b]]), start = getstart(pm.ext[:ne].buspairs, bi_bp[b], "wr_start", 1.0))
195-
@variable(pm.model, min(0, wi_min[bi_bp[b]]) <= wi_ne[b in pm.ext[:ne].branch_indexes] <= max(0, wi_max[bi_bp[b]]), start = getstart(pm.ext[:ne].buspairs, bi_bp[b], "wr_start"))
190+
@variable(pm.model, min(0, wi_min[bi_bp[b]]) <= wi_ne[b in pm.ext[:ne].branch_indexes] <= max(0, wi_max[bi_bp[b]]), start = getstart(pm.ext[:ne].buspairs, bi_bp[b], "wi_start"))
196191
return wr_ne, wi_ne
197192
end
198193

199-
function variable_active_line_flow_ne{T}(pm::GenericPowerModel{T}; bounded = true)
200-
if bounded
201-
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= p_ne[(l,i,j) in pm.ext[:ne].arcs] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "p_start"))
202-
else
203-
@variable(pm.model, p[(l,i,j) in pm.ext[:ne].arcs], start = getstart(pm.ext[:ne].branches, l, "p_start"))
204-
end
194+
function variable_active_line_flow_ne{T}(pm::GenericPowerModel{T})
195+
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= p_ne[(l,i,j) in pm.ext[:ne].arcs] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "p_start"))
205196
return p_ne
206197
end
207198

208-
function variable_active_line_flow_ne{T <: StandardDCPForm}(pm::GenericPowerModel{T}; bounded = true)
209-
if bounded
210-
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= p_ne[(l,i,j) in pm.ext[:ne].arcs_from] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "p_start"))
211-
else
212-
@variable(pm.model, p_ne[(l,i,j) in pm.ext[:ne].arcs_from], start = getstart(pm.ext[:ne].branches, l, "p_start"))
213-
end
214-
199+
function variable_active_line_flow_ne{T <: StandardDCPForm}(pm::GenericPowerModel{T})
200+
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= p_ne[(l,i,j) in pm.ext[:ne].arcs_from] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "p_start"))
201+
215202
p_ne_expr = Dict([((l,i,j), 1.0*p_ne[(l,i,j)]) for (l,i,j) in pm.ext[:ne].arcs_from])
216203
p_ne_expr = merge(p_ne_expr, Dict([((l,j,i), -1.0*p_ne[(l,i,j)]) for (l,i,j) in pm.ext[:ne].arcs_from]))
217204

218205
pm.model.ext[:p_ne_expr] = p_ne_expr
219206
end
220207

221-
222-
function variable_reactive_line_flow_ne{T}(pm::GenericPowerModel{T}; bounded = true)
223-
if bounded
224-
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= q_ne[(l,i,j) in pm.ext[:ne].arcs] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "q_start"))
225-
else
226-
@variable(pm.model, q_ne[(l,i,j) in pm.ext[:ne].arcs], start = getstart(pm.ext[:ne].branches, l, "q_start"))
227-
end
208+
function variable_reactive_line_flow_ne{T}(pm::GenericPowerModel{T})
209+
@variable(pm.model, -pm.ext[:ne].branches[l]["rate_a"] <= q_ne[(l,i,j) in pm.ext[:ne].arcs] <= pm.ext[:ne].branches[l]["rate_a"], start = getstart(pm.ext[:ne].branches, l, "q_start"))
228210
return q_ne
229211
end
230212

@@ -233,7 +215,6 @@ function variable_reactive_line_flow_ne{T <: AbstractDCPForm}(pm::GenericPowerMo
233215
end
234216

235217

236-
237218
#### TNEP specific objectives
238219

239220
### Cost of building lines
@@ -243,6 +224,7 @@ function objective_tnep_cost{T}(pm::GenericPowerModel{T})
243224
return @objective(pm.model, Min, sum{ branches[i]["construction_cost"]*line_ne[i], (i,branch) in branches} )
244225
end
245226

227+
246228
#### TNEP specific constraints
247229

248230
function constraint_active_ohms_yt_ne{T <: AbstractACPForm}(pm::GenericPowerModel{T}, branch)
@@ -648,22 +630,4 @@ end
648630

649631

650632

651-
##### TNEP specific solution extractors
652-
function add_branch_ne_setpoint{T}(sol, pm::GenericPowerModel{T})
653-
add_setpoint(sol, pm, "branch_ne", "index", "built", :line_ne; default_value = (item) -> 1)
654-
end
655-
656-
function add_branch_flow_setpoint_ne{T}(sol, pm::GenericPowerModel{T})
657-
# check the line flows were requested
658-
if haskey(pm.setting, "output") && haskey(pm.setting["output"], "line_flows") && pm.setting["output"]["line_flows"] == true
659-
mva_base = pm.data["baseMVA"]
660-
661-
add_setpoint(sol, pm, "branch_ne", "index", "p_from", :p_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["f_bus"], item["t_bus"])])
662-
add_setpoint(sol, pm, "branch_ne", "index", "q_from", :q_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["f_bus"], item["t_bus"])])
663-
add_setpoint(sol, pm, "branch_ne", "index", "p_to", :p_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["t_bus"], item["f_bus"])])
664-
add_setpoint(sol, pm, "branch_ne", "index", "q_to", :q_ne; scale = (x,item) -> x*mva_base, extract_var = (var,idx,item) -> var[(idx, item["t_bus"], item["f_bus"])])
665-
end
666-
end
667-
668-
669633

test/runtests.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ ipopt_solver = IpoptSolver(tol=1e-6, print_level=0)
2626
pajarito_solver = PajaritoSolver(mip_solver=GLPKSolverMIP(), cont_solver=ipopt_solver, log_level=0)
2727
scs_solver = SCSSolver(max_iters=1000000, verbose=0)
2828

29-
#include("output.jl")
29+
include("output.jl")
3030

31-
#include("matpower.jl")
31+
include("matpower.jl")
3232

3333
# used by OTS and Loadshed TS models
3434
function check_br_status(sol)
@@ -37,12 +37,12 @@ function check_br_status(sol)
3737
end
3838
end
3939

40-
#include("pf.jl")
40+
include("pf.jl")
4141

42-
#include("opf.jl")
42+
include("opf.jl")
4343

44-
#include("misc.jl")
44+
include("misc.jl")
4545

46-
#include("ots.jl")
46+
include("ots.jl")
4747

4848
include("tnep.jl")

0 commit comments

Comments
 (0)