| Conditions | 2 |
| Total Lines | 67 |
| Code Lines | 47 |
| Lines | 67 |
| Ratio | 100 % |
| 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 -*- |
||
| 37 | View Code Duplication | def main(): |
|
|
|
|||
| 38 | |||
| 39 | solver = "cbc" # 'glpk', 'gurobi',... |
||
| 40 | solver_verbose = False # show/hide solver output |
||
| 41 | |||
| 42 | date_time_index = solph.create_time_index(2000, interval=0.25, number=8) |
||
| 43 | |||
| 44 | energy_system = solph.EnergySystem( |
||
| 45 | timeindex=date_time_index, infer_last_interval=False |
||
| 46 | ) |
||
| 47 | |||
| 48 | bus = solph.buses.Bus(label="bus") |
||
| 49 | source = solph.components.Source( |
||
| 50 | label="source", |
||
| 51 | outputs={ |
||
| 52 | bus: solph.flows.Flow( |
||
| 53 | nominal_value=2, |
||
| 54 | variable_costs=0.2, |
||
| 55 | max=[0, 0, 0, 0, 1, 0.25, 0.75, 1], |
||
| 56 | ) |
||
| 57 | }, |
||
| 58 | ) |
||
| 59 | storage = solph.components.GenericStorage( |
||
| 60 | label="storage", |
||
| 61 | inputs={bus: solph.flows.Flow()}, |
||
| 62 | outputs={bus: solph.flows.Flow()}, |
||
| 63 | nominal_storage_capacity=4, |
||
| 64 | initial_storage_level=0.5, |
||
| 65 | ) |
||
| 66 | sink = solph.components.Sink( |
||
| 67 | label="sink", |
||
| 68 | inputs={ |
||
| 69 | bus: solph.flows.Flow( |
||
| 70 | nominal_value=2, |
||
| 71 | variable_costs=0.1, |
||
| 72 | fix=[1, 1, 0.5, 0.5, 0, 0, 0, 0], |
||
| 73 | ) |
||
| 74 | }, |
||
| 75 | ) |
||
| 76 | |||
| 77 | energy_system.add(bus, source, sink, storage) |
||
| 78 | model = solph.Model(energy_system) |
||
| 79 | model.solve(solver=solver, solve_kwargs={"tee": solver_verbose}) |
||
| 80 | |||
| 81 | results = solph.processing.results(model) |
||
| 82 | |||
| 83 | results_df = results[(storage, None)]["sequences"].copy() |
||
| 84 | results_df["storage_inflow"] = results[(bus, storage)]["sequences"]["flow"] |
||
| 85 | results_df["storage_outflow"] = results[(storage, bus)]["sequences"]["flow"] |
||
| 86 | |||
| 87 | print(results_df) |
||
| 88 | |||
| 89 | if plt is not None: |
||
| 90 | plt.plot( |
||
| 91 | results[(bus, storage)]["sequences"], |
||
| 92 | drawstyle="steps-post", |
||
| 93 | label="Storage inflow", |
||
| 94 | ) |
||
| 95 | plt.plot(results[(storage, None)]["sequences"], label="Storage content") |
||
| 96 | plt.plot( |
||
| 97 | results[(storage, bus)]["sequences"], |
||
| 98 | drawstyle="steps-post", |
||
| 99 | label="Storage outflow", |
||
| 100 | ) |
||
| 101 | |||
| 102 | plt.legend(loc="lower left") |
||
| 103 | plt.show() |
||
| 104 | |||
| 108 |