# Get power flow equations from PowerModels?

**URL:** https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673
**Category:** Optimization (Mathematical)
**Created:** [January 22, 2020, 7:13pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673 "2020-01-22T19:13:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [January 22, 2020, 7:13pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673/1 "2020-01-22T19:13:07Z")

</div>

Is it possible to obtain the power flow equations in the form of f(x) = 0 from [`PowerModels.jl`](https://github.com/lanl-ansi/PowerModels.jl)?

Follow-up question, is it possible to get the system of nonlinear equations h(x) = 0 that make up a power flow problem, i.e. h(x) = 0, where h(x) = [f(x)^\top\!, \, g(x)]^\top, where f(x) are the said power flow equations, and h(x) are the bus specifications?

Perhaps @ccoffrin can help me out? Thanks!

---

<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: [January 22, 2020, 8:07pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673/2 "2020-01-22T20:07:19Z")

</div>

Hi @timueh! Thanks for your question.

As of PowerModels v0.14, PowerModels focuses on building JuMP models (with DC Power Flow being the only dedicated routine that does not require JuMP). So it is not easy to extract the system of equations into Julia functions, such as `f(x)`. If you would like to quickly inspect the JuMP model you can do this,

```julia
using PowerModels; using JuMP; using Ipopt
m = Model()
result = run_ac_opf("case.m", with_optimizer(Ipopt.Optimizer), jump_model=m)
println(m)

```

This issue may be of interest, [Dedicated AC Power Flow Solver · Issue #590 · lanl-ansi/PowerModels.jl · GitHub](https://github.com/lanl-ansi/PowerModels.jl/issues/590), which seeks to develop a dedicated AC Power Flow solver in PowerModels that would not require JuMP, which could be used to extract the functions you are interested in.

You might find the `calc_power_balance` and `calc_branch_flow_ac` functions useful, which simply evaluate the power flow equations on the data model.

---

<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: [April 24, 2020, 3:34pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673/3 "2020-04-24T15:34:25Z")

</div>

Some new documentation on this point, [Power Flow · PowerModels](https://lanl-ansi.github.io/PowerModels.jl/stable/power-flow/)

---

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [April 24, 2020, 6:24pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673/4 "2020-04-24T18:24:37Z")

</div>

Thanks for the great work! Unfortunately, this comes three months too late for an industry project, but I’ll definitely bear that in mind for future reference.

I very much like that you compute the Jacobian as well!

Related to the original question, if I were to return the function `f!`, that gives me the desired equations.

> <https://github.com/lanl-ansi/PowerModels.jl/blob/07a4925d52f857bf0726e7bd53b1e02eccdcb148/src/prob/pf.jl#L484>

---

<div class="post-metadata">

### Author: ![timueh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timueh/32/12379_2.png) [@timueh](https://discourse.julialang.org/u/timueh)
#### Post date: [April 24, 2020, 6:28pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673/5 "2020-04-24T18:28:52Z")

</div>

Oh, one more thing: what’s the dimension of `F` and `x`. Is it like in matpower that x \in \mathbb{R}^{n\_{pv} + 2n\_{pq}}?

---

<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: [April 24, 2020, 7:27pm UTC](https://discourse.julialang.org/t/get-power-flow-equations-from-powermodels/33673/6 "2020-04-24T19:27:07Z")

</div>

The dimension of `F` and `x` is `2*|N|` where N is the number of buses. What each varibales means varies by the bus type.
