Home > matpower7.0 > lib > t > t_apply_changes.m

t_apply_changes

PURPOSE ^

T_APPLY_CHANGES Tests for code in apply_changes.m.

SYNOPSIS ^

function t_apply_changes(quiet)

DESCRIPTION ^

T_APPLY_CHANGES  Tests for code in apply_changes.m.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_apply_changes(quiet)
0002 %T_APPLY_CHANGES  Tests for code in apply_changes.m.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2008-2017, Power Systems Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   This file is part of MATPOWER.
0009 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0010 %   See https://matpower.org for more info.
0011 
0012 if nargin < 1
0013     quiet = 0;
0014 end
0015 
0016 n_tests = 576;
0017 
0018 t_begin(n_tests, quiet);
0019 
0020 %% define named indices into data matrices
0021 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0022     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0023 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0024     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0025     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0026 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0027     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0028     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0029 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0030 [CT_LABEL, CT_PROB, CT_TABLE, CT_TBUS, CT_TGEN, CT_TBRCH, CT_TAREABUS, ...
0031     CT_TAREAGEN, CT_TAREABRCH, CT_ROW, CT_COL, CT_CHGTYPE, CT_REP, ...
0032     CT_REL, CT_ADD, CT_NEWVAL, CT_TLOAD, CT_TAREALOAD, CT_LOAD_ALL_PQ, ...
0033     CT_LOAD_FIX_PQ, CT_LOAD_DIS_PQ, CT_LOAD_ALL_P, CT_LOAD_FIX_P, ...
0034     CT_LOAD_DIS_P, CT_TGENCOST, CT_TAREAGENCOST, CT_MODCOST_F, ...
0035     CT_MODCOST_X] = idx_ct;
0036 
0037 mpc = loadcase('t_auction_case');
0038 mpc.gen(8, GEN_BUS) = 2;    %% multiple d. loads per area, same bus as gen
0039 mpc.gen(8, [QG QMIN QMAX]) = [ 3 0 3 ];
0040 %% put load before gen in matrix
0041 mpc.gen = [mpc.gen(8, :); mpc.gen(1:7, :); mpc.gen(9, :)];
0042 ld = find(isload(mpc.gen));
0043 
0044 %% create map of external bus numbers to bus indices
0045 i2e = mpc.bus(:, BUS_I);
0046 e2i = sparse(max(i2e), 1);
0047 e2i(i2e) = (1:size(mpc.bus, 1))';
0048 
0049 for k = 1:3
0050     a{k} = find(mpc.bus(:, BUS_AREA) == k); %% buses in area k
0051     [junk, tmp, junk2] = intersect(e2i(mpc.gen(ld, GEN_BUS)), a{k});
0052     lda{k} = ld(tmp);                       %% disp loads in area k
0053     ga{k} = find(ismember(e2i(mpc.gen(:, GEN_BUS)), a{k}));
0054     tmp1 = find(ismember(e2i(mpc.branch(:, F_BUS)), a{k}));
0055     tmp2 = find(ismember(e2i(mpc.branch(:, T_BUS)), a{k}));
0056     bra{k} = unique([tmp1; tmp2]);
0057 end
0058 for k = 1:3
0059     area(k).fixed.p = sum(mpc.bus(a{k}, PD));
0060     area(k).fixed.q = sum(mpc.bus(a{k}, QD));
0061     area(k).disp.p = -sum(mpc.gen(lda{k}, PMIN));
0062     area(k).disp.qmin = -sum(mpc.gen(lda{k}, QMIN));
0063     area(k).disp.qmax = -sum(mpc.gen(lda{k}, QMAX));
0064     area(k).disp.q = area(k).disp.qmin + area(k).disp.qmax;
0065     area(k).both.p = area(k).fixed.p + area(k).disp.p;
0066     area(k).both.q = area(k).fixed.q + area(k).disp.q;
0067 end
0068 total.fixed.p = sum(mpc.bus(:, PD));
0069 total.fixed.q = sum(mpc.bus(:, QD));
0070 total.disp.p = -sum(mpc.gen(ld, PMIN));
0071 total.disp.qmin = -sum(mpc.gen(ld, QMIN));
0072 total.disp.qmax = -sum(mpc.gen(ld, QMAX));
0073 total.disp.q = total.disp.qmin + total.disp.qmax;
0074 total.both.p = total.fixed.p + total.disp.p;
0075 total.both.q = total.fixed.q + total.disp.q;
0076 
0077 %% load change table
0078 chgtab = t_chgtab();
0079 
0080 %%-----  bus  -----
0081 t = 'bus, replace';
0082 g = apply_changes(1, mpc, chgtab);
0083 e = mpc;
0084 e.bus(1, GS) = 5;
0085 t_is(g.bus, e.bus, 12, t);
0086 
0087 t = 'bus, scale';
0088 g = apply_changes(2, mpc, chgtab);
0089 e = mpc;
0090 e.bus(2, VMIN) = 1;
0091 t_is(g.bus, e.bus, 12, t);
0092 
0093 t = 'bus, shift';
0094 g = apply_changes(3, mpc, chgtab);
0095 e = mpc;
0096 e.bus(:, VMAX) = mpc.bus(:, VMAX) - 0.01;
0097 t_is(g.bus, e.bus, 12, t);
0098 
0099 %%-----  branch  -----
0100 t = 'branch, replace';
0101 g = apply_changes(4, mpc, chgtab);
0102 e = mpc;
0103 e.branch(1, BR_STATUS) = 0;
0104 t_is(g.branch, e.branch, 12, t);
0105 
0106 t = 'branch, scale';
0107 g = apply_changes(5, mpc, chgtab);
0108 e = mpc;
0109 e.branch(2, RATE_A) = 1000;
0110 t_is(g.branch, e.branch, 12, t);
0111 
0112 t = 'branch, shift';
0113 g = apply_changes(6, mpc, chgtab);
0114 e = mpc;
0115 e.branch(:, RATE_B) = mpc.branch(:, RATE_B) + 0.1;
0116 t_is(g.branch, e.branch, 12, t);
0117 
0118 %%-----  gen  -----
0119 t = 'gen, replace';
0120 g = apply_changes(7, mpc, chgtab);
0121 e = mpc;
0122 e.gen(1, GEN_STATUS) = 0;
0123 t_is(g.gen, e.gen, 12, t);
0124 
0125 t = 'gen, scale';
0126 g = apply_changes(8, mpc, chgtab);
0127 e = mpc;
0128 e.gen(2, QMAX) = 66;
0129 t_is(g.gen, e.gen, 12, t);
0130 
0131 t = 'gen, shift';
0132 g = apply_changes(9, mpc, chgtab);
0133 e = mpc;
0134 e.gen(:, PMIN) = mpc.gen(:, PMIN) + 0.5;
0135 t_is(g.gen, e.gen, 12, t);
0136 
0137 %%-----  gencost  -----
0138 t = 'gencost, replace';
0139 g = apply_changes(78, mpc, chgtab);
0140 e = mpc;
0141 e.gencost(1, STARTUP) = 1000;
0142 t_is(g.gencost, e.gencost, 12, t);
0143 
0144 t = 'gencost, scale';
0145 g = apply_changes(79, mpc, chgtab);
0146 e = mpc;
0147 e.gencost(2, COST+3) = 264;
0148 t_is(g.gencost, e.gencost, 12, t);
0149 
0150 t = 'gencost, shift';
0151 g = apply_changes(80, mpc, chgtab);
0152 e = mpc;
0153 e.gencost(:, NCOST) = 3;
0154 t_is(g.gencost, e.gencost, 12, t);
0155 
0156 t = 'gencost, modcost x, scale';
0157 g = apply_changes(81, mpc, chgtab);
0158 e = mpc;
0159 e.gencost(1, :) = modcost(mpc.gencost(1, :), 1.1, 'SCALE_X');
0160 t_is(g.gencost, e.gencost, 12, t);
0161 
0162 t = 'gencost, modcost x, scale all';
0163 g = apply_changes(82, mpc, chgtab);
0164 e = mpc;
0165 e.gencost = modcost(mpc.gencost, 0.9, 'SCALE_X');
0166 t_is(g.gencost, e.gencost, 12, t);
0167 
0168 t = 'gencost, modcost x, shift';
0169 g = apply_changes(83, mpc, chgtab);
0170 e = mpc;
0171 e.gencost(2, :) = modcost(mpc.gencost(2, :), -10, 'SHIFT_X');
0172 t_is(g.gencost, e.gencost, 12, t);
0173 
0174 t = 'gencost, modcost x, shift all';
0175 g = apply_changes(84, mpc, chgtab);
0176 e = mpc;
0177 e.gencost = modcost(mpc.gencost, 20, 'SHIFT_X');
0178 t_is(g.gencost, e.gencost, 12, t);
0179 
0180 t = 'gencost, modcost f, scale';
0181 g = apply_changes(85, mpc, chgtab);
0182 e = mpc;
0183 e.gencost(1, :) = modcost(mpc.gencost(1, :), 1.1, 'SCALE_F');
0184 t_is(g.gencost, e.gencost, 12, t);
0185 
0186 t = 'gencost, modcost f, scale all';
0187 g = apply_changes(86, mpc, chgtab);
0188 e = mpc;
0189 e.gencost = modcost(mpc.gencost, 0.9, 'SCALE_F');
0190 t_is(g.gencost, e.gencost, 12, t);
0191 
0192 t = 'gencost, modcost f, shift';
0193 g = apply_changes(87, mpc, chgtab);
0194 e = mpc;
0195 e.gencost(2, :) = modcost(mpc.gencost(2, :), -10, 'SHIFT_F');
0196 t_is(g.gencost, e.gencost, 12, t);
0197 
0198 t = 'gencost, modcost f, shift all';
0199 g = apply_changes(88, mpc, chgtab);
0200 e = mpc;
0201 e.gencost = modcost(mpc.gencost, 20, 'SHIFT_F');
0202 t_is(g.gencost, e.gencost, 12, t);
0203 
0204 %%-----  area bus  -----
0205 t = 'area bus : replace';
0206 g = apply_changes(10, mpc, chgtab);
0207 e = mpc;
0208 e.bus(a{1}, VMAX) = 1.1;
0209 t_is(g.bus, e.bus, 12, t);
0210 
0211 t = 'area bus, scale';
0212 g = apply_changes(11, mpc, chgtab);
0213 e = mpc;
0214 e.bus(a{2}, VMAX) = e.bus(a{2}, VMAX) * 1.01;
0215 t_is(g.bus, e.bus, 12, t);
0216 
0217 t = 'area bus, shift';
0218 g = apply_changes(12, mpc, chgtab);
0219 e = mpc;
0220 e.bus(a{3}, VMAX) = mpc.bus(a{3}, VMAX) + 0.1;
0221 t_is(g.bus, e.bus, 12, t);
0222 
0223 %%-----  area branch  -----
0224 t = 'area branch : replace';
0225 g = apply_changes(13, mpc, chgtab);
0226 e = mpc;
0227 e.branch(bra{1}, RATE_B) = 100;
0228 t_is(g.branch, e.branch, 12, t);
0229 
0230 t = 'area branch, scale';
0231 g = apply_changes(14, mpc, chgtab);
0232 e = mpc;
0233 e.branch(bra{2}, RATE_B) = e.branch(bra{2}, RATE_B) * 1.1;
0234 t_is(g.branch, e.branch, 12, t);
0235 
0236 t = 'area branch, shift';
0237 g = apply_changes(15, mpc, chgtab);
0238 e = mpc;
0239 e.branch(bra{3}, RATE_B) = mpc.branch(bra{3}, RATE_B) + 0.5;
0240 t_is(g.branch, e.branch, 12, t);
0241 
0242 %%-----  area gen  -----
0243 t = 'area gen : replace';
0244 g = apply_changes(16, mpc, chgtab);
0245 e = mpc;
0246 e.gen(ga{1}, PMIN) = 0;
0247 t_is(g.gen, e.gen, 12, t);
0248 
0249 t = 'area gen, scale';
0250 g = apply_changes(17, mpc, chgtab);
0251 e = mpc;
0252 e.gen(ga{2}, PMIN) = e.gen(ga{2}, PMIN) * 1.1;
0253 t_is(g.gen, e.gen, 12, t);
0254 
0255 t = 'area gen, shift';
0256 g = apply_changes(18, mpc, chgtab);
0257 e = mpc;
0258 e.gen(ga{3}, PMIN) = mpc.gen(ga{3}, PMIN) + 0.1;
0259 t_is(g.gen, e.gen, 12, t);
0260 
0261 %%-----  area gencost  -----
0262 t = 'area gencost : replace';
0263 g = apply_changes(89, mpc, chgtab);
0264 e = mpc;
0265 e.gencost(ga{1}, SHUTDOWN) = 500;
0266 t_is(g.gencost, e.gencost, 12, t);
0267 
0268 t = 'area gencost, scale';
0269 g = apply_changes(90, mpc, chgtab);
0270 e = mpc;
0271 e.gencost(ga{2}, COST+5) = e.gencost(ga{2}, COST+5) * 1.1;
0272 t_is(g.gencost, e.gencost, 12, t);
0273 
0274 t = 'area gencost, shift';
0275 g = apply_changes(91, mpc, chgtab);
0276 e = mpc;
0277 e.gencost(ga{3}, NCOST) = 2;
0278 t_is(g.gencost, e.gencost, 12, t);
0279 
0280 t = 'area gencost, modcost x, scale';
0281 g = apply_changes(92, mpc, chgtab);
0282 e = mpc;
0283 e.gencost(ga{1}, :) = modcost(mpc.gencost(ga{1}, :), 1.1, 'SCALE_X');
0284 t_is(g.gencost, e.gencost, 12, t);
0285 
0286 t = 'area gencost, modcost x, shift';
0287 g = apply_changes(93, mpc, chgtab);
0288 e = mpc;
0289 e.gencost(ga{2}, :) = modcost(mpc.gencost(ga{2}, :), -10, 'SHIFT_X');
0290 t_is(g.gencost, e.gencost, 12, t);
0291 
0292 t = 'area gencost, modcost f, scale';
0293 g = apply_changes(94, mpc, chgtab);
0294 e = mpc;
0295 e.gencost(ga{1}, :) = modcost(mpc.gencost(ga{1}, :), 1.1, 'SCALE_F');
0296 t_is(g.gencost, e.gencost, 12, t);
0297 
0298 t = 'area gencost, modcost f, shift';
0299 g = apply_changes(95, mpc, chgtab);
0300 e = mpc;
0301 e.gencost(ga{2}, :) = modcost(mpc.gencost(ga{2}, :), -10, 'SHIFT_F');
0302 t_is(g.gencost, e.gencost, 12, t);
0303 
0304 %%-----  single load zone, one scale factor  -----
0305 dmd = 2;
0306 t = 'all fixed loads (PQ) * 2 : ';
0307 e = mpc;
0308 g = apply_changes(20, mpc, chgtab);
0309 t_is(sum(g.bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0310 t_is(sum(g.bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0311 t_is(-sum(g.gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0312 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0313 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0314 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0315 
0316 t = 'all fixed loads (P) * 2 : ';
0317 e = mpc;
0318 g = apply_changes(21, mpc, chgtab);
0319 t_is(sum(g.bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0320 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0321 t_is(-sum(g.gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0322 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0323 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0324 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0325 
0326 t = 'all loads (PQ) * 2 : ';
0327 e = mpc;
0328 g = apply_changes(22, mpc, chgtab);
0329 t_is(sum(g.bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0330 t_is(sum(g.bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0331 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0332 t_is(-sum(g.gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0333 t_is(-sum(g.gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0334 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0335 
0336 t = 'all loads (P) * 2 : ';
0337 e = mpc;
0338 g = apply_changes(23, mpc, chgtab);
0339 t_is(sum(g.bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0340 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0341 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0342 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0343 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0344 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0345 
0346 t = 'all disp loads (PQ) * 2 : ';
0347 e = mpc;
0348 g = apply_changes(24, mpc, chgtab);
0349 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0350 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0351 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0352 t_is(-sum(g.gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0353 t_is(-sum(g.gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0354 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0355 
0356 t = 'all disp loads (P) * 2 : ';
0357 e = mpc;
0358 g = apply_changes(25, mpc, chgtab);
0359 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0360 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0361 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0362 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0363 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0364 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0365 
0366 t = 'all loads+cost (PQ) * 2 : ';
0367 e = mpc;
0368 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_F');
0369 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_X');
0370 g = apply_changes(122, mpc, chgtab);
0371 t_is(sum(g.bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0372 t_is(sum(g.bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0373 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0374 t_is(-sum(g.gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0375 t_is(-sum(g.gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0376 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0377 
0378 t = 'all loads+cost (P) * 2 : ';
0379 e = mpc;
0380 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_F');
0381 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_X');
0382 g = apply_changes(123, mpc, chgtab);
0383 t_is(sum(g.bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0384 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0385 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0386 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0387 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0388 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0389 
0390 t = 'all disp loads+cost (PQ) * 2 : ';
0391 e = mpc;
0392 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_F');
0393 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_X');
0394 g = apply_changes(124, mpc, chgtab);
0395 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0396 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0397 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0398 t_is(-sum(g.gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0399 t_is(-sum(g.gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0400 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0401 
0402 t = 'all disp loads+cost (P) * 2 : ';
0403 e = mpc;
0404 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_F');
0405 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd, 'SCALE_X');
0406 g = apply_changes(125, mpc, chgtab);
0407 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0408 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0409 t_is(-sum(g.gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0410 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0411 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0412 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0413 
0414 %%-----  single load zone, one replace quantity  -----
0415 dmd = 200;
0416 t = 'all fixed loads (PQ) => total = 200 : ';
0417 e = mpc;
0418 g = apply_changes(26, mpc, chgtab);
0419 t_is(sum(g.bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0420 t_is(sum(g.bus(:, QD)), (dmd-total.disp.p)/total.fixed.p*total.fixed.q, 8, [t 'total fixed Q']);
0421 t_is(-sum(g.gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0422 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0423 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0424 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0425 
0426 t = 'all fixed loads (P) => total = 200 : ';
0427 e = mpc;
0428 g = apply_changes(27, mpc, chgtab);
0429 t_is(sum(g.bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0430 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0431 t_is(-sum(g.gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0432 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0433 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0434 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0435 
0436 t = 'all loads (PQ) => total = 200 : ';
0437 e = mpc;
0438 g = apply_changes(28, mpc, chgtab);
0439 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0440 t_is(sum(g.bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0441 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0442 t_is(-sum(g.gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0443 t_is(-sum(g.gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0444 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0445 
0446 t = 'all loads (P) => total = 200 : ';
0447 e = mpc;
0448 g = apply_changes(29, mpc, chgtab);
0449 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0450 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0451 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0452 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0453 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0454 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0455 
0456 t = 'all disp loads (PQ) => total = 200 : ';
0457 e = mpc;
0458 g = apply_changes(30, mpc, chgtab);
0459 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0460 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0461 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0462 t_is(-sum(g.gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0463 t_is(-sum(g.gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0464 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0465 
0466 t = 'all disp loads (P) => total = 200 : ';
0467 e = mpc;
0468 g = apply_changes(31, mpc, chgtab);
0469 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0470 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0471 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0472 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0473 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0474 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0475 
0476 t = 'all loads+cost (PQ) => total = 200 : ';
0477 e = mpc;
0478 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_F');
0479 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_X');
0480 g = apply_changes(128, mpc, chgtab);
0481 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0482 t_is(sum(g.bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0483 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0484 t_is(-sum(g.gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0485 t_is(-sum(g.gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0486 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0487 
0488 t = 'all loads+cost (P) => total = 200 : ';
0489 e = mpc;
0490 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_F');
0491 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_X');
0492 g = apply_changes(129, mpc, chgtab);
0493 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0494 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0495 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0496 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0497 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0498 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0499 
0500 t = 'all disp loads+cost (PQ) => total = 200 : ';
0501 e = mpc;
0502 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_F');
0503 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_X');
0504 g = apply_changes(130, mpc, chgtab);
0505 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0506 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0507 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0508 t_is(-sum(g.gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0509 t_is(-sum(g.gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0510 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0511 
0512 t = 'all disp loads+cost (P) => total = 200 : ';
0513 e = mpc;
0514 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_F');
0515 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_X');
0516 g = apply_changes(131, mpc, chgtab);
0517 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0518 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0519 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0520 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0521 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0522 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0523 
0524 %%-----  single load zone, one shift quantity  -----
0525 dmd = 25 + total_load(mpc, 'all');
0526 t = 'all fixed loads (PQ) => total + 25 : ';
0527 e = mpc;
0528 g = apply_changes(32, mpc, chgtab);
0529 t_is(sum(g.bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0530 t_is(sum(g.bus(:, QD)), (dmd-total.disp.p)/total.fixed.p*total.fixed.q, 8, [t 'total fixed Q']);
0531 t_is(-sum(g.gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0532 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0533 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0534 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0535 
0536 t = 'all fixed loads (P) => total + 25 : ';
0537 e = mpc;
0538 g = apply_changes(33, mpc, chgtab);
0539 t_is(sum(g.bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0540 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0541 t_is(-sum(g.gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0542 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0543 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0544 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0545 
0546 t = 'all loads (PQ) => total + 25 : ';
0547 e = mpc;
0548 g = apply_changes(34, mpc, chgtab);
0549 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0550 t_is(sum(g.bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0551 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0552 t_is(-sum(g.gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0553 t_is(-sum(g.gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0554 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0555 
0556 t = 'all loads (P) => total + 25 : ';
0557 e = mpc;
0558 g = apply_changes(35, mpc, chgtab);
0559 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0560 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0561 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0562 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0563 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0564 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0565 
0566 t = 'all disp loads (PQ) => total + 25 : ';
0567 e = mpc;
0568 g = apply_changes(36, mpc, chgtab);
0569 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0570 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0571 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0572 t_is(-sum(g.gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0573 t_is(-sum(g.gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0574 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0575 
0576 t = 'all disp loads (P) => total + 25 : ';
0577 e = mpc;
0578 g = apply_changes(37, mpc, chgtab);
0579 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0580 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0581 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0582 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0583 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0584 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0585 
0586 t = 'all loads+cost (PQ) => total + 25 : ';
0587 e = mpc;
0588 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_F');
0589 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_X');
0590 g = apply_changes(134, mpc, chgtab);
0591 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0592 t_is(sum(g.bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0593 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0594 t_is(-sum(g.gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0595 t_is(-sum(g.gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0596 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0597 
0598 t = 'all loads+cost (P) => total + 25 : ';
0599 e = mpc;
0600 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_F');
0601 e.gencost(ld, :) = modcost(e.gencost(ld, :), dmd/total.both.p, 'SCALE_X');
0602 g = apply_changes(135, mpc, chgtab);
0603 t_is(sum(g.bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0604 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0605 t_is(-sum(g.gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0606 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0607 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0608 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0609 
0610 t = 'all disp loads+cost (PQ) => total + 25 : ';
0611 e = mpc;
0612 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_F');
0613 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_X');
0614 g = apply_changes(136, mpc, chgtab);
0615 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0616 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0617 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0618 t_is(-sum(g.gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0619 t_is(-sum(g.gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0620 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0621 
0622 t = 'all disp loads+cost (P) => total + 25 : ';
0623 e = mpc;
0624 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_F');
0625 e.gencost(ld, :) = modcost(e.gencost(ld, :), (dmd-total.fixed.p)/total.disp.p, 'SCALE_X');
0626 g = apply_changes(137, mpc, chgtab);
0627 t_is(sum(g.bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0628 t_is(sum(g.bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0629 t_is(-sum(g.gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0630 t_is(-sum(g.gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0631 t_is(-sum(g.gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0632 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0633 
0634 %%-----  single bus, one scale factor  -----
0635 nb = size(mpc.bus, 1);
0636 ng = size(mpc.gen, 1);
0637 idx1 = [1 3:nb]';
0638 idx2 = (2:ng)';
0639 dmd = 2;
0640 t = 'bus 2 fixed loads (PQ) * 2 : ';
0641 e = mpc;
0642 g = apply_changes(40, mpc, chgtab);
0643 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0644 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0645 t_is(g.bus(2, PD), dmd*mpc.bus(2, PD), 8, [t 'fixed P']);
0646 t_is(g.bus(2, QD), dmd*mpc.bus(2, QD), 8, [t 'fixed Q']);
0647 t_is(g.gen(1, PMIN), mpc.gen(1, PMIN), 8, [t 'disp P']);
0648 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0649 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0650 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0651 
0652 t = 'bus 2 fixed loads (P) * 2 : ';
0653 e = mpc;
0654 g = apply_changes(41, mpc, chgtab);
0655 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0656 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0657 t_is(g.bus(2, PD), dmd*mpc.bus(2, PD), 8, [t 'fixed P']);
0658 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0659 t_is(g.gen(1, PMIN), mpc.gen(1, PMIN), 8, [t 'disp P']);
0660 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0661 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0662 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0663 
0664 t = 'bus 2 loads (PQ) * 2 : ';
0665 e = mpc;
0666 g = apply_changes(42, mpc, chgtab);
0667 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0668 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0669 t_is(g.bus(2, PD), dmd*mpc.bus(2, PD), 8, [t 'fixed P']);
0670 t_is(g.bus(2, QD), dmd*mpc.bus(2, QD), 8, [t 'fixed Q']);
0671 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0672 t_is(g.gen(1, QMIN), dmd*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0673 t_is(g.gen(1, QMAX), dmd*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0674 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0675 
0676 t = 'bus 2 loads (P) * 2 : ';
0677 e = mpc;
0678 g = apply_changes(43, mpc, chgtab);
0679 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0680 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0681 t_is(g.bus(2, PD), dmd*mpc.bus(2, PD), 8, [t 'fixed P']);
0682 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0683 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0684 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0685 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0686 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0687 
0688 t = 'bus 2 disp loads (PQ) * 2 : ';
0689 e = mpc;
0690 g = apply_changes(44, mpc, chgtab);
0691 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0692 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0693 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0694 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0695 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0696 t_is(g.gen(1, QMIN), dmd*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0697 t_is(g.gen(1, QMAX), dmd*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0698 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0699 
0700 t = 'bus 2 disp loads (P) * 2 : ';
0701 e = mpc;
0702 g = apply_changes(45, mpc, chgtab);
0703 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0704 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0705 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0706 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0707 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0708 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0709 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0710 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0711 
0712 t = 'bus 2 loads+cost (PQ) * 2 : ';
0713 e = mpc;
0714 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_F');
0715 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_X');
0716 g = apply_changes(142, mpc, chgtab);
0717 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0718 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0719 t_is(g.bus(2, PD), dmd*mpc.bus(2, PD), 8, [t 'fixed P']);
0720 t_is(g.bus(2, QD), dmd*mpc.bus(2, QD), 8, [t 'fixed Q']);
0721 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0722 t_is(g.gen(1, QMIN), dmd*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0723 t_is(g.gen(1, QMAX), dmd*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0724 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0725 
0726 t = 'bus 2 loads+cost (P) * 2 : ';
0727 e = mpc;
0728 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_F');
0729 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_X');
0730 g = apply_changes(143, mpc, chgtab);
0731 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0732 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0733 t_is(g.bus(2, PD), dmd*mpc.bus(2, PD), 8, [t 'fixed P']);
0734 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0735 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0736 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0737 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0738 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0739 
0740 t = 'bus 2 disp loads+cost (PQ) * 2 : ';
0741 e = mpc;
0742 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_F');
0743 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_X');
0744 g = apply_changes(144, mpc, chgtab);
0745 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0746 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0747 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0748 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0749 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0750 t_is(g.gen(1, QMIN), dmd*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0751 t_is(g.gen(1, QMAX), dmd*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0752 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0753 
0754 t = 'bus 2 disp loads+cost (P) * 2 : ';
0755 e = mpc;
0756 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_F');
0757 e.gencost(1, :) = modcost(e.gencost(1, :), dmd, 'SCALE_X');
0758 g = apply_changes(145, mpc, chgtab);
0759 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0760 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0761 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0762 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0763 t_is(g.gen(1, PMIN), dmd*mpc.gen(1, PMIN), 8, [t 'disp P']);
0764 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0765 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0766 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0767 
0768 %%-----  single bus, one replace quantity  -----
0769 dmd = 50;
0770 t = 'bus 2 fixed loads (PQ) => Pd = 50 : ';
0771 scale = (dmd+mpc.gen(1, PMIN)) / mpc.bus(2, PD);
0772 e = mpc;
0773 g = apply_changes(46, mpc, chgtab);
0774 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0775 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0776 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0777 t_is(g.bus(2, QD), scale*mpc.bus(2, QD), 8, [t 'fixed Q']);
0778 t_is(g.gen(1, PMIN), mpc.gen(1, PMIN), 8, [t 'disp P']);
0779 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0780 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0781 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0782 
0783 t = 'bus 2 fixed loads (P) => Pd = 50 : ';
0784 e = mpc;
0785 g = apply_changes(47, mpc, chgtab);
0786 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0787 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0788 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0789 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0790 t_is(g.gen(1, PMIN), mpc.gen(1, PMIN), 8, [t 'disp P']);
0791 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0792 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0793 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0794 
0795 t = 'bus 2 loads (PQ) => Pd = 50 : ';
0796 scale = dmd / (mpc.bus(2, PD) - mpc.gen(1, PMIN));
0797 e = mpc;
0798 g = apply_changes(48, mpc, chgtab);
0799 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0800 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0801 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0802 t_is(g.bus(2, QD), scale*mpc.bus(2, QD), 8, [t 'fixed Q']);
0803 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0804 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0805 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0806 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0807 
0808 t = 'bus 2 loads (P) => Pd = 50 : ';
0809 e = mpc;
0810 g = apply_changes(49, mpc, chgtab);
0811 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0812 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0813 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0814 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0815 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0816 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0817 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0818 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0819 
0820 t = 'bus 2 disp loads (PQ) => Pd = 50 : ';
0821 scale = (dmd - mpc.bus(2, PD)) / -mpc.gen(1, PMIN);
0822 e = mpc;
0823 g = apply_changes(50, mpc, chgtab);
0824 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0825 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0826 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0827 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0828 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0829 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0830 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0831 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0832 
0833 t = 'bus 2 disp loads (P) => Pd = 50 : ';
0834 e = mpc;
0835 g = apply_changes(51, mpc, chgtab);
0836 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0837 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0838 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0839 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0840 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0841 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0842 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0843 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0844 
0845 t = 'bus 2 loads+cost (PQ) => Pd = 50 : ';
0846 scale = dmd / (mpc.bus(2, PD) - mpc.gen(1, PMIN));
0847 e = mpc;
0848 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
0849 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
0850 g = apply_changes(148, mpc, chgtab);
0851 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0852 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0853 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0854 t_is(g.bus(2, QD), scale*mpc.bus(2, QD), 8, [t 'fixed Q']);
0855 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0856 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0857 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0858 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0859 
0860 t = 'bus 2 loads+cost (P) => Pd = 50 : ';
0861 e = mpc;
0862 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
0863 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
0864 g = apply_changes(149, mpc, chgtab);
0865 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0866 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0867 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0868 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0869 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0870 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0871 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0872 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0873 
0874 t = 'bus 2 disp loads+cost (PQ) => Pd = 50 : ';
0875 scale = (dmd - mpc.bus(2, PD)) / -mpc.gen(1, PMIN);
0876 e = mpc;
0877 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
0878 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
0879 g = apply_changes(150, mpc, chgtab);
0880 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0881 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0882 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0883 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0884 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0885 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0886 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0887 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0888 
0889 t = 'bus 2 disp loads+cost (P) => Pd = 50 : ';
0890 e = mpc;
0891 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
0892 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
0893 g = apply_changes(151, mpc, chgtab);
0894 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0895 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0896 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0897 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0898 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0899 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0900 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0901 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0902 
0903 %%-----  single bus, one shift quantity  -----
0904 dmd = 10 + (mpc.bus(2, PD) - mpc.gen(1, PMIN));
0905 t = 'bus 2 fixed loads (PQ) => total + 25 : ';
0906 scale = (dmd+mpc.gen(1, PMIN)) / mpc.bus(2, PD);
0907 e = mpc;
0908 g = apply_changes(52, mpc, chgtab);
0909 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0910 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0911 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0912 t_is(g.bus(2, QD), scale*mpc.bus(2, QD), 8, [t 'fixed Q']);
0913 t_is(g.gen(1, PMIN), mpc.gen(1, PMIN), 8, [t 'disp P']);
0914 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0915 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0916 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0917 
0918 t = 'bus 2 fixed loads (P) => total + 25 : ';
0919 e = mpc;
0920 g = apply_changes(53, mpc, chgtab);
0921 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0922 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0923 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0924 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0925 t_is(g.gen(1, PMIN), mpc.gen(1, PMIN), 8, [t 'disp P']);
0926 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0927 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0928 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0929 
0930 t = 'bus 2 loads (PQ) => total + 25 : ';
0931 scale = dmd / (mpc.bus(2, PD) - mpc.gen(1, PMIN));
0932 e = mpc;
0933 g = apply_changes(54, mpc, chgtab);
0934 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0935 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0936 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0937 t_is(g.bus(2, QD), scale*mpc.bus(2, QD), 8, [t 'fixed Q']);
0938 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0939 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0940 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0941 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0942 
0943 t = 'bus 2 loads (P) => total + 25 : ';
0944 e = mpc;
0945 g = apply_changes(55, mpc, chgtab);
0946 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0947 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0948 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0949 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0950 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0951 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0952 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0953 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0954 
0955 t = 'bus 2 disp loads (PQ) => total + 25 : ';
0956 scale = (dmd - mpc.bus(2, PD)) / -mpc.gen(1, PMIN);
0957 e = mpc;
0958 g = apply_changes(56, mpc, chgtab);
0959 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0960 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0961 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0962 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0963 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0964 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0965 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0966 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0967 
0968 t = 'bus 2 disp loads (P) => total + 25 : ';
0969 e = mpc;
0970 g = apply_changes(57, mpc, chgtab);
0971 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0972 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0973 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
0974 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
0975 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0976 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0977 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0978 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0979 
0980 t = 'bus 2 loads+cost (PQ) => total + 25 : ';
0981 scale = dmd / (mpc.bus(2, PD) - mpc.gen(1, PMIN));
0982 e = mpc;
0983 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
0984 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
0985 g = apply_changes(154, mpc, chgtab);
0986 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
0987 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
0988 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
0989 t_is(g.bus(2, QD), scale*mpc.bus(2, QD), 8, [t 'fixed Q']);
0990 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
0991 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
0992 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
0993 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
0994 
0995 t = 'bus 2 loads+cost (P) => total + 25 : ';
0996 e = mpc;
0997 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
0998 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
0999 g = apply_changes(155, mpc, chgtab);
1000 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
1001 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
1002 t_is(g.bus(2, PD), scale*mpc.bus(2, PD), 8, [t 'fixed P']);
1003 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
1004 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
1005 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
1006 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
1007 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
1008 
1009 t = 'bus 2 disp loads+cost (PQ) => total + 25 : ';
1010 scale = (dmd - mpc.bus(2, PD)) / -mpc.gen(1, PMIN);
1011 e = mpc;
1012 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
1013 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
1014 g = apply_changes(156, mpc, chgtab);
1015 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
1016 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
1017 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
1018 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
1019 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
1020 t_is(g.gen(1, QMIN), scale*mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
1021 t_is(g.gen(1, QMAX), scale*mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
1022 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
1023 
1024 t = 'bus 2 disp loads+cost (P) => total + 25 : ';
1025 e = mpc;
1026 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_F');
1027 e.gencost(1, :) = modcost(e.gencost(1, :), scale, 'SCALE_X');
1028 g = apply_changes(157, mpc, chgtab);
1029 t_is(g.bus(idx1, :), mpc.bus(idx1, :), 12, [t 'bus']);
1030 t_is(g.gen(idx2, :), mpc.gen(idx2, :), 12, [t 'gen']);
1031 t_is(g.bus(2, PD), mpc.bus(2, PD), 8, [t 'fixed P']);
1032 t_is(g.bus(2, QD), mpc.bus(2, QD), 8, [t 'fixed Q']);
1033 t_is(g.gen(1, PMIN), scale*mpc.gen(1, PMIN), 8, [t 'disp P']);
1034 t_is(g.gen(1, QMIN), mpc.gen(1, QMIN), 8, [t 'disp Qmin']);
1035 t_is(g.gen(1, QMAX), mpc.gen(1, QMAX), 8, [t 'disp Qmax']);
1036 t_is(g.gencost(ld, :), e.gencost(ld, :), 8, [t 'disp gencost']);
1037 
1038 %%-----  1 zone, area scale factor  -----
1039 t = 'area fixed loads (PQ) * [3 1 1] : ';
1040 dmd = [3 1 1];
1041 e = mpc;
1042 g = apply_changes(60, mpc, chgtab);
1043 for k = 1:length(dmd)
1044     if k > 1
1045         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1046         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1047     else
1048         t_is(sum(g.bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1049         t_is(sum(g.bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1050         t_is(-sum(g.gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1051         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1052         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1053     end
1054 end
1055 
1056 t = 'area fixed loads (P) * [3 1 1] : ';
1057 e = mpc;
1058 g = apply_changes(61, mpc, chgtab);
1059 for k = 1:length(dmd)
1060     if k > 1
1061         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1062         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1063     else
1064         t_is(sum(g.bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1065         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1066         t_is(-sum(g.gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1067         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1068         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1069     end
1070 end
1071 
1072 t = 'all area loads (PQ) * [3 1 1] : ';
1073 e = mpc;
1074 g = apply_changes(62, mpc, chgtab);
1075 for k = 1:length(dmd)
1076     if k > 1
1077         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1078         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1079     else
1080         t_is(sum(g.bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1081         t_is(sum(g.bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1082         t_is(-sum(g.gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1083         t_is(-sum(g.gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1084         t_is(-sum(g.gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1085     end
1086 end
1087 
1088 t = 'all area loads (P) * [3 1 1] : ';
1089 e = mpc;
1090 g = apply_changes(63, mpc, chgtab);
1091 for k = 1:length(dmd)
1092     if k > 1
1093         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1094         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1095     else
1096         t_is(sum(g.bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1097         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1098         t_is(-sum(g.gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1099         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1100         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1101     end
1102 end
1103 
1104 t = 'area disp loads (PQ) * [3 1 1] : ';
1105 e = mpc;
1106 g = apply_changes(64, mpc, chgtab);
1107 for k = 1:length(dmd)
1108     if k > 1
1109         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1110         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1111     else
1112         t_is(sum(g.bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1113         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1114         t_is(-sum(g.gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1115         t_is(-sum(g.gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1116         t_is(-sum(g.gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1117     end
1118 end
1119 
1120 t = 'area disp loads (P) * [3 1 1] : ';
1121 e = mpc;
1122 g = apply_changes(65, mpc, chgtab);
1123 for k = 1:length(dmd)
1124     if k > 1
1125         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1126         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1127     else
1128         t_is(sum(g.bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1129         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1130         t_is(-sum(g.gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1131         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1132         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1133     end
1134 end
1135 
1136 %%-----  1 zones, area target quantity  -----
1137 t = 'area fixed loads (PQ) => total = 100 : ';
1138 dmd = 100;
1139 e = mpc;
1140 g = apply_changes(66, mpc, chgtab);
1141 scale = [(dmd-area(1).disp.p) / area(1).fixed.p 1 1];
1142 for k = 1:length(dmd)
1143     if k > 1
1144         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1145         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1146     else
1147         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1148         t_is(sum(g.bus(a{k}, QD)), scale(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1149         t_is(-sum(g.gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1150         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1151         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1152     end
1153 end
1154 
1155 t = 'area fixed loads (P) => total = 100 : ';
1156 e = mpc;
1157 g = apply_changes(67, mpc, chgtab);
1158 for k = 1:length(dmd)
1159     if k > 1
1160         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1161         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1162     else
1163         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1164         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1165         t_is(-sum(g.gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1166         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1167         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1168     end
1169 end
1170 
1171 t = 'all area loads (PQ) => total = 100 : ';
1172 e = mpc;
1173 g = apply_changes(68, mpc, chgtab);
1174 scale = [dmd / (area(1).fixed.p + area(1).disp.p) 1 1];
1175 for k = 1:length(dmd)
1176     if k > 1
1177         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1178         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1179     else
1180         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1181         t_is(sum(g.bus(a{k}, QD)), scale(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1182         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1183         t_is(-sum(g.gen(lda{k}, QMIN)), scale(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1184         t_is(-sum(g.gen(lda{k}, QMAX)), scale(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1185     end
1186 end
1187 
1188 t = 'all area loads (P) => total = 100 : ';
1189 e = mpc;
1190 g = apply_changes(69, mpc, chgtab);
1191 for k = 1:length(dmd)
1192     if k > 1
1193         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1194         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1195     else
1196         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1197         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1198         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1199         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1200         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1201     end
1202 end
1203 
1204 t = 'area disp loads (PQ) => total = 100 : ';
1205 e = mpc;
1206 g = apply_changes(70, mpc, chgtab);
1207 scale = [(dmd - area(1).fixed.p) / area(1).disp.p 1 1];
1208 for k = 1:length(dmd)
1209     if k > 1
1210         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1211         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1212     else
1213         t_is(sum(g.bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1214         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1215         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1216         t_is(-sum(g.gen(lda{k}, QMIN)), scale(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1217         t_is(-sum(g.gen(lda{k}, QMAX)), scale(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1218     end
1219 end
1220 
1221 t = 'area disp loads (P) => total = 100 : ';
1222 e = mpc;
1223 g = apply_changes(71, mpc, chgtab);
1224 for k = 1:length(dmd)
1225     if k > 1
1226         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1227         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1228     else
1229         t_is(sum(g.bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1230         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1231         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1232         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1233         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1234     end
1235 end
1236 
1237 %%-----  1 zones, area shift quantity  -----
1238 t = 'area fixed loads (PQ) => total = total + 20 : ';
1239 dmd = area(1).both.p + 20;
1240 e = mpc;
1241 g = apply_changes(72, mpc, chgtab);
1242 scale = [(dmd-area(1).disp.p) / area(1).fixed.p 1 1];
1243 for k = 1:length(dmd)
1244     if k > 1
1245         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1246         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1247     else
1248         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1249         t_is(sum(g.bus(a{k}, QD)), scale(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1250         t_is(-sum(g.gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1251         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1252         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1253     end
1254 end
1255 
1256 t = 'area fixed loads (P) => total + 20 : ';
1257 e = mpc;
1258 g = apply_changes(73, mpc, chgtab);
1259 for k = 1:length(dmd)
1260     if k > 1
1261         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1262         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1263     else
1264         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1265         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1266         t_is(-sum(g.gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1267         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1268         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1269     end
1270 end
1271 
1272 t = 'all area loads (PQ) => total + 20 : ';
1273 e = mpc;
1274 g = apply_changes(74, mpc, chgtab);
1275 scale = [dmd / (area(1).fixed.p + area(1).disp.p) 1 1];
1276 for k = 1:length(dmd)
1277     if k > 1
1278         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1279         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1280     else
1281         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1282         t_is(sum(g.bus(a{k}, QD)), scale(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1283         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1284         t_is(-sum(g.gen(lda{k}, QMIN)), scale(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1285         t_is(-sum(g.gen(lda{k}, QMAX)), scale(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1286     end
1287 end
1288 
1289 t = 'all area loads (P) => total + 20 : ';
1290 e = mpc;
1291 g = apply_changes(75, mpc, chgtab);
1292 for k = 1:length(dmd)
1293     if k > 1
1294         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1295         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1296     else
1297         t_is(sum(g.bus(a{k}, PD)), scale(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1298         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1299         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1300         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1301         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1302     end
1303 end
1304 
1305 t = 'area disp loads (PQ) => total + 20 : ';
1306 e = mpc;
1307 g = apply_changes(76, mpc, chgtab);
1308 scale = [(dmd - area(1).fixed.p) / area(1).disp.p 1 1];
1309 for k = 1:length(dmd)
1310     if k > 1
1311         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1312         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1313     else
1314         t_is(sum(g.bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1315         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1316         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1317         t_is(-sum(g.gen(lda{k}, QMIN)), scale(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1318         t_is(-sum(g.gen(lda{k}, QMAX)), scale(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1319     end
1320 end
1321 
1322 t = 'area disp loads (P) => total + 20 : ';
1323 e = mpc;
1324 g = apply_changes(77, mpc, chgtab);
1325 for k = 1:length(dmd)
1326     if k > 1
1327         t_is(g.bus(a{k}, :), mpc.bus(a{k}, :), 12, sprintf('%s area %d bus', t, k));
1328         t_is(g.gen(ga{k}, :), mpc.gen(ga{k}, :), 12, sprintf('%s area %d gen', t, k));
1329     else
1330         t_is(sum(g.bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1331         t_is(sum(g.bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1332         t_is(-sum(g.gen(lda{k}, PMIN)), scale(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
1333         t_is(-sum(g.gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1334         t_is(-sum(g.gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1335     end
1336 end
1337 
1338 t = 'savechgtab : ';
1339 tmpfname = sprintf('chgtab_%d', fix(1e9*rand));
1340 tmp_m_name = sprintf('%s.m', tmpfname);
1341 savechgtab(tmpfname, chgtab);
1342 t_ok(exist(tmp_m_name, 'file'), [t 'M-file exists']);
1343 ct = feval(tmpfname);
1344 t_is(ct, chgtab, 5, [t 'table from M-file matches']);
1345 delete(tmp_m_name);
1346 t_ok(~exist(tmp_m_name, 'file'), [t 'M-file successfully deleted']);
1347 
1348 tmpfname = sprintf('chgtab_%d', fix(1e9*rand));
1349 tmp_mat_name = sprintf('%s.mat', tmpfname);
1350 savechgtab(tmp_mat_name, chgtab);
1351 t_ok(exist(tmp_mat_name, 'file'), [t 'MAT-file exists']);
1352 s = load(tmp_mat_name);
1353 t_is(s.chgtab, chgtab, 12, [t 'table from MAT-file matches']);
1354 delete(tmp_mat_name);
1355 t_ok(~exist(tmp_mat_name, 'file'), [t 'MAT-file successfully deleted']);
1356 
1357 t_end;

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005