Hello Ray,

I'm trying to add power flow constraints such that Pf - f(V) = 0 and also
adding the power flows as variables in the code.

I've attached my code for more clarity. I'm unsure about formulating the
hessian function. I'm adding these constraints so that I can verify
Lagrangian function later on to find global solution with perhaps an
extended range.

Regards,
Monisha

Monisha Raju
Graduate Research Assistant
University at Buffalo

On Tue, Dec 5, 2017 at 3:52 PM, Ray Zimmerman <[email protected]> wrote:

> I’m not sure what you mean by add them *before* running the OPF. The
> results struct and its x field do not exist until after running the OPF.
>
> In any case, I’m not sure why you want to do this. Maybe if you give a
> little more detail on what you are trying to accomplish, we can suggest a
> better approach.
>
>     Ray
>
>
> On Dec 4, 2017, at 1:27 PM, Monisha Raju <[email protected]> wrote:
>
> Hi,
>
> I was wondering if there was a way I could add power flows Pf, Qf, Pt and
> Qt to the .x (final value of optimization variables) field at the output.
>
> I'm adding power flows from branch data after executing runopf into the .x
> field. I would like to know if I could do this before execution.
>
> Regards,
> Monisha Raju
> Graduate Research Assistant
> University at Buffalo
>
>
>
function [g, dg] = power_flow_fcn(x, mpc, Yf, Yt, il, mpopt)

%% Extract required data
[F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
    TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
    ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;

%% unpack data
branch = mpc.branch;
[Va, Vm] = deal(x{:});

V = Vm .* exp(1j * Va);

Pf0 = branch(:, PF); Pt0 = branch(:, PT);
Qf0 = branch(:, QF); Qt0 = branch(:, QT);
Sf = V(branch(il, F_BUS)) .* conj(Yf * V); 
St = V(branch(il, T_BUS)) .* conj(Yt * V);
Pf = real(Sf); Qf = imag(Sf);
Pt = real(St); Qt = imag(St);

mpc.user_vars = {'Pf', nl, Pf, Pmin, Pmax};
mpc.user_vars = {'Qf', nl, Qf, Qmin, Qmax};
mpc.user_vars = {'Pt', nl, Pt, Pmin, Pmax};
mpc.user_vars = {'Qt', nl, Qt, Qmin, Qmax};

g = [Pf-Pf0; Qf-Qf0; Pt-Pt0;  Qt-Qt0];
dAf_dPf = sparse(1:nl, 1:nl, 2*Pf, nl, nl);
dAf_dQf = sparse(1:nl, 1:nl, 2*Qf, nl, nl);
dAt_dPt = sparse(1:nl, 1:nl, 2*Pt, nl, nl);
dAt_dQt = sparse(1:nl, 1:nl, 2*Qt, nl, nl);
dg = [dAf_dPf; dAf_dQf; dAt_dPt; dAt_dQt];    
function d2G = power_flow_hess(x, lambda, mpc, Yf, Yt, il, mpopt)

[Va, Vm] = deal(x{:});

%% unpack needed parameters
nb = length(Va);        %% number of buses
nl2 = length(il);       %% number of constrained lines

%% reconstruct V
V = Vm .* exp(1j * Va);

%%----- evaluate Hessian of flow constraints -----
%% keep dimensions of empty matrices/vectors compatible
%% (required to avoid problems when using Knitro
%%  on cases with all lines unconstrained)
nmu = length(lambda) / 2;
if nmu
    muF = lambda(1:nmu);
    muT = lambda((1:nmu)+nmu);
else    %% keep dimensions of empty matrices/vectors compatible
    muF = zeros(0,1);   %% (required to avoid problems when using Knitro
    muT = zeros(0,1);   %%  on cases with all lines unconstrained)
end
f = mpc.branch(il, F_BUS);    %% list of "from" buses
t = mpc.branch(il, T_BUS);    %% list of "to" buses
Cf = sparse(1:nl2, f, ones(nl2, 1), nl2, nb);   %% connection matrix for line & 
from buses
Ct = sparse(1:nl2, t, ones(nl2, 1), nl2, nb);   %% connection matrix for line & 
to buses
[dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, Sf, St] = dSbr_dV(mpc.branch(il,:), Yf, 
Yt, V);
[dAf_dPf, dAf_dQf, dAt_dPt, dAt_dQt] = dAbr_dV(dSf_dVa, dSf_dVm, dSt_dVa, 
dSt_dVm, Sf, St);
% [Hfaa, Hfav, Hfva, Hfvv] = d2ASbr_dV2(dSf_dVa, dSf_dVm, Sf, Cf, Yf, V, muF);
% [Htaa, Htav, Htva, Htvv] = d2ASbr_dV2(dSt_dVa, dSt_dVm, St, Ct, Yt, V, muT);
% d2G = [Hfaa Hfav; Hfva Hfvv] + [Htaa Htav; Htva Htvv];
mpc = loadcase('case9');
nl = size(mpc.branch,1);

%% Non-linear equality branch flow constraints
mpc.user_constraints.nle = {
    {'flow_constraint', 4*nl, 'power_flow_fcn', 'power_flow_hess', {'Va', 'Vm'}}
    };
r = runopf(mpc);

Reply via email to