e2nIEE / pandapower
1
# -*- coding: utf-8 -*-
2

3
# Copyright (c) 2016-2021 by University of Kassel and Fraunhofer Institute for Energy Economics
4
# and Energy System Technology (IEE), Kassel. All rights reserved.
5

6

7 1
import warnings
8 1
from sys import stdout
9

10 1
from pandapower.pypower.add_userfcn import add_userfcn
11 1
from pandapower.pypower.ppoption import ppoption
12 1
from scipy.sparse import csr_matrix as sparse
13

14 1
from pandapower.auxiliary import ppException, _clean_up, _add_auxiliary_elements
15 1
from pandapower.pypower.idx_bus import VM
16 1
from pandapower.pypower.opf import opf
17 1
from pandapower.pypower.printpf import printpf
18 1
from pandapower.pd2ppc import _pd2ppc
19 1
from pandapower.pf.run_newton_raphson_pf import _run_newton_raphson_pf
20 1
from pandapower.results import _copy_results_ppci_to_ppc, init_results, \
21
    _extract_results
22

23

24 1
class OPFNotConverged(ppException):
25
    """
26
    Exception being raised in case optimal powerflow did not converge.
27
    """
28 1
    pass
29

30

31 1
def _optimal_powerflow(net, verbose, suppress_warnings, **kwargs):
32 1
    ac = net["_options"]["ac"]
33 1
    init = net["_options"]["init"]
34

35 1
    ppopt = ppoption(VERBOSE=verbose, OPF_FLOW_LIM=2, PF_DC=not ac, INIT=init, **kwargs)
36 1
    net["OPF_converged"] = False
37 1
    net["converged"] = False
38 1
    _add_auxiliary_elements(net)
39 1
    init_results(net, "opf")
40

41 1
    ppc, ppci = _pd2ppc(net)
42

43 1
    if not ac:
44 1
        ppci["bus"][:, VM] = 1.0
45 1
    net["_ppc_opf"] = ppci
46 1
    if len(net.dcline) > 0:
47 1
        ppci = add_userfcn(ppci, 'formulation', _add_dcline_constraints, args=net)
48

49 1
    if init == "pf":
50 1
        ppci = _run_pf_before_opf(net, ppci)
51 1
    if suppress_warnings:
52 1
        with warnings.catch_warnings():
53 1
            warnings.simplefilter("ignore")
54 1
            result = opf(ppci, ppopt)
55
    else:
56 0
        result = opf(ppci, ppopt)
57
#    net["_ppc_opf"] = result
58

59 1
    if verbose:
60 0
        ppopt['OUT_ALL'] = 1
61 0
        printpf(baseMVA=result["baseMVA"], bus=result["bus"], gen=result["gen"], fd=stdout,
62
                branch=result["branch"],  success=result["success"], et=result["et"], ppopt=ppopt)
63

64 1
    if verbose:
65 0
        ppopt['OUT_ALL'] = 1
66 0
        printpf(baseMVA=result["baseMVA"], bus=result["bus"], gen=result["gen"], fd=stdout,
67
                branch=result["branch"],  success=result["success"], et=result["et"], ppopt=ppopt)
68

69 1
    if not result["success"]:
70 1
        raise OPFNotConverged("Optimal Power Flow did not converge!")
71

72
    # ppci doesn't contain out of service elements, but ppc does -> copy results accordingly
73 1
    mode = net["_options"]["mode"]
74 1
    result = _copy_results_ppci_to_ppc(result, ppc, mode=mode)
75

76
#    net["_ppc_opf"] = result
77 1
    net["OPF_converged"] = True
78 1
    _extract_results(net, result)
79 1
    _clean_up(net)
80

81

82 1
def _add_dcline_constraints(om, net):
83
    # from numpy import hstack, diag, eye, zeros
84 1
    ppc = om.get_ppc()
85 1
    ndc = net.dcline.in_service.sum()  ## number of in-service DC lines
86 1
    if ndc > 0:
87 1
        ng = ppc['gen'].shape[0]  ## number of total gens
88 1
        Adc = sparse((ndc, ng))
89 1
        gen_lookup = net._pd2ppc_lookups["gen"]
90

91 1
        dcline_gens_from = net.gen.index[-2 * ndc::2]
92 1
        dcline_gens_to = net.gen.index[-2 * ndc + 1::2]
93 1
        for i, (f, t, loss, active) in enumerate(zip(dcline_gens_from, dcline_gens_to,
94
                                                     net.dcline.loss_percent.values,
95
                                                     net.dcline.in_service.values)):
96 1
            if active:
97 1
                Adc[i, gen_lookup[f]] = 1. + loss / 100
98 1
                Adc[i, gen_lookup[t]] = 1.
99

100
        ## constraints
101 1
        nL0 = -net.dcline.loss_mw.values # absolute losses
102
        #    L1  = -net.dcline.loss_percent.values * 1e-2 #relative losses
103
        #    Adc = sparse(hstack([zeros((ndc, ng)), diag(1-L1), eye(ndc)]))
104

105
        ## add them to the model
106 1
        om = om.add_constraints('dcline', Adc, nL0, nL0, ['Pg'])
107

108

109 1
def _run_pf_before_opf(net, ppci):
110
#    net._options["numba"] = True
111 1
    net._options["tolerance_mva"] = 1e-8
112 1
    net._options["max_iteration"] = 10
113 1
    net._options["algorithm"] = "nr"
114 1
    return _run_newton_raphson_pf(ppci, net["_options"])

Read our documentation on viewing source code .

Loading