Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Jan 30, 2024
1 parent 6ed4c35 commit 04fee93
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 136 deletions.
25 changes: 0 additions & 25 deletions src/core/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -811,31 +811,6 @@ function _make_full_matrix_variable(diag::Vector{T}, lowertriangle::Vector{T}, u
return matrix
end

# TODO refactor into several functions
"helper to determine if expession has any Nonlinear terms"
function _has_nl_expression(x)::Bool
if isa(x, JuMP.NonlinearExpression)
return true
elseif isa(x, Array)
if any(_has_nl_expression.(x))
return true
end
elseif isa(x, Dict)
for i in values(x)
if _has_nl_expression(i)
return true
end
end
elseif isa(x, JuMP.Containers.DenseAxisArray)
for i in values(x.data)
if _has_nl_expression(i)
return true
end
end
end
return false
end


"""
correct_mc_voltage_angle_differences!(data::Dict{String,<:Any}, default_pad::Real=deg2rad(10.0))
Expand Down
64 changes: 17 additions & 47 deletions src/core/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,59 +248,29 @@ end

"gen connections adaptation of min fuel cost polynomial linquad objective"
function _objective_mc_min_fuel_cost_polynomial_linquad(pm::AbstractUnbalancedPowerModel; report::Bool=true)
pg_contains_nl_exp = any(x<:JuMP.NonlinearExpression for x in vcat([typeof.(isa(pg, JuMP.Containers.DenseAxisArray) ? pg.data : pg) for nw in nw_ids(pm) for (id,pg) in var(pm, nw, :pg)]...))
gen_cost = Dict()

if !pg_contains_nl_exp
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum(var(pm, n, :pg, i))

if length(gen["cost"]) == 1
gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
gen_cost[(n,i)] = gen["cost"][1]*pg + gen["cost"][2]
elseif length(gen["cost"]) == 3
gen_cost[(n,i)] = gen["cost"][1]*pg^2 + gen["cost"][2]*pg + gen["cost"][3]
else
gen_cost[(n,i)] = 0.0
end
end
end
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
pg = sum(var(pm, n, :pg, i))

return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
)
else
for (n, nw_ref) in nws(pm)
for (i,gen) in nw_ref[:gen]
bus = gen["gen_bus"]

#to avoid function calls inside of @NLconstraint:
pg = var(pm, n, :pg, i)
pg = isa(pg, JuMP.Containers.DenseAxisArray) ? pg.data : pg

int_dim = length(pg)
if length(gen["cost"]) == 1
gen_cost[(n,i)] = gen["cost"][1]
elseif length(gen["cost"]) == 2
gen_cost[(n,i)] = JuMP.@expression(pm.model, gen["cost"][1]*sum(pg[i] for i in 1:int_dim) + gen["cost"][2])
elseif length(gen["cost"]) == 3
gen_cost[(n,i)] = JuMP.@expression(pm.model, gen["cost"][1]*sum(pg[i] for i in 1:int_dim)^2 + gen["cost"][2]*sum(pg[i] for i in 1:int_dim) + gen["cost"][3])
else
gen_cost[(n,i)] = 0.0
end
if length(gen["cost"]) == 1
gen_cost[(n,i)] = gen["cost"][1]

Check warning on line 258 in src/core/objective.jl

View check run for this annotation

Codecov / codecov/patch

src/core/objective.jl#L258

Added line #L258 was not covered by tests
elseif length(gen["cost"]) == 2
gen_cost[(n,i)] = gen["cost"][1]*pg + gen["cost"][2]
elseif length(gen["cost"]) == 3
gen_cost[(n,i)] = gen["cost"][1]*pg^2 + gen["cost"][2]*pg + gen["cost"][3]

Check warning on line 262 in src/core/objective.jl

View check run for this annotation

Codecov / codecov/patch

src/core/objective.jl#L261-L262

Added lines #L261 - L262 were not covered by tests
else
gen_cost[(n,i)] = 0.0

Check warning on line 264 in src/core/objective.jl

View check run for this annotation

Codecov / codecov/patch

src/core/objective.jl#L264

Added line #L264 was not covered by tests
end
end

return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
)
end

return JuMP.@objective(pm.model, Min,
sum(
sum( gen_cost[(n,i)] for (i,gen) in nw_ref[:gen] )
for (n, nw_ref) in nws(pm))
)
end


Expand Down
18 changes: 3 additions & 15 deletions src/core/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ function _IM.solution_preprocessor(pm::AbstractUnbalancedPowerModel, solution::D
end


"custom `build_solution_values` for multiconductor (vector) variables"
function _IM.build_solution_values(var::JuMP.Containers.DenseAxisArray{<:JuMP.VariableRef,1})
return JuMP.value.(var.data)
end


"custom `build_solution_values` for multiconductor (vector) nonlinear expressions"
function _IM.build_solution_values(var::JuMP.Containers.DenseAxisArray{<:JuMP.NonlinearExpression,1})
return JuMP.value.(var.data)
end


"custom `build_solution_values` for multiconductor (vector) generic affine expressions"
function _IM.build_solution_values(var::JuMP.Containers.DenseAxisArray{<:JuMP.GenericAffExpr,1})
return JuMP.value.(var.data)
"custom `build_solution_values` for multiconductor (vector) JuMP expressions"
function _IM.build_solution_values(var::JuMP.AbstractJuMPScalar)
return JuMP.value(var)
end


Expand Down
18 changes: 9 additions & 9 deletions src/form/acp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function constraint_mc_power_balance_capc(pm::AbstractUnbalancedACPModel, nw::In
cap_state = 1.0
ncnds = length(terminals)
Gs = fill(0.0, ncnds, ncnds)
Bs = convert(Matrix{JuMP.NonlinearExpression}, JuMP.@expression(pm.model, [idx=1:ncnds, jdx=1:ncnds], 0.0))
Bs = convert(Matrix{JuMP.NonlinearExpr}, JuMP.@expression(pm.model, [idx=1:ncnds, jdx=1:ncnds], 0.0))
for (val, connections) in bus_shunts
shunt = ref(pm,nw,:shunt,val)
for (idx,c) in enumerate(connections)
Expand Down Expand Up @@ -1083,8 +1083,8 @@ function constraint_mc_load_power_wye(pm::AbstractUnbalancedACPModel, nw::Int, i
pd_bus = a
qd_bus = b
else
pd_bus = Vector{JuMP.NonlinearExpression}([])
qd_bus = Vector{JuMP.NonlinearExpression}([])
pd_bus = JuMP.NonlinearExpr[]
qd_bus = JuMP.NonlinearExpr[]

for (idx, c) in enumerate(connections)
crd = JuMP.@expression(pm.model,
Expand All @@ -1111,8 +1111,8 @@ function constraint_mc_load_power_wye(pm::AbstractUnbalancedACPModel, nw::Int, i
sol(pm, nw, :load, id)[:pd_bus] = pd_bus
sol(pm, nw, :load, id)[:qd_bus] = qd_bus

pd = Vector{JuMP.NonlinearExpression}([])
qd = Vector{JuMP.NonlinearExpression}([])
pd = JuMP.NonlinearExpr[]
qd = JuMP.NonlinearExpr[]

for (idx,c) in enumerate(connections)
push!(pd, JuMP.@expression(pm.model, a[idx]*vm[c]^alpha[idx] ))
Expand Down Expand Up @@ -1168,8 +1168,8 @@ function constraint_mc_load_power_delta(pm::AbstractUnbalancedACPModel, nw::Int,
end
end

pd_bus = Vector{JuMP.NonlinearExpression}([])
qd_bus = Vector{JuMP.NonlinearExpression}([])
pd_bus = JuMP.NonlinearExpr[]
qd_bus = JuMP.NonlinearExpr[]
for (idx,c) in enumerate(conn_bus)
push!(pd_bus, JuMP.@expression(pm.model, vm[c]*cos(va[c])*crd_bus[c]+vm[c]*sin(va[c])*cid_bus[c]))
push!(qd_bus, JuMP.@expression(pm.model, -vm[c]*cos(va[c])*cid_bus[c]+vm[c]*sin(va[c])*crd_bus[c]))
Expand Down Expand Up @@ -1237,8 +1237,8 @@ function constraint_mc_generator_power_delta(pm::AbstractUnbalancedACPModel, nw:
end
end

pg_bus = Vector{JuMP.NonlinearExpression}([])
qg_bus = Vector{JuMP.NonlinearExpression}([])
pg_bus = JuMP.NonlinearExpr[]
qg_bus = JuMP.NonlinearExpr[]
for c in conn_bus
push!(pg_bus, JuMP.@expression(pm.model, vm[c]*cos(va[c])*crg_bus[c]+vm[c]*sin(va[c])*cig_bus[c]))
push!(qg_bus, JuMP.@expression(pm.model, -vm[c]*cos(va[c])*cig_bus[c]+vm[c]*sin(va[c])*crg_bus[c]))
Expand Down
22 changes: 11 additions & 11 deletions src/form/acr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ function constraint_mc_power_balance_capc(pm::AbstractUnbalancedACRModel, nw::In
# calculate Gs, Bs
ncnds = length(terminals)
Gt = fill(0.0, ncnds, ncnds)
Bt = convert(Matrix{JuMP.NonlinearExpression}, JuMP.@expression(pm.model, [idx=1:ncnds, jdx=1:ncnds], 0.0))
Bt = convert(Matrix{JuMP.Nonlinear}, JuMP.@expression(pm.model, [idx=1:ncnds, jdx=1:ncnds], 0.0))
for (val, connections) in bus_shunts
shunt = ref(pm,nw,:shunt,val)
for (idx,c) in enumerate(connections)
Expand Down Expand Up @@ -780,8 +780,8 @@ function constraint_mc_load_power_wye(pm::AbstractUnbalancedACRModel, nw::Int, i
pd_bus = a
qd_bus = b
else
pd_bus = Vector{JuMP.NonlinearExpression}([])
qd_bus = Vector{JuMP.NonlinearExpression}([])
pd_bus = JuMP.NonlinearExpr[]
qd_bus = JuMP.NonlinearExpr[]

for (idx,c) in enumerate(connections)
crd = JuMP.@expression(pm.model, a[idx]*vr[c]*(vr[c]^2+vi[c]^2)^(alpha[idx]/2-1)+b[idx]*vi[c]*(vr[c]^2+vi[c]^2)^(beta[idx]/2 -1))
Expand All @@ -802,8 +802,8 @@ function constraint_mc_load_power_wye(pm::AbstractUnbalancedACRModel, nw::Int, i
sol(pm, nw, :load, id)[:pd_bus] = pd_bus
sol(pm, nw, :load, id)[:qd_bus] = qd_bus

pd = Vector{JuMP.NonlinearExpression}([])
qd = Vector{JuMP.NonlinearExpression}([])
pd = JuMP.NonlinearExpr[]
qd = JuMP.NonlinearExpr[]

for (idx,c) in enumerate(connections)
push!(pd, JuMP.@expression(pm.model, a[idx]*(vr[c]^2+vi[c]^2)^(alpha[idx]/2) ))
Expand Down Expand Up @@ -853,8 +853,8 @@ function constraint_mc_load_power_delta(pm::AbstractUnbalancedACRModel, nw::Int,
end
end

pd_bus = Vector{JuMP.NonlinearExpression}([])
qd_bus = Vector{JuMP.NonlinearExpression}([])
pd_bus = JuMP.NonlinearExpr[]
qd_bus = JuMP.NonlinearExpr[]
for (idx,c) in enumerate(conn_bus)
push!(pd_bus, JuMP.@expression(pm.model, vr[c]*crd_bus[c]+vi[c]*cid_bus[c]))
push!(qd_bus, JuMP.@expression(pm.model, -vr[c]*cid_bus[c]+vi[c]*crd_bus[c]))
Expand All @@ -870,8 +870,8 @@ function constraint_mc_load_power_delta(pm::AbstractUnbalancedACRModel, nw::Int,
sol(pm, nw, :load, id)[:pd_bus] = pd_bus
sol(pm, nw, :load, id)[:qd_bus] = qd_bus

pd = Vector{JuMP.NonlinearExpression}([])
qd = Vector{JuMP.NonlinearExpression}([])
pd = JuMP.NonlinearExpr[]
qd = JuMP.NonlinearExpr[]
for (idx,c) in enumerate(connections)
push!(pd, JuMP.@expression(pm.model, a[idx]*(vrd[c]^2+vid[c]^2)^(alpha[idx]/2) ))
push!(qd, JuMP.@expression(pm.model, b[idx]*(vrd[c]^2+vid[c]^2)^(beta[idx]/2) ))
Expand Down Expand Up @@ -931,8 +931,8 @@ function constraint_mc_generator_power_delta(pm::AbstractUnbalancedACRModel, nw:
end
end

pg_bus = Vector{JuMP.NonlinearExpression}([])
qg_bus = Vector{JuMP.NonlinearExpression}([])
pg_bus = JuMP.NonlinearExpr[]
qg_bus = JuMP.NonlinearExpr[]
for (idx,c) in enumerate(conn_bus)
push!(pg_bus, JuMP.@expression(pm.model, vr[c]*crg_bus[c]+vi[c]*cig_bus[c]))
push!(qg_bus, JuMP.@expression(pm.model, -vr[c]*cig_bus[c]+vi[c]*crg_bus[c]))
Expand Down
4 changes: 2 additions & 2 deletions src/form/en_acr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ function constraint_mc_load_power_delta(pm::AbstractExplicitNeutralACRModel, nw:
sol(pm, nw, :load, id)[:pd_bus] = pd_bus
sol(pm, nw, :load, id)[:qd_bus] = qd_bus

pd = Vector{JuMP.NonlinearExpression}([])
qd = Vector{JuMP.NonlinearExpression}([])
pd = JuMP.NonlinearExpr[]
qd = JuMP.NonlinearExpr[]
for idx in 1:nph
push!(pd, JuMP.@expression(pm.model, a[idx]*(vrd[idx]^2+vid[idx]^2)^(alpha[idx]/2) ))
push!(qd, JuMP.@expression(pm.model, b[idx]*(vrd[idx]^2+vid[idx]^2)^(beta[idx]/2) ))
Expand Down
20 changes: 10 additions & 10 deletions src/form/en_ivr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ function constraint_mc_generator_power_wye(pm::AbstractNLExplicitNeutralIVRModel
phases = connections[1:end-1]
n = connections[end]

pg = Vector{JuMP.NonlinearExpression}([])
qg = Vector{JuMP.NonlinearExpression}([])
pg = JuMP.NonlinearExpr[]
qg = JuMP.NonlinearExpr[]

for (idx, p) in enumerate(phases)
push!(pg, JuMP.@expression(pm.model, (vr[p]-vr[n])*crg[idx]+(vi[p]-vi[n])*cig[idx]))
Expand Down Expand Up @@ -198,8 +198,8 @@ function constraint_mc_generator_power_delta(pm::AbstractNLExplicitNeutralIVRMod
vig[idx] = JuMP.@expression(pm.model, vi[c]-vi[d])
end

pg = Vector{JuMP.NonlinearExpression}([])
qg = Vector{JuMP.NonlinearExpression}([])
pg = JuMP.NonlinearExpr[]
qg = JuMP.NonlinearExpr[]
for idx in 1:nph
push!(pg, JuMP.@expression(pm.model, vrg[idx]*crg[idx]+vig[idx]*cig[idx]))
push!(qg, JuMP.@expression(pm.model, -vrg[idx]*cig[idx]+vig[idx]*crg[idx]))
Expand Down Expand Up @@ -499,8 +499,8 @@ function constraint_mc_load_current_wye(pm::AbstractExplicitNeutralIVRModel, nw:
vr = var(pm, nw, :vr, bus_id)
vi = var(pm, nw, :vi, bus_id)

crd = Vector{JuMP.NonlinearExpression}([])
cid = Vector{JuMP.NonlinearExpression}([])
crd = JuMP.NonlinearExpr[]
cid = JuMP.NonlinearExpr[]

phases = connections[1:end-1]
n = connections[end]
Expand All @@ -526,8 +526,8 @@ function constraint_mc_load_current_wye(pm::AbstractExplicitNeutralIVRModel, nw:
var(pm, nw, :cid_bus)[id] = cid_bus = _merge_bus_flows(pm, [cid..., cid_bus_n], connections)

if report
pd_bus = Vector{JuMP.NonlinearExpression}([])
qd_bus = Vector{JuMP.NonlinearExpression}([])
pd_bus = JuMP.NonlinearExpr[]
qd_bus = JuMP.NonlinearExpr[]
for (idx,c) in enumerate(connections)
push!(pd_bus, JuMP.@expression(pm.model, vr[c]*crd_bus[c]+vi[c]*cid_bus[c]))
push!(qd_bus, JuMP.@expression(pm.model, -vr[c]*cid_bus[c]+vi[c]*crd_bus[c]))
Expand All @@ -542,8 +542,8 @@ function constraint_mc_load_current_wye(pm::AbstractExplicitNeutralIVRModel, nw:
sol(pm, nw, :load, id)[:crd_bus] = crd_bus
sol(pm, nw, :load, id)[:cid_bus] = cid_bus

pd = Vector{JuMP.NonlinearExpression}([])
qd = Vector{JuMP.NonlinearExpression}([])
pd = JuMP.NonlinearExpr[]
qd = JuMP.NonlinearExpr[]
for (idx, p) in enumerate(phases)
push!(pd, JuMP.@expression(pm.model, a[idx]*(vr[p]^2+vi[p]^2)^(alpha[idx]/2) ))
push!(qd, JuMP.@expression(pm.model, b[idx]*(vr[p]^2+vi[p]^2)^(beta[idx]/2) ))
Expand Down
Loading

0 comments on commit 04fee93

Please sign in to comment.