| Conditions | 1 |
| Total Lines | 65 |
| Code Lines | 33 |
| 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 -*- |
||
| 39 | def main(): |
||
| 40 | ########################################################################## |
||
| 41 | # Calculate parameters and initialize the energy system and |
||
| 42 | ########################################################################## |
||
| 43 | periods = 24 |
||
| 44 | time = pd.date_range("1/1/2018", periods=periods, freq="H") |
||
| 45 | |||
| 46 | demand_heat = np.full(periods, 5) |
||
| 47 | demand_heat[:4] = 0 |
||
| 48 | demand_heat[4:18] = 4 |
||
| 49 | |||
| 50 | activity_costs = np.full(periods, 5) |
||
| 51 | activity_costs[18:] = 0 |
||
| 52 | |||
| 53 | es = solph.EnergySystem(timeindex=time) |
||
| 54 | |||
| 55 | b_heat = solph.Bus(label="b_heat") |
||
| 56 | |||
| 57 | es.add(b_heat) |
||
| 58 | |||
| 59 | sink_heat = solph.components.Sink( |
||
| 60 | label="demand", |
||
| 61 | inputs={b_heat: solph.Flow(fix=demand_heat, nominal_value=1)}, |
||
| 62 | ) |
||
| 63 | |||
| 64 | fireplace = solph.components.Source( |
||
| 65 | label="fireplace", |
||
| 66 | outputs={ |
||
| 67 | b_heat: solph.Flow( |
||
| 68 | nominal_value=3, |
||
| 69 | variable_costs=0, |
||
| 70 | nonconvex=solph.NonConvex(activity_costs=activity_costs), |
||
| 71 | ) |
||
| 72 | }, |
||
| 73 | ) |
||
| 74 | |||
| 75 | boiler = solph.components.Source( |
||
| 76 | label="boiler", |
||
| 77 | outputs={b_heat: solph.Flow(nominal_value=10, variable_costs=1)}, |
||
| 78 | ) |
||
| 79 | |||
| 80 | es.add(sink_heat, fireplace, boiler) |
||
| 81 | |||
| 82 | ########################################################################## |
||
| 83 | # Optimise the energy system |
||
| 84 | ########################################################################## |
||
| 85 | |||
| 86 | # create an optimization problem and solve it |
||
| 87 | om = solph.Model(es) |
||
| 88 | |||
| 89 | # solve model |
||
| 90 | om.solve(solver="cbc", solve_kwargs={"tee": True}) |
||
| 91 | |||
| 92 | ########################################################################## |
||
| 93 | # Check and plot the results |
||
| 94 | ########################################################################## |
||
| 95 | |||
| 96 | results = solph.processing.results(om) |
||
| 97 | |||
| 98 | # plot data |
||
| 99 | data = solph.views.node(results, "b_heat")["sequences"] |
||
| 100 | ax = data.plot(kind="line", drawstyle="steps-post", grid=True, rot=0) |
||
| 101 | ax.set_xlabel("Time") |
||
| 102 | ax.set_ylabel("Heat (arb. units)") |
||
| 103 | plt.show() |
||
| 104 | |||
| 108 |