| Conditions | 2 |
| Total Lines | 152 |
| Code Lines | 76 |
| 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 -*- |
||
| 79 | def main(): |
||
| 80 | # Read data file |
||
| 81 | filename = os.path.join(os.getcwd(), "storage_investmentd.csv") |
||
| 82 | try: |
||
| 83 | data = pd.read_csv(filename) |
||
| 84 | except FileNotFoundError: |
||
| 85 | msg = "Data file not found: {0}. Only one value used!" |
||
| 86 | warnings.warn(msg.format(filename), UserWarning) |
||
| 87 | data = pd.DataFrame( |
||
| 88 | {"pv": [0.3, 0.5], "wind": [0.6, 0.8], "demand_el": [500, 600]} |
||
| 89 | ) |
||
| 90 | |||
| 91 | number_timesteps = len(data) |
||
| 92 | |||
| 93 | ########################################################################## |
||
| 94 | # Initialize the energy system and read/calculate necessary parameters |
||
| 95 | ########################################################################## |
||
| 96 | |||
| 97 | logger.define_logging() |
||
| 98 | logging.info("Initialize the energy system") |
||
| 99 | date_time_index = solph.create_time_index(2012, number=number_timesteps) |
||
| 100 | energysystem = solph.EnergySystem( |
||
| 101 | timeindex=date_time_index, infer_last_interval=False |
||
| 102 | ) |
||
| 103 | |||
| 104 | price_gas = 0.04 |
||
| 105 | |||
| 106 | # If the period is one year the equivalent periodical costs (epc) of an |
||
| 107 | # investment are equal to the annuity. Use oemof's economic tools. |
||
| 108 | epc_wind = economics.annuity(capex=1000, n=20, wacc=0.05) |
||
| 109 | epc_pv = economics.annuity(capex=1000, n=20, wacc=0.05) |
||
| 110 | epc_storage = economics.annuity(capex=1000, n=20, wacc=0.05) |
||
| 111 | |||
| 112 | ########################################################################## |
||
| 113 | # Create oemof objects |
||
| 114 | ########################################################################## |
||
| 115 | |||
| 116 | logging.info("Create oemof objects") |
||
| 117 | # create natural gas bus |
||
| 118 | bgas = solph.Bus(label="natural_gas") |
||
| 119 | |||
| 120 | # create electricity bus |
||
| 121 | bel = solph.Bus(label="electricity") |
||
| 122 | |||
| 123 | energysystem.add(bgas, bel) |
||
| 124 | |||
| 125 | # create excess component for the electricity bus to allow overproduction |
||
| 126 | excess = solph.components.Sink( |
||
| 127 | label="excess_bel", inputs={bel: solph.Flow()} |
||
| 128 | ) |
||
| 129 | |||
| 130 | # create source object representing the gas commodity (annual limit) |
||
| 131 | gas_resource = solph.components.Source( |
||
| 132 | label="rgas", outputs={bgas: solph.Flow(variable_costs=price_gas)} |
||
| 133 | ) |
||
| 134 | |||
| 135 | # create fixed source object representing wind power plants |
||
| 136 | wind = solph.components.Source( |
||
| 137 | label="wind", |
||
| 138 | outputs={ |
||
| 139 | bel: solph.Flow( |
||
| 140 | fix=data["wind"], |
||
| 141 | investment=solph.Investment(ep_costs=epc_wind), |
||
| 142 | ) |
||
| 143 | }, |
||
| 144 | ) |
||
| 145 | |||
| 146 | # create fixed source object representing pv power plants |
||
| 147 | pv = solph.components.Source( |
||
| 148 | label="pv", |
||
| 149 | outputs={ |
||
| 150 | bel: solph.Flow( |
||
| 151 | fix=data["pv"], investment=solph.Investment(ep_costs=epc_pv) |
||
| 152 | ) |
||
| 153 | }, |
||
| 154 | ) |
||
| 155 | |||
| 156 | # create simple sink object representing the electrical demand |
||
| 157 | demand = solph.components.Sink( |
||
| 158 | label="demand", |
||
| 159 | inputs={bel: solph.Flow(fix=data["demand_el"], nominal_value=1)}, |
||
| 160 | ) |
||
| 161 | |||
| 162 | # create simple transformer object representing a gas power plant |
||
| 163 | pp_gas = solph.components.Transformer( |
||
| 164 | label="pp_gas", |
||
| 165 | inputs={bgas: solph.Flow()}, |
||
| 166 | outputs={bel: solph.Flow(nominal_value=10e10, variable_costs=0)}, |
||
| 167 | conversion_factors={bel: 0.58}, |
||
| 168 | ) |
||
| 169 | |||
| 170 | # create storage object representing a battery |
||
| 171 | storage = solph.components.GenericStorage( |
||
| 172 | label="storage", |
||
| 173 | inputs={bel: solph.Flow(variable_costs=0.0001)}, |
||
| 174 | outputs={bel: solph.Flow()}, |
||
| 175 | loss_rate=0.00, |
||
| 176 | initial_storage_level=0, |
||
| 177 | invest_relation_input_capacity=1 / 6, |
||
| 178 | invest_relation_output_capacity=1 / 6, |
||
| 179 | inflow_conversion_factor=1, |
||
| 180 | outflow_conversion_factor=0.8, |
||
| 181 | investment=solph.Investment(ep_costs=epc_storage), |
||
| 182 | ) |
||
| 183 | |||
| 184 | energysystem.add(excess, gas_resource, wind, pv, demand, pp_gas, storage) |
||
| 185 | |||
| 186 | ########################################################################## |
||
| 187 | # Optimise the energy system |
||
| 188 | ########################################################################## |
||
| 189 | |||
| 190 | logging.info("Optimise the energy system") |
||
| 191 | |||
| 192 | # initialise the operational model |
||
| 193 | om = solph.Model(energysystem) |
||
| 194 | |||
| 195 | # if tee_switch is true solver messages will be displayed |
||
| 196 | logging.info("Solve the optimization problem") |
||
| 197 | om.solve(solver="cbc", solve_kwargs={"tee": True}) |
||
| 198 | |||
| 199 | ########################################################################## |
||
| 200 | # Check and plot the results |
||
| 201 | ########################################################################## |
||
| 202 | |||
| 203 | # check if the new result object is working for custom components |
||
| 204 | results = solph.processing.results(om) |
||
| 205 | |||
| 206 | electricity_bus = solph.views.node(results, "electricity") |
||
| 207 | |||
| 208 | meta_results = solph.processing.meta_results(om) |
||
| 209 | pp.pprint(meta_results) |
||
| 210 | |||
| 211 | my_results = electricity_bus["scalars"] |
||
| 212 | |||
| 213 | # installed capacity of storage in GWh |
||
| 214 | my_results["storage_invest_GWh"] = ( |
||
| 215 | results[(storage, None)]["scalars"]["invest"] / 1e6 |
||
| 216 | ) |
||
| 217 | |||
| 218 | # installed capacity of wind power plant in MW |
||
| 219 | my_results["wind_invest_MW"] = ( |
||
| 220 | results[(wind, bel)]["scalars"]["invest"] / 1e3 |
||
| 221 | ) |
||
| 222 | |||
| 223 | # resulting renewable energy share |
||
| 224 | my_results["res_share"] = ( |
||
| 225 | 1 |
||
| 226 | - results[(pp_gas, bel)]["sequences"].sum() |
||
| 227 | / results[(bel, demand)]["sequences"].sum() |
||
| 228 | ) |
||
| 229 | |||
| 230 | pp.pprint(my_results) |
||
| 231 | |||
| 235 |