1
|
|
|
import logging |
2
|
|
|
import os |
3
|
|
|
|
4
|
|
|
import matplotlib.pyplot as plt |
5
|
|
|
import pandas as pd |
6
|
|
|
from oemof.tools import logger |
7
|
|
|
from oemof.network import SubNetwork |
8
|
|
|
|
9
|
|
|
from oemof.solph import EnergySystem |
10
|
|
|
from oemof.solph import Model |
11
|
|
|
from oemof.solph import buses |
12
|
|
|
from oemof.solph import components |
13
|
|
|
from oemof.solph import create_time_index |
14
|
|
|
from oemof.solph import flows |
15
|
|
|
from oemof.solph import helpers |
16
|
|
|
from oemof.solph import Results |
17
|
|
|
|
18
|
|
|
STORAGE_LABEL = "battery_storage" |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
def get_data_from_file_path(file_path: str) -> pd.DataFrame: |
22
|
|
|
file_dir = os.path.dirname(os.path.abspath(__file__)) |
23
|
|
|
data = pd.read_csv(file_dir + "/" + file_path) |
24
|
|
|
return data |
25
|
|
|
|
26
|
|
|
|
27
|
|
View Code Duplication |
def plot_figures_for(element: dict) -> None: |
|
|
|
|
28
|
|
|
figure, axes = plt.subplots(figsize=(10, 5)) |
29
|
|
|
element["sequences"].plot(ax=axes, kind="line", drawstyle="steps-post") |
30
|
|
|
plt.legend( |
31
|
|
|
loc="upper center", |
32
|
|
|
prop={"size": 8}, |
33
|
|
|
bbox_to_anchor=(0.5, 1.25), |
34
|
|
|
ncol=2, |
35
|
|
|
) |
36
|
|
|
figure.subplots_adjust(top=0.8) |
37
|
|
|
plt.show() |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def main(): |
41
|
|
|
# For models that need a long time to optimise, saving and loading the |
42
|
|
|
# EnergySystem might be advised. By default, we do not do this here. Feel |
43
|
|
|
# free to experiment with this once you understood the rest of the code. |
44
|
|
|
|
45
|
|
|
# ************************************************************************* |
46
|
|
|
# ********** PART 1 - Define and optimise the energy system *************** |
47
|
|
|
# ************************************************************************* |
48
|
|
|
|
49
|
|
|
# Read data file |
50
|
|
|
file_name = "subnetwork_example.csv" |
51
|
|
|
data = get_data_from_file_path(file_name) |
52
|
|
|
|
53
|
|
|
solver = "cbc" # 'glpk', 'gurobi',.... |
54
|
|
|
debug = False # Set number_of_timesteps to 3 to get a readable lp-file. |
55
|
|
|
number_of_time_steps = len(data) |
56
|
|
|
solver_verbose = False # show/hide solver output |
57
|
|
|
|
58
|
|
|
# initiate the logger (see the API docs for more information) |
59
|
|
|
logger.define_logging( |
60
|
|
|
logfile="oemof_example.log", |
61
|
|
|
screen_level=logging.INFO, |
62
|
|
|
file_level=logging.INFO, |
63
|
|
|
) |
64
|
|
|
|
65
|
|
|
logging.info("Initialize the energy system") |
66
|
|
|
date_time_index = create_time_index(2012, number=number_of_time_steps) |
67
|
|
|
|
68
|
|
|
# create the energysystem and assign the time index |
69
|
|
|
energysystem = EnergySystem( |
70
|
|
|
timeindex=date_time_index, infer_last_interval=False |
71
|
|
|
) |
72
|
|
|
|
73
|
|
|
########################################################################## |
74
|
|
|
# Create oemof objects |
75
|
|
|
########################################################################## |
76
|
|
|
|
77
|
|
|
logging.info("Create oemof objects") |
78
|
|
|
|
79
|
|
|
# The bus objects were assigned to variables which makes it easier to |
80
|
|
|
# connect components to these buses (see below). |
81
|
|
|
|
82
|
|
|
# create natural gas bus |
83
|
|
|
bus_gas = buses.Bus(label="natural_gas") |
84
|
|
|
|
85
|
|
|
# create electricity bus |
86
|
|
|
bus_electricity = buses.Bus(label="electricity") |
87
|
|
|
|
88
|
|
|
# adding the buses to the energy system |
89
|
|
|
energysystem.add(bus_gas, bus_electricity) |
90
|
|
|
|
91
|
|
|
# create excess component for the electricity bus to allow overproduction |
92
|
|
|
energysystem.add( |
93
|
|
|
components.Sink( |
94
|
|
|
label="excess_bus_electricity", |
95
|
|
|
inputs={bus_electricity: flows.Flow()}, |
96
|
|
|
) |
97
|
|
|
) |
98
|
|
|
|
99
|
|
|
# create source object representing the gas commodity |
100
|
|
|
energysystem.add( |
101
|
|
|
components.Source( |
102
|
|
|
label="rgas", |
103
|
|
|
outputs={bus_gas: flows.Flow()}, |
104
|
|
|
) |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
# *** SUB-NETWORK *************************** |
108
|
|
|
# Add a subnetwork for Renewable Energies. This not a Facade it just meant |
109
|
|
|
# to group components |
110
|
|
|
renewables = SubNetwork("renewables") |
111
|
|
|
re_bus = renewables.subnode(buses.Bus, "re_elec") |
112
|
|
|
|
113
|
|
|
# create fixed source object representing wind power plants |
114
|
|
|
renewables.subnode( |
115
|
|
|
components.Source, |
116
|
|
|
label="wind", |
117
|
|
|
outputs={re_bus: flows.Flow(fix=data["wind"], nominal_value=1000000)}, |
118
|
|
|
) |
119
|
|
|
# create fixed source object representing pv power plants |
120
|
|
|
renewables.subnode( |
121
|
|
|
components.Source, |
122
|
|
|
label="pv", |
123
|
|
|
outputs={re_bus: flows.Flow(fix=data["pv"], nominal_value=582000)}, |
124
|
|
|
) |
125
|
|
|
renewables.subnode( |
126
|
|
|
components.Converter, |
127
|
|
|
label="connection", |
128
|
|
|
outputs={bus_electricity: flows.Flow()}, |
129
|
|
|
inputs={re_bus: flows.Flow()}, |
130
|
|
|
) |
131
|
|
|
energysystem.add(renewables) # Subnetwork to Energysystem |
132
|
|
|
# ************************************************************* |
133
|
|
|
|
134
|
|
|
# create simple sink object representing the electrical demand |
135
|
|
|
# nominal_value is set to 1 because demand_el is not a normalised series |
136
|
|
|
energysystem.add( |
137
|
|
|
components.Sink( |
138
|
|
|
label="demand", |
139
|
|
|
inputs={ |
140
|
|
|
bus_electricity: flows.Flow( |
141
|
|
|
fix=data["demand_el"], nominal_value=1 |
142
|
|
|
) |
143
|
|
|
}, |
144
|
|
|
) |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
# create simple converter object representing a gas power plant |
148
|
|
|
energysystem.add( |
149
|
|
|
components.Converter( |
150
|
|
|
label="pp_gas", |
151
|
|
|
inputs={bus_gas: flows.Flow()}, |
152
|
|
|
outputs={ |
153
|
|
|
bus_electricity: flows.Flow( |
154
|
|
|
nominal_capacity=10e10, variable_costs=50 |
155
|
|
|
) |
156
|
|
|
}, |
157
|
|
|
conversion_factors={bus_electricity: 0.58}, |
158
|
|
|
) |
159
|
|
|
) |
160
|
|
|
|
161
|
|
|
# create storage object representing a battery |
162
|
|
|
nominal_capacity = 10077997 |
163
|
|
|
nominal_value = nominal_capacity / 6 |
164
|
|
|
|
165
|
|
|
battery_storage = components.GenericStorage( |
166
|
|
|
nominal_capacity=nominal_capacity, |
167
|
|
|
label=STORAGE_LABEL, |
168
|
|
|
inputs={bus_electricity: flows.Flow(nominal_value=nominal_value)}, |
169
|
|
|
outputs={ |
170
|
|
|
bus_electricity: flows.Flow( |
171
|
|
|
nominal_value=nominal_value, variable_costs=0.001 |
172
|
|
|
) |
173
|
|
|
}, |
174
|
|
|
loss_rate=0.00, |
175
|
|
|
initial_storage_level=None, |
176
|
|
|
inflow_conversion_factor=1, |
177
|
|
|
outflow_conversion_factor=0.8, |
178
|
|
|
) |
179
|
|
|
|
180
|
|
|
energysystem.add(battery_storage) |
181
|
|
|
|
182
|
|
|
########################################################################## |
183
|
|
|
# Optimise the energy system and plot the results |
184
|
|
|
########################################################################## |
185
|
|
|
|
186
|
|
|
logging.info("Optimise the energy system") |
187
|
|
|
|
188
|
|
|
# initialise the operational model |
189
|
|
|
energysystem_model = Model(energysystem) |
190
|
|
|
|
191
|
|
|
# This is for debugging only. It is not(!) necessary to solve the problem |
192
|
|
|
# and should be set to False to save time and disc space in normal use. For |
193
|
|
|
# debugging the timesteps should be set to 3, to increase the readability |
194
|
|
|
# of the lp-file. |
195
|
|
|
if debug: |
196
|
|
|
file_path = os.path.join( |
197
|
|
|
helpers.extend_basic_path("lp_files"), "basic_example.lp" |
198
|
|
|
) |
199
|
|
|
logging.info(f"Store lp-file in {file_path}.") |
200
|
|
|
io_option = {"symbolic_solver_labels": True} |
201
|
|
|
energysystem_model.write(file_path, io_options=io_option) |
202
|
|
|
|
203
|
|
|
# if tee_switch is true solver messages will be displayed |
204
|
|
|
logging.info("Solve the optimization problem") |
205
|
|
|
energysystem_model.solve( |
206
|
|
|
solver=solver, solve_kwargs={"tee": solver_verbose} |
207
|
|
|
) |
208
|
|
|
|
209
|
|
|
results = Results(energysystem_model) |
210
|
|
|
|
211
|
|
|
# ToDO Implement a filter methode for the Result object to exclude |
212
|
|
|
# subcomponents of a facade/sub-network |
213
|
|
|
# The following lines are meant to show how the result should look like |
214
|
|
|
# in case the subcomponents should be exclude. There should not be a |
215
|
|
|
# postprocessing it is better to filter the nodes directly |
216
|
|
|
|
217
|
|
|
# Filter columns that are internal only |
218
|
|
|
keep_columns = [ |
219
|
|
|
c |
220
|
|
|
for c in results.flow.columns |
221
|
|
|
if getattr(c[1].label, "parent", None) |
222
|
|
|
!= getattr(c[0].label, "parent", None) |
223
|
|
|
or ( |
224
|
|
|
getattr(c[0].label, "parent", True) is True |
225
|
|
|
and getattr(c[1].label, "parent", True) is True |
226
|
|
|
) |
227
|
|
|
] |
228
|
|
|
flow_results_filtered = results.flow[keep_columns].copy() |
229
|
|
|
|
230
|
|
|
# Replace subcomponent with facade object |
231
|
|
|
for level in [0, 1]: |
232
|
|
|
flow_results_filtered.rename( |
233
|
|
|
columns={ |
234
|
|
|
c[level]: getattr(c[level].label, "parent", c[level]) |
235
|
|
|
for c in flow_results_filtered.columns |
236
|
|
|
}, |
237
|
|
|
level=level, |
238
|
|
|
inplace=True, |
239
|
|
|
) |
240
|
|
|
|
241
|
|
|
print("**** All results ****") |
242
|
|
|
print(results.flow.sum()) |
243
|
|
|
|
244
|
|
|
print("**** Filtered results ****") |
245
|
|
|
print(flow_results_filtered.sum()) |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
if __name__ == "__main__": |
249
|
|
|
main() |
250
|
|
|
|