| Conditions | 4 |
| Total Lines | 154 |
| Code Lines | 97 |
| 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 -*- |
||
| 63 | def solve_model(data, year=2025, es=None, n=20, r=0.05): |
||
| 64 | if es is None: |
||
| 65 | es = EnergySystem(timeindex=data.index) |
||
| 66 | |||
| 67 | var_cost = discounted_average_price(energy_prices(), r, n, year) |
||
| 68 | invest_cost = investment_costs().loc[year] |
||
| 69 | |||
| 70 | # Create Investment objects from cost data |
||
| 71 | investments = {} |
||
| 72 | for key in ["gas boiler", "heat pump", "battery", "pv"]: |
||
| 73 | try: |
||
| 74 | epc = annuity( |
||
| 75 | invest_cost[(key, "specific_costs [Eur/kW]")], n, r |
||
| 76 | ) |
||
| 77 | except KeyError: |
||
| 78 | epc = annuity( |
||
| 79 | invest_cost[(key, "specific_costs [Eur/kWh]")], n, r |
||
| 80 | ) |
||
| 81 | fix_cost = calculate_fix_cost(invest_cost[(key, "fixed_costs [Eur]")]) |
||
| 82 | investments[key] = Investment(ep_costs=epc, fixed_costs=fix_cost) |
||
| 83 | |||
| 84 | # Buses |
||
| 85 | bus_el = Bus(label="electricity") |
||
| 86 | bus_heat = Bus(label="heat") |
||
| 87 | bus_gas = Bus(label="gas") |
||
| 88 | es.add(bus_el, bus_heat, bus_gas) |
||
| 89 | |||
| 90 | # Sources |
||
| 91 | es.add( |
||
| 92 | cmp.Source( |
||
| 93 | label="PV", |
||
| 94 | outputs={ |
||
| 95 | bus_el: Flow( |
||
| 96 | fix=data["PV (kW/kWp)"], |
||
| 97 | nominal_capacity=investments["pv"], |
||
| 98 | ) |
||
| 99 | }, |
||
| 100 | ) |
||
| 101 | ) |
||
| 102 | es.add( |
||
| 103 | cmp.Source( |
||
| 104 | label="Shortage_heat", outputs={bus_heat: Flow(variable_costs=99)} |
||
| 105 | ) |
||
| 106 | ) |
||
| 107 | es.add( |
||
| 108 | cmp.Source( |
||
| 109 | label="Grid import", |
||
| 110 | outputs={ |
||
| 111 | bus_el: Flow( |
||
| 112 | variable_costs=var_cost["electricity_prices [Eur/kWh]"] |
||
| 113 | ) |
||
| 114 | }, |
||
| 115 | ) |
||
| 116 | ) |
||
| 117 | es.add( |
||
| 118 | cmp.Source( |
||
| 119 | label="Gas import", |
||
| 120 | outputs={ |
||
| 121 | bus_el: Flow(variable_costs=var_cost["gas_prices [Eur/kWh]"]) |
||
| 122 | }, |
||
| 123 | ) |
||
| 124 | ) |
||
| 125 | |||
| 126 | # Battery |
||
| 127 | es.add( |
||
| 128 | cmp.GenericStorage( |
||
| 129 | label="Battery", |
||
| 130 | inputs={bus_el: Flow()}, |
||
| 131 | outputs={bus_el: Flow()}, |
||
| 132 | nominal_capacity=investments["battery"], # kWh |
||
| 133 | min_storage_level=0.0, |
||
| 134 | max_storage_level=1.0, |
||
| 135 | balanced=True, |
||
| 136 | loss_rate=0.001, # 0.1%/h |
||
| 137 | inflow_conversion_factor=0.95, # Lade-Wirkungsgrad |
||
| 138 | outflow_conversion_factor=0.95, # Entlade-Wirkungsgrad |
||
| 139 | ) |
||
| 140 | ) |
||
| 141 | |||
| 142 | # Sinks |
||
| 143 | es.add(cmp.Sink(label="Excess_el", inputs={bus_el: Flow()})) |
||
| 144 | es.add(cmp.Sink(label="Excess_heat", inputs={bus_heat: Flow()})) |
||
| 145 | es.add( |
||
| 146 | cmp.Sink( |
||
| 147 | label="Heat demand", |
||
| 148 | inputs={ |
||
| 149 | bus_heat: Flow( |
||
| 150 | fix=data["heat demand (kW)"], nominal_capacity=5.0 |
||
| 151 | ) |
||
| 152 | }, |
||
| 153 | ) |
||
| 154 | ) |
||
| 155 | es.add( |
||
| 156 | cmp.Sink( |
||
| 157 | label="Electricity demand", |
||
| 158 | inputs={ |
||
| 159 | bus_el: Flow( |
||
| 160 | fix=data["electricity demand (kW)"], nominal_capacity=1.0 |
||
| 161 | ) |
||
| 162 | }, |
||
| 163 | ) |
||
| 164 | ) |
||
| 165 | es.add( |
||
| 166 | cmp.Sink( |
||
| 167 | label="Electric Vehicle", |
||
| 168 | inputs={ |
||
| 169 | bus_el: Flow(fix=data["ev charge (kW)"], nominal_capacity=1.0) |
||
| 170 | }, |
||
| 171 | ) |
||
| 172 | ) |
||
| 173 | es.add( |
||
| 174 | cmp.Sink( |
||
| 175 | label="Grid Feed-in", |
||
| 176 | inputs={ |
||
| 177 | bus_el: Flow( |
||
| 178 | variable_costs=-var_cost["pv_feed_in [Eur/kWh]"] / 1000 |
||
| 179 | ) |
||
| 180 | }, |
||
| 181 | ) |
||
| 182 | ) |
||
| 183 | |||
| 184 | # Heat Pump |
||
| 185 | es.add( |
||
| 186 | cmp.Converter( |
||
| 187 | label="Heat pump", |
||
| 188 | inputs={bus_el: Flow()}, |
||
| 189 | outputs={ |
||
| 190 | bus_heat: Flow(nominal_capacity=investments["heat pump"]) |
||
| 191 | }, |
||
| 192 | conversion_factors={bus_heat: data["cop"]}, |
||
| 193 | ) |
||
| 194 | ) |
||
| 195 | # Gas Boiler |
||
| 196 | es.add( |
||
| 197 | cmp.Converter( |
||
| 198 | label="Gas Boiler", |
||
| 199 | inputs={bus_gas: Flow()}, |
||
| 200 | outputs={ |
||
| 201 | bus_heat: Flow(nominal_capacity=investments["gas boiler"]) |
||
| 202 | }, |
||
| 203 | conversion_factors={bus_heat: data["cop"]}, |
||
| 204 | ) |
||
| 205 | ) |
||
| 206 | |||
| 207 | graph.create_nx_graph(es, filename=Path(Path.home(), "test_graph.graphml")) |
||
| 208 | |||
| 209 | # Create Model and solve it |
||
| 210 | logging.info("Creating Model...") |
||
| 211 | m = Model(es) |
||
| 212 | logging.info("Solving Model...") |
||
| 213 | m.solve(solver="cbc", solve_kwargs={"tee": False}) |
||
| 214 | |||
| 215 | # Create Results |
||
| 216 | return Results(m) |
||
| 217 | |||
| 292 |