| Conditions | 2 |
| Total Lines | 81 |
| Code Lines | 50 |
| 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 -*- |
||
| 26 | def main(): |
||
| 27 | # Create demand data |
||
| 28 | demand_el = [0] * 24 |
||
| 29 | for n in [10, 15, 19]: |
||
| 30 | demand_el[n] = 1 |
||
| 31 | |||
| 32 | # create an energy system |
||
| 33 | idx = solph.create_time_index(2017, number=24) |
||
| 34 | es = solph.EnergySystem(timeindex=idx, infer_last_interval=False) |
||
| 35 | |||
| 36 | # power bus and components |
||
| 37 | bel = solph.Bus(label="bel") |
||
| 38 | |||
| 39 | demand_el = solph.components.Sink( |
||
| 40 | label="demand_el", |
||
| 41 | inputs={bel: solph.Flow(fix=demand_el, nominal_value=10)}, |
||
| 42 | ) |
||
| 43 | |||
| 44 | dummy_el = solph.components.Sink( |
||
| 45 | label="dummy_el", inputs={bel: solph.Flow(variable_costs=10)} |
||
| 46 | ) |
||
| 47 | |||
| 48 | pp1 = solph.components.Source( |
||
| 49 | label="plant_min_down_constraints", |
||
| 50 | outputs={ |
||
| 51 | bel: solph.Flow( |
||
| 52 | nominal_value=10, |
||
| 53 | min=0.5, |
||
| 54 | max=1.0, |
||
| 55 | variable_costs=10, |
||
| 56 | nonconvex=solph.NonConvex( |
||
| 57 | minimum_downtime=4, initial_status=0 |
||
| 58 | ), |
||
| 59 | ) |
||
| 60 | }, |
||
| 61 | ) |
||
| 62 | |||
| 63 | pp2 = solph.components.Source( |
||
| 64 | label="plant_min_up_constraints", |
||
| 65 | outputs={ |
||
| 66 | bel: solph.Flow( |
||
| 67 | nominal_value=10, |
||
| 68 | min=0.5, |
||
| 69 | max=1.0, |
||
| 70 | variable_costs=10, |
||
| 71 | nonconvex=solph.NonConvex(minimum_uptime=2, initial_status=1), |
||
| 72 | ) |
||
| 73 | }, |
||
| 74 | ) |
||
| 75 | |||
| 76 | es.add(bel, dummy_el, demand_el, pp1, pp2) |
||
| 77 | |||
| 78 | # create an optimization problem and solve it |
||
| 79 | om = solph.Model(es) |
||
| 80 | |||
| 81 | # debugging |
||
| 82 | # om.write('problem.lp', io_options={'symbolic_solver_labels': True}) |
||
| 83 | |||
| 84 | # solve model |
||
| 85 | om.solve(solver="cbc", solve_kwargs={"tee": True}) |
||
| 86 | |||
| 87 | # create result object |
||
| 88 | results = solph.processing.results(om) |
||
| 89 | |||
| 90 | # plot data |
||
| 91 | data = solph.views.node(results, "bel")["sequences"] |
||
| 92 | data[[(("bel", "demand_el"), "flow"), (("bel", "dummy_el"), "flow")]] *= -1 |
||
| 93 | exclude = ["dummy_el", "status"] |
||
| 94 | columns = [ |
||
| 95 | c |
||
| 96 | for c in data.columns |
||
| 97 | if not any(s in c[0] or s in c[1] for s in exclude) |
||
| 98 | ] |
||
| 99 | data = data[columns] |
||
| 100 | fig, ax = plt.subplots(figsize=(10, 5)) |
||
| 101 | data.plot(ax=ax, kind="line", drawstyle="steps-post", grid=True, rot=0) |
||
| 102 | ax.set_xlabel("Hour") |
||
| 103 | ax.set_ylabel("P [MW]") |
||
| 104 | plt.legend(loc="upper center", bbox_to_anchor=(0.5, 1.3), ncol=1) |
||
| 105 | fig.subplots_adjust(top=0.8) |
||
| 106 | plt.show() |
||
| 107 | |||
| 111 |