# Difficulty with utilizing PowerModels.update data! function

**URL:** https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906
**Category:** Optimization (Mathematical)
**Tags:** community
**Created:** [December 20, 2022, 5:07pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906 "2022-12-20T17:07:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Wahee01](https://avatars.discourse-cdn.com/v4/letter/w/aca169/32.png) [@Wahee01](https://discourse.julialang.org/u/Wahee01)
#### Post date: [December 20, 2022, 5:07pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906/1 "2022-12-20T17:07:54Z")

</div>

Hello to everyone. I apologize if my inquiry is too simple. Julia is a programming language that I am learning. For optimal power flow and power flow problems, I use the PowerModels toolkit. PowerModels.update data! is a function that allows the user to update the prior result for future calculation.

For example, if I compute DC-OPF using DCPPowerModel and then run AC power flow to see if the DC-OPF is AC practicable, the code frequently returns an error. When I utilized ACPPowerModel and SOCWRPowerModel, this was not the case. Here is my code.

using PowerModels

using Ipopt

using SCS

using MAT

using Gurobi

network\_data = PowerModels.parse\_file(“case9.m”)

result=solve\_opf(network\_data, DCPPowerModel, Gurobi.Optimizer)

PowerModels.print\_summary(result[“solution”])

PowerModels.update\_data!(network\_data, result[“solution”])

result2=PowerModels.compute\_ac\_pf(network\_data)

PowerModels.print\_summary(result2[“solution”])

result3=PowerModels.calc\_branch\_flow\_ac(network\_data)

PowerModels.print\_summary(result3)

---

<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 20, 2022, 7:19pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906/2 "2022-12-20T19:19:04Z")

</div>

The issue here is that `result=solve_opf(network_data, DCPPowerModel, Gurobi.Optimizer)` returns `NaN` values for the reactive power parts of the solution. Then the call to `PowerModels.update_data!(network_data, result[“solution”])` overwrites the reactive parts of the network data with these `NaN` values which makes it not a well formed AC dataset for `compute_ac_pf`.

The recommend fix would be to replace `PowerModels.update_data!(network_data, result[“solution”])` with a variant you write by hand that only updates the active power values you are interested in (usually the `pg` values on the generators and sometimes the `va` values on the buses).

---

<div class="post-metadata">

### Author: ![Wahee01](https://avatars.discourse-cdn.com/v4/letter/w/aca169/32.png) [@Wahee01](https://discourse.julialang.org/u/Wahee01)
#### Post date: [December 22, 2022, 3:02pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906/3 "2022-12-22T15:02:59Z")

</div>

Thank you very much for responding. So I only have to hunt for an other technique of updating my data when I want to evaluate whether the DC-OPF set point is AC power flow possible. Does PowerModels.update data! work as expected for ACPPowerModel and SOCWRPowerModel?

---

<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 22, 2022, 4:52pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906/4 "2022-12-22T16:52:01Z")

</div>

Sure thing.

In all cases, `update_data!` works as expected, in that it overwrites one dictionary with another. That is the limits of its functionality.

The issue you are having here is related to the output semantics of `solve_opf` with a network model that does not define reactive power aspects of the problem, i.e. `DCPPowerModel`. The convention in PowerModels is to use `NaN`’s when a particular required value is not available, so that the data output structure is standardized for all models. If you use a model that defines all of these values (e.g. `ACPPowerModel`) `update_data!` will do what you are expecting without special work. In cases where not all data is well defined, i.e. `DCPPowerModel`, you need to write some custom code for the specific use case that you have in mind.

---

<div class="post-metadata">

### Author: ![Wahee01](https://avatars.discourse-cdn.com/v4/letter/w/aca169/32.png) [@Wahee01](https://discourse.julialang.org/u/Wahee01)
#### Post date: [December 22, 2022, 5:32pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906/5 "2022-12-22T17:32:19Z")

</div>

Thank you very much for your rapid answer. Please do you have any example code that might be of assistance?

---

<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 23, 2022, 4:25pm UTC](https://discourse.julialang.org/t/difficulty-with-utilizing-powermodels-update-data-function/91906/6 "2022-12-23T16:25:03Z")

</div>

I think what you are looking for is something like this code below. I would caution, setting up an AC PowerFlow study from a DC dispatch solution has a number of subtle points (e.g. line losses) that should be treated with care.

```julia
using PowerModels, Ipopt

case_file = joinpath(dirname(pathof(PowerModels)), "..", "test", "data", "matpower", "case9.m")

network_data = parse_file(case_file)

result = solve_opf(network_data, DCPPowerModel, Ipopt.Optimizer)
print_summary(result["solution"])

for (i,gen) in result["solution"]["gen"]
    network_data["gen"][i]["pg"] = gen["pg"]
end

result2 =solve_pf(network_data, ACPPowerModel, Ipopt.Optimizer)
print_summary(result2["solution"])

```
