0001 function [Avl, lvl, uvl, ivl] = makeAvl(baseMVA, gen)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0028 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0029 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0030
0031 if nargin < 2
0032 mpc = baseMVA;
0033 baseMVA = mpc.baseMVA;
0034 gen = mpc.gen;
0035 else
0036 mpc = [];
0037 end
0038
0039
0040 ng = size(gen, 1);
0041 Pg = gen(:, PG) / baseMVA;
0042 Qg = gen(:, QG) / baseMVA;
0043 Pmin = gen(:, PMIN) / baseMVA;
0044 Qmin = gen(:, QMIN) / baseMVA;
0045 Qmax = gen(:, QMAX) / baseMVA;
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 ivl = find( isload(gen) & (Qmin ~= 0 | Qmax ~= 0) );
0058 nvl = size(ivl, 1);
0059
0060
0061 if any( Qmin(ivl) ~= 0 & Qmax(ivl) ~= 0 )
0062 if isempty(mpc)
0063 s = '';
0064 else
0065 k = find(Qmin(ivl) ~= 0 & Qmax(ivl) ~= 0);
0066 if isfield(mpc, 'order') && mpc.order.state == 'i'
0067 gidx = mpc.order.gen.i2e(ivl(k));
0068 else
0069 gidx = ivl(k);
0070 end
0071 s = sprintf('Invalid Q limits for dispatchable load in row %d of gen matrix\n', gidx);
0072 end
0073 error('makeAvl: Either Qmin or Qmax must be equal to zero for each dispatchable load.\n%s', s);
0074 end
0075
0076
0077
0078
0079
0080 Qlim = (Qmin(ivl) == 0) .* Qmax(ivl) + ...
0081 (Qmax(ivl) == 0) .* Qmin(ivl);
0082 if any( abs( Qg(ivl) - Pg(ivl) .* Qlim ./ Pmin(ivl) ) > 1e-6 )
0083 if isempty(mpc)
0084 s = '';
0085 else
0086 k = find(abs( Qg(ivl) - Pg(ivl) .* Qlim ./ Pmin(ivl) ) > 1e-6);
0087 if isfield(mpc, 'order') && mpc.order.state == 'i'
0088 gidx = mpc.order.gen.i2e(ivl(k));
0089 else
0090 gidx = ivl(k);
0091 end
0092 s = sprintf('QG for dispatchable load in row %d of gen matrix must be PG * %g\n', [gidx Qlim ./ Pmin(ivl)]');
0093 end
0094 error('makeAvl: %s\n %s\n %s\n %s\n%s', ...
0095 'For a dispatchable load, PG and QG must be consistent', ...
0096 'with the power factor defined by PMIN and the relevant', ...
0097 '(non-zero) QMIN or QMAX limit.', ...
0098 'Note: Setting PG = QG = 0 satisfies this condition.', s);
0099 end
0100
0101
0102 if nvl > 0
0103 xx = Pmin(ivl);
0104 yy = Qlim;
0105 pftheta = atan2(yy, xx);
0106 pc = sin(pftheta);
0107 qc = -cos(pftheta);
0108 ii = [ (1:nvl)'; (1:nvl)' ];
0109 jj = [ ivl; ivl+ng ];
0110 Avl = sparse(ii, jj, [pc; qc], nvl, 2*ng);
0111 lvl = zeros(nvl, 1);
0112 uvl = lvl;
0113 else
0114 Avl = sparse(0, 2*ng);
0115 lvl =[];
0116 uvl =[];
0117 end