# Power Balance reference in PowerModels.jl

**URL:** <https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865>\
**Category:** Optimization (Mathematical)\
**Created:** [August 22, 2019, 8:24pm UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865 "2019-08-22T20:24:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewrosemberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewrosemberg/32/9902_2.png) [@andrewrosemberg](https://discourse.julialang.org/u/andrewrosemberg)\
**Post date:** [August 22, 2019, 8:24pm UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865/1 "2019-08-22T20:24:10Z")

</div>

I’m running an OPF problem with SDPWRMForm and SOCWRConicForm formulations (AbstractWForms) and I’m trying to retrieve the duals from the power balance constraints (lam\_kcl\_r in the output). However, it seams that the reference to those constraints (kcl\_p) is not stored as it is in the DCPlosslessForm (in the “con” dictionary).

Could the solution be to change the power balance constraint in [“shared form”](https://github.com/lanl-ansi/PowerModels.jl/blob/52e39620093a80997ef2e1f9cb3996f5ce89def5/src/form/shared.jl#L58) to:

```julia
function constraint_power_balance_shunt(pm::GenericPowerModel{T}, n::Int, c::Int, i, bus_arcs, bus_arcs_dc, bus_gens, bus_pd, bus_qd, bus_gs, bus_bs) where T <: AbstractWForms
    w = var(pm, n, c, :w, i)
    pg = var(pm, n, c, :pg)
    qg = var(pm, n, c, :qg)
    p = var(pm, n, c, :p)
    q = var(pm, n, c, :q)
    p_dc = var(pm, n, c, :p_dc)
    q_dc = var(pm, n, c, :q_dc)

    con(pm, n, c, :kcl_p)[i] = JuMP.@constraint(pm.model, sum(p[a] for a in bus_arcs) + sum(p_dc[a_dc] for a_dc in bus_arcs_dc) == sum(pg[g] for g in bus_gens) - sum(pd for pd in values(bus_pd)) - sum(gs for gs in values(bus_gs))*w)
    con(pm, n, c, :kcl_q)[i] = JuMP.@constraint(pm.model, sum(q[a] for a in bus_arcs) + sum(q_dc[a_dc] for a_dc in bus_arcs_dc) == sum(qg[g] for g in bus_gens) - sum(qd for qd in values(bus_qd)) + sum(bs for bs in values(bus_bs))*w)
end

```

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [August 22, 2019, 10:54pm UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865/2 "2019-08-22T22:54:46Z")

</div>

Thus far I have not tested extracting duals from the SOC or SDP models, so it would not surprise me if there are bugs there. If you find one, feel free to post an issue in the PowerModels repo.

Your suggestion appears to be correct in this case, you need to add `con(pm, n, c, :kcl_p)[i]=...` and `con(pm, n, c, :kcl_q)[i]=...` to the W-space `constraint_power_balance_shunt ` constraint.

---

<div class="post-metadata">

**Author:** ![andrewrosemberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewrosemberg/32/9902_2.png) [@andrewrosemberg](https://discourse.julialang.org/u/andrewrosemberg)\
**Post date:** [August 22, 2019, 11:49pm UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865/3 "2019-08-22T23:49:47Z")

</div>

Thanks @ccoffrin! I will fork the repo to test the idea and, if it works, I will create the PR.

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [August 29, 2019, 4:19pm UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865/4 "2019-08-29T16:19:03Z")

</div>

This should be fixed as of PowerModels v0.12.5. Let me know if you still have any issue.

---

<div class="post-metadata">

**Author:** ![andrewrosemberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrewrosemberg/32/9902_2.png) [@andrewrosemberg](https://discourse.julialang.org/u/andrewrosemberg)\
**Post date:** [August 29, 2019, 5:01pm UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865/5 "2019-08-29T17:01:45Z")

</div>

The issue seams to be resolved. At least in my case! Thanks again @ccoffrin!

---

<div class="post-metadata">

**Author:** ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)\
**Post date:** [August 30, 2019, 4:19am UTC](https://discourse.julialang.org/t/power-balance-reference-in-powermodels-jl/27865/6 "2019-08-30T04:19:20Z")

</div>

Glad to hear it @andrewrosemberg!
