Conditions | 3 |
Total Lines | 206 |
Code Lines | 102 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import logging |
||
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 | |||
250 |