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