|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
General description |
|
5
|
|
|
------------------- |
|
6
|
|
|
This script shows how to do a linear optimal powerflow (lopf) calculation |
|
7
|
|
|
based on custom oemof components. The example is based on the PyPSA |
|
8
|
|
|
simple lopf example. |
|
9
|
|
|
|
|
10
|
|
|
Note: As oemof currently does not support models with one timesteps, therefore |
|
11
|
|
|
there are two. |
|
12
|
|
|
|
|
13
|
|
|
Installation requirements |
|
14
|
|
|
------------------------- |
|
15
|
|
|
This example requires oemof.solph (v0.5.x), install by: |
|
16
|
|
|
|
|
17
|
|
|
pip install oemof.solph[examples] |
|
18
|
|
|
|
|
19
|
|
|
To draw the graph pygraphviz is required, installed by: |
|
20
|
|
|
|
|
21
|
|
|
pip install pygraphviz |
|
22
|
|
|
|
|
23
|
|
|
License |
|
24
|
|
|
------- |
|
25
|
|
|
Simon Hilpert - 12.12.2017 - [email protected] |
|
26
|
|
|
|
|
27
|
|
|
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_ |
|
28
|
|
|
|
|
29
|
|
|
""" |
|
30
|
|
|
import networkx as nx |
|
31
|
|
|
import pandas as pd |
|
32
|
|
|
from matplotlib import pyplot as plt |
|
33
|
|
|
from oemof.network.graph import create_nx_graph |
|
34
|
|
|
from oemof.solph import EnergySystem, Investment, Model, processing, views |
|
35
|
|
|
from oemof.solph.components import Sink, Source |
|
36
|
|
|
from oemof.solph.buses.experimental import ElectricalBus |
|
37
|
|
|
from oemof.solph.flows.experimental import ElectricalLine |
|
38
|
|
|
from oemof.solph.flows import Flow |
|
39
|
|
|
|
|
40
|
|
|
try: |
|
41
|
|
|
import pygraphviz as pygz |
|
42
|
|
|
except ModuleNotFoundError: |
|
43
|
|
|
pygz = None |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
View Code Duplication |
def draw_graph( |
|
|
|
|
|
|
47
|
|
|
grph, |
|
48
|
|
|
edge_labels=True, |
|
49
|
|
|
node_color="#AFAFAF", |
|
50
|
|
|
edge_color="#CFCFCF", |
|
51
|
|
|
plot=True, |
|
52
|
|
|
node_size=2000, |
|
53
|
|
|
with_labels=True, |
|
54
|
|
|
arrows=True, |
|
55
|
|
|
layout="neato", |
|
56
|
|
|
): |
|
57
|
|
|
""" |
|
58
|
|
|
Draw a graph. This function will be removed in future versions. |
|
59
|
|
|
|
|
60
|
|
|
Parameters |
|
61
|
|
|
---------- |
|
62
|
|
|
grph : networkxGraph |
|
63
|
|
|
A graph to draw. |
|
64
|
|
|
edge_labels : boolean |
|
65
|
|
|
Use nominal values of flow as edge label |
|
66
|
|
|
node_color : dict or string |
|
67
|
|
|
Hex color code oder matplotlib color for each node. If string, all |
|
68
|
|
|
colors are the same. |
|
69
|
|
|
|
|
70
|
|
|
edge_color : string |
|
71
|
|
|
Hex color code oder matplotlib color for edge color. |
|
72
|
|
|
|
|
73
|
|
|
plot : boolean |
|
74
|
|
|
Show matplotlib plot. |
|
75
|
|
|
|
|
76
|
|
|
node_size : integer |
|
77
|
|
|
Size of nodes. |
|
78
|
|
|
|
|
79
|
|
|
with_labels : boolean |
|
80
|
|
|
Draw node labels. |
|
81
|
|
|
|
|
82
|
|
|
arrows : boolean |
|
83
|
|
|
Draw arrows on directed edges. Works only if an optimization_model has |
|
84
|
|
|
been passed. |
|
85
|
|
|
layout : string |
|
86
|
|
|
networkx graph layout, one of: neato, dot, twopi, circo, fdp, sfdp. |
|
87
|
|
|
""" |
|
88
|
|
|
if isinstance(node_color, dict): |
|
89
|
|
|
node_color = [node_color.get(g, "#AFAFAF") for g in grph.nodes()] |
|
90
|
|
|
|
|
91
|
|
|
# set drawing options |
|
92
|
|
|
options = { |
|
93
|
|
|
# "prog": "dot", |
|
94
|
|
|
"with_labels": with_labels, |
|
95
|
|
|
"node_color": node_color, |
|
96
|
|
|
"edge_color": edge_color, |
|
97
|
|
|
"node_size": node_size, |
|
98
|
|
|
"arrows": arrows, |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
# draw graph |
|
102
|
|
|
pos = nx.drawing.nx_agraph.graphviz_layout(grph, prog=layout) |
|
103
|
|
|
|
|
104
|
|
|
nx.draw(grph, pos=pos, **options) |
|
105
|
|
|
|
|
106
|
|
|
# add edge labels for all edges |
|
107
|
|
|
if edge_labels is True and plt: |
|
108
|
|
|
labels = nx.get_edge_attributes(grph, "weight") |
|
109
|
|
|
nx.draw_networkx_edge_labels(grph, pos=pos, edge_labels=labels) |
|
110
|
|
|
|
|
111
|
|
|
# show output |
|
112
|
|
|
if plot is True: |
|
113
|
|
|
plt.show() |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
datetimeindex = pd.date_range("1/1/2017", periods=2, freq="H") |
|
117
|
|
|
|
|
118
|
|
|
es = EnergySystem(timeindex=datetimeindex) |
|
119
|
|
|
|
|
120
|
|
|
b_el0 = ElectricalBus(label="b_0", v_min=-1, v_max=1) |
|
121
|
|
|
|
|
122
|
|
|
b_el1 = ElectricalBus(label="b_1", v_min=-1, v_max=1) |
|
123
|
|
|
|
|
124
|
|
|
b_el2 = ElectricalBus(label="b_2", v_min=-1, v_max=1) |
|
125
|
|
|
|
|
126
|
|
|
es.add(b_el0, b_el1, b_el2) |
|
127
|
|
|
|
|
128
|
|
|
es.add( |
|
129
|
|
|
ElectricalLine( |
|
130
|
|
|
input=b_el0, |
|
131
|
|
|
output=b_el1, |
|
132
|
|
|
reactance=0.0001, |
|
133
|
|
|
investment=Investment(ep_costs=10), |
|
134
|
|
|
min=-1, |
|
135
|
|
|
max=1, |
|
136
|
|
|
) |
|
137
|
|
|
) |
|
138
|
|
|
|
|
139
|
|
|
es.add( |
|
140
|
|
|
ElectricalLine( |
|
141
|
|
|
input=b_el1, |
|
142
|
|
|
output=b_el2, |
|
143
|
|
|
reactance=0.0001, |
|
144
|
|
|
nominal_value=60, |
|
145
|
|
|
min=-1, |
|
146
|
|
|
max=1, |
|
147
|
|
|
) |
|
148
|
|
|
) |
|
149
|
|
|
|
|
150
|
|
|
es.add( |
|
151
|
|
|
ElectricalLine( |
|
152
|
|
|
input=b_el2, |
|
153
|
|
|
output=b_el0, |
|
154
|
|
|
reactance=0.0001, |
|
155
|
|
|
nominal_value=60, |
|
156
|
|
|
min=-1, |
|
157
|
|
|
max=1, |
|
158
|
|
|
) |
|
159
|
|
|
) |
|
160
|
|
|
|
|
161
|
|
|
es.add( |
|
162
|
|
|
Source( |
|
163
|
|
|
label="gen_0", |
|
164
|
|
|
outputs={b_el0: Flow(nominal_value=100, variable_costs=50)}, |
|
165
|
|
|
) |
|
166
|
|
|
) |
|
167
|
|
|
|
|
168
|
|
|
es.add( |
|
169
|
|
|
Source( |
|
170
|
|
|
label="gen_1", |
|
171
|
|
|
outputs={b_el1: Flow(nominal_value=100, variable_costs=25)}, |
|
172
|
|
|
) |
|
173
|
|
|
) |
|
174
|
|
|
|
|
175
|
|
|
es.add(Sink(label="load", inputs={b_el2: Flow(nominal_value=100, fix=[1, 1])})) |
|
176
|
|
|
|
|
177
|
|
|
m = Model(energysystem=es) |
|
178
|
|
|
|
|
179
|
|
|
# m.write('lopf.lp', io_options={'symbolic_solver_labels': True}) |
|
180
|
|
|
|
|
181
|
|
|
m.solve(solver="cbc", solve_kwargs={"tee": True, "keepfiles": False}) |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
m.results() |
|
185
|
|
|
|
|
186
|
|
|
graph = create_nx_graph(es) |
|
187
|
|
|
|
|
188
|
|
|
if pygz is not None: |
|
189
|
|
|
draw_graph( |
|
190
|
|
|
graph, |
|
191
|
|
|
plot=True, |
|
192
|
|
|
layout="neato", |
|
193
|
|
|
node_size=3000, |
|
194
|
|
|
node_color={"b_0": "#cd3333", "b_1": "#7EC0EE", "b_2": "#eeac7e"}, |
|
195
|
|
|
) |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
results = processing.results(m) |
|
199
|
|
|
|
|
200
|
|
|
print(views.node(results, "gen_0")["sequences"]) |
|
201
|
|
|
print(views.node(results, "gen_1")["sequences"]) |
|
202
|
|
|
print(views.node(results, "load")["sequences"]) |
|
203
|
|
|
|