OPF_CURRENT_BALANCE_FCN Evaluates AC current balance constraints and their gradients. [G, DG] = OPF_CURRENT_BALANCE_FCN(X, OM, YBUS, MPOPT) Computes the real or imaginary current balance equality constraints for AC optimal power flow. Computes constraint vectors and their gradients. Inputs: X : optimization vector MPC : MATPOWER case struct YBUS : bus admittance matrix MPOPT : MATPOWER options struct Outputs: G : vector of equality constraint values (real/imaginary current balances) DG : (optional) equality constraint gradients Examples: g = opf_current_balance_fcn(x, mpc, Ybus, mpopt); [g, dg] = opf_current_balance_fcn(x, mpc, Ybus, mpopt); See also OPF_POWER_BALANCE_HESS
0001 function [g, dg] = opf_current_balance_fcn(x, mpc, Ybus, mpopt) 0002 %OPF_CURRENT_BALANCE_FCN Evaluates AC current balance constraints and their gradients. 0003 % [G, DG] = OPF_CURRENT_BALANCE_FCN(X, OM, YBUS, MPOPT) 0004 % 0005 % Computes the real or imaginary current balance equality constraints for 0006 % AC optimal power flow. Computes constraint vectors and their gradients. 0007 % 0008 % Inputs: 0009 % X : optimization vector 0010 % MPC : MATPOWER case struct 0011 % YBUS : bus admittance matrix 0012 % MPOPT : MATPOWER options struct 0013 % 0014 % Outputs: 0015 % G : vector of equality constraint values (real/imaginary current balances) 0016 % DG : (optional) equality constraint gradients 0017 % 0018 % Examples: 0019 % g = opf_current_balance_fcn(x, mpc, Ybus, mpopt); 0020 % [g, dg] = opf_current_balance_fcn(x, mpc, Ybus, mpopt); 0021 % 0022 % See also OPF_POWER_BALANCE_HESS 0023 0024 % MATPOWER 0025 % Copyright (c) 1996-2018, Power Systems Engineering Research Center (PSERC) 0026 % by Baljinnyam Sereeter, Delft University of Technology 0027 % and Ray Zimmerman, PSERC Cornell 0028 % 0029 % This file is part of MATPOWER. 0030 % Covered by the 3-clause BSD License (see LICENSE file for details). 0031 % See https://matpower.org for more info. 0032 0033 %%----- initialize ----- 0034 %% define named indices into data matrices 0035 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ... 0036 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ... 0037 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen; 0038 0039 %% unpack data 0040 [baseMVA, bus, gen] = deal(mpc.baseMVA, mpc.bus, mpc.gen); 0041 if mpopt.opf.v_cartesian 0042 [Vr, Vi, Pg, Qg] = deal(x{:}); 0043 V = Vr + 1j* Vi; %% reconstruct V 0044 else 0045 [Va, Vm, Pg, Qg] = deal(x{:}); 0046 V = Vm .* exp(1j * Va); %% reconstruct V 0047 end 0048 0049 %% problem dimensions 0050 nb = length(V); %% number of buses 0051 ng = length(Pg); %% number of dispatchable injections 0052 0053 %% ----- evaluate constraints ----- 0054 %% put Pg, Qg back in gen 0055 gen(:, PG) = Pg * baseMVA; %% active generation in MW 0056 gen(:, QG) = Qg * baseMVA; %% reactive generation in MVAr 0057 0058 %% rebuild Sbus 0059 Sbus = makeSbus(baseMVA, bus, gen); %% net injected power in p.u. 0060 0061 %% evaluate complex current balance mismatches 0062 mis = Ybus*V - conj(Sbus./V); 0063 0064 %% assemble real and imaginary current balance constraints 0065 g = [ real(mis); %% real current mismatch 0066 imag(mis) ]; %% imaginary current mismatch 0067 0068 %%----- evaluate constraint gradients ----- 0069 if nargout > 1 0070 %% compute partials of injected bus powers 0071 Cg = sparse(gen(:, GEN_BUS), 1:ng, 1, nb, ng); 0072 InvConjV = sparse(1:nb, 1:nb, 1./conj(V), nb, nb); 0073 dImis_dPg = -InvConjV * Cg; %% dImis w.r.t. Pg 0074 dImis_dQg = -1j * dImis_dPg; %% dImis w.r.t. Qg 0075 [dImis_dV1, dImis_dV2] = dImis_dV(Sbus, Ybus, V, mpopt.opf.v_cartesian); %% w.r.t. V 0076 dg = [ 0077 real([dImis_dV1 dImis_dV2 dImis_dPg dImis_dQg]); %% Ir mismatch w.r.t V1, V2, Pg, Qg 0078 imag([dImis_dV1 dImis_dV2 dImis_dPg dImis_dQg]) ; %% Ii mismatch w.r.t V1, V2, Pg, Qg 0079 ]; 0080 end