| Conditions | 1 |
| Total Lines | 162 |
| Code Lines | 111 |
| 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 | """ |
||
| 35 | def main(): |
||
| 36 | # The gradient for the output of the natural gas power plant. |
||
| 37 | # Change the gradient between 0.1 and 0.0001 and check the results. The |
||
| 38 | # more flexible the power plant can be run the less the storage will be |
||
| 39 | # used. |
||
| 40 | gradient = 0.01 |
||
| 41 | |||
| 42 | date_time_index = pd.date_range("1/1/2012", periods=48, freq="H") |
||
| 43 | print(date_time_index) |
||
| 44 | energysystem = EnergySystem(timeindex=date_time_index, timemode="explicit") |
||
| 45 | |||
| 46 | demand = [ |
||
| 47 | 209643, |
||
| 48 | 207497, |
||
| 49 | 200108, |
||
| 50 | 191892, |
||
| 51 | 185717, |
||
| 52 | 180672, |
||
| 53 | 172683, |
||
| 54 | 170048, |
||
| 55 | 171132, |
||
| 56 | 179532, |
||
| 57 | 189155, |
||
| 58 | 201026, |
||
| 59 | 208466, |
||
| 60 | 207718, |
||
| 61 | 205443, |
||
| 62 | 206255, |
||
| 63 | 217240, |
||
| 64 | 232798, |
||
| 65 | 237321, |
||
| 66 | 232387, |
||
| 67 | 224306, |
||
| 68 | 219280, |
||
| 69 | 223701, |
||
| 70 | 213926, |
||
| 71 | 201834, |
||
| 72 | 192215, |
||
| 73 | 187152, |
||
| 74 | 184355, |
||
| 75 | 184438, |
||
| 76 | 182786, |
||
| 77 | 180105, |
||
| 78 | 191509, |
||
| 79 | 207104, |
||
| 80 | 222501, |
||
| 81 | 231127, |
||
| 82 | 238410, |
||
| 83 | 241184, |
||
| 84 | 237413, |
||
| 85 | 234469, |
||
| 86 | 235193, |
||
| 87 | 242730, |
||
| 88 | 264196, |
||
| 89 | 265950, |
||
| 90 | 260283, |
||
| 91 | 245578, |
||
| 92 | 238849, |
||
| 93 | 241553, |
||
| 94 | 231372, |
||
| 95 | ] |
||
| 96 | |||
| 97 | # create natural gas bus |
||
| 98 | bgas = buses.Bus(label="natural_gas") |
||
| 99 | |||
| 100 | # create electricity bus |
||
| 101 | bel = buses.Bus(label="electricity") |
||
| 102 | |||
| 103 | # adding the buses to the energy system |
||
| 104 | energysystem.add(bgas, bel) |
||
| 105 | |||
| 106 | # create excess component for the electricity bus to allow overproduction |
||
| 107 | energysystem.add(cmp.Sink(label="excess_bel", inputs={bel: flows.Flow()})) |
||
| 108 | |||
| 109 | # create source object representing the gas commodity (annual limit) |
||
| 110 | energysystem.add( |
||
| 111 | cmp.Source( |
||
| 112 | label="rgas", |
||
| 113 | outputs={bgas: flows.Flow(variable_costs=5)}, |
||
| 114 | ) |
||
| 115 | ) |
||
| 116 | |||
| 117 | # create simple sink object representing the electrical demand |
||
| 118 | energysystem.add( |
||
| 119 | cmp.Sink( |
||
| 120 | label="demand", |
||
| 121 | inputs={bel: flows.Flow(fix=demand, nominal_value=1)}, |
||
| 122 | ) |
||
| 123 | ) |
||
| 124 | |||
| 125 | # create simple transformer object representing a gas power plant |
||
| 126 | energysystem.add( |
||
| 127 | cmp.Transformer( |
||
| 128 | label="pp_gas", |
||
| 129 | inputs={bgas: flows.Flow()}, |
||
| 130 | outputs={ |
||
| 131 | bel: flows.Flow( |
||
| 132 | nominal_value=10e5, |
||
| 133 | negative_gradient={"ub": gradient}, |
||
| 134 | positive_gradient={"ub": gradient}, |
||
| 135 | ) |
||
| 136 | }, |
||
| 137 | conversion_factors={bel: 0.58}, |
||
| 138 | ) |
||
| 139 | ) |
||
| 140 | |||
| 141 | # create storage object representing a battery |
||
| 142 | storage = cmp.GenericStorage( |
||
| 143 | nominal_storage_capacity=999999999, |
||
| 144 | label="storage", |
||
| 145 | inputs={bel: flows.Flow()}, |
||
| 146 | outputs={bel: flows.Flow()}, |
||
| 147 | loss_rate=0.0, |
||
| 148 | initial_storage_level=None, |
||
| 149 | inflow_conversion_factor=1, |
||
| 150 | outflow_conversion_factor=0.8, |
||
| 151 | ) |
||
| 152 | |||
| 153 | energysystem.add(storage) |
||
| 154 | |||
| 155 | # initialise the operational model |
||
| 156 | model = Model(energysystem) |
||
| 157 | |||
| 158 | # solve |
||
| 159 | model.solve(solver="cbc") |
||
| 160 | |||
| 161 | # processing the results |
||
| 162 | results = processing.results(model) |
||
| 163 | |||
| 164 | # *** Create a table with all sequences and store it into a file (csv/xlsx) |
||
| 165 | flows_to_bus = pd.DataFrame( |
||
| 166 | { |
||
| 167 | str(k[0].label): v["sequences"]["flow"] |
||
| 168 | for k, v in results.items() |
||
| 169 | if k[1] is not None and k[1] == bel |
||
| 170 | } |
||
| 171 | ) |
||
| 172 | flows_from_bus = pd.DataFrame( |
||
| 173 | { |
||
| 174 | str(k[1].label): v["sequences"]["flow"] |
||
| 175 | for k, v in results.items() |
||
| 176 | if k[1] is not None and k[0] == bel |
||
| 177 | } |
||
| 178 | ) |
||
| 179 | |||
| 180 | storage = pd.DataFrame( |
||
| 181 | { |
||
| 182 | str(k[0].label): v["sequences"]["storage_content"] |
||
| 183 | for k, v in results.items() |
||
| 184 | if k[1] is None and k[0] == storage |
||
| 185 | } |
||
| 186 | ) |
||
| 187 | |||
| 188 | my_flows = pd.concat( |
||
| 189 | [flows_to_bus, flows_from_bus, storage], |
||
| 190 | keys=["to_bus", "from_bus", "content", "duals"], |
||
| 191 | axis=1, |
||
| 192 | ) |
||
| 193 | |||
| 194 | print(my_flows) |
||
| 195 | my_flows.plot() |
||
| 196 | plt.show() |
||
| 197 | |||
| 201 |