Conditions | 6 |
Total Lines | 377 |
Code Lines | 200 |
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 -*- |
||
338 | def _create(self, group=None): |
||
339 | """ |
||
340 | Create constraints for GenericCAESBlock. |
||
341 | |||
342 | Parameters |
||
343 | ---------- |
||
344 | group : list |
||
345 | List containing `.GenericCAES` objects. |
||
346 | e.g. groups=[gcaes1, gcaes2,..] |
||
347 | """ |
||
348 | m = self.parent_block() |
||
349 | |||
350 | if group is None: |
||
351 | return None |
||
352 | |||
353 | self.GENERICCAES = Set(initialize=[n for n in group]) |
||
354 | |||
355 | # Compression: Binary variable for operation status |
||
356 | self.cmp_st = Var(self.GENERICCAES, m.TIMESTEPS, within=Binary) |
||
357 | |||
358 | # Compression: Realized capacity |
||
359 | self.cmp_p = Var( |
||
360 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
361 | ) |
||
362 | |||
363 | # Compression: Max. Capacity |
||
364 | self.cmp_p_max = Var( |
||
365 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
366 | ) |
||
367 | |||
368 | # Compression: Heat flow |
||
369 | self.cmp_q_out_sum = Var( |
||
370 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
371 | ) |
||
372 | |||
373 | # Compression: Waste heat |
||
374 | self.cmp_q_waste = Var( |
||
375 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
376 | ) |
||
377 | |||
378 | # Expansion: Binary variable for operation status |
||
379 | self.exp_st = Var(self.GENERICCAES, m.TIMESTEPS, within=Binary) |
||
380 | |||
381 | # Expansion: Realized capacity |
||
382 | self.exp_p = Var( |
||
383 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
384 | ) |
||
385 | |||
386 | # Expansion: Max. Capacity |
||
387 | self.exp_p_max = Var( |
||
388 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
389 | ) |
||
390 | |||
391 | # Expansion: Heat flow of natural gas co-firing |
||
392 | self.exp_q_in_sum = Var( |
||
393 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
394 | ) |
||
395 | |||
396 | # Expansion: Heat flow of natural gas co-firing |
||
397 | self.exp_q_fuel_in = Var( |
||
398 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
399 | ) |
||
400 | |||
401 | # Expansion: Heat flow of additional firing |
||
402 | self.exp_q_add_in = Var( |
||
403 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
404 | ) |
||
405 | |||
406 | # Cavern: Filling levelh |
||
407 | self.cav_level = Var( |
||
408 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
409 | ) |
||
410 | |||
411 | # Cavern: Energy inflow |
||
412 | self.cav_e_in = Var( |
||
413 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
414 | ) |
||
415 | |||
416 | # Cavern: Energy outflow |
||
417 | self.cav_e_out = Var( |
||
418 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
419 | ) |
||
420 | |||
421 | # TES: Filling levelh |
||
422 | self.tes_level = Var( |
||
423 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
424 | ) |
||
425 | |||
426 | # TES: Energy inflow |
||
427 | self.tes_e_in = Var( |
||
428 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
429 | ) |
||
430 | |||
431 | # TES: Energy outflow |
||
432 | self.tes_e_out = Var( |
||
433 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
434 | ) |
||
435 | |||
436 | # Spot market: Positive capacity |
||
437 | self.exp_p_spot = Var( |
||
438 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
439 | ) |
||
440 | |||
441 | # Spot market: Negative capacity |
||
442 | self.cmp_p_spot = Var( |
||
443 | self.GENERICCAES, m.TIMESTEPS, within=NonNegativeReals |
||
444 | ) |
||
445 | |||
446 | # Compression: Capacity on markets |
||
447 | def cmp_p_constr_rule(block, n, t): |
||
448 | expr = 0 |
||
449 | expr += -self.cmp_p[n, t] |
||
450 | expr += m.flow[list(n.electrical_input.keys())[0], n, t] |
||
451 | return expr == 0 |
||
452 | |||
453 | self.cmp_p_constr = Constraint( |
||
454 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_p_constr_rule |
||
455 | ) |
||
456 | |||
457 | # Compression: Max. capacity depending on cavern filling level |
||
458 | def cmp_p_max_constr_rule(block, n, t): |
||
459 | if t != 0: |
||
460 | return ( |
||
461 | self.cmp_p_max[n, t] |
||
462 | == n.params["cmp_p_max_m"] * self.cav_level[n, t - 1] |
||
463 | + n.params["cmp_p_max_b"] |
||
464 | ) |
||
465 | else: |
||
466 | return self.cmp_p_max[n, t] == n.params["cmp_p_max_b"] |
||
467 | |||
468 | self.cmp_p_max_constr = Constraint( |
||
469 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_p_max_constr_rule |
||
470 | ) |
||
471 | |||
472 | def cmp_p_max_area_constr_rule(block, n, t): |
||
473 | return self.cmp_p[n, t] <= self.cmp_p_max[n, t] |
||
474 | |||
475 | self.cmp_p_max_area_constr = Constraint( |
||
476 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_p_max_area_constr_rule |
||
477 | ) |
||
478 | |||
479 | # Compression: Status of operation (on/off) |
||
480 | def cmp_st_p_min_constr_rule(block, n, t): |
||
481 | return ( |
||
482 | self.cmp_p[n, t] >= n.params["cmp_p_min"] * self.cmp_st[n, t] |
||
483 | ) |
||
484 | |||
485 | self.cmp_st_p_min_constr = Constraint( |
||
486 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_st_p_min_constr_rule |
||
487 | ) |
||
488 | |||
489 | def cmp_st_p_max_constr_rule(block, n, t): |
||
490 | return ( |
||
491 | self.cmp_p[n, t] |
||
492 | <= ( |
||
493 | n.params["cmp_p_max_m"] * n.params["cav_level_max"] |
||
494 | + n.params["cmp_p_max_b"] |
||
495 | ) |
||
496 | * self.cmp_st[n, t] |
||
497 | ) |
||
498 | |||
499 | self.cmp_st_p_max_constr = Constraint( |
||
500 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_st_p_max_constr_rule |
||
501 | ) |
||
502 | |||
503 | # (7) Compression: Heat flow out |
||
504 | def cmp_q_out_constr_rule(block, n, t): |
||
505 | return ( |
||
506 | self.cmp_q_out_sum[n, t] |
||
507 | == n.params["cmp_q_out_m"] * self.cmp_p[n, t] |
||
508 | + n.params["cmp_q_out_b"] * self.cmp_st[n, t] |
||
509 | ) |
||
510 | |||
511 | self.cmp_q_out_constr = Constraint( |
||
512 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_q_out_constr_rule |
||
513 | ) |
||
514 | |||
515 | # (8) Compression: Definition of single heat flows |
||
516 | def cmp_q_out_sum_constr_rule(block, n, t): |
||
517 | return ( |
||
518 | self.cmp_q_out_sum[n, t] |
||
519 | == self.cmp_q_waste[n, t] + self.tes_e_in[n, t] |
||
520 | ) |
||
521 | |||
522 | self.cmp_q_out_sum_constr = Constraint( |
||
523 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_q_out_sum_constr_rule |
||
524 | ) |
||
525 | |||
526 | # (9) Compression: Heat flow out ratio |
||
527 | def cmp_q_out_shr_constr_rule(block, n, t): |
||
528 | return self.cmp_q_waste[n, t] * n.params[ |
||
529 | "cmp_q_tes_share" |
||
530 | ] == self.tes_e_in[n, t] * (1 - n.params["cmp_q_tes_share"]) |
||
531 | |||
532 | self.cmp_q_out_shr_constr = Constraint( |
||
533 | self.GENERICCAES, m.TIMESTEPS, rule=cmp_q_out_shr_constr_rule |
||
534 | ) |
||
535 | |||
536 | # (10) Expansion: Capacity on markets |
||
537 | def exp_p_constr_rule(block, n, t): |
||
538 | expr = 0 |
||
539 | expr += -self.exp_p[n, t] |
||
540 | expr += m.flow[n, list(n.electrical_output.keys())[0], t] |
||
541 | return expr == 0 |
||
542 | |||
543 | self.exp_p_constr = Constraint( |
||
544 | self.GENERICCAES, m.TIMESTEPS, rule=exp_p_constr_rule |
||
545 | ) |
||
546 | |||
547 | # (11-12) Expansion: Max. capacity depending on cavern filling level |
||
548 | def exp_p_max_constr_rule(block, n, t): |
||
549 | if t != 0: |
||
550 | return ( |
||
551 | self.exp_p_max[n, t] |
||
552 | == n.params["exp_p_max_m"] * self.cav_level[n, t - 1] |
||
553 | + n.params["exp_p_max_b"] |
||
554 | ) |
||
555 | else: |
||
556 | return self.exp_p_max[n, t] == n.params["exp_p_max_b"] |
||
557 | |||
558 | self.exp_p_max_constr = Constraint( |
||
559 | self.GENERICCAES, m.TIMESTEPS, rule=exp_p_max_constr_rule |
||
560 | ) |
||
561 | |||
562 | # (13) |
||
563 | def exp_p_max_area_constr_rule(block, n, t): |
||
564 | return self.exp_p[n, t] <= self.exp_p_max[n, t] |
||
565 | |||
566 | self.exp_p_max_area_constr = Constraint( |
||
567 | self.GENERICCAES, m.TIMESTEPS, rule=exp_p_max_area_constr_rule |
||
568 | ) |
||
569 | |||
570 | # (14) Expansion: Status of operation (on/off) |
||
571 | def exp_st_p_min_constr_rule(block, n, t): |
||
572 | return ( |
||
573 | self.exp_p[n, t] >= n.params["exp_p_min"] * self.exp_st[n, t] |
||
574 | ) |
||
575 | |||
576 | self.exp_st_p_min_constr = Constraint( |
||
577 | self.GENERICCAES, m.TIMESTEPS, rule=exp_st_p_min_constr_rule |
||
578 | ) |
||
579 | |||
580 | # (15) |
||
581 | def exp_st_p_max_constr_rule(block, n, t): |
||
582 | return ( |
||
583 | self.exp_p[n, t] |
||
584 | <= ( |
||
585 | n.params["exp_p_max_m"] * n.params["cav_level_max"] |
||
586 | + n.params["exp_p_max_b"] |
||
587 | ) |
||
588 | * self.exp_st[n, t] |
||
589 | ) |
||
590 | |||
591 | self.exp_st_p_max_constr = Constraint( |
||
592 | self.GENERICCAES, m.TIMESTEPS, rule=exp_st_p_max_constr_rule |
||
593 | ) |
||
594 | |||
595 | # (16) Expansion: Heat flow in |
||
596 | def exp_q_in_constr_rule(block, n, t): |
||
597 | return ( |
||
598 | self.exp_q_in_sum[n, t] |
||
599 | == n.params["exp_q_in_m"] * self.exp_p[n, t] |
||
600 | + n.params["exp_q_in_b"] * self.exp_st[n, t] |
||
601 | ) |
||
602 | |||
603 | self.exp_q_in_constr = Constraint( |
||
604 | self.GENERICCAES, m.TIMESTEPS, rule=exp_q_in_constr_rule |
||
605 | ) |
||
606 | |||
607 | # (17) Expansion: Fuel allocation |
||
608 | def exp_q_fuel_constr_rule(block, n, t): |
||
609 | expr = 0 |
||
610 | expr += -self.exp_q_fuel_in[n, t] |
||
611 | expr += m.flow[list(n.fuel_input.keys())[0], n, t] |
||
612 | return expr == 0 |
||
613 | |||
614 | self.exp_q_fuel_constr = Constraint( |
||
615 | self.GENERICCAES, m.TIMESTEPS, rule=exp_q_fuel_constr_rule |
||
616 | ) |
||
617 | |||
618 | # (18) Expansion: Definition of single heat flows |
||
619 | def exp_q_in_sum_constr_rule(block, n, t): |
||
620 | return ( |
||
621 | self.exp_q_in_sum[n, t] |
||
622 | == self.exp_q_fuel_in[n, t] |
||
623 | + self.tes_e_out[n, t] |
||
624 | + self.exp_q_add_in[n, t] |
||
625 | ) |
||
626 | |||
627 | self.exp_q_in_sum_constr = Constraint( |
||
628 | self.GENERICCAES, m.TIMESTEPS, rule=exp_q_in_sum_constr_rule |
||
629 | ) |
||
630 | |||
631 | # (19) Expansion: Heat flow in ratio |
||
632 | def exp_q_in_shr_constr_rule(block, n, t): |
||
633 | return n.params["exp_q_tes_share"] * self.exp_q_fuel_in[n, t] == ( |
||
634 | 1 - n.params["exp_q_tes_share"] |
||
635 | ) * (self.exp_q_add_in[n, t] + self.tes_e_out[n, t]) |
||
636 | |||
637 | self.exp_q_in_shr_constr = Constraint( |
||
638 | self.GENERICCAES, m.TIMESTEPS, rule=exp_q_in_shr_constr_rule |
||
639 | ) |
||
640 | |||
641 | # (20) Cavern: Energy inflow |
||
642 | def cav_e_in_constr_rule(block, n, t): |
||
643 | return ( |
||
644 | self.cav_e_in[n, t] |
||
645 | == n.params["cav_e_in_m"] * self.cmp_p[n, t] |
||
646 | + n.params["cav_e_in_b"] |
||
647 | ) |
||
648 | |||
649 | self.cav_e_in_constr = Constraint( |
||
650 | self.GENERICCAES, m.TIMESTEPS, rule=cav_e_in_constr_rule |
||
651 | ) |
||
652 | |||
653 | # (21) Cavern: Energy outflow |
||
654 | def cav_e_out_constr_rule(block, n, t): |
||
655 | return ( |
||
656 | self.cav_e_out[n, t] |
||
657 | == n.params["cav_e_out_m"] * self.exp_p[n, t] |
||
658 | + n.params["cav_e_out_b"] |
||
659 | ) |
||
660 | |||
661 | self.cav_e_out_constr = Constraint( |
||
662 | self.GENERICCAES, m.TIMESTEPS, rule=cav_e_out_constr_rule |
||
663 | ) |
||
664 | |||
665 | # (22-23) Cavern: Storage balance |
||
666 | def cav_eta_constr_rule(block, n, t): |
||
667 | if t != 0: |
||
668 | return n.params["cav_eta_temp"] * self.cav_level[ |
||
669 | n, t |
||
670 | ] == self.cav_level[n, t - 1] + m.timeincrement[t] * ( |
||
671 | self.cav_e_in[n, t] - self.cav_e_out[n, t] |
||
672 | ) |
||
673 | else: |
||
674 | return n.params["cav_eta_temp"] * self.cav_level[ |
||
675 | n, t |
||
676 | ] == m.timeincrement[t] * ( |
||
677 | self.cav_e_in[n, t] - self.cav_e_out[n, t] |
||
678 | ) |
||
679 | |||
680 | self.cav_eta_constr = Constraint( |
||
681 | self.GENERICCAES, m.TIMESTEPS, rule=cav_eta_constr_rule |
||
682 | ) |
||
683 | |||
684 | # (24) Cavern: Upper bound |
||
685 | def cav_ub_constr_rule(block, n, t): |
||
686 | return self.cav_level[n, t] <= n.params["cav_level_max"] |
||
687 | |||
688 | self.cav_ub_constr = Constraint( |
||
689 | self.GENERICCAES, m.TIMESTEPS, rule=cav_ub_constr_rule |
||
690 | ) |
||
691 | |||
692 | # (25-26) TES: Storage balance |
||
693 | def tes_eta_constr_rule(block, n, t): |
||
694 | if t != 0: |
||
695 | return self.tes_level[n, t] == self.tes_level[ |
||
696 | n, t - 1 |
||
697 | ] + m.timeincrement[t] * ( |
||
698 | self.tes_e_in[n, t] - self.tes_e_out[n, t] |
||
699 | ) |
||
700 | else: |
||
701 | return self.tes_level[n, t] == m.timeincrement[t] * ( |
||
702 | self.tes_e_in[n, t] - self.tes_e_out[n, t] |
||
703 | ) |
||
704 | |||
705 | self.tes_eta_constr = Constraint( |
||
706 | self.GENERICCAES, m.TIMESTEPS, rule=tes_eta_constr_rule |
||
707 | ) |
||
708 | |||
709 | # (27) TES: Upper bound |
||
710 | def tes_ub_constr_rule(block, n, t): |
||
711 | return self.tes_level[n, t] <= n.params["tes_level_max"] |
||
712 | |||
713 | self.tes_ub_constr = Constraint( |
||
714 | self.GENERICCAES, m.TIMESTEPS, rule=tes_ub_constr_rule |
||
715 | ) |
||
716 |