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

Generated on Fri 16-Dec-2016 12:45:37 by m2html © 2005