| Conditions | 1 |
| Total Lines | 56 |
| Code Lines | 39 |
| 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 -*- |
||
| 65 | def main(): |
||
| 66 | |||
| 67 | date_time_index = solph.create_time_index(2025, number=2) |
||
| 68 | |||
| 69 | # create the energysystem and assign the time index |
||
| 70 | es = solph.EnergySystem( |
||
| 71 | timeindex=date_time_index, infer_last_interval=False |
||
| 72 | ) |
||
| 73 | |||
| 74 | house = SubNetwork("house") |
||
| 75 | |||
| 76 | el_bus = house.subnode( |
||
| 77 | solph.Bus, |
||
| 78 | local_name="el", |
||
| 79 | ) |
||
| 80 | el_source = solph.components.Source( |
||
| 81 | label="el_grid", |
||
| 82 | outputs={el_bus: solph.Flow(variable_costs=0.3)}, |
||
| 83 | ) |
||
| 84 | es.add(house, el_bus, el_source) |
||
| 85 | |||
| 86 | heat_demands = house.subnode( |
||
| 87 | SubNetwork, |
||
| 88 | local_name="heat demand", |
||
| 89 | ) |
||
| 90 | demand_bus_dhw = heat_demands.subnode(solph.Bus, "b_dhw") |
||
| 91 | demand_bus_sh = heat_demands.subnode(solph.Bus, "b_sh") |
||
| 92 | |||
| 93 | heat_demands.subnode( |
||
| 94 | solph.components.Sink, |
||
| 95 | local_name="d_dhw", |
||
| 96 | inputs={demand_bus_dhw: solph.Flow(nominal_capacity=1, fix=[0, 0.2])}, |
||
| 97 | ) |
||
| 98 | heat_demands.subnode( |
||
| 99 | solph.components.Sink, |
||
| 100 | local_name="d_sh", |
||
| 101 | inputs={demand_bus_sh: solph.Flow(nominal_capacity=1, fix=[0.4, 2.1])}, |
||
| 102 | ) |
||
| 103 | es.add(heat_demands) |
||
| 104 | hp = house.subnode( |
||
| 105 | HeatPump, |
||
| 106 | local_name="hp", |
||
| 107 | el_supply=el_bus, |
||
| 108 | heat_demand={demand_bus_dhw: 60.0, demand_bus_sh: 30}, |
||
| 109 | source_temperature=[3, 0], |
||
| 110 | cpf=0.45, |
||
| 111 | el_power_limit=3, |
||
| 112 | ) |
||
| 113 | es.add(hp) |
||
| 114 | |||
| 115 | model = solph.Model(es) |
||
| 116 | model.solve() |
||
| 117 | |||
| 118 | results = solph.Results(model) |
||
| 119 | |||
| 120 | print(results.flow) |
||
| 121 | |||
| 125 |