# Adding a new function to the PowerModelsAnnex

**URL:** <https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634>\
**Category:** Optimization (Mathematical)\
**Created:** [July 24, 2020, 8:21pm UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634 "2020-07-24T20:21:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [July 24, 2020, 8:21pm UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634/1 "2020-07-24T20:21:09Z")

</div>

Hi all,

I have written a function, i.e, sdp\_opf, to solve the OPF problem via SDP relaxation which exports the build\_sdp\_opf. However, when I want to run my code via build\_sdp\_opf(case,model) I get the following error.

```julia
build_sdp_opf not defined

```

I have already written similar functions to and add them to the PowerModelsAnnex in Julia 0.6 to solve the OPF problem and I didn’t get any error. However, recently I upgraded my Julia to 1.3 and I got the above error. I will appreciate it if you let me know about possible actions that I should take to run my code in Julia 1.3. Thanks in advance!

---

<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:** [July 25, 2020, 1:06am UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634/2 "2020-07-25T01:06:09Z")

</div>

This seems like a repeat of a question in,

> [@SDP relaxation in PowerModelsAneex](https://discourse.julialang.org/t/sdp-relaxation-in-powermodelsaneex/41898/6):
>
> Hi Dr Coffrin, Thanks for the response. I is the eye matrix, defined in LinearAlgebra package, that you have directly used in your SDP code in PowerModels. You have used LinearAlgebra package in your code. But When I added that package to PowerModelsAnnex I got the above error. My problem is how I can use the LinearAlgebra library in the power model annex. I will definitely look at the Julia package development documents. All the best, Rasoul

I think the core point here is that a lot changed about how package development works between Julia v0.6 and Julia v1.0. You might start by reviewing those changes. For example,

> **[My New Workflow with Julia 1.0](https://erik-engheim.medium.com/my-new-workflow-with-julia-1-0-99711103d97c)**
>
> A practical guide to how you can work with Julia in a practical and effective manner.

[http://abelsiqueira.github.io/blog/package-development-on-julia-10/](http://abelsiqueira.github.io/blog/package-development-on-julia-10/)

It’s fairly similar to how it use to work, just make sure that you are working out of the “dev” directory and that your active environment is setup to use that version of the code.

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [July 25, 2020, 5:04pm UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634/3 "2020-07-25T17:04:35Z")

</div>

```
Hi Dr. Coffrin,

```

Thanks for your response, it was useful and I really appreciate it. I fixed that error by activating the environment. As you might know I am writing the SDP relaxation for the OPF problem in rectangular coordinate in PowerModelsAnnex.  
The core difference between my formulation and yours in PowerMpdels is about how the “W” matrix is defined. In my implementation I have defined a 2n\*2n positive semidefinite matrix named “W” and then I extract “w”,”wr”, “wi” out of the “W” matrix to defined all other constraints based on these variables.  
I have two questions. First, with the following implementation I get an error (i.e. MethodError: no method matching \_get\_name(::QuoteNode)) for line “@variable(model,:w)[i] = W[w\_idx,w\_idx]+W[w\_idx+n\_bus,w\_idx+n\_bus]”.

My second question is, even after commenting out that line I get the following error, UndefVarError: nw not defined. I don’t have any “nw” in my code except in the second line. I will appreciate it if you guide me to solve these error. Thanks in advance!

```julia
    PowerModels.standardize_cost_terms!(data, order=2)
    ref = PowerModels.build_ref(data)[:nw][0]
    bus_ids = keys(ref[:bus])
    n_bus = length(bus_ids)
    w_index = 1:length(bus_ids)
    lookup_w_index = Dict((bi,i) for (i,bi) in enumerate(bus_ids))

    WR_start_1 = zeros(length(bus_ids), length(bus_ids)) + I
    WR_start_2 = zeros(length(bus_ids), length(bus_ids))
    WR_start_3 = zeros(length(bus_ids), length(bus_ids))
    WR_start_4 = zeros(length(bus_ids), length(bus_ids))

    W_start=[WR_start_1 WR_start_2;WR_start_3 WR_start_4]
    @variable(model,
    W[i=1:2*length(bus_ids), j=1:2*length(bus_ids)], PSD, base_name="$(nw)_W", start=W_start[i,j]
    )
    @variable(model)[:w] = Dict{Int,Any}()
    for (i, bus) in ref(pm, :bus)
        w_idx = lookup_w_index[i]
        @variable(model,:w)[i] = W[w_idx,w_idx]+W[w_idx+n_bus,w_idx+n_bus]
   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:** [July 25, 2020, 5:28pm UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634/4 "2020-07-25T17:28:17Z")

</div>

I think the issue is with this `@variable(model)[:w]`, I think the code should look something more like this,

```julia
    PowerModels.standardize_cost_terms!(data, order=2)
    ref = PowerModels.build_ref(data)[:nw][0]
    bus_ids = keys(ref[:bus])
    n_bus = length(bus_ids)
    w_index = 1:length(bus_ids)
    lookup_w_index = Dict((bi,i) for (i,bi) in enumerate(bus_ids))

    WR_start_1 = zeros(length(bus_ids), length(bus_ids)) + I
    WR_start_2 = zeros(length(bus_ids), length(bus_ids))
    WR_start_3 = zeros(length(bus_ids), length(bus_ids))
    WR_start_4 = zeros(length(bus_ids), length(bus_ids))

    W_start=[WR_start_1 WR_start_2;WR_start_3 WR_start_4]
    @variable(model,
    W[i=1:2*length(bus_ids), j=1:2*length(bus_ids)], PSD, base_name="$(nw)_W", start=W_start[i,j]
    )
    w_lookup = Dict{Int,Any}()
    for (i, bus) in ref(pm, :bus)
        w_idx = lookup_w_index[i]
        w_lookup[i] = W[w_idx,w_idx]+W[w_idx+n_bus,w_idx+n_bus]
   end

```

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [July 26, 2020, 12:51am UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634/5 "2020-07-26T00:51:05Z")

</div>

Thanks for your invaluable response! It fixed the error. Can you please give me some insights on how to solve the issue in the second part of my question. I still get the following error, UndefVarError: nw not defined.  
I don’t have any “nw” in my code except in the second line. Thanks in advance!

---

<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:** [July 26, 2020, 4:37pm UTC](https://discourse.julialang.org/t/adding-a-new-function-to-the-powermodelsannex/43634/6 "2020-07-26T16:37:50Z")

</div>

> [@ccoffrin](#):
>
> `base_name="$(nw)_W"`

Most likely this line should be changed to something like `base_name="W"`
