# Extracting the Power Flow equations from PowerModels

**URL:** https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165
**Category:** Optimization (Mathematical)
**Tags:** optimization, power-flow
**Created:** [December 5, 2023, 12:38pm UTC](https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165 "2023-12-05T12:38:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jim16](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jim16](https://discourse.julialang.org/u/Jim16)
#### Post date: [December 5, 2023, 12:38pm UTC](https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165/1 "2023-12-05T12:38:36Z")

</div>

Hello,

I’m relatively new to both Julia and PowerModels.jl, and I wish to compare different algorithms for solving power flow equations. My objective is to obtain the power flow equations from PowerModels.jl, allowing me to integrate them into various algorithms and compare results with those obtained using JuMP.

In the PowerModels documentation, I haven’t come across a specific function that returns the equations. Does such a function exist, or is do you know an alternative approach to achieve this?

Thanks in advance!

Jim

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 6, 2023, 7:22pm UTC](https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165/2 "2023-12-06T19:22:51Z")

</div>

Hi @Jim16, welcome to the forum. (Sorry I didn’t see your question yesterday, I’ve now moved it to the “Optimization (Mathematical)” section.)

What format do you want/expect the equations to be in?

The math formulation is described in [Mathematical Model · PowerModels](https://lanl-ansi.github.io/PowerModels.jl/stable/math-model/).

You can also see the underlying JuMP model with `pm.model`:  
[https://lanl-ansi.github.io/PowerModels.jl/stable/quickguide/#Building-PowerModels-from-Network-Data-Dictionaries](https://lanl-ansi.github.io/PowerModels.jl/stable/quickguide/#Building-PowerModels-from-Network-Data-Dictionaries)

---

<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: [December 6, 2023, 8:03pm UTC](https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165/3 "2023-12-06T20:03:26Z")

</div>

Hi @Jim16, thank for taking a look at PowerModels. To add to the suggestions of @odow, some times folks are looking for raw JuMP models for the OPF problems that PowerModels builds in a more flexible way. Here is a good reference for those,

> <https://github.com/lanl-ansi/PowerModelsAnnex.jl/tree/master/src/model>
>
> //github.com/lanl-ansi/PowerModelsAnnex.jl/tree/master/src/model

> **[GitHub - lanl-ansi/rosetta-opf: AC-OPF Implementations in Various NLP...](https://github.com/lanl-ansi/rosetta-opf)**
>
> AC-OPF Implementations in Various NLP Modeling Frameworks - GitHub - lanl-ansi/rosetta-opf: AC-OPF Implementations in Various NLP Modeling Frameworks

Another question we get a fair bit it to view the mathematical JuMP model that PowerModels builds for a given function call. In this case you can do a trick like this (I did not test this code),

```julia
using PowerModels, JuMP, Ipopt
m = Model()
result = solve_ac_opf(data, Ipopt.Optimizer, jump_model=m)
println(m)

```

---

<div class="post-metadata">

### Author: ![Jim16](https://avatars.discourse-cdn.com/v4/letter/j/9d8465/32.png) [@Jim16](https://discourse.julialang.org/u/Jim16)
#### Post date: [December 7, 2023, 8:47am UTC](https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165/4 "2023-12-07T08:47:30Z")

</div>

Thanks for your answers !

@odow : the best format would be to have them as a function on all the voltage magnitudes and angles, so that solving the pf equations would be the same as finding the roots of the function.

@ccoffrin : I’ll have a look at PowerModelsAnnex, thank you. About the bit of code you shared, I had already used this trick, but I didn’t manage to build an actual function containing the equations of the model. I could only display them.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 7, 2023, 7:48pm UTC](https://discourse.julialang.org/t/extracting-the-power-flow-equations-from-powermodels/107165/5 "2023-12-07T19:48:23Z")

</div>

I don’t know if there’s an easy way to get the equations as a Julia function that you can call.

This repo has a number of Julia function implementations though:

> <https://github.com/lanl-ansi/rosetta-opf/blob/main/optimization.jl>
