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