Example #1
0
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 userfcn_reserves_int2ext(results, *args):
    """This is the 'int2ext' stage userfcn callback that converts everything
    back to external indexing and packages up the results. It expects to
    find a 'reserves' field in the results struct as described for ppc
    above, including the two additional fields 'igr' and 'rgens'. It also
    expects the results to contain a variable 'R' and linear constraints
    'Pg_plus_R' and 'Rreq' which are used to populate output fields in
    results.reserves. The optional args are not currently used.
    """
    ## initialize some things
    r = results['reserves']

    ## grab some info in internal indexing order
    igr = r['igr']  ## indices of gens available to provide reserves
    ng = results['gen'].shape[0]  ## number of on-line gens (+ disp loads)

    ##-----  convert stuff back to external indexing  -----
    ## convert all reserve parameters (zones, costs, qty, rgens)
    if 'qty' in r:
        results = i2e_field(results, ['reserves', 'qty'], ordering='gen')

    results = i2e_field(results, ['reserves', 'cost'], ordering='gen')
    results = i2e_field(results, ['reserves', 'zones'], ordering='gen', dim=1)
    results = i2e_field(results, ['reserves', 'rgens'], ordering='gen', dim=1)
    results['order']['int']['reserves']['igr'] = results['reserves'][
        'igr']  ## save internal version
    results['reserves']['igr'] = results['order']['ext']['reserves'][
        'igr']  ## use external version
    r = results['reserves']  ## update
    o = results['order']  ## update

    ## grab same info in external indexing order
    igr0 = r['igr']  ## indices of gens available to provide reserves
    ng0 = o['ext']['gen'].shape[0]  ## number of gens (+ disp loads)

    ##-----  results post-processing  -----
    ## get the results (per gen reserves, multipliers) with internal gen indexing
    ## and convert from p.u. to per MW units
    _, Rl, Ru = results['om'].getv('R')
    R = zeros(ng)
    Rmin = zeros(ng)
    Rmax = zeros(ng)
    mu_l = zeros(ng)
    mu_u = zeros(ng)
    mu_Pmax = zeros(ng)
    R[igr] = results['var']['val']['R'] * results['baseMVA']
    Rmin[igr] = Rl * results['baseMVA']
    Rmax[igr] = Ru * results['baseMVA']
    mu_l[igr] = results['var']['mu']['l']['R'] / results['baseMVA']
    mu_u[igr] = results['var']['mu']['u']['R'] / results['baseMVA']
    mu_Pmax[igr] = results['lin']['mu']['u']['Pg_plus_R'] / results['baseMVA']

    ## store in results in results struct
    z = zeros(ng0)
    results['reserves']['R'] = i2e_data(results, R, z, 'gen')
    results['reserves']['Rmin'] = i2e_data(results, Rmin, z, 'gen')
    results['reserves']['Rmax'] = i2e_data(results, Rmax, z, 'gen')
    if 'mu' not in results['reserves']:
        results['reserves']['mu'] = {}
    results['reserves']['mu']['l'] = i2e_data(results, mu_l, z, 'gen')
    results['reserves']['mu']['u'] = i2e_data(results, mu_u, z, 'gen')
    results['reserves']['mu']['Pmax'] = i2e_data(results, mu_Pmax, z, 'gen')
    results['reserves']['prc'] = z
    for k in igr0:
        iz = find(r['zones'][:, k])
        results['reserves']['prc'][k] = sum(
            results['lin']['mu']['l']['Rreq'][iz]) / results['baseMVA']

    results['reserves']['totalcost'] = results['cost']['Rcost']

    ## replace ng x 1 cost, qty with ngr x 1 originals
    if 'original' in r:
        if 'qty' in r:
            results['reserves']['qty'] = r['original']['qty']
        results['reserves']['cost'] = r['original']['cost']
        del results['reserves']['original']

    return results
Example #3
0
def userfcn_reserves_int2ext(results, *args):
    """This is the 'int2ext' stage userfcn callback that converts everything
    back to external indexing and packages up the results. It expects to
    find a 'reserves' field in the results struct as described for ppc
    above, including the two additional fields 'igr' and 'rgens'. It also
    expects the results to contain a variable 'R' and linear constraints
    'Pg_plus_R' and 'Rreq' which are used to populate output fields in
    results.reserves. The optional args are not currently used.
    """
    ## initialize some things
    r = results['reserves']

    ## grab some info in internal indexing order
    igr = r['igr']                ## indices of gens available to provide reserves
    ng  = results['gen'].shape[0] ## number of on-line gens (+ disp loads)

    ##-----  convert stuff back to external indexing  -----
    ## convert all reserve parameters (zones, costs, qty, rgens)
    if 'qty' in r:
        results = i2e_field(results, ['reserves', 'qty'], ordering='gen')

    results = i2e_field(results, ['reserves', 'cost'], ordering='gen')
    results = i2e_field(results, ['reserves', 'zones'], ordering='gen', dim=1)
    results = i2e_field(results, ['reserves', 'rgens'], ordering='gen', dim=1)
    results['order']['int']['reserves']['igr'] = results['reserves']['igr']  ## save internal version
    results['reserves']['igr'] = results['order']['ext']['reserves']['igr']  ## use external version
    r = results['reserves']       ## update
    o = results['order']          ## update

    ## grab same info in external indexing order
    igr0 = r['igr']               ## indices of gens available to provide reserves
    ng0  = o['ext']['gen'].shape[0]  ## number of gens (+ disp loads)

    ##-----  results post-processing  -----
    ## get the results (per gen reserves, multipliers) with internal gen indexing
    ## and convert from p.u. to per MW units
    _, Rl, Ru = results['om'].getv('R')
    R       = zeros(ng)
    Rmin    = zeros(ng)
    Rmax    = zeros(ng)
    mu_l    = zeros(ng)
    mu_u    = zeros(ng)
    mu_Pmax = zeros(ng)
    R[igr]       = results['var']['val']['R'] * results['baseMVA']
    Rmin[igr]    = Rl * results['baseMVA']
    Rmax[igr]    = Ru * results['baseMVA']
    mu_l[igr]    = results['var']['mu']['l']['R'] / results['baseMVA']
    mu_u[igr]    = results['var']['mu']['u']['R'] / results['baseMVA']
    mu_Pmax[igr] = results['lin']['mu']['u']['Pg_plus_R'] / results['baseMVA']

    ## store in results in results struct
    z = zeros(ng0)
    results['reserves']['R']       = i2e_data(results, R, z, 'gen')
    results['reserves']['Rmin']    = i2e_data(results, Rmin, z, 'gen')
    results['reserves']['Rmax']    = i2e_data(results, Rmax, z, 'gen')
    if 'mu' not in results['reserves']:
        results['reserves']['mu'] = {}
    results['reserves']['mu']['l']    = i2e_data(results, mu_l, z, 'gen')
    results['reserves']['mu']['u']    = i2e_data(results, mu_u, z, 'gen')
    results['reserves']['mu']['Pmax'] = i2e_data(results, mu_Pmax, z, 'gen')
    results['reserves']['prc']        = z
    for k in igr0:
        iz = find(r['zones'][:, k])
        results['reserves']['prc'][k] = sum(results['lin']['mu']['l']['Rreq'][iz]) / results['baseMVA']

    results['reserves']['totalcost'] = results['cost']['Rcost']

    ## replace ng x 1 cost, qty with ngr x 1 originals
    if 'original' in r:
        if 'qty' in r:
            results['reserves']['qty'] = r['original']['qty']
        results['reserves']['cost'] = r['original']['cost']
        del results['reserves']['original']

    return results
Example #4
0
def int2ext(ppc, val_or_field=None, oldval=None, ordering=None, dim=0):
    """Converts internal to external bus numbering.

    C{ppc = int2ext(ppc)}

    If the input is a single PYPOWER case dict, then it restores all
    buses, generators and branches that were removed because of being
    isolated or off-line, and reverts to the original generator ordering
    and original bus numbering. This requires that the 'order' key
    created by L{ext2int} be in place.

    Example::
        ppc = int2ext(ppc)

    @see: L{ext2int}, L{i2e_field}, L{i2e_data}

    @author: Ray Zimmerman (PSERC Cornell)
    """
    ppc = deepcopy(ppc)
    if val_or_field is None: # nargin == 1
        if 'order' not in ppc:
            sys.stderr.write('int2ext: ppc does not have the "order" field '
                'required for conversion back to external numbering.\n')
        o = ppc["order"]

        if o["state"] == 'i':
            ## execute userfcn callbacks for 'int2ext' stage
            if 'userfcn' in ppc:
                ppc = run_userfcn(ppc["userfcn"], 'int2ext', ppc)

            ## save data matrices with internal ordering & restore originals
            o["int"] = {}
            o["int"]["bus"]    = ppc["bus"].copy()
            o["int"]["branch"] = ppc["branch"].copy()
            o["int"]["gen"]    = ppc["gen"].copy()
            ppc["bus"]     = o["ext"]["bus"].copy()
            ppc["branch"]  = o["ext"]["branch"].copy()
            ppc["gen"]     = o["ext"]["gen"].copy()
            if 'gencost' in ppc:
                o["int"]["gencost"] = ppc["gencost"].copy()
                ppc["gencost"] = o["ext"]["gencost"].copy()
            if 'areas' in ppc:
                o["int"]["areas"] = ppc["areas"].copy()
                ppc["areas"] = o["ext"]["areas"].copy()
            if 'A' in ppc:
                o["int"]["A"] = ppc["A"].copy()
                ppc["A"] = o["ext"]["A"].copy()
            if 'N' in ppc:
                o["int"]["N"] = ppc["N"].copy()
                ppc["N"] = o["ext"]["N"].copy()

            ## update data (in bus, branch and gen only)
            ppc["bus"][o["bus"]["status"]["on"], :] = \
                o["int"]["bus"]
            ppc["branch"][o["branch"]["status"]["on"], :] = \
                o["int"]["branch"]
            ppc["gen"][o["gen"]["status"]["on"], :] = \
                o["int"]["gen"][o["gen"]["i2e"], :]
            if 'areas' in ppc:
                ppc["areas"][o["areas"]["status"]["on"], :] = \
                    o["int"]["areas"]

            ## revert to original bus numbers
            ppc["bus"][o["bus"]["status"]["on"], BUS_I] = \
                o["bus"]["i2e"] \
                    [ ppc["bus"][o["bus"]["status"]["on"], BUS_I].astype(int) ]
            ppc["branch"][o["branch"]["status"]["on"], F_BUS] = \
                o["bus"]["i2e"][ ppc["branch"] \
                    [o["branch"]["status"]["on"], F_BUS].astype(int) ]
            ppc["branch"][o["branch"]["status"]["on"], T_BUS] = \
                o["bus"]["i2e"][ ppc["branch"] \
                    [o["branch"]["status"]["on"], T_BUS].astype(int) ]
            ppc["gen"][o["gen"]["status"]["on"], GEN_BUS] = \
                o["bus"]["i2e"][ ppc["gen"] \
                    [o["gen"]["status"]["on"], GEN_BUS].astype(int) ]
            if 'areas' in ppc:
                ppc["areas"][o["areas"]["status"]["on"], PRICE_REF_BUS] = \
                    o["bus"]["i2e"][ ppc["areas"] \
                    [o["areas"]["status"]["on"], PRICE_REF_BUS].astype(int) ]

            if 'ext' in o: del o['ext']
            o["state"] = 'e'
            ppc["order"] = o
        else:
            sys.stderr.write('int2ext: ppc claims it is already using '
                         'external numbering.\n')
    else:                    ## convert extra data
        if isinstance(val_or_field, str) or isinstance(val_or_field, list):
            ## field (key)
            warn('Calls of the form MPC = INT2EXT(MPC, ''FIELD_NAME'', ...) have been deprecated. Please replace INT2EXT with I2E_FIELD.')
            bus, gen = val_or_field, oldval
            if ordering is not None:
                dim = ordering
            ppc = i2e_field(ppc, bus, gen, dim)
        else:
            ## value
            warn('Calls of the form VAL = INT2EXT(MPC, VAL, ...) have been deprecated. Please replace INT2EXT with I2E_DATA.')
            bus, gen, branch = val_or_field, oldval, ordering
            ppc = i2e_data(ppc, bus, gen, branch, dim)

    return ppc