0001 function om = opf_setup(mpc, mpopt)
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
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 dc = mpopt(10);
0042 alg = mpopt(11);
0043 verbose = mpopt(31);
0044
0045
0046 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0047 VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0048 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0049 MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0050 QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0051 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0052 TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0053 ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0054 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0055
0056
0057 nb = size(mpc.bus, 1);
0058 nl = size(mpc.branch, 1);
0059 ng = size(mpc.gen, 1);
0060 if isfield(mpc, 'A')
0061 nusr = size(mpc.A, 1);
0062 else
0063 nusr = 0;
0064 end
0065 if isfield(mpc, 'N')
0066 nw = size(mpc.N, 1);
0067 else
0068 nw = 0;
0069 end
0070
0071 if dc
0072
0073 mpc.gencost = pqcost(mpc.gencost, ng);
0074
0075
0076 if nusr || nw
0077 acc = [nb+(1:nb) 2*nb+ng+(1:ng)];
0078 if nusr && size(mpc.A, 2) >= 2*nb + 2*ng
0079
0080 if any(any(mpc.A(:, acc)))
0081 error('opf_setup: attempting to solve DC OPF with user constraints on Vm or Qg');
0082 end
0083 mpc.A(:, acc) = [];
0084 end
0085 if nw && size(mpc.N, 2) >= 2*nb + 2*ng
0086
0087 if any(any(mpc.N(:, acc)))
0088 [ii, jj] = find(mpc.N(:, acc));
0089 ii = unique(ii);
0090 if any(mpc.Cw(ii)) || (isfield(mpc, 'H') && ~isempty(mpc.H) && ...
0091 any(any(mpc.H(:, ii))))
0092 error('opf_setup: attempting to solve DC OPF with user costs on Vm or Qg');
0093 end
0094 end
0095 mpc.N(:, acc) = [];
0096 end
0097 end
0098 end
0099
0100
0101 pwl1 = find(mpc.gencost(:, MODEL) == PW_LINEAR & mpc.gencost(:, NCOST) == 2);
0102
0103 if ~isempty(pwl1)
0104 x0 = mpc.gencost(pwl1, COST);
0105 y0 = mpc.gencost(pwl1, COST+1);
0106 x1 = mpc.gencost(pwl1, COST+2);
0107 y1 = mpc.gencost(pwl1, COST+3);
0108 m = (y1 - y0) ./ (x1 - x0);
0109 b = y0 - m .* x0;
0110 mpc.gencost(pwl1, MODEL) = POLYNOMIAL;
0111 mpc.gencost(pwl1, NCOST) = 2;
0112 mpc.gencost(pwl1, COST:COST+1) = [m b];
0113 end
0114
0115
0116 [baseMVA, bus, gen, branch, gencost, Au, lbu, ubu, mpopt, ...
0117 N, fparm, H, Cw, z0, zl, zu, userfcn] = opf_args(mpc, mpopt);
0118
0119
0120 refs = find(bus(:, BUS_TYPE) == REF);
0121 if length(refs) > 1 && verbose > 0
0122 errstr = ['\nopf_setup: Warning: Multiple reference buses.\n', ...
0123 ' For a system with islands, a reference bus in each island\n', ...
0124 ' may help convergence, but in a fully connected system such\n', ...
0125 ' a situation is probably not reasonable.\n\n' ];
0126 fprintf(errstr);
0127 end
0128
0129
0130 Va = bus(:, VA) * (pi/180);
0131 Vm = bus(:, VM);
0132 Vm(gen(:, GEN_BUS)) = gen(:, VG);
0133 Pg = gen(:, PG) / baseMVA;
0134 Qg = gen(:, QG) / baseMVA;
0135 Pmin = gen(:, PMIN) / baseMVA;
0136 Pmax = gen(:, PMAX) / baseMVA;
0137 Qmin = gen(:, QMIN) / baseMVA;
0138 Qmax = gen(:, QMAX) / baseMVA;
0139
0140 if dc
0141
0142 nv = 0;
0143 nq = 0;
0144 q1 = [];
0145
0146
0147 [B, Bf, Pbusinj, Pfinj] = makeBdc(baseMVA, bus, branch);
0148 neg_Cg = sparse(gen(:, GEN_BUS), 1:ng, -1, nb, ng);
0149 Amis = [B neg_Cg];
0150 bmis = -(bus(:, PD) + bus(:, GS)) / baseMVA - Pbusinj;
0151
0152
0153 il = find(branch(:, RATE_A) ~= 0 & branch(:, RATE_A) < 1e10);
0154 nl2 = length(il);
0155 lpf = -Inf * ones(nl2, 1);
0156 upf = branch(il, RATE_A) / baseMVA - Pfinj(il);
0157 upt = branch(il, RATE_A) / baseMVA + Pfinj(il);
0158
0159 user_vars = {'Va', 'Pg'};
0160 ycon_vars = {'Pg', 'y'};
0161 else
0162
0163 nv = nb;
0164 nq = ng;
0165 q1 = 1+ng;
0166
0167
0168 [Avl, lvl, uvl] = makeAvl(baseMVA, gen);
0169
0170
0171 [Apqh, ubpqh, Apql, ubpql, Apqdata] = makeApq(baseMVA, gen);
0172
0173 user_vars = {'Va', 'Vm', 'Pg', 'Qg'};
0174 ycon_vars = {'Pg', 'Qg', 'y'};
0175 end
0176
0177
0178 Vau = Inf * ones(nb, 1);
0179 Val = -Vau;
0180 Vau(refs) = Va(refs);
0181 Val(refs) = Va(refs);
0182
0183
0184 [Aang, lang, uang, iang] = makeAang(baseMVA, branch, nb, mpopt);
0185
0186
0187 if alg == 545 || alg == 550
0188 ny = 0;
0189 Ay = sparse(0, ng+nq);
0190 by =[];
0191 else
0192 ipwl = find(gencost(:, MODEL) == PW_LINEAR);
0193 ny = size(ipwl, 1);
0194 [Ay, by] = makeAy(baseMVA, ng, gencost, 1, q1, 1+ng+nq);
0195 end
0196 if any(gencost(:, MODEL) ~= POLYNOMIAL & gencost(:, MODEL) ~= PW_LINEAR)
0197 error('opf_setup: some generator cost rows have invalid MODEL value');
0198 end
0199
0200
0201
0202 nx = nb+nv + ng+nq;
0203 if nusr
0204 nz = size(mpc.A, 2) - nx;
0205 if nz < 0
0206 error('opf_setup: user supplied A matrix must have at least %d columns.', nx);
0207 end
0208 else
0209 nz = 0;
0210 if nw
0211 if size(mpc.N, 2) ~= nx;
0212 error('opf_setup: user supplied N matrix must have %d columns.', nx);
0213 end
0214 end
0215 end
0216
0217
0218 om = opf_model(mpc);
0219 if ~isempty(pwl1)
0220 om = userdata(om, 'pwl1', pwl1);
0221 end
0222 if dc
0223 om = userdata(om, 'Bf', Bf);
0224 om = userdata(om, 'Pfinj', Pfinj);
0225 om = userdata(om, 'iang', iang);
0226 om = add_vars(om, 'Va', nb, Va, Val, Vau);
0227 om = add_vars(om, 'Pg', ng, Pg, Pmin, Pmax);
0228 om = add_constraints(om, 'Pmis', Amis, bmis, bmis, {'Va', 'Pg'});
0229 om = add_constraints(om, 'Pf', Bf(il,:), lpf, upf, {'Va'});
0230 om = add_constraints(om, 'Pt', -Bf(il,:), lpf, upt, {'Va'});
0231 om = add_constraints(om, 'ang', Aang, lang, uang, {'Va'});
0232 else
0233 om = userdata(om, 'Apqdata', Apqdata);
0234 om = userdata(om, 'iang', iang);
0235 om = add_vars(om, 'Va', nb, Va, Val, Vau);
0236 om = add_vars(om, 'Vm', nb, Vm, bus(:, VMIN), bus(:, VMAX));
0237 om = add_vars(om, 'Pg', ng, Pg, Pmin, Pmax);
0238 om = add_vars(om, 'Qg', ng, Qg, Qmin, Qmax);
0239 om = add_constraints(om, 'Pmis', nb, 'nonlinear');
0240 om = add_constraints(om, 'Qmis', nb, 'nonlinear');
0241 om = add_constraints(om, 'Sf', nl, 'nonlinear');
0242 om = add_constraints(om, 'St', nl, 'nonlinear');
0243 om = add_constraints(om, 'PQh', Apqh, [], ubpqh, {'Pg', 'Qg'});
0244 om = add_constraints(om, 'PQl', Apql, [], ubpql, {'Pg', 'Qg'});
0245 om = add_constraints(om, 'vl', Avl, lvl, uvl, {'Pg', 'Qg'});
0246 om = add_constraints(om, 'ang', Aang, lang, uang, {'Va'});
0247 end
0248
0249
0250 if ny > 0
0251 om = add_vars(om, 'y', ny);
0252 om = add_constraints(om, 'ycon', Ay, [], by, ycon_vars);
0253 end
0254
0255
0256 if nz > 0
0257 om = add_vars(om, 'z', nz, z0, zl, zu);
0258 user_vars{end+1} = 'z';
0259 end
0260 if nusr
0261 om = add_constraints(om, 'usr', mpc.A, lbu, ubu, user_vars);
0262 end
0263 if nw
0264 user_cost.N = mpc.N;
0265 user_cost.Cw = Cw;
0266 if ~isempty(fparm)
0267 user_cost.dd = fparm(:, 1);
0268 user_cost.rh = fparm(:, 2);
0269 user_cost.kk = fparm(:, 3);
0270 user_cost.mm = fparm(:, 4);
0271 end
0272 if ~isempty(H)
0273 user_cost.H = H;
0274 end
0275 om = add_costs(om, 'usr', user_cost, user_vars);
0276 end
0277
0278
0279 om = run_userfcn(userfcn, 'formulation', om);