| Conditions | 24 |
| Total Lines | 556 |
| Code Lines | 359 |
| 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 data.datasets.sanity_checks.sanitycheck_emobility_mit() 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 | """ |
||
| 505 | input_geo_thermal = db.select_dataframe( |
||
| 506 | """SELECT carrier, |
||
| 507 | SUM(capacity::numeric) as Urban_central_geo_thermal_MW |
||
| 508 | FROM supply.egon_scenario_capacities |
||
| 509 | WHERE carrier= 'urban_central_geo_thermal' |
||
| 510 | AND scenario_name IN ('eGon2035') |
||
| 511 | GROUP BY (carrier); |
||
| 512 | """, |
||
| 513 | warning=False, |
||
| 514 | )["urban_central_geo_thermal_mw"].values[0] |
||
| 515 | |||
| 516 | output_geo_thermal = db.select_dataframe( |
||
| 517 | """SELECT carrier, SUM(p_nom::numeric) as geo_thermal_MW |
||
| 518 | FROM grid.egon_etrago_generator |
||
| 519 | WHERE carrier= 'geo_thermal' |
||
| 520 | AND scn_name IN ('eGon2035') |
||
| 521 | GROUP BY (carrier); |
||
| 522 | """, |
||
| 523 | warning=False, |
||
| 524 | )["geo_thermal_mw"].values[0] |
||
| 525 | |||
| 526 | e_geo_thermal = ( |
||
| 527 | round((output_geo_thermal - input_geo_thermal) / input_geo_thermal, 2) |
||
| 528 | * 100 |
||
| 529 | ) |
||
| 530 | logger.info(f"'geothermal': {e_geo_thermal} %") |
||
| 531 | |||
| 532 | |||
| 533 | def residential_electricity_annual_sum(rtol=1e-5): |
||
| 534 | """Sanity check for dataset electricity_demand_timeseries : |
||
| 535 | Demand_Building_Assignment |
||
| 536 | |||
| 537 | Aggregate the annual demand of all census cells at NUTS3 to compare |
||
| 538 | with initial scaling parameters from DemandRegio. |
||
| 539 | """ |
||
| 540 | |||
| 541 | df_nuts3_annual_sum = db.select_dataframe( |
||
| 542 | sql=""" |
||
| 543 | SELECT dr.nuts3, dr.scenario, dr.demand_regio_sum, profiles.profile_sum |
||
| 544 | FROM ( |
||
| 545 | SELECT scenario, SUM(demand) AS profile_sum, vg250_nuts3 |
||
| 546 | FROM demand.egon_demandregio_zensus_electricity AS egon, |
||
| 547 | boundaries.egon_map_zensus_vg250 AS boundaries |
||
| 548 | Where egon.zensus_population_id = boundaries.zensus_population_id |
||
| 549 | AND sector = 'residential' |
||
| 550 | GROUP BY vg250_nuts3, scenario |
||
| 551 | ) AS profiles |
||
| 552 | JOIN ( |
||
| 553 | SELECT nuts3, scenario, sum(demand) AS demand_regio_sum |
||
| 554 | FROM demand.egon_demandregio_hh |
||
| 555 | GROUP BY year, scenario, nuts3 |
||
| 556 | ) AS dr |
||
| 557 | ON profiles.vg250_nuts3 = dr.nuts3 and profiles.scenario = dr.scenario |
||
| 558 | """ |
||
| 559 | ) |
||
| 560 | |||
| 561 | np.testing.assert_allclose( |
||
| 562 | actual=df_nuts3_annual_sum["profile_sum"], |
||
| 563 | desired=df_nuts3_annual_sum["demand_regio_sum"], |
||
| 564 | rtol=rtol, |
||
| 565 | verbose=False, |
||
| 566 | ) |
||
| 567 | |||
| 568 | logger.info( |
||
| 569 | "Aggregated annual residential electricity demand" |
||
| 570 | " matches with DemandRegio at NUTS-3." |
||
| 571 | ) |
||
| 572 | |||
| 573 | |||
| 574 | def residential_electricity_hh_refinement(rtol=1e-5): |
||
| 575 | """Sanity check for dataset electricity_demand_timeseries : |
||
| 576 | Household Demands |
||
| 577 | |||
| 578 | Check sum of aggregated household types after refinement method |
||
| 579 | was applied and compare it to the original census values.""" |
||
| 580 | |||
| 581 | df_refinement = db.select_dataframe( |
||
| 582 | sql=""" |
||
| 583 | SELECT refined.nuts3, refined.characteristics_code, |
||
| 584 | refined.sum_refined::int, census.sum_census::int |
||
| 585 | FROM( |
||
| 586 | SELECT nuts3, characteristics_code, SUM(hh_10types) as sum_refined |
||
| 587 | FROM society.egon_destatis_zensus_household_per_ha_refined |
||
| 588 | GROUP BY nuts3, characteristics_code) |
||
| 589 | AS refined |
||
| 590 | JOIN( |
||
| 591 | SELECT t.nuts3, t.characteristics_code, sum(orig) as sum_census |
||
| 592 | FROM( |
||
| 593 | SELECT nuts3, cell_id, characteristics_code, |
||
| 594 | sum(DISTINCT(hh_5types))as orig |
||
| 595 | FROM society.egon_destatis_zensus_household_per_ha_refined |
||
| 596 | GROUP BY cell_id, characteristics_code, nuts3) AS t |
||
| 597 | GROUP BY t.nuts3, t.characteristics_code ) AS census |
||
| 598 | ON refined.nuts3 = census.nuts3 |
||
| 599 | AND refined.characteristics_code = census.characteristics_code |
||
| 600 | """ |
||
| 601 | ) |
||
| 602 | |||
| 603 | np.testing.assert_allclose( |
||
| 604 | actual=df_refinement["sum_refined"], |
||
| 605 | desired=df_refinement["sum_census"], |
||
| 606 | rtol=rtol, |
||
| 607 | verbose=False, |
||
| 608 | ) |
||
| 609 | |||
| 610 | logger.info("All Aggregated household types match at NUTS-3.") |
||
| 611 | |||
| 612 | |||
| 613 | def cts_electricity_demand_share(rtol=1e-5): |
||
| 614 | """Sanity check for dataset electricity_demand_timeseries : |
||
| 615 | CtsBuildings |
||
| 616 | |||
| 617 | Check sum of aggregated cts electricity demand share which equals to one |
||
| 618 | for every substation as the substation profile is linearly disaggregated |
||
| 619 | to all buildings.""" |
||
| 620 | |||
| 621 | with db.session_scope() as session: |
||
| 622 | cells_query = session.query(EgonCtsElectricityDemandBuildingShare) |
||
| 623 | |||
| 624 | df_demand_share = pd.read_sql( |
||
| 625 | cells_query.statement, cells_query.session.bind, index_col=None |
||
| 626 | ) |
||
| 627 | |||
| 628 | np.testing.assert_allclose( |
||
| 629 | actual=df_demand_share.groupby(["bus_id", "scenario"])[ |
||
| 630 | "profile_share" |
||
| 631 | ].sum(), |
||
| 632 | desired=1, |
||
| 633 | rtol=rtol, |
||
| 634 | verbose=False, |
||
| 635 | ) |
||
| 636 | |||
| 637 | logger.info("The aggregated demand shares equal to one!.") |
||
| 638 | |||
| 639 | |||
| 640 | def cts_heat_demand_share(rtol=1e-5): |
||
| 641 | """Sanity check for dataset electricity_demand_timeseries |
||
| 642 | : CtsBuildings |
||
| 643 | |||
| 644 | Check sum of aggregated cts heat demand share which equals to one |
||
| 645 | for every substation as the substation profile is linearly disaggregated |
||
| 646 | to all buildings.""" |
||
| 647 | |||
| 648 | with db.session_scope() as session: |
||
| 649 | cells_query = session.query(EgonCtsHeatDemandBuildingShare) |
||
| 650 | |||
| 651 | df_demand_share = pd.read_sql( |
||
| 652 | cells_query.statement, cells_query.session.bind, index_col=None |
||
| 653 | ) |
||
| 654 | |||
| 655 | np.testing.assert_allclose( |
||
| 656 | actual=df_demand_share.groupby(["bus_id", "scenario"])[ |
||
| 657 | "profile_share" |
||
| 658 | ].sum(), |
||
| 659 | desired=1, |
||
| 660 | rtol=rtol, |
||
| 661 | verbose=False, |
||
| 662 | ) |
||
| 663 | |||
| 664 | logger.info("The aggregated demand shares equal to one!.") |
||
| 665 | |||
| 666 | |||
| 667 | def sanitycheck_emobility_mit(): |
||
| 668 | """Execute sanity checks for eMobility: motorized individual travel |
||
| 669 | |||
| 670 | Checks data integrity for eGon2035, eGon2035_lowflex and eGon100RE scenario |
||
| 671 | using assertions: |
||
| 672 | 1. Allocated EV numbers and EVs allocated to grid districts |
||
| 673 | 2. Trip data (original inout data from simBEV) |
||
| 674 | 3. Model data in eTraGo PF tables (grid.egon_etrago_*) |
||
| 675 | |||
| 676 | Parameters |
||
| 677 | ---------- |
||
| 678 | None |
||
| 679 | |||
| 680 | Returns |
||
| 681 | ------- |
||
| 682 | None |
||
| 683 | """ |
||
| 684 | |||
| 685 | def check_ev_allocation(): |
||
| 686 | # Get target number for scenario |
||
| 687 | ev_count_target = scenario_variation_parameters["ev_count"] |
||
| 688 | print(f" Target count: {str(ev_count_target)}") |
||
| 689 | |||
| 690 | # Get allocated numbers |
||
| 691 | ev_counts_dict = {} |
||
| 692 | with db.session_scope() as session: |
||
| 693 | for table, level in zip( |
||
| 694 | [ |
||
| 695 | EgonEvCountMvGridDistrict, |
||
| 696 | EgonEvCountMunicipality, |
||
| 697 | EgonEvCountRegistrationDistrict, |
||
| 698 | ], |
||
| 699 | ["Grid District", "Municipality", "Registration District"], |
||
| 700 | ): |
||
| 701 | query = session.query( |
||
| 702 | func.sum( |
||
| 703 | table.bev_mini |
||
| 704 | + table.bev_medium |
||
| 705 | + table.bev_luxury |
||
| 706 | + table.phev_mini |
||
| 707 | + table.phev_medium |
||
| 708 | + table.phev_luxury |
||
| 709 | ).label("ev_count") |
||
| 710 | ).filter( |
||
| 711 | table.scenario == scenario_name, |
||
| 712 | table.scenario_variation == scenario_var_name, |
||
| 713 | ) |
||
| 714 | |||
| 715 | ev_counts = pd.read_sql( |
||
| 716 | query.statement, query.session.bind, index_col=None |
||
| 717 | ) |
||
| 718 | ev_counts_dict[level] = ev_counts.iloc[0].ev_count |
||
| 719 | print( |
||
| 720 | f" Count table: Total count for level {level} " |
||
| 721 | f"(table: {table.__table__}): " |
||
| 722 | f"{str(ev_counts_dict[level])}" |
||
| 723 | ) |
||
| 724 | |||
| 725 | # Compare with scenario target (only if not in testmode) |
||
| 726 | if TESTMODE_OFF: |
||
| 727 | for level, count in ev_counts_dict.items(): |
||
| 728 | np.testing.assert_allclose( |
||
| 729 | count, |
||
| 730 | ev_count_target, |
||
| 731 | rtol=0.0001, |
||
| 732 | err_msg=f"EV numbers in {level} seems to be flawed.", |
||
| 733 | ) |
||
| 734 | else: |
||
| 735 | print(" Testmode is on, skipping sanity check...") |
||
| 736 | |||
| 737 | # Get allocated EVs in grid districts |
||
| 738 | with db.session_scope() as session: |
||
| 739 | query = session.query( |
||
| 740 | func.count(EgonEvMvGridDistrict.egon_ev_pool_ev_id).label( |
||
| 741 | "ev_count" |
||
| 742 | ), |
||
| 743 | ).filter( |
||
| 744 | EgonEvMvGridDistrict.scenario == scenario_name, |
||
| 745 | EgonEvMvGridDistrict.scenario_variation == scenario_var_name, |
||
| 746 | ) |
||
| 747 | ev_count_alloc = ( |
||
| 748 | pd.read_sql(query.statement, query.session.bind, index_col=None) |
||
| 749 | .iloc[0] |
||
| 750 | .ev_count |
||
| 751 | ) |
||
| 752 | print( |
||
| 753 | f" EVs allocated to Grid Districts " |
||
| 754 | f"(table: {EgonEvMvGridDistrict.__table__}) total count: " |
||
| 755 | f"{str(ev_count_alloc)}" |
||
| 756 | ) |
||
| 757 | |||
| 758 | # Compare with scenario target (only if not in testmode) |
||
| 759 | if TESTMODE_OFF: |
||
| 760 | np.testing.assert_allclose( |
||
| 761 | ev_count_alloc, |
||
| 762 | ev_count_target, |
||
| 763 | rtol=0.0001, |
||
| 764 | err_msg=( |
||
| 765 | "EV numbers allocated to Grid Districts seems to be flawed." |
||
| 766 | ), |
||
| 767 | ) |
||
| 768 | else: |
||
| 769 | print(" Testmode is on, skipping sanity check...") |
||
| 770 | |||
| 771 | return ev_count_alloc |
||
| 772 | |||
| 773 | def check_trip_data(): |
||
| 774 | # Check if trips start at timestep 0 and have a max. of 35040 steps |
||
| 775 | # (8760h in 15min steps) |
||
| 776 | print(" Checking timeranges...") |
||
| 777 | with db.session_scope() as session: |
||
| 778 | query = session.query( |
||
| 779 | func.count(EgonEvTrip.event_id).label("cnt") |
||
| 780 | ).filter( |
||
| 781 | or_( |
||
| 782 | and_( |
||
| 783 | EgonEvTrip.park_start > 0, |
||
| 784 | EgonEvTrip.simbev_event_id == 0, |
||
| 785 | ), |
||
| 786 | EgonEvTrip.park_end |
||
| 787 | > (60 / int(meta_run_config.stepsize)) * 8760, |
||
| 788 | ), |
||
| 789 | EgonEvTrip.scenario == scenario_name, |
||
| 790 | ) |
||
| 791 | invalid_trips = pd.read_sql( |
||
| 792 | query.statement, query.session.bind, index_col=None |
||
| 793 | ) |
||
| 794 | np.testing.assert_equal( |
||
| 795 | invalid_trips.iloc[0].cnt, |
||
| 796 | 0, |
||
| 797 | err_msg=( |
||
| 798 | f"{str(invalid_trips.iloc[0].cnt)} trips in table " |
||
| 799 | f"{EgonEvTrip.__table__} have invalid timesteps." |
||
| 800 | ), |
||
| 801 | ) |
||
| 802 | |||
| 803 | # Check if charging demand can be covered by available charging energy |
||
| 804 | # while parking |
||
| 805 | print(" Compare charging demand with available power...") |
||
| 806 | with db.session_scope() as session: |
||
| 807 | query = session.query( |
||
| 808 | func.count(EgonEvTrip.event_id).label("cnt") |
||
| 809 | ).filter( |
||
| 810 | func.round( |
||
| 811 | cast( |
||
| 812 | (EgonEvTrip.park_end - EgonEvTrip.park_start + 1) |
||
| 813 | * EgonEvTrip.charging_capacity_nominal |
||
| 814 | * (int(meta_run_config.stepsize) / 60), |
||
| 815 | Numeric, |
||
| 816 | ), |
||
| 817 | 3, |
||
| 818 | ) |
||
| 819 | < cast(EgonEvTrip.charging_demand, Numeric), |
||
| 820 | EgonEvTrip.scenario == scenario_name, |
||
| 821 | ) |
||
| 822 | invalid_trips = pd.read_sql( |
||
| 823 | query.statement, query.session.bind, index_col=None |
||
| 824 | ) |
||
| 825 | np.testing.assert_equal( |
||
| 826 | invalid_trips.iloc[0].cnt, |
||
| 827 | 0, |
||
| 828 | err_msg=( |
||
| 829 | f"In {str(invalid_trips.iloc[0].cnt)} trips (table: " |
||
| 830 | f"{EgonEvTrip.__table__}) the charging demand cannot be " |
||
| 831 | f"covered by available charging power." |
||
| 832 | ), |
||
| 833 | ) |
||
| 834 | |||
| 835 | def check_model_data(): |
||
| 836 | # Check if model components were fully created |
||
| 837 | print(" Check if all model components were created...") |
||
| 838 | # Get MVGDs which got EV allocated |
||
| 839 | with db.session_scope() as session: |
||
| 840 | query = ( |
||
| 841 | session.query( |
||
| 842 | EgonEvMvGridDistrict.bus_id, |
||
| 843 | ) |
||
| 844 | .filter( |
||
| 845 | EgonEvMvGridDistrict.scenario == scenario_name, |
||
| 846 | EgonEvMvGridDistrict.scenario_variation |
||
| 847 | == scenario_var_name, |
||
| 848 | ) |
||
| 849 | .group_by(EgonEvMvGridDistrict.bus_id) |
||
| 850 | ) |
||
| 851 | mvgds_with_ev = ( |
||
| 852 | pd.read_sql(query.statement, query.session.bind, index_col=None) |
||
| 853 | .bus_id.sort_values() |
||
| 854 | .to_list() |
||
| 855 | ) |
||
| 856 | |||
| 857 | # Load model components |
||
| 858 | with db.session_scope() as session: |
||
| 859 | query = ( |
||
| 860 | session.query( |
||
| 861 | EgonPfHvLink.bus0.label("mvgd_bus_id"), |
||
| 862 | EgonPfHvLoad.bus.label("emob_bus_id"), |
||
| 863 | EgonPfHvLoad.load_id.label("load_id"), |
||
| 864 | EgonPfHvStore.store_id.label("store_id"), |
||
| 865 | ) |
||
| 866 | .select_from(EgonPfHvLoad, EgonPfHvStore) |
||
| 867 | .join( |
||
| 868 | EgonPfHvLoadTimeseries, |
||
| 869 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id, |
||
| 870 | ) |
||
| 871 | .join( |
||
| 872 | EgonPfHvStoreTimeseries, |
||
| 873 | EgonPfHvStoreTimeseries.store_id == EgonPfHvStore.store_id, |
||
| 874 | ) |
||
| 875 | .filter( |
||
| 876 | EgonPfHvLoad.carrier == "land transport EV", |
||
| 877 | EgonPfHvLoad.scn_name == scenario_name, |
||
| 878 | EgonPfHvLoadTimeseries.scn_name == scenario_name, |
||
| 879 | EgonPfHvStore.carrier == "battery storage", |
||
| 880 | EgonPfHvStore.scn_name == scenario_name, |
||
| 881 | EgonPfHvStoreTimeseries.scn_name == scenario_name, |
||
| 882 | EgonPfHvLink.scn_name == scenario_name, |
||
| 883 | EgonPfHvLink.bus1 == EgonPfHvLoad.bus, |
||
| 884 | EgonPfHvLink.bus1 == EgonPfHvStore.bus, |
||
| 885 | ) |
||
| 886 | ) |
||
| 887 | model_components = pd.read_sql( |
||
| 888 | query.statement, query.session.bind, index_col=None |
||
| 889 | ) |
||
| 890 | |||
| 891 | # Check number of buses with model components connected |
||
| 892 | mvgd_buses_with_ev = model_components.loc[ |
||
| 893 | model_components.mvgd_bus_id.isin(mvgds_with_ev) |
||
| 894 | ] |
||
| 895 | np.testing.assert_equal( |
||
| 896 | len(mvgds_with_ev), |
||
| 897 | len(mvgd_buses_with_ev), |
||
| 898 | err_msg=( |
||
| 899 | f"Number of Grid Districts with connected model components " |
||
| 900 | f"({str(len(mvgd_buses_with_ev))} in tables egon_etrago_*) " |
||
| 901 | f"differ from number of Grid Districts that got EVs " |
||
| 902 | f"allocated ({len(mvgds_with_ev)} in table " |
||
| 903 | f"{EgonEvMvGridDistrict.__table__})." |
||
| 904 | ), |
||
| 905 | ) |
||
| 906 | |||
| 907 | # Check if all required components exist (if no id is NaN) |
||
| 908 | np.testing.assert_equal( |
||
| 909 | model_components.drop_duplicates().isna().any().any(), |
||
| 910 | False, |
||
| 911 | err_msg=( |
||
| 912 | f"Some components are missing (see True values): " |
||
| 913 | f"{model_components.drop_duplicates().isna().any()}" |
||
| 914 | ), |
||
| 915 | ) |
||
| 916 | |||
| 917 | # Get all model timeseries |
||
| 918 | print(" Loading model timeseries...") |
||
| 919 | # Get all model timeseries |
||
| 920 | model_ts_dict = { |
||
| 921 | "Load": { |
||
| 922 | "carrier": "land transport EV", |
||
| 923 | "table": EgonPfHvLoad, |
||
| 924 | "table_ts": EgonPfHvLoadTimeseries, |
||
| 925 | "column_id": "load_id", |
||
| 926 | "columns_ts": ["p_set"], |
||
| 927 | "ts": None, |
||
| 928 | }, |
||
| 929 | "Link": { |
||
| 930 | "carrier": "BEV charger", |
||
| 931 | "table": EgonPfHvLink, |
||
| 932 | "table_ts": EgonPfHvLinkTimeseries, |
||
| 933 | "column_id": "link_id", |
||
| 934 | "columns_ts": ["p_max_pu"], |
||
| 935 | "ts": None, |
||
| 936 | }, |
||
| 937 | "Store": { |
||
| 938 | "carrier": "battery storage", |
||
| 939 | "table": EgonPfHvStore, |
||
| 940 | "table_ts": EgonPfHvStoreTimeseries, |
||
| 941 | "column_id": "store_id", |
||
| 942 | "columns_ts": ["e_min_pu", "e_max_pu"], |
||
| 943 | "ts": None, |
||
| 944 | }, |
||
| 945 | } |
||
| 946 | |||
| 947 | with db.session_scope() as session: |
||
| 948 | for node, attrs in model_ts_dict.items(): |
||
| 949 | print(f" Loading {node} timeseries...") |
||
| 950 | subquery = ( |
||
| 951 | session.query( |
||
| 952 | getattr(attrs["table"], attrs["column_id"]) |
||
| 953 | ) |
||
| 954 | .filter(attrs["table"].carrier == attrs["carrier"]) |
||
| 955 | .filter(attrs["table"].scn_name == scenario_name) |
||
| 956 | .subquery() |
||
| 957 | ) |
||
| 958 | |||
| 959 | cols = [ |
||
| 960 | getattr(attrs["table_ts"], c) for c in attrs["columns_ts"] |
||
| 961 | ] |
||
| 962 | query = session.query( |
||
| 963 | getattr(attrs["table_ts"], attrs["column_id"]), *cols |
||
| 964 | ).filter( |
||
| 965 | getattr(attrs["table_ts"], attrs["column_id"]).in_( |
||
| 966 | subquery |
||
| 967 | ), |
||
| 968 | attrs["table_ts"].scn_name == scenario_name, |
||
| 969 | ) |
||
| 970 | attrs["ts"] = pd.read_sql( |
||
| 971 | query.statement, |
||
| 972 | query.session.bind, |
||
| 973 | index_col=attrs["column_id"], |
||
| 974 | ) |
||
| 975 | |||
| 976 | # Check if all timeseries have 8760 steps |
||
| 977 | print(" Checking timeranges...") |
||
| 978 | for node, attrs in model_ts_dict.items(): |
||
| 979 | for col in attrs["columns_ts"]: |
||
| 980 | ts = attrs["ts"] |
||
| 981 | invalid_ts = ts.loc[ts[col].apply(lambda _: len(_)) != 8760][ |
||
| 982 | col |
||
| 983 | ].apply(len) |
||
| 984 | np.testing.assert_equal( |
||
| 985 | len(invalid_ts), |
||
| 986 | 0, |
||
| 987 | err_msg=( |
||
| 988 | f"{str(len(invalid_ts))} rows in timeseries do not " |
||
| 989 | f"have 8760 timesteps. Table: " |
||
| 990 | f"{attrs['table_ts'].__table__}, Column: {col}, IDs: " |
||
| 991 | f"{str(list(invalid_ts.index))}" |
||
| 992 | ), |
||
| 993 | ) |
||
| 994 | |||
| 995 | # Compare total energy demand in model with some approximate values |
||
| 996 | # (per EV: 14,000 km/a, 0.17 kWh/km) |
||
| 997 | print(" Checking energy demand in model...") |
||
| 998 | total_energy_model = ( |
||
| 999 | model_ts_dict["Load"]["ts"].p_set.apply(lambda _: sum(_)).sum() |
||
| 1000 | / 1e6 |
||
| 1001 | ) |
||
| 1002 | print(f" Total energy amount in model: {total_energy_model} TWh") |
||
| 1003 | total_energy_scenario_approx = ev_count_alloc * 14000 * 0.17 / 1e9 |
||
| 1004 | print( |
||
| 1005 | f" Total approximated energy amount in scenario: " |
||
| 1006 | f"{total_energy_scenario_approx} TWh" |
||
| 1007 | ) |
||
| 1008 | np.testing.assert_allclose( |
||
| 1009 | total_energy_model, |
||
| 1010 | total_energy_scenario_approx, |
||
| 1011 | rtol=0.1, |
||
| 1012 | err_msg=( |
||
| 1013 | "The total energy amount in the model deviates heavily " |
||
| 1014 | "from the approximated value for current scenario." |
||
| 1015 | ), |
||
| 1016 | ) |
||
| 1017 | |||
| 1018 | # Compare total storage capacity |
||
| 1019 | print(" Checking storage capacity...") |
||
| 1020 | # Load storage capacities from model |
||
| 1021 | with db.session_scope() as session: |
||
| 1022 | query = session.query( |
||
| 1023 | func.sum(EgonPfHvStore.e_nom).label("e_nom") |
||
| 1024 | ).filter( |
||
| 1025 | EgonPfHvStore.scn_name == scenario_name, |
||
| 1026 | EgonPfHvStore.carrier == "battery storage", |
||
| 1027 | ) |
||
| 1028 | storage_capacity_model = ( |
||
| 1029 | pd.read_sql( |
||
| 1030 | query.statement, query.session.bind, index_col=None |
||
| 1031 | ).e_nom.sum() |
||
| 1032 | / 1e3 |
||
| 1033 | ) |
||
| 1034 | print( |
||
| 1035 | f" Total storage capacity ({EgonPfHvStore.__table__}): " |
||
| 1036 | f"{round(storage_capacity_model, 1)} GWh" |
||
| 1037 | ) |
||
| 1038 | |||
| 1039 | # Load occurences of each EV |
||
| 1040 | with db.session_scope() as session: |
||
| 1041 | query = ( |
||
| 1042 | session.query( |
||
| 1043 | EgonEvMvGridDistrict.bus_id, |
||
| 1044 | EgonEvPool.type, |
||
| 1045 | func.count(EgonEvMvGridDistrict.egon_ev_pool_ev_id).label( |
||
| 1046 | "count" |
||
| 1047 | ), |
||
| 1048 | ) |
||
| 1049 | .join( |
||
| 1050 | EgonEvPool, |
||
| 1051 | EgonEvPool.ev_id |
||
| 1052 | == EgonEvMvGridDistrict.egon_ev_pool_ev_id, |
||
| 1053 | ) |
||
| 1054 | .filter( |
||
| 1055 | EgonEvMvGridDistrict.scenario == scenario_name, |
||
| 1056 | EgonEvMvGridDistrict.scenario_variation |
||
| 1057 | == scenario_var_name, |
||
| 1058 | EgonEvPool.scenario == scenario_name, |
||
| 1059 | ) |
||
| 1060 | .group_by(EgonEvMvGridDistrict.bus_id, EgonEvPool.type) |
||
| 1061 | ) |
||
| 1224 |