# DC load flow constraint

**URL:** https://discourse.julialang.org/t/dc-load-flow-constraint/80564
**Category:** Optimization (Mathematical)
**Created:** [May 5, 2022, 7:53pm UTC](https://discourse.julialang.org/t/dc-load-flow-constraint/80564 "2022-05-05T19:53:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)
#### Post date: [May 5, 2022, 7:53pm UTC](https://discourse.julialang.org/t/dc-load-flow-constraint/80564/1 "2022-05-05T19:53:02Z")

</div>

Hello,

I am trying to fit in a DC load flow equation into my zonal dispatch model without much success.

```julia
#using DataFrames, JuMp

generators = DataFrame(index = ["conventional_gas", "conventional_nuc", "West_A_Wind", "Central_C_Wind", "North_D_Wind", 
                                "West_A_ROR", "Central_C_ROR", "North_D_ROR"], R_ID = 1:8, node_id = [1,2,1,2,3,1,2,3],
                                g_max = [100,250,40,60,80,30,100,60], cost = [20,8,0,0,0,0,0,0], conventional = [1,1,0,0,0,0,0,0], WIND = [0,0,1,1,1,0,0,0],
                                ROR = [0,0,0,0,0,1,1,1])

network = DataFrame(network_zones = ["z1","z2","z3","z4"], network_lines = 1:4, z1 = [1,0,0,1],
                    z2 = [-1,1,1,0], z3 = [0,-1,0,0], z4 = [0,0,-1,-1], reactance_unit = [1,1,1,1])

lines = select(network[1:4,:], 
    :network_lines, :z1, :z2, :z3)

incidence = [1 -1 0 0; 0 1 -1 0; 0 1 0 -1; 1 0 0 -1]

```

```julia
#Sets
L = lines.network_lines 
T = convert(Array{Int64}, 1:4)
Z = convert(Array{Int64}, 1:4)

```

```julia
#Dispatch_Model = Model(Clp.Optimizer)

#@variable(Dispatch_Model, vFLOW[T,L])
#@variable(Dispatch_Model, vVoltage[T,Z])

@constraint(Dispatch_Model, 
    DC_LineFlow[t in T, l in L], 
    vFLOW[t,l] == sum(incidence[z][l] * vVoltage[t,z]/network.reactance_unit[l] for z in Z)) 

```

```julia
#Error message when running the DC_LineFlow constraint

ERROR: BoundsError
Stacktrace:
  [1] getindex(x::Int64, i::Int64)
    @ Base .\number.jl:96
  [2] macro expansion
    @ C:\Users\mbahe\.julia\packages\MutableArithmetics\0Y9ZS\src\rewrite.jl:279 [inlined]
  [3] macro expansion
    @ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\macros.jl:676 [inlined]
  [4] (::var"#17#18")(t::Int64, l::Int64)
    @ Main C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\macro.jl:194
  [5] #38
    @ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:105 [inlined]
  [6] iterate
    @ .\generator.jl:47 [inlined]
  [7] collect_to!(dest::Matrix{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}, itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, JuMP.Containers.var"#38#39"{var"#17#18"}}, offs::Int64, st::Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64}})
    @ Base .\array.jl:724
  [8] collect_to_with_first!(dest::Matrix{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}}, v1::ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}, ScalarShape}, itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, JuMP.Containers.var"#38#39"{var"#17#18"}}, st::Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64}})
    @ Base .\array.jl:702
  [9] collect(itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, JuMP.Containers.var"#38#39"{var"#17#18"}})
    @ Base .\array.jl:683
 [10] map(f::Function, A::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}})
    @ Base .\abstractarray.jl:2323
 [11] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, #unused#::Type{JuMP.Containers.DenseAxisArray})
    @ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:105
 [12] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}})
    @ JuMP.Containers C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\Containers\container.jl:66
 [13] macro expansion
    @ C:\Users\mbahe\.julia\packages\JuMP\klrjG\src\macros.jl:142 [inlined]
 [14] top-level scope
    @ c:\Users\mbahe\Desktop\JuliaDiscourse\dc_load_flow_question.jl:30

```

I have two main problems here. First is constraining the flow over the lines (vFLOW) to the basic equation. Secondly, how do i use the incidence matrix that is in the network dataframe instead of manually hardcoding it…

Any thoughts or suggestions is much appreciated. Thanks

---

<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: [May 5, 2022, 8:31pm UTC](https://discourse.julialang.org/t/dc-load-flow-constraint/80564/2 "2022-05-05T20:31:15Z")

</div>

I’m going to suggest the same things as your other question [Renewable generation constraint](https://discourse.julialang.org/t/renewable-generation-constraint/80526).

```julia
incidence = [1 -1 0 0; 0 1 -1 0; 0 1 0 -1; 1 0 0 -1]

```

This is a Matrix. You index it as `incidence[row, column]`.

> `sum(incidence[z][l] * vVoltag`

But here you indexed it as `incidence[z][l]`. It should probably be `incidence[z, l]`.

Since both questions have been small typos, you should carefully checking each part of what you write.

---

<div class="post-metadata">

### Author: ![mbah](https://avatars.discourse-cdn.com/v4/letter/m/bcef8e/32.png) [@mbah](https://discourse.julialang.org/u/mbah)
#### Post date: [May 5, 2022, 10:19pm UTC](https://discourse.julialang.org/t/dc-load-flow-constraint/80564/3 "2022-05-05T22:19:50Z")

</div>

Thank you very much for the insight @odow. Yup, I definitely need to review the typos beforehand.
