1
# -*- coding: utf-8 -*-
2

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

6 1
import networkx as nx
7

8 1
from pandapower.topology.create_graph import get_edge_table, add_edges, init_par
9

10 1
try:
11 1
    import pplog as logging
12 1
except ImportError:
13 1
    import logging
14

15 1
INDEX = 0
16 1
F_JUNCTION = 1
17 1
T_JUNCTION = 2
18

19 1
WEIGHT = 0
20

21 1
logger = logging.getLogger(__name__)
22

23

24 1
def create_nxgraph(net, include_pipes=True, include_valves=True, include_pumps=True,
25
                   nogojunctions=None, notravjunctions=None, multi=True,
26
                   include_out_of_service=False):
27

28 1
    if multi:
29 1
        mg = nx.MultiGraph()
30
    else:
31 0
        mg = nx.Graph()
32

33 1
    if hasattr(net, "pipe"):
34 1
        pipe = get_edge_table(net, "pipe", include_pipes)
35 1
        if pipe is not None:
36 1
            indices, parameter, in_service = init_par(pipe)
37 1
            indices[:, F_JUNCTION] = pipe.from_junction.values
38 1
            indices[:, T_JUNCTION] = pipe.to_junction.values
39 1
            parameter[:, WEIGHT] = pipe.length_km.values
40 1
            add_edges(mg, indices, parameter, in_service, net, "pipe")
41

42 1
    if hasattr(net, "valve"):
43 1
        valve = get_edge_table(net, "valve", include_valves)
44 1
        if valve is not None:
45 1
            indices, parameter, in_service = init_par(valve)
46 1
            indices[:, F_JUNCTION] = valve.from_junction.values
47 1
            indices[:, T_JUNCTION] = valve.to_junction.values
48 1
            add_edges(mg, indices, parameter, in_service, net, "valve")
49

50 1
    if hasattr(net, "pump"):
51 1
        pump = get_edge_table(net, "pump", include_pumps)
52 1
        if pump is not None:
53 1
            indices, parameter, in_service = init_par(pump)
54 1
            indices[:, F_JUNCTION] = pump.from_junction.values
55 1
            indices[:, T_JUNCTION] = pump.to_junction.values
56 1
            add_edges(mg, indices, parameter, in_service, net, "pump")
57

58
    # add all junctions that were not added when creating branches
59 1
    if len(mg.nodes()) < len(net.junction.index):
60 1
        for b in set(net.junction.index) - set(mg.nodes()):
61 1
            mg.add_node(b)
62

63
    # remove nogojunctions
64 1
    if nogojunctions is not None:
65 0
        for b in nogojunctions:
66 0
            mg.remove_node(b)
67

68
    # remove the edges pointing away of notravjunctions
69 1
    if notravjunctions is not None:
70 0
        for b in notravjunctions:
71 0
            for i in list(mg[b].keys()):
72 0
                try:
73 0
                    del mg[b][i]  # networkx versions < 2.0
74 0
                except:
75 0
                    del mg._adj[b][i]  # networkx versions 2.0
76

77
    # remove out of service junctions
78 1
    if not include_out_of_service:
79 1
        for b in net.junction.index[~net.junction.in_service.values]:
80 0
            mg.remove_node(b)
81

82 1
    return mg

Read our documentation on viewing source code .

Loading