# Power flow optimization

The data for example is taken from MATPOWER website. MATPOWER is Matlab package for solving power flow and optimal power flow problems.

using Convex, SCS
using Test
aux(str) = joinpath(@__DIR__, "aux_files", str) # path to auxiliary files

TOL = 1e-2;
input = matopen(aux("Data.mat"))
varnames = names(input)

n = size(Data[2], 1);
Y = Data[2];
inj = Data[1];
W = ComplexVariable(n, n);
objective = real(sum(diag(W)));
c1 = Constraint[];
for i in 2:n
push!(c1, dot(Y[i, :], W[i, :]) == inj[i])
end
c2 = isposdef(W)
c3 = real(W[1, 1]) == 1.06^2;
push!(c1, c2)
push!(c1, c3)
p = maximize(objective, c1);
solve!(p, SCS.Optimizer; silent = true)
Problem statistics
problem is DCP         : true
number of variables    : 1 (392 scalar elements)
number of constraints  : 15 (811 scalar elements)
number of coefficients : 248
number of atoms        : 66

Solution summary
termination status : OPTIMAL
primal status      : FEASIBLE_POINT
dual status        : FEASIBLE_POINT
objective value    : 15.1275

Expression graph
maximize
└─ real (affine; real)
└─ sum (affine; complex)
└─ diag (affine; complex)
└─ …
subject to
├─ == constraint (affine)
│  └─ + (affine; complex)
│     ├─ sum (affine; complex)
│     │  └─ …
│     └─ complex constant
├─ == constraint (affine)
│  └─ + (affine; complex)
│     ├─ sum (affine; complex)
│     │  └─ …
│     └─ complex constant
├─ == constraint (affine)
│  └─ + (affine; complex)
│     ├─ sum (affine; complex)
│     │  └─ …
│     └─ complex constant
⋮

p.optval
15.127504333586348
evaluate(objective)
15.127504333586351
output = matopen(joinpath(@__DIR__, "Res.mat"))
names(output)
@test imag_diff ≈ zeros(n, n) atol = TOL
Test Passed