| Conditions | 1 |
| Total Lines | 159 |
| Code Lines | 78 |
| 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 | # -*- coding: utf-8 -*- |
||
| 49 | def main(): |
||
| 50 | |||
| 51 | # ************************************************************************* |
||
| 52 | # ********** PART 1 - Define and optimise the energy system *************** |
||
| 53 | # ************************************************************************* |
||
| 54 | |||
| 55 | # Read data file |
||
| 56 | file_name = os.path.realpath( |
||
| 57 | os.path.join( |
||
| 58 | __file__, |
||
| 59 | "..", |
||
| 60 | "..", |
||
| 61 | "..", |
||
| 62 | "..", |
||
| 63 | r"examples\result_object\time_series.csv", |
||
| 64 | ) |
||
| 65 | ) |
||
| 66 | data = pd.read_csv(file_name) |
||
| 67 | |||
| 68 | solver = "cbc" # 'glpk', 'gurobi',.... |
||
| 69 | number_of_time_steps = len(data) |
||
| 70 | solver_verbose = False # show/hide solver output |
||
| 71 | |||
| 72 | # initiate the logger (see the API docs for more information) |
||
| 73 | logger.define_logging( |
||
| 74 | logfile="oemof_example.log", |
||
| 75 | screen_level=logging.INFO, |
||
| 76 | file_level=logging.INFO, |
||
| 77 | ) |
||
| 78 | |||
| 79 | logging.info("Initialize the energy system") |
||
| 80 | date_time_index = create_time_index(2012, number=number_of_time_steps) |
||
| 81 | |||
| 82 | # create the energysystem and assign the time index |
||
| 83 | energysystem = EnergySystem( |
||
| 84 | timeindex=date_time_index, infer_last_interval=False |
||
| 85 | ) |
||
| 86 | |||
| 87 | ########################################################################## |
||
| 88 | # Create oemof objects |
||
| 89 | ########################################################################## |
||
| 90 | |||
| 91 | logging.info("Create oemof objects") |
||
| 92 | |||
| 93 | # The bus objects were assigned to variables which makes it easier to |
||
| 94 | # connect components to these buses (see below). |
||
| 95 | |||
| 96 | # create natural gas bus |
||
| 97 | bus_gas = buses.Bus(label="natural gas") |
||
| 98 | |||
| 99 | # create electricity bus |
||
| 100 | bus_electricity = buses.Bus(label="electricity") |
||
| 101 | |||
| 102 | # create heat bus |
||
| 103 | bus_heat = buses.Bus(label="heat") |
||
| 104 | |||
| 105 | # adding the buses to the energy system |
||
| 106 | energysystem.add(bus_gas, bus_electricity, bus_heat) |
||
| 107 | |||
| 108 | # create sink for heat demand |
||
| 109 | energysystem.add( |
||
| 110 | components.Sink( |
||
| 111 | label="Heat Demand", |
||
| 112 | inputs={ |
||
| 113 | bus_heat: flows.Flow(fix=data["demand_el"], nominal_value=2) |
||
| 114 | }, |
||
| 115 | ) |
||
| 116 | ) |
||
| 117 | |||
| 118 | # create source object representing the gas commodity |
||
| 119 | energysystem.add( |
||
| 120 | components.Source( |
||
| 121 | label="Gas Grid", |
||
| 122 | outputs={bus_gas: flows.Flow()}, |
||
| 123 | ) |
||
| 124 | ) |
||
| 125 | |||
| 126 | # create fixed source object representing power grid |
||
| 127 | energysystem.add( |
||
| 128 | components.Source( |
||
| 129 | label="Power Grid", |
||
| 130 | outputs={bus_electricity: flows.Flow()}, |
||
| 131 | ) |
||
| 132 | ) |
||
| 133 | |||
| 134 | # create fixed source object representing pv power plants |
||
| 135 | energysystem.add( |
||
| 136 | components.Source( |
||
| 137 | label="Pv Plant", |
||
| 138 | outputs={ |
||
| 139 | bus_electricity: flows.Flow( |
||
| 140 | fix=data["pv"], nominal_value=582000 |
||
| 141 | ) |
||
| 142 | }, |
||
| 143 | ) |
||
| 144 | ) |
||
| 145 | |||
| 146 | # create simple sink object representing the electrical demand |
||
| 147 | # nominal_capacity is set to 1 because demand_el is not a normalised series |
||
| 148 | energysystem.add( |
||
| 149 | components.Sink( |
||
| 150 | label="Electricity Demand", |
||
| 151 | inputs={ |
||
| 152 | bus_electricity: flows.Flow( |
||
| 153 | fix=data["demand_el"], nominal_value=1 |
||
| 154 | ) |
||
| 155 | }, |
||
| 156 | ) |
||
| 157 | ) |
||
| 158 | |||
| 159 | # create simple converter object representing a gas boiler |
||
| 160 | energysystem.add( |
||
| 161 | components.Converter( |
||
| 162 | label="Gas boiler", |
||
| 163 | inputs={bus_gas: flows.Flow()}, |
||
| 164 | outputs={bus_heat: flows.Flow(variable_costs=50)}, |
||
| 165 | conversion_factors={bus_electricity: 0.58}, |
||
| 166 | ) |
||
| 167 | ) |
||
| 168 | |||
| 169 | # create simple converter object representing a heatpump |
||
| 170 | energysystem.add( |
||
| 171 | components.Converter( |
||
| 172 | label="Heatpump", |
||
| 173 | inputs={bus_electricity: flows.Flow()}, |
||
| 174 | outputs={bus_heat: flows.Flow(variable_costs=50)}, |
||
| 175 | conversion_factors={bus_electricity: 0.58}, |
||
| 176 | ) |
||
| 177 | ) |
||
| 178 | |||
| 179 | ########################################################################## |
||
| 180 | # Optimise the energy system and plot the results |
||
| 181 | ########################################################################## |
||
| 182 | |||
| 183 | logging.info("Optimise the energy system") |
||
| 184 | |||
| 185 | # initialise the operational model |
||
| 186 | energysystem_model = Model(energysystem) |
||
| 187 | |||
| 188 | esgr = ESGraphRenderer( |
||
| 189 | energysystem_model, |
||
| 190 | legend=False, |
||
| 191 | filepath=os.path.realpath( |
||
| 192 | os.path.join(__file__, "..", "..", "ES.svg") |
||
| 193 | ), |
||
| 194 | img_format="svg", |
||
| 195 | ) |
||
| 196 | esgr.render() |
||
| 197 | |||
| 198 | # if tee_switch is true solver messages will be displayed |
||
| 199 | logging.info("Solve the optimization problem") |
||
| 200 | energysystem_model.solve( |
||
| 201 | solver=solver, solve_kwargs={"tee": solver_verbose} |
||
| 202 | ) |
||
| 203 | |||
| 204 | # after the solve method of the model has been called |
||
| 205 | results = processing.results(energysystem_model) |
||
| 206 | fig_dict = esgr.sankey(results) |
||
| 207 | pio.show(fig_dict) |
||
| 208 | |||
| 211 |