Conditions | 16 |
Total Lines | 191 |
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:
Complex classes like solph.flows._simple_flow_block.SimpleFlowBlock._create_constraints() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
192 | def _create_constraints(self): |
||
193 | r"""Creates all constraints for standard flows. |
||
194 | |||
195 | The following constraints are created, if the appropriate attribute of |
||
196 | the *Flow* (see :class:`~oemof.solph.flows._flow.Flow`) object is set: |
||
197 | |||
198 | * `Flow.full_load_time_max` is not `None` (full_load_time_max_constr): |
||
199 | .. math:: |
||
200 | \sum_t P(t) \cdot \tau \leq F_{max} \cdot P_{nom} |
||
201 | |||
202 | * `Flow.full_load_time_min` is not `None` (full_load_time_min_constr): |
||
203 | .. math:: |
||
204 | \sum_t P(t) \cdot \tau \geq F_{min} \cdot P_{nom} |
||
205 | |||
206 | * `Flow.negative_gradient` is not `None` (negative_gradient_constr): |
||
207 | .. math:: |
||
208 | P(t-1) - P(t) \geq ve_n(t) |
||
209 | |||
210 | * `Flow.positive_gradient` is not `None` (positive_gradient_constr): |
||
211 | .. math:: |
||
212 | P(t) - P(t-1) \geq ve_p(t) |
||
213 | |||
214 | * `Flow.integer` is `True` |
||
215 | .. math:: |
||
216 | P(t) = i(t) |
||
217 | """ |
||
218 | m = self.parent_block() |
||
219 | |||
220 | def _flow_full_load_time_max_rule(model): |
||
221 | """Rule definition for build action of max. sum flow constraint.""" |
||
222 | for inp, out in self.FULL_LOAD_TIME_MAX_FLOWS: |
||
223 | lhs = sum( |
||
224 | m.flow[inp, out, ts] |
||
225 | * m.timeincrement[ts] |
||
226 | * m.tsam_weighting[ts] |
||
227 | for ts in m.TIMESTEPS |
||
228 | ) |
||
229 | rhs = ( |
||
230 | m.flows[inp, out].full_load_time_max |
||
231 | * m.flows[inp, out].nominal_capacity |
||
232 | ) |
||
233 | self.full_load_time_max_constr.add((inp, out), lhs <= rhs) |
||
234 | |||
235 | self.full_load_time_max_constr = Constraint( |
||
236 | self.FULL_LOAD_TIME_MAX_FLOWS, noruleinit=True |
||
237 | ) |
||
238 | self.full_load_time_max_build = BuildAction( |
||
239 | rule=_flow_full_load_time_max_rule |
||
240 | ) |
||
241 | |||
242 | def _flow_full_load_time_min_rule(_): |
||
243 | """Rule definition for build action of min. sum flow constraint.""" |
||
244 | for inp, out in self.FULL_LOAD_TIME_MIN_FLOWS: |
||
245 | lhs = sum( |
||
246 | m.flow[inp, out, ts] |
||
247 | * m.timeincrement[ts] |
||
248 | * m.tsam_weighting[ts] |
||
249 | for ts in m.TIMESTEPS |
||
250 | ) |
||
251 | rhs = ( |
||
252 | m.flows[inp, out].full_load_time_min |
||
253 | * m.flows[inp, out].nominal_capacity |
||
254 | ) |
||
255 | self.full_load_time_min_constr.add((inp, out), lhs >= rhs) |
||
256 | |||
257 | self.full_load_time_min_constr = Constraint( |
||
258 | self.FULL_LOAD_TIME_MIN_FLOWS, noruleinit=True |
||
259 | ) |
||
260 | self.full_load_time_min_build = BuildAction( |
||
261 | rule=_flow_full_load_time_min_rule |
||
262 | ) |
||
263 | |||
264 | def _positive_gradient_flow_rule(_): |
||
265 | """Rule definition for positive gradient constraint.""" |
||
266 | for inp, out in self.POSITIVE_GRADIENT_FLOWS: |
||
267 | for index in range(1, len(m.TIMESTEPS) + 1): |
||
268 | if m.TIMESTEPS.at(index) > 0: |
||
269 | lhs = ( |
||
270 | m.flow[ |
||
271 | inp, |
||
272 | out, |
||
273 | m.TIMESTEPS.at(index), |
||
274 | ] |
||
275 | - m.flow[ |
||
276 | inp, |
||
277 | out, |
||
278 | m.TIMESTEPS.at(index - 1), |
||
279 | ] |
||
280 | ) |
||
281 | rhs = self.positive_gradient[ |
||
282 | inp, out, m.TIMESTEPS.at(index) |
||
283 | ] |
||
284 | self.positive_gradient_constr.add( |
||
285 | (inp, out, m.TIMESTEPS.at(index)), |
||
286 | lhs <= rhs, |
||
287 | ) |
||
288 | else: |
||
289 | lhs = self.positive_gradient[inp, out, 0] |
||
290 | rhs = 0 |
||
291 | self.positive_gradient_constr.add( |
||
292 | (inp, out, m.TIMESTEPS.at(index)), |
||
293 | lhs == rhs, |
||
294 | ) |
||
295 | |||
296 | self.positive_gradient_constr = Constraint( |
||
297 | self.POSITIVE_GRADIENT_FLOWS, m.TIMESTEPS, noruleinit=True |
||
298 | ) |
||
299 | self.positive_gradient_build = BuildAction( |
||
300 | rule=_positive_gradient_flow_rule |
||
301 | ) |
||
302 | |||
303 | def _negative_gradient_flow_rule(model): |
||
304 | """Rule definition for negative gradient constraint.""" |
||
305 | for inp, out in self.NEGATIVE_GRADIENT_FLOWS: |
||
306 | for index in range(1, len(m.TIMESTEPS) + 1): |
||
307 | if m.TIMESTEPS.at(index) > 0: |
||
308 | lhs = ( |
||
309 | m.flow[inp, out, m.TIMESTEPS.at(index - 1)] |
||
310 | - m.flow[inp, out, m.TIMESTEPS.at(index)] |
||
311 | ) |
||
312 | rhs = self.negative_gradient[ |
||
313 | inp, out, m.TIMESTEPS.at(index) |
||
314 | ] |
||
315 | self.negative_gradient_constr.add( |
||
316 | (inp, out, m.TIMESTEPS.at(index)), |
||
317 | lhs <= rhs, |
||
318 | ) |
||
319 | else: |
||
320 | lhs = self.negative_gradient[inp, out, 0] |
||
321 | rhs = 0 |
||
322 | self.negative_gradient_constr.add( |
||
323 | (inp, out, m.TIMESTEPS.at(index)), |
||
324 | lhs == rhs, |
||
325 | ) |
||
326 | |||
327 | self.negative_gradient_constr = Constraint( |
||
328 | self.NEGATIVE_GRADIENT_FLOWS, m.TIMESTEPS, noruleinit=True |
||
329 | ) |
||
330 | self.negative_gradient_build = BuildAction( |
||
331 | rule=_negative_gradient_flow_rule |
||
332 | ) |
||
333 | |||
334 | def _integer_flow_rule(_, ii, oi, ti): |
||
335 | """Force flow variable to NonNegativeInteger values.""" |
||
336 | return self.integer_flow[ii, oi, ti] == m.flow[ii, oi, ti] |
||
337 | |||
338 | self.integer_flow_constr = Constraint( |
||
339 | self.INTEGER_FLOWS, m.TIMESTEPS, rule=_integer_flow_rule |
||
340 | ) |
||
341 | |||
342 | if m.es.periods is not None: |
||
343 | |||
344 | def _lifetime_output_rule(_): |
||
345 | """Force flow value to zero when lifetime is reached""" |
||
346 | for inp, out in self.LIFETIME_FLOWS: |
||
347 | for p, ts in m.TIMEINDEX: |
||
348 | if m.flows[inp, out].lifetime <= m.es.periods_years[p]: |
||
349 | lhs = m.flow[inp, out, ts] |
||
350 | rhs = 0 |
||
351 | self.lifetime_output.add( |
||
352 | (inp, out, p, ts), (lhs == rhs) |
||
353 | ) |
||
354 | |||
355 | self.lifetime_output = Constraint( |
||
356 | self.LIFETIME_FLOWS, m.TIMEINDEX, noruleinit=True |
||
357 | ) |
||
358 | self.lifetime_output_build = BuildAction( |
||
359 | rule=_lifetime_output_rule |
||
360 | ) |
||
361 | |||
362 | def _lifetime_age_output_rule(block): |
||
363 | """Force flow value to zero when lifetime is reached |
||
364 | considering initial age |
||
365 | """ |
||
366 | for inp, out in self.LIFETIME_AGE_FLOWS: |
||
367 | for p, ts in m.TIMEINDEX: |
||
368 | if ( |
||
369 | m.flows[inp, out].lifetime - m.flows[inp, out].age |
||
370 | <= m.es.periods_years[p] |
||
371 | ): |
||
372 | lhs = m.flow[inp, out, ts] |
||
373 | rhs = 0 |
||
374 | self.lifetime_age_output.add( |
||
375 | (inp, out, p, ts), (lhs == rhs) |
||
376 | ) |
||
377 | |||
378 | self.lifetime_age_output = Constraint( |
||
379 | self.LIFETIME_AGE_FLOWS, m.TIMEINDEX, noruleinit=True |
||
380 | ) |
||
381 | self.lifetime_age_output_build = BuildAction( |
||
382 | rule=_lifetime_age_output_rule |
||
383 | ) |
||
510 |