def t_makeLODF(quiet=False): """Tests for C{makeLODF}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ntests = 31 t_begin(ntests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_auction_case') verbose = 0 #not quiet ## load case ppc = loadcase(casefile) ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0) r = rundcopf(ppc, ppopt) baseMVA, bus, gen, branch = r['baseMVA'], r['bus'], r['gen'], r['branch'] _, bus, gen, branch = ext2int1(bus, gen, branch) ## compute injections and flows F0 = branch[:, PF] ## create some PTDF matrices H = makePTDF(baseMVA, bus, branch, 0) ## create some PTDF matrices try: LODF = makeLODF(branch, H) except ZeroDivisionError: pass ## take out non-essential lines one-by-one and see what happens ppc['bus'] = bus ppc['gen'] = gen branch0 = branch outages = r_[arange(12), arange(13, 15), arange(16, 18), [19], arange(26, 33), arange(34, 41)] for k in outages: ppc['branch'] = branch0.copy() ppc['branch'][k, BR_STATUS] = 0 r, _ = rundcpf(ppc, ppopt) baseMVA, bus, gen, branch = \ r['baseMVA'], r['bus'], r['gen'], r['branch'] F = branch[:, PF] t_is(LODF[:, k], (F - F0) / F0[k], 6, 'LODF[:, %d]' % k) t_end()
def t_totcost(quiet=False): """Tests for code in C{totcost}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 22 t_begin(n_tests, quiet) ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost = array([ [2, 0, 0, 3, 0.01, 0.1, 1, 0, 0, 0, 0, 0], [2, 0, 0, 5, 0.0006, 0.005, 0.04, 0.3, 2, 0, 0, 0], [1, 0, 0, 4, 0, 0, 10, 200, 20, 600, 30, 1200], [1, 0, 0, 4, -30, -2400, -20, -1800, -10, -1000, 0, 0] ]) t = 'totcost - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0])), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0])), [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0])), [1.24, 2, 0, 0], 8, t) t = 'totcost - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0])), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0])), [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0])), [1, 2.8096, 0, 0], 8, t) t = 'totcost - pwl (gen)' t_is(totcost(gencost, array([0, 0, -10, 0 ])), [1, 2, -200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 5, 0 ])), [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0])), [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0])), [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0])), [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0])), [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0])), [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0])), [1, 2, 1500, 0], 8, t) t = 'totcost - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, 10 ])), [1, 2, 0, 1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -5 ])), [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10])), [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15])), [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20])), [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25])), [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30])), [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35])), [1, 2, 0, -2700], 8, t) t_end()
def t_makeLODF(quiet=False): """Tests for C{makeLODF}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ntests = 31 t_begin(ntests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_auction_case') verbose = 0#not quiet ## load case ppc = loadcase(casefile) ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0) r = rundcopf(ppc, ppopt) baseMVA, bus, gen, branch = r['baseMVA'], r['bus'], r['gen'], r['branch'] _, bus, gen, branch = ext2int1(bus, gen, branch) ## compute injections and flows F0 = branch[:, PF] ## create some PTDF matrices H = makePTDF(baseMVA, bus, branch, 0) ## create some PTDF matrices try: LODF = makeLODF(branch, H) except ZeroDivisionError: pass ## take out non-essential lines one-by-one and see what happens ppc['bus'] = bus ppc['gen'] = gen branch0 = branch outages = r_[arange(12), arange(13, 15), arange(16, 18), [19], arange(26, 33), arange(34, 41)] for k in outages: ppc['branch'] = branch0.copy() ppc['branch'][k, BR_STATUS] = 0 r, _ = rundcpf(ppc, ppopt) baseMVA, bus, gen, branch = \ r['baseMVA'], r['bus'], r['gen'], r['branch'] F = branch[:, PF] t_is(LODF[:, k], (F - F0) / F0[k], 6, 'LODF[:, %d]' % k) t_end()
def t_totcost(quiet=False): """Tests for code in C{totcost}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 22 t_begin(n_tests, quiet) ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost = array([[2, 0, 0, 3, 0.01, 0.1, 1, 0, 0, 0, 0, 0], [2, 0, 0, 5, 0.0006, 0.005, 0.04, 0.3, 2, 0, 0, 0], [1, 0, 0, 4, 0, 0, 10, 200, 20, 600, 30, 1200], [1, 0, 0, 4, -30, -2400, -20, -1800, -10, -1000, 0, 0]]) t = 'totcost - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0])), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0])), [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0])), [1.24, 2, 0, 0], 8, t) t = 'totcost - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0])), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0])), [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0])), [1, 2.8096, 0, 0], 8, t) t = 'totcost - pwl (gen)' t_is(totcost(gencost, array([0, 0, -10, 0])), [1, 2, -200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 5, 0])), [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0])), [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0])), [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0])), [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0])), [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0])), [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0])), [1, 2, 1500, 0], 8, t) t = 'totcost - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, 10])), [1, 2, 0, 1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -5])), [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10])), [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15])), [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20])), [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25])), [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30])), [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35])), [1, 2, 0, -2700], 8, t) t_end()
def t_hasPQcap(quiet=False): """Tests for C{hasPQcap}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(4, quiet) ## generator data # bus Pg Qg Qmax Qmin Vg mBase status Pmax Pmin Pc1 Pc2 Qc1min Qc1max Qc2min Qc2max ramp_agc ramp_10 ramp_30 ramp_q apf gen = array([ [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, 0, 12, 0, 2, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -15, 12, -15, 2, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 0, -2, 0, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 15, -2, 15, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 12, -2, 2, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, 0, 12, 0, 8, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -15, 12, -15, 8, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 0, -8, 0, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 15, -8, 15, 0, 0, 0, 0, 0], [1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 12, -8, 8, 0, 0, 0, 0, 0] ]) t = 'hasPQcap(gen)' t_is(hasPQcap(gen), [0,1,1,1,1,1,1,0,1,0,0], 12, t) t = 'hasPQcap(gen, \'B\')' t_is(hasPQcap(gen, 'B'), [0,1,1,1,1,1,1,0,1,0,0], 12, t) t = 'hasPQcap(gen, \'U\')' t_is(hasPQcap(gen, 'U'), [0,1,1,1,0,1,0,0,1,0,0], 12, t) t = 'hasPQcap(gen, \'L\')' t_is(hasPQcap(gen, 'L'), [0,1,0,1,1,1,1,0,0,0,0], 12, t) t_end()
def t_qps_pypower(quiet=False): """Tests of C{qps_pypower} QP solvers. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ algs = [200, 250, 400, 500, 600, 700] names = ['PIPS', 'sc-PIPS', 'IPOPT', 'CPLEX', 'MOSEK', 'Gurobi'] check = [None, None, 'ipopt', 'cplex', 'mosek', 'gurobipy'] n = 36 t_begin(n * len(algs), quiet) for k in range(len(algs)): if check[k] is not None and not have_fcn(check[k]): t_skip(n, '%s not installed' % names[k]) else: opt = {'verbose': 0, 'alg': algs[k]} if names[k] == 'PIPS' or names[k] == 'sc-PIPS': opt['pips_opt'] = {} opt['pips_opt']['comptol'] = 1e-8 if names[k] == 'CPLEX': # alg = 0 ## default uses barrier method with NaN bug in lower lim multipliers alg = 2 ## use dual simplex ppopt = ppoption(CPLEX_LPMETHOD=alg, CPLEX_QPMETHOD=min([4, alg])) opt['cplex_opt'] = cplex_options([], ppopt) if names[k] == 'MOSEK': # alg = 5 ## use dual simplex ppopt = ppoption() # ppopt = ppoption(ppopt, MOSEK_LP_ALG = alg) ppopt = ppoption(ppopt, MOSEK_GAP_TOL=1e-9) opt['mosek_opt'] = mosek_options([], ppopt) t = '%s - 3-d LP : ' % names[k] ## example from 'doc linprog' c = array([-5, -4, -6], float) A = sparse([[1, -1, 1], [3, 2, 4], [3, 2, 0]], dtype=float) l = None u = array([20, 42, 30], float) xmin = array([0, 0, 0], float) x0 = None x, f, s, _, lam = qps_pypower(None, c, A, l, u, xmin, None, None, opt) t_is(s, 1, 12, [t, 'success']) t_is(x, [0, 15, 3], 6, [t, 'x']) t_is(f, -78, 6, [t, 'f']) t_is(lam['mu_l'], [0, 0, 0], 13, [t, 'lam.mu_l']) t_is(lam['mu_u'], [0, 1.5, 0.5], 9, [t, 'lam.mu_u']) t_is(lam['lower'], [1, 0, 0], 9, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - unconstrained 3-d quadratic : ' % names[k] ## from http://www.akiti.ca/QuadProgEx0Constr.html H = sparse([[5, -2, -1], [-2, 4, 3], [-1, 3, 5]], dtype=float) c = array([2, -35, -47], float) x0 = array([0, 0, 0], float) x, f, s, _, lam = qps_pypower(H, c, opt=opt) t_is(s, 1, 12, [t, 'success']) t_is(x, [3, 5, 7], 8, [t, 'x']) t_is(f, -249, 13, [t, 'f']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(shape(x)), 13, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - constrained 2-d QP : ' % names[k] ## example from 'doc quadprog' H = sparse([[1, -1], [-1, 2]], dtype=float) c = array([-2, -6], float) A = sparse([[1, 1], [-1, 2], [2, 1]], dtype=float) l = None u = array([2, 2, 3], float) xmin = array([0, 0]) x0 = None x, f, s, _, lam = qps_pypower(H, c, A, l, u, xmin, None, x0, opt) t_is(s, 1, 12, [t, 'success']) t_is(x, array([2., 4.]) / 3, 7, [t, 'x']) t_is(f, -74. / 9, 6, [t, 'f']) t_is(lam['mu_l'], [0., 0., 0.], 13, [t, 'lam.mu_l']) t_is(lam['mu_u'], array([28., 4., 0.]) / 9, 7, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(shape(x)), 8, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - constrained 4-d QP : ' % names[k] ## from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm H = sparse([[1003.1, 4.3, 6.3, 5.9], [4.3, 2.2, 2.1, 3.9], [6.3, 2.1, 3.5, 4.8], [5.9, 3.9, 4.8, 10.0]]) c = zeros(4) A = sparse([[1, 1, 1, 1], [0.17, 0.11, 0.10, 0.18]]) l = array([1, 0.10]) u = array([1, Inf]) xmin = zeros(4) x0 = array([1, 0, 0, 1], float) x, f, s, _, lam = qps_pypower(H, c, A, l, u, xmin, None, x0, opt) t_is(s, 1, 12, [t, 'success']) t_is(x, array([0, 2.8, 0.2, 0]) / 3, 5, [t, 'x']) t_is(f, 3.29 / 3, 6, [t, 'f']) t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) t_is(lam['mu_u'], [0, 0], 13, [t, 'lam.mu_u']) t_is(lam['lower'], [2.24, 0, 0, 1.7667], 4, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - (dict) constrained 4-d QP : ' % names[k] p = { 'H': H, 'A': A, 'l': l, 'u': u, 'xmin': xmin, 'x0': x0, 'opt': opt } x, f, s, _, lam = qps_pypower(p) t_is(s, 1, 12, [t, 'success']) t_is(x, array([0, 2.8, 0.2, 0]) / 3, 5, [t, 'x']) t_is(f, 3.29 / 3, 6, [t, 'f']) t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) t_is(lam['mu_u'], [0, 0], 13, [t, 'lam.mu_u']) t_is(lam['lower'], [2.24, 0, 0, 1.7667], 4, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - infeasible LP : ' % names[k] p = { 'A': sparse([1, 1]), 'c': array([1, 1]), 'u': array([-1]), 'xmin': array([0, 0]), 'opt': opt } x, f, s, _, lam = qps_pypower(p) t_ok(s <= 0, [t, 'no success']) t_end()
def t_pf(quiet=False): """Tests for power flow solvers. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(33, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_pf') verbose = not quiet ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0) ## get solved AC power flow case from MAT-file ## defines bus_soln, gen_soln, branch_soln soln9_pf = loadmat(join(tdir, 'soln9_pf.mat'), struct_as_record=False) bus_soln = soln9_pf['bus_soln'] gen_soln = soln9_pf['gen_soln'] branch_soln = soln9_pf['branch_soln'] ## run Newton PF t = 'Newton PF : '; ppopt = ppoption(ppopt, PF_ALG=1) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## run fast-decoupled PF (XB version) t = 'Fast Decoupled (XB) PF : '; ppopt = ppoption(ppopt, PF_ALG=2) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## run fast-decoupled PF (BX version) t = 'Fast Decoupled (BX) PF : '; ppopt = ppoption(ppopt, PF_ALG=3) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## run Gauss-Seidel PF t = 'Gauss-Seidel PF : '; ppopt = ppoption(ppopt, PF_ALG=4) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 5, [t, 'bus']) t_is(gen, gen_soln, 5, [t, 'gen']) t_is(branch, branch_soln, 5, [t, 'branch']) ## get solved AC power flow case from MAT-file ## defines bus_soln, gen_soln, branch_soln soln9_dcpf = loadmat(join(tdir, 'soln9_dcpf.mat'), struct_as_record=False) bus_soln = soln9_dcpf['bus_soln'] gen_soln = soln9_dcpf['gen_soln'] branch_soln = soln9_dcpf['branch_soln'] ## run DC PF t = 'DC PF : ' results, success = rundcpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## check Qg distribution, when Qmin = Qmax t = 'check Qg : ' ppopt = ppoption(ppopt, PF_ALG=1, VERBOSE=0) ppc = loadcase(casefile) ppc['gen'][0, [QMIN, QMAX]] = [20, 20] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0, QG], 24.07, 2, [t, 'single gen, Qmin = Qmax']) ppc['gen'] = r_[array([ ppc['gen'][0, :] ]), ppc['gen']] ppc['gen'][0, [QMIN, QMAX]] = [10, 10] ppc['gen'][1, [QMIN, QMAX]] = [ 0, 50] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [10, 14.07], 2, [t, '2 gens, Qmin = Qmax for one']) ppc['gen'][0, [QMIN, QMAX]] = [10, 10] ppc['gen'][1, [QMIN, QMAX]] = [-50, -50] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [12.03, 12.03], 2, [t, '2 gens, Qmin = Qmax for both']) ppc['gen'][0, [QMIN, QMAX]] = [0, 50] ppc['gen'][1, [QMIN, QMAX]] = [0, 100] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [8.02, 16.05], 2, [t, '2 gens, proportional']) ppc['gen'][0, [QMIN, QMAX]] = [-50, 0] ppc['gen'][1, [QMIN, QMAX]] = [50, 150] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [-50 + 8.02, 50 + 16.05], 2, [t, '2 gens, proportional']) ## network with islands t = 'network w/islands : DC PF : ' ppc0 = loadcase(casefile) ppc0['gen'][0, PG] = 60 ppc0['gen'][0, [PMIN, PMAX, QMIN, QMAX, PG, QG]] = \ ppc0['gen'][0, [PMIN, PMAX, QMIN, QMAX, PG, QG]] / 2 ppc0['gen'] = r_[array([ ppc0['gen'][0, :] ]), ppc0['gen']] ppc1 = ppc0.copy() ppc = ppc0.copy() nb = ppc['bus'].shape[0] ppc1['bus'][:, BUS_I] = ppc1['bus'][:, BUS_I] + nb ppc1['branch'][:, F_BUS] = ppc1['branch'][:, F_BUS] + nb ppc1['branch'][:, T_BUS] = ppc1['branch'][:, T_BUS] + nb ppc1['gen'][:, GEN_BUS] = ppc1['gen'][:, GEN_BUS] + nb ppc['bus'] = r_[ppc['bus'], ppc1['bus']] ppc['branch'] = r_[ppc['branch'], ppc1['branch']] ppc['gen'] = r_[ppc['gen'], ppc1['gen']] #ppopt = ppoption(ppopt, OUT_BUS=1, OUT_GEN=1, OUT_ALL=-1, VERBOSE=2) ppopt = ppoption(ppopt, VERBOSE=verbose) r = rundcpf(ppc, ppopt) t_is(r['bus'][ :9, VA], bus_soln[:, VA], 8, [t, 'voltage angles 1']) t_is(r['bus'][10:18, VA], bus_soln[:, VA], 8, [t, 'voltage angles 2']) Pg = r_[gen_soln[0, PG] - 30, 30, gen_soln[1:3, PG]] t_is(r['gen'][ :4, PG], Pg, 8, [t, 'active power generation 1']) t_is(r['gen'][4:8, PG], Pg, 8, [t, 'active power generation 1']) t = 'network w/islands : AC PF : ' ## get solved AC power flow case from MAT-file soln9_pf = loadmat(join(tdir, 'soln9_pf.mat'), struct_as_record=False) bus_soln = soln9_pf['bus_soln'] gen_soln = soln9_pf['gen_soln'] branch_soln = soln9_pf['branch_soln'] r = runpf(ppc, ppopt) t_is(r['bus'][ :9, VA], bus_soln[:, VA], 8, [t, 'voltage angles 1']) t_is(r['bus'][9:18, VA], bus_soln[:, VA], 8, [t, 'voltage angles 2']) Pg = r_[gen_soln[0, PG] - 30, 30, gen_soln[1:3, PG]] t_is(r['gen'][ :4, PG], Pg, 8, [t, 'active power generation 1']) t_is(r['gen'][4:8, PG], Pg, 8, [t, 'active power generation 1']) t_end()
def t_pips(quiet=False): """Tests of pips NLP solver. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(60, quiet) t = 'unconstrained banana function : ' ## from MATLAB Optimization Toolbox's bandem.m f_fcn = f2 x0 = array([-1.9, 2]) # solution = pips(f_fcn, x0, opt={'verbose': 2}) solution = pips(f_fcn, x0) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1, 1], 13, [t, 'x']) t_is(f, 0, 13, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) t = 'unconstrained 3-d quadratic : ' ## from http://www.akiti.ca/QuadProgEx0Constr.html f_fcn = f3 x0 = array([0, 0, 0], float) # solution = pips(f_fcn, x0, opt={'verbose': 2}) solution = pips(f_fcn, x0) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [3, 5, 7], 13, [t, 'x']) t_is(f, -244, 13, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) t = 'constrained 4-d QP : ' ## from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm f_fcn = f4 x0 = array([1.0, 0.0, 0.0, 1.0]) A = array([ [1.0, 1.0, 1.0, 1.0 ], [0.17, 0.11, 0.10, 0.18] ]) l = array([1, 0.10]) u = array([1.0, Inf]) xmin = zeros(4) # solution = pips(f_fcn, x0, A, l, u, xmin, opt={'verbose': 2}) solution = pips(f_fcn, x0, A, l, u, xmin) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, array([0, 2.8, 0.2, 0]) / 3, 6, [t, 'x']) t_is(f, 3.29 / 3, 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) t_is(lam['mu_u'], array([0, 0]), 13, [t, 'lam.mu_u']) t_is(lam['lower'], array([2.24, 0, 0, 1.7667]), 4, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) # H = array([ # [1003.1, 4.3, 6.3, 5.9], # [ 4.3, 2.2, 2.1, 3.9], # [ 6.3, 2.1, 3.5, 4.8], # [ 5.9, 3.9, 4.8, 10.0] # ]) # c = zeros(4) # ## check with quadprog (for dev testing only) # x, f, s, out, lam = quadprog(H,c,-A(2,:), -0.10, A(1,:), 1, xmin) # t_is(s, 1, 13, [t, 'success']) # t_is(x, [0 2.8 0.2 0]/3, 6, [t, 'x']) # t_is(f, 3.29/3, 6, [t, 'f']) # t_is(lam['eqlin'], -6.58/3, 6, [t, 'lam.eqlin']) # t_is(lam.['ineqlin'], 0, 13, [t, 'lam.ineqlin']) # t_is(lam['lower'], [2.24001.7667], 4, [t, 'lam[\'lower\']']) # t_is(lam['upper'], [0000], 13, [t, 'lam[\'upper\']']) t = 'constrained 2-d nonlinear : ' ## from http://en.wikipedia.org/wiki/Nonlinear_programming#2-dimensional_example f_fcn = f5 gh_fcn = gh5 hess_fcn = hess5 x0 = array([1.1, 0.0]) xmin = zeros(2) # xmax = 3 * ones(2, 1) # solution = pips(f_fcn, x0, xmin=xmin, gh_fcn=gh_fcn, hess_fcn=hess_fcn, opt={'verbose': 2}) solution = pips(f_fcn, x0, xmin=xmin, gh_fcn=gh_fcn, hess_fcn=hess_fcn) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1, 1], 6, [t, 'x']) t_is(f, -2, 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['ineqnonlin'], array([0, 0.5]), 6, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) # ## check with fmincon (for dev testing only) # # fmoptions = optimset('Algorithm', 'interior-point') # # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], xmin, [], gh_fcn, fmoptions) # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], [], [], gh_fcn) # t_is(s, 1, 13, [t, 'success']) # t_is(x, [1 1], 4, [t, 'x']) # t_is(f, -2, 6, [t, 'f']) # t_is(lam.ineqnonlin, [00.5], 6, [t, 'lam.ineqnonlin']) t = 'constrained 3-d nonlinear : ' ## from http://en.wikipedia.org/wiki/Nonlinear_programming#3-dimensional_example f_fcn = f6 gh_fcn = gh6 hess_fcn = hess6 x0 = array([1.0, 1.0, 0.0]) # solution = pips(f_fcn, x0, gh_fcn=gh_fcn, hess_fcn=hess_fcn, opt={'verbose': 2, 'comptol': 1e-9}) solution = pips(f_fcn, x0, gh_fcn=gh_fcn, hess_fcn=hess_fcn) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1.58113883, 2.23606798, 1.58113883], 6, [t, 'x']) t_is(f, -5 * sqrt(2), 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['ineqnonlin'], array([0, sqrt(2) / 2]), 7, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) # ## check with fmincon (for dev testing only) # # fmoptions = optimset('Algorithm', 'interior-point') # # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], xmin, [], gh_fcn, fmoptions) # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], [], [], gh_fcn) # t_is(s, 1, 13, [t, 'success']) # t_is(x, [1.58113883 2.23606798 1.58113883], 4, [t, 'x']) # t_is(f, -5*sqrt(2), 8, [t, 'f']) # t_is(lam.ineqnonlin, [0sqrt(2)/2], 8, [t, 'lam.ineqnonlin']) t = 'constrained 3-d nonlinear (dict) : ' p = {'f_fcn': f_fcn, 'x0': x0, 'gh_fcn': gh_fcn, 'hess_fcn': hess_fcn} solution = pips(p) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1.58113883, 2.23606798, 1.58113883], 6, [t, 'x']) t_is(f, -5 * sqrt(2), 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['ineqnonlin'], [0, sqrt(2) / 2], 7, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) t = 'constrained 4-d nonlinear : ' ## Hock & Schittkowski test problem #71 f_fcn = f7 gh_fcn = gh7 hess_fcn = hess7 x0 = array([1.0, 5.0, 5.0, 1.0]) xmin = ones(4) xmax = 5 * xmin # solution = pips(f_fcn, x0, xmin=xmin, xmax=xmax, gh_fcn=gh_fcn, hess_fcn=hess_fcn, opt={'verbose': 2, 'comptol': 1e-9}) solution = pips(f_fcn, x0, xmin=xmin, xmax=xmax, gh_fcn=gh_fcn, hess_fcn=hess_fcn) x, f, s, lam, _ = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1, 4.7429994, 3.8211503, 1.3794082], 6, [t, 'x']) t_is(f, 17.0140173, 6, [t, 'f']) t_is(lam['eqnonlin'], 0.1614686, 5, [t, 'lam.eqnonlin']) t_is(lam['ineqnonlin'], 0.55229366, 5, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], [1.08787121024, 0, 0, 0], 5, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 7, [t, 'lam[\'upper\']']) t_end()
def t_dcline(quiet=False): """Tests for DC line extension in L{{toggle_dcline}. @author: Ray Zimmerman (PSERC Cornell) """ num_tests = 50 t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_dcline') if quiet: verbose = False else: verbose = False t0 = '' ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OPF_ALG=560, OPF_ALG_DC=200) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose) ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## load case ppc0 = loadcase(casefile) del ppc0['dclinecost'] ppc = ppc0 ppc = toggle_dcline(ppc, 'on') ppc = toggle_dcline(ppc, 'off') ndc = ppc['dcline'].shape[0] ## run AC OPF w/o DC lines t = ''.join([t0, 'AC OPF (no DC lines) : ']) r0 = runopf(ppc0, ppopt) success = r0['success'] t_ok(success, [t, 'success']) r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) t_is(r['f'], r0['f'], 8, [t, 'f']) t_is(r['bus'][:, ib_data], r0['bus'][:, ib_data], 10, [t, 'bus data']) t_is(r['bus'][:, ib_voltage], r0['bus'][:, ib_voltage], 3, [t, 'bus voltage']) t_is(r['bus'][:, ib_lam], r0['bus'][:, ib_lam], 3, [t, 'bus lambda']) t_is(r['bus'][:, ib_mu], r0['bus'][:, ib_mu], 2, [t, 'bus mu']) t_is(r['gen'][:, ig_data], r0['gen'][:, ig_data], 10, [t, 'gen data']) t_is(r['gen'][:, ig_disp], r0['gen'][:, ig_disp], 3, [t, 'gen dispatch']) t_is(r['gen'][:, ig_mu], r0['gen'][:, ig_mu], 3, [t, 'gen mu']) t_is(r['branch'][:, ibr_data], r0['branch'][:, ibr_data], 10, [t, 'branch data']) t_is(r['branch'][:, ibr_flow], r0['branch'][:, ibr_flow], 3, [t, 'branch flow']) t_is(r['branch'][:, ibr_mu], r0['branch'][:, ibr_mu], 2, [t, 'branch mu']) t = ''.join([t0, 'AC PF (no DC lines) : ']) ppc1 = { 'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy() } ppc1['bus'][:, VM] = 1 ppc1['bus'][:, VA] = 0 rp = runpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is(rp['bus'][:, ib_voltage], r['bus'][:, ib_voltage], 3, [t, 'bus voltage']) t_is(rp['gen'][:, ig_disp], r['gen'][:, ig_disp], 3, [t, 'gen dispatch']) t_is(rp['branch'][:, ibr_flow], r['branch'][:, ibr_flow], 3, [t, 'branch flow']) ## run with DC lines t = ''.join([t0, 'AC OPF (with DC lines) : ']) ppc = toggle_dcline(ppc, 'on') r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) expected = array([[10, 8.9, -10, 10, 1.0674, 1.0935], [2.2776, 2.2776, 0, 0, 1.0818, 1.0665], [0, 0, 0, 0, 1.0000, 1.0000], [10, 9.5, 0.0563, -10, 1.0778, 1.0665]]) t_is(r['dcline'][:, c.PF:c.VT + 1], expected, 4, [t, 'P Q V']) expected = array([[0, 0.8490, 0.6165, 0, 0, 0.2938], [0, 0, 0, 0.4290, 0.0739, 0], [0, 0, 0, 0, 0, 0], [0, 7.2209, 0, 0, 0.0739, 0]]) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected, 3, [t, 'mu']) t = ''.join([t0, 'AC PF (with DC lines) : ']) ppc1 = { 'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy() } ppc1 = toggle_dcline(ppc1, 'on') ppc1['bus'][:, VM] = 1 ppc1['bus'][:, VA] = 0 rp = runpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is(rp['bus'][:, ib_voltage], r['bus'][:, ib_voltage], 3, [t, 'bus voltage']) #t_is( rp['gen'][:,ig_disp ], r['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is(rp['gen'][:2, ig_disp], r['gen'][:2, ig_disp], 3, [t, 'gen dispatch']) t_is(rp['gen'][2, PG], r['gen'][2, PG], 3, [t, 'gen dispatch']) t_is(rp['gen'][2, QG] + rp['dcline'][0, c.QF], r['gen'][2, QG] + r['dcline'][0, c.QF], 3, [t, 'gen dispatch']) t_is(rp['branch'][:, ibr_flow], r['branch'][:, ibr_flow], 3, [t, 'branch flow']) ## add appropriate P and Q injections and check angles and generation when running PF t = ''.join([t0, 'AC PF (with equivalent injections) : ']) ppc1 = { 'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy() } ppc1['bus'][:, VM] = 1 ppc1['bus'][:, VA] = 0 for k in range(ndc): if ppc1['dcline'][k, c.BR_STATUS]: ff = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.F_BUS]) tt = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.T_BUS]) ppc1['bus'][ff, PD] = ppc1['bus'][ff, PD] + r['dcline'][k, c.PF] ppc1['bus'][ff, QD] = ppc1['bus'][ff, QD] - r['dcline'][k, c.QF] ppc1['bus'][tt, PD] = ppc1['bus'][tt, PD] - r['dcline'][k, c.PT] ppc1['bus'][tt, QD] = ppc1['bus'][tt, QD] - r['dcline'][k, c.QT] ppc1['bus'][ff, VM] = r['dcline'][k, c.VF] ppc1['bus'][tt, VM] = r['dcline'][k, c.VT] ppc1['bus'][ff, BUS_TYPE] = PV ppc1['bus'][tt, BUS_TYPE] = PV rp = runpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is(rp['bus'][:, ib_voltage], r['bus'][:, ib_voltage], 3, [t, 'bus voltage']) t_is(rp['gen'][:, ig_disp], r['gen'][:, ig_disp], 3, [t, 'gen dispatch']) t_is(rp['branch'][:, ibr_flow], r['branch'][:, ibr_flow], 3, [t, 'branch flow']) ## test DC OPF t = ''.join([t0, 'DC OPF (with DC lines) : ']) ppc = ppc0.copy() ppc['gen'][0, PMIN] = 10 ppc['branch'][4, RATE_A] = 100 ppc = toggle_dcline(ppc, 'on') r = rundcopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) expected = array([[10, 8.9, 0, 0, 1.01, 1], [2, 2, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1], [10, 9.5, 0, 0, 1, 0.98]]) t_is(r['dcline'][:, c.PF:c.VT + 1], expected, 4, [t, 'P Q V']) expected = array([[0, 1.8602, 0, 0, 0, 0], [1.8507, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0.2681, 0, 0, 0, 0]]) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected, 3, [t, 'mu']) t = ''.join([t0, 'DC PF (with DC lines) : ']) ppc1 = { 'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy() } ppc1 = toggle_dcline(ppc1, 'on') ppc1['bus'][:, VA] = 0 rp = rundcpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is(rp['bus'][:, ib_voltage], r['bus'][:, ib_voltage], 3, [t, 'bus voltage']) t_is(rp['gen'][:, ig_disp], r['gen'][:, ig_disp], 3, [t, 'gen dispatch']) t_is(rp['branch'][:, ibr_flow], r['branch'][:, ibr_flow], 3, [t, 'branch flow']) ## add appropriate P injections and check angles and generation when running PF t = ''.join([t0, 'DC PF (with equivalent injections) : ']) ppc1 = { 'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy() } ppc1['bus'][:, VA] = 0 for k in range(ndc): if ppc1['dcline'][k, c.BR_STATUS]: ff = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.F_BUS]) tt = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.T_BUS]) ppc1['bus'][ff, PD] = ppc1['bus'][ff, PD] + r['dcline'][k, c.PF] ppc1['bus'][tt, PD] = ppc1['bus'][tt, PD] - r['dcline'][k, c.PT] ppc1['bus'][ff, BUS_TYPE] = PV ppc1['bus'][tt, BUS_TYPE] = PV rp = rundcpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is(rp['bus'][:, ib_voltage], r['bus'][:, ib_voltage], 3, [t, 'bus voltage']) t_is(rp['gen'][:, ig_disp], r['gen'][:, ig_disp], 3, [t, 'gen dispatch']) t_is(rp['branch'][:, ibr_flow], r['branch'][:, ibr_flow], 3, [t, 'branch flow']) ## run with DC lines t = ''.join([t0, 'AC OPF (with DC lines + poly cost) : ']) ppc = loadcase(casefile) ppc = toggle_dcline(ppc, 'on') r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) expected1 = array([[10, 8.9, -10, 10, 1.0663, 1.0936], [7.8429, 7.8429, 0, 0, 1.0809, 1.0667], [0, 0, 0, 0, 1.0000, 1.0000], [6.0549, 5.7522, -0.5897, -10, 1.0778, 1.0667]]) t_is(r['dcline'][:, c.PF:c.VT + 1], expected1, 4, [t, 'P Q V']) expected2 = array([[0, 0.7605, 0.6226, 0, 0, 0.2980], [0, 0, 0, 0.4275, 0.0792, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0.0792, 0]]) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected2, 3, [t, 'mu']) ppc['dclinecost'][3, :8] = array([2, 0, 0, 4, 0, 0, 7.3, 0]) r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) t_is(r['dcline'][:, c.PF:c.VT + 1], expected1, 4, [t, 'P Q V']) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected2, 3, [t, 'mu']) t = ''.join([t0, 'AC OPF (with DC lines + pwl cost) : ']) ppc['dclinecost'][3, :8] = array([1, 0, 0, 2, 0, 0, 10, 73]) r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) t_is(r['dcline'][:, c.PF:c.VT + 1], expected1, 4, [t, 'P Q V']) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected2, 3, [t, 'mu']) t_end()
def t_hessian(quiet=False): """Numerical tests of 2nd derivative code. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(44, quiet) ## run powerflow to get solved case ppopt = ppoption(VERBOSE=0, OUT_ALL=0) results, _ = runpf(case30(), ppopt) baseMVA, bus, gen, branch = \ results['baseMVA'], results['bus'], results['gen'], results['branch'] ## switch to internal bus numbering and build admittance matrices _, bus, gen, branch = ext2int1(bus, gen, branch) Ybus, Yf, Yt = makeYbus(baseMVA, bus, branch) Vm = bus[:, VM] Va = bus[:, VA] * (pi / 180) V = Vm * exp(1j * Va) f = branch[:, F_BUS] ## list of "from" buses t = branch[:, T_BUS] ## list of "to" buses nl = len(f) nb = len(V) Cf = sparse((ones(nl), (range(nl), f)), (nl, nb)) ## connection matrix for line & from buses Ct = sparse((ones(nl), (range(nl), t)), (nl, nb)) ## connection matrix for line & to buses pert = 1e-8 ##----- check d2Sbus_dV2 code ----- t = ' - d2Sbus_dV2 (complex power injections)' lam = 10 * random.rand(nb) num_Haa = zeros((nb, nb), complex) num_Hav = zeros((nb, nb), complex) num_Hva = zeros((nb, nb), complex) num_Hvv = zeros((nb, nb), complex) dSbus_dVm, dSbus_dVa = dSbus_dV(Ybus, V) Haa, Hav, Hva, Hvv = d2Sbus_dV2(Ybus, V, lam) for i in range(nb): Vap = V.copy() Vap[i] = Vm[i] * exp(1j * (Va[i] + pert)) dSbus_dVm_ap, dSbus_dVa_ap = dSbus_dV(Ybus, Vap) num_Haa[:, i] = (dSbus_dVa_ap - dSbus_dVa).T * lam / pert num_Hva[:, i] = (dSbus_dVm_ap - dSbus_dVm).T * lam / pert Vmp = V.copy() Vmp[i] = (Vm[i] + pert) * exp(1j * Va[i]) dSbus_dVm_mp, dSbus_dVa_mp = dSbus_dV(Ybus, Vmp) num_Hav[:, i] = (dSbus_dVa_mp - dSbus_dVa).T * lam / pert num_Hvv[:, i] = (dSbus_dVm_mp - dSbus_dVm).T * lam / pert t_is(Haa.todense(), num_Haa, 4, ['Haa', t]) t_is(Hav.todense(), num_Hav, 4, ['Hav', t]) t_is(Hva.todense(), num_Hva, 4, ['Hva', t]) t_is(Hvv.todense(), num_Hvv, 4, ['Hvv', t]) ##----- check d2Sbr_dV2 code ----- t = ' - d2Sbr_dV2 (complex power flows)' lam = 10 * random.rand(nl) # lam = [1 zeros(nl-1, 1)] num_Gfaa = zeros((nb, nb), complex) num_Gfav = zeros((nb, nb), complex) num_Gfva = zeros((nb, nb), complex) num_Gfvv = zeros((nb, nb), complex) num_Gtaa = zeros((nb, nb), complex) num_Gtav = zeros((nb, nb), complex) num_Gtva = zeros((nb, nb), complex) num_Gtvv = zeros((nb, nb), complex) dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, _, _ = dSbr_dV(branch, Yf, Yt, V) Gfaa, Gfav, Gfva, Gfvv = d2Sbr_dV2(Cf, Yf, V, lam) Gtaa, Gtav, Gtva, Gtvv = d2Sbr_dV2(Ct, Yt, V, lam) for i in range(nb): Vap = V.copy() Vap[i] = Vm[i] * exp(1j * (Va[i] + pert)) dSf_dVa_ap, dSf_dVm_ap, dSt_dVa_ap, dSt_dVm_ap, Sf_ap, St_ap = \ dSbr_dV(branch, Yf, Yt, Vap) num_Gfaa[:, i] = (dSf_dVa_ap - dSf_dVa).T * lam / pert num_Gfva[:, i] = (dSf_dVm_ap - dSf_dVm).T * lam / pert num_Gtaa[:, i] = (dSt_dVa_ap - dSt_dVa).T * lam / pert num_Gtva[:, i] = (dSt_dVm_ap - dSt_dVm).T * lam / pert Vmp = V.copy() Vmp[i] = (Vm[i] + pert) * exp(1j * Va[i]) dSf_dVa_mp, dSf_dVm_mp, dSt_dVa_mp, dSt_dVm_mp, Sf_mp, St_mp = \ dSbr_dV(branch, Yf, Yt, Vmp) num_Gfav[:, i] = (dSf_dVa_mp - dSf_dVa).T * lam / pert num_Gfvv[:, i] = (dSf_dVm_mp - dSf_dVm).T * lam / pert num_Gtav[:, i] = (dSt_dVa_mp - dSt_dVa).T * lam / pert num_Gtvv[:, i] = (dSt_dVm_mp - dSt_dVm).T * lam / pert t_is(Gfaa.todense(), num_Gfaa, 4, ['Gfaa', t]) t_is(Gfav.todense(), num_Gfav, 4, ['Gfav', t]) t_is(Gfva.todense(), num_Gfva, 4, ['Gfva', t]) t_is(Gfvv.todense(), num_Gfvv, 4, ['Gfvv', t]) t_is(Gtaa.todense(), num_Gtaa, 4, ['Gtaa', t]) t_is(Gtav.todense(), num_Gtav, 4, ['Gtav', t]) t_is(Gtva.todense(), num_Gtva, 4, ['Gtva', t]) t_is(Gtvv.todense(), num_Gtvv, 4, ['Gtvv', t]) ##----- check d2Ibr_dV2 code ----- t = ' - d2Ibr_dV2 (complex currents)' lam = 10 * random.rand(nl) # lam = [1, zeros(nl-1)] num_Gfaa = zeros((nb, nb), complex) num_Gfav = zeros((nb, nb), complex) num_Gfva = zeros((nb, nb), complex) num_Gfvv = zeros((nb, nb), complex) num_Gtaa = zeros((nb, nb), complex) num_Gtav = zeros((nb, nb), complex) num_Gtva = zeros((nb, nb), complex) num_Gtvv = zeros((nb, nb), complex) dIf_dVa, dIf_dVm, dIt_dVa, dIt_dVm, _, _ = dIbr_dV(branch, Yf, Yt, V) Gfaa, Gfav, Gfva, Gfvv = d2Ibr_dV2(Yf, V, lam) Gtaa, Gtav, Gtva, Gtvv = d2Ibr_dV2(Yt, V, lam) for i in range(nb): Vap = V.copy() Vap[i] = Vm[i] * exp(1j * (Va[i] + pert)) dIf_dVa_ap, dIf_dVm_ap, dIt_dVa_ap, dIt_dVm_ap, If_ap, It_ap = \ dIbr_dV(branch, Yf, Yt, Vap) num_Gfaa[:, i] = (dIf_dVa_ap - dIf_dVa).T * lam / pert num_Gfva[:, i] = (dIf_dVm_ap - dIf_dVm).T * lam / pert num_Gtaa[:, i] = (dIt_dVa_ap - dIt_dVa).T * lam / pert num_Gtva[:, i] = (dIt_dVm_ap - dIt_dVm).T * lam / pert Vmp = V.copy() Vmp[i] = (Vm[i] + pert) * exp(1j * Va[i]) dIf_dVa_mp, dIf_dVm_mp, dIt_dVa_mp, dIt_dVm_mp, If_mp, It_mp = \ dIbr_dV(branch, Yf, Yt, Vmp) num_Gfav[:, i] = (dIf_dVa_mp - dIf_dVa).T * lam / pert num_Gfvv[:, i] = (dIf_dVm_mp - dIf_dVm).T * lam / pert num_Gtav[:, i] = (dIt_dVa_mp - dIt_dVa).T * lam / pert num_Gtvv[:, i] = (dIt_dVm_mp - dIt_dVm).T * lam / pert t_is(Gfaa.todense(), num_Gfaa, 4, ['Gfaa', t]) t_is(Gfav.todense(), num_Gfav, 4, ['Gfav', t]) t_is(Gfva.todense(), num_Gfva, 4, ['Gfva', t]) t_is(Gfvv.todense(), num_Gfvv, 4, ['Gfvv', t]) t_is(Gtaa.todense(), num_Gtaa, 4, ['Gtaa', t]) t_is(Gtav.todense(), num_Gtav, 4, ['Gtav', t]) t_is(Gtva.todense(), num_Gtva, 4, ['Gtva', t]) t_is(Gtvv.todense(), num_Gtvv, 4, ['Gtvv', t]) ##----- check d2ASbr_dV2 code ----- t = ' - d2ASbr_dV2 (squared apparent power flows)' lam = 10 * random.rand(nl) # lam = [1 zeros(nl-1, 1)] num_Gfaa = zeros((nb, nb), complex) num_Gfav = zeros((nb, nb), complex) num_Gfva = zeros((nb, nb), complex) num_Gfvv = zeros((nb, nb), complex) num_Gtaa = zeros((nb, nb), complex) num_Gtav = zeros((nb, nb), complex) num_Gtva = zeros((nb, nb), complex) num_Gtvv = zeros((nb, nb), complex) dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, Sf, St = dSbr_dV(branch, Yf, Yt, V) dAf_dVa, dAf_dVm, dAt_dVa, dAt_dVm = \ dAbr_dV(dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, Sf, St) Gfaa, Gfav, Gfva, Gfvv = d2ASbr_dV2(dSf_dVa, dSf_dVm, Sf, Cf, Yf, V, lam) Gtaa, Gtav, Gtva, Gtvv = d2ASbr_dV2(dSt_dVa, dSt_dVm, St, Ct, Yt, V, lam) for i in range(nb): Vap = V.copy() Vap[i] = Vm[i] * exp(1j * (Va[i] + pert)) dSf_dVa_ap, dSf_dVm_ap, dSt_dVa_ap, dSt_dVm_ap, Sf_ap, St_ap = \ dSbr_dV(branch, Yf, Yt, Vap) dAf_dVa_ap, dAf_dVm_ap, dAt_dVa_ap, dAt_dVm_ap = \ dAbr_dV(dSf_dVa_ap, dSf_dVm_ap, dSt_dVa_ap, dSt_dVm_ap, Sf_ap, St_ap) num_Gfaa[:, i] = (dAf_dVa_ap - dAf_dVa).T * lam / pert num_Gfva[:, i] = (dAf_dVm_ap - dAf_dVm).T * lam / pert num_Gtaa[:, i] = (dAt_dVa_ap - dAt_dVa).T * lam / pert num_Gtva[:, i] = (dAt_dVm_ap - dAt_dVm).T * lam / pert Vmp = V.copy() Vmp[i] = (Vm[i] + pert) * exp(1j * Va[i]) dSf_dVa_mp, dSf_dVm_mp, dSt_dVa_mp, dSt_dVm_mp, Sf_mp, St_mp = \ dSbr_dV(branch, Yf, Yt, Vmp) dAf_dVa_mp, dAf_dVm_mp, dAt_dVa_mp, dAt_dVm_mp = \ dAbr_dV(dSf_dVa_mp, dSf_dVm_mp, dSt_dVa_mp, dSt_dVm_mp, Sf_mp, St_mp) num_Gfav[:, i] = (dAf_dVa_mp - dAf_dVa).T * lam / pert num_Gfvv[:, i] = (dAf_dVm_mp - dAf_dVm).T * lam / pert num_Gtav[:, i] = (dAt_dVa_mp - dAt_dVa).T * lam / pert num_Gtvv[:, i] = (dAt_dVm_mp - dAt_dVm).T * lam / pert t_is(Gfaa.todense(), num_Gfaa, 2, ['Gfaa', t]) t_is(Gfav.todense(), num_Gfav, 2, ['Gfav', t]) t_is(Gfva.todense(), num_Gfva, 2, ['Gfva', t]) t_is(Gfvv.todense(), num_Gfvv, 2, ['Gfvv', t]) t_is(Gtaa.todense(), num_Gtaa, 2, ['Gtaa', t]) t_is(Gtav.todense(), num_Gtav, 2, ['Gtav', t]) t_is(Gtva.todense(), num_Gtva, 2, ['Gtva', t]) t_is(Gtvv.todense(), num_Gtvv, 2, ['Gtvv', t]) ##----- check d2ASbr_dV2 code ----- t = ' - d2ASbr_dV2 (squared real power flows)' lam = 10 * random.rand(nl) # lam = [1 zeros(nl-1, 1)] num_Gfaa = zeros((nb, nb), complex) num_Gfav = zeros((nb, nb), complex) num_Gfva = zeros((nb, nb), complex) num_Gfvv = zeros((nb, nb), complex) num_Gtaa = zeros((nb, nb), complex) num_Gtav = zeros((nb, nb), complex) num_Gtva = zeros((nb, nb), complex) num_Gtvv = zeros((nb, nb), complex) dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, Sf, St = dSbr_dV(branch, Yf, Yt, V) dAf_dVa, dAf_dVm, dAt_dVa, dAt_dVm = \ dAbr_dV(dSf_dVa.real, dSf_dVm.real, dSt_dVa.real, dSt_dVm.real, Sf.real, St.real) Gfaa, Gfav, Gfva, Gfvv = d2ASbr_dV2(dSf_dVa.real, dSf_dVm.real, Sf.real, Cf, Yf, V, lam) Gtaa, Gtav, Gtva, Gtvv = d2ASbr_dV2(dSt_dVa.real, dSt_dVm.real, St.real, Ct, Yt, V, lam) for i in range(nb): Vap = V.copy() Vap[i] = Vm[i] * exp(1j * (Va[i] + pert)) dSf_dVa_ap, dSf_dVm_ap, dSt_dVa_ap, dSt_dVm_ap, Sf_ap, St_ap = \ dSbr_dV(branch, Yf, Yt, Vap) dAf_dVa_ap, dAf_dVm_ap, dAt_dVa_ap, dAt_dVm_ap = \ dAbr_dV(dSf_dVa_ap.real, dSf_dVm_ap.real, dSt_dVa_ap.real, dSt_dVm_ap.real, Sf_ap.real, St_ap.real) num_Gfaa[:, i] = (dAf_dVa_ap - dAf_dVa).T * lam / pert num_Gfva[:, i] = (dAf_dVm_ap - dAf_dVm).T * lam / pert num_Gtaa[:, i] = (dAt_dVa_ap - dAt_dVa).T * lam / pert num_Gtva[:, i] = (dAt_dVm_ap - dAt_dVm).T * lam / pert Vmp = V.copy() Vmp[i] = (Vm[i] + pert) * exp(1j * Va[i]) dSf_dVa_mp, dSf_dVm_mp, dSt_dVa_mp, dSt_dVm_mp, Sf_mp, St_mp = \ dSbr_dV(branch, Yf, Yt, Vmp) dAf_dVa_mp, dAf_dVm_mp, dAt_dVa_mp, dAt_dVm_mp = \ dAbr_dV(dSf_dVa_mp.real, dSf_dVm_mp.real, dSt_dVa_mp.real, dSt_dVm_mp.real, Sf_mp.real, St_mp.real) num_Gfav[:, i] = (dAf_dVa_mp - dAf_dVa).T * lam / pert num_Gfvv[:, i] = (dAf_dVm_mp - dAf_dVm).T * lam / pert num_Gtav[:, i] = (dAt_dVa_mp - dAt_dVa).T * lam / pert num_Gtvv[:, i] = (dAt_dVm_mp - dAt_dVm).T * lam / pert t_is(Gfaa.todense(), num_Gfaa, 2, ['Gfaa', t]) t_is(Gfav.todense(), num_Gfav, 2, ['Gfav', t]) t_is(Gfva.todense(), num_Gfva, 2, ['Gfva', t]) t_is(Gfvv.todense(), num_Gfvv, 2, ['Gfvv', t]) t_is(Gtaa.todense(), num_Gtaa, 2, ['Gtaa', t]) t_is(Gtav.todense(), num_Gtav, 2, ['Gtav', t]) t_is(Gtva.todense(), num_Gtva, 2, ['Gtva', t]) t_is(Gtvv.todense(), num_Gtvv, 2, ['Gtvv', t]) ##----- check d2AIbr_dV2 code ----- t = ' - d2AIbr_dV2 (squared current magnitudes)' lam = 10 * random.rand(nl) # lam = [1 zeros(nl-1, 1)] num_Gfaa = zeros((nb, nb), complex) num_Gfav = zeros((nb, nb), complex) num_Gfva = zeros((nb, nb), complex) num_Gfvv = zeros((nb, nb), complex) num_Gtaa = zeros((nb, nb), complex) num_Gtav = zeros((nb, nb), complex) num_Gtva = zeros((nb, nb), complex) num_Gtvv = zeros((nb, nb), complex) dIf_dVa, dIf_dVm, dIt_dVa, dIt_dVm, If, It = dIbr_dV(branch, Yf, Yt, V) dAf_dVa, dAf_dVm, dAt_dVa, dAt_dVm = \ dAbr_dV(dIf_dVa, dIf_dVm, dIt_dVa, dIt_dVm, If, It) Gfaa, Gfav, Gfva, Gfvv = d2AIbr_dV2(dIf_dVa, dIf_dVm, If, Yf, V, lam) Gtaa, Gtav, Gtva, Gtvv = d2AIbr_dV2(dIt_dVa, dIt_dVm, It, Yt, V, lam) for i in range(nb): Vap = V.copy() Vap[i] = Vm[i] * exp(1j * (Va[i] + pert)) dIf_dVa_ap, dIf_dVm_ap, dIt_dVa_ap, dIt_dVm_ap, If_ap, It_ap = \ dIbr_dV(branch, Yf, Yt, Vap) dAf_dVa_ap, dAf_dVm_ap, dAt_dVa_ap, dAt_dVm_ap = \ dAbr_dV(dIf_dVa_ap, dIf_dVm_ap, dIt_dVa_ap, dIt_dVm_ap, If_ap, It_ap) num_Gfaa[:, i] = (dAf_dVa_ap - dAf_dVa).T * lam / pert num_Gfva[:, i] = (dAf_dVm_ap - dAf_dVm).T * lam / pert num_Gtaa[:, i] = (dAt_dVa_ap - dAt_dVa).T * lam / pert num_Gtva[:, i] = (dAt_dVm_ap - dAt_dVm).T * lam / pert Vmp = V.copy() Vmp[i] = (Vm[i] + pert) * exp(1j * Va[i]) dIf_dVa_mp, dIf_dVm_mp, dIt_dVa_mp, dIt_dVm_mp, If_mp, It_mp = \ dIbr_dV(branch, Yf, Yt, Vmp) dAf_dVa_mp, dAf_dVm_mp, dAt_dVa_mp, dAt_dVm_mp = \ dAbr_dV(dIf_dVa_mp, dIf_dVm_mp, dIt_dVa_mp, dIt_dVm_mp, If_mp, It_mp) num_Gfav[:, i] = (dAf_dVa_mp - dAf_dVa).T * lam / pert num_Gfvv[:, i] = (dAf_dVm_mp - dAf_dVm).T * lam / pert num_Gtav[:, i] = (dAt_dVa_mp - dAt_dVa).T * lam / pert num_Gtvv[:, i] = (dAt_dVm_mp - dAt_dVm).T * lam / pert t_is(Gfaa.todense(), num_Gfaa, 3, ['Gfaa', t]) t_is(Gfav.todense(), num_Gfav, 3, ['Gfav', t]) t_is(Gfva.todense(), num_Gfva, 3, ['Gfva', t]) t_is(Gfvv.todense(), num_Gfvv, 2, ['Gfvv', t]) t_is(Gtaa.todense(), num_Gtaa, 3, ['Gtaa', t]) t_is(Gtav.todense(), num_Gtav, 3, ['Gtav', t]) t_is(Gtva.todense(), num_Gtva, 3, ['Gtva', t]) t_is(Gtvv.todense(), num_Gtvv, 2, ['Gtvv', t]) t_end()
def t_dcline(quiet=False): """Tests for DC line extension in L{{toggle_dcline}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ num_tests = 50 t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_dcline') if quiet: verbose = False else: verbose = False t0 = '' ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OPF_ALG=560, OPF_ALG_DC=200) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose) ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## load case ppc0 = loadcase(casefile) del ppc0['dclinecost'] ppc = ppc0 ppc = toggle_dcline(ppc, 'on') ppc = toggle_dcline(ppc, 'off') ndc = ppc['dcline'].shape[0] ## run AC OPF w/o DC lines t = ''.join([t0, 'AC OPF (no DC lines) : ']) r0 = runopf(ppc0, ppopt) success = r0['success'] t_ok(success, [t, 'success']) r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) t_is(r['f'], r0['f'], 8, [t, 'f']) t_is( r['bus'][:,ib_data ], r0['bus'][:,ib_data ], 10, [t, 'bus data']) t_is( r['bus'][:,ib_voltage], r0['bus'][:,ib_voltage], 3, [t, 'bus voltage']) t_is( r['bus'][:,ib_lam ], r0['bus'][:,ib_lam ], 3, [t, 'bus lambda']) t_is( r['bus'][:,ib_mu ], r0['bus'][:,ib_mu ], 2, [t, 'bus mu']) t_is( r['gen'][:,ig_data ], r0['gen'][:,ig_data ], 10, [t, 'gen data']) t_is( r['gen'][:,ig_disp ], r0['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is( r['gen'][:,ig_mu ], r0['gen'][:,ig_mu ], 3, [t, 'gen mu']) t_is(r['branch'][:,ibr_data ], r0['branch'][:,ibr_data ], 10, [t, 'branch data']) t_is(r['branch'][:,ibr_flow ], r0['branch'][:,ibr_flow ], 3, [t, 'branch flow']) t_is(r['branch'][:,ibr_mu ], r0['branch'][:,ibr_mu ], 2, [t, 'branch mu']) t = ''.join([t0, 'AC PF (no DC lines) : ']) ppc1 = {'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy()} ppc1['bus'][:, VM] = 1 ppc1['bus'][:, VA] = 0 rp = runpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is( rp['bus'][:,ib_voltage], r['bus'][:,ib_voltage], 3, [t, 'bus voltage']) t_is( rp['gen'][:,ig_disp ], r['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is(rp['branch'][:,ibr_flow ], r['branch'][:,ibr_flow ], 3, [t, 'branch flow']) ## run with DC lines t = ''.join([t0, 'AC OPF (with DC lines) : ']) ppc = toggle_dcline(ppc, 'on') r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) expected = array([ [10, 8.9, -10, 10, 1.0674, 1.0935], [2.2776, 2.2776, 0, 0, 1.0818, 1.0665], [0, 0, 0, 0, 1.0000, 1.0000], [10, 9.5, 0.0563, -10, 1.0778, 1.0665] ]) t_is(r['dcline'][:, c.PF:c.VT + 1], expected, 4, [t, 'P Q V']) expected = array([ [0, 0.8490, 0.6165, 0, 0, 0.2938], [0, 0, 0, 0.4290, 0.0739, 0], [0, 0, 0, 0, 0, 0], [0, 7.2209, 0, 0, 0.0739, 0] ]) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected, 3, [t, 'mu']) t = ''.join([t0, 'AC PF (with DC lines) : ']) ppc1 = {'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy()} ppc1 = toggle_dcline(ppc1, 'on') ppc1['bus'][:, VM] = 1 ppc1['bus'][:, VA] = 0 rp = runpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is( rp['bus'][:,ib_voltage], r['bus'][:,ib_voltage], 3, [t, 'bus voltage']) #t_is( rp['gen'][:,ig_disp ], r['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is( rp['gen'][:2,ig_disp ], r['gen'][:2,ig_disp ], 3, [t, 'gen dispatch']) t_is( rp['gen'][2,PG ], r['gen'][2,PG ], 3, [t, 'gen dispatch']) t_is( rp['gen'][2,QG]+rp['dcline'][0,c.QF], r['gen'][2,QG]+r['dcline'][0,c.QF], 3, [t, 'gen dispatch']) t_is(rp['branch'][:,ibr_flow ], r['branch'][:,ibr_flow ], 3, [t, 'branch flow']) ## add appropriate P and Q injections and check angles and generation when running PF t = ''.join([t0, 'AC PF (with equivalent injections) : ']) ppc1 = {'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy()} ppc1['bus'][:, VM] = 1 ppc1['bus'][:, VA] = 0 for k in range(ndc): if ppc1['dcline'][k, c.BR_STATUS]: ff = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.F_BUS]) tt = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.T_BUS]) ppc1['bus'][ff, PD] = ppc1['bus'][ff, PD] + r['dcline'][k, c.PF] ppc1['bus'][ff, QD] = ppc1['bus'][ff, QD] - r['dcline'][k, c.QF] ppc1['bus'][tt, PD] = ppc1['bus'][tt, PD] - r['dcline'][k, c.PT] ppc1['bus'][tt, QD] = ppc1['bus'][tt, QD] - r['dcline'][k, c.QT] ppc1['bus'][ff, VM] = r['dcline'][k, c.VF] ppc1['bus'][tt, VM] = r['dcline'][k, c.VT] ppc1['bus'][ff, BUS_TYPE] = PV ppc1['bus'][tt, BUS_TYPE] = PV rp = runpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is( rp['bus'][:,ib_voltage], r['bus'][:,ib_voltage], 3, [t, 'bus voltage']) t_is( rp['gen'][:,ig_disp ], r['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is(rp['branch'][:,ibr_flow ], r['branch'][:,ibr_flow ], 3, [t, 'branch flow']) ## test DC OPF t = ''.join([t0, 'DC OPF (with DC lines) : ']) ppc = ppc0.copy() ppc['gen'][0, PMIN] = 10 ppc['branch'][4, RATE_A] = 100 ppc = toggle_dcline(ppc, 'on') r = rundcopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) expected = array([ [10, 8.9, 0, 0, 1.01, 1], [2, 2, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1], [10, 9.5, 0, 0, 1, 0.98] ]) t_is(r['dcline'][:, c.PF:c.VT + 1], expected, 4, [t, 'P Q V']) expected = array([ [0, 1.8602, 0, 0, 0, 0], [1.8507, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0.2681, 0, 0, 0, 0] ]) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected, 3, [t, 'mu']) t = ''.join([t0, 'DC PF (with DC lines) : ']) ppc1 = {'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy()} ppc1 = toggle_dcline(ppc1, 'on') ppc1['bus'][:, VA] = 0 rp = rundcpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is( rp['bus'][:,ib_voltage], r['bus'][:,ib_voltage], 3, [t, 'bus voltage']) t_is( rp['gen'][:,ig_disp ], r['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is(rp['branch'][:,ibr_flow ], r['branch'][:,ibr_flow ], 3, [t, 'branch flow']) ## add appropriate P injections and check angles and generation when running PF t = ''.join([t0, 'DC PF (with equivalent injections) : ']) ppc1 = {'baseMVA': r['baseMVA'], 'bus': r['bus'][:, :VMIN + 1].copy(), 'gen': r['gen'][:, :APF + 1].copy(), 'branch': r['branch'][:, :ANGMAX + 1].copy(), 'gencost': r['gencost'].copy(), 'dcline': r['dcline'][:, :c.LOSS1 + 1].copy()} ppc1['bus'][:, VA] = 0 for k in range(ndc): if ppc1['dcline'][k, c.BR_STATUS]: ff = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.F_BUS]) tt = find(ppc1['bus'][:, BUS_I] == ppc1['dcline'][k, c.T_BUS]) ppc1['bus'][ff, PD] = ppc1['bus'][ff, PD] + r['dcline'][k, c.PF] ppc1['bus'][tt, PD] = ppc1['bus'][tt, PD] - r['dcline'][k, c.PT] ppc1['bus'][ff, BUS_TYPE] = PV ppc1['bus'][tt, BUS_TYPE] = PV rp = rundcpf(ppc1, ppopt) success = rp['success'] t_ok(success, [t, 'success']) t_is( rp['bus'][:,ib_voltage], r['bus'][:,ib_voltage], 3, [t, 'bus voltage']) t_is( rp['gen'][:,ig_disp ], r['gen'][:,ig_disp ], 3, [t, 'gen dispatch']) t_is(rp['branch'][:,ibr_flow ], r['branch'][:,ibr_flow ], 3, [t, 'branch flow']) ## run with DC lines t = ''.join([t0, 'AC OPF (with DC lines + poly cost) : ']) ppc = loadcase(casefile) ppc = toggle_dcline(ppc, 'on') r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) expected1 = array([ [10, 8.9, -10, 10, 1.0663, 1.0936], [7.8429, 7.8429, 0, 0, 1.0809, 1.0667], [0, 0, 0, 0, 1.0000, 1.0000], [6.0549, 5.7522, -0.5897, -10, 1.0778, 1.0667] ]) t_is(r['dcline'][:, c.PF:c.VT + 1], expected1, 4, [t, 'P Q V']) expected2 = array([ [0, 0.7605, 0.6226, 0, 0, 0.2980], [0, 0, 0, 0.4275, 0.0792, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0.0792, 0] ]) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected2, 3, [t, 'mu']) ppc['dclinecost'][3, :8] = array([2, 0, 0, 4, 0, 0, 7.3, 0]) r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) t_is(r['dcline'][:, c.PF:c.VT + 1], expected1, 4, [t, 'P Q V']) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected2, 3, [t, 'mu']) t = ''.join([t0, 'AC OPF (with DC lines + pwl cost) : ']) ppc['dclinecost'][3, :8] = array([1, 0, 0, 2, 0, 0, 10, 73]) r = runopf(ppc, ppopt) success = r['success'] t_ok(success, [t, 'success']) t_is(r['dcline'][:, c.PF:c.VT + 1], expected1, 4, [t, 'P Q V']) t_is(r['dcline'][:, c.MU_PMIN:c.MU_QMAXT + 1], expected2, 3, [t, 'mu']) t_end()
def t_modcost(quiet=False): """Tests for code in C{modcost}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 80 t_begin(n_tests, quiet) ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost0 = array([ [2, 0, 0, 3, 0.01, 0.1, 1, 0, 0, 0, 0, 0], [2, 0, 0, 5, 0.0006, 0.005, 0.04, 0.3, 2, 0, 0, 0], [1, 0, 0, 4, 0, 0, 10, 200, 20, 600, 30, 1200], [1, 0, 0, 4, -30, -2400, -20, -1800, -10, -1000, 0, 0] ]) gencost = modcost(gencost0, 5, 'SCALE_F') ##----- POLYSHIFT ----- t = 'modcost SCALE_F - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0])) / 5, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0])) / 5, [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0])) / 5, [1.24, 2, 0, 0], 8, t) t = 'modcost SCALE_F - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0])) / 5, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0])) / 5, [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0])) / 5, [1, 2.8096, 0, 0], 8, t) t = 'modcost SCALE_F - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0 ])) / 5, [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0])) / 5, [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0])) / 5, [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0])) / 5, [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0])) / 5, [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0])) / 5, [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0])) / 5, [1, 2, 1500, 0], 8, t) t = 'modcost SCALE_F - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5 ])) / 5, [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10])) / 5, [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15])) / 5, [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20])) / 5, [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25])) / 5, [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30])) / 5, [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35])) / 5, [1, 2, 0, -2700], 8, t) gencost = modcost(gencost0, 2, 'SCALE_X') t = 'modcost SCALE_X - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0]) * 2), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0]) * 2), [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0]) * 2), [1.24, 2, 0, 0], 8, t) t = 'modcost SCALE_X - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0]) * 2), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0]) * 2), [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0]) * 2), [1, 2.8096, 0, 0], 8, t) t = 'modcost SCALE_X - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0 ]) * 2), [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0]) * 2), [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0]) * 2), [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0]) * 2), [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0]) * 2), [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0]) * 2), [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0]) * 2), [1, 2, 1500, 0], 8, t) t = 'modcost SCALE_X - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5 ]) * 2), [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10]) * 2), [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15]) * 2), [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20]) * 2), [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25]) * 2), [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30]) * 2), [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35]) * 2), [1, 2, 0, -2700], 8, t) gencost = modcost(gencost0, 3, 'SHIFT_F') t = 'modcost SHIFT_F - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0])) - 3, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0])) - 3, [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0])) - 3, [1.24, 2, 0, 0], 8, t) t = 'modcost SHIFT_F - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0])) - 3, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0])) - 3, [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0])) - 3, [1, 2.8096, 0, 0], 8, t) t = 'modcost SHIFT_F - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0 ])) - 3, [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0])) - 3, [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0])) - 3, [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0])) - 3, [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0])) - 3, [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0])) - 3, [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0])) - 3, [1, 2, 1500, 0], 8, t) t = 'modcost SHIFT_F - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5 ])) - 3, [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10])) - 3, [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15])) - 3, [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20])) - 3, [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25])) - 3, [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30])) - 3, [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35])) - 3, [1, 2, 0, -2700], 8, t) gencost = modcost(gencost0, -4, 'SHIFT_X') t = 'modcost SHIFT_X - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0]) - 4), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0]) - 4), [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0]) - 4), [1.24, 2, 0, 0], 8, t) t = 'modcost SHIFT_X - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0]) - 4), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0]) - 4), [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0]) - 4), [1, 2.8096, 0, 0], 8, t) t = 'modcost SHIFT_X - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0 ]) - 4), [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0]) - 4), [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0]) - 4), [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0]) - 4), [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0]) - 4), [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0]) - 4), [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0]) - 4), [1, 2, 1500, 0], 8, t) t = 'modcost SHIFT_X - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5 ]) - 4), [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10]) - 4), [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15]) - 4), [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20]) - 4), [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25]) - 4), [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30]) - 4), [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35]) - 4), [1, 2, 0, -2700], 8, t) t_end()
def t_opf_dc_pips(quiet=False): """Tests for DC optimal power flow using PIPS solver. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ num_tests = 23 t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') verbose = 0#not quiet t0 = 'DC OPF (PIPS): ' ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0, OPF_ALG_DC=200) ## run DC OPF ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) #ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## get solved DC power flow case from MAT-file soln9_dcopf = loadmat(join(tdir, 'soln9_dcopf.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_dcopf['bus_soln'] gen_soln = soln9_dcopf['gen_soln'] branch_soln = soln9_dcopf['branch_soln'] f_soln = soln9_dcopf['f_soln'][0] ## run OPF t = t0 r = rundcopf(casefile, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ##----- run OPF with extra linear user constraints & costs ----- ## two new z variables ## 0 <= z1, P2 - P1 <= z1 ## 0 <= z2, P2 - P3 <= z2 ## with A and N sized for DC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [9, 10, 12, 10, 11, 13] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 14)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [12, 13])), (2, 14)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 1 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) ## with A and N sized for AC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [18, 19, 24, 19, 20, 25] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 26)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [24, 25])), (2, 26)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 2 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) t = ''.join([t0, 'infeasible : ']) ## with A and N sized for DC opf ppc = loadcase(casefile) ppc['A'] = sparse(([1, 1], ([0, 0], [9, 10])), (1, 14)) ## Pg1 + Pg2 ppc['u'] = array([Inf]) ppc['l'] = array([600]) r = rundcopf(ppc, ppopt) t_ok(not r['success'], [t, 'no success']) t_end()
def t_runopf_w_res(quiet=False): """Tests C{runopf_w_res} and the associated callbacks. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(46, quiet) verbose = 0 #not quiet tdir = dirname(__file__) casefile = join(tdir, 't_case30_userfcns') ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose, OPF_ALG=560) t = 'runopf_w_res(' 't_case30_userfcns' ') : ' r = runopf_w_res(casefile, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 19.3906, 0.6094], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 7, [t, 'mu.Pmax']) ppc = loadcase(casefile) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t = 'gen 5 no reserves : ' ppc = loadcase(casefile) ppc['reserves']['zones'][:, 4] = 0 ppc['reserves']['cost'] = delete(ppc['reserves']['cost'], 4) ppc['reserves']['qty'] = delete(ppc['reserves']['qty'], 4) r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 0, 20], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 0, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 6, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 187.5, 4, [t, 'totalcost']) t = 'extra offline gen : ' ppc = loadcase(casefile) idx = list(range(3)) + [4] + list(range(3, 6)) ppc['gen'] = ppc['gen'][idx, :] ppc['gencost'] = ppc['gencost'][idx, :] ppc['reserves']['zones'] = ppc['reserves']['zones'][:, idx] ppc['reserves']['cost'] = ppc['reserves']['cost'][idx] ppc['reserves']['qty'] = ppc['reserves']['qty'][idx] ppc['gen'][3, GEN_STATUS] = 0 r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 0, 19.3906, 0.6094], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 5.5, 2, 5.5, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 0, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0, 0.5, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t = 'both extra & gen 6 no res : ' ppc = loadcase(casefile) idx = list(range(3)) + [4] + list(range(3, 6)) ppc['gen'] = ppc['gen'][idx, :] ppc['gencost'] = ppc['gencost'][idx, :] ppc['reserves']['zones'] = ppc['reserves']['zones'][:, idx] ppc['reserves']['cost'] = ppc['reserves']['cost'][idx] ppc['reserves']['qty'] = ppc['reserves']['qty'][idx] ppc['gen'][3, GEN_STATUS] = 0 ppc['reserves']['zones'][:, 5] = 0 ppc['reserves']['cost'] = delete(ppc['reserves']['cost'], 5) ppc['reserves']['qty'] = delete(ppc['reserves']['qty'], 5) r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 0, 0, 20], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 5.5, 2, 0, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 0, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0, 0], 6, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0, 0, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 187.5, 4, [t, 'totalcost']) t = 'no qty (Rmax) : ' ppc = loadcase(casefile) del ppc['reserves']['qty'] r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [39.3826, 0.6174, 0, 0, 19.3818, 0.6182], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 5, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 5, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0.1, 0, 0, 0, 0.5, 0], 5, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['totalcost'], 176.3708, 4, [t, 'totalcost']) t = 'RAMP_10, no qty (Rmax) : ' ppc = loadcase(casefile) del ppc['reserves']['qty'] ppc['gen'][0, RAMP_10] = 25 r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 19.3906, 0.6094], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t_end()
def t_opf_pips(quiet=False): """Tests for PIPS-based AC optimal power flow. @author: Ray Zimmerman (PSERC Cornell) """ num_tests = 101 t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') verbose = 0#not quiet t0 = 'PIPS : ' ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose, OPF_ALG=560) ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## get solved AC power flow case from MAT-file soln9_opf = loadmat(join(tdir, 'soln9_opf.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf['bus_soln'] gen_soln = soln9_opf['gen_soln'] branch_soln = soln9_opf['branch_soln'] f_soln = soln9_opf['f_soln'][0] ## run OPF t = t0 r = runopf(casefile, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ## run with automatic conversion of single-block pwl to linear costs t = ''.join([t0, '(single-block PWL) : ']) ppc = loadcase(casefile) ppc['gencost'][2, NCOST] = 2 r = runopf(ppc, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) xr = r_[r['var']['val']['Va'], r['var']['val']['Vm'], r['var']['val']['Pg'], r['var']['val']['Qg'], 0, r['var']['val']['y']] t_is(r['x'], xr, 8, [t, 'check on raw x returned from OPF']) ## get solved AC power flow case from MAT-file soln9_opf_Plim = loadmat(join(tdir, 'soln9_opf_Plim.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_Plim['bus_soln'] gen_soln = soln9_opf_Plim['gen_soln'] branch_soln = soln9_opf_Plim['branch_soln'] f_soln = soln9_opf_Plim['f_soln'][0] ## run OPF with active power line limits t = ''.join([t0, '(P line lim) : ']) ppopt1 = ppoption(ppopt, OPF_FLOW_LIM=1) r = runopf(casefile, ppopt1) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ##----- test OPF with quadratic gen costs moved to generalized costs ----- ppc = loadcase(casefile) ppc['gencost'] = array([ [2, 1500, 0, 3, 0.11, 5, 0], [2, 2000, 0, 3, 0.085, 1.2, 0], [2, 3000, 0, 3, 0.1225, 1, 0] ]) r = runopf(ppc, ppopt) bus_soln, gen_soln, branch_soln, f_soln, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] branch_soln = branch_soln[:, :MU_ST + 1] A = None l = array([]) u = array([]) nb = ppc['bus'].shape[0] # number of buses ng = ppc['gen'].shape[0] # number of gens thbas = 0; thend = thbas + nb vbas = thend; vend = vbas + nb pgbas = vend; pgend = pgbas + ng # qgbas = pgend; qgend = qgbas + ng nxyz = 2 * nb + 2 * ng N = sparse((ppc['baseMVA'] * ones(ng), (arange(ng), arange(pgbas, pgend))), (ng, nxyz)) fparm = ones((ng, 1)) * array([[1, 0, 0, 1]]) ix = argsort(ppc['gen'][:, 0]) H = 2 * spdiags(ppc['gencost'][ix, 4], 0, ng, ng, 'csr') Cw = ppc['gencost'][ix, 5] ppc['gencost'][:, 4:7] = 0 ## run OPF with quadratic gen costs moved to generalized costs t = ''.join([t0, 'w/quadratic generalized gen cost : ']) r = opf(ppc, A, l, u, ppopt, N, fparm, H, Cw) f, bus, gen, branch, success = \ r['f'], r['bus'], r['gen'], r['branch'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_is(r['cost']['usr'], f, 12, [t, 'user cost']) ##----- run OPF with extra linear user constraints & costs ----- ## single new z variable constrained to be greater than or equal to ## deviation from 1 pu voltage at bus 1, linear cost on this z ## get solved AC power flow case from MAT-file soln9_opf_extras1 = loadmat(join(tdir, 'soln9_opf_extras1.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_extras1['bus_soln'] gen_soln = soln9_opf_extras1['gen_soln'] branch_soln = soln9_opf_extras1['branch_soln'] f_soln = soln9_opf_extras1['f_soln'][0] row = [0, 0, 1, 1] col = [9, 24, 9, 24] A = sparse(([-1, 1, 1, 1], (row, col)), (2, 25)) u = array([Inf, Inf]) l = array([-1, 1]) N = sparse(([1], ([0], [24])), (1, 25)) ## new z variable only fparm = array([[1, 0, 0, 1]]) ## w = r = z H = sparse((1, 1)) ## no quadratic term Cw = array([100.0]) t = ''.join([t0, 'w/extra constraints & costs 1 : ']) r = opf(casefile, A, l, u, ppopt, N, fparm, H, Cw) f, bus, gen, branch, success = \ r['f'], r['bus'], r['gen'], r['branch'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_is(r['var']['val']['z'], 0.025419, 6, [t, 'user variable']) t_is(r['cost']['usr'], 2.5419, 4, [t, 'user cost']) ##----- test OPF with capability curves ----- ppc = loadcase(join(tdir, 't_case9_opfv2')) ## remove angle diff limits ppc['branch'][0, ANGMAX] = 360 ppc['branch'][8, ANGMIN] = -360 ## get solved AC power flow case from MAT-file soln9_opf_PQcap = loadmat(join(tdir, 'soln9_opf_PQcap.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_PQcap['bus_soln'] gen_soln = soln9_opf_PQcap['gen_soln'] branch_soln = soln9_opf_PQcap['branch_soln'] f_soln = soln9_opf_PQcap['f_soln'][0] ## run OPF with capability curves t = ''.join([t0, 'w/capability curves : ']) r = runopf(ppc, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ##----- test OPF with angle difference limits ----- ppc = loadcase(join(tdir, 't_case9_opfv2')) ## remove capability curves ppc['gen'][ix_(arange(1, 3), [PC1, PC2, QC1MIN, QC1MAX, QC2MIN, QC2MAX])] = zeros((2, 6)) ## get solved AC power flow case from MAT-file soln9_opf_ang = loadmat(join(tdir, 'soln9_opf_ang.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_ang['bus_soln'] gen_soln = soln9_opf_ang['gen_soln'] branch_soln = soln9_opf_ang['branch_soln'] f_soln = soln9_opf_ang['f_soln'][0] ## run OPF with angle difference limits t = ''.join([t0, 'w/angle difference limits : ']) r = runopf(ppc, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 1, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_is(branch[:, ibr_angmu ], branch_soln[:, ibr_angmu ], 2, [t, 'branch angle mu']) ##----- test OPF with ignored angle difference limits ----- ## get solved AC power flow case from MAT-file soln9_opf = loadmat(join(tdir, 'soln9_opf.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf['bus_soln'] gen_soln = soln9_opf['gen_soln'] branch_soln = soln9_opf['branch_soln'] f_soln = soln9_opf['f_soln'][0] ## run OPF with ignored angle difference limits t = ''.join([t0, 'w/ignored angle difference limits : ']) ppopt1 = ppoption(ppopt, OPF_IGNORE_ANG_LIM=1) r = runopf(ppc, ppopt1) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] ## ang limits are not in this solution data, so let's remove them branch[0, ANGMAX] = 360 branch[8, ANGMIN] = -360 t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_end()
def t_jacobian(quiet=False): """Numerical tests of partial derivative code. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(28, quiet) ## run powerflow to get solved case ppopt = ppoption(VERBOSE=0, OUT_ALL=0) ppc = loadcase(case30()) results, _ = runpf(ppc, ppopt) baseMVA, bus, gen, branch = \ results['baseMVA'], results['bus'], results['gen'], results['branch'] ## switch to internal bus numbering and build admittance matrices _, bus, gen, branch = ext2int1(bus, gen, branch) Ybus, Yf, Yt = makeYbus(baseMVA, bus, branch) Ybus_full = Ybus.todense() Yf_full = Yf.todense() Yt_full = Yt.todense() Vm = bus[:, VM] Va = bus[:, VA] * (pi / 180) V = Vm * exp(1j * Va) f = branch[:, F_BUS].astype(int) ## list of "from" buses t = branch[:, T_BUS].astype(int) ## list of "to" buses #nl = len(f) nb = len(V) pert = 1e-8 Vm = array([Vm]).T # column array Va = array([Va]).T # column array Vc = array([V]).T # column array ##----- check dSbus_dV code ----- ## full matrices dSbus_dVm_full, dSbus_dVa_full = dSbus_dV(Ybus_full, V) ## sparse matrices dSbus_dVm, dSbus_dVa = dSbus_dV(Ybus, V) dSbus_dVm_sp = dSbus_dVm.todense() dSbus_dVa_sp = dSbus_dVa.todense() ## compute numerically to compare Vmp = (Vm * ones((1, nb)) + pert*eye(nb)) * (exp(1j * Va) * ones((1, nb))) Vap = (Vm * ones((1, nb))) * (exp(1j * (Va*ones((1, nb)) + pert*eye(nb)))) num_dSbus_dVm = (Vmp * conj(Ybus * Vmp) - Vc * ones((1, nb)) * conj(Ybus * Vc * ones((1, nb)))) / pert num_dSbus_dVa = (Vap * conj(Ybus * Vap) - Vc * ones((1, nb)) * conj(Ybus * Vc * ones((1, nb)))) / pert t_is(dSbus_dVm_sp, num_dSbus_dVm, 5, 'dSbus_dVm (sparse)') t_is(dSbus_dVa_sp, num_dSbus_dVa, 5, 'dSbus_dVa (sparse)') t_is(dSbus_dVm_full, num_dSbus_dVm, 5, 'dSbus_dVm (full)') t_is(dSbus_dVa_full, num_dSbus_dVa, 5, 'dSbus_dVa (full)') ##----- check dSbr_dV code ----- ## full matrices dSf_dVa_full, dSf_dVm_full, dSt_dVa_full, dSt_dVm_full, _, _ = \ dSbr_dV(branch, Yf_full, Yt_full, V) ## sparse matrices dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, Sf, St = dSbr_dV(branch, Yf, Yt, V) dSf_dVa_sp = dSf_dVa.todense() dSf_dVm_sp = dSf_dVm.todense() dSt_dVa_sp = dSt_dVa.todense() dSt_dVm_sp = dSt_dVm.todense() ## compute numerically to compare Vmpf = Vmp[f, :] Vapf = Vap[f, :] Vmpt = Vmp[t, :] Vapt = Vap[t, :] Sf2 = (Vc[f] * ones((1, nb))) * conj(Yf * Vc * ones((1, nb))) St2 = (Vc[t] * ones((1, nb))) * conj(Yt * Vc * ones((1, nb))) Smpf = Vmpf * conj(Yf * Vmp) Sapf = Vapf * conj(Yf * Vap) Smpt = Vmpt * conj(Yt * Vmp) Sapt = Vapt * conj(Yt * Vap) num_dSf_dVm = (Smpf - Sf2) / pert num_dSf_dVa = (Sapf - Sf2) / pert num_dSt_dVm = (Smpt - St2) / pert num_dSt_dVa = (Sapt - St2) / pert t_is(dSf_dVm_sp, num_dSf_dVm, 5, 'dSf_dVm (sparse)') t_is(dSf_dVa_sp, num_dSf_dVa, 5, 'dSf_dVa (sparse)') t_is(dSt_dVm_sp, num_dSt_dVm, 5, 'dSt_dVm (sparse)') t_is(dSt_dVa_sp, num_dSt_dVa, 5, 'dSt_dVa (sparse)') t_is(dSf_dVm_full, num_dSf_dVm, 5, 'dSf_dVm (full)') t_is(dSf_dVa_full, num_dSf_dVa, 5, 'dSf_dVa (full)') t_is(dSt_dVm_full, num_dSt_dVm, 5, 'dSt_dVm (full)') t_is(dSt_dVa_full, num_dSt_dVa, 5, 'dSt_dVa (full)') ##----- check dAbr_dV code ----- ## full matrices dAf_dVa_full, dAf_dVm_full, dAt_dVa_full, dAt_dVm_full = \ dAbr_dV(dSf_dVa_full, dSf_dVm_full, dSt_dVa_full, dSt_dVm_full, Sf, St) ## sparse matrices dAf_dVa, dAf_dVm, dAt_dVa, dAt_dVm = \ dAbr_dV(dSf_dVa, dSf_dVm, dSt_dVa, dSt_dVm, Sf, St) dAf_dVa_sp = dAf_dVa.todense() dAf_dVm_sp = dAf_dVm.todense() dAt_dVa_sp = dAt_dVa.todense() dAt_dVm_sp = dAt_dVm.todense() ## compute numerically to compare num_dAf_dVm = (abs(Smpf)**2 - abs(Sf2)**2) / pert num_dAf_dVa = (abs(Sapf)**2 - abs(Sf2)**2) / pert num_dAt_dVm = (abs(Smpt)**2 - abs(St2)**2) / pert num_dAt_dVa = (abs(Sapt)**2 - abs(St2)**2) / pert t_is(dAf_dVm_sp, num_dAf_dVm, 4, 'dAf_dVm (sparse)') t_is(dAf_dVa_sp, num_dAf_dVa, 4, 'dAf_dVa (sparse)') t_is(dAt_dVm_sp, num_dAt_dVm, 4, 'dAt_dVm (sparse)') t_is(dAt_dVa_sp, num_dAt_dVa, 4, 'dAt_dVa (sparse)') t_is(dAf_dVm_full, num_dAf_dVm, 4, 'dAf_dVm (full)') t_is(dAf_dVa_full, num_dAf_dVa, 4, 'dAf_dVa (full)') t_is(dAt_dVm_full, num_dAt_dVm, 4, 'dAt_dVm (full)') t_is(dAt_dVa_full, num_dAt_dVa, 4, 'dAt_dVa (full)') ##----- check dIbr_dV code ----- ## full matrices dIf_dVa_full, dIf_dVm_full, dIt_dVa_full, dIt_dVm_full, _, _ = \ dIbr_dV(branch, Yf_full, Yt_full, V) ## sparse matrices dIf_dVa, dIf_dVm, dIt_dVa, dIt_dVm, _, _ = dIbr_dV(branch, Yf, Yt, V) dIf_dVa_sp = dIf_dVa.todense() dIf_dVm_sp = dIf_dVm.todense() dIt_dVa_sp = dIt_dVa.todense() dIt_dVm_sp = dIt_dVm.todense() ## compute numerically to compare num_dIf_dVm = (Yf * Vmp - Yf * Vc * ones((1, nb))) / pert num_dIf_dVa = (Yf * Vap - Yf * Vc * ones((1, nb))) / pert num_dIt_dVm = (Yt * Vmp - Yt * Vc * ones((1, nb))) / pert num_dIt_dVa = (Yt * Vap - Yt * Vc * ones((1, nb))) / pert t_is(dIf_dVm_sp, num_dIf_dVm, 5, 'dIf_dVm (sparse)') t_is(dIf_dVa_sp, num_dIf_dVa, 5, 'dIf_dVa (sparse)') t_is(dIt_dVm_sp, num_dIt_dVm, 5, 'dIt_dVm (sparse)') t_is(dIt_dVa_sp, num_dIt_dVa, 5, 'dIt_dVa (sparse)') t_is(dIf_dVm_full, num_dIf_dVm, 5, 'dIf_dVm (full)') t_is(dIf_dVa_full, num_dIf_dVa, 5, 'dIf_dVa (full)') t_is(dIt_dVm_full, num_dIt_dVm, 5, 'dIt_dVm (full)') t_is(dIt_dVa_full, num_dIt_dVa, 5, 'dIt_dVa (full)') t_end()
def t_scale_load(quiet=False): """Tests for code in C{scale_load}. @author: Ray Zimmerman (PSERC Cornell) """ n_tests = 275 t_begin(n_tests, quiet) ppc = loadcase(join(dirname(__file__), 't_auction_case')) ppc['gen'][7, GEN_BUS] = 2 ## multiple d. loads per area, same bus as gen ppc['gen'][7, [QG, QMIN, QMAX]] = array([3, 0, 3]) ## put it load before gen in matrix ppc['gen'] = vstack( [ppc['gen'][7, :], ppc['gen'][:7, :], ppc['gen'][8, :]]) ld = find(isload(ppc['gen'])) a = [None] * 3 lda = [None] * 3 for k in range(3): a[k] = find(ppc['bus'][:, BUS_AREA] == k + 1) ## buses in area k tmp = find(in1d(ppc['gen'][ld, GEN_BUS] - 1, a[k])) lda[k] = ld[tmp] ## disp loads in area k area = [None] * 3 for k in range(3): area[k] = {'fixed': {}, 'disp': {}, 'both': {}} area[k]['fixed']['p'] = sum(ppc['bus'][a[k], PD]) area[k]['fixed']['q'] = sum(ppc['bus'][a[k], QD]) area[k]['disp']['p'] = -sum(ppc['gen'][lda[k], PMIN]) area[k]['disp']['qmin'] = -sum(ppc['gen'][lda[k], QMIN]) area[k]['disp']['qmax'] = -sum(ppc['gen'][lda[k], QMAX]) area[k]['disp'][ 'q'] = area[k]['disp']['qmin'] + area[k]['disp']['qmax'] area[k]['both']['p'] = area[k]['fixed']['p'] + area[k]['disp']['p'] area[k]['both']['q'] = area[k]['fixed']['q'] + area[k]['disp']['q'] total = {'fixed': {}, 'disp': {}, 'both': {}} total['fixed']['p'] = sum(ppc['bus'][:, PD]) total['fixed']['q'] = sum(ppc['bus'][:, QD]) total['disp']['p'] = -sum(ppc['gen'][ld, PMIN]) total['disp']['qmin'] = -sum(ppc['gen'][ld, QMIN]) total['disp']['qmax'] = -sum(ppc['gen'][ld, QMAX]) total['disp']['q'] = total['disp']['qmin'] + total['disp']['qmax'] total['both']['p'] = total['fixed']['p'] + total['disp']['p'] total['both']['q'] = total['fixed']['q'] + total['disp']['q'] ##----- single load zone, one scale factor ----- load = array([2]) t = 'all fixed loads (PQ) * 2 : ' bus, _ = scale_load(load, ppc['bus']) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all fixed loads (P) * 2 : ' opt = {'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (PQ) * 2 : ' bus, gen = scale_load(load, ppc['bus'], ppc['gen']) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), load * total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), load * total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (P) * 2 : ' opt = {'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (PQ) * 2 : ' opt = {'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), load * total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), load * total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (P) * 2 : ' opt = {'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) ##----- single load zone, one scale quantity ----- load = array([200.0]) t = 'all fixed loads (PQ) => total = 200 : ' opt = {'scale': 'QUANTITY'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) t_is(sum(bus[:, PD]), load, 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load / total['fixed']['p'] * total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'scale': 'QUANTITY', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load - total['disp']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), (load - total['disp']['p']) / total['fixed']['p'] * total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all fixed loads (P) => total = 200 : ' opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) t_is(sum(bus[:, PD]), load, 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load - total['disp']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (PQ) => total = 200 : ' opt = {'scale': 'QUANTITY'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load / total['both']['p'] * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load / total['both']['p'] * total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load / total['both']['p'] * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), load / total['both']['p'] * total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), load / total['both']['p'] * total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (P) => total = 200 : ' opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load / total['both']['p'] * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load / total['both']['p'] * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (PQ) => total = 200 : ' opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load - total['fixed']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), (load - total['fixed']['p']) / total['disp']['p'] * total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), (load - total['fixed']['p']) / total['disp']['p'] * total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (P) => total = 200 : ' opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load - total['fixed']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) ##----- 3 zones, area scale factors ----- t = 'area fixed loads (PQ) * [3 2 1] : ' load = array([3, 2, 1]) bus, _ = scale_load(load, ppc['bus']) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) opt = {'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area fixed loads (P) * [3 2 1] : ' load = array([3, 2, 1]) opt = {'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) opt = {'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'all area loads (PQ) * [3 2 1] : ' bus, gen = scale_load(load, ppc['bus'], ppc['gen']) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), load[k] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), load[k] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'all area loads (P) * [3 2 1] : ' opt = {'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area disp loads (PQ) * [3 2 1] : ' opt = {'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), load[k] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), load[k] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area disp loads (P) * [3 2 1] : ' opt = {'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) ##----- 3 zones, area scale quantities ----- t = 'area fixed loads (PQ) => total = [100 80 60] : ' load = array([100, 80, 60], float) opt = {'scale': 'QUANTITY'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k], 8, '%s area %d fixed P' % (t, k)) 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)) opt = {'scale': 'QUANTITY', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] - area[k]['disp']['p'], 8, '%s area %d fixed P' % (t, k)) 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)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area fixed loads (P) => total = [100 80 60] : ' load = array([100, 80, 60], float) opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] - area[k]['disp']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'all area loads (PQ) => total = [100 80 60] : ' opt = {'scale': 'QUANTITY'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): 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)) 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)) 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)) 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)) 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)) t = 'all area loads (P) => total = [100 80 60] : ' opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): 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)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 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)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area disp loads (PQ) => total = [100 80 60] : throws expected exception' load = array([100, 80, 60], float) opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} err = 0 try: bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) except ScalingError as e: expected = 'scale_load: impossible to make zone 2 load equal 80 by scaling non-existent dispatchable load' err = expected not in str(e) t_ok(err, t) t = 'area disp loads (PQ) => total = [100 74.3941 60] : ' load = array([100, area[1]['fixed']['p'], 60], float) opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] - area[k]['fixed']['p'], 8, '%s area %d disp P' % (t, k)) if k == 1: t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) else: 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)) 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)) t = 'area disp loads (P) => total = [100 74.3941 60] : ' opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] - area[k]['fixed']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) ##----- explict single load zone ----- t = 'explicit single load zone' load_zone = zeros(ppc['bus'].shape[0]) load_zone[[2, 3]] = 1 load = array([2.0]) bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) Pd = ppc['bus'][:, PD] Pd[[2, 3]] = load * Pd[[2, 3]] t_is(bus[:, PD], Pd, 8, t) ##----- explict multiple load zone ----- t = 'explicit multiple load zone' load_zone = zeros(ppc['bus'].shape[0]) load_zone[[2, 3]] = 1 load_zone[[6, 7]] = 2 load = array([2, 0.5]) bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) Pd = ppc['bus'][:, PD] Pd[[2, 3]] = load[0] * Pd[[2, 3]] Pd[[6, 7]] = load[1] * Pd[[6, 7]] t_is(bus[:, PD], Pd, 8, t) t_end()
def t_total_load(quiet=False): """Tests for code in C{total_load}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 48 t_begin(n_tests, quiet) ppc = loadcase(join(dirname(__file__), 't_auction_case')) ppc['gen'][7, GEN_BUS] = 2 ## multiple d. loads per area, same bus as gen ppc['gen'][7, [QG, QMIN, QMAX]] = array([3, 0, 3]) ## put it load before gen in matrix ppc['gen'] = vstack( [ppc['gen'][7, :], ppc['gen'][:7, :], ppc['gen'][8, :]]) ld = find(isload(ppc['gen'])) a = [None] * 3 lda = [None] * 3 for k in range(3): a[k] = find(ppc['bus'][:, BUS_AREA] == k + 1) ## buses in area k tmp = find(in1d(ppc['gen'][ld, GEN_BUS] - 1, a[k])) lda[k] = ld[tmp] ## disp loads in area k area = [None] * 3 for k in range(3): area[k] = {'fixed': {}, 'disp': {}, 'both': {}} area[k]['fixed']['p'] = sum(ppc['bus'][a[k], PD]) area[k]['fixed']['q'] = sum(ppc['bus'][a[k], QD]) area[k]['disp']['p'] = -sum(ppc['gen'][lda[k], PMIN]) area[k]['disp']['qmin'] = -sum(ppc['gen'][lda[k], QMIN]) area[k]['disp']['qmax'] = -sum(ppc['gen'][lda[k], QMAX]) area[k]['disp'][ 'q'] = area[k]['disp']['qmin'] + area[k]['disp']['qmax'] area[k]['both']['p'] = area[k]['fixed']['p'] + area[k]['disp']['p'] area[k]['both']['q'] = area[k]['fixed']['q'] + area[k]['disp']['q'] total = {'fixed': {}, 'disp': {}, 'both': {}} total['fixed']['p'] = sum(ppc['bus'][:, PD]) total['fixed']['q'] = sum(ppc['bus'][:, QD]) total['disp']['p'] = -sum(ppc['gen'][ld, PMIN]) total['disp']['qmin'] = -sum(ppc['gen'][ld, QMIN]) total['disp']['qmax'] = -sum(ppc['gen'][ld, QMAX]) total['disp']['q'] = total['disp']['qmin'] + total['disp']['qmax'] total['both']['p'] = total['fixed']['p'] + total['disp']['p'] total['both']['q'] = total['fixed']['q'] + total['disp']['q'] ##----- all load ----- t = 'Pd, _ = total_load(bus) : ' Pd, _ = total_load(ppc['bus']) t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus) : ' Pd, Qd = total_load(ppc['bus']) t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['fixed']['q'], area[1]['fixed']['q'], area[2]['fixed']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen) : ' Pd, _ = total_load(ppc['bus'], ppc['gen']) t_is(Pd, [area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen) : ' Pd, Qd = total_load(ppc['bus'], ppc['gen']) t_is(Pd, [area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['both']['q'], area[1]['both']['q'], area[2]['both']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, None, \'all\') : ' Pd, _ = total_load(ppc['bus'], None, 'all') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, None, \'all\') : ' Pd, Qd = total_load(ppc['bus'], None, 'all') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t_is(Qd, total['fixed']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t_is(Qd, total['both']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\', \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all', 'BOTH') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\', \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all', 'BOTH') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t_is(Qd, total['both']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\', \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all', 'FIXED') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\', \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all', 'FIXED') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t_is(Qd, total['fixed']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\', \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all', 'DISPATCHABLE') t_is(Pd, total['disp']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\', \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all', 'DISPATCHABLE') t_is(Pd, total['disp']['p'], 12, [t, 'Pd']) t_is(Qd, total['disp']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, None, \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], None, 'BOTH') t_is(Pd, r_[area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, None, \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], None, 'BOTH') t_is(Pd, [area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['both']['q'], area[1]['both']['q'], area[2]['both']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, None, \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], None, 'FIXED') t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, None, \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], None, 'FIXED') t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['fixed']['q'], area[1]['fixed']['q'], area[2]['fixed']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, None, \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], None, 'DISPATCHABLE') t_is(Pd, [area[0]['disp']['p'], area[1]['disp']['p'], area[2]['disp']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, None, \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], None, 'DISPATCHABLE') t_is(Pd, [area[0]['disp']['p'], area[1]['disp']['p'], area[2]['disp']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['disp']['q'], area[1]['disp']['q'], area[2]['disp']['q']], 12, [t, 'Qd']) ##----- explicit single load zone ----- nb = ppc['bus'].shape[0] load_zone = zeros(nb, int) k = find(ppc['bus'][:, BUS_AREA] == 2) ## area 2 load_zone[k] = 1 t = 'Pd, _ = total_load(bus, gen, load_zone1, \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, area[1]['both']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone1, \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, area[1]['both']['p'], 12, [t, 'Pd']) t_is(Qd, area[1]['both']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone1, \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, area[1]['fixed']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone1, \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, area[1]['fixed']['p'], 12, [t, 'Pd']) t_is(Qd, area[1]['fixed']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone1, \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, area[1]['disp']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone1, \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, area[1]['disp']['p'], 12, [t, 'Pd']) t_is(Qd, area[1]['disp']['q'], 12, [t, 'Qd']) ##----- explicit multiple load zone ----- load_zone = zeros(nb, int) k = find(ppc['bus'][:, BUS_AREA] == 3) ## area 3 load_zone[k] = 1 k = find(ppc['bus'][:, BUS_AREA] == 1) ## area 1 load_zone[k] = 2 t = 'Pd, _ = total_load(bus, gen, load_zone2, \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, [area[2]['both']['p'], area[0]['both']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone2, \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, [area[2]['both']['p'], area[0]['both']['p']], 12, [t, 'Pd']) t_is(Qd, [area[2]['both']['q'], area[0]['both']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone2, \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, [area[2]['fixed']['p'], area[0]['fixed']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone2, \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, [area[2]['fixed']['p'], area[0]['fixed']['p']], 12, [t, 'Pd']) t_is(Qd, [area[2]['fixed']['q'], area[0]['fixed']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone2, \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, [area[2]['disp']['p'], area[0]['disp']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone2, \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, [area[2]['disp']['p'], area[0]['disp']['p']], 12, [t, 'Pd']) t_is(Qd, [area[2]['disp']['q'], area[0]['disp']['q']], 12, [t, 'Qd']) t_end()
def t_pf(quiet=False): """Tests for power flow solvers. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(33, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_pf') verbose = not quiet ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0) ## get solved AC power flow case from MAT-file ## defines bus_soln, gen_soln, branch_soln soln9_pf = loadmat(join(tdir, 'soln9_pf.mat'), struct_as_record=False) bus_soln = soln9_pf['bus_soln'] gen_soln = soln9_pf['gen_soln'] branch_soln = soln9_pf['branch_soln'] ## run Newton PF t = 'Newton PF : ' ppopt = ppoption(ppopt, PF_ALG=1) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## run fast-decoupled PF (XB version) t = 'Fast Decoupled (XB) PF : ' ppopt = ppoption(ppopt, PF_ALG=2) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## run fast-decoupled PF (BX version) t = 'Fast Decoupled (BX) PF : ' ppopt = ppoption(ppopt, PF_ALG=3) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## run Gauss-Seidel PF t = 'Gauss-Seidel PF : ' ppopt = ppoption(ppopt, PF_ALG=4) results, success = runpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 5, [t, 'bus']) t_is(gen, gen_soln, 5, [t, 'gen']) t_is(branch, branch_soln, 5, [t, 'branch']) ## get solved AC power flow case from MAT-file ## defines bus_soln, gen_soln, branch_soln soln9_dcpf = loadmat(join(tdir, 'soln9_dcpf.mat'), struct_as_record=False) bus_soln = soln9_dcpf['bus_soln'] gen_soln = soln9_dcpf['gen_soln'] branch_soln = soln9_dcpf['branch_soln'] ## run DC PF t = 'DC PF : ' results, success = rundcpf(casefile, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_ok(success, [t, 'success']) t_is(bus, bus_soln, 6, [t, 'bus']) t_is(gen, gen_soln, 6, [t, 'gen']) t_is(branch, branch_soln, 6, [t, 'branch']) ## check Qg distribution, when Qmin = Qmax t = 'check Qg : ' ppopt = ppoption(ppopt, PF_ALG=1, VERBOSE=0) ppc = loadcase(casefile) ppc['gen'][0, [QMIN, QMAX]] = [20, 20] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0, QG], 24.07, 2, [t, 'single gen, Qmin = Qmax']) ppc['gen'] = r_[array([ppc['gen'][0, :]]), ppc['gen']] ppc['gen'][0, [QMIN, QMAX]] = [10, 10] ppc['gen'][1, [QMIN, QMAX]] = [0, 50] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [10, 14.07], 2, [t, '2 gens, Qmin = Qmax for one']) ppc['gen'][0, [QMIN, QMAX]] = [10, 10] ppc['gen'][1, [QMIN, QMAX]] = [-50, -50] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [12.03, 12.03], 2, [t, '2 gens, Qmin = Qmax for both']) ppc['gen'][0, [QMIN, QMAX]] = [0, 50] ppc['gen'][1, [QMIN, QMAX]] = [0, 100] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [8.02, 16.05], 2, [t, '2 gens, proportional']) ppc['gen'][0, [QMIN, QMAX]] = [-50, 0] ppc['gen'][1, [QMIN, QMAX]] = [50, 150] results, success = runpf(ppc, ppopt) bus, gen, branch = results['bus'], results['gen'], results['branch'] t_is(gen[0:2, QG], [-50 + 8.02, 50 + 16.05], 2, [t, '2 gens, proportional']) ## network with islands t = 'network w/islands : DC PF : ' ppc0 = loadcase(casefile) ppc0['gen'][0, PG] = 60 ppc0['gen'][0, [PMIN, PMAX, QMIN, QMAX, PG, QG]] = \ ppc0['gen'][0, [PMIN, PMAX, QMIN, QMAX, PG, QG]] / 2 ppc0['gen'] = r_[array([ppc0['gen'][0, :]]), ppc0['gen']] ppc1 = ppc0.copy() ppc = ppc0.copy() nb = ppc['bus'].shape[0] ppc1['bus'][:, BUS_I] = ppc1['bus'][:, BUS_I] + nb ppc1['branch'][:, F_BUS] = ppc1['branch'][:, F_BUS] + nb ppc1['branch'][:, T_BUS] = ppc1['branch'][:, T_BUS] + nb ppc1['gen'][:, GEN_BUS] = ppc1['gen'][:, GEN_BUS] + nb ppc['bus'] = r_[ppc['bus'], ppc1['bus']] ppc['branch'] = r_[ppc['branch'], ppc1['branch']] ppc['gen'] = r_[ppc['gen'], ppc1['gen']] #ppopt = ppoption(ppopt, OUT_BUS=1, OUT_GEN=1, OUT_ALL=-1, VERBOSE=2) ppopt = ppoption(ppopt, VERBOSE=verbose) r = rundcpf(ppc, ppopt) t_is(r['bus'][:9, VA], bus_soln[:, VA], 8, [t, 'voltage angles 1']) t_is(r['bus'][10:18, VA], bus_soln[:, VA], 8, [t, 'voltage angles 2']) Pg = r_[gen_soln[0, PG] - 30, 30, gen_soln[1:3, PG]] t_is(r['gen'][:4, PG], Pg, 8, [t, 'active power generation 1']) t_is(r['gen'][4:8, PG], Pg, 8, [t, 'active power generation 1']) t = 'network w/islands : AC PF : ' ## get solved AC power flow case from MAT-file soln9_pf = loadmat(join(tdir, 'soln9_pf.mat'), struct_as_record=False) bus_soln = soln9_pf['bus_soln'] gen_soln = soln9_pf['gen_soln'] branch_soln = soln9_pf['branch_soln'] r = runpf(ppc, ppopt) t_is(r['bus'][:9, VA], bus_soln[:, VA], 8, [t, 'voltage angles 1']) t_is(r['bus'][9:18, VA], bus_soln[:, VA], 8, [t, 'voltage angles 2']) Pg = r_[gen_soln[0, PG] - 30, 30, gen_soln[1:3, PG]] t_is(r['gen'][:4, PG], Pg, 8, [t, 'active power generation 1']) t_is(r['gen'][4:8, PG], Pg, 8, [t, 'active power generation 1']) t_end()
def t_total_load(quiet=False): """Tests for code in C{total_load}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 48 t_begin(n_tests, quiet) ppc = loadcase(join(dirname(__file__), 't_auction_case')) ppc['gen'][7, GEN_BUS] = 2 ## multiple d. loads per area, same bus as gen ppc['gen'][7, [QG, QMIN, QMAX]] = array([3, 0, 3]) ## put it load before gen in matrix ppc['gen'] = vstack([ppc['gen'][7, :], ppc['gen'][:7, :], ppc['gen'][8, :]]) ld = find(isload(ppc['gen'])) a = [None] * 3 lda = [None] * 3 for k in range(3): a[k] = find(ppc['bus'][:, BUS_AREA] == k + 1) ## buses in area k tmp = find( in1d(ppc['gen'][ld, GEN_BUS] - 1, a[k]) ) lda[k] = ld[tmp] ## disp loads in area k area = [None] * 3 for k in range(3): area[k] = {'fixed': {}, 'disp': {}, 'both': {}} area[k]['fixed']['p'] = sum(ppc['bus'][a[k], PD]) area[k]['fixed']['q'] = sum(ppc['bus'][a[k], QD]) area[k]['disp']['p'] = -sum(ppc['gen'][lda[k], PMIN]) area[k]['disp']['qmin'] = -sum(ppc['gen'][lda[k], QMIN]) area[k]['disp']['qmax'] = -sum(ppc['gen'][lda[k], QMAX]) area[k]['disp']['q'] = area[k]['disp']['qmin'] + area[k]['disp']['qmax'] area[k]['both']['p'] = area[k]['fixed']['p'] + area[k]['disp']['p'] area[k]['both']['q'] = area[k]['fixed']['q'] + area[k]['disp']['q'] total = {'fixed': {}, 'disp': {}, 'both': {}} total['fixed']['p'] = sum(ppc['bus'][:, PD]) total['fixed']['q'] = sum(ppc['bus'][:, QD]) total['disp']['p'] = -sum(ppc['gen'][ld, PMIN]) total['disp']['qmin'] = -sum(ppc['gen'][ld, QMIN]) total['disp']['qmax'] = -sum(ppc['gen'][ld, QMAX]) total['disp']['q'] = total['disp']['qmin'] + total['disp']['qmax'] total['both']['p'] = total['fixed']['p'] + total['disp']['p'] total['both']['q'] = total['fixed']['q'] + total['disp']['q'] ##----- all load ----- t = 'Pd, _ = total_load(bus) : ' Pd, _ = total_load(ppc['bus']) t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus) : ' Pd, Qd = total_load(ppc['bus']) t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['fixed']['q'], area[1]['fixed']['q'], area[2]['fixed']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen) : ' Pd, _ = total_load(ppc['bus'], ppc['gen']) t_is(Pd, [area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen) : ' Pd, Qd = total_load(ppc['bus'], ppc['gen']) t_is(Pd, [area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['both']['q'], area[1]['both']['q'], area[2]['both']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, None, \'all\') : ' Pd, _ = total_load(ppc['bus'], None, 'all') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, None, \'all\') : ' Pd, Qd = total_load(ppc['bus'], None, 'all') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t_is(Qd, total['fixed']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t_is(Qd, total['both']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\', \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all', 'BOTH') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\', \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all', 'BOTH') t_is(Pd, total['both']['p'], 12, [t, 'Pd']) t_is(Qd, total['both']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\', \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all', 'FIXED') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\', \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all', 'FIXED') t_is(Pd, total['fixed']['p'], 12, [t, 'Pd']) t_is(Qd, total['fixed']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, \'all\', \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], 'all', 'DISPATCHABLE') t_is(Pd, total['disp']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, \'all\', \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], 'all', 'DISPATCHABLE') t_is(Pd, total['disp']['p'], 12, [t, 'Pd']) t_is(Qd, total['disp']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, None, \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], None, 'BOTH') t_is(Pd, r_[area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, None, \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], None, 'BOTH') t_is(Pd, [area[0]['both']['p'], area[1]['both']['p'], area[2]['both']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['both']['q'], area[1]['both']['q'], area[2]['both']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, None, \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], None, 'FIXED') t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, None, \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], None, 'FIXED') t_is(Pd, [area[0]['fixed']['p'], area[1]['fixed']['p'], area[2]['fixed']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['fixed']['q'], area[1]['fixed']['q'], area[2]['fixed']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, None, \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], None, 'DISPATCHABLE') t_is(Pd, [area[0]['disp']['p'], area[1]['disp']['p'], area[2]['disp']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, None, \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], None, 'DISPATCHABLE') t_is(Pd, [area[0]['disp']['p'], area[1]['disp']['p'], area[2]['disp']['p']], 12, [t, 'Pd']) t_is(Qd, [area[0]['disp']['q'], area[1]['disp']['q'], area[2]['disp']['q']], 12, [t, 'Qd']) ##----- explicit single load zone ----- nb = ppc['bus'].shape[0] load_zone = zeros(nb, int) k = find(ppc['bus'][:, BUS_AREA] == 2) ## area 2 load_zone[k] = 1 t = 'Pd, _ = total_load(bus, gen, load_zone1, \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, area[1]['both']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone1, \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, area[1]['both']['p'], 12, [t, 'Pd']) t_is(Qd, area[1]['both']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone1, \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, area[1]['fixed']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone1, \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, area[1]['fixed']['p'], 12, [t, 'Pd']) t_is(Qd, area[1]['fixed']['q'], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone1, \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, area[1]['disp']['p'], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone1, \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, area[1]['disp']['p'], 12, [t, 'Pd']) t_is(Qd, area[1]['disp']['q'], 12, [t, 'Qd']) ##----- explicit multiple load zone ----- load_zone = zeros(nb, int) k = find(ppc['bus'][:, BUS_AREA] == 3) ## area 3 load_zone[k] = 1 k = find(ppc['bus'][:, BUS_AREA] == 1) ## area 1 load_zone[k] = 2 t = 'Pd, _ = total_load(bus, gen, load_zone2, \'BOTH\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, [area[2]['both']['p'], area[0]['both']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone2, \'BOTH\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'BOTH') t_is(Pd, [area[2]['both']['p'], area[0]['both']['p']], 12, [t, 'Pd']) t_is(Qd, [area[2]['both']['q'], area[0]['both']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone2, \'FIXED\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, [area[2]['fixed']['p'], area[0]['fixed']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone2, \'FIXED\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'FIXED') t_is(Pd, [area[2]['fixed']['p'], area[0]['fixed']['p']], 12, [t, 'Pd']) t_is(Qd, [area[2]['fixed']['q'], area[0]['fixed']['q']], 12, [t, 'Qd']) t = 'Pd, _ = total_load(bus, gen, load_zone2, \'DISPATCHABLE\') : ' Pd, _ = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, [area[2]['disp']['p'], area[0]['disp']['p']], 12, [t, 'Pd']) t = 'Pd, Qd = total_load(bus, gen, load_zone2, \'DISPATCHABLE\') : ' Pd, Qd = total_load(ppc['bus'], ppc['gen'], load_zone, 'DISPATCHABLE') t_is(Pd, [area[2]['disp']['p'], area[0]['disp']['p']], 12, [t, 'Pd']) t_is(Qd, [area[2]['disp']['q'], area[0]['disp']['q']], 12, [t, 'Qd']) t_end()
def t_opf_dc_pips_sc(quiet=False): """Tests for DC optimal power flow using PIPS-sc solver. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ num_tests = 23 t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') verbose = 0 #not quiet t0 = 'DC OPF (PIPS-sc): ' ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0, OPF_ALG_DC=250) ## run DC OPF ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) #ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## get solved DC power flow case from MAT-file soln9_dcopf = loadmat(join(tdir, 'soln9_dcopf.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_dcopf['bus_soln'] gen_soln = soln9_dcopf['gen_soln'] branch_soln = soln9_dcopf['branch_soln'] f_soln = soln9_dcopf['f_soln'][0] ## run OPF t = t0 r = rundcopf(casefile, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is(bus[:, ib_data], bus_soln[:, ib_data], 10, [t, 'bus data']) t_is(bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is(bus[:, ib_lam], bus_soln[:, ib_lam], 3, [t, 'bus lambda']) t_is(bus[:, ib_mu], bus_soln[:, ib_mu], 2, [t, 'bus mu']) t_is(gen[:, ig_data], gen_soln[:, ig_data], 10, [t, 'gen data']) t_is(gen[:, ig_disp], gen_soln[:, ig_disp], 3, [t, 'gen dispatch']) t_is(gen[:, ig_mu], gen_soln[:, ig_mu], 3, [t, 'gen mu']) t_is(branch[:, ibr_data], branch_soln[:, ibr_data], 10, [t, 'branch data']) t_is(branch[:, ibr_flow], branch_soln[:, ibr_flow], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu], branch_soln[:, ibr_mu], 2, [t, 'branch mu']) ##----- run OPF with extra linear user constraints & costs ----- ## two new z variables ## 0 <= z1, P2 - P1 <= z1 ## 0 <= z2, P2 - P3 <= z2 ## with A and N sized for DC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [9, 10, 12, 10, 11, 13] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 14)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [12, 13])), (2, 14)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 1 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) ## with A and N sized for AC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [18, 19, 24, 19, 20, 25] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 26)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [24, 25])), (2, 26)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 2 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) t = ''.join([t0, 'infeasible : ']) ## with A and N sized for DC opf ppc = loadcase(casefile) ppc['A'] = sparse(([1, 1], ([0, 0], [9, 10])), (1, 14)) ## Pg1 + Pg2 ppc['u'] = array([Inf]) ppc['l'] = array([600]) r = rundcopf(ppc, ppopt) t_ok(not r['success'], [t, 'no success']) t_end()
def t_ext2int2ext(quiet=False): """Tests C{ext2int} and C{int2ext}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(85, quiet) ##----- ppc = e2i_data/i2e_data(ppc) ----- t = 'ppc = e2i_data(ppc) : ' ppce = loadcase(t_case_ext()) ppci = loadcase(t_case_int()) ppc = e2i_data(ppce) t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost']) t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas']) t_is(ppc['A'], ppci['A'], 12, [t, 'A']) t_is(ppc['N'], ppci['N'], 12, [t, 'N']) t = 'ppc = e2i_data(ppc) - repeat : ' ppc = e2i_data(ppc) t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost']) t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas']) t_is(ppc['A'], ppci['A'], 12, [t, 'A']) t_is(ppc['N'], ppci['N'], 12, [t, 'N']) t = 'ppc = i2e_data(ppc) : ' ppc = i2e_data(ppc) t_is(ppc['bus'], ppce['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppce['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppce['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppce['gencost'], 12, [t, 'gencost']) t_is(ppc['areas'], ppce['areas'], 12, [t, 'areas']) t_is(ppc['A'], ppce['A'], 12, [t, 'A']) t_is(ppc['N'], ppce['N'], 12, [t, 'N']) ##----- val = e2i_data/i2e_data(ppc, val, ...) ----- t = 'val = e2i_data(ppc, val, \'bus\')' ppc = e2i_data(ppce) got = e2i_data(ppc, ppce['xbus'], 'bus') ex = ppce['xbus'] ex = delete(ex, 5, 0) t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, \'bus\')' tmp = ones(ppce['xbus'].shape) tmp[5, :] = ppce['xbus'][5, :] got = i2e_data(ppc, ex, tmp, 'bus') t_is(got, ppce['xbus'], 12, t) t = 'val = e2i_data(ppc, val, \'bus\', 1)' got = e2i_data(ppc, ppce['xbus'], 'bus', 1) ex = ppce['xbus'] ex = delete(ex, 5, 1) t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, \'bus\', 1)' tmp = ones(ppce['xbus'].shape) tmp[:, 5] = ppce['xbus'][:, 5] got = i2e_data(ppc, ex, tmp, 'bus', 1) t_is(got, ppce['xbus'], 12, t) t = 'val = e2i_data(ppc, val, \'gen\')' got = e2i_data(ppc, ppce['xgen'], 'gen') ex = ppce['xgen'][[3, 1, 0], :] t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, \'gen\')' tmp = ones(ppce['xgen'].shape) tmp[2, :] = ppce['xgen'][2, :] got = i2e_data(ppc, ex, tmp, 'gen') t_is(got, ppce['xgen'], 12, t) t = 'val = e2i_data(ppc, val, \'gen\', 1)' got = e2i_data(ppc, ppce['xgen'], 'gen', 1) ex = ppce['xgen'][:, [3, 1, 0]] t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, \'gen\', 1)' tmp = ones(ppce['xgen'].shape) tmp[:, 2] = ppce['xgen'][:, 2] got = i2e_data(ppc, ex, tmp, 'gen', 1) t_is(got, ppce['xgen'], 12, t) t = 'val = e2i_data(ppc, val, \'branch\')' got = e2i_data(ppc, ppce['xbranch'], 'branch') ex = ppce['xbranch'] ex = delete(ex, 6, 0) t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, \'branch\')' tmp = ones(ppce['xbranch'].shape) tmp[6, :] = ppce['xbranch'][6, :] got = i2e_data(ppc, ex, tmp, 'branch') t_is(got, ppce['xbranch'], 12, t) t = 'val = e2i_data(ppc, val, \'branch\', 1)' got = e2i_data(ppc, ppce['xbranch'], 'branch', 1) ex = ppce['xbranch'] ex = delete(ex, 6, 1) t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, \'branch\', 1)' tmp = ones(ppce['xbranch'].shape) tmp[:, 6] = ppce['xbranch'][:, 6] got = i2e_data(ppc, ex, tmp, 'branch', 1) t_is(got, ppce['xbranch'], 12, t) t = 'val = e2i_data(ppc, val, {\'branch\', \'gen\', \'bus\'})' got = e2i_data(ppc, ppce['xrows'], ['branch', 'gen', 'bus']) ex = r_[ppce['xbranch'][list(range(6)) + list(range(7, 10)), :4], ppce['xgen'][[3, 1, 0], :], ppce['xbus'][list(range(5)) + list(range(6, 10)), :4], -1 * ones((2, 4))] t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, {\'branch\', \'gen\', \'bus\'})' tmp1 = ones(ppce['xbranch'][:, :4].shape) tmp1[6, :4] = ppce['xbranch'][6, :4] tmp2 = ones(ppce['xgen'].shape) tmp2[2, :] = ppce['xgen'][2, :] tmp3 = ones(ppce['xbus'][:, :4].shape) tmp3[5, :4] = ppce['xbus'][5, :4] tmp = r_[tmp1, tmp2, tmp3] got = i2e_data(ppc, ex, tmp, ['branch', 'gen', 'bus']) t_is(got, ppce['xrows'], 12, t) t = 'val = e2i_data(ppc, val, {\'branch\', \'gen\', \'bus\'}, 1)' got = e2i_data(ppc, ppce['xcols'], ['branch', 'gen', 'bus'], 1) ex = r_[ppce['xbranch'][list(range(6)) + list(range(7, 10)), :4], ppce['xgen'][[3, 1, 0], :], ppce['xbus'][list(range(5)) + list(range(6, 10)), :4], -1 * ones((2, 4))].T t_is(got, ex, 12, t) t = 'val = i2e_data(ppc, val, oldval, {\'branch\', \'gen\', \'bus\'}, 1)' tmp1 = ones(ppce['xbranch'][:, :4].shape) tmp1[6, :4] = ppce['xbranch'][6, :4] tmp2 = ones(ppce['xgen'].shape) tmp2[2, :] = ppce['xgen'][2, :] tmp3 = ones(ppce['xbus'][:, :4].shape) tmp3[5, :4] = ppce['xbus'][5, :4] tmp = r_[tmp1, tmp2, tmp3].T got = i2e_data(ppc, ex, tmp, ['branch', 'gen', 'bus'], 1) t_is(got, ppce['xcols'], 12, t) ##----- ppc = e2i_field/i2e_field(ppc, field, ...) ----- t = 'ppc = e2i_field(ppc, field, \'bus\')' ppc = e2i_field(ppce) ex = ppce['xbus'] ex = delete(ex, 5, 0) got = e2i_field(ppc, 'xbus', 'bus') t_is(got['xbus'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, \'bus\')' got = i2e_field(got, 'xbus', ordering='bus') t_is(got['xbus'], ppce['xbus'], 12, t) t = 'ppc = e2i_field(ppc, field, \'bus\', 1)' ex = ppce['xbus'] ex = delete(ex, 5, 1) got = e2i_field(ppc, 'xbus', 'bus', 1) t_is(got['xbus'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, \'bus\', 1)' got = i2e_field(got, 'xbus', ordering='bus', dim=1) t_is(got['xbus'], ppce['xbus'], 12, t) t = 'ppc = e2i_field(ppc, field, \'gen\')' ex = ppce['xgen'][[3, 1, 0], :] got = e2i_field(ppc, 'xgen', 'gen') t_is(got['xgen'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, \'gen\')' got = i2e_field(got, 'xgen', ordering='gen') t_is(got['xgen'], ppce['xgen'], 12, t) t = 'ppc = e2i_field(ppc, field, \'gen\', 1)' ex = ppce['xgen'][:, [3, 1, 0]] got = e2i_field(ppc, 'xgen', 'gen', 1) t_is(got['xgen'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, \'gen\', 1)' got = i2e_field(got, 'xgen', ordering='gen', dim=1) t_is(got['xgen'], ppce['xgen'], 12, t) t = 'ppc = e2i_field(ppc, field, \'branch\')' ex = ppce['xbranch'] ex = delete(ex, 6, 0) got = e2i_field(ppc, 'xbranch', 'branch') t_is(got['xbranch'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, \'branch\')' got = i2e_field(got, 'xbranch', ordering='branch') t_is(got['xbranch'], ppce['xbranch'], 12, t) t = 'ppc = e2i_field(ppc, field, \'branch\', 1)' ex = ppce['xbranch'] ex = delete(ex, 6, 1) got = e2i_field(ppc, 'xbranch', 'branch', 1) t_is(got['xbranch'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, \'branch\', 1)' got = i2e_field(got, 'xbranch', ordering='branch', dim=1) t_is(got['xbranch'], ppce['xbranch'], 12, t) t = 'ppc = e2i_field(ppc, field, {\'branch\', \'gen\', \'bus\'})' ex = r_[ppce['xbranch'][list(range(6)) + list(range(7, 10)), :4], ppce['xgen'][[3, 1, 0], :], ppce['xbus'][list(range(5)) + list(range(6, 10)), :4], -1 * ones((2, 4))] got = e2i_field(ppc, 'xrows', ['branch', 'gen', 'bus']) t_is(got['xrows'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, {\'branch\', \'gen\', \'bus\'})' got = i2e_field(got, 'xrows', ordering=['branch', 'gen', 'bus']) t_is(got['xrows'], ppce['xrows'], 12, t) t = 'ppc = e2i_field(ppc, field, {\'branch\', \'gen\', \'bus\'}, 1)' ex = r_[ppce['xbranch'][list(range(6)) + list(range(7, 10)), :4], ppce['xgen'][[3, 1, 0], :], ppce['xbus'][list(range(5)) + list(range(6, 10)), :4], -1 * ones((2, 4))].T got = e2i_field(ppc, 'xcols', ['branch', 'gen', 'bus'], 1) t_is(got['xcols'], ex, 12, t) t = 'ppc = i2e_field(ppc, field, {\'branch\', \'gen\', \'bus\'})' got = i2e_field(got, 'xcols', ordering=['branch', 'gen', 'bus'], dim=1) t_is(got['xcols'], ppce['xcols'], 12, t) t = 'ppc = e2i_field(ppc, {\'field1\', \'field2\'}, ordering)' ex = ppce['x']['more'][[3, 1, 0], :] got = e2i_field(ppc, ['x', 'more'], 'gen') t_is(got['x']['more'], ex, 12, t) t = 'ppc = i2e_field(ppc, {\'field1\', \'field2\'}, ordering)' got = i2e_field(got, ['x', 'more'], ordering='gen') t_is(got['x']['more'], ppce['x']['more'], 12, t) t = 'ppc = e2i_field(ppc, {\'field1\', \'field2\'}, ordering, 1)' ex = ppce['x']['more'][:, [3, 1, 0]] got = e2i_field(ppc, ['x', 'more'], 'gen', 1) t_is(got['x']['more'], ex, 12, t) t = 'ppc = i2e_field(ppc, {\'field1\', \'field2\'}, ordering, 1)' got = i2e_field(got, ['x', 'more'], ordering='gen', dim=1) t_is(got['x']['more'], ppce['x']['more'], 12, t) ##----- more ppc = ext2int/int2ext(ppc) ----- t = 'ppc = ext2int(ppc) - bus/gen/branch only : ' ppce = loadcase(t_case_ext()) ppci = loadcase(t_case_int()) del ppce['gencost'] del ppce['areas'] del ppce['A'] del ppce['N'] del ppci['gencost'] del ppci['areas'] del ppci['A'] del ppci['N'] ppc = ext2int(ppce) t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen']) t = 'ppc = ext2int(ppc) - no areas/A : ' ppce = loadcase(t_case_ext()) ppci = loadcase(t_case_int()) del ppce['areas'] del ppce['A'] del ppci['areas'] del ppci['A'] ppc = ext2int(ppce) t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost']) t_is(ppc['N'], ppci['N'], 12, [t, 'N']) t = 'ppc = ext2int(ppc) - Qg cost, no N : ' ppce = loadcase(t_case_ext()) ppci = loadcase(t_case_int()) del ppce['N'] del ppci['N'] ppce['gencost'] = c_[ppce['gencost'], ppce['gencost']] ppci['gencost'] = c_[ppci['gencost'], ppci['gencost']] ppc = ext2int(ppce) t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost']) t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas']) t_is(ppc['A'], ppci['A'], 12, [t, 'A']) t = 'ppc = ext2int(ppc) - A, N are DC sized : ' ppce = loadcase(t_case_ext()) ppci = loadcase(t_case_int()) eVmQgcols = list(range(10, 20)) + list(range(24, 28)) iVmQgcols = list(range(9, 18)) + list(range(21, 24)) ppce['A'] = delete(ppce['A'], eVmQgcols, 1) ppce['N'] = delete(ppce['N'], eVmQgcols, 1) ppci['A'] = delete(ppci['A'], iVmQgcols, 1) ppci['N'] = delete(ppci['N'], iVmQgcols, 1) ppc = ext2int(ppce) t_is(ppc['bus'], ppci['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppci['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppci['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppci['gencost'], 12, [t, 'gencost']) t_is(ppc['areas'], ppci['areas'], 12, [t, 'areas']) t_is(ppc['A'], ppci['A'], 12, [t, 'A']) t_is(ppc['N'], ppci['N'], 12, [t, 'N']) t = 'ppc = int2ext(ppc) - A, N are DC sized : ' ppc = int2ext(ppc) t_is(ppc['bus'], ppce['bus'], 12, [t, 'bus']) t_is(ppc['branch'], ppce['branch'], 12, [t, 'branch']) t_is(ppc['gen'], ppce['gen'], 12, [t, 'gen']) t_is(ppc['gencost'], ppce['gencost'], 12, [t, 'gencost']) t_is(ppc['areas'], ppce['areas'], 12, [t, 'areas']) t_is(ppc['A'], ppce['A'], 12, [t, 'A']) t_is(ppc['N'], ppce['N'], 12, [t, 'N']) t_end()
def t_modcost(quiet=False): """Tests for code in C{modcost}. @author: Ray Zimmerman (PSERC Cornell) """ n_tests = 80 t_begin(n_tests, quiet) ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost0 = array([[2, 0, 0, 3, 0.01, 0.1, 1, 0, 0, 0, 0, 0], [2, 0, 0, 5, 0.0006, 0.005, 0.04, 0.3, 2, 0, 0, 0], [1, 0, 0, 4, 0, 0, 10, 200, 20, 600, 30, 1200], [1, 0, 0, 4, -30, -2400, -20, -1800, -10, -1000, 0, 0]]) gencost = modcost(gencost0, 5, 'SCALE_F') ##----- POLYSHIFT ----- t = 'modcost SCALE_F - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0])) / 5, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0])) / 5, [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0])) / 5, [1.24, 2, 0, 0], 8, t) t = 'modcost SCALE_F - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0])) / 5, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0])) / 5, [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0])) / 5, [1, 2.8096, 0, 0], 8, t) t = 'modcost SCALE_F - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0])) / 5, [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0])) / 5, [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0])) / 5, [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0])) / 5, [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0])) / 5, [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0])) / 5, [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0])) / 5, [1, 2, 1500, 0], 8, t) t = 'modcost SCALE_F - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5])) / 5, [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10])) / 5, [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15])) / 5, [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20])) / 5, [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25])) / 5, [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30])) / 5, [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35])) / 5, [1, 2, 0, -2700], 8, t) gencost = modcost(gencost0, 2, 'SCALE_X') t = 'modcost SCALE_X - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0]) * 2), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0]) * 2), [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0]) * 2), [1.24, 2, 0, 0], 8, t) t = 'modcost SCALE_X - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0]) * 2), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0]) * 2), [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0]) * 2), [1, 2.8096, 0, 0], 8, t) t = 'modcost SCALE_X - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0]) * 2), [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0]) * 2), [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0]) * 2), [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0]) * 2), [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0]) * 2), [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0]) * 2), [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0]) * 2), [1, 2, 1500, 0], 8, t) t = 'modcost SCALE_X - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5]) * 2), [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10]) * 2), [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15]) * 2), [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20]) * 2), [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25]) * 2), [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30]) * 2), [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35]) * 2), [1, 2, 0, -2700], 8, t) gencost = modcost(gencost0, 3, 'SHIFT_F') t = 'modcost SHIFT_F - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0])) - 3, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0])) - 3, [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0])) - 3, [1.24, 2, 0, 0], 8, t) t = 'modcost SHIFT_F - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0])) - 3, [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0])) - 3, [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0])) - 3, [1, 2.8096, 0, 0], 8, t) t = 'modcost SHIFT_F - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0])) - 3, [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0])) - 3, [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0])) - 3, [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0])) - 3, [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0])) - 3, [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0])) - 3, [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0])) - 3, [1, 2, 1500, 0], 8, t) t = 'modcost SHIFT_F - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5])) - 3, [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10])) - 3, [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15])) - 3, [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20])) - 3, [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25])) - 3, [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30])) - 3, [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35])) - 3, [1, 2, 0, -2700], 8, t) gencost = modcost(gencost0, -4, 'SHIFT_X') t = 'modcost SHIFT_X - quadratic' t_is(totcost(gencost, array([0, 0, 0, 0]) - 4), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([1, 0, 0, 0]) - 4), [1.11, 2, 0, 0], 8, t) t_is(totcost(gencost, array([2, 0, 0, 0]) - 4), [1.24, 2, 0, 0], 8, t) t = 'modcost SHIFT_X - 4th order polynomial' t_is(totcost(gencost, array([0, 0, 0, 0]) - 4), [1, 2, 0, 0], 8, t) t_is(totcost(gencost, array([0, 1, 0, 0]) - 4), [1, 2.3456, 0, 0], 8, t) t_is(totcost(gencost, array([0, 2, 0, 0]) - 4), [1, 2.8096, 0, 0], 8, t) t = 'modcost SHIFT_X - pwl (gen)' t_is(totcost(gencost, array([0, 0, 5, 0]) - 4), [1, 2, 100, 0], 8, t) t_is(totcost(gencost, array([0, 0, 10, 0]) - 4), [1, 2, 200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 15, 0]) - 4), [1, 2, 400, 0], 8, t) t_is(totcost(gencost, array([0, 0, 20, 0]) - 4), [1, 2, 600, 0], 8, t) t_is(totcost(gencost, array([0, 0, 25, 0]) - 4), [1, 2, 900, 0], 8, t) t_is(totcost(gencost, array([0, 0, 30, 0]) - 4), [1, 2, 1200, 0], 8, t) t_is(totcost(gencost, array([0, 0, 35, 0]) - 4), [1, 2, 1500, 0], 8, t) t = 'modcost SHIFT_X - pwl (load)' t_is(totcost(gencost, array([0, 0, 0, -5]) - 4), [1, 2, 0, -500], 8, t) t_is(totcost(gencost, array([0, 0, 0, -10]) - 4), [1, 2, 0, -1000], 8, t) t_is(totcost(gencost, array([0, 0, 0, -15]) - 4), [1, 2, 0, -1400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -20]) - 4), [1, 2, 0, -1800], 8, t) t_is(totcost(gencost, array([0, 0, 0, -25]) - 4), [1, 2, 0, -2100], 8, t) t_is(totcost(gencost, array([0, 0, 0, -30]) - 4), [1, 2, 0, -2400], 8, t) t_is(totcost(gencost, array([0, 0, 0, -35]) - 4), [1, 2, 0, -2700], 8, t) t_end()
def t_opf_ipopt(quiet=False): """Tests for IPOPT-based AC optimal power flow. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ num_tests = 101 t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') verbose = 0#not quiet t0 = 'IPOPT : ' ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose, OPF_ALG=580) ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## get solved AC power flow case from MAT-file soln9_opf = loadmat(join(tdir, 'soln9_opf.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf['bus_soln'] gen_soln = soln9_opf['gen_soln'] branch_soln = soln9_opf['branch_soln'] f_soln = soln9_opf['f_soln'][0] ## run OPF t = t0 r = runopf(casefile, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ## run with automatic conversion of single-block pwl to linear costs t = ''.join([t0, '(single-block PWL) : ']) ppc = loadcase(casefile) ppc['gencost'][2, NCOST] = 2 r = runopf(ppc, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) xr = r_[r['var']['val']['Va'], r['var']['val']['Vm'], r['var']['val']['Pg'], r['var']['val']['Qg'], 0, r['var']['val']['y']] t_is(r['x'], xr, 8, [t, 'check on raw x returned from OPF']) ## get solved AC power flow case from MAT-file soln9_opf_Plim = loadmat(join(tdir, 'soln9_opf_Plim.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_Plim['bus_soln'] gen_soln = soln9_opf_Plim['gen_soln'] branch_soln = soln9_opf_Plim['branch_soln'] f_soln = soln9_opf_Plim['f_soln'][0] ## run OPF with active power line limits t = ''.join([t0, '(P line lim) : ']) ppopt1 = ppoption(ppopt, OPF_FLOW_LIM=1) r = runopf(casefile, ppopt1) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ##----- test OPF with quadratic gen costs moved to generalized costs ----- ppc = loadcase(casefile) ppc['gencost'] = array([ [2, 1500, 0, 3, 0.11, 5, 0], [2, 2000, 0, 3, 0.085, 1.2, 0], [2, 3000, 0, 3, 0.1225, 1, 0] ]) r = runopf(ppc, ppopt) bus_soln, gen_soln, branch_soln, f_soln, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] branch_soln = branch_soln[:, :MU_ST + 1] A = None l = array([]) u = array([]) nb = ppc['bus'].shape[0] # number of buses ng = ppc['gen'].shape[0] # number of gens thbas = 0; thend = thbas + nb vbas = thend; vend = vbas + nb pgbas = vend; pgend = pgbas + ng # qgbas = pgend; qgend = qgbas + ng nxyz = 2 * nb + 2 * ng N = sparse((ppc['baseMVA'] * ones(ng), (arange(ng), arange(pgbas, pgend))), (ng, nxyz)) fparm = ones((ng, 1)) * array([[1, 0, 0, 1]]) ix = argsort(ppc['gen'][:, 0]) H = 2 * spdiags(ppc['gencost'][ix, 4], 0, ng, ng, 'csr') Cw = ppc['gencost'][ix, 5] ppc['gencost'][:, 4:7] = 0 ## run OPF with quadratic gen costs moved to generalized costs t = ''.join([t0, 'w/quadratic generalized gen cost : ']) r = opf(ppc, A, l, u, ppopt, N, fparm, H, Cw) f, bus, gen, branch, success = \ r['f'], r['bus'], r['gen'], r['branch'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_is(r['cost']['usr'], f, 12, [t, 'user cost']) ##----- run OPF with extra linear user constraints & costs ----- ## single new z variable constrained to be greater than or equal to ## deviation from 1 pu voltage at bus 1, linear cost on this z ## get solved AC power flow case from MAT-file soln9_opf_extras1 = loadmat(join(tdir, 'soln9_opf_extras1.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_extras1['bus_soln'] gen_soln = soln9_opf_extras1['gen_soln'] branch_soln = soln9_opf_extras1['branch_soln'] f_soln = soln9_opf_extras1['f_soln'][0] row = [0, 0, 1, 1] col = [9, 24, 9, 24] A = sparse(([-1, 1, 1, 1], (row, col)), (2, 25)) u = array([Inf, Inf]) l = array([-1, 1]) N = sparse(([1], ([0], [24])), (1, 25)) ## new z variable only fparm = array([[1, 0, 0, 1]]) ## w = r = z H = sparse((1, 1)) ## no quadratic term Cw = array([100.0]) t = ''.join([t0, 'w/extra constraints & costs 1 : ']) r = opf(casefile, A, l, u, ppopt, N, fparm, H, Cw) f, bus, gen, branch, success = \ r['f'], r['bus'], r['gen'], r['branch'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_is(r['var']['val']['z'], 0.025419, 6, [t, 'user variable']) t_is(r['cost']['usr'], 2.5419, 4, [t, 'user cost']) ##----- test OPF with capability curves ----- ppc = loadcase(join(tdir, 't_case9_opfv2')) ## remove angle diff limits ppc['branch'][0, ANGMAX] = 360 ppc['branch'][8, ANGMIN] = -360 ## get solved AC power flow case from MAT-file soln9_opf_PQcap = loadmat(join(tdir, 'soln9_opf_PQcap.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_PQcap['bus_soln'] gen_soln = soln9_opf_PQcap['gen_soln'] branch_soln = soln9_opf_PQcap['branch_soln'] f_soln = soln9_opf_PQcap['f_soln'][0] ## run OPF with capability curves t = ''.join([t0, 'w/capability curves : ']) r = runopf(ppc, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ##----- test OPF with angle difference limits ----- ppc = loadcase(join(tdir, 't_case9_opfv2')) ## remove capability curves ppc['gen'][ix_(arange(1, 3), [PC1, PC2, QC1MIN, QC1MAX, QC2MIN, QC2MAX])] = zeros((2, 6)) ## get solved AC power flow case from MAT-file soln9_opf_ang = loadmat(join(tdir, 'soln9_opf_ang.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf_ang['bus_soln'] gen_soln = soln9_opf_ang['gen_soln'] branch_soln = soln9_opf_ang['branch_soln'] f_soln = soln9_opf_ang['f_soln'][0] ## run OPF with angle difference limits t = ''.join([t0, 'w/angle difference limits : ']) r = runopf(ppc, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 1, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_is(branch[:, ibr_angmu ], branch_soln[:, ibr_angmu ], 2, [t, 'branch angle mu']) ##----- test OPF with ignored angle difference limits ----- ## get solved AC power flow case from MAT-file soln9_opf = loadmat(join(tdir, 'soln9_opf.mat'), struct_as_record=True) ## defines bus_soln, gen_soln, branch_soln, f_soln bus_soln = soln9_opf['bus_soln'] gen_soln = soln9_opf['gen_soln'] branch_soln = soln9_opf['branch_soln'] f_soln = soln9_opf['f_soln'][0] ## run OPF with ignored angle difference limits t = ''.join([t0, 'w/ignored angle difference limits : ']) ppopt1 = ppoption(ppopt, OPF_IGNORE_ANG_LIM=1) r = runopf(ppc, ppopt1) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] ## ang limits are not in this solution data, so let's remove them branch[0, ANGMAX] = 360 branch[8, ANGMIN] = -360 t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) t_end()
def t_scale_load(quiet=False): """Tests for code in C{scale_load}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 275 t_begin(n_tests, quiet) ppc = loadcase(join(dirname(__file__), 't_auction_case')) ppc['gen'][7, GEN_BUS] = 2 ## multiple d. loads per area, same bus as gen ppc['gen'][7, [QG, QMIN, QMAX]] = array([3, 0, 3]) ## put it load before gen in matrix ppc['gen'] = vstack([ppc['gen'][7, :], ppc['gen'][:7, :], ppc['gen'][8, :]]) ld = find(isload(ppc['gen'])) a = [None] * 3 lda = [None] * 3 for k in range(3): a[k] = find(ppc['bus'][:, BUS_AREA] == k + 1) ## buses in area k tmp = find( in1d(ppc['gen'][ld, GEN_BUS] - 1, a[k]) ) lda[k] = ld[tmp] ## disp loads in area k area = [None] * 3 for k in range(3): area[k] = {'fixed': {}, 'disp': {}, 'both': {}} area[k]['fixed']['p'] = sum(ppc['bus'][a[k], PD]) area[k]['fixed']['q'] = sum(ppc['bus'][a[k], QD]) area[k]['disp']['p'] = -sum(ppc['gen'][lda[k], PMIN]) area[k]['disp']['qmin'] = -sum(ppc['gen'][lda[k], QMIN]) area[k]['disp']['qmax'] = -sum(ppc['gen'][lda[k], QMAX]) area[k]['disp']['q'] = area[k]['disp']['qmin'] + area[k]['disp']['qmax'] area[k]['both']['p'] = area[k]['fixed']['p'] + area[k]['disp']['p'] area[k]['both']['q'] = area[k]['fixed']['q'] + area[k]['disp']['q'] total = {'fixed': {}, 'disp': {}, 'both': {}} total['fixed']['p'] = sum(ppc['bus'][:, PD]) total['fixed']['q'] = sum(ppc['bus'][:, QD]) total['disp']['p'] = -sum(ppc['gen'][ld, PMIN]) total['disp']['qmin'] = -sum(ppc['gen'][ld, QMIN]) total['disp']['qmax'] = -sum(ppc['gen'][ld, QMAX]) total['disp']['q'] = total['disp']['qmin'] + total['disp']['qmax'] total['both']['p'] = total['fixed']['p'] + total['disp']['p'] total['both']['q'] = total['fixed']['q'] + total['disp']['q'] ##----- single load zone, one scale factor ----- load = array([2]) t = 'all fixed loads (PQ) * 2 : ' bus, _ = scale_load(load, ppc['bus']) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all fixed loads (P) * 2 : ' opt = {'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (PQ) * 2 : ' bus, gen = scale_load(load, ppc['bus'], ppc['gen']) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load * total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), load * total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), load * total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (P) * 2 : ' opt = {'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load * total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (PQ) * 2 : ' opt = {'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), load * total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), load * total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (P) * 2 : ' opt = {'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load * total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) ##----- single load zone, one scale quantity ----- load = array([200.0]) t = 'all fixed loads (PQ) => total = 200 : ' opt = {'scale': 'QUANTITY'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) t_is(sum(bus[:, PD]), load, 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load / total['fixed']['p'] * total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'scale': 'QUANTITY', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load - total['disp']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), (load - total['disp']['p'])/total['fixed']['p']*total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all fixed loads (P) => total = 200 : ' opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) t_is(sum(bus[:, PD]), load, 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load - total['disp']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (PQ) => total = 200 : ' opt = {'scale': 'QUANTITY'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load / total['both']['p']*total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), load / total['both']['p']*total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load / total['both']['p']*total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), load / total['both']['p']*total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), load / total['both']['p']*total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all loads (P) => total = 200 : ' opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), load / total['both']['p']*total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load / total['both']['p']*total['disp']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (PQ) => total = 200 : ' opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load - total['fixed']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), (load - total['fixed']['p'])/total['disp']['p']*total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), (load - total['fixed']['p'])/total['disp']['p']*total['disp']['qmax'], 8, [t, 'total disp Qmax']) t = 'all disp loads (P) => total = 200 : ' opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) t_is(sum(bus[:, PD]), total['fixed']['p'], 8, [t, 'total fixed P']) t_is(sum(bus[:, QD]), total['fixed']['q'], 8, [t, 'total fixed Q']) t_is(-sum(gen[ld, PMIN]), load - total['fixed']['p'], 8, [t, 'total disp P']) t_is(-sum(gen[ld, QMIN]), total['disp']['qmin'], 8, [t, 'total disp Qmin']) t_is(-sum(gen[ld, QMAX]), total['disp']['qmax'], 8, [t, 'total disp Qmax']) ##----- 3 zones, area scale factors ----- t = 'area fixed loads (PQ) * [3 2 1] : ' load = array([3, 2, 1]) bus, _ = scale_load(load, ppc['bus']) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) opt = {'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area fixed loads (P) * [3 2 1] : ' load = array([3, 2, 1]) opt = {'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) opt = {'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'all area loads (PQ) * [3 2 1] : ' bus, gen = scale_load(load, ppc['bus'], ppc['gen']) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), load[k] * area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), load[k] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), load[k] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'all area loads (P) * [3 2 1] : ' opt = {'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] * area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area disp loads (PQ) * [3 2 1] : ' opt = {'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), load[k] * area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), load[k] * area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area disp loads (P) * [3 2 1] : ' opt = {'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k] * area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) ##----- 3 zones, area scale quantities ----- t = 'area fixed loads (PQ) => total = [100 80 60] : ' load = array([100, 80, 60], float) opt = {'scale': 'QUANTITY'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k], 8, '%s area %d fixed P' % (t, k)) 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)) opt = {'scale': 'QUANTITY', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k] - area[k]['disp']['p'], 8, '%s area %d fixed P' % (t, k)) 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)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area fixed loads (P) => total = [100 80 60] : ' load = array([100, 80, 60], float) opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, _ = scale_load(load, ppc['bus'], None, None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'FIXED'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), load[k]-area[k]['disp']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), area[k]['disp']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'all area loads (PQ) => total = [100 80 60] : ' opt = {'scale': 'QUANTITY'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): 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)) 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)) 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)) 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)) 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)) t = 'all area loads (P) => total = [100 80 60] : ' opt = {'scale': 'QUANTITY', 'pq': 'P'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): 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)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) 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)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) t = 'area disp loads (PQ) => total = [100 80 60] : throws expected exception' load = array([100, 80, 60], float) opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} err = 0 try: bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) except ScalingError as e: expected = 'scale_load: impossible to make zone 2 load equal 80 by scaling non-existent dispatchable load' err = expected not in str(e) t_ok(err, t) t = 'area disp loads (PQ) => total = [100 74.3941 60] : ' load = array([100, area[1]['fixed']['p'], 60], float) opt = {'scale': 'QUANTITY', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k]-area[k]['fixed']['p'], 8, '%s area %d disp P' % (t, k)) if k == 1: t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) else: 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)) 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)) t = 'area disp loads (P) => total = [100 74.3941 60] : ' opt = {'scale': 'QUANTITY', 'pq': 'P', 'which': 'DISPATCHABLE'} bus, gen = scale_load(load, ppc['bus'], ppc['gen'], None, opt) for k in range(len(load)): t_is(sum(bus[a[k], PD]), area[k]['fixed']['p'], 8, '%s area %d fixed P' % (t, k)) t_is(sum(bus[a[k], QD]), area[k]['fixed']['q'], 8, '%s area %d fixed Q' % (t, k)) t_is(-sum(gen[lda[k], PMIN]), load[k]-area[k]['fixed']['p'], 8, '%s area %d disp P' % (t, k)) t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) ##----- explict single load zone ----- t = 'explicit single load zone' load_zone = zeros(ppc['bus'].shape[0]) load_zone[[2, 3]] = 1 load = array([2.0]) bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) Pd = ppc['bus'][:, PD] Pd[[2, 3]] = load * Pd[[2, 3]] t_is( bus[:, PD], Pd, 8, t) ##----- explict multiple load zone ----- t = 'explicit multiple load zone' load_zone = zeros(ppc['bus'].shape[0]) load_zone[[2, 3]] = 1 load_zone[[6, 7]] = 2 load = array([2, 0.5]) bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) Pd = ppc['bus'][:, PD] Pd[[2, 3]] = load[0] * Pd[[2, 3]] Pd[[6, 7]] = load[1] * Pd[[6, 7]] t_is( bus[:, PD], Pd, 8, t) t_end()
def t_makePTDF(quiet=False): """Tests for C{makePTDF}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ ntests = 24 t_begin(ntests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') verbose = 0 #not quiet ## load case ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0) r = rundcopf(casefile, ppopt) baseMVA, bus, gen, branch = r['baseMVA'], r['bus'], r['gen'], r['branch'] _, bus, gen, branch = ext2int1(bus, gen, branch) nb = bus.shape[0] nbr = branch.shape[0] ng = gen.shape[0] ## compute injections and flows Cg = sparse((ones(ng), (gen[:, GEN_BUS], arange(ng))), (nb, ng)) Pg = Cg * gen[:, PG] Pd = bus[:, PD] P = Pg - Pd ig = find(P > 0) il = find(P <= 0) F = branch[:, PF] ## create corresponding slack distribution matrices e1 = zeros((nb, 1)) e1[0] = 1 e4 = zeros((nb, 1)) e4[3] = 1 D1 = eye(nb, nb) - dot(e1, ones((1, nb))) D4 = eye(nb, nb) - dot(e4, ones((1, nb))) Deq = eye(nb, nb) - ones((nb, 1)) / nb * ones((1, nb)) Dg = eye(nb) - matrix(Pd / sum(Pd)).T * ones(nb) Dd = eye(nb) - matrix(Pg / sum(Pg)).T * ones(nb) ## create some PTDF matrices H1 = makePTDF(baseMVA, bus, branch, 0) H4 = makePTDF(baseMVA, bus, branch, 3) Heq = makePTDF(baseMVA, bus, branch, ones(nb)) Hg = makePTDF(baseMVA, bus, branch, Pd) Hd = makePTDF(baseMVA, bus, branch, Pg) ## matrices get properly transformed by slack dist matrices t_is(H1, dot(H1, D1), 8, 'H1 == H1 * D1') t_is(H4, dot(H1, D4), 8, 'H4 == H1 * D4') t_is(Heq, dot(H1, Deq), 8, 'Heq == H1 * Deq') t_is(Hg, dot(H1, Dg), 8, 'Hg == H1 * Dg') t_is(Hd, dot(H1, Dd), 8, 'Hd == H1 * Dd') t_is(H1, dot(Heq, D1), 8, 'H1 == Heq * D1') t_is(H4, dot(Heq, D4), 8, 'H4 == Heq * D4') t_is(Heq, dot(Heq, Deq), 8, 'Heq == Heq * Deq') t_is(Hg, dot(Heq, Dg), 8, 'Hg == Heq * Dg') t_is(Hd, dot(Heq, Dd), 8, 'Hd == Heq * Dd') t_is(H1, dot(Hg, D1), 8, 'H1 == Hg * D1') t_is(H4, dot(Hg, D4), 8, 'H4 == Hg * D4') t_is(Heq, dot(Hg, Deq), 8, 'Heq == Hg * Deq') t_is(Hg, dot(Hg, Dg), 8, 'Hg == Hg * Dg') t_is(Hd, dot(Hg, Dd), 8, 'Hd == Hg * Dd') ## PTDFs can reconstruct flows t_is(F, dot(H1, P), 3, 'Flow == H1 * P') t_is(F, dot(H4, P), 3, 'Flow == H4 * P') t_is(F, dot(Heq, P), 3, 'Flow == Heq * P') t_is(F, dot(Hg, P), 3, 'Flow == Hg * P') t_is(F, dot(Hd, P), 3, 'Flow == Hd * P') ## other t_is(F, dot(Hg, Pg), 3, 'Flow == Hg * Pg') t_is(F, dot(Hd, (-Pd)), 3, 'Flow == Hd * (-Pd)') t_is(zeros(nbr), dot(Hg, (-Pd)), 3, 'zeros == Hg * (-Pd)') t_is(zeros(nbr), dot(Hd, Pg), 3, 'zeros == Hd * Pg') t_end()
def t_opf_dc_gurobi(quiet=False): """Tests for DC optimal power flow using Gurobi solver. """ algs = [0, 1, 2, 3, 4] num_tests = 23 * len(algs) t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') if quiet: verbose = False else: verbose = False ppopt = ppoption('OUT_ALL', 0, 'VERBOSE', verbose); ppopt = ppoption(ppopt, 'OPF_ALG_DC', 700); ## run DC OPF if have_fcn('gurobipy'): for k in range(len(algs)): ppopt = ppoption(ppopt, 'GRB_METHOD', algs[k]) methods = [ 'automatic', 'primal simplex', 'dual simplex', 'barrier', 'concurrent', 'deterministic concurrent', ] t0 = 'DC OPF (Gurobi %s): ' % methods[k] ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) #ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## get solved DC power flow case from MAT-file ## defines bus_soln, gen_soln, branch_soln, f_soln soln9_dcopf = loadmat(join(tdir, 'soln9_dcopf.mat'), struct_as_record=True) bus_soln, gen_soln, branch_soln, f_soln = \ soln9_dcopf['bus_soln'], soln9_dcopf['gen_soln'], \ soln9_dcopf['branch_soln'], soln9_dcopf['f_soln'] ## run OPF t = t0 r = rundcopf(casefile, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is( bus[:, ib_data ], bus_soln[:, ib_data ], 10, [t, 'bus data']) t_is( bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is( bus[:, ib_lam ], bus_soln[:, ib_lam ], 3, [t, 'bus lambda']) t_is( bus[:, ib_mu ], bus_soln[:, ib_mu ], 2, [t, 'bus mu']) t_is( gen[:, ig_data ], gen_soln[:, ig_data ], 10, [t, 'gen data']) t_is( gen[:, ig_disp ], gen_soln[:, ig_disp ], 3, [t, 'gen dispatch']) t_is( gen[:, ig_mu ], gen_soln[:, ig_mu ], 3, [t, 'gen mu']) t_is(branch[:, ibr_data ], branch_soln[:, ibr_data ], 10, [t, 'branch data']) t_is(branch[:, ibr_flow ], branch_soln[:, ibr_flow ], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu ], branch_soln[:, ibr_mu ], 2, [t, 'branch mu']) ##----- run OPF with extra linear user constraints & costs ----- ## two new z variables ## 0 <= z1, P2 - P1 <= z1 ## 0 <= z2, P2 - P3 <= z2 ## with A and N sized for DC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [9, 10, 12, 10, 11, 13] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 14)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [12, 13])), (2, 14)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 1 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) ## with A and N sized for AC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [18, 19, 24, 19, 20, 25] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 26)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [24, 25])), (2, 26)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 2 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) t = ''.join([t0, 'infeasible : ']) ## with A and N sized for DC opf ppc = loadcase(casefile) ppc['A'] = sparse(([1, 1], ([0, 0], [9, 10])), (1, 14)) ## Pg1 + Pg2 ppc['u'] = array([Inf]) ppc['l'] = array([600]) r = rundcopf(ppc, ppopt) t_ok(not r['success'], [t, 'no success']) else: t_skip(num_tests, 'Gurobi not available') t_end()
def t_hasPQcap(quiet=False): """Tests for C{hasPQcap}. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(4, quiet) ## generator data # bus Pg Qg Qmax Qmin Vg mBase status Pmax Pmin Pc1 Pc2 Qc1min Qc1max Qc2min Qc2max ramp_agc ramp_10 ramp_30 ramp_q apf gen = array([[ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, 0, 12, 0, 2, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -15, 12, -15, 2, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 0, -2, 0, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 15, -2, 15, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 12, -2, 2, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, 0, 12, 0, 8, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -15, 12, -15, 8, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 0, -8, 0, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 15, -8, 15, 0, 0, 0, 0, 0 ], [ 1, 10, 0, 10, -10, 1, 100, 1, 10, 2, 0, 20, -12, 12, -8, 8, 0, 0, 0, 0, 0 ]]) t = 'hasPQcap(gen)' t_is(hasPQcap(gen), [0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0], 12, t) t = 'hasPQcap(gen, \'B\')' t_is(hasPQcap(gen, 'B'), [0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0], 12, t) t = 'hasPQcap(gen, \'U\')' t_is(hasPQcap(gen, 'U'), [0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0], 12, t) t = 'hasPQcap(gen, \'L\')' t_is(hasPQcap(gen, 'L'), [0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0], 12, t) t_end()
def t_makePTDF(quiet=False): """Tests for C{makePTDF}. @author: Ray Zimmerman (PSERC Cornell) """ ntests = 24 t_begin(ntests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') verbose = 0#not quiet ## load case ppopt = ppoption(VERBOSE=verbose, OUT_ALL=0) r = rundcopf(casefile, ppopt) baseMVA, bus, gen, branch = r['baseMVA'], r['bus'], r['gen'], r['branch'] _, bus, gen, branch = ext2int1(bus, gen, branch) nb = bus.shape[0] nbr = branch.shape[0] ng = gen.shape[0] ## compute injections and flows Cg = sparse((ones(ng), (gen[:, GEN_BUS], arange(ng))), (nb, ng)) Pg = Cg * gen[:, PG] Pd = bus[:, PD] P = Pg - Pd ig = find(P > 0) il = find(P <= 0) F = branch[:, PF] ## create corresponding slack distribution matrices e1 = zeros((nb, 1)); e1[0] = 1 e4 = zeros((nb, 1)); e4[3] = 1 D1 = eye(nb, nb) - dot(e1, ones((1, nb))) D4 = eye(nb, nb) - dot(e4, ones((1, nb))) Deq = eye(nb, nb) - ones((nb, 1)) / nb * ones((1, nb)) Dg = eye(nb) - matrix( Pd / sum(Pd) ).T * ones(nb) Dd = eye(nb) - matrix( Pg / sum(Pg) ).T * ones(nb) ## create some PTDF matrices H1 = makePTDF(baseMVA, bus, branch, 0) H4 = makePTDF(baseMVA, bus, branch, 3) Heq = makePTDF(baseMVA, bus, branch, ones(nb)) Hg = makePTDF(baseMVA, bus, branch, Pd) Hd = makePTDF(baseMVA, bus, branch, Pg) ## matrices get properly transformed by slack dist matrices t_is(H1, dot(H1, D1), 8, 'H1 == H1 * D1') t_is(H4, dot(H1, D4), 8, 'H4 == H1 * D4') t_is(Heq, dot(H1, Deq), 8, 'Heq == H1 * Deq') t_is(Hg, dot(H1, Dg), 8, 'Hg == H1 * Dg') t_is(Hd, dot(H1, Dd), 8, 'Hd == H1 * Dd') t_is(H1, dot(Heq, D1), 8, 'H1 == Heq * D1') t_is(H4, dot(Heq, D4), 8, 'H4 == Heq * D4') t_is(Heq, dot(Heq, Deq), 8, 'Heq == Heq * Deq') t_is(Hg, dot(Heq, Dg), 8, 'Hg == Heq * Dg') t_is(Hd, dot(Heq, Dd), 8, 'Hd == Heq * Dd') t_is(H1, dot(Hg, D1), 8, 'H1 == Hg * D1') t_is(H4, dot(Hg, D4), 8, 'H4 == Hg * D4') t_is(Heq, dot(Hg, Deq), 8, 'Heq == Hg * Deq') t_is(Hg, dot(Hg, Dg), 8, 'Hg == Hg * Dg') t_is(Hd, dot(Hg, Dd), 8, 'Hd == Hg * Dd') ## PTDFs can reconstruct flows t_is(F, dot(H1, P), 3, 'Flow == H1 * P') t_is(F, dot(H4, P), 3, 'Flow == H4 * P') t_is(F, dot(Heq, P), 3, 'Flow == Heq * P') t_is(F, dot(Hg, P), 3, 'Flow == Hg * P') t_is(F, dot(Hd, P), 3, 'Flow == Hd * P') ## other t_is(F, dot(Hg, Pg), 3, 'Flow == Hg * Pg') t_is(F, dot(Hd, (-Pd)), 3, 'Flow == Hd * (-Pd)') t_is(zeros(nbr), dot(Hg, (-Pd)), 3, 'zeros == Hg * (-Pd)') t_is(zeros(nbr), dot(Hd, Pg), 3, 'zeros == Hd * Pg') t_end()
def t_loadcase(quiet=False): """Test that C{loadcase} works with an object as well as case file. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(240, quiet) ## compare result of loading from M-file file to result of using data matrices tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') matfile = join(tdir, 't_mat9_opf') pfcasefile = join(tdir, 't_case9_pf') pfmatfile = join(tdir, 't_mat9_pf') casefilev2 = join(tdir, 't_case9_opfv2') matfilev2 = join(tdir, 't_mat9_opfv2') pfcasefilev2 = join(tdir, 't_case9_pfv2') pfmatfilev2 = join(tdir, 't_mat9_pfv2') ## read version 1 OPF data matrices baseMVA, bus, gen, branch, areas, gencost = t_case9_opf() ## save as .mat file savemat(matfile + '.mat', {'baseMVA': baseMVA, 'bus': bus, 'gen': gen, 'branch': branch, 'areas': areas, 'gencost': gencost}, oned_as='row') ## read version 2 OPF data matrices ppc = t_case9_opfv2() ## save as .mat file savemat(matfilev2 + '.mat', {'ppc': ppc}, oned_as='column') ## prepare expected matrices for v1 load ## (missing gen cap curve & branch ang diff lims) tmp1 = (ppc['baseMVA'], ppc['bus'].copy(), ppc['gen'].copy(), ppc['branch'].copy(), ppc['areas'].copy(), ppc['gencost'].copy()) tmp2 = (ppc['baseMVA'], ppc['bus'].copy(), ppc['gen'].copy(), ppc['branch'].copy(), ppc['areas'].copy(), ppc['gencost'].copy()) ## remove capability curves, angle difference limits tmp1[2][1:3, [PC1, PC2, QC1MIN, QC1MAX, QC2MIN, QC2MAX]] = zeros((2,6)) tmp1[3][0, ANGMAX] = 360 tmp1[3][8, ANGMIN] = -360 baseMVA, bus, gen, branch, areas, gencost = tmp1 ##----- load OPF data into individual matrices ----- t = 'loadcase(opf_PY_file_v1) without .py extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(casefile, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_PY_file_v1) with .py extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(casefile + '.py', False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_MAT_file_v1) without .mat extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(matfile, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_MAT_file_v1) with .mat extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(matfile + '.mat', False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) ## prepare expected matrices for v2 load baseMVA, bus, gen, branch, areas, gencost = tmp2 t = 'loadcase(opf_PY_file_v2) without .py extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(casefilev2, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_PY_file_v2) with .py extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(casefilev2 + '.py', False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_MAT_file_v2) without .mat extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(matfilev2, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_MAT_file_v2) with .mat extension : ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = \ loadcase(matfilev2 + '.mat', False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t_is(areas1, areas, 12, [t, 'areas']) t_is(gencost1, gencost, 12, [t, 'gencost']) ## prepare expected matrices for v1 load baseMVA, bus, gen, branch, areas, gencost = tmp1 t = 'loadcase(opf_struct_v1) (no version): ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = t_case9_opf() c = {} c['baseMVA'] = baseMVA1 c['bus'] = bus1.copy() c['gen'] = gen1.copy() c['branch'] = branch1.copy() c['areas'] = areas1.copy() c['gencost'] = gencost1.copy() baseMVA2, bus2, gen2, branch2, areas2, gencost2 = loadcase(c, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) t_is(areas2, areas, 12, [t, 'areas']) t_is(gencost2, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_struct_v1) (version=\'1\'): ' c['version'] = '1' baseMVA2, bus2, gen2, branch2, areas2, gencost2 = loadcase(c, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) t_is(areas2, areas, 12, [t, 'areas']) t_is(gencost2, gencost, 12, [t, 'gencost']) ## prepare expected matrices for v2 load baseMVA, bus, gen, branch, areas, gencost = tmp2 t = 'loadcase(opf_struct_v2) (no version): ' c = {} c['baseMVA'] = baseMVA c['bus'] = bus.copy() c['gen'] = gen.copy() c['branch'] = branch.copy() c['areas'] = areas.copy() c['gencost'] = gencost.copy() baseMVA2, bus2, gen2, branch2, areas2, gencost2 = loadcase(c, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) t_is(areas2, areas, 12, [t, 'areas']) t_is(gencost2, gencost, 12, [t, 'gencost']) t = 'loadcase(opf_struct_v2) (version=''2''): ' c = {} c['baseMVA'] = baseMVA c['bus'] = bus.copy() c['gen'] = gen.copy() c['branch'] = branch.copy() c['areas'] = areas.copy() c['gencost'] = gencost.copy() c['version'] = '2' baseMVA2, bus2, gen2, branch2, areas2, gencost2 = loadcase(c, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) t_is(areas2, areas, 12, [t, 'areas']) t_is(gencost2, gencost, 12, [t, 'gencost']) ##----- load OPF data into struct ----- ## prepare expected matrices for v1 load baseMVA, bus, gen, branch, areas, gencost = tmp1 t = 'ppc = loadcase(opf_PY_file_v1) without .py extension : ' ppc1 = loadcase(casefile) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_PY_file_v1) with .py extension : ' ppc1 = loadcase(casefile + '.py') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_MAT_file_v1) without .mat extension : ' ppc1 = loadcase(matfile) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_MAT_file_v1) with .mat extension : ' ppc1 = loadcase(matfile + '.mat') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) ## prepare expected matrices for v2 load baseMVA, bus, gen, branch, areas, gencost = tmp2 t = 'ppc = loadcase(opf_PY_file_v2) without .m extension : ' ppc1 = loadcase(casefilev2) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_PY_file_v2) with .py extension : ' ppc1 = loadcase(casefilev2 + '.py') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_MAT_file_v2) without .mat extension : ' ppc1 = loadcase(matfilev2) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_MAT_file_v2) with .mat extension : ' ppc1 = loadcase(matfilev2 + '.mat') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t_is(ppc1['areas'], areas, 12, [t, 'areas']) t_is(ppc1['gencost'], gencost, 12, [t, 'gencost']) ## prepare expected matrices for v1 load baseMVA, bus, gen, branch, areas, gencost = tmp1 t = 'ppc = loadcase(opf_struct_v1) (no version): ' baseMVA1, bus1, gen1, branch1, areas1, gencost1 = t_case9_opf() c = {} c['baseMVA'] = baseMVA1 c['bus'] = bus1.copy() c['gen'] = gen1.copy() c['branch'] = branch1.copy() c['areas'] = areas1.copy() c['gencost'] = gencost1.copy() ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) t_is(ppc2['areas'], areas, 12, [t, 'areas']) t_is(ppc2['gencost'], gencost, 12, [t, 'gencost']) t = 'ppc = loadcase(opf_struct_v1) (version=''1''): ' c['version'] = '1' ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) t_is(ppc2['areas'], areas, 12, [t, 'areas']) t_is(ppc2['gencost'], gencost, 12, [t, 'gencost']) ## prepare expected matrices for v2 load baseMVA, bus, gen, branch, areas, gencost = tmp2 t = 'ppc = loadcase(opf_struct_v2) (no version): ' c = {} c['baseMVA'] = baseMVA c['bus'] = bus.copy() c['gen'] = gen.copy() c['branch'] = branch.copy() c['areas'] = areas.copy() c['gencost'] = gencost.copy() ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) t_is(ppc2['areas'], areas, 12, [t, 'areas']) t_is(ppc2['gencost'], gencost, 12, [t, 'gencost']) t_ok(ppc2['version'] == '2', [t, 'version']) t = 'ppc = loadcase(opf_struct_v2) (version=''2''): ' c = {} c['baseMVA'] = baseMVA c['bus'] = bus.copy() c['gen'] = gen.copy() c['branch'] = branch.copy() c['areas'] = areas.copy() c['gencost'] = gencost.copy() c['version'] = '2' ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) t_is(ppc2['areas'], areas, 12, [t, 'areas']) t_is(ppc2['gencost'], gencost, 12, [t, 'gencost']) ## read version 1 PF data matrices baseMVA, bus, gen, branch = t_case9_pf() savemat(pfmatfile + '.mat', {'baseMVA': baseMVA, 'bus': bus, 'gen': gen, 'branch': branch}, oned_as='column') ## read version 2 PF data matrices ppc = t_case9_pfv2() tmp = (ppc['baseMVA'], ppc['bus'].copy(), ppc['gen'].copy(), ppc['branch'].copy()) baseMVA, bus, gen, branch = tmp ## save as .mat file savemat(pfmatfilev2 + '.mat', {'ppc': ppc}, oned_as='column') ##----- load PF data into individual matrices ----- t = 'loadcase(pf_PY_file_v1) without .py extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfcasefile, False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_PY_file_v1) with .py extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfcasefile + '.py', False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_MAT_file_v1) without .mat extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfmatfile, False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_MAT_file_v1) with .mat extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfmatfile + '.mat', False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_PY_file_v2) without .py extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfcasefilev2, False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_PY_file_v2) with .py extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfcasefilev2 + '.py', False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_MAT_file_v2) without .mat extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfmatfilev2, False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_MAT_file_v2) with .mat extension : ' baseMVA1, bus1, gen1, branch1 = \ loadcase(pfmatfilev2 + '.mat', False, False, False) t_is(baseMVA1, baseMVA, 12, [t, 'baseMVA']) t_is(bus1, bus, 12, [t, 'bus']) t_is(gen1, gen, 12, [t, 'gen']) t_is(branch1, branch, 12, [t, 'branch']) t = 'loadcase(pf_struct_v1) (no version): ' baseMVA1, bus1, gen1, branch1 = t_case9_pf() c = {} c['baseMVA'] = baseMVA1 c['bus'] = bus1.copy() c['gen'] = gen1.copy() c['branch'] = branch1.copy() baseMVA2, bus2, gen2, branch2 = loadcase(c, False, False, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) t = 'loadcase(pf_struct_v1) (version=''1''): ' c['version'] = '1' baseMVA2, bus2, gen2, branch2 = loadcase(c, False, False, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) t = 'loadcase(pf_struct_v2) : ' c = {} c['baseMVA'] = baseMVA c['bus'] = bus.copy() c['gen'] = gen.copy() c['branch'] = branch.copy() c['version'] = '2' baseMVA2, bus2, gen2, branch2 = loadcase(c, False, False, False) t_is(baseMVA2, baseMVA, 12, [t, 'baseMVA']) t_is(bus2, bus, 12, [t, 'bus']) t_is(gen2, gen, 12, [t, 'gen']) t_is(branch2, branch, 12, [t, 'branch']) ##----- load PF data into struct ----- t = 'ppc = loadcase(pf_PY_file_v1) without .py extension : ' ppc1 = loadcase(pfcasefile) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_PY_file_v1) with .py extension : ' ppc1 = loadcase(pfcasefile + '.py') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_MAT_file_v1) without .mat extension : ' ppc1 = loadcase(pfmatfile) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_MAT_file_v1) with .mat extension : ' ppc1 = loadcase(pfmatfile + '.mat') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_PY_file_v2) without .py extension : ' ppc1 = loadcase(pfcasefilev2) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_PY_file_v2) with .py extension : ' ppc1 = loadcase(pfcasefilev2 + '.py') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_MAT_file_v2) without .mat extension : ' ppc1 = loadcase(pfmatfilev2) t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_MAT_file_v2) with .mat extension : ' ppc1 = loadcase(pfmatfilev2 + '.mat') t_is(ppc1['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc1['bus'], bus, 12, [t, 'bus']) t_is(ppc1['gen'], gen, 12, [t, 'gen']) t_is(ppc1['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_struct_v1) (no version): ' baseMVA1, bus1, gen1, branch1 = t_case9_pf() c = {} c['baseMVA'] = baseMVA1 c['bus'] = bus1.copy() c['gen'] = gen1.copy() c['branch'] = branch1.copy() ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_struct_v1) (version=''1''): ' c['version'] = '1' ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) t = 'ppc = loadcase(pf_struct_v2) : ' c = {} c['baseMVA'] = baseMVA c['bus'] = bus.copy() c['gen'] = gen.copy() c['branch'] = branch.copy() c['version'] = '2' ppc2 = loadcase(c) t_is(ppc2['baseMVA'], baseMVA, 12, [t, 'baseMVA']) t_is(ppc2['bus'], bus, 12, [t, 'bus']) t_is(ppc2['gen'], gen, 12, [t, 'gen']) t_is(ppc2['branch'], branch, 12, [t, 'branch']) ## cleanup os.remove(matfile + '.mat') os.remove(pfmatfile + '.mat') os.remove(matfilev2 + '.mat') os.remove(pfmatfilev2 + '.mat') t = 'runpf(my_PY_file)' ppopt = ppoption(VERBOSE=0, OUT_ALL=0) results3, success = runpf(pfcasefile, ppopt) baseMVA3, bus3, gen3, branch3 = results3['baseMVA'], results3['bus'], \ results3['gen'], results3['branch'] t_ok( success, t ) t = 'runpf(my_object)' results4, success = runpf(c, ppopt) baseMVA4, bus4, gen4, branch4 = results4['baseMVA'], results4['bus'], \ results4['gen'], results4['branch'] t_ok( success, t ) t = 'runpf result comparison : ' t_is(baseMVA3, baseMVA4, 12, [t, 'baseMVA']) t_is(bus3, bus4, 12, [t, 'bus']) t_is(gen3, gen4, 12, [t, 'gen']) t_is(branch3, branch4, 12, [t, 'branch']) t = 'runpf(modified_struct)' c['gen'][2, 1] = c['gen'][2, 1] + 1 ## increase gen 3 output by 1 results5, success = runpf(c, ppopt) gen5 = results5['gen'] t_is(gen5[0, 1], gen4[0, 1] - 1, 1, t) ## slack bus output should decrease by 1 t_end()
def t_opf_userfcns(quiet=False): """Tests for userfcn callbacks (reserves/iflims) w/OPF. Includes high-level tests of reserves and iflims implementations. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(38, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case30_userfcns') verbose = 0 #not quiet ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose, OPF_ALG=560, OPF_ALG_DC=200) #ppopt = ppoption(ppopt, OUT_ALL=-1, VERBOSE=2, OUT_GEN=1) ## run the OPF with fixed reserves t = 'fixed reserves : ' ppc = loadcase(casefile) ppc = toggle_reserves(ppc, 'on') r = runopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['reserves']['R'], [25, 15, 0, 0, 19.3906, 0.6094], 4, [t, 'reserves.R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 4, [t, 'reserves.prc']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 4, [t, 'reserves.mu.Pmax']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 4, [t, 'reserves.mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 4, [t, 'reserves.mu.u']) t_ok('P' not in r['if'], [t, 'no iflims']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t = 'toggle_reserves(ppc, \'off\') : ' ppc = toggle_reserves(ppc, 'off') r = runopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_ok('R' not in r['reserves'], [t, 'no reserves']) t_ok('P' not in r['if'], [t, 'no iflims']) t = 'interface flow lims (DC) : ' ppc = loadcase(casefile) ppc = toggle_iflims(ppc, 'on') r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-15, 20], 4, [t, 'if.P']) t_is(r['if']['mu']['l'], [4.8427, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 13.2573], 4, [t, 'if.mu.u']) t_is(r['branch'][13, PF], 8.244, 3, [t, 'flow in branch 14']) t_ok('R' not in r['reserves'], [t, 'no reserves']) t = 'reserves + interface flow lims (DC) : ' ppc = loadcase(casefile) ppc = toggle_reserves(ppc, 'on') ppc = toggle_iflims(ppc, 'on') r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-15, 20], 4, [t, 'if.P']) t_is(r['if']['mu']['l'], [4.8427, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 38.2573], 4, [t, 'if.mu.u']) t_is(r['reserves']['R'], [25, 15, 0, 0, 16.9, 3.1], 4, [t, 'reserves.R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 4, [t, 'reserves.prc']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 4, [t, 'reserves.mu.Pmax']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 4, [t, 'reserves.mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 4, [t, 'reserves.mu.u']) t_is(r['reserves']['totalcost'], 179.05, 4, [t, 'totalcost']) t = 'interface flow lims (AC) : ' ppc = toggle_reserves(ppc, 'off') r = runopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-9.101, 21.432], 3, [t, 'if.P']) t_is(r['if']['mu']['l'], [0, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 10.198], 3, [t, 'if.mu.u']) t_ok('R' not in r['reserves'], [t, 'no reserves']) t = 'interface flow lims (line out) : ' ppc = loadcase(casefile) ppc = toggle_iflims(ppc, 'on') ppc['branch'][11, BR_STATUS] = 0 ## take out line 6-10 r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-15, 20], 4, [t, 'if.P']) t_is(r['if']['mu']['l'], [4.8427, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 13.2573], 4, [t, 'if.mu.u']) t_is(r['branch'][13, PF], 10.814, 3, [t, 'flow in branch 14']) t_ok('R' not in r['reserves'], [t, 'no reserves']) # r['reserves']['R'] # r['reserves']['prc'] # r['reserves']['mu.Pmax'] # r['reserves']['mu']['l'] # r['reserves']['mu']['u'] # r['reserves']['totalcost'] # # r['if']['P'] # r['if']['mu']['l'] # r['if']['mu']['u'] t_end()
def t_runopf_w_res(quiet=False): """Tests C{runopf_w_res} and the associated callbacks. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(46, quiet) verbose = 0#not quiet tdir = dirname(__file__) casefile = join(tdir, 't_case30_userfcns') ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose, OPF_ALG=560) t = 'runopf_w_res(''t_case30_userfcns'') : ' r = runopf_w_res(casefile, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 19.3906, 0.6094], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 7, [t, 'mu.Pmax']) ppc = loadcase(casefile) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t = 'gen 5 no reserves : '; ppc = loadcase(casefile) ppc['reserves']['zones'][:, 4] = 0 ppc['reserves']['cost'] = delete(ppc['reserves']['cost'], 4) ppc['reserves']['qty'] = delete(ppc['reserves']['qty'], 4) r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 0, 20], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 0, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 6, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 187.5, 4, [t, 'totalcost']) t = 'extra offline gen : '; ppc = loadcase(casefile) idx = list(range(3)) + [4] + list(range(3, 6)) ppc['gen'] = ppc['gen'][idx, :] ppc['gencost'] = ppc['gencost'][idx, :] ppc['reserves']['zones'] = ppc['reserves']['zones'][:, idx] ppc['reserves']['cost'] = ppc['reserves']['cost'][idx] ppc['reserves']['qty'] = ppc['reserves']['qty'][idx] ppc['gen'][3, GEN_STATUS] = 0 r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 0, 19.3906, 0.6094], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 5.5, 2, 5.5, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 0, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0, 0.5, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t = 'both extra & gen 6 no res : '; ppc = loadcase(casefile) idx = list(range(3)) + [4] + list(range(3, 6)) ppc['gen'] = ppc['gen'][idx, :] ppc['gencost'] = ppc['gencost'][idx, :] ppc['reserves']['zones'] = ppc['reserves']['zones'][:, idx] ppc['reserves']['cost'] = ppc['reserves']['cost'][idx] ppc['reserves']['qty'] = ppc['reserves']['qty'][idx] ppc['gen'][3, GEN_STATUS] = 0 ppc['reserves']['zones'][:, 5] = 0 ppc['reserves']['cost'] = delete(ppc['reserves']['cost'], 5) ppc['reserves']['qty'] = delete(ppc['reserves']['qty'], 5) r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 0, 0, 20], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 5.5, 2, 0, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 0, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0, 0], 6, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0, 0, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['qty'], ppc['reserves']['qty'], 12, [t, 'qty']) t_is(r['reserves']['totalcost'], 187.5, 4, [t, 'totalcost']) t = 'no qty (Rmax) : ' ppc = loadcase(casefile) del ppc['reserves']['qty'] r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [39.3826, 0.6174, 0, 0, 19.3818, 0.6182], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 5, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 5, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0.1, 0, 0, 0, 0.5, 0], 5, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['totalcost'], 176.3708, 4, [t, 'totalcost']) t = 'RAMP_10, no qty (Rmax) : '; ppc = loadcase(casefile) del ppc['reserves']['qty'] ppc['gen'][0, RAMP_10] = 25 r = runopf_w_res(ppc, ppopt) t_is(r['reserves']['R'], [25, 15, 0, 0, 19.3906, 0.6094], 4, [t, 'R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 6, [t, 'prc']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 7, [t, 'mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 7, [t, 'mu.u']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 7, [t, 'mu.Pmax']) t_is(r['reserves']['cost'], ppc['reserves']['cost'], 12, [t, 'cost']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t_end()
def t_pips(quiet=False): """Tests of pips NLP solver. @author: Ray Zimmerman (PSERC Cornell) """ t_begin(60, quiet) t = 'unconstrained banana function : ' ## from MATLAB Optimization Toolbox's bandem.m f_fcn = f2 x0 = array([-1.9, 2]) # solution = pips(f_fcn, x0, opt={'verbose': 2}) solution = pips(f_fcn, x0) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1, 1], 13, [t, 'x']) t_is(f, 0, 13, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) t = 'unconstrained 3-d quadratic : ' ## from http://www.akiti.ca/QuadProgEx0Constr.html f_fcn = f3 x0 = array([0, 0, 0], float) # solution = pips(f_fcn, x0, opt={'verbose': 2}) solution = pips(f_fcn, x0) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [3, 5, 7], 13, [t, 'x']) t_is(f, -244, 13, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) t = 'constrained 4-d QP : ' ## from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm f_fcn = f4 x0 = array([1.0, 0.0, 0.0, 1.0]) A = array([[1.0, 1.0, 1.0, 1.0], [0.17, 0.11, 0.10, 0.18]]) l = array([1, 0.10]) u = array([1.0, Inf]) xmin = zeros(4) # solution = pips(f_fcn, x0, A, l, u, xmin, opt={'verbose': 2}) solution = pips(f_fcn, x0, A, l, u, xmin) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, array([0, 2.8, 0.2, 0]) / 3, 6, [t, 'x']) t_is(f, 3.29 / 3, 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) t_is(lam['mu_u'], array([0, 0]), 13, [t, 'lam.mu_u']) t_is(lam['lower'], array([2.24, 0, 0, 1.7667]), 4, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) # H = array([ # [1003.1, 4.3, 6.3, 5.9], # [ 4.3, 2.2, 2.1, 3.9], # [ 6.3, 2.1, 3.5, 4.8], # [ 5.9, 3.9, 4.8, 10.0] # ]) # c = zeros(4) # ## check with quadprog (for dev testing only) # x, f, s, out, lam = quadprog(H,c,-A(2,:), -0.10, A(1,:), 1, xmin) # t_is(s, 1, 13, [t, 'success']) # t_is(x, [0 2.8 0.2 0]/3, 6, [t, 'x']) # t_is(f, 3.29/3, 6, [t, 'f']) # t_is(lam['eqlin'], -6.58/3, 6, [t, 'lam.eqlin']) # t_is(lam.['ineqlin'], 0, 13, [t, 'lam.ineqlin']) # t_is(lam['lower'], [2.24001.7667], 4, [t, 'lam[\'lower\']']) # t_is(lam['upper'], [0000], 13, [t, 'lam[\'upper\']']) t = 'constrained 2-d nonlinear : ' ## from http://en.wikipedia.org/wiki/Nonlinear_programming#2-dimensional_example f_fcn = f5 gh_fcn = gh5 hess_fcn = hess5 x0 = array([1.1, 0.0]) xmin = zeros(2) # xmax = 3 * ones(2, 1) # solution = pips(f_fcn, x0, xmin=xmin, gh_fcn=gh_fcn, hess_fcn=hess_fcn, opt={'verbose': 2}) solution = pips(f_fcn, x0, xmin=xmin, gh_fcn=gh_fcn, hess_fcn=hess_fcn) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1, 1], 6, [t, 'x']) t_is(f, -2, 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['ineqnonlin'], array([0, 0.5]), 6, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) # ## check with fmincon (for dev testing only) # # fmoptions = optimset('Algorithm', 'interior-point') # # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], xmin, [], gh_fcn, fmoptions) # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], [], [], gh_fcn) # t_is(s, 1, 13, [t, 'success']) # t_is(x, [1 1], 4, [t, 'x']) # t_is(f, -2, 6, [t, 'f']) # t_is(lam.ineqnonlin, [00.5], 6, [t, 'lam.ineqnonlin']) t = 'constrained 3-d nonlinear : ' ## from http://en.wikipedia.org/wiki/Nonlinear_programming#3-dimensional_example f_fcn = f6 gh_fcn = gh6 hess_fcn = hess6 x0 = array([1.0, 1.0, 0.0]) # solution = pips(f_fcn, x0, gh_fcn=gh_fcn, hess_fcn=hess_fcn, opt={'verbose': 2, 'comptol': 1e-9}) solution = pips(f_fcn, x0, gh_fcn=gh_fcn, hess_fcn=hess_fcn) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1.58113883, 2.23606798, 1.58113883], 6, [t, 'x']) t_is(f, -5 * sqrt(2), 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['ineqnonlin'], array([0, sqrt(2) / 2]), 7, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) # ## check with fmincon (for dev testing only) # # fmoptions = optimset('Algorithm', 'interior-point') # # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], xmin, [], gh_fcn, fmoptions) # [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], [], [], gh_fcn) # t_is(s, 1, 13, [t, 'success']) # t_is(x, [1.58113883 2.23606798 1.58113883], 4, [t, 'x']) # t_is(f, -5*sqrt(2), 8, [t, 'f']) # t_is(lam.ineqnonlin, [0sqrt(2)/2], 8, [t, 'lam.ineqnonlin']) t = 'constrained 3-d nonlinear (dict) : ' p = {'f_fcn': f_fcn, 'x0': x0, 'gh_fcn': gh_fcn, 'hess_fcn': hess_fcn} solution = pips(p) x, f, s, lam, out = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1.58113883, 2.23606798, 1.58113883], 6, [t, 'x']) t_is(f, -5 * sqrt(2), 6, [t, 'f']) t_is(out['hist'][-1]['compcond'], 0, 6, [t, 'compcond']) t_is(lam['ineqnonlin'], [0, sqrt(2) / 2], 7, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(x.shape), 13, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 13, [t, 'lam[\'upper\']']) t = 'constrained 4-d nonlinear : ' ## Hock & Schittkowski test problem #71 f_fcn = f7 gh_fcn = gh7 hess_fcn = hess7 x0 = array([1.0, 5.0, 5.0, 1.0]) xmin = ones(4) xmax = 5 * xmin # solution = pips(f_fcn, x0, xmin=xmin, xmax=xmax, gh_fcn=gh_fcn, hess_fcn=hess_fcn, opt={'verbose': 2, 'comptol': 1e-9}) solution = pips(f_fcn, x0, xmin=xmin, xmax=xmax, gh_fcn=gh_fcn, hess_fcn=hess_fcn) x, f, s, lam, _ = solution["x"], solution["f"], solution["eflag"], \ solution["lmbda"], solution["output"] t_is(s, 1, 13, [t, 'success']) t_is(x, [1, 4.7429994, 3.8211503, 1.3794082], 6, [t, 'x']) t_is(f, 17.0140173, 6, [t, 'f']) t_is(lam['eqnonlin'], 0.1614686, 5, [t, 'lam.eqnonlin']) t_is(lam['ineqnonlin'], 0.55229366, 5, [t, 'lam.ineqnonlin']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], [1.08787121024, 0, 0, 0], 5, [t, 'lam[\'lower\']']) t_is(lam['upper'], zeros(x.shape), 7, [t, 'lam[\'upper\']']) t_end()
def t_qps_pypower(quiet=False): """Tests of C{qps_pypower} QP solvers. @author: Ray Zimmerman (PSERC Cornell) """ algs = [200, 250, 400, 500, 600, 700] names = ['PIPS', 'sc-PIPS', 'IPOPT', 'CPLEX', 'MOSEK', 'Gurobi'] check = [None, None, 'ipopt', 'cplex', 'mosek', 'gurobipy'] n = 36 t_begin(n * len(algs), quiet) for k in range(len(algs)): if check[k] is not None and not have_fcn(check[k]): t_skip(n, '%s not installed' % names[k]) else: opt = {'verbose': 0, 'alg': algs[k]} if names[k] == 'PIPS' or names[k] == 'sc-PIPS': opt['pips_opt'] = {} opt['pips_opt']['comptol'] = 1e-8 if names[k] == 'CPLEX': # alg = 0 ## default uses barrier method with NaN bug in lower lim multipliers alg = 2 ## use dual simplex ppopt = ppoption(CPLEX_LPMETHOD = alg, CPLEX_QPMETHOD = min([4, alg])) opt['cplex_opt'] = cplex_options([], ppopt) if names[k] == 'MOSEK': # alg = 5 ## use dual simplex ppopt = ppoption() # ppopt = ppoption(ppopt, MOSEK_LP_ALG = alg) ppopt = ppoption(ppopt, MOSEK_GAP_TOL=1e-9) opt['mosek_opt'] = mosek_options([], ppopt) t = '%s - 3-d LP : ' % names[k] ## example from 'doc linprog' c = array([-5, -4, -6], float) A = sparse([[1, -1, 1], [3, 2, 4], [3, 2, 0]], dtype=float) l = None u = array([20, 42, 30], float) xmin = array([0, 0, 0], float) x0 = None x, f, s, _, lam = qps_pypower(None, c, A, l, u, xmin, None, None, opt) t_is(s, 1, 12, [t, 'success']) t_is(x, [0, 15, 3], 6, [t, 'x']) t_is(f, -78, 6, [t, 'f']) t_is(lam['mu_l'], [0, 0, 0], 13, [t, 'lam.mu_l']) t_is(lam['mu_u'], [0, 1.5, 0.5], 9, [t, 'lam.mu_u']) t_is(lam['lower'], [1, 0, 0], 9, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - unconstrained 3-d quadratic : ' % names[k] ## from http://www.akiti.ca/QuadProgEx0Constr.html H = sparse([ [ 5, -2, -1], [-2, 4, 3], [-1, 3, 5] ], dtype=float) c = array([2, -35, -47], float) x0 = array([0, 0, 0], float) x, f, s, _, lam = qps_pypower(H, c, opt=opt) t_is(s, 1, 12, [t, 'success']) t_is(x, [3, 5, 7], 8, [t, 'x']) t_is(f, -249, 13, [t, 'f']) t_ok(len(lam['mu_l']) == 0, [t, 'lam.mu_l']) t_ok(len(lam['mu_u']) == 0, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(shape(x)), 13, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - constrained 2-d QP : ' % names[k] ## example from 'doc quadprog' H = sparse([[ 1, -1], [-1, 2]], dtype=float) c = array([-2, -6], float) A = sparse([[ 1, 1], [-1, 2], [ 2, 1]], dtype=float) l = None u = array([2, 2, 3], float) xmin = array([0, 0]) x0 = None x, f, s, _, lam = qps_pypower(H, c, A, l, u, xmin, None, x0, opt) t_is(s, 1, 12, [t, 'success']) t_is(x, array([2., 4.]) / 3, 7, [t, 'x']) t_is(f, -74. / 9, 6, [t, 'f']) t_is(lam['mu_l'], [0., 0., 0.], 13, [t, 'lam.mu_l']) t_is(lam['mu_u'], array([28., 4., 0.]) / 9, 7, [t, 'lam.mu_u']) t_is(lam['lower'], zeros(shape(x)), 8, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - constrained 4-d QP : ' % names[k] ## from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm H = sparse([[1003.1, 4.3, 6.3, 5.9], [4.3, 2.2, 2.1, 3.9], [6.3, 2.1, 3.5, 4.8], [5.9, 3.9, 4.8, 10.0]]) c = zeros(4) A = sparse([[ 1, 1, 1, 1], [0.17, 0.11, 0.10, 0.18]]) l = array([1, 0.10]) u = array([1, Inf]) xmin = zeros(4) x0 = array([1, 0, 0, 1], float) x, f, s, _, lam = qps_pypower(H, c, A, l, u, xmin, None, x0, opt) t_is(s, 1, 12, [t, 'success']) t_is(x, array([0, 2.8, 0.2, 0]) / 3, 5, [t, 'x']) t_is(f, 3.29 / 3, 6, [t, 'f']) t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) t_is(lam['mu_u'], [0, 0], 13, [t, 'lam.mu_u']) t_is(lam['lower'], [2.24, 0, 0, 1.7667], 4, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - (dict) constrained 4-d QP : ' % names[k] p = {'H': H, 'A': A, 'l': l, 'u': u, 'xmin': xmin, 'x0': x0, 'opt': opt} x, f, s, _, lam = qps_pypower(p) t_is(s, 1, 12, [t, 'success']) t_is(x, array([0, 2.8, 0.2, 0]) / 3, 5, [t, 'x']) t_is(f, 3.29 / 3, 6, [t, 'f']) t_is(lam['mu_l'], array([6.58, 0]) / 3, 6, [t, 'lam.mu_l']) t_is(lam['mu_u'], [0, 0], 13, [t, 'lam.mu_u']) t_is(lam['lower'], [2.24, 0, 0, 1.7667], 4, [t, 'lam.lower']) t_is(lam['upper'], zeros(shape(x)), 13, [t, 'lam.upper']) t = '%s - infeasible LP : ' % names[k] p = {'A': sparse([1, 1]), 'c': array([1, 1]), 'u': array([-1]), 'xmin': array([0, 0]), 'opt': opt} x, f, s, _, lam = qps_pypower(p) t_ok(s <= 0, [t, 'no success']) t_end()
def t_opf_userfcns(quiet=False): """Tests for userfcn callbacks (reserves/iflims) w/OPF. Includes high-level tests of reserves and iflims implementations. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ t_begin(38, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case30_userfcns') verbose = 0#not quiet ppopt = ppoption(OPF_VIOLATION=1e-6, PDIPM_GRADTOL=1e-8, PDIPM_COMPTOL=1e-8, PDIPM_COSTTOL=1e-9) ppopt = ppoption(ppopt, OUT_ALL=0, VERBOSE=verbose, OPF_ALG=560, OPF_ALG_DC=200) #ppopt = ppoption(ppopt, OUT_ALL=-1, VERBOSE=2, OUT_GEN=1) ## run the OPF with fixed reserves t = 'fixed reserves : ' ppc = loadcase(casefile) ppc = toggle_reserves(ppc, 'on') r = runopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['reserves']['R'], [25, 15, 0, 0, 19.3906, 0.6094], 4, [t, 'reserves.R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 4, [t, 'reserves.prc']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 4, [t, 'reserves.mu.Pmax']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 4, [t, 'reserves.mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 4, [t, 'reserves.mu.u']) t_ok('P' not in r['if'], [t, 'no iflims']) t_is(r['reserves']['totalcost'], 177.8047, 4, [t, 'totalcost']) t = 'toggle_reserves(ppc, \'off\') : '; ppc = toggle_reserves(ppc, 'off') r = runopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_ok('R' not in r['reserves'], [t, 'no reserves']) t_ok('P' not in r['if'], [t, 'no iflims']) t = 'interface flow lims (DC) : ' ppc = loadcase(casefile) ppc = toggle_iflims(ppc, 'on') r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-15, 20], 4, [t, 'if.P']) t_is(r['if']['mu']['l'], [4.8427, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 13.2573], 4, [t, 'if.mu.u']) t_is(r['branch'][13, PF], 8.244, 3, [t, 'flow in branch 14']) t_ok('R' not in r['reserves'], [t, 'no reserves']) t = 'reserves + interface flow lims (DC) : ' ppc = loadcase(casefile) ppc = toggle_reserves(ppc, 'on') ppc = toggle_iflims(ppc, 'on') r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-15, 20], 4, [t, 'if.P']) t_is(r['if']['mu']['l'], [4.8427, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 38.2573], 4, [t, 'if.mu.u']) t_is(r['reserves']['R'], [25, 15, 0, 0, 16.9, 3.1], 4, [t, 'reserves.R']) t_is(r['reserves']['prc'], [2, 2, 2, 2, 5.5, 5.5], 4, [t, 'reserves.prc']) t_is(r['reserves']['mu']['Pmax'], [0, 0, 0, 0, 0.5, 0], 4, [t, 'reserves.mu.Pmax']) t_is(r['reserves']['mu']['l'], [0, 0, 1, 2, 0, 0], 4, [t, 'reserves.mu.l']) t_is(r['reserves']['mu']['u'], [0.1, 0, 0, 0, 0, 0], 4, [t, 'reserves.mu.u']) t_is(r['reserves']['totalcost'], 179.05, 4, [t, 'totalcost']) t = 'interface flow lims (AC) : ' ppc = toggle_reserves(ppc, 'off') r = runopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-9.101, 21.432], 3, [t, 'if.P']) t_is(r['if']['mu']['l'], [0, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 10.198], 3, [t, 'if.mu.u']) t_ok('R' not in r['reserves'], [t, 'no reserves']) t = 'interface flow lims (line out) : ' ppc = loadcase(casefile) ppc = toggle_iflims(ppc, 'on') ppc['branch'][11, BR_STATUS] = 0 ## take out line 6-10 r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['if']['P'], [-15, 20], 4, [t, 'if.P']) t_is(r['if']['mu']['l'], [4.8427, 0], 4, [t, 'if.mu.l']) t_is(r['if']['mu']['u'], [0, 13.2573], 4, [t, 'if.mu.u']) t_is(r['branch'][13, PF], 10.814, 3, [t, 'flow in branch 14']) t_ok('R' not in r['reserves'], [t, 'no reserves']) # r['reserves']['R'] # r['reserves']['prc'] # r['reserves']['mu.Pmax'] # r['reserves']['mu']['l'] # r['reserves']['mu']['u'] # r['reserves']['totalcost'] # # r['if']['P'] # r['if']['mu']['l'] # r['if']['mu']['u'] t_end()
def t_opf_dc_gurobi(quiet=False): """Tests for DC optimal power flow using Gurobi solver. """ algs = [0, 1, 2, 3, 4] num_tests = 23 * len(algs) t_begin(num_tests, quiet) tdir = dirname(__file__) casefile = join(tdir, 't_case9_opf') if quiet: verbose = False else: verbose = False ppopt = ppoption('OUT_ALL', 0, 'VERBOSE', verbose) ppopt = ppoption(ppopt, 'OPF_ALG_DC', 700) ## run DC OPF if have_fcn('gurobipy'): for k in range(len(algs)): ppopt = ppoption(ppopt, 'GRB_METHOD', algs[k]) methods = [ 'automatic', 'primal simplex', 'dual simplex', 'barrier', 'concurrent', 'deterministic concurrent', ] t0 = 'DC OPF (Gurobi %s): ' % methods[k] ## set up indices ib_data = r_[arange(BUS_AREA + 1), arange(BASE_KV, VMIN + 1)] ib_voltage = arange(VM, VA + 1) ib_lam = arange(LAM_P, LAM_Q + 1) ib_mu = arange(MU_VMAX, MU_VMIN + 1) ig_data = r_[[GEN_BUS, QMAX, QMIN], arange(MBASE, APF + 1)] ig_disp = array([PG, QG, VG]) ig_mu = arange(MU_PMAX, MU_QMIN + 1) ibr_data = arange(ANGMAX + 1) ibr_flow = arange(PF, QT + 1) ibr_mu = array([MU_SF, MU_ST]) #ibr_angmu = array([MU_ANGMIN, MU_ANGMAX]) ## get solved DC power flow case from MAT-file ## defines bus_soln, gen_soln, branch_soln, f_soln soln9_dcopf = loadmat(join(tdir, 'soln9_dcopf.mat'), struct_as_record=True) bus_soln, gen_soln, branch_soln, f_soln = \ soln9_dcopf['bus_soln'], soln9_dcopf['gen_soln'], \ soln9_dcopf['branch_soln'], soln9_dcopf['f_soln'] ## run OPF t = t0 r = rundcopf(casefile, ppopt) bus, gen, branch, f, success = \ r['bus'], r['gen'], r['branch'], r['f'], r['success'] t_ok(success, [t, 'success']) t_is(f, f_soln, 3, [t, 'f']) t_is(bus[:, ib_data], bus_soln[:, ib_data], 10, [t, 'bus data']) t_is(bus[:, ib_voltage], bus_soln[:, ib_voltage], 3, [t, 'bus voltage']) t_is(bus[:, ib_lam], bus_soln[:, ib_lam], 3, [t, 'bus lambda']) t_is(bus[:, ib_mu], bus_soln[:, ib_mu], 2, [t, 'bus mu']) t_is(gen[:, ig_data], gen_soln[:, ig_data], 10, [t, 'gen data']) t_is(gen[:, ig_disp], gen_soln[:, ig_disp], 3, [t, 'gen dispatch']) t_is(gen[:, ig_mu], gen_soln[:, ig_mu], 3, [t, 'gen mu']) t_is(branch[:, ibr_data], branch_soln[:, ibr_data], 10, [t, 'branch data']) t_is(branch[:, ibr_flow], branch_soln[:, ibr_flow], 3, [t, 'branch flow']) t_is(branch[:, ibr_mu], branch_soln[:, ibr_mu], 2, [t, 'branch mu']) ##----- run OPF with extra linear user constraints & costs ----- ## two new z variables ## 0 <= z1, P2 - P1 <= z1 ## 0 <= z2, P2 - P3 <= z2 ## with A and N sized for DC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [9, 10, 12, 10, 11, 13] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 14)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [12, 13])), (2, 14)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 1 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) ## with A and N sized for AC opf ppc = loadcase(casefile) row = [0, 0, 0, 1, 1, 1] col = [18, 19, 24, 19, 20, 25] ppc['A'] = sparse(([-1, 1, -1, 1, -1, -1], (row, col)), (2, 26)) ppc['u'] = array([0, 0]) ppc['l'] = array([-Inf, -Inf]) ppc['zl'] = array([0, 0]) ppc['N'] = sparse(([1, 1], ([0, 1], [24, 25])), (2, 26)) ## new z variables only ppc['fparm'] = ones((2, 1)) * array([[1, 0, 0, 1]]) ## w = r = z ppc['H'] = sparse((2, 2)) ## no quadratic term ppc['Cw'] = array([1000, 1]) t = ''.join([t0, 'w/extra constraints & costs 2 : ']) r = rundcopf(ppc, ppopt) t_ok(r['success'], [t, 'success']) t_is(r['gen'][0, PG], 116.15974, 4, [t, 'Pg1 = 116.15974']) t_is(r['gen'][1, PG], 116.15974, 4, [t, 'Pg2 = 116.15974']) t_is(r['var']['val']['z'], [0, 0.3348], 4, [t, 'user vars']) t_is(r['cost']['usr'], 0.3348, 3, [t, 'user costs']) t = ''.join([t0, 'infeasible : ']) ## with A and N sized for DC opf ppc = loadcase(casefile) ppc['A'] = sparse(([1, 1], ([0, 0], [9, 10])), (1, 14)) ## Pg1 + Pg2 ppc['u'] = array([Inf]) ppc['l'] = array([600]) r = rundcopf(ppc, ppopt) t_ok(not r['success'], [t, 'no success']) else: t_skip(num_tests, 'Gurobi not available') t_end()
def t_off2case(quiet=False): """Tests for code in C{off2case}. @author: Ray Zimmerman (PSERC Cornell) @author: Richard Lincoln """ n_tests = 35 t_begin(n_tests, quiet) ## generator data # bus Pg Qg Qmax Qmin Vg mBase status Pmax Pmin Pc1 Pc2 Qc1min Qc1max Qc2min Qc2max ramp_agc ramp_10 ramp_30 ramp_q apf gen0 = array([ [1, 10, 0, 60, -15, 1, 100, 1, 60, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 10, 0, 60, -15, 1, 100, 1, 60, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [7, -30, -15, 0, -15, 1, 100, 1, 0, -30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [13, 10, 0, 60, -15, 1, 100, 1, 60, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [30, -30, 7.5, 7.5, 0, 1, 100, 1, 0, -30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ], float) ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost0 = array([ [1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 100, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 0, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 50, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000] ], float) try: from pypower.extras.smartmarket import off2case except ImportError: t_skip(n_tests, 'smartmarket code not available') return t = 'isload()' t_is(isload(gen0), array([0, 0, 1, 0, 1], bool), 8, t) G = find( ~isload(gen0) ) L = find( isload(gen0) ) nGL = len(G) + len(L) t = 'P offers only'; offers = {'P': {}} offers['P']['qty'] = array([[25], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) gen, gencost = off2case(gen0, gencost0, offers) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'].flatten() gen1[L, GEN_STATUS] = 0 t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0.copy() gencost1[ix_(G, range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700], ]), zeros((3, 4))] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) offers['P']['qty'] = array([[25], [26], [0], [27], [0]], float) offers['P']['prc'] = array([[10], [50], [0], [100], [0]], float) gen, gencost = off2case(gen0, gencost0, offers) t_is( gen, gen1, 8, [t, ' (all rows in offer) - gen'] ) t_is( gencost, gencost1, 8, [t, ' (all rows in offer) - gencost'] ) t = 'P offers only (GEN_STATUS=0 for 0 qty offer)'; offers['P']['qty'] = array([ [0], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) gen, gencost = off2case(gen0, gencost0, offers) gen1 = gen0.copy() gen1[G[1:3], PMAX] = offers['P']['qty'].flatten()[1:3] gen1[G[0], GEN_STATUS] = 0 gen1[L, GEN_STATUS] = 0 t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0.copy() gencost1[ix_(G[1:3], range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700] ]), zeros((2, 4))] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers, lim[\'P\'][\'max_offer\']'; offers['P']['qty'] = array([[25], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) lim = {'P': {'max_offer': 75}} gen, gencost = off2case(gen0, gencost0, offers, lim=lim) gen1 = gen0.copy() gen1[G[:2], PMAX] = offers['P']['qty'].flatten()[:2, :] gen1[r_[G[2], L], GEN_STATUS] = 0 t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0.copy() gencost1[ix_(G[:2], range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300] ]), zeros((2, 4))] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers & P bids'; bids = {'P': {'qty': array([ [20], [28]], float), 'prc': array([[100], [10]], float)}} gen, gencost = off2case(gen0, gencost0, offers, bids) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[ix_(L, [PMIN, QMIN, QMAX])] = array([ [-20, -10, 0], [-28, 0, 7] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :8].copy() gencost1[ix_(G, range(NCOST, NCOST + 4))] = array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700] ]) gencost1[ix_(L, range(NCOST, NCOST + 4))] = array([ [2, -20, -2000, 0, 0], [2, -28, -280, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers & P bids (all rows in bid)'; bids['P']['qty'] = array([[0], [0], [20], [0], [28]], float) bids['P']['prc'] = array([[0], [0], [100], [0], [10]], float) gen, gencost = off2case(gen0, gencost0, offers, bids) t_is( gen, gen1, 8, [t, ' - gen'] ) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers & P bids (GEN_STATUS=0 for 0 qty bid)'; bids['P']['qty'] = array([ [0], [28]], float) bids['P']['prc'] = array([[100], [10]], float) gen, gencost = off2case(gen0, gencost0, offers, bids) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[L[0], GEN_STATUS] = 0 gen1[L[1], [PMIN, QMIN, QMAX]] = array([-28, 0, 7]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0.copy() gencost1[ix_(G, range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700] ]), zeros((3, 4))] gencost1[L[1], NCOST:NCOST + 8] = c_[array([ [2, -28, -280, 0, 0] ]), zeros((1, 4))] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers & P bids (1 gen with both)'; gen2 = gen0.copy() gen2[1, PMIN] = -5 bids['P']['qty'] = array([[0], [3], [20], [0], [28]], float) bids['P']['prc'] = array([[0], [50], [100], [0], [10]], float) gen, gencost = off2case(gen2, gencost0, offers, bids) gen1 = gen2.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[1, PMIN] = -sum( bids['P']['qty'][1, :] ) gen1[ix_(L, [PMIN, QMIN, QMAX])] = array([ [-20, -10, 0], [-28, 0, 7] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :10].copy() gencost1[ix_(G, range(NCOST, NCOST + 7))] = array([ [2, 0, 0, 25, 250, 0, 0], [3, -3, -150, 0, 0, 26, 1300], [2, 0, 0, 27, 2700, 0, 0] ]) gencost1[ix_(L, range(NCOST, NCOST + 7))] = c_[array([ [2, -20, -2000, 0, 0], [2, -28, -280, 0, 0] ]), zeros((2, 2))] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers & P bids, lim[\'P\'][\'max_offer\']/[\'min_bid\']' bids['P']['qty'] = array([[20], [28]], float) bids['P']['prc'] = array([[100], [10]], float) lim['P']['min_bid'] = 50.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[G[:2], PMAX] = offers['P']['qty'][:2, :] gen1[r_[G[2], L[1]], GEN_STATUS] = 0 gen1[L[0], [PMIN, QMIN, QMAX]] = array([-20, -10, 0]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0.copy() gencost1[ix_(G[:2], range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300] ]), zeros((2, 4))] gencost1[L[0], NCOST:NCOST + 9] = array([2, -20, -2000, 0, 0, 0, 0, 0, 0]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'P offers & P bids, lim[\'P\'][\'max_offer\']/[\'min_bid\'], multi-block' offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[ 20, 10], [12, 18]], float) bids['P']['prc'] = array([[100, 60], [70, 10]], float) gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[G, PMAX] = array([10, 50, 25]) gen1[ix_(L, [PMIN, QMIN, QMAX])] = array([ [-30, -15, 0], [-12, 0, 3] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :10].copy() gencost1[ix_(G, range(NCOST, NCOST + 7))] = array([ [2, 0, 0, 10, 100, 0, 0], [3, 0, 0, 20, 500, 50, 2450], [2, 0, 0, 25, 1250, 0, 0] ]) gencost1[ix_(L, range(NCOST, NCOST + 7))] = array([ [3, -30, -2600, -20, -2000, 0, 0], [2, -12, -840, 0, 0, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) ##----- reactive ----- ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost0 = array([ [1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 100, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 0, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 50, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [1, 0, 0, 4, -15, -150, 0, 0, 30, 150, 60, 450], [1, 100, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 3, -20, -15, -10, -10, 0, 0, 0, 0], [1, 0, 0, 3, 0, 0, 40, 80, 60, 180, 0, 0], [1, 0, 50, 2, 0, 0, 0, 0, 0, 0, 0, 0] ], float) t = 'PQ offers only'; offers['P']['qty'] = array([[25], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) offers['Q']['qty'] = array([[10], [20], [30]], float) offers['Q']['prc'] = array([[10], [5], [1]], float) gen, gencost = off2case(gen0, gencost0, offers) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[G, QMAX] = offers['Q']['qty'] gen1[G, QMIN] = 0 gen1[L, GEN_STATUS] = 0 t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0.copy() gencost1[ix_(G, range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700] ]), zeros((3, 4))] gencost1[ix_(G + nGL - 1, range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 10, 100], [2, 0, 0, 20, 100], [2, 0, 0, 30, 30] ]), zeros((3, 4))] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'PQ offers & PQ bids, lim.P/Q.max_offer/min_bid, multi-block'; offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[ 20, 10], [12, 18]], float) bids['P']['prc'] = array([[100, 60], [70, 10]], float) offers['Q']['qty'] = array([[ 5, 5], [10, 10], [15, 15]], float) offers['Q']['prc'] = array([[10, 20], [ 5, 60], [ 1, 10]], float) bids['Q']['qty'] = array([ 15, 10, 15, 15, 0], float) bids['Q']['prc'] = array([-10, 0, 5, -20, 10], float) lim['Q']['max_offer'] = 50.0 lim['Q']['min_bid'] = -15.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[:, [GEN_STATUS, PMIN, PMAX, QMIN, QMAX]] = array([ [1, 10, 10, -15, 10], [1, 12, 50, -10, 10], [1, -10, 0, -5, 0], [1, 12, 25, 0, 30], [0, -30, 0, 0, 7.5] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST - 1:NCOST + 9] = array([ [2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [3, -30, -2600, -20, -2000, 0, 0, 0, 0], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [2, -15, -75, 0, 0, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'PQ offers & PQ bids, for gen, no P, no shutdown'; gen2 = gen0.copy() gen2[0, PMIN] = 0 offers['P']['qty'] = array([[0, 40], [20, 30], [25, 25]], float) gen, gencost = off2case(gen2, gencost0, offers, bids, lim) gen1[0, [PMIN, PMAX, QMIN, QMAX]] = array([0, 0, -15, 10]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1[0, NCOST:NCOST + 9] = gencost0[0, NCOST:NCOST + 9] t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'PQ offers & PQ bids, for gen, no Q, no shutdown'; offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['Q']['qty'] = array([[ 5, 5], [ 0, 10], [15, 15]], float) bids['Q']['qty'] = array([15, 0, 15, 15, 0], float) gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1[0, [PMIN, PMAX, QMIN, QMAX]] = array([10, 10, -15, 10]) ## restore original gen1[1, [PMIN, PMAX, QMIN, QMAX]] = array([12, 50, 0, 0]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1[ix_([0, 1, 6], range(NCOST, NCOST + 9))] = array([ [2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'PQ offers & PQ bids, lim.P/Q.max_offer/min_bid, multi-block'; offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[10, 0], [12, 18]], float) bids['P']['prc'] = array([[100, 60], [70, 10]], float) offers['Q']['qty'] = array([[5, 5], [10, 10], [15, 15]], float) offers['Q']['prc'] = array([[10, 20], [5, 60], [1, 10]], float) bids['Q']['qty'] = array([15, 10, 10, 15, 0], float) bids['Q']['prc'] = array([-10, 0, 5, -20, 10], float) lim['Q']['max_offer'] = 50.0 lim['Q']['min_bid'] = -15.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[:, [GEN_STATUS, PMIN, PMAX, QMIN, QMAX]] = array([ [1, 10, 10, -15, 10], [1, 12, 50, -10, 10], [1, -10, 0, -5, 0], [1, 12, 25, 0, 30], [0, -30, 0, 0, 7.5] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST:NCOST + 9] = array([ [2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [2, -10, -1000, 0, 0, 0, 0, 0, 0], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [2, -10, -50, 0, 0, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'PQ offers & PQ bids, zero Q load w/P bid, shutdown bugfix'; gen1 = gen0.copy() gen1[4, [QG, QMIN, QMAX]] = 0 gen, gencost = off2case(gen1, gencost0, offers, bids, lim) gen1[:, [PMIN, PMAX, QMIN, QMAX]] = array([ [ 10, 10, -15, 10], [ 12, 50, -10, 10], [-10, 0, -5, 0], [ 12, 25, 0, 30], [-12, 0, 0, 0] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST - 1:NCOST + 9] = array([ [2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [2, -10, -1000, 0, 0, 0, 0, 0, 0], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [2, -12, -840, 0, 0, 0, 0, 0, 0], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [2, -10, -50, 0, 0, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t = 'PQ offers & PQ bids, non-zero Q load w/no P bid, shutdown bugfix'; offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[0, 10], [12, 18]], float) bids['P']['prc'] = array([[100, 40], [70, 10]], float) offers['Q']['qty'] = array([[ 5, 5], [10, 10], [15, 15]], float) offers['Q']['prc'] = array([[10, 20], [ 5, 60], [ 1, 10]], float) bids['Q']['qty'] = array([ 15, 10, 15, 15, 0], float) bids['Q']['prc'] = array([-10, 0, 5, -20, 10], float) lim['Q']['max_offer'] = 50.0 lim['Q']['min_bid'] = -15.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[:, [GEN_STATUS, PMIN, PMAX, QMIN, QMAX]] = array([ [1, 10, 10, -15, 10], [1, 12, 50, -10, 10], [0, -30, 0, -15, 0], [1, 12, 25, 0, 30], [0, -30, 0, 0, 7.5] ]) t_is( gen, gen1, 8, [t, ' - gen'] ) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST - 1:NCOST + 9] = array([ [2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [3, -20, -15, -10, -10, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0] ]) t_is( gencost, gencost1, 8, [t, ' - gencost'] ) t_end()
def t_off2case(quiet=False): """Tests for code in C{off2case}. @author: Ray Zimmerman (PSERC Cornell) """ n_tests = 35 t_begin(n_tests, quiet) ## generator data # bus Pg Qg Qmax Qmin Vg mBase status Pmax Pmin Pc1 Pc2 Qc1min Qc1max Qc2min Qc2max ramp_agc ramp_10 ramp_30 ramp_q apf gen0 = array([[ 1, 10, 0, 60, -15, 1, 100, 1, 60, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 2, 10, 0, 60, -15, 1, 100, 1, 60, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 7, -30, -15, 0, -15, 1, 100, 1, 0, -30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 13, 10, 0, 60, -15, 1, 100, 1, 60, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 30, -30, 7.5, 7.5, 0, 1, 100, 1, 0, -30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]], float) ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost0 = array([[1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 100, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 0, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 50, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000]], float) try: from pypower.extras.smartmarket import off2case except ImportError: t_skip(n_tests, 'smartmarket code not available') return t = 'isload()' t_is(isload(gen0), array([0, 0, 1, 0, 1], bool), 8, t) G = find(~isload(gen0)) L = find(isload(gen0)) nGL = len(G) + len(L) t = 'P offers only' offers = {'P': {}} offers['P']['qty'] = array([[25], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) gen, gencost = off2case(gen0, gencost0, offers) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'].flatten() gen1[L, GEN_STATUS] = 0 t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0.copy() gencost1[ix_(G, range(NCOST, NCOST + 9))] = c_[array([ [2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700], ]), zeros((3, 4))] t_is(gencost, gencost1, 8, [t, ' - gencost']) offers['P']['qty'] = array([[25], [26], [0], [27], [0]], float) offers['P']['prc'] = array([[10], [50], [0], [100], [0]], float) gen, gencost = off2case(gen0, gencost0, offers) t_is(gen, gen1, 8, [t, ' (all rows in offer) - gen']) t_is(gencost, gencost1, 8, [t, ' (all rows in offer) - gencost']) t = 'P offers only (GEN_STATUS=0 for 0 qty offer)' offers['P']['qty'] = array([[0], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) gen, gencost = off2case(gen0, gencost0, offers) gen1 = gen0.copy() gen1[G[1:3], PMAX] = offers['P']['qty'].flatten()[1:3] gen1[G[0], GEN_STATUS] = 0 gen1[L, GEN_STATUS] = 0 t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0.copy() gencost1[ix_(G[1:3], range( NCOST, NCOST + 9))] = c_[array([[2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700]]), zeros((2, 4))] t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers, lim[\'P\'][\'max_offer\']' offers['P']['qty'] = array([[25], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) lim = {'P': {'max_offer': 75}} gen, gencost = off2case(gen0, gencost0, offers, lim=lim) gen1 = gen0.copy() gen1[G[:2], PMAX] = offers['P']['qty'].flatten()[:2, :] gen1[r_[G[2], L], GEN_STATUS] = 0 t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0.copy() gencost1[ix_(G[:2], range( NCOST, NCOST + 9))] = c_[array([[2, 0, 0, 25, 250], [2, 0, 0, 26, 1300]]), zeros((2, 4))] t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers & P bids' bids = { 'P': { 'qty': array([[20], [28]], float), 'prc': array([[100], [10]], float) } } gen, gencost = off2case(gen0, gencost0, offers, bids) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[ix_(L, [PMIN, QMIN, QMAX])] = array([[-20, -10, 0], [-28, 0, 7]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :8].copy() gencost1[ix_(G, range(NCOST, NCOST + 4))] = array([[2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700]]) gencost1[ix_(L, range(NCOST, NCOST + 4))] = array([[2, -20, -2000, 0, 0], [2, -28, -280, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers & P bids (all rows in bid)' bids['P']['qty'] = array([[0], [0], [20], [0], [28]], float) bids['P']['prc'] = array([[0], [0], [100], [0], [10]], float) gen, gencost = off2case(gen0, gencost0, offers, bids) t_is(gen, gen1, 8, [t, ' - gen']) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers & P bids (GEN_STATUS=0 for 0 qty bid)' bids['P']['qty'] = array([[0], [28]], float) bids['P']['prc'] = array([[100], [10]], float) gen, gencost = off2case(gen0, gencost0, offers, bids) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[L[0], GEN_STATUS] = 0 gen1[L[1], [PMIN, QMIN, QMAX]] = array([-28, 0, 7]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0.copy() gencost1[ix_(G, range(NCOST, NCOST + 9))] = c_[ array([[2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700]]), zeros((3, 4))] gencost1[L[1], NCOST:NCOST + 8] = c_[array([[2, -28, -280, 0, 0]]), zeros((1, 4))] t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers & P bids (1 gen with both)' gen2 = gen0.copy() gen2[1, PMIN] = -5 bids['P']['qty'] = array([[0], [3], [20], [0], [28]], float) bids['P']['prc'] = array([[0], [50], [100], [0], [10]], float) gen, gencost = off2case(gen2, gencost0, offers, bids) gen1 = gen2.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[1, PMIN] = -sum(bids['P']['qty'][1, :]) gen1[ix_(L, [PMIN, QMIN, QMAX])] = array([[-20, -10, 0], [-28, 0, 7]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :10].copy() gencost1[ix_(G, range(NCOST, NCOST + 7))] = array([[2, 0, 0, 25, 250, 0, 0], [3, -3, -150, 0, 0, 26, 1300], [2, 0, 0, 27, 2700, 0, 0]]) gencost1[ix_(L, range( NCOST, NCOST + 7))] = c_[array([[2, -20, -2000, 0, 0], [2, -28, -280, 0, 0]]), zeros((2, 2))] t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers & P bids, lim[\'P\'][\'max_offer\']/[\'min_bid\']' bids['P']['qty'] = array([[20], [28]], float) bids['P']['prc'] = array([[100], [10]], float) lim['P']['min_bid'] = 50.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[G[:2], PMAX] = offers['P']['qty'][:2, :] gen1[r_[G[2], L[1]], GEN_STATUS] = 0 gen1[L[0], [PMIN, QMIN, QMAX]] = array([-20, -10, 0]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0.copy() gencost1[ix_(G[:2], range( NCOST, NCOST + 9))] = c_[array([[2, 0, 0, 25, 250], [2, 0, 0, 26, 1300]]), zeros((2, 4))] gencost1[L[0], NCOST:NCOST + 9] = array([2, -20, -2000, 0, 0, 0, 0, 0, 0]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'P offers & P bids, lim[\'P\'][\'max_offer\']/[\'min_bid\'], multi-block' offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[20, 10], [12, 18]], float) bids['P']['prc'] = array([[100, 60], [70, 10]], float) gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[G, PMAX] = array([10, 50, 25]) gen1[ix_(L, [PMIN, QMIN, QMAX])] = array([[-30, -15, 0], [-12, 0, 3]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :10].copy() gencost1[ix_(G, range(NCOST, NCOST + 7))] = array([[2, 0, 0, 10, 100, 0, 0], [3, 0, 0, 20, 500, 50, 2450], [2, 0, 0, 25, 1250, 0, 0]]) gencost1[ix_(L, range(NCOST, NCOST + 7))] = array([[3, -30, -2600, -20, -2000, 0, 0], [2, -12, -840, 0, 0, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) ##----- reactive ----- ## generator cost data # 1 startup shutdown n x1 y1 ... xn yn # 2 startup shutdown n c(n-1) ... c0 gencost0 = array([[1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 100, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 0, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [1, 0, 0, 4, 0, 0, 12, 240, 36, 1200, 60, 2400], [1, 0, 50, 4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [1, 0, 0, 4, -15, -150, 0, 0, 30, 150, 60, 450], [1, 100, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 3, -20, -15, -10, -10, 0, 0, 0, 0], [1, 0, 0, 3, 0, 0, 40, 80, 60, 180, 0, 0], [1, 0, 50, 2, 0, 0, 0, 0, 0, 0, 0, 0]], float) t = 'PQ offers only' offers['P']['qty'] = array([[25], [26], [27]], float) offers['P']['prc'] = array([[10], [50], [100]], float) offers['Q']['qty'] = array([[10], [20], [30]], float) offers['Q']['prc'] = array([[10], [5], [1]], float) gen, gencost = off2case(gen0, gencost0, offers) gen1 = gen0.copy() gen1[G, PMAX] = offers['P']['qty'] gen1[G, QMAX] = offers['Q']['qty'] gen1[G, QMIN] = 0 gen1[L, GEN_STATUS] = 0 t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0.copy() gencost1[ix_(G, range(NCOST, NCOST + 9))] = c_[ array([[2, 0, 0, 25, 250], [2, 0, 0, 26, 1300], [2, 0, 0, 27, 2700]]), zeros((3, 4))] gencost1[ix_(G + nGL - 1, range(NCOST, NCOST + 9))] = c_[ array([[2, 0, 0, 10, 100], [2, 0, 0, 20, 100], [2, 0, 0, 30, 30]]), zeros((3, 4))] t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'PQ offers & PQ bids, lim.P/Q.max_offer/min_bid, multi-block' offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[20, 10], [12, 18]], float) bids['P']['prc'] = array([[100, 60], [70, 10]], float) offers['Q']['qty'] = array([[5, 5], [10, 10], [15, 15]], float) offers['Q']['prc'] = array([[10, 20], [5, 60], [1, 10]], float) bids['Q']['qty'] = array([15, 10, 15, 15, 0], float) bids['Q']['prc'] = array([-10, 0, 5, -20, 10], float) lim['Q']['max_offer'] = 50.0 lim['Q']['min_bid'] = -15.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[:, [GEN_STATUS, PMIN, PMAX, QMIN, QMAX]] = array([[1, 10, 10, -15, 10], [1, 12, 50, -10, 10], [1, -10, 0, -5, 0], [1, 12, 25, 0, 30], [0, -30, 0, 0, 7.5]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST - 1:NCOST + 9] = array( [[2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [3, -30, -2600, -20, -2000, 0, 0, 0, 0], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [2, -15, -75, 0, 0, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'PQ offers & PQ bids, for gen, no P, no shutdown' gen2 = gen0.copy() gen2[0, PMIN] = 0 offers['P']['qty'] = array([[0, 40], [20, 30], [25, 25]], float) gen, gencost = off2case(gen2, gencost0, offers, bids, lim) gen1[0, [PMIN, PMAX, QMIN, QMAX]] = array([0, 0, -15, 10]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1[0, NCOST:NCOST + 9] = gencost0[0, NCOST:NCOST + 9] t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'PQ offers & PQ bids, for gen, no Q, no shutdown' offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['Q']['qty'] = array([[5, 5], [0, 10], [15, 15]], float) bids['Q']['qty'] = array([15, 0, 15, 15, 0], float) gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1[0, [PMIN, PMAX, QMIN, QMAX]] = array([10, 10, -15, 10]) ## restore original gen1[1, [PMIN, PMAX, QMIN, QMAX]] = array([12, 50, 0, 0]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1[ix_([0, 1, 6], range(NCOST, NCOST + 9))] = array( [[2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'PQ offers & PQ bids, lim.P/Q.max_offer/min_bid, multi-block' offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[10, 0], [12, 18]], float) bids['P']['prc'] = array([[100, 60], [70, 10]], float) offers['Q']['qty'] = array([[5, 5], [10, 10], [15, 15]], float) offers['Q']['prc'] = array([[10, 20], [5, 60], [1, 10]], float) bids['Q']['qty'] = array([15, 10, 10, 15, 0], float) bids['Q']['prc'] = array([-10, 0, 5, -20, 10], float) lim['Q']['max_offer'] = 50.0 lim['Q']['min_bid'] = -15.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[:, [GEN_STATUS, PMIN, PMAX, QMIN, QMAX]] = array([[1, 10, 10, -15, 10], [1, 12, 50, -10, 10], [1, -10, 0, -5, 0], [1, 12, 25, 0, 30], [0, -30, 0, 0, 7.5]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST:NCOST + 9] = array( [[2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [2, -10, -1000, 0, 0, 0, 0, 0, 0], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [2, -10, -50, 0, 0, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'PQ offers & PQ bids, zero Q load w/P bid, shutdown bugfix' gen1 = gen0.copy() gen1[4, [QG, QMIN, QMAX]] = 0 gen, gencost = off2case(gen1, gencost0, offers, bids, lim) gen1[:, [PMIN, PMAX, QMIN, QMAX]] = array([[10, 10, -15, 10], [12, 50, -10, 10], [-10, 0, -5, 0], [12, 25, 0, 30], [-12, 0, 0, 0]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST - 1:NCOST + 9] = array([[2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [2, -10, -1000, 0, 0, 0, 0, 0, 0], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [2, -12, -840, 0, 0, 0, 0, 0, 0], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [2, -10, -50, 0, 0, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t = 'PQ offers & PQ bids, non-zero Q load w/no P bid, shutdown bugfix' offers['P']['qty'] = array([[10, 40], [20, 30], [25, 25]], float) offers['P']['prc'] = array([[10, 100], [25, 65], [50, 90]], float) bids['P']['qty'] = array([[0, 10], [12, 18]], float) bids['P']['prc'] = array([[100, 40], [70, 10]], float) offers['Q']['qty'] = array([[5, 5], [10, 10], [15, 15]], float) offers['Q']['prc'] = array([[10, 20], [5, 60], [1, 10]], float) bids['Q']['qty'] = array([15, 10, 15, 15, 0], float) bids['Q']['prc'] = array([-10, 0, 5, -20, 10], float) lim['Q']['max_offer'] = 50.0 lim['Q']['min_bid'] = -15.0 gen, gencost = off2case(gen0, gencost0, offers, bids, lim) gen1 = gen0.copy() gen1[:, [GEN_STATUS, PMIN, PMAX, QMIN, QMAX]] = array([[1, 10, 10, -15, 10], [1, 12, 50, -10, 10], [0, -30, 0, -15, 0], [1, 12, 25, 0, 30], [0, -30, 0, 0, 7.5]]) t_is(gen, gen1, 8, [t, ' - gen']) gencost1 = gencost0[:, :12].copy() gencost1[:, NCOST - 1:NCOST + 9] = array( [[2, 0, 0, 10, 100, 0, 0, 0, 0], [3, 0, 0, 20, 500, 50, 2450, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [2, 0, 0, 25, 1250, 0, 0, 0, 0], [4, -30, 0, -20, 1000, -10, 2000, 0, 3000], [4, -15, 150, 0, 0, 5, 50, 10, 150], [3, -10, 0, 0, 0, 10, 50, 0, 0], [3, -20, -15, -10, -10, 0, 0, 0, 0], [3, 0, 0, 15, 15, 30, 165, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0, 0]]) t_is(gencost, gencost1, 8, [t, ' - gencost']) t_end()
t_is(-sum(gen[lda[k], QMIN]), area[k]['disp']['qmin'], 8, '%s area %d disp Qmin' % (t, k)) t_is(-sum(gen[lda[k], QMAX]), area[k]['disp']['qmax'], 8, '%s area %d disp Qmax' % (t, k)) ##----- explict single load zone ----- t = 'explicit single load zone' load_zone = zeros(ppc['bus'].shape[0]) load_zone[[2, 3]] = 1 load = array([2.0]) bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) Pd = ppc['bus'][:, PD] Pd[[2, 3]] = load * Pd[[2, 3]] t_is( bus[:, PD], Pd, 8, t) ##----- explict multiple load zone ----- t = 'explicit multiple load zone' load_zone = zeros(ppc['bus'].shape[0]) load_zone[[2, 3]] = 1 load_zone[[6, 7]] = 2 load = array([2, 0.5]) bus, gen = scale_load(load, ppc['bus'], ppc['gen'], load_zone) Pd = ppc['bus'][:, PD] Pd[[2, 3]] = load[0] * Pd[[2, 3]] Pd[[6, 7]] = load[1] * Pd[[6, 7]] t_is( bus[:, PD], Pd, 8, t) t_end() if __name__ == '__main__': t_scale_load(quiet=False)