| Conditions | 2 |
| Total Lines | 139 |
| Code Lines | 103 |
| 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 -*- |
||
| 37 | def main(): |
||
| 38 | solver = "cbc" # 'glpk', 'gurobi',... |
||
| 39 | solver_verbose = False # show/hide solver output |
||
| 40 | |||
| 41 | date_time_index = pd.DatetimeIndex( |
||
| 42 | data=[ |
||
| 43 | "2000-1-1T00:00:00", |
||
| 44 | "2000-1-1T00:15:00", |
||
| 45 | "2000-1-1T00:30:00", |
||
| 46 | "2000-1-1T00:45:00", |
||
| 47 | "2000-1-1T01:00:00", |
||
| 48 | "2000-1-1T01:00:01", |
||
| 49 | "2000-1-1T02:00:00", |
||
| 50 | "2000-1-1T03:00:00", |
||
| 51 | "2000-1-1T04:00:00", |
||
| 52 | "2000-1-1T05:00:00", |
||
| 53 | ] |
||
| 54 | ) |
||
| 55 | |||
| 56 | energy_system = solph.EnergySystem( |
||
| 57 | timeindex=date_time_index, infer_last_interval=False |
||
| 58 | ) |
||
| 59 | |||
| 60 | bus = solph.buses.Bus(label="bus") |
||
| 61 | source = solph.components.Source( |
||
| 62 | label="source", |
||
| 63 | outputs={ |
||
| 64 | bus: solph.flows.Flow( |
||
| 65 | nominal_value=16, |
||
| 66 | variable_costs=0.2, |
||
| 67 | max=[0, 0, 0, 0, 0, 0, 0, 1, 1], |
||
| 68 | ) |
||
| 69 | }, |
||
| 70 | ) |
||
| 71 | |||
| 72 | # storage with constant losses |
||
| 73 | storage_fixed = solph.components.GenericStorage( |
||
| 74 | label="storage_fixed", |
||
| 75 | inputs={bus: solph.flows.Flow()}, |
||
| 76 | outputs={bus: solph.flows.Flow()}, |
||
| 77 | nominal_storage_capacity=8, |
||
| 78 | initial_storage_level=1, |
||
| 79 | fixed_losses_absolute=1, # 1 energy unit loss per hour |
||
| 80 | ) |
||
| 81 | |||
| 82 | # storage with relative losses, we disallow outflows in the first time |
||
| 83 | # steps, so that the content is not transferred to storage_fixed |
||
| 84 | storage_relative = solph.components.GenericStorage( |
||
| 85 | label="storage_relative", |
||
| 86 | inputs={bus: solph.flows.Flow()}, |
||
| 87 | outputs={ |
||
| 88 | bus: solph.flows.Flow( |
||
| 89 | nominal_value=4, max=[0, 0, 0, 0, 0, 0, 0, 1, 1] |
||
| 90 | ) |
||
| 91 | }, |
||
| 92 | nominal_storage_capacity=8, |
||
| 93 | initial_storage_level=1, |
||
| 94 | loss_rate=0.5, # 50 % losses per hour |
||
| 95 | ) |
||
| 96 | sink = solph.components.Sink( |
||
| 97 | label="sink", |
||
| 98 | inputs={ |
||
| 99 | bus: solph.flows.Flow( |
||
| 100 | nominal_value=8, |
||
| 101 | variable_costs=0.1, |
||
| 102 | fix=[0.75, 0.5, 0, 0, 1, 0, 0, 0, 0], |
||
| 103 | ) |
||
| 104 | }, |
||
| 105 | ) |
||
| 106 | |||
| 107 | energy_system.add(bus, source, sink, storage_relative, storage_fixed) |
||
| 108 | model = solph.Model(energy_system) |
||
| 109 | model.solve(solver=solver, solve_kwargs={"tee": solver_verbose}) |
||
| 110 | |||
| 111 | results = solph.processing.results(model) |
||
| 112 | |||
| 113 | results_df = results[(storage_fixed, None)]["sequences"].copy() |
||
| 114 | results_df.rename( |
||
| 115 | columns={"storage_content": "storage_fixed"}, inplace=True |
||
| 116 | ) |
||
| 117 | results_df["storage_fixed_inflow"] = results[(bus, storage_fixed)][ |
||
| 118 | "sequences" |
||
| 119 | ]["flow"] |
||
| 120 | results_df["storage_fixed_outflow"] = results[(storage_fixed, bus)][ |
||
| 121 | "sequences" |
||
| 122 | ]["flow"] |
||
| 123 | results_df["storage_relative"] = results[(storage_relative, None)][ |
||
| 124 | "sequences" |
||
| 125 | ] |
||
| 126 | results_df["storage_relative_inflow"] = results[(bus, storage_relative)][ |
||
| 127 | "sequences" |
||
| 128 | ]["flow"] |
||
| 129 | results_df["storage_relative_outflow"] = results[(storage_relative, bus)][ |
||
| 130 | "sequences" |
||
| 131 | ]["flow"] |
||
| 132 | |||
| 133 | print(results_df) |
||
| 134 | |||
| 135 | if plt is not None: |
||
| 136 | plt.plot( |
||
| 137 | results[(bus, storage_fixed)]["sequences"], |
||
| 138 | "r-", |
||
| 139 | drawstyle="steps-post", |
||
| 140 | label="storage_fixed inflow", |
||
| 141 | ) |
||
| 142 | plt.plot( |
||
| 143 | results[(storage_fixed, None)]["sequences"], |
||
| 144 | "r--", |
||
| 145 | label="storage_fixed content", |
||
| 146 | ) |
||
| 147 | plt.plot(results[(storage_fixed, None)]["sequences"], "r+") |
||
| 148 | plt.plot( |
||
| 149 | results[(storage_fixed, bus)]["sequences"], |
||
| 150 | "r:", |
||
| 151 | drawstyle="steps-post", |
||
| 152 | label="storage_fixed outflow", |
||
| 153 | ) |
||
| 154 | |||
| 155 | plt.plot( |
||
| 156 | results[(bus, storage_relative)]["sequences"], |
||
| 157 | "m-", |
||
| 158 | drawstyle="steps-post", |
||
| 159 | label="storage_relative inflow", |
||
| 160 | ) |
||
| 161 | plt.plot( |
||
| 162 | results[(storage_relative, None)]["sequences"], |
||
| 163 | "m--", |
||
| 164 | label="storage_relative content", |
||
| 165 | ) |
||
| 166 | plt.plot(results[(storage_relative, None)]["sequences"], "m+") |
||
| 167 | plt.plot( |
||
| 168 | results[(storage_relative, bus)]["sequences"], |
||
| 169 | "m:", |
||
| 170 | drawstyle="steps-post", |
||
| 171 | label="storage_relative outflow", |
||
| 172 | ) |
||
| 173 | |||
| 174 | plt.legend() |
||
| 175 | plt.show() |
||
| 176 | |||
| 180 |