Package pypower :: Package t :: Module t_scale_load
[hide private]
[frames] | no frames]

Source Code for Module pypower.t.t_scale_load

  1  # Copyright (C) 2008-2011 Power System Engineering Research Center (PSERC) 
  2  # Copyright (C) 2011 Richard Lincoln 
  3  # 
  4  # PYPOWER is free software: you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published 
  6  # by the Free Software Foundation, either version 3 of the License, 
  7  # or (at your option) any later version. 
  8  # 
  9  # PYPOWER is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  """Tests for code in C{scale_load}. 
 18  """ 
 19   
 20  from os.path import dirname, join 
 21   
 22  from numpy import array, zeros, in1d, vstack, flatnonzero as find 
 23   
 24  from pypower.loadcase import loadcase 
 25  from pypower.isload import isload 
 26  from pypower.scale_load import scale_load, ScalingError 
 27   
 28  from pypower.idx_bus import PD, QD, BUS_AREA 
 29  from pypower.idx_gen import GEN_BUS, QG, PMIN, QMIN, QMAX 
 30   
 31  from pypower.t.t_begin import t_begin 
 32  from pypower.t.t_is import t_is 
 33  from pypower.t.t_ok import t_ok 
 34  from pypower.t.t_end import t_end 
 35   
 36   
37 -def t_scale_load(quiet=False):
38 """Tests for code in C{scale_load}. 39 40 @author: Ray Zimmerman (PSERC Cornell) 41 @author: Richard Lincoln 42 """ 43 n_tests = 275 44 45 t_begin(n_tests, quiet) 46 47 ppc = loadcase(join(dirname(__file__), 't_auction_case')) 48 ppc['gen'][7, GEN_BUS] = 2 ## multiple d. loads per area, same bus as gen 49 ppc['gen'][7, [QG, QMIN, QMAX]] = array([3, 0, 3]) 50 ## put it load before gen in matrix 51 52 ppc['gen'] = vstack([ppc['gen'][7, :], ppc['gen'][:7, :], ppc['gen'][8, :]]) 53 ld = find(isload(ppc['gen'])) 54 a = [None] * 3 55 lda = [None] * 3 56 for k in range(3): 57 a[k] = find(ppc['bus'][:, BUS_AREA] == k + 1) ## buses in area k 58 tmp = find( in1d(ppc['gen'][ld, GEN_BUS] - 1, a[k]) ) 59 lda[k] = ld[tmp] ## disp loads in area k 60 61 area = [None] * 3 62 for k in range(3): 63 area[k] = {'fixed': {}, 'disp': {}, 'both': {}} 64 area[k]['fixed']['p'] = sum(ppc['bus'][a[k], PD]) 65 area[k]['fixed']['q'] = sum(ppc['bus'][a[k], QD]) 66 area[k]['disp']['p'] = -sum(ppc['gen'][lda[k], PMIN]) 67 area[k]['disp']['qmin'] = -sum(ppc['gen'][lda[k], QMIN]) 68 area[k]['disp']['qmax'] = -sum(ppc['gen'][lda[k], QMAX]) 69 area[k]['disp']['q'] = area[k]['disp']['qmin'] + area[k]['disp']['qmax'] 70 area[k]['both']['p'] = area[k]['fixed']['p'] + area[k]['disp']['p'] 71 area[k]['both']['q'] = area[k]['fixed']['q'] + area[k]['disp']['q'] 72 73 total = {'fixed': {}, 'disp': {}, 'both': {}} 74 total['fixed']['p'] = sum(ppc['bus'][:, PD]) 75 total['fixed']['q'] = sum(ppc['bus'][:, QD]) 76 total['disp']['p'] = -sum(ppc['gen'][ld, PMIN]) 77 total['disp']['qmin'] = -sum(ppc['gen'][ld, QMIN]) 78 total['disp']['qmax'] = -sum(ppc['gen'][ld, QMAX]) 79 total['disp']['q'] = total['disp']['qmin'] + total['disp']['qmax'] 80 total['both']['p'] = total['fixed']['p'] + total['disp']['p'] 81 total['both']['q'] = total['fixed']['q'] + total['disp']['q'] 82 83 ##----- single load zone, one scale factor ----- 84 load = array([2]) 85 t = 'all fixed loads (PQ) * 2 : ' 86 bus, _ = scale_load(load, ppc['bus']) 87 t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) 88 t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) 89 opt = {'which': 'FIXED'} 90 91 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 92 93 t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) 94 t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) 95 t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) 96 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 97 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 98 99 t = 'all fixed loads (P) * 2 : ' 100 opt = {'pq': 'P'} 101 bus, _ = scale_load(load, ppc['bus'], None, None, opt) 102 t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) 103 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 104 opt = {'pq': 'P', 'which': 'FIXED'} 105 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 106 t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) 107 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 108 t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) 109 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 110 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 111 112 t = 'all loads (PQ) * 2 : ' 113 bus, gen = scale_load(load, ppc['bus'], ppc['gen']) 114 t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) 115 t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) 116 t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) 117 t_is(-sum(gen[ld, QMIN]), load * total['disp']['qmin'], 8, [t, 'total disp Qmin']) 118 t_is(-sum(gen[ld, QMAX]), load * total['disp']['qmax'], 8, [t, 'total disp Qmax']) 119 120 t = 'all loads (P) * 2 : ' 121 opt = {'pq': 'P'} 122 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 123 t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) 124 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 125 t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) 126 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 127 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 128 129 t = 'all disp loads (PQ) * 2 : ' 130 opt = {'which': 'DISPATCHABLE'} 131 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 132 t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) 133 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 134 t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) 135 t_is(-sum(gen[ld, QMIN]), load * total['disp']['qmin'], 8, [t, 'total disp Qmin']) 136 t_is(-sum(gen[ld, QMAX]), load * total['disp']['qmax'], 8, [t, 'total disp Qmax']) 137 138 t = 'all disp loads (P) * 2 : ' 139 opt = {'pq': 'P', 'which': 'DISPATCHABLE'} 140 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 141 t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) 142 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 143 t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) 144 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 145 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 146 147 ##----- single load zone, one scale quantity ----- 148 load = array([200.0]) 149 t = 'all fixed loads (PQ) => total = 200 : ' 150 opt = {'scale': 'QUANTITY'} 151 bus, _ = scale_load(load, ppc['bus'], None, None, opt) 152 t_is(sum(bus[:, PD]), load, 8, [t, 'total fixed P']) 153 t_is(sum(bus[:, QD]), load / total['fixed']['p'] * total['fixed']['q'], 8, [t, 'total fixed Q']) 154 opt = {'scale': 'QUANTITY', 'which': 'FIXED'} 155 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 156 t_is(sum(bus[:, PD]), load - total['disp']['p'], 8, [t, 'total fixed P']) 157 t_is(sum(bus[:, QD]), (load - total['disp']['p'])/total['fixed']['p']*total['fixed']['q'], 8, [t, 'total fixed Q']) 158 t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) 159 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 160 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 161 162 t = 'all fixed loads (P) => total = 200 : ' 163 opt = {'scale': 'QUANTITY', 'pq': 'P'} 164 bus, _ = scale_load(load, ppc['bus'], None, None, opt) 165 t_is(sum(bus[:, PD]), load, 8, [t, 'total fixed P']) 166 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 167 opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'FIXED'} 168 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 169 t_is(sum(bus[:, PD]), load - total['disp']['p'], 8, [t, 'total fixed P']) 170 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 171 t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) 172 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 173 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 174 175 t = 'all loads (PQ) => total = 200 : ' 176 opt = {'scale': 'QUANTITY'} 177 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 178 t_is(sum(bus[:, PD]), load / total['both']['p']*total['fixed']['p'], 8, [t, 'total fixed P']) 179 t_is(sum(bus[:, QD]), load / total['both']['p']*total['fixed']['q'], 8, [t, 'total fixed Q']) 180 t_is(-sum(gen[ld, PMIN]), load / total['both']['p']*total['disp']['p'], 8, [t, 'total disp P']) 181 t_is(-sum(gen[ld, QMIN]), load / total['both']['p']*total['disp']['qmin'], 8, [t, 'total disp Qmin']) 182 t_is(-sum(gen[ld, QMAX]), load / total['both']['p']*total['disp']['qmax'], 8, [t, 'total disp Qmax']) 183 184 t = 'all loads (P) => total = 200 : ' 185 opt = {'scale': 'QUANTITY', 'pq': 'P'} 186 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 187 t_is(sum(bus[:, PD]), load / total['both']['p']*total['fixed']['p'], 8, [t, 'total fixed P']) 188 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 189 t_is(-sum(gen[ld, PMIN]), load / total['both']['p']*total['disp']['p'], 8, [t, 'total disp P']) 190 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 191 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 192 193 t = 'all disp loads (PQ) => total = 200 : ' 194 opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} 195 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 196 t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) 197 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 198 t_is(-sum(gen[ld, PMIN]), load - total['fixed']['p'], 8, [t, 'total disp P']) 199 t_is(-sum(gen[ld, QMIN]), (load - total['fixed']['p'])/total['disp']['p']*total['disp']['qmin'], 8, [t, 'total disp Qmin']) 200 t_is(-sum(gen[ld, QMAX]), (load - total['fixed']['p'])/total['disp']['p']*total['disp']['qmax'], 8, [t, 'total disp Qmax']) 201 202 t = 'all disp loads (P) => total = 200 : ' 203 opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'DISPATCHABLE'} 204 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 205 t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) 206 t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) 207 t_is(-sum(gen[ld, PMIN]), load - total['fixed']['p'], 8, [t, 'total disp P']) 208 t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) 209 t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) 210 211 ##----- 3 zones, area scale factors ----- 212 t = 'area fixed loads (PQ) * [3 2 1] : ' 213 load = array([3, 2, 1]) 214 bus, _ = scale_load(load, ppc['bus']) 215 for k in range(len(load)): 216 t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 217 t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 218 219 opt = {'which': 'FIXED'} 220 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 221 for k in range(len(load)): 222 t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 223 t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 224 t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 225 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 226 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 227 228 t = 'area fixed loads (P) * [3 2 1] : ' 229 load = array([3, 2, 1]) 230 opt = {'pq': 'P'} 231 bus, _ = scale_load(load, ppc['bus'], None, None, opt) 232 for k in range(len(load)): 233 t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 234 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 235 236 opt = {'pq': 'P', 'which': 'FIXED'} 237 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 238 for k in range(len(load)): 239 t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 240 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 241 t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 242 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 243 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 244 245 t = 'all area loads (PQ) * [3 2 1] : ' 246 bus, gen = scale_load(load, ppc['bus'], ppc['gen']) 247 for k in range(len(load)): 248 t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 249 t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 250 t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 251 t_is(-sum(gen[lda[k], QMIN]), load[k] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 252 t_is(-sum(gen[lda[k], QMAX]), load[k] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 253 254 255 t = 'all area loads (P) * [3 2 1] : ' 256 opt = {'pq': 'P'} 257 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 258 for k in range(len(load)): 259 t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 260 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 261 t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 262 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 263 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 264 265 t = 'area disp loads (PQ) * [3 2 1] : ' 266 opt = {'which': 'DISPATCHABLE'} 267 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 268 for k in range(len(load)): 269 t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 270 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 271 t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 272 t_is(-sum(gen[lda[k], QMIN]), load[k] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 273 t_is(-sum(gen[lda[k], QMAX]), load[k] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 274 275 t = 'area disp loads (P) * [3 2 1] : ' 276 opt = {'pq': 'P', 'which': 'DISPATCHABLE'} 277 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 278 for k in range(len(load)): 279 t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 280 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 281 t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 282 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 283 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 284 285 ##----- 3 zones, area scale quantities ----- 286 t = 'area fixed loads (PQ) => total = [100 80 60] : ' 287 load = array([100, 80, 60], float) 288 opt = {'scale': 'QUANTITY'} 289 bus, _ = scale_load(load, ppc['bus'], None, None, opt) 290 for k in range(len(load)): 291 t_is(sum(bus[a[k], PD]), load[k], 8, '%s area %d fixed P' % (t, k)) 292 t_is(sum(bus[a[k], QD]), load[k] / area[k]['fixed']['p'] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 293 294 opt = {'scale': 'QUANTITY', 'which': 'FIXED'} 295 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 296 for k in range(len(load)): 297 t_is(sum(bus[a[k], PD]), load[k] - area[k]['disp']['p'], 8, '%s area %d fixed P' % (t, k)) 298 t_is(sum(bus[a[k], QD]), (load[k] - area[k]['disp']['p']) / area[k]['fixed']['p'] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 299 t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 300 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 301 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 302 303 t = 'area fixed loads (P) => total = [100 80 60] : ' 304 load = array([100, 80, 60], float) 305 opt = {'scale': 'QUANTITY', 'pq': 'P'} 306 bus, _ = scale_load(load, ppc['bus'], None, None, opt) 307 for k in range(len(load)): 308 t_is(sum(bus[a[k], PD]), load[k], 8, '%s area %d fixed P' % (t, k)) 309 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 310 311 opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'FIXED'} 312 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 313 for k in range(len(load)): 314 t_is(sum(bus[a[k], PD]), load[k]-area[k]['disp']['p'], 8, '%s area %d fixed P' % (t, k)) 315 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 316 t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 317 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 318 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 319 320 t = 'all area loads (PQ) => total = [100 80 60] : ' 321 opt = {'scale': 'QUANTITY'} 322 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 323 for k in range(len(load)): 324 t_is(sum(bus[a[k], PD]), load[k] / area[k]['both']['p'] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 325 t_is(sum(bus[a[k], QD]), load[k] / area[k]['both']['p'] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 326 t_is(-sum(gen[lda[k], PMIN]), load[k] / area[k]['both']['p'] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 327 t_is(-sum(gen[lda[k], QMIN]), load[k] / area[k]['both']['p'] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 328 t_is(-sum(gen[lda[k], QMAX]), load[k] / area[k]['both']['p'] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 329 330 t = 'all area loads (P) => total = [100 80 60] : ' 331 opt = {'scale': 'QUANTITY', 'pq': 'P'} 332 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 333 for k in range(len(load)): 334 t_is(sum(bus[a[k], PD]), load[k] / area[k]['both']['p'] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 335 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 336 t_is(-sum(gen[lda[k], PMIN]), load[k] / area[k]['both']['p'] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) 337 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 338 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 339 340 t = 'area disp loads (PQ) => total = [100 80 60] : throws expected exception' 341 load = array([100, 80, 60], float) 342 opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} 343 err = 0 344 try: 345 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 346 except ScalingError, e: 347 expected = 'scale_load: impossible to make zone 2 load equal 80 by scaling non-existent dispatchable load' 348 err = expected not in str(e) 349 t_ok(err, t) 350 351 t = 'area disp loads (PQ) => total = [100 74.3941 60] : ' 352 load = array([100, area[1]['fixed']['p'], 60], float) 353 opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} 354 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 355 for k in range(len(load)): 356 t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 357 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 358 t_is(-sum(gen[lda[k], PMIN]), load[k]-area[k]['fixed']['p'], 8, '%s area %d disp P' % (t, k)) 359 if k == 1: 360 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 361 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 362 else: 363 t_is(-sum(gen[lda[k], QMIN]), (load[k] - area[k]['fixed']['p']) / area[k]['disp']['p'] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 364 t_is(-sum(gen[lda[k], QMAX]), (load[k] - area[k]['fixed']['p']) / area[k]['disp']['p'] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 365 366 t = 'area disp loads (P) => total = [100 74.3941 60] : ' 367 opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'DISPATCHABLE'} 368 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) 369 for k in range(len(load)): 370 t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) 371 t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 372 t_is(-sum(gen[lda[k], PMIN]), load[k]-area[k]['fixed']['p'], 8, '%s area %d disp P' % (t, k)) 373 t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) 374 t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) 375 376 ##----- explict single load zone ----- 377 t = 'explicit single load zone' 378 load_zone = zeros(ppc['bus'].shape[0]) 379 load_zone[[2, 3]] = 1 380 load = array([2.0]) 381 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) 382 Pd = ppc['bus'][:, PD] 383 Pd[[2, 3]] = load * Pd[[2, 3]] 384 t_is( bus[:, PD], Pd, 8, t) 385 386 ##----- explict multiple load zone ----- 387 t = 'explicit multiple load zone' 388 load_zone = zeros(ppc['bus'].shape[0]) 389 load_zone[[2, 3]] = 1 390 load_zone[[6, 7]] = 2 391 load = array([2, 0.5]) 392 bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) 393 Pd = ppc['bus'][:, PD] 394 Pd[[2, 3]] = load[0] * Pd[[2, 3]] 395 Pd[[6, 7]] = load[1] * Pd[[6, 7]] 396 t_is( bus[:, PD], Pd, 8, t) 397 398 t_end()
399 400 401 if __name__ == '__main__': 402 t_scale_load(quiet=False) 403