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