| Conditions | 2 |
| Total Lines | 92 |
| Code Lines | 65 |
| 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 -*- |
||
| 47 | def main(optimize=True): |
||
| 48 | es = EnergySystem( |
||
| 49 | timeindex=pd.date_range("2022-01-01", freq="1H", periods=24), |
||
| 50 | infer_last_interval=True, |
||
| 51 | ) |
||
| 52 | |||
| 53 | multiplexer = Bus( |
||
| 54 | label="multiplexer", |
||
| 55 | ) |
||
| 56 | |||
| 57 | storage = GenericStorage( |
||
| 58 | label="storage", |
||
| 59 | nominal_capacity=3, |
||
| 60 | initial_storage_level=1, |
||
| 61 | balanced=True, |
||
| 62 | loss_rate=0.05, |
||
| 63 | inputs={multiplexer: Flow()}, |
||
| 64 | outputs={multiplexer: Flow()}, |
||
| 65 | ) |
||
| 66 | |||
| 67 | es.add(multiplexer, storage) |
||
| 68 | |||
| 69 | in_0 = Source( |
||
| 70 | label="in_0", |
||
| 71 | outputs={multiplexer: Flow(nominal_capacity=0.5, variable_costs=0.15)}, |
||
| 72 | ) |
||
| 73 | es.add(in_0) |
||
| 74 | |||
| 75 | in_1 = Source( |
||
| 76 | label="in_1", outputs={multiplexer: Flow(nominal_capacity=0.1)} |
||
| 77 | ) |
||
| 78 | es.add(in_1) |
||
| 79 | |||
| 80 | out_0 = Sink( |
||
| 81 | label="out_0", |
||
| 82 | inputs={multiplexer: Flow(nominal_capacity=0.25, variable_costs=-0.1)}, |
||
| 83 | ) |
||
| 84 | es.add(out_0) |
||
| 85 | |||
| 86 | out_1 = Sink( |
||
| 87 | label="out_1", |
||
| 88 | inputs={multiplexer: Flow(nominal_capacity=0.15, variable_costs=-0.1)}, |
||
| 89 | ) |
||
| 90 | es.add(out_1) |
||
| 91 | |||
| 92 | if optimize is False: |
||
| 93 | return es |
||
| 94 | |||
| 95 | model = Model(es) |
||
| 96 | |||
| 97 | storage_level_constraint( |
||
| 98 | model=model, |
||
| 99 | name="multiplexer", |
||
| 100 | storage_component=storage, |
||
| 101 | multiplexer_bus=multiplexer, |
||
| 102 | input_levels={in_1: 1 / 3}, # in_0 is always active |
||
| 103 | output_levels={out_0: 1 / 6, out_1: 1 / 2}, |
||
| 104 | ) |
||
| 105 | model.solve() |
||
| 106 | |||
| 107 | my_results = results(model) |
||
| 108 | |||
| 109 | df = pd.DataFrame(my_results[(storage, None)]["sequences"]) |
||
| 110 | df["in1_status"] = my_results[(in_1, None)]["sequences"] |
||
| 111 | df["out1_status"] = my_results[(out_1, None)]["sequences"] |
||
| 112 | df["out0_status"] = my_results[(out_0, None)]["sequences"] |
||
| 113 | |||
| 114 | df["in1"] = my_results[(in_1, multiplexer)]["sequences"] |
||
| 115 | df["in0"] = my_results[(in_0, multiplexer)]["sequences"] |
||
| 116 | df["out0"] = my_results[(multiplexer, out_0)]["sequences"] |
||
| 117 | df["out1"] = my_results[(multiplexer, out_1)]["sequences"] |
||
| 118 | |||
| 119 | plt.step(df.index, df["in0"], where="post", label="inflow (<= 1)") |
||
| 120 | plt.step(df.index, df["in1"], where="post", label="inflow (< 1/3)") |
||
| 121 | plt.step(df.index, df["out0"], where="post", label="outflow (> 1/6)") |
||
| 122 | plt.step(df.index, df["out1"], where="post", label="outflow (> 1/2)") |
||
| 123 | |||
| 124 | plt.grid() |
||
| 125 | plt.legend() |
||
| 126 | plt.ylabel("Flow Power (arb. units)") |
||
| 127 | plt.ylim(0, 0.5) |
||
| 128 | |||
| 129 | plt.twinx() |
||
| 130 | |||
| 131 | plt.plot(df.index, df["storage_content"], "k--", label="storage content") |
||
| 132 | plt.ylim(0, 3) |
||
| 133 | plt.legend(loc="center right") |
||
| 134 | plt.ylabel("Stored Energy (arb. units)") |
||
| 135 | |||
| 136 | print(df) |
||
| 137 | |||
| 138 | plt.show() |
||
| 139 | |||
| 143 |