| Conditions | 3 |
| Total Lines | 107 |
| Code Lines | 66 |
| 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 -*- |
||
| 52 | def storage_example(): |
||
| 53 | timeseries = pd.DataFrame( |
||
| 54 | {"demand_el": [7, 6, 6, 7], "pv_el": [3, 5, 3, 12]} |
||
| 55 | ) |
||
| 56 | |||
| 57 | # create an energy system |
||
| 58 | idx = pd.date_range("1/1/2017", periods=len(timeseries), freq="H") |
||
| 59 | es = solph.EnergySystem(timeindex=idx) |
||
| 60 | |||
| 61 | for data_set in DATA: |
||
| 62 | name = data_set["name"] |
||
| 63 | |||
| 64 | # power bus |
||
| 65 | bel = solph.Bus(label="bel_{0}".format(name)) |
||
| 66 | es.add(bel) |
||
| 67 | |||
| 68 | es.add( |
||
| 69 | solph.components.Source( |
||
| 70 | label="source_el_{0}".format(name), |
||
| 71 | outputs={ |
||
| 72 | bel: solph.Flow(variable_costs=PARAMETER["el_price"]) |
||
| 73 | }, |
||
| 74 | ) |
||
| 75 | ) |
||
| 76 | |||
| 77 | es.add( |
||
| 78 | solph.components.Source( |
||
| 79 | label="pv_el_{0}".format(name), |
||
| 80 | outputs={ |
||
| 81 | bel: solph.Flow(fix=timeseries["pv_el"], nominal_value=1) |
||
| 82 | }, |
||
| 83 | ) |
||
| 84 | ) |
||
| 85 | |||
| 86 | es.add( |
||
| 87 | solph.components.Sink( |
||
| 88 | label="demand_el_{0}".format(name), |
||
| 89 | inputs={ |
||
| 90 | bel: solph.Flow( |
||
| 91 | fix=timeseries["demand_el"], nominal_value=1 |
||
| 92 | ) |
||
| 93 | }, |
||
| 94 | ) |
||
| 95 | ) |
||
| 96 | |||
| 97 | es.add( |
||
| 98 | solph.components.Sink( |
||
| 99 | label="excess_{0}".format(name), |
||
| 100 | inputs={bel: solph.Flow()}, |
||
| 101 | ) |
||
| 102 | ) |
||
| 103 | |||
| 104 | # Electric Storage |
||
| 105 | es.add( |
||
| 106 | solph.components.GenericStorage( |
||
| 107 | label="storage_elec_{0}".format(name), |
||
| 108 | nominal_storage_capacity=PARAMETER["nominal_storage_capacity"], |
||
| 109 | inputs={bel: solph.Flow()}, |
||
| 110 | outputs={bel: solph.Flow()}, |
||
| 111 | initial_storage_level=data_set["initial_storage_level"], |
||
| 112 | balanced=data_set["balanced"], |
||
| 113 | ) |
||
| 114 | ) |
||
| 115 | |||
| 116 | # create an optimization problem and solve it |
||
| 117 | om = solph.Model(es) |
||
| 118 | |||
| 119 | # solve model |
||
| 120 | om.solve(solver="cbc") |
||
| 121 | |||
| 122 | # create result object |
||
| 123 | results = solph.processing.results(om) |
||
| 124 | |||
| 125 | components = [x for x in results if x[1] is None] |
||
| 126 | |||
| 127 | storage_cap = pd.DataFrame() |
||
| 128 | balance = pd.Series(dtype=float) |
||
| 129 | |||
| 130 | storages = [x[0] for x in components if "storage" in x[0].label] |
||
| 131 | |||
| 132 | for s in storages: |
||
| 133 | name = s.label |
||
| 134 | storage_cap[name] = results[s, None]["sequences"]["storage_content"] |
||
| 135 | balance[name] = storage_cap.iloc[0][name] - storage_cap.iloc[-1][name] |
||
| 136 | |||
| 137 | storage_cap.plot( |
||
| 138 | drawstyle="steps-mid", |
||
| 139 | subplots=False, |
||
| 140 | sharey=True, |
||
| 141 | title="Storage content", |
||
| 142 | ) |
||
| 143 | storage_cap.plot( |
||
| 144 | drawstyle="steps-mid", |
||
| 145 | subplots=True, |
||
| 146 | sharey=True, |
||
| 147 | title="Storage content", |
||
| 148 | ) |
||
| 149 | |||
| 150 | balance.plot( |
||
| 151 | kind="bar", |
||
| 152 | linewidth=1, |
||
| 153 | edgecolor="#000000", |
||
| 154 | rot=0, |
||
| 155 | ax=plt.subplots()[1], |
||
| 156 | title="Gained energy from storage", |
||
| 157 | ) |
||
| 158 | plt.show() |
||
| 159 | |||
| 163 |