| Conditions | 3 |
| Total Lines | 210 |
| Code Lines | 106 |
| 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 |
||
| 69 | def main(): |
||
| 70 | # For models that need a long time to optimise, saving and loading the |
||
| 71 | # EnergySystem might be advised. By default, we do not do this here. Feel |
||
| 72 | # free to experiment with this once you understood the rest of the code. |
||
| 73 | |||
| 74 | # ************************************************************************* |
||
| 75 | # ********** PART 1 - Define and optimise the energy system *************** |
||
| 76 | # ************************************************************************* |
||
| 77 | |||
| 78 | # Read data file |
||
| 79 | file_name = "subnetwork_example.csv" |
||
| 80 | data = get_data_from_file_path(file_name) |
||
| 81 | |||
| 82 | solver = "cbc" # 'glpk', 'gurobi',.... |
||
| 83 | debug = False # Set number_of_timesteps to 3 to get a readable lp-file. |
||
| 84 | number_of_time_steps = len(data) |
||
| 85 | solver_verbose = False # show/hide solver output |
||
| 86 | |||
| 87 | # initiate the logger (see the API docs for more information) |
||
| 88 | logger.define_logging( |
||
| 89 | logfile="oemof_example.log", |
||
| 90 | screen_level=logging.INFO, |
||
| 91 | file_level=logging.INFO, |
||
| 92 | ) |
||
| 93 | |||
| 94 | logging.info("Initialize the energy system") |
||
| 95 | date_time_index = create_time_index(2012, number=number_of_time_steps) |
||
| 96 | |||
| 97 | # create the energysystem and assign the time index |
||
| 98 | energysystem = EnergySystem( |
||
| 99 | timeindex=date_time_index, infer_last_interval=False |
||
| 100 | ) |
||
| 101 | |||
| 102 | ########################################################################## |
||
| 103 | # Create oemof objects |
||
| 104 | ########################################################################## |
||
| 105 | |||
| 106 | logging.info("Create oemof objects") |
||
| 107 | |||
| 108 | # The bus objects were assigned to variables which makes it easier to |
||
| 109 | # connect components to these buses (see below). |
||
| 110 | |||
| 111 | # create natural gas bus |
||
| 112 | bus_gas = buses.Bus(label=HierachicalLabel("natural_gas")) |
||
| 113 | |||
| 114 | # create electricity bus |
||
| 115 | bus_electricity = buses.Bus(label=HierachicalLabel("electricity")) |
||
| 116 | |||
| 117 | # adding the buses to the energy system |
||
| 118 | energysystem.add(bus_gas, bus_electricity) |
||
| 119 | |||
| 120 | # create excess component for the electricity bus to allow overproduction |
||
| 121 | energysystem.add( |
||
| 122 | components.Sink( |
||
| 123 | label="excess_bus_electricity", |
||
| 124 | inputs={bus_electricity: flows.Flow()}, |
||
| 125 | ) |
||
| 126 | ) |
||
| 127 | |||
| 128 | # create source object representing the gas commodity |
||
| 129 | energysystem.add( |
||
| 130 | components.Source( |
||
| 131 | label="rgas", |
||
| 132 | outputs={bus_gas: flows.Flow()}, |
||
| 133 | ) |
||
| 134 | ) |
||
| 135 | |||
| 136 | # *** SUB-NETWORK *************************** |
||
| 137 | # Add a subnetwork for Renewable Energies. This not a Facade it just meant |
||
| 138 | # to group components |
||
| 139 | renewables = SubNetwork("renewables") |
||
| 140 | re_bus = renewables.subnode(buses.Bus, "re_elec") |
||
| 141 | |||
| 142 | # create fixed source object representing wind power plants |
||
| 143 | renewables.subnode( |
||
| 144 | Volatile, |
||
| 145 | label="wind", |
||
| 146 | output_bus=re_bus, |
||
| 147 | timeseries=data["wind"], |
||
| 148 | nominal_capacity=1000000, |
||
| 149 | ) |
||
| 150 | # create fixed source object representing pv power plants |
||
| 151 | renewables.subnode( |
||
| 152 | Volatile, |
||
| 153 | label="pv", |
||
| 154 | output_bus=re_bus, |
||
| 155 | timeseries=data["pv"], |
||
| 156 | nominal_capacity=582000, |
||
| 157 | ) |
||
| 158 | renewables.subnode( |
||
| 159 | components.Converter, |
||
| 160 | label="connection", |
||
| 161 | outputs={bus_electricity: flows.Flow()}, |
||
| 162 | inputs={re_bus: flows.Flow()}, |
||
| 163 | ) |
||
| 164 | energysystem.add(renewables) # Subnetwork to Energysystem |
||
| 165 | # ************************************************************* |
||
| 166 | |||
| 167 | # create simple sink object representing the electrical demand |
||
| 168 | # nominal_value is set to 1 because demand_el is not a normalised series |
||
| 169 | energysystem.add( |
||
| 170 | components.Sink( |
||
| 171 | label="demand", |
||
| 172 | inputs={ |
||
| 173 | bus_electricity: flows.Flow( |
||
| 174 | fix=data["demand_el"], nominal_capacity=1 |
||
| 175 | ) |
||
| 176 | }, |
||
| 177 | ) |
||
| 178 | ) |
||
| 179 | |||
| 180 | # create simple converter object representing a gas power plant |
||
| 181 | energysystem.add( |
||
| 182 | components.Converter( |
||
| 183 | label="pp_gas", |
||
| 184 | inputs={bus_gas: flows.Flow()}, |
||
| 185 | outputs={ |
||
| 186 | bus_electricity: flows.Flow( |
||
| 187 | nominal_capacity=10e10, variable_costs=50 |
||
| 188 | ) |
||
| 189 | }, |
||
| 190 | conversion_factors={bus_electricity: 0.58}, |
||
| 191 | ) |
||
| 192 | ) |
||
| 193 | |||
| 194 | # create storage object representing a battery |
||
| 195 | nominal_capacity = 10077997 |
||
| 196 | nominal_value = nominal_capacity / 6 |
||
| 197 | |||
| 198 | battery_storage = components.GenericStorage( |
||
| 199 | nominal_capacity=nominal_capacity, |
||
| 200 | label=STORAGE_LABEL, |
||
| 201 | inputs={bus_electricity: flows.Flow(nominal_capacity=nominal_value)}, |
||
| 202 | outputs={ |
||
| 203 | bus_electricity: flows.Flow( |
||
| 204 | nominal_capacity=nominal_value, variable_costs=0.001 |
||
| 205 | ) |
||
| 206 | }, |
||
| 207 | loss_rate=0.00, |
||
| 208 | initial_storage_level=None, |
||
| 209 | inflow_conversion_factor=1, |
||
| 210 | outflow_conversion_factor=0.8, |
||
| 211 | ) |
||
| 212 | |||
| 213 | energysystem.add(battery_storage) |
||
| 214 | |||
| 215 | ########################################################################## |
||
| 216 | # Optimise the energy system and plot the results |
||
| 217 | ########################################################################## |
||
| 218 | |||
| 219 | logging.info("Optimise the energy system") |
||
| 220 | |||
| 221 | # initialise the operational model |
||
| 222 | energysystem_model = Model(energysystem) |
||
| 223 | |||
| 224 | # This is for debugging only. It is not(!) necessary to solve the problem |
||
| 225 | # and should be set to False to save time and disc space in normal use. For |
||
| 226 | # debugging the timesteps should be set to 3, to increase the readability |
||
| 227 | # of the lp-file. |
||
| 228 | if debug: |
||
| 229 | file_path = os.path.join( |
||
| 230 | helpers.extend_basic_path("lp_files"), "basic_example.lp" |
||
| 231 | ) |
||
| 232 | logging.info(f"Store lp-file in {file_path}.") |
||
| 233 | io_option = {"symbolic_solver_labels": True} |
||
| 234 | energysystem_model.write(file_path, io_options=io_option) |
||
| 235 | |||
| 236 | # if tee_switch is true solver messages will be displayed |
||
| 237 | logging.info("Solve the optimization problem") |
||
| 238 | energysystem_model.solve( |
||
| 239 | solver=solver, solve_kwargs={"tee": solver_verbose} |
||
| 240 | ) |
||
| 241 | |||
| 242 | results = Results(energysystem_model) |
||
| 243 | |||
| 244 | # ToDO Implement a filter methode for the Result object to exclude |
||
| 245 | # subcomponents of a facade/sub-network |
||
| 246 | # The following lines are meant to show how the result should look like |
||
| 247 | # in case the subcomponents should be exclude. There should not be a |
||
| 248 | # postprocessing it is better to filter the nodes directly |
||
| 249 | |||
| 250 | # Filter columns that are internal only |
||
| 251 | keep_columns = [ |
||
| 252 | c |
||
| 253 | for c in results.flow.columns |
||
| 254 | if getattr(c[1].label, "parent", None) |
||
| 255 | != getattr(c[0].label, "parent", None) |
||
| 256 | or ( |
||
| 257 | getattr(c[0].label, "parent", True) is True |
||
| 258 | and getattr(c[1].label, "parent", True) is True |
||
| 259 | ) |
||
| 260 | ] |
||
| 261 | flow_results_filtered = results.flow[keep_columns].copy() |
||
| 262 | |||
| 263 | # Replace subcomponent with facade object |
||
| 264 | for level in [0, 1]: |
||
| 265 | flow_results_filtered.rename( |
||
| 266 | columns={ |
||
| 267 | c[level]: getattr(c[level].label, "parent", c[level]) |
||
| 268 | for c in flow_results_filtered.columns |
||
| 269 | }, |
||
| 270 | level=level, |
||
| 271 | inplace=True, |
||
| 272 | ) |
||
| 273 | |||
| 274 | print("**** All results ****") |
||
| 275 | print(results.flow.sum()) |
||
| 276 | |||
| 277 | print("**** Filtered results ****") |
||
| 278 | print(flow_results_filtered.sum()) |
||
| 279 | |||
| 283 |