Skip to content

Commit 6221bb3

Browse files
authored
Some Minor Fixes (#331)
* update calc_branch_t to use function dispatch instead of map (close #271 ) * rename mp functions to mc functions * rename mpv to mcv and reduce use of getmcv (close #295) * adding doc string to post_tp_opf in mc tests
1 parent 3431f68 commit 6221bb3

10 files changed

+124
-124
lines changed

src/core/data.jl

+12-12
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ function calc_branch_t(branch::Dict{String,Any})
5555
tap_ratio = branch["tap"]
5656
angle_shift = branch["shift"]
5757

58-
tr = map(*, tap_ratio, map(cos, angle_shift))
59-
ti = map(*, tap_ratio, map(sin, angle_shift))
58+
tr = tap_ratio.*cos.(angle_shift)
59+
ti = tap_ratio.*sin.(angle_shift)
6060

6161
return tr, ti
6262
end
@@ -771,29 +771,29 @@ function _check_cost_functions(id, comp)
771771
for c in 1:length(comp["ncost"])
772772
cnd_str = length(comp["ncost"]) > 1 ? "conductor $(c) " : ""
773773
if comp["model"][c] == 1
774-
if length(PowerModels.getmpv(comp["cost"], c)) != 2*comp["ncost"][c]
775-
error("$(cnd_str)ncost of $(comp["ncost"][c]) not consistent with $(length(PowerModels.getmpv(comp["cost"], c))) cost values")
774+
if length(PowerModels.getmcv(comp["cost"], c)) != 2*comp["ncost"][c]
775+
error("$(cnd_str)ncost of $(comp["ncost"][c]) not consistent with $(length(PowerModels.getmcv(comp["cost"], c))) cost values")
776776
end
777-
if length(PowerModels.getmpv(comp["cost"], c)) < 4
777+
if length(PowerModels.getmcv(comp["cost"], c)) < 4
778778
error("$(cnd_str)cost includes $(comp["ncost"][c]) points, but at least two points are required")
779779
end
780-
for i in 3:2:length(PowerModels.getmpv(comp["cost"], c))
781-
if PowerModels.getmpv(comp["cost"], c)[i-2] >= PowerModels.getmpv(comp["cost"], c)[i]
780+
for i in 3:2:length(PowerModels.getmcv(comp["cost"], c))
781+
if PowerModels.getmcv(comp["cost"], c)[i-2] >= PowerModels.getmcv(comp["cost"], c)[i]
782782
error("non-increasing x values in $(cnd_str)pwl cost model")
783783
end
784784
end
785785
if "pmin" in keys(comp) && "pmax" in keys(comp)
786786
pmin = comp["pmin"][c]
787787
pmax = comp["pmax"][c]
788-
for i in 3:2:length(PowerModels.getmpv(comp["cost"], c))
789-
if PowerModels.getmpv(comp["cost"], c)[i] < pmin || PowerModels.getmpv(comp["cost"], c)[i] > pmax
790-
warn(LOGGER, "$(cnd_str)pwl x value $(PowerModels.getmpv(comp["cost"], c)[i]) is outside the generator bounds $(pmin)-$(pmax)")
788+
for i in 3:2:length(PowerModels.getmcv(comp["cost"], c))
789+
if PowerModels.getmcv(comp["cost"], c)[i] < pmin || PowerModels.getmcv(comp["cost"], c)[i] > pmax
790+
warn(LOGGER, "$(cnd_str)pwl x value $(PowerModels.getmcv(comp["cost"], c)[i]) is outside the generator bounds $(pmin)-$(pmax)")
791791
end
792792
end
793793
end
794794
elseif comp["model"][c] == 2
795-
if length(PowerModels.getmpv(comp["cost"], c)) != comp["ncost"][c]
796-
error("$(cnd_str)ncost of $(comp["ncost"][c]) not consistent with $(length(PowerModels.getmpv(comp["cost"], c))) cost values")
795+
if length(PowerModels.getmcv(comp["cost"], c)) != comp["ncost"][c]
796+
error("$(cnd_str)ncost of $(comp["ncost"][c]) not consistent with $(length(PowerModels.getmcv(comp["cost"], c))) cost values")
797797
end
798798
else
799799
warn(LOGGER, "Unknown $(cnd_str)generator cost model of type $(comp["model"][c])")

src/core/multiconductor.jl

+21-21
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ end
1414
MultiConductorVector(value::T, conductors::Int) where T = MultiConductorVector([value for i in 1:conductors])
1515
Base.map(f, a::MultiConductorVector{T}) where T = MultiConductorVector{T}(map(f, a.values))
1616
Base.map(f, a::MultiConductorVector{T}, b::MultiConductorVector{T}) where T = MultiConductorVector{T}(map(f, a.values, b.values))
17-
conductors(mpv::MultiConductorVector) = length(mpv.values)
17+
conductors(mcv::MultiConductorVector) = length(mcv.values)
1818

1919

2020
""
21-
function Base.setindex!(mpv::MultiConductorVector{T}, v::T, i::Int) where T
22-
mpv.values[i] = v
21+
function Base.setindex!(mcv::MultiConductorVector{T}, v::T, i::Int) where T
22+
mcv.values[i] = v
2323
end
2424

2525

@@ -32,24 +32,24 @@ end
3232
MultiConductorMatrix(value::T, conductors::Int) where T = MultiConductorMatrix(value*eye(conductors))
3333
Base.map(f, a::MultiConductorMatrix{T}) where T = MultiConductorMatrix{T}(map(f, a.values))
3434
Base.map(f, a::MultiConductorMatrix{T}, b::MultiConductorMatrix{T}) where T = MultiConductorMatrix{T}(map(f, a.values, b.values))
35-
conductors(mpv::MultiConductorMatrix) = size(mpv.values, 1)
35+
conductors(mcv::MultiConductorMatrix) = size(mcv.values, 1)
3636

3737

3838
""
39-
function Base.setindex!(mpv::MultiConductorMatrix{T}, v::T, i::Int, j::Int) where T
40-
mpv.values[i,j] = v
39+
function Base.setindex!(mcv::MultiConductorMatrix{T}, v::T, i::Int, j::Int) where T
40+
mcv.values[i,j] = v
4141
end
4242

4343

44-
Base.start(mpv::MultiConductorValue) = start(mpv.values)
45-
Base.next(mpv::MultiConductorValue, state) = next(mpv.values, state)
46-
Base.done(mpv::MultiConductorValue, state) = done(mpv.values, state)
44+
Base.start(mcv::MultiConductorValue) = start(mcv.values)
45+
Base.next(mcv::MultiConductorValue, state) = next(mcv.values, state)
46+
Base.done(mcv::MultiConductorValue, state) = done(mcv.values, state)
4747

48-
Base.length(mpv::MultiConductorValue) = length(mpv.values)
49-
Base.size(mpv::MultiConductorValue, a...) = size(mpv.values, a...)
50-
Base.getindex(mpv::MultiConductorValue, args...) = mpv.values[args...]
48+
Base.length(mcv::MultiConductorValue) = length(mcv.values)
49+
Base.size(mcv::MultiConductorValue, a...) = size(mcv.values, a...)
50+
Base.getindex(mcv::MultiConductorValue, args...) = mcv.values[args...]
5151

52-
Base.show(io::IO, mpv::MultiConductorValue) = Base.show(io, mpv.values)
52+
Base.show(io::IO, mcv::MultiConductorValue) = Base.show(io, mcv.values)
5353

5454
Base.broadcast(f::Any, a::Any, b::MultiConductorValue) = broadcast(f, a, b.values)
5555
Base.broadcast(f::Any, a::MultiConductorValue, b::Any) = broadcast(f, a.values, b)
@@ -128,12 +128,12 @@ Base.rad2deg(a::MultiConductorMatrix) = MultiConductorMatrix(map(rad2deg, a.valu
128128
Base.deg2rad(a::MultiConductorVector) = MultiConductorVector(map(deg2rad, a.values))
129129
Base.deg2rad(a::MultiConductorMatrix) = MultiConductorMatrix(map(deg2rad, a.values))
130130

131-
JSON.lower(mpv::MultiConductorValue) = mpv.values
131+
JSON.lower(mcv::MultiConductorValue) = mcv.values
132132

133133

134134
"converts a MultiConductorValue value to a string in summary"
135-
function InfrastructureModels._value2string(mpv::MultiConductorValue, float_precision::Int)
136-
a = join([InfrastructureModels._value2string(v, float_precision) for v in mpv.values], ", ")
135+
function InfrastructureModels._value2string(mcv::MultiConductorValue, float_precision::Int)
136+
a = join([InfrastructureModels._value2string(v, float_precision) for v in mcv.values], ", ")
137137
return "[$(a)]"
138138
end
139139

@@ -147,8 +147,8 @@ function Base.isapprox(a::MultiConductorValue, b::MultiConductorValue; kwargs...
147147
end
148148

149149

150-
getmpv(value::Any, conductor::Int) = value
151-
getmpv(value::Any, conductor_i::Int, conductor_j::Int) = value
152-
getmpv(value::MultiConductorVector, conductor::Int) = value[conductor]
153-
getmpv(value::MultiConductorMatrix{T}, conductor::Int) where T = MultiConductorVector{T}(value[conductor])
154-
getmpv(value::MultiConductorMatrix, conductor_i::Int, conductor_j::Int) = value[conductor_i, conductor_j]
150+
getmcv(value::Any, conductor::Int) = value
151+
getmcv(value::Any, conductor_i::Int, conductor_j::Int) = value
152+
getmcv(value::MultiConductorVector, conductor::Int) = value[conductor]
153+
getmcv(value::MultiConductorMatrix{T}, conductor::Int) where T = MultiConductorVector{T}(value[conductor])
154+
getmcv(value::MultiConductorMatrix, conductor_i::Int, conductor_j::Int) = value[conductor_i, conductor_j]

src/core/ref.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function calc_voltage_product_bounds(buspairs, conductor::Int=1)
1010

1111
buspairs_conductor = Dict()
1212
for (bp, buspair) in buspairs
13-
buspairs_conductor[bp] = Dict([(k, getmpv(v, conductor)) for (k,v) in buspair])
13+
buspairs_conductor[bp] = Dict([(k, getmcv(v, conductor)) for (k,v) in buspair])
1414
end
1515

1616
for (bp, buspair) in buspairs_conductor

src/core/variable.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
function getval(comp::Dict{String,Any}, key::String, conductor::Int, default=0.0)
88
if haskey(comp, key)
99
vals = comp[key]
10-
return getmpv(vals, conductor)
10+
return getmcv(vals, conductor)
1111
end
1212
return default
1313
end

src/form/wr.jl

+12-12
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ function variable_voltage_magnitude_sqr_from_ne(pm::GenericPowerModel{T}; nw::In
284284
var(pm, nw, cnd)[:w_fr_ne] = @variable(pm.model,
285285
[i in ids(pm, nw, :ne_branch)], basename="$(nw)_$(cnd)_w_fr_ne",
286286
lowerbound = 0,
287-
upperbound = getmpv(buses[branches[i]["f_bus"]]["vmax"], cnd)^2,
287+
upperbound = (buses[branches[i]["f_bus"]]["vmax"][cnd])^2,
288288
start = getval(ref(pm, nw, :bus, i), "w_fr_start", cnd, 1.001)
289289
)
290290
end
@@ -297,7 +297,7 @@ function variable_voltage_magnitude_sqr_to_ne(pm::GenericPowerModel{T}; nw::Int=
297297
var(pm, nw, cnd)[:w_to_ne] = @variable(pm.model,
298298
[i in ids(pm, nw, :ne_branch)], basename="$(nw)_$(cnd)_w_to_ne",
299299
lowerbound = 0,
300-
upperbound = getmpv(buses[branches[i]["t_bus"]]["vmax"], cnd)^2,
300+
upperbound = (buses[branches[i]["t_bus"]]["vmax"][cnd])^2,
301301
start = getval(ref(pm, nw, :bus, i), "w_to", cnd, 1.001)
302302
)
303303
end
@@ -353,8 +353,8 @@ function variable_voltage_magnitude_product(pm::GenericPowerModel{T}; nw::Int=pm
353353
buspairs = ref(pm, nw, :buspairs)
354354
var(pm, nw, cnd)[:vv] = @variable(pm.model,
355355
[bp in keys(buspairs)], basename="$(nw)_$(cnd)_vv",
356-
lowerbound = getmpv(buspairs[bp]["vm_fr_min"], cnd)*getmpv(buspairs[bp]["vm_to_min"], cnd),
357-
upperbound = getmpv(buspairs[bp]["vm_fr_max"], cnd)*getmpv(buspairs[bp]["vm_to_max"], cnd),
356+
lowerbound = buspairs[bp]["vm_fr_min"][cnd]*buspairs[bp]["vm_to_min"][cnd],
357+
upperbound = buspairs[bp]["vm_fr_max"][cnd]*buspairs[bp]["vm_to_max"][cnd],
358358
start = getval(buspairs[bp], "vv_start", cnd, 1.0)
359359
)
360360
end
@@ -365,8 +365,8 @@ function variable_cosine(pm::GenericPowerModel{T}; nw::Int=pm.cnw, cnd::Int=pm.c
365365
cos_max = Dict([(bp, Inf) for bp in ids(pm, nw, :buspairs)])
366366

367367
for (bp, buspair) in ref(pm, nw, :buspairs)
368-
angmin = getmpv(buspair["angmin"], cnd)
369-
angmax = getmpv(buspair["angmax"], cnd)
368+
angmin = buspair["angmin"][cnd]
369+
angmax = buspair["angmax"][cnd]
370370
if angmin >= 0
371371
cos_max[bp] = cos(angmin)
372372
cos_min[bp] = cos(angmax)
@@ -404,7 +404,7 @@ function variable_current_magnitude_sqr(pm::GenericPowerModel{T}; nw::Int=pm.cnw
404404
buspairs = ref(pm, nw, :buspairs)
405405
ub = Dict()
406406
for (bp, buspair) in buspairs
407-
ub[bp] = (getmpv(buspair["rate_a"],cnd)*getmpv(buspair["tap"], cnd)/getmpv(buspair["vm_fr_min"], cnd))^2
407+
ub[bp] = ((buspair["rate_a"][cnd]*buspair["tap"][cnd])/buspair["vm_fr_min"][cnd])^2
408408
end
409409
var(pm, nw, cnd)[:cm] = @variable(pm.model,
410410
[bp in ids(pm, nw, :buspairs)], basename="$(nw)_$(cnd)_cm",
@@ -572,8 +572,8 @@ function variable_voltage_magnitude_product_on_off(pm::GenericPowerModel{T}; nw:
572572
branches = ref(pm, nw, :branch)
573573
buses = ref(pm, nw, :bus)
574574

575-
vv_min = Dict([(l, getmpv(buses[branch["f_bus"]]["vmin"], cnd)*getmpv(buses[branch["t_bus"]]["vmin"], cnd)) for (l, branch) in branches])
576-
vv_max = Dict([(l, getmpv(buses[branch["f_bus"]]["vmax"], cnd)*getmpv(buses[branch["t_bus"]]["vmax"], cnd)) for (l, branch) in branches])
575+
vv_min = Dict([(l, buses[branch["f_bus"]]["vmin"][cnd]*buses[branch["t_bus"]]["vmin"][cnd]) for (l, branch) in branches])
576+
vv_max = Dict([(l, buses[branch["f_bus"]]["vmax"][cnd]*buses[branch["t_bus"]]["vmax"][cnd]) for (l, branch) in branches])
577577

578578
var(pm, nw, cnd)[:vv] = @variable(pm.model,
579579
[l in ids(pm, nw, :branch)], basename="$(nw)_$(cnd)_vv",
@@ -590,8 +590,8 @@ function variable_cosine_on_off(pm::GenericPowerModel{T}; nw::Int=pm.cnw, cnd::I
590590
cos_max = Dict([(l, Inf) for l in ids(pm, nw, :branch)])
591591

592592
for (l, branch) in ref(pm, nw, :branch)
593-
angmin = getmpv(branch["angmin"], cnd)
594-
angmax = getmpv(branch["angmax"], cnd)
593+
angmin = branch["angmin"][cnd]
594+
angmax = branch["angmax"][cnd]
595595
if angmin >= 0
596596
cos_max[l] = cos(angmin)
597597
cos_min[l] = cos(angmax)
@@ -633,7 +633,7 @@ function variable_current_magnitude_sqr_on_off(pm::GenericPowerModel{T}; nw::Int
633633
cm_max = Dict()
634634
for (l, branch) in branches
635635
vm_fr_min = ref(pm, nw, :bus, branch["f_bus"], "vmin", cnd)
636-
cm_max[l] = (getmpv(branch["rate_a"],cnd)*getmpv(branch["tap"],cnd)/vm_fr_min)^2
636+
cm_max[l] = ((branch["rate_a"][cnd]*branch["tap"][cnd])/vm_fr_min)^2
637637
end
638638

639639
var(pm, nw, cnd)[:cm] = @variable(pm.model,

src/form/wrm.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ function variable_voltage(pm::GenericPowerModel{T}; nw::Int=pm.cnw, cnd::Int=pm.
3636
wi_ii = WR[w_idx,w_idx]
3737

3838
if bounded
39-
setlowerbound(wr_ii, getmpv(bus["vmin"], cnd)^2)
40-
setupperbound(wr_ii, getmpv(bus["vmax"], cnd)^2)
39+
setlowerbound(wr_ii, (bus["vmin"][cnd])^2)
40+
setupperbound(wr_ii, (bus["vmax"][cnd])^2)
4141

4242
#this breaks SCS on the 3 bus exmple
4343
#setlowerbound(wi_ii, 0)

src/prob/test.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ end
103103

104104

105105
""
106-
function run_mp_opf(file, model_constructor, solver; kwargs...)
107-
return run_generic_model(file, model_constructor, solver, post_mp_opf; multiconductor=true, kwargs...)
106+
function run_mc_opf(file, model_constructor, solver; kwargs...)
107+
return run_generic_model(file, model_constructor, solver, post_mc_opf; multiconductor=true, kwargs...)
108108
end
109109

110110
""
111-
function post_mp_opf(pm::GenericPowerModel)
111+
function post_mc_opf(pm::GenericPowerModel)
112112
for c in conductor_ids(pm)
113113
variable_voltage(pm, cnd=c)
114114
variable_generation(pm, cnd=c)
@@ -146,12 +146,12 @@ end
146146

147147

148148
""
149-
function run_mn_mp_opf(file, model_constructor, solver; kwargs...)
150-
return run_generic_model(file, model_constructor, solver, post_mn_mp_opf; multinetwork=true, multiconductor=true, kwargs...)
149+
function run_mn_mc_opf(file, model_constructor, solver; kwargs...)
150+
return run_generic_model(file, model_constructor, solver, post_mn_mc_opf; multinetwork=true, multiconductor=true, kwargs...)
151151
end
152152

153153
""
154-
function post_mn_mp_opf(pm::GenericPowerModel)
154+
function post_mn_mc_opf(pm::GenericPowerModel)
155155
for (n, network) in nws(pm)
156156
for c in conductor_ids(pm, nw=n)
157157
variable_voltage(pm, nw=n, cnd=c)

test/common.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ function build_mn_data(base_data_1, base_data_2)
3333
end
3434

3535

36-
function build_mp_data(base_data; conductors::Int=3)
36+
function build_mc_data(base_data; conductors::Int=3)
3737
mp_data = PowerModels.parse_file(base_data)
3838
PowerModels.make_multiconductor(mp_data, conductors)
3939
return mp_data
4040
end
4141

4242

43-
function build_mn_mp_data(base_data; replicates::Int=3, conductors::Int=3)
43+
function build_mn_mc_data(base_data; replicates::Int=3, conductors::Int=3)
4444
mp_data = PowerModels.parse_file(base_data)
4545
PowerModels.make_multiconductor(mp_data, conductors)
46-
mn_mp_data = InfrastructureModels.replicate(mp_data, replicates)
47-
for (nw, network) in mn_mp_data["nw"]
48-
network["conductors"] = mn_mp_data["conductors"]
46+
mn_mc_data = InfrastructureModels.replicate(mp_data, replicates)
47+
for (nw, network) in mn_mc_data["nw"]
48+
network["conductors"] = mn_mc_data["conductors"]
4949
end
50-
return mn_mp_data
50+
return mn_mc_data
5151
end
5252

53-
function build_mn_mp_data(base_data_1, base_data_2; conductors_1::Int=3, conductors_2::Int=3)
53+
function build_mn_mc_data(base_data_1, base_data_2; conductors_1::Int=3, conductors_2::Int=3)
5454
mp_data_1 = PowerModels.parse_file(base_data_1)
5555
mp_data_2 = PowerModels.parse_file(base_data_2)
5656

0 commit comments

Comments
 (0)