| Conditions | 1 |
| Total Lines | 64 |
| Code Lines | 38 |
| 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 | """ |
||
| 23 | def test_non_convex_status_variables(): |
||
| 24 | # Energy System |
||
| 25 | energy_system = solph.EnergySystem( |
||
| 26 | timeindex=pd.date_range(start="2025-01-01 12:00", freq="h", periods=3), |
||
| 27 | infer_last_interval=False, |
||
| 28 | ) |
||
| 29 | |||
| 30 | # Buses |
||
| 31 | bus_heat = solph.Bus("heat") |
||
| 32 | |||
| 33 | energy_system.add(bus_heat) |
||
| 34 | |||
| 35 | demand_heat = solph.components.Sink( |
||
| 36 | label="demand", |
||
| 37 | inputs={bus_heat: solph.Flow(nominal_capacity=5, fix=[0.5, 0.3])}, |
||
| 38 | ) |
||
| 39 | energy_system.add(demand_heat) |
||
| 40 | |||
| 41 | # gas boiler with minimal load |
||
| 42 | boiler = solph.components.Source( |
||
| 43 | label="gb", |
||
| 44 | outputs={ |
||
| 45 | bus_heat: solph.Flow( |
||
| 46 | nonconvex=solph.NonConvex(), |
||
| 47 | nominal_capacity=5, |
||
| 48 | min=0.5, |
||
| 49 | variable_costs=0.15, |
||
| 50 | ), |
||
| 51 | }, |
||
| 52 | ) |
||
| 53 | energy_system.add(boiler) |
||
| 54 | |||
| 55 | # heat pump with limited size |
||
| 56 | heat_pump = solph.components.Source( |
||
| 57 | label="hp", |
||
| 58 | outputs={ |
||
| 59 | bus_heat: solph.Flow( |
||
| 60 | nominal_capacity=solph.Investment(maximum=1, ep_costs=0.1), |
||
| 61 | min=0.5, |
||
| 62 | nonconvex=solph.NonConvex(), |
||
| 63 | variable_costs=0.1, |
||
| 64 | ) |
||
| 65 | }, |
||
| 66 | ) |
||
| 67 | energy_system.add(heat_pump) |
||
| 68 | |||
| 69 | el_heater = solph.components.Source( |
||
| 70 | label="rh", |
||
| 71 | outputs={bus_heat: solph.Flow(variable_costs=0.3)}, |
||
| 72 | ) |
||
| 73 | energy_system.add(el_heater) |
||
| 74 | |||
| 75 | # Model |
||
| 76 | model = solph.Model(energy_system) |
||
| 77 | |||
| 78 | # Optimization |
||
| 79 | model.solve(solver="cbc", solve_kwargs={"tee": False}) |
||
| 80 | |||
| 81 | results = solph.Results(model) |
||
| 82 | |||
| 83 | assert (results.status[(boiler, bus_heat)] == [1, 0]).all() |
||
| 84 | assert (results.status[(heat_pump, bus_heat)] == [0, 1]).all() |
||
| 85 | |||
| 86 | print(results.flow) |
||
| 87 | |||
| 91 |