コード例 #1
0
ファイル: t_makeLODF.py プロジェクト: redw0lf/PYPOWER
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()
コード例 #2
0
ファイル: t_makeLODF.py プロジェクト: charlie0389/PYPOWER
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()
コード例 #3
0
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()
コード例 #4
0
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()
コード例 #5
0
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()
コード例 #6
0
ファイル: t_opf_dc_pips.py プロジェクト: charlie0389/PYPOWER
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()
コード例 #7
0
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()
コード例 #8
0
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()
コード例 #9
0
ファイル: t_opf_userfcns.py プロジェクト: redw0lf/PYPOWER
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()
コード例 #10
0
ファイル: t_makePTDF.py プロジェクト: Anastien/PYPOWER
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()
コード例 #11
0
def validate_from_ppc(
        ppc_net,
        net,
        pf_type="runpp",
        max_diff_values={
            "bus_vm_pu": 1e-6,
            "bus_va_degree": 1e-5,
            "branch_p_mw": 1e-6,
            "branch_q_mvar": 1e-6,
            "gen_p_mw": 1e-6,
            "gen_q_mvar": 1e-6
        },
        run=True):
    """
    This function validates the pypower case files to pandapower net structure conversion via a \
    comparison of loadflow calculation results. (Hence the opf cost conversion is not validated.)

    INPUT:

        **ppc_net** - The pypower case file, which must already contain the pypower powerflow
            results or pypower must be importable.

        **net** - The pandapower network.

    OPTIONAL:

        **pf_type** ("runpp", string) - Type of validated power flow. Possible are ("runpp",
            "rundcpp", "runopp", "rundcopp")

        **max_diff_values** - Dict of maximal allowed difference values. The keys must be
        'vm_pu', 'va_degree', 'p_branch_mw', 'q_branch_mvar', 'p_gen_mw' and 'q_gen_mvar' and
        the values floats.

        **run** (True, bool or list of two bools) - changing the value to False avoids trying to run
            (optimal) loadflows. Giving a list of two bools addresses first pypower and second
            pandapower.

    OUTPUT:

        **conversion_success** - conversion_success is returned as False if pypower or pandapower
        cannot calculate a powerflow or if the maximum difference values (max_diff_values )
        cannot be hold.

    EXAMPLE:

        import pandapower.converter as pc

        net = cv.from_ppc(ppc_net, f_hz=50)

        conversion_success = cv.validate_from_ppc(ppc_net, net)

    NOTE:

        The user has to take care that the loadflow results already are included in the provided \
        ppc_net or pypower is importable.
    """
    # check in case of optimal powerflow comparison whether cost information exist
    if "opp" in pf_type:
        if not (len(net.polynomial_cost) | len(net.piecewise_linear_cost)):
            if "gencost" in ppc_net:
                if not len(ppc_net["gencost"]):
                    logger.debug(
                        'ppc and pandapower net do not include cost information.'
                    )
                    return True
                else:
                    logger.error(
                        'The pandapower net does not include cost information.'
                    )
                    return False
            else:
                logger.debug(
                    'ppc and pandapower net do not include cost information.')
                return True

    # guarantee run parameter as list, for pypower and pandapower (optimal) powerflow run
    run = [run, run] if isinstance(run, bool) else run

    # --- check pypower powerflow success, if possible
    if pypower_import and run[0]:
        try:
            if pf_type == "runpp":
                ppc_net = runpf.runpf(ppc_net, ppopt)[0]
            elif pf_type == "rundcpp":
                ppc_net = rundcpf.rundcpf(ppc_net, ppopt)[0]
            elif pf_type == "runopp":
                ppc_net = runopf.runopf(ppc_net, ppopt)
            elif pf_type == "rundcopp":
                ppc_net = rundcopf.rundcopf(ppc_net, ppopt)
            else:
                raise ValueError("The pf_type %s is unknown" % pf_type)
        except:
            logger.debug("The pypower run did not work.")
    ppc_success = True
    if 'success' in ppc_net.keys():
        if ppc_net['success'] != 1:
            ppc_success = False
            logger.error(
                "The given ppc data indicates an unsuccessful pypower powerflow: "
                + "'ppc_net['success'] != 1'")
    if (ppc_net['branch'].shape[1] < 17):
        ppc_success = False
        logger.error(
            "The shape of given ppc data indicates missing pypower powerflow results."
        )

    # --- try to run a pandapower powerflow
    if run[1]:
        if pf_type == "runpp":
            try:
                pp.runpp(net,
                         init="dc",
                         calculate_voltage_angles=True,
                         trafo_model="pi")
            except pp.LoadflowNotConverged:
                try:
                    pp.runpp(net,
                             calculate_voltage_angles=True,
                             init="flat",
                             trafo_model="pi")
                except pp.LoadflowNotConverged:
                    try:
                        pp.runpp(net,
                                 trafo_model="pi",
                                 calculate_voltage_angles=False)
                        if "bus_va_degree" in max_diff_values.keys():
                            max_diff_values[
                                "bus_va_degree"] = 1e2 if max_diff_values[
                                    "bus_va_degree"] < 1e2 else max_diff_values[
                                        "bus_va_degree"]
                        logger.info("voltage_angles could be calculated.")
                    except pp.LoadflowNotConverged:
                        logger.error(
                            'The pandapower powerflow does not converge.')
        elif pf_type == "rundcpp":
            try:
                pp.rundcpp(net, trafo_model="pi")
            except pp.LoadflowNotConverged:
                logger.error('The pandapower dc powerflow does not converge.')
        elif pf_type == "runopp":
            try:
                pp.runopp(net, init="flat", calculate_voltage_angles=True)
            except pp.OPFNotConverged:
                try:
                    pp.runopp(net, init="pf", calculate_voltage_angles=True)
                except (pp.OPFNotConverged, pp.LoadflowNotConverged, KeyError):
                    try:
                        pp.runopp(net,
                                  init="flat",
                                  calculate_voltage_angles=False)
                        logger.info("voltage_angles could be calculated.")
                        if "bus_va_degree" in max_diff_values.keys():
                            max_diff_values[
                                "bus_va_degree"] = 1e2 if max_diff_values[
                                    "bus_va_degree"] < 1e2 else max_diff_values[
                                        "bus_va_degree"]
                    except pp.OPFNotConverged:
                        try:
                            pp.runopp(net,
                                      init="pf",
                                      calculate_voltage_angles=False)
                            if "bus_va_degree" in max_diff_values.keys():
                                max_diff_values[
                                    "bus_va_degree"] = 1e2 if max_diff_values[
                                        "bus_va_degree"] < 1e2 else max_diff_values[
                                            "bus_va_degree"]
                            logger.info("voltage_angles could be calculated.")
                        except (pp.OPFNotConverged, pp.LoadflowNotConverged,
                                KeyError):
                            logger.error(
                                'The pandapower optimal powerflow does not converge.'
                            )
        elif pf_type == "rundcopp":
            try:
                pp.rundcopp(net)
            except pp.LoadflowNotConverged:
                logger.error(
                    'The pandapower dc optimal powerflow does not converge.')
        else:
            raise ValueError("The pf_type %s is unknown" % pf_type)

    # --- prepare powerflow result comparison by reordering pp results as they are in ppc results
    if not ppc_success:
        return False
    if "opp" in pf_type:
        if not net.OPF_converged:
            return
    elif not net.converged:
        return False

    # --- store pypower powerflow results
    ppc_res = dict.fromkeys(ppc_elms)
    ppc_res["branch"] = ppc_net['branch'][:, 13:17]
    ppc_res["bus"] = ppc_net['bus'][:, 7:9]
    ppc_res["gen"] = ppc_net['gen'][:, 1:3]

    # --- pandapower bus result table
    pp_res = dict.fromkeys(ppc_elms)
    pp_res["bus"] = array(net.res_bus.sort_index()[['vm_pu', 'va_degree']])

    # --- pandapower gen result table
    pp_res["gen"] = zeros([1, 2])
    # consideration of parallel generators via storing how much generators have been considered
    # each node
    # if in ppc is only one gen -> numpy initially uses one dim array -> change to two dim array
    if len(ppc_net["gen"].shape) == 1:
        ppc_net["gen"] = array(ppc_net["gen"], ndmin=2)
    GENS = DataFrame(ppc_net['gen'][:, [0]].astype(int))
    GEN_uniq = GENS.drop_duplicates()
    already_used_gen = Series(zeros(GEN_uniq.shape[0]).astype(int),
                              index=[int(v) for v in GEN_uniq.values])
    change_q_compare = []
    for i, j in GENS.iterrows():
        current_bus_type, current_bus_idx, same_bus_gen_idx, first_same_bus_in_service_gen_idx, \
            last_same_bus_in_service_gen_idx = _gen_bus_info(ppc_net, i)
        if current_bus_type == 3 and i == first_same_bus_in_service_gen_idx:
            pp_res["gen"] = append(
                pp_res["gen"],
                array(net.res_ext_grid[net.ext_grid.bus == current_bus_idx][[
                    'p_mw', 'q_mvar'
                ]]).reshape((1, 2)), 0)
        elif current_bus_type == 2 and i == first_same_bus_in_service_gen_idx:
            pp_res["gen"] = append(
                pp_res["gen"],
                array(net.res_gen[net.gen.bus == current_bus_idx][[
                    'p_mw', 'q_mvar'
                ]]).reshape((1, 2)), 0)
        else:
            pp_res["gen"] = append(
                pp_res["gen"],
                array(net.res_sgen[net.sgen.bus == current_bus_idx][[
                    'p_mw', 'q_mvar'
                ]])[already_used_gen.at[int(j)]].reshape((1, 2)), 0)
            already_used_gen.at[int(j)] += 1
            change_q_compare += [int(j)]
    pp_res["gen"] = pp_res["gen"][1:, :]  # delete initial zero row

    # --- pandapower branch result table
    pp_res["branch"] = zeros([1, 4])
    # consideration of parallel branches via storing how often branches were considered
    # each node-to-node-connection
    try:
        init1 = concat([net.line.from_bus, net.line.to_bus], axis=1,
                       sort=True).drop_duplicates()
        init2 = concat([net.trafo.hv_bus, net.trafo.lv_bus], axis=1,
                       sort=True).drop_duplicates()
    except TypeError:
        # legacy pandas < 0.21
        init1 = concat([net.line.from_bus, net.line.to_bus],
                       axis=1).drop_duplicates()
        init2 = concat([net.trafo.hv_bus, net.trafo.lv_bus],
                       axis=1).drop_duplicates()
    init1['hv_bus'] = nan
    init1['lv_bus'] = nan
    init2['from_bus'] = nan
    init2['to_bus'] = nan
    try:
        already_used_branches = concat([init1, init2], axis=0, sort=True)
    except TypeError:
        # pandas < 0.21 legacy
        already_used_branches = concat([init1, init2], axis=0)
    already_used_branches['number'] = zeros(
        [already_used_branches.shape[0], 1]).astype(int)
    BRANCHES = DataFrame(ppc_net['branch'][:, [0, 1, 8, 9]])
    for i in BRANCHES.index:
        from_bus = pp.get_element_index(net,
                                        'bus',
                                        name=int(ppc_net['branch'][i, 0]))
        to_bus = pp.get_element_index(net,
                                      'bus',
                                      name=int(ppc_net['branch'][i, 1]))
        from_vn_kv = ppc_net['bus'][from_bus, 9]
        to_vn_kv = ppc_net['bus'][to_bus, 9]
        ratio = BRANCHES[2].at[i]
        angle = BRANCHES[3].at[i]
        # from line results
        if (from_vn_kv == to_vn_kv) & ((ratio == 0) |
                                       (ratio == 1)) & (angle == 0):
            pp_res["branch"] = append(
                pp_res["branch"],
                array(net.res_line[(net.line.from_bus == from_bus)
                                   & (net.line.to_bus == to_bus)][[
                                       'p_from_mw', 'q_from_mvar', 'p_to_mw',
                                       'q_to_mvar'
                                   ]])
                [int(already_used_branches.number.loc[
                    (already_used_branches.from_bus == from_bus) &
                    (already_used_branches.to_bus == to_bus)].values)].reshape(
                        1, 4), 0)
            already_used_branches.number.loc[
                (already_used_branches.from_bus == from_bus)
                & (already_used_branches.to_bus == to_bus)] += 1
        # from trafo results
        else:
            if from_vn_kv >= to_vn_kv:
                pp_res["branch"] = append(
                    pp_res["branch"],
                    array(net.res_trafo[(net.trafo.hv_bus == from_bus)
                                        & (net.trafo.lv_bus == to_bus)]
                          [['p_hv_mw', 'q_hv_mvar', 'p_lv_mw', 'q_lv_mvar'
                            ]])[int(already_used_branches.number.loc[
                                (already_used_branches.hv_bus == from_bus)
                                & (already_used_branches.lv_bus == to_bus)].
                                    values)].reshape(1, 4), 0)
                already_used_branches.number.loc[
                    (already_used_branches.hv_bus == from_bus)
                    & (already_used_branches.lv_bus == to_bus)] += 1
            else:  # switch hv-lv-connection of pypower connection buses
                pp_res["branch"] = append(
                    pp_res["branch"],
                    array(net.res_trafo[(net.trafo.hv_bus == to_bus)
                                        & (net.trafo.lv_bus == from_bus)]
                          [['p_lv_mw', 'q_lv_mvar', 'p_hv_mw', 'q_hv_mvar'
                            ]])[int(already_used_branches.number.loc[
                                (already_used_branches.hv_bus == to_bus)
                                & (already_used_branches.lv_bus == from_bus)].
                                    values)].reshape(1, 4), 0)
                already_used_branches.number.loc[
                    (already_used_branches.hv_bus == to_bus)
                    & (already_used_branches.lv_bus == from_bus)] += 1
    pp_res["branch"] = pp_res["branch"][1:, :]  # delete initial zero row

    # --- do the powerflow result comparison
    diff_res = dict.fromkeys(ppc_elms)
    diff_res["bus"] = ppc_res["bus"] - pp_res["bus"]
    diff_res["bus"][:, 1] -= diff_res["bus"][0, 1]  # remove va_degree offset
    diff_res["branch"] = ppc_res["branch"] - pp_res["branch"]
    diff_res["gen"] = ppc_res["gen"] - pp_res["gen"]
    # comparison of buses with several generator units only as q sum
    for i in GEN_uniq.loc[GEN_uniq[0].isin(change_q_compare)].index:
        next_is = GEN_uniq.index[GEN_uniq.index > i]
        if len(next_is) > 0:
            next_i = next_is[0]
        else:
            next_i = GENS.index[-1] + 1
        if (next_i - i) > 1:
            diff_res["gen"][i:next_i, 1] = sum(diff_res["gen"][i:next_i, 1])
    # logger info
    logger.debug(
        "Maximum voltage magnitude difference between pypower and pandapower: "
        "%.2e pu" % max_(abs(diff_res["bus"][:, 0])))
    logger.debug(
        "Maximum voltage angle difference between pypower and pandapower: "
        "%.2e degree" % max_(abs(diff_res["bus"][:, 1])))
    logger.debug(
        "Maximum branch flow active power difference between pypower and pandapower: "
        "%.2e MW" % max_(abs(diff_res["branch"][:, [0, 2]])))
    logger.debug(
        "Maximum branch flow reactive power difference between pypower and "
        "pandapower: %.2e MVAr" % max_(abs(diff_res["branch"][:, [1, 3]])))
    logger.debug(
        "Maximum active power generation difference between pypower and pandapower: "
        "%.2e MW" % max_(abs(diff_res["gen"][:, 0])))
    logger.debug(
        "Maximum reactive power generation difference between pypower and pandapower: "
        "%.2e MVAr" % max_(abs(diff_res["gen"][:, 1])))
    if _validate_diff_res(diff_res, {"bus_vm_pu": 1e-3, "bus_va_degree": 1e-3, "branch_p_mw": 1e-6,
                                     "branch_q_mvar": 1e-6}) and \
            (max_(abs(diff_res["gen"])) > 1e-1).any():
        logger.debug(
            "The active/reactive power generation difference possibly results "
            "because of a pypower error. Please validate "
            "the results via pypower loadflow."
        )  # this occurs e.g. at ppc case9
    # give a return
    if isinstance(max_diff_values, dict):
        return _validate_diff_res(diff_res, max_diff_values)
    else:
        logger.debug("'max_diff_values' must be a dict.")
コード例 #12
0
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()
コード例 #13
0
ファイル: t_opf_dc_pips_sc.py プロジェクト: redw0lf/PYPOWER
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()