# PowerModels power flow

**URL:** <https://discourse.julialang.org/t/powermodels-power-flow/37986>\
**Category:** General Usage\
**Created:** [April 21, 2020, 6:48pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986 "2020-04-21T18:48:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [April 21, 2020, 6:48pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/1 "2020-04-21T18:48:24Z")

</div>

Hello,

I am using PowerModels to analyse the power flow of a network.

Why is the voltage angle different from the results obtained by MATPOWER tools in MATLAB.  
I am using this line to calculate the optimal power flow.

`result = run_opf("case9.m", ACPPowerModel, with_optimizer(Ipopt.Optimizer))`

Thank you

---

<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 21, 2020, 8:44pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/2 "2020-04-21T20:44:34Z")

</div>

Hi @hmemy, thanks for trying out PowerModels! Could you provide a little more details about how the solutions differ (e.g. is it in the objective value, voltage profile, generator dispatch) and also what commands you are running in Matpower to compare?

This paper is the last time I did a side-by-side comparison was in this paper, [[1711.01728] PowerModels.jl: An Open-Source Framework for Exploring Power Flow Formulations](https://arxiv.org/abs/1711.01728), at that time, the solutions of AC Power Flow were identical or nearly so.

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [April 22, 2020, 6:35am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/3 "2020-04-22T06:35:54Z")

</div>

Hi @ccoffrin,

I have figured it out. It is just MATPOWER’s is in degrees and PowerModels is in rad.

**Another question,**  
`savecase` function is utilised In MATPOWER to create a new case file. How can I create a new case file in PowerModels?

Note/ I have already looked at your paper. Great work !

---

<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 22, 2020, 2:25pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/4 "2020-04-22T14:25:28Z")

</div>

Great! I am glad to hear it.

That is good point that I forgot about! By default PowerModels works in the `per_unit` convention for its data, while Matpower uses a mixed units convention. You can use `make_mixed_units!` to turn the PowerModels data into Matpower’s unit convention, but you will have to turn it back with `make_per_unit!` before using it again in PowerModels.

In terms of saving data, you can try the `export_matpower` function. It is well tested but the results might look a little different than a pure Matpower case file becouse PowerModels supports a more general model of the network components.

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [April 25, 2020, 10:27am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/5 "2020-04-25T10:27:39Z")

</div>

Hello @ccoffrin, I hope you are well.

I have created new data dictionaries named: new\_bus, new\_gen, new\_branch, and I would like to save them in a file order to run an optimal power flow analysis. Could you please explain how to achieve this? Many thanks!

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [April 25, 2020, 10:31am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/6 "2020-04-25T10:31:33Z")

</div>

With the **[HDF5](https://github.com/JuliaIO/HDF5.jl)** package you can save \*.jld files, the equivalent of \*.mat files for Julia.

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [April 25, 2020, 10:48am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/7 "2020-04-25T10:48:57Z")

</div>

Hi @martincornejo, is the saved file readable in PowerModels?

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [April 25, 2020, 11:07am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/8 "2020-04-25T11:07:04Z")

</div>

If you have created a dictionary `network_data` that contains all the necessary data in the PowerModels _format_ you don’t need to save it for PowerModels to read it, you can simply feed it directly. Take [this example taken from the documentation](https://lanl-ansi.github.io/PowerModels.jl/stable/quickguide/#Building-PowerModels-from-Network-Data-Dictionaries-1)

```julia
# network_data is your dictionary with bus, branch and gen data

pm = instantiate_model(network_data, ACPPowerModel, PowerModels.build_opf)

print(pm.model)

result = optimize_model!(pm, optimizer=with_optimizer(Ipopt.Optimizer))

```

If for some reason you would like to save the dictionary `network_data` to work later on it, or send it to a colleague you could save it into a julia data file (\*.jld). As for the instantiated model `pm` I’m not sure how you could save it, but I think it should be possible.

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [April 25, 2020, 11:07am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/9 "2020-04-25T11:07:57Z")

</div>

I will give it a try. Thank you

---

<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 25, 2020, 9:06pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/10 "2020-04-25T21:06:06Z")

</div>

@hmemy, you can also try the `export_matpower` function provided in PowerModels. Know that you might see some extra data tables in there because PowerModels supports a more general network model than Matpower. That said, if you restrict your self to the Matpower subset, it should work as expected.

Also for general information, I generally think about PowerModels data dicts as an internal data model, but they are JSON serializable. This is quite helpful when building multi-language pipelines.

---

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [November 13, 2021, 4:04am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/11 "2021-11-13T04:04:41Z")

</div>

> [@ccoffrin](#):
>
> export\_matpower

hello, ccoffrin, I’m a new to learn PowerModels and Julia, can you tell me how to use “export\_matpower” function?  
thanks!

---

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [November 13, 2021, 4:10am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/12 "2021-11-13T04:10:10Z")

</div>

> [@ccoffrin](#):
>
> `export_matpower` function

I have known it, thank you for your useful reply.

---

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [November 13, 2021, 7:46am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/13 "2021-11-13T07:46:31Z")

</div>

Hello, @ccoffrin, now I am using export\_matpower, the m file show the result, but  
there are too many digits after the decimal point.  
When I use print\_summary, display in repl doesn’t have so many digits.  
can I set the number of digits in export\_matpower function?

---

<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:** [November 18, 2021, 1:32am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/14 "2021-11-18T01:32:25Z")

</div>

There is no feature to truncate the accuracy of `export_matpower` and I would generally not encourage this as solutions can easily be sensitive to the 7th decimal place in the voltage magnitude values.

You could do some pre-processing on the PowerModels data dictionary to truncate the data values before calling `export_matpower` and this should have the effect you are looking for.

---

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [November 21, 2021, 2:37pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/15 "2021-11-21T14:37:30Z")

</div>

Thank you very much @ccoffrin .  
Now I am studying SOC-OPF in PowerModelsAnnex, the program have calculated W value, but I can’t find the function calculates V value using W.  
Looking forward to your reply！

---

<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:** [November 21, 2021, 10:56pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/16 "2021-11-21T22:56:14Z")

</div>

I think what you are looking for is the solution processor called `sol_data_model!`, have a look at this example,

[https://github.com/lanl-ansi/PowerModels.jl/blob/master/test/output.jl#L256](https://github.com/lanl-ansi/PowerModels.jl/blob/master/test/output.jl#L256)

---

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [November 22, 2021, 3:23am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/17 "2021-11-22T03:23:34Z")

</div>

Yes, that is the model I want.  
But when I see the definition of `sol_data_model!`，I only find the solution of ‘vm’ in `function _sol_data_model_w!(solution::Dict)`

You know the SOC-OPF result gives ‘w’ 、‘wr’、‘wi’, atan(wi/wr) only gets the angle between vi and vj, but they are not the angle relative to the reference bus, I want to know how to compute the ‘va’ in `sol_data_model!`  
looking forwar your reply, thank you very much.

---

<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:** [November 23, 2021, 4:57pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/18 "2021-11-23T16:57:23Z")

</div>

@Xuekui_wang in general it is not possible to map the `wr` `wi` values into `va` values, this is because `wr` `wi` are a higher dimension (|E|) than the `va` values (|N|), this is part of the reason with SOC-OPF is a relaxation and not an exact solution. So this is not supported by PowerModels by default.

---

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [November 24, 2021, 1:35am UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/19 "2021-11-24T01:35:10Z")

</div>

Yes, thank you ccoffrin, I think I have known it.  
If I want the result of `va`，the power flow can be used based on SOC result of “Pg”.

---

<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:** [November 24, 2021, 4:52pm UTC](https://discourse.julialang.org/t/powermodels-power-flow/37986/20 "2021-11-24T16:52:34Z")

</div>

Yes, that is a reasonable approach to recover an AC feasible solution.
