Home > matpower7.1 > lib > t > t_scale_load.m

t_scale_load

PURPOSE ^

T_SCALE_LOAD Tests for code in SCALE_LOAD.

SYNOPSIS ^

function t_scale_load(quiet)

DESCRIPTION ^

T_SCALE_LOAD  Tests for code in SCALE_LOAD.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_scale_load(quiet)
0002 %T_SCALE_LOAD  Tests for code in SCALE_LOAD.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2008-2016, 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 = 846;
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 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost;
0027 
0028 mpc = loadcase('t_auction_case');
0029 mpc.gen(8, GEN_BUS) = 2;    %% multiple d. loads per area, same bus as gen
0030 mpc.gen(8, [QG QMIN QMAX]) = [ 3 0 3 ];
0031 mpc.gencost(7, COST:end) = [-30 -600 -20 -300 -10 -100 0 0];    % 10, 20, 30
0032 mpc.gencost(8, COST:end) = [-30  -60 -20  -30 -10  -10 0 0];    % 1, 2, 3
0033 mpc.gencost(9, COST:end) = [-30 -850 -10 -250  -5  -50 0 0];    % 10, 20, 30
0034 %% split a load in 2 half-size duplicates at same bus ...
0035 mpc.gen(8, [PG PMIN PMAX QG QMIN QMAX]) = mpc.gen(8, [PG PMIN PMAX QG QMIN QMAX]) / 2;
0036 mpc.gencost(8, COST:end) = mpc.gencost(8, COST:end) / 2;
0037 %% ... with one before gens in matrix
0038 mpc.gen = [mpc.gen(8, :); mpc.gen];
0039 mpc.gencost = [mpc.gencost(8, :); mpc.gencost];
0040 gg = find(~isload(mpc.gen));
0041 ld = find( isload(mpc.gen));
0042 for k = 1:3
0043     a{k} = find(mpc.bus(:, BUS_AREA) == k); %% buses in area k
0044     tmp = find(ismember(mpc.gen(ld, GEN_BUS), a{k}));
0045     lda{k} = ld(tmp);                       %% disp loads in area k
0046 end
0047 for k = 1:3
0048     area(k).fixed.p = sum(mpc.bus(a{k}, PD));
0049     area(k).fixed.q = sum(mpc.bus(a{k}, QD));
0050     area(k).disp.p = -sum(mpc.gen(lda{k}, PMIN));
0051     area(k).disp.qmin = -sum(mpc.gen(lda{k}, QMIN));
0052     area(k).disp.qmax = -sum(mpc.gen(lda{k}, QMAX));
0053     area(k).disp.q = area(k).disp.qmin + area(k).disp.qmax;
0054     area(k).both.p = area(k).fixed.p + area(k).disp.p;
0055     area(k).both.q = area(k).fixed.q + area(k).disp.q;
0056 end
0057 total.fixed.p = sum(mpc.bus(:, PD));
0058 total.fixed.q = sum(mpc.bus(:, QD));
0059 total.disp.p = -sum(mpc.gen(ld, PMIN));
0060 total.disp.qmin = -sum(mpc.gen(ld, QMIN));
0061 total.disp.qmax = -sum(mpc.gen(ld, QMAX));
0062 total.disp.q = total.disp.qmin + total.disp.qmax;
0063 total.both.p = total.fixed.p + total.disp.p;
0064 total.both.q = total.fixed.q + total.disp.q;
0065 orig_gc = mpc.gencost;
0066 
0067 %%-----  single load zone, one scale factor  -----
0068 dmd = 2;
0069 t = 'all fixed loads (PQ) * 2 : ';
0070 bus = scale_load(dmd, mpc.bus);
0071 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0072 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0073 opt = struct('which', 'FIXED');
0074 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0075 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0076 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0077 t_is(-sum(gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0078 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0079 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0080 
0081 t = 'all fixed loads (P) * 2 : ';
0082 opt = struct('pq', 'P');
0083 bus = scale_load(dmd, mpc.bus, [], [], opt);
0084 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0085 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0086 opt = struct('pq', 'P', 'which', 'FIXED');
0087 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0088 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0089 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0090 t_is(-sum(gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0091 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0092 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0093 
0094 t = 'all loads (PQ) * 2 : ';
0095 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen);
0096 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0097 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0098 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0099 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0100 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0101 
0102 t = 'all loads/costs (PQ) * 2 : ';
0103 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], [], mpc.gencost);
0104 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0105 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0106 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0107 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0108 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0109 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0110 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0111 
0112 t = 'all loads/costs (PQ) * 2 : ';
0113 opt = struct('cost', 1);
0114 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0115 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0116 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0117 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0118 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0119 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0120 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0121 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0122 
0123 t = 'all loads (PQ) * 2 : ';
0124 opt = struct('cost', 0);
0125 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0126 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0127 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0128 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0129 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0130 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0131 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0132 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0133 
0134 t = 'all loads (P) * 2 : ';
0135 opt = struct('pq', 'P');
0136 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0137 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0138 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0139 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0140 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0141 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0142 
0143 t = 'all loads/costs (P) * 2 : ';
0144 opt = struct('pq', 'P');
0145 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0146 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0147 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0148 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0149 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0150 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0151 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0152 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0153 
0154 t = 'all disp loads (PQ) * 2 : ';
0155 opt = struct('which', 'DISPATCHABLE');
0156 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0157 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0158 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0159 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0160 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0161 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0162 
0163 t = 'all disp loads/costs (PQ) * 2 : ';
0164 opt = struct('which', 'DISPATCHABLE');
0165 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0166 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0167 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0168 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0169 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0170 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0171 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0172 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0173 
0174 t = 'all disp loads (P) * 2 : ';
0175 opt = struct('pq', 'P', 'which', 'DISPATCHABLE');
0176 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0177 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0178 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0179 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0180 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0181 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0182 
0183 t = 'all disp loads/costs (P) * 2 : ';
0184 opt = struct('pq', 'P', 'which', 'DISPATCHABLE');
0185 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0186 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0187 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0188 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0189 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0190 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0191 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0192 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0193 
0194 %%-----  single load zone, one scale quantity  -----
0195 dmd = 200;
0196 t = 'all fixed loads (PQ) => total = 200 : ';
0197 opt = struct('scale', 'QUANTITY');
0198 bus = scale_load(dmd, mpc.bus, [], [], opt);
0199 t_is(sum(bus(:, PD)), dmd, 8, [t 'total fixed P']);
0200 t_is(sum(bus(:, QD)), dmd/total.fixed.p*total.fixed.q, 8, [t 'total fixed Q']);
0201 opt = struct('scale', 'QUANTITY', 'which', 'FIXED');
0202 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0203 t_is(sum(bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0204 t_is(sum(bus(:, QD)), (dmd-total.disp.p)/total.fixed.p*total.fixed.q, 8, [t 'total fixed Q']);
0205 t_is(-sum(gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0206 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0207 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0208 
0209 t = 'all fixed loads (P) => total = 200 : ';
0210 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0211 bus = scale_load(dmd, mpc.bus, [], [], opt);
0212 t_is(sum(bus(:, PD)), dmd, 8, [t 'total fixed P']);
0213 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0214 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'FIXED');
0215 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0216 t_is(sum(bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0217 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0218 t_is(-sum(gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0219 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0220 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0221 
0222 t = 'all loads (PQ) => total = 200 : ';
0223 opt = struct('scale', 'QUANTITY');
0224 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0225 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0226 t_is(sum(bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0227 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0228 t_is(-sum(gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0229 t_is(-sum(gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0230 
0231 t = 'all loads/costs (PQ) => total = 200 : ';
0232 opt = struct('scale', 'QUANTITY');
0233 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0234 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0235 t_is(sum(bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0236 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0237 t_is(-sum(gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0238 t_is(-sum(gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0239 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0240 t_is(gencost(ld, COST:end), dmd/total.both.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0241 
0242 t = 'all loads (P) => total = 200 : ';
0243 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0244 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0245 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0246 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0247 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0248 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0249 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0250 
0251 t = 'all loads/costs (P) => total = 200 : ';
0252 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0253 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0254 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0255 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0256 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0257 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0258 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0259 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0260 t_is(gencost(ld, COST:end), dmd/total.both.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0261 
0262 t = 'all disp loads (PQ) => total = 200 : ';
0263 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0264 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0265 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0266 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0267 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0268 t_is(-sum(gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0269 t_is(-sum(gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0270 
0271 t = 'all disp loads/costs (PQ) => total = 200 : ';
0272 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0273 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0274 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0275 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0276 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0277 t_is(-sum(gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0278 t_is(-sum(gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0279 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0280 t_is(gencost(ld, COST:end), (dmd-total.fixed.p)/total.disp.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0281 
0282 t = 'all disp loads (P) => total = 200 : ';
0283 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'DISPATCHABLE');
0284 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0285 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0286 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0287 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0288 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0289 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0290 
0291 t = 'all disp loads/costs (P) => total = 200 : ';
0292 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'DISPATCHABLE');
0293 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0294 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0295 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0296 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0297 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0298 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0299 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0300 t_is(gencost(ld, COST:end), (dmd-total.fixed.p)/total.disp.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0301 
0302 %%-----  3 zones, area scale factors  -----
0303 t = 'area fixed loads (PQ) * [3 2 1] : ';
0304 dmd = [3 2 1];
0305 bus = scale_load(dmd, mpc.bus);
0306 for k = 1:length(dmd)
0307     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0308     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0309 end
0310 opt = struct('which', 'FIXED');
0311 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0312 for k = 1:length(dmd)
0313     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0314     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0315     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0316     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0317     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0318 end
0319 
0320 t = 'area fixed loads (P) * [3 2 1] : ';
0321 dmd = [3 2 1];
0322 opt = struct('pq', 'P');
0323 bus = scale_load(dmd, mpc.bus, [], [], opt);
0324 for k = 1:length(dmd)
0325     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0326     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0327 end
0328 opt = struct('pq', 'P', 'which', 'FIXED');
0329 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0330 for k = 1:length(dmd)
0331     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0332     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0333     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0334     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0335     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0336 end
0337 
0338 t = 'all area loads (PQ) * [3 2 1] : ';
0339 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen);
0340 for k = 1:length(dmd)
0341     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0342     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0343     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0344     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0345     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0346 end
0347 
0348 t = 'all area loads/costs (PQ) * [3 2 1] : ';
0349 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], [], mpc.gencost);
0350 for k = 1:length(dmd)
0351     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0352     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0353     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0354     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0355     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0356     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0357 end
0358 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0359 
0360 t = 'all area loads (P) * [3 2 1] : ';
0361 opt = struct('pq', 'P');
0362 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0363 for k = 1:length(dmd)
0364     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0365     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0366     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0367     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0368     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0369 end
0370 
0371 t = 'all area loads/costs (P) * [3 2 1] : ';
0372 opt = struct('pq', 'P');
0373 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0374 for k = 1:length(dmd)
0375     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0376     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0377     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0378     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0379     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0380     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0381 end
0382 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0383 
0384 t = 'area disp loads (PQ) * [3 2 1] : ';
0385 opt = struct('which', 'DISPATCHABLE');
0386 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0387 for k = 1:length(dmd)
0388     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0389     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0390     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0391     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0392     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0393 end
0394 
0395 t = 'area disp loads/costs (PQ) * [3 2 1] : ';
0396 opt = struct('which', 'DISPATCHABLE');
0397 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0398 for k = 1:length(dmd)
0399     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0400     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0401     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0402     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0403     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0404     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0405 end
0406 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0407 
0408 t = 'area disp loads (P) * [3 2 1] : ';
0409 opt = struct('pq', 'P', 'which', 'DISPATCHABLE');
0410 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0411 for k = 1:length(dmd)
0412     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0413     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0414     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0415     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0416     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0417 end
0418 
0419 t = 'area disp loads/costs (P) * [3 2 1] : ';
0420 opt = struct('pq', 'P', 'which', 'DISPATCHABLE');
0421 [bus, gen, gencost] = scale_load(dmd, mpc.bus, mpc.gen, [], opt, mpc.gencost);
0422 for k = 1:length(dmd)
0423     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0424     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0425     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0426     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0427     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0428     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0429 end
0430 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0431 
0432 %%-----  3 zones, area scale quantities  -----
0433 t = 'area fixed loads (PQ) => total = [100 80 60] : ';
0434 dmd = [100 80 60];
0435 opt = struct('scale', 'QUANTITY');
0436 bus = scale_load(dmd, mpc.bus, [], [], opt);
0437 for k = 1:length(dmd)
0438     t_is(sum(bus(a{k}, PD)), dmd(k), 8, sprintf('%s area %d fixed P', t, k));
0439     t_is(sum(bus(a{k}, QD)), dmd(k)/area(k).fixed.p*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0440 end
0441 opt = struct('scale', 'QUANTITY', 'which', 'FIXED');
0442 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0443 for k = 1:length(dmd)
0444     t_is(sum(bus(a{k}, PD)), dmd(k)-area(k).disp.p, 8, sprintf('%s area %d fixed P', t, k));
0445     t_is(sum(bus(a{k}, QD)), (dmd(k)-area(k).disp.p)/area(k).fixed.p*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0446     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0447     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0448     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0449 end
0450 
0451 t = 'area fixed loads (P) => total = [100 80 60] : ';
0452 dmd = [100 80 60];
0453 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0454 bus = scale_load(dmd, mpc.bus, [], [], opt);
0455 for k = 1:length(dmd)
0456     t_is(sum(bus(a{k}, PD)), dmd(k), 8, sprintf('%s area %d fixed P', t, k));
0457     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0458 end
0459 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'FIXED');
0460 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0461 for k = 1:length(dmd)
0462     t_is(sum(bus(a{k}, PD)), dmd(k)-area(k).disp.p, 8, sprintf('%s area %d fixed P', t, k));
0463     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0464     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0465     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0466     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0467 end
0468 
0469 t = 'all area loads (PQ) => total = [100 80 60] : ';
0470 opt = struct('scale', 'QUANTITY');
0471 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0472 for k = 1:length(dmd)
0473     t_is(sum(bus(a{k}, PD)), dmd(k)/area(k).both.p*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0474     t_is(sum(bus(a{k}, QD)), dmd(k)/area(k).both.p*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0475     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)/area(k).both.p*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0476     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)/area(k).both.p*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0477     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)/area(k).both.p*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0478 end
0479 
0480 t = 'all area loads (P) => total = [100 80 60] : ';
0481 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0482 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0483 for k = 1:length(dmd)
0484     t_is(sum(bus(a{k}, PD)), dmd(k)/area(k).both.p*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0485     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0486     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)/area(k).both.p*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0487     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0488     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0489 end
0490 
0491 t = 'area disp loads (PQ) => total = [100 80 60] : throws expected exception';
0492 dmd = [100 80 60];
0493 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0494 err = 0;
0495 try
0496     [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0497 catch
0498     [msg, id] = lasterr;
0499     expected = 'scale_load: impossible to make zone 2 load equal 80 by scaling non-existent dispatchable load';
0500     if ~isempty(strfind(msg, expected))
0501         err = 1;
0502     end
0503 end
0504 t_ok(err, t);
0505 
0506 t = 'area disp loads (PQ) => total = [100 74.3941 60] : ';
0507 dmd = [100 area(2).fixed.p 60];
0508 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0509 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0510 for k = 1:length(dmd)
0511     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0512     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0513     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)-area(k).fixed.p, 8, sprintf('%s area %d disp P', t, k));
0514     if k == 2
0515         t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0516         t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0517     else
0518         t_is(-sum(gen(lda{k}, QMIN)), (dmd(k)-area(k).fixed.p)/area(k).disp.p*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0519         t_is(-sum(gen(lda{k}, QMAX)), (dmd(k)-area(k).fixed.p)/area(k).disp.p*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0520     end
0521 end
0522 
0523 t = 'area disp loads (P) => total = [100 74.3941 60] : ';
0524 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'DISPATCHABLE');
0525 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, [], opt);
0526 for k = 1:length(dmd)
0527     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0528     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0529     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)-area(k).fixed.p, 8, sprintf('%s area %d disp P', t, k));
0530     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0531     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0532 end
0533 
0534 %%-----  explict single load zone  -----
0535 t = 'explicit single load zone';
0536 load_zone = zeros(1, size(mpc.bus, 1));
0537 load_zone([3 4]) = 1;
0538 dmd = 2;
0539 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, load_zone);
0540 Pd = mpc.bus(:, PD);
0541 Pd([3 4]) = dmd * Pd([3 4]);
0542 t_is( bus(:, PD), Pd, 8, t);
0543 
0544 %%-----  explict multiple load zone  -----
0545 t = 'explicit multiple load zone';
0546 load_zone = zeros(1, size(mpc.bus, 1));
0547 load_zone([3 4]) = 1;
0548 load_zone([7 8]) = 2;
0549 dmd = [2 0.5];
0550 [bus, gen] = scale_load(dmd, mpc.bus, mpc.gen, load_zone);
0551 Pd = mpc.bus(:, PD);
0552 Pd([3 4]) = dmd(1) * Pd([3 4]);
0553 Pd([7 8]) = dmd(2) * Pd([7 8]);
0554 t_is( bus(:, PD), Pd, 8, t);
0555 
0556 
0557 
0558 %%------------------------------------------------------------------
0559 %% mostly same tests below, but with MPC input/output
0560 
0561 %%-----  single load zone, one scale factor  -----
0562 dmd = 2;
0563 t = 'all loads (PQ) * 2 : ';
0564 opt = struct('cost', 0);
0565 mpc1 = scale_load(dmd, mpc, [], opt);
0566 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0567 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0568 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0569 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0570 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0571 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0572 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0573 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0574 
0575 t = 'all loads/costs (PQ) * 2 : ';
0576 mpc1 = scale_load(dmd, mpc);
0577 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0578 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0579 t_is(sum(bus(:, QD)), dmd*total.fixed.q, 8, [t 'total fixed Q']);
0580 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0581 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0582 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0583 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0584 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0585 
0586 t = 'all loads (P) * 2 : ';
0587 opt = struct('pq', 'P', 'cost', 0);
0588 mpc1 = scale_load(dmd, mpc, [], opt);
0589 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0590 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0591 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0592 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0593 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0594 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0595 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0596 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0597 
0598 t = 'all loads/costs (P) * 2 : ';
0599 opt = struct('pq', 'P');
0600 mpc1 = scale_load(dmd, mpc, [], opt);
0601 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0602 t_is(sum(bus(:, PD)), dmd*total.fixed.p, 8, [t 'total fixed P']);
0603 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0604 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0605 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0606 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0607 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0608 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0609 
0610 t = 'all disp loads (PQ) * 2 : ';
0611 opt = struct('which', 'DISPATCHABLE', 'cost', 0);
0612 mpc1 = scale_load(dmd, mpc, [], opt);
0613 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0614 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0615 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0616 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0617 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0618 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0619 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0620 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0621 
0622 t = 'all disp loads/costs (PQ) * 2 : ';
0623 opt = struct('which', 'DISPATCHABLE');
0624 mpc1 = scale_load(dmd, mpc, [], opt);
0625 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0626 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0627 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0628 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0629 t_is(-sum(gen(ld, QMIN)), dmd*total.disp.qmin, 8, [t 'total disp Qmin']);
0630 t_is(-sum(gen(ld, QMAX)), dmd*total.disp.qmax, 8, [t 'total disp Qmax']);
0631 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0632 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0633 
0634 t = 'all disp loads (P) * 2 : ';
0635 opt = struct('pq', 'P', 'which', 'DISPATCHABLE', 'cost', 0);
0636 mpc1 = scale_load(dmd, mpc, [], opt);
0637 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0638 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0639 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0640 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0641 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0642 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0643 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0644 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0645 
0646 t = 'all disp loads/costs (P) * 2 : ';
0647 opt = struct('pq', 'P', 'which', 'DISPATCHABLE', 'cost', 1);
0648 mpc1 = scale_load(dmd, mpc, [], opt);
0649 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0650 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0651 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0652 t_is(-sum(gen(ld, PMIN)), dmd*total.disp.p, 8, [t 'total disp P']);
0653 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0654 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0655 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0656 t_is(gencost(ld, COST:end), 2*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0657 
0658 %%-----  single load zone, one scale quantity  -----
0659 dmd = 200;
0660 t = 'all fixed loads (PQ) => total = 200 : ';
0661 opt = struct('scale', 'QUANTITY', 'which', 'FIXED');
0662 mpc1 = scale_load(dmd, mpc, [], opt);
0663 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0664 t_is(sum(bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0665 t_is(sum(bus(:, QD)), (dmd-total.disp.p)/total.fixed.p*total.fixed.q, 8, [t 'total fixed Q']);
0666 t_is(-sum(gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0667 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0668 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0669 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0670 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0671 
0672 t = 'all fixed loads (P) => total = 200 : ';
0673 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'FIXED', 'cost', 1);
0674 mpc1 = scale_load(dmd, mpc, [], opt);
0675 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0676 t_is(sum(bus(:, PD)), dmd-total.disp.p, 8, [t 'total fixed P']);
0677 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0678 t_is(-sum(gen(ld, PMIN)), total.disp.p, 8, [t 'total disp P']);
0679 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0680 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0681 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0682 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0683 
0684 t = 'all loads (PQ) => total = 200 : ';
0685 opt = struct('scale', 'QUANTITY', 'cost', 0);
0686 mpc1 = scale_load(dmd, mpc, [], opt);
0687 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0688 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0689 t_is(sum(bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0690 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0691 t_is(-sum(gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0692 t_is(-sum(gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0693 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0694 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0695 
0696 t = 'all loads/costs (PQ) => total = 200 : ';
0697 opt = struct('scale', 'QUANTITY');
0698 mpc1 = scale_load(dmd, mpc, [], opt);
0699 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0700 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0701 t_is(sum(bus(:, QD)), dmd/total.both.p*total.fixed.q, 8, [t 'total fixed Q']);
0702 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0703 t_is(-sum(gen(ld, QMIN)), dmd/total.both.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0704 t_is(-sum(gen(ld, QMAX)), dmd/total.both.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0705 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0706 t_is(gencost(ld, COST:end), dmd/total.both.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0707 
0708 t = 'all loads (P) => total = 200 : ';
0709 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'cost', 0);
0710 mpc1 = scale_load(dmd, mpc, [], opt);
0711 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0712 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0713 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0714 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0715 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0716 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0717 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0718 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0719 
0720 t = 'all loads/costs (P) => total = 200 : ';
0721 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0722 mpc1 = scale_load(dmd, mpc, [], opt);
0723 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0724 t_is(sum(bus(:, PD)), dmd/total.both.p*total.fixed.p, 8, [t 'total fixed P']);
0725 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0726 t_is(-sum(gen(ld, PMIN)), dmd/total.both.p*total.disp.p, 8, [t 'total disp P']);
0727 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0728 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0729 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0730 t_is(gencost(ld, COST:end), dmd/total.both.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0731 
0732 t = 'all disp loads (PQ) => total = 200 : ';
0733 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE', 'cost', 0);
0734 mpc1 = scale_load(dmd, mpc, [], opt);
0735 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0736 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0737 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0738 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0739 t_is(-sum(gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0740 t_is(-sum(gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0741 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0742 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0743 
0744 t = 'all disp loads/costs (PQ) => total = 200 : ';
0745 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0746 mpc1 = scale_load(dmd, mpc, [], opt);
0747 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0748 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0749 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0750 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0751 t_is(-sum(gen(ld, QMIN)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmin, 8, [t 'total disp Qmin']);
0752 t_is(-sum(gen(ld, QMAX)), (dmd-total.fixed.p)/total.disp.p*total.disp.qmax, 8, [t 'total disp Qmax']);
0753 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0754 t_is(gencost(ld, COST:end), (dmd-total.fixed.p)/total.disp.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0755 
0756 t = 'all disp loads (P) => total = 200 : ';
0757 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'DISPATCHABLE', 'cost', 0);
0758 mpc1 = scale_load(dmd, mpc, [], opt);
0759 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0760 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0761 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0762 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0763 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0764 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0765 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0766 t_is(gencost(ld, COST:end),   orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0767 
0768 t = 'all disp loads/costs (P) => total = 200 : ';
0769 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'DISPATCHABLE');
0770 mpc1 = scale_load(dmd, mpc, [], opt);
0771 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0772 t_is(sum(bus(:, PD)), total.fixed.p, 8, [t 'total fixed P']);
0773 t_is(sum(bus(:, QD)), total.fixed.q, 8, [t 'total fixed Q']);
0774 t_is(-sum(gen(ld, PMIN)), dmd-total.fixed.p, 8, [t 'total disp P']);
0775 t_is(-sum(gen(ld, QMIN)), total.disp.qmin, 8, [t 'total disp Qmin']);
0776 t_is(-sum(gen(ld, QMAX)), total.disp.qmax, 8, [t 'total disp Qmax']);
0777 t_is(gencost(gg, COST:end),   orig_gc(gg, COST:end), 8, [t 'gencost gens']);
0778 t_is(gencost(ld, COST:end), (dmd-total.fixed.p)/total.disp.p*orig_gc(ld, COST:end), 8, [t 'gencost loads']);
0779 
0780 %%-----  3 zones, area scale factors  -----
0781 t = 'area fixed loads (PQ) * [3 2 1] : ';
0782 dmd = [3 2 1];
0783 opt = struct('which', 'FIXED', 'cost', 1);
0784 mpc1 = scale_load(dmd, mpc, [], opt);
0785 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0786 for k = 1:length(dmd)
0787     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0788     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0789     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0790     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0791     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0792     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0793 end
0794 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0795 
0796 t = 'area fixed loads (P) * [3 2 1] : ';
0797 dmd = [3 2 1];
0798 opt = struct('pq', 'P', 'which', 'FIXED', 'cost', 1);
0799 mpc1 = scale_load(dmd, mpc, [], opt);
0800 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0801 for k = 1:length(dmd)
0802     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0803     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0804     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0805     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0806     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0807     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0808 end
0809 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0810 
0811 t = 'all area loads (PQ) * [3 2 1] : ';
0812 opt = struct('cost', 0);
0813 mpc1 = scale_load(dmd, mpc, [], opt);
0814 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0815 for k = 1:length(dmd)
0816     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0817     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0818     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0819     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0820     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0821     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0822 end
0823 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0824 
0825 t = 'all area loads/costs (PQ) * [3 2 1] : ';
0826 mpc1 = scale_load(dmd, mpc);
0827 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0828 for k = 1:length(dmd)
0829     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0830     t_is(sum(bus(a{k}, QD)), dmd(k)*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0831     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0832     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0833     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0834     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0835 end
0836 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0837 
0838 t = 'all area loads (P) * [3 2 1] : ';
0839 opt = struct('pq', 'P', 'cost', 0);
0840 mpc1 = scale_load(dmd, mpc, [], opt);
0841 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0842 for k = 1:length(dmd)
0843     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0844     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0845     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0846     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0847     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0848     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0849 end
0850 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0851 
0852 t = 'all area loads/costs (P) * [3 2 1] : ';
0853 opt = struct('pq', 'P');
0854 mpc1 = scale_load(dmd, mpc, [], opt);
0855 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0856 for k = 1:length(dmd)
0857     t_is(sum(bus(a{k}, PD)), dmd(k)*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0858     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0859     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0860     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0861     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0862     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0863 end
0864 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0865 
0866 t = 'area disp loads (PQ) * [3 2 1] : ';
0867 opt = struct('which', 'DISPATCHABLE', 'cost', 0);
0868 mpc1 = scale_load(dmd, mpc, [], opt);
0869 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0870 for k = 1:length(dmd)
0871     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0872     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0873     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0874     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0875     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0876     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0877 end
0878 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0879 
0880 t = 'area disp loads/costs (PQ) * [3 2 1] : ';
0881 opt = struct('which', 'DISPATCHABLE');
0882 mpc1 = scale_load(dmd, mpc, [], opt);
0883 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0884 for k = 1:length(dmd)
0885     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0886     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0887     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0888     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0889     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0890     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0891 end
0892 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0893 
0894 t = 'area disp loads (P) * [3 2 1] : ';
0895 opt = struct('pq', 'P', 'which', 'DISPATCHABLE', 'cost', 0);
0896 mpc1 = scale_load(dmd, mpc, [], opt);
0897 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0898 for k = 1:length(dmd)
0899     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0900     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0901     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0902     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0903     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0904     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0905 end
0906 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0907 
0908 t = 'area disp loads/costs (P) * [3 2 1] : ';
0909 opt = struct('pq', 'P', 'which', 'DISPATCHABLE');
0910 mpc1 = scale_load(dmd, mpc, [], opt);
0911 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0912 for k = 1:length(dmd)
0913     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0914     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0915     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0916     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0917     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0918     t_is(gencost(lda{k}, COST:end), dmd(k)*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0919 end
0920 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0921 
0922 %%-----  3 zones, area scale quantities  -----
0923 t = 'area fixed loads (PQ) => total = [100 80 60] : ';
0924 dmd = [100 80 60];
0925 opt = struct('scale', 'QUANTITY', 'which', 'FIXED');
0926 mpc1 = scale_load(dmd, mpc, [], opt);
0927 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0928 for k = 1:length(dmd)
0929     t_is(sum(bus(a{k}, PD)), dmd(k)-area(k).disp.p, 8, sprintf('%s area %d fixed P', t, k));
0930     t_is(sum(bus(a{k}, QD)), (dmd(k)-area(k).disp.p)/area(k).fixed.p*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0931     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0932     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0933     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0934     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0935 end
0936 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0937 
0938 t = 'area fixed loads (P) => total = [100 80 60] : ';
0939 dmd = [100 80 60];
0940 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'FIXED', 'cost', 1);
0941 mpc1 = scale_load(dmd, mpc, [], opt);
0942 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0943 for k = 1:length(dmd)
0944     t_is(sum(bus(a{k}, PD)), dmd(k)-area(k).disp.p, 8, sprintf('%s area %d fixed P', t, k));
0945     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0946     t_is(-sum(gen(lda{k}, PMIN)), area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0947     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0948     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0949     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0950 end
0951 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0952 
0953 t = 'all area loads (PQ) => total = [100 80 60] : ';
0954 opt = struct('scale', 'QUANTITY', 'cost', 0);
0955 mpc1 = scale_load(dmd, mpc, [], opt);
0956 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0957 for k = 1:length(dmd)
0958     t_is(sum(bus(a{k}, PD)), dmd(k)/area(k).both.p*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0959     t_is(sum(bus(a{k}, QD)), dmd(k)/area(k).both.p*area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0960     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)/area(k).both.p*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0961     t_is(-sum(gen(lda{k}, QMIN)), dmd(k)/area(k).both.p*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0962     t_is(-sum(gen(lda{k}, QMAX)), dmd(k)/area(k).both.p*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0963     t_is(gencost(lda{k}, COST:end), orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0964 end
0965 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0966 
0967 t = 'all area loads/costs (P) => total = [100 80 60] : ';
0968 opt = struct('scale', 'QUANTITY', 'pq', 'P');
0969 mpc1 = scale_load(dmd, mpc, [], opt);
0970 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
0971 for k = 1:length(dmd)
0972     t_is(sum(bus(a{k}, PD)), dmd(k)/area(k).both.p*area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
0973     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
0974     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)/area(k).both.p*area(k).disp.p, 8, sprintf('%s area %d disp P', t, k));
0975     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
0976     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
0977     t_is(gencost(lda{k}, COST:end), dmd(k)/area(k).both.p*orig_gc(lda{k}, COST:end), 8, sprintf('%s area %d gencost loads', t, k));
0978 end
0979 t_is(gencost(gg, COST:end), orig_gc(gg, COST:end), 8, sprintf('%s gencost gens', t));
0980 
0981 t = 'area disp loads (PQ) => total = [100 80 60] : throws expected exception';
0982 dmd = [100 80 60];
0983 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0984 err = 0;
0985 try
0986     mpc1 = scale_load(dmd, mpc, [], opt);
0987 catch
0988     [msg, id] = lasterr;
0989     expected = 'scale_load: impossible to make zone 2 load equal 80 by scaling non-existent dispatchable load';
0990     if ~isempty(strfind(msg, expected))
0991         err = 1;
0992     end
0993 end
0994 t_ok(err, t);
0995 
0996 t = 'area disp loads (PQ) => total = [100 74.3941 60] : ';
0997 dmd = [100 area(2).fixed.p 60];
0998 opt = struct('scale', 'QUANTITY', 'which', 'DISPATCHABLE');
0999 mpc1 = scale_load(dmd, mpc, [], opt);
1000 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
1001 for k = 1:length(dmd)
1002     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1003     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1004     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)-area(k).fixed.p, 8, sprintf('%s area %d disp P', t, k));
1005     if k == 2
1006         t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1007         t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1008     else
1009         t_is(-sum(gen(lda{k}, QMIN)), (dmd(k)-area(k).fixed.p)/area(k).disp.p*area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1010         t_is(-sum(gen(lda{k}, QMAX)), (dmd(k)-area(k).fixed.p)/area(k).disp.p*area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1011     end
1012 end
1013 
1014 t = 'area disp loads (P) => total = [100 74.3941 60] : ';
1015 opt = struct('scale', 'QUANTITY', 'pq', 'P', 'which', 'DISPATCHABLE');
1016 mpc1 = scale_load(dmd, mpc, [], opt);
1017 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
1018 for k = 1:length(dmd)
1019     t_is(sum(bus(a{k}, PD)), area(k).fixed.p, 8, sprintf('%s area %d fixed P', t, k));
1020     t_is(sum(bus(a{k}, QD)), area(k).fixed.q, 8, sprintf('%s area %d fixed Q', t, k));
1021     t_is(-sum(gen(lda{k}, PMIN)), dmd(k)-area(k).fixed.p, 8, sprintf('%s area %d disp P', t, k));
1022     t_is(-sum(gen(lda{k}, QMIN)), area(k).disp.qmin, 8, sprintf('%s area %d disp Qmin', t, k));
1023     t_is(-sum(gen(lda{k}, QMAX)), area(k).disp.qmax, 8, sprintf('%s area %d disp Qmax', t, k));
1024 end
1025 
1026 %%-----  explict single load zone  -----
1027 t = 'explicit single load zone';
1028 load_zone = zeros(1, size(mpc.bus, 1));
1029 load_zone([3 4]) = 1;
1030 dmd = 2;
1031 mpc1 = scale_load(dmd, mpc, load_zone);
1032 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
1033 Pd = mpc.bus(:, PD);
1034 Pd([3 4]) = dmd * Pd([3 4]);
1035 t_is( bus(:, PD), Pd, 8, t);
1036 
1037 %%-----  explict multiple load zone  -----
1038 t = 'explicit multiple load zone';
1039 load_zone = zeros(1, size(mpc.bus, 1));
1040 load_zone([3 4]) = 1;
1041 load_zone([7 8]) = 2;
1042 dmd = [2 0.5];
1043 mpc1 = scale_load(dmd, mpc, load_zone);
1044 [bus, gen, gencost] = deal(mpc1.bus, mpc1.gen, mpc1.gencost);
1045 Pd = mpc.bus(:, PD);
1046 Pd([3 4]) = dmd(1) * Pd([3 4]);
1047 Pd([7 8]) = dmd(2) * Pd([7 8]);
1048 t_is( bus(:, PD), Pd, 8, t);
1049 
1050 t_end;

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005