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 copy
8

9 1
import numpy as np
10 1
from scipy.io import savemat
11

12 1
from pandapower.converter.pypower import to_ppc
13

14 1
try:
15 1
    import pplog as logging
16 1
except ImportError:
17 1
    import logging
18

19 1
logger = logging.getLogger(__name__)
20

21

22 1
def to_mpc(net, filename=None, **kwargs):
23
    """
24
    This function converts a pandapower net to a matpower case files (.mat) version 2.
25
    Note: python is 0-based while Matlab is 1-based.
26

27
    INPUT:
28
        **net** - The pandapower net.
29

30
    OPTIONAL:
31
        **filename** (str, None) - File path + name of the mat file which will be created. If None
32
            the mpc will only be returned
33

34
        ****kwargs** - please look at to_ppc() documentation
35

36
    EXAMPLE:
37
        import pandapower.converter as pc
38
        import pandapower.networks as pn
39
        net = pn.case9()
40
        pc.to_mpc(net, "case9.mat")
41

42
    """
43 1
    ppc = to_ppc(net, **kwargs)
44

45 1
    mpc = dict()
46 1
    mpc["mpc"] = _ppc2mpc(ppc)
47 1
    if filename is not None:
48
        # savemat
49 0
        savemat(filename, mpc)
50

51 1
    return mpc
52

53

54 1
def _ppc2mpc(ppc):
55
    """
56
    Convert network in Pypower/Matpower format
57
    Convert 0-based python to 1-based Matlab
58

59
    **INPUT**:
60
        * net - The pandapower format network
61
        * filename - File path + name of the mat file which is created
62
    """
63

64
    # convert to matpower
65
    # Matlab is one-based, so all entries (buses, lines, gens) have to start with 1 instead of 0
66 1
    mpc = copy.deepcopy(ppc)
67 1
    if len(np.where(mpc["bus"][:, 0] == 0)[0]):
68 1
        mpc["bus"][:, 0] = mpc["bus"][:, 0] + 1
69 1
        mpc["gen"][:, 0] = mpc["gen"][:, 0] + 1
70 1
        mpc["branch"][:, 0:2] = mpc["branch"][:, 0:2] + 1
71
    # adjust for the matpower converter -> taps should be 0 when there is no transformer, but are 1
72 1
    mpc["branch"][np.where(mpc["branch"][:, 8] == 1), 8] = 0
73
    # version is a string
74 1
    mpc["version"] = str(mpc["version"])
75
    # baseMVA has to be a float instead of int
76 1
    mpc["baseMVA"] = mpc["baseMVA"] * 1.0
77 1
    return mpc
78

79

80 1
if "__main__" == __name__:
81 1
    pass

Read our documentation on viewing source code .

Loading