| Conditions | 24 | 
| Total Lines | 555 | 
| Code Lines | 358 | 
| 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 | """ | ||
| 668 | def sanitycheck_emobility_mit(): | ||
| 669 | """Execute sanity checks for eMobility: motorized individual travel | ||
| 670 | |||
| 671 | Checks data integrity for eGon2035, eGon2035_lowflex and eGon100RE scenario | ||
| 672 | using assertions: | ||
| 673 | 1. Allocated EV numbers and EVs allocated to grid districts | ||
| 674 | 2. Trip data (original inout data from simBEV) | ||
| 675 | 3. Model data in eTraGo PF tables (grid.egon_etrago_*) | ||
| 676 | |||
| 677 | Parameters | ||
| 678 | ---------- | ||
| 679 | None | ||
| 680 | |||
| 681 | Returns | ||
| 682 | ------- | ||
| 683 | None | ||
| 684 | """ | ||
| 685 | |||
| 686 | def check_ev_allocation(): | ||
| 687 | # Get target number for scenario | ||
| 688 | ev_count_target = scenario_variation_parameters["ev_count"] | ||
| 689 |         print(f"  Target count: {str(ev_count_target)}") | ||
| 690 | |||
| 691 | # Get allocated numbers | ||
| 692 |         ev_counts_dict = {} | ||
| 693 | with db.session_scope() as session: | ||
| 694 | for table, level in zip( | ||
| 695 | [ | ||
| 696 | EgonEvCountMvGridDistrict, | ||
| 697 | EgonEvCountMunicipality, | ||
| 698 | EgonEvCountRegistrationDistrict, | ||
| 699 | ], | ||
| 700 | ["Grid District", "Municipality", "Registration District"], | ||
| 701 | ): | ||
| 702 | query = session.query( | ||
| 703 | func.sum( | ||
| 704 | table.bev_mini | ||
| 705 | + table.bev_medium | ||
| 706 | + table.bev_luxury | ||
| 707 | + table.phev_mini | ||
| 708 | + table.phev_medium | ||
| 709 | + table.phev_luxury | ||
| 710 |                     ).label("ev_count") | ||
| 711 | ).filter( | ||
| 712 | table.scenario == scenario_name, | ||
| 713 | table.scenario_variation == scenario_var_name, | ||
| 714 | ) | ||
| 715 | |||
| 716 | ev_counts = pd.read_sql( | ||
| 717 | query.statement, query.session.bind, index_col=None | ||
| 718 | ) | ||
| 719 | ev_counts_dict[level] = ev_counts.iloc[0].ev_count | ||
| 720 | print( | ||
| 721 |                     f"    Count table: Total count for level {level} " | ||
| 722 |                     f"(table: {table.__table__}): " | ||
| 723 |                     f"{str(ev_counts_dict[level])}" | ||
| 724 | ) | ||
| 725 | |||
| 726 | # Compare with scenario target (only if not in testmode) | ||
| 727 | if TESTMODE_OFF: | ||
| 728 | for level, count in ev_counts_dict.items(): | ||
| 729 | np.testing.assert_allclose( | ||
| 730 | count, | ||
| 731 | ev_count_target, | ||
| 732 | rtol=0.0001, | ||
| 733 |                     err_msg=f"EV numbers in {level} seems to be flawed.", | ||
| 734 | ) | ||
| 735 | else: | ||
| 736 |             print("    Testmode is on, skipping sanity check...") | ||
| 737 | |||
| 738 | # Get allocated EVs in grid districts | ||
| 739 | with db.session_scope() as session: | ||
| 740 | query = session.query( | ||
| 741 | func.count(EgonEvMvGridDistrict.egon_ev_pool_ev_id).label( | ||
| 742 | "ev_count" | ||
| 743 | ), | ||
| 744 | ).filter( | ||
| 745 | EgonEvMvGridDistrict.scenario == scenario_name, | ||
| 746 | EgonEvMvGridDistrict.scenario_variation == scenario_var_name, | ||
| 747 | ) | ||
| 748 | ev_count_alloc = ( | ||
| 749 | pd.read_sql(query.statement, query.session.bind, index_col=None) | ||
| 750 | .iloc[0] | ||
| 751 | .ev_count | ||
| 752 | ) | ||
| 753 | print( | ||
| 754 | f" EVs allocated to Grid Districts " | ||
| 755 |             f"(table: {EgonEvMvGridDistrict.__table__}) total count: " | ||
| 756 |             f"{str(ev_count_alloc)}" | ||
| 757 | ) | ||
| 758 | |||
| 759 | # Compare with scenario target (only if not in testmode) | ||
| 760 | if TESTMODE_OFF: | ||
| 761 | np.testing.assert_allclose( | ||
| 762 | ev_count_alloc, | ||
| 763 | ev_count_target, | ||
| 764 | rtol=0.0001, | ||
| 765 | err_msg=( | ||
| 766 | "EV numbers allocated to Grid Districts seems to be " | ||
| 767 | "flawed." | ||
| 768 | ), | ||
| 769 | ) | ||
| 770 | else: | ||
| 771 |             print("    Testmode is on, skipping sanity check...") | ||
| 772 | |||
| 773 | return ev_count_alloc | ||
| 774 | |||
| 775 | def check_trip_data(): | ||
| 776 | # Check if trips start at timestep 0 and have a max. of 35040 steps | ||
| 777 | # (8760h in 15min steps) | ||
| 778 |         print("  Checking timeranges...") | ||
| 779 | with db.session_scope() as session: | ||
| 780 | query = session.query( | ||
| 781 |                 func.count(EgonEvTrip.event_id).label("cnt") | ||
| 782 | ).filter( | ||
| 783 | or_( | ||
| 784 | and_( | ||
| 785 | EgonEvTrip.park_start > 0, | ||
| 786 | EgonEvTrip.simbev_event_id == 0, | ||
| 787 | ), | ||
| 788 | EgonEvTrip.park_end | ||
| 789 | > (60 / int(meta_run_config.stepsize)) * 8760, | ||
| 790 | ), | ||
| 791 | EgonEvTrip.scenario == scenario_name, | ||
| 792 | ) | ||
| 793 | invalid_trips = pd.read_sql( | ||
| 794 | query.statement, query.session.bind, index_col=None | ||
| 795 | ) | ||
| 796 | np.testing.assert_equal( | ||
| 797 | invalid_trips.iloc[0].cnt, | ||
| 798 | 0, | ||
| 799 | err_msg=( | ||
| 800 |                 f"{str(invalid_trips.iloc[0].cnt)} trips in table " | ||
| 801 |                 f"{EgonEvTrip.__table__} have invalid timesteps." | ||
| 802 | ), | ||
| 803 | ) | ||
| 804 | |||
| 805 | # Check if charging demand can be covered by available charging energy | ||
| 806 | # while parking | ||
| 807 |         print("  Compare charging demand with available power...") | ||
| 808 | with db.session_scope() as session: | ||
| 809 | query = session.query( | ||
| 810 |                 func.count(EgonEvTrip.event_id).label("cnt") | ||
| 811 | ).filter( | ||
| 812 | func.round( | ||
| 813 | cast( | ||
| 814 | (EgonEvTrip.park_end - EgonEvTrip.park_start + 1) | ||
| 815 | * EgonEvTrip.charging_capacity_nominal | ||
| 816 | * (int(meta_run_config.stepsize) / 60), | ||
| 817 | Numeric, | ||
| 818 | ), | ||
| 819 | 3, | ||
| 820 | ) | ||
| 821 | < cast(EgonEvTrip.charging_demand, Numeric), | ||
| 822 | EgonEvTrip.scenario == scenario_name, | ||
| 823 | ) | ||
| 824 | invalid_trips = pd.read_sql( | ||
| 825 | query.statement, query.session.bind, index_col=None | ||
| 826 | ) | ||
| 827 | np.testing.assert_equal( | ||
| 828 | invalid_trips.iloc[0].cnt, | ||
| 829 | 0, | ||
| 830 | err_msg=( | ||
| 831 |                 f"In {str(invalid_trips.iloc[0].cnt)} trips (table: " | ||
| 832 |                 f"{EgonEvTrip.__table__}) the charging demand cannot be " | ||
| 833 | f"covered by available charging power." | ||
| 834 | ), | ||
| 835 | ) | ||
| 836 | |||
| 837 | def check_model_data(): | ||
| 838 | # Check if model components were fully created | ||
| 839 |         print("  Check if all model components were created...") | ||
| 840 | # Get MVGDs which got EV allocated | ||
| 841 | with db.session_scope() as session: | ||
| 842 | query = ( | ||
| 843 | session.query( | ||
| 844 | EgonEvMvGridDistrict.bus_id, | ||
| 845 | ) | ||
| 846 | .filter( | ||
| 847 | EgonEvMvGridDistrict.scenario == scenario_name, | ||
| 848 | EgonEvMvGridDistrict.scenario_variation | ||
| 849 | == scenario_var_name, | ||
| 850 | ) | ||
| 851 | .group_by(EgonEvMvGridDistrict.bus_id) | ||
| 852 | ) | ||
| 853 | mvgds_with_ev = ( | ||
| 854 | pd.read_sql(query.statement, query.session.bind, index_col=None) | ||
| 855 | .bus_id.sort_values() | ||
| 856 | .to_list() | ||
| 857 | ) | ||
| 858 | |||
| 859 | # Load model components | ||
| 860 | with db.session_scope() as session: | ||
| 861 | query = ( | ||
| 862 | session.query( | ||
| 863 |                     EgonPfHvLink.bus0.label("mvgd_bus_id"), | ||
| 864 |                     EgonPfHvLoad.bus.label("emob_bus_id"), | ||
| 865 |                     EgonPfHvLoad.load_id.label("load_id"), | ||
| 866 |                     EgonPfHvStore.store_id.label("store_id"), | ||
| 867 | ) | ||
| 868 | .select_from(EgonPfHvLoad, EgonPfHvStore) | ||
| 869 | .join( | ||
| 870 | EgonPfHvLoadTimeseries, | ||
| 871 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id, | ||
| 872 | ) | ||
| 873 | .join( | ||
| 874 | EgonPfHvStoreTimeseries, | ||
| 875 | EgonPfHvStoreTimeseries.store_id == EgonPfHvStore.store_id, | ||
| 876 | ) | ||
| 877 | .filter( | ||
| 878 | EgonPfHvLoad.carrier == "land transport EV", | ||
| 879 | EgonPfHvLoad.scn_name == scenario_name, | ||
| 880 | EgonPfHvLoadTimeseries.scn_name == scenario_name, | ||
| 881 | EgonPfHvStore.carrier == "battery storage", | ||
| 882 | EgonPfHvStore.scn_name == scenario_name, | ||
| 883 | EgonPfHvStoreTimeseries.scn_name == scenario_name, | ||
| 884 | EgonPfHvLink.scn_name == scenario_name, | ||
| 885 | EgonPfHvLink.bus1 == EgonPfHvLoad.bus, | ||
| 886 | EgonPfHvLink.bus1 == EgonPfHvStore.bus, | ||
| 887 | ) | ||
| 888 | ) | ||
| 889 | model_components = pd.read_sql( | ||
| 890 | query.statement, query.session.bind, index_col=None | ||
| 891 | ) | ||
| 892 | |||
| 893 | # Check number of buses with model components connected | ||
| 894 | mvgd_buses_with_ev = model_components.loc[ | ||
| 895 | model_components.mvgd_bus_id.isin(mvgds_with_ev) | ||
| 896 | ] | ||
| 897 | np.testing.assert_equal( | ||
| 898 | len(mvgds_with_ev), | ||
| 899 | len(mvgd_buses_with_ev), | ||
| 900 | err_msg=( | ||
| 901 | f"Number of Grid Districts with connected model components " | ||
| 902 |                 f"({str(len(mvgd_buses_with_ev))} in tables egon_etrago_*) " | ||
| 903 | f"differ from number of Grid Districts that got EVs " | ||
| 904 |                 f"allocated ({len(mvgds_with_ev)} in table " | ||
| 905 |                 f"{EgonEvMvGridDistrict.__table__})." | ||
| 906 | ), | ||
| 907 | ) | ||
| 908 | |||
| 909 | # Check if all required components exist (if no id is NaN) | ||
| 910 | np.testing.assert_equal( | ||
| 911 | model_components.drop_duplicates().isna().any().any(), | ||
| 912 | False, | ||
| 913 | err_msg=( | ||
| 914 | f"Some components are missing (see True values): " | ||
| 915 |                 f"{model_components.drop_duplicates().isna().any()}" | ||
| 916 | ), | ||
| 917 | ) | ||
| 918 | |||
| 919 | # Get all model timeseries | ||
| 920 |         print("  Loading model timeseries...") | ||
| 921 | # Get all model timeseries | ||
| 922 |         model_ts_dict = { | ||
| 923 |             "Load": { | ||
| 924 | "carrier": "land transport EV", | ||
| 925 | "table": EgonPfHvLoad, | ||
| 926 | "table_ts": EgonPfHvLoadTimeseries, | ||
| 927 | "column_id": "load_id", | ||
| 928 | "columns_ts": ["p_set"], | ||
| 929 | "ts": None, | ||
| 930 | }, | ||
| 931 |             "Link": { | ||
| 932 | "carrier": "BEV charger", | ||
| 933 | "table": EgonPfHvLink, | ||
| 934 | "table_ts": EgonPfHvLinkTimeseries, | ||
| 935 | "column_id": "link_id", | ||
| 936 | "columns_ts": ["p_max_pu"], | ||
| 937 | "ts": None, | ||
| 938 | }, | ||
| 939 |             "Store": { | ||
| 940 | "carrier": "battery storage", | ||
| 941 | "table": EgonPfHvStore, | ||
| 942 | "table_ts": EgonPfHvStoreTimeseries, | ||
| 943 | "column_id": "store_id", | ||
| 944 | "columns_ts": ["e_min_pu", "e_max_pu"], | ||
| 945 | "ts": None, | ||
| 946 | }, | ||
| 947 | } | ||
| 948 | |||
| 949 | with db.session_scope() as session: | ||
| 950 | for node, attrs in model_ts_dict.items(): | ||
| 951 |                 print(f"    Loading {node} timeseries...") | ||
| 952 | subquery = ( | ||
| 953 | session.query(getattr(attrs["table"], attrs["column_id"])) | ||
| 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 | ) | ||
| 1062 | count_per_ev_all = pd.read_sql( | ||
| 1063 | query.statement, query.session.bind, index_col="bus_id" | ||
| 1064 | ) | ||
| 1065 | count_per_ev_all["bat_cap"] = count_per_ev_all.type.map( | ||
| 1066 | meta_tech_data.battery_capacity | ||
| 1067 | ) | ||
| 1068 | count_per_ev_all["bat_cap_total_MWh"] = ( | ||
| 1069 | count_per_ev_all["count"] * count_per_ev_all.bat_cap / 1e3 | ||
| 1070 | ) | ||
| 1071 | storage_capacity_simbev = count_per_ev_all.bat_cap_total_MWh.div( | ||
| 1072 | 1e3 | ||
| 1073 | ).sum() | ||
| 1074 | print( | ||
| 1075 | f" Total storage capacity (simBEV): " | ||
| 1076 |             f"{round(storage_capacity_simbev, 1)} GWh" | ||
| 1077 | ) | ||
| 1078 | |||
| 1079 | np.testing.assert_allclose( | ||
| 1080 | storage_capacity_model, | ||
| 1081 | storage_capacity_simbev, | ||
| 1082 | rtol=0.01, | ||
| 1083 | err_msg=( | ||
| 1084 | "The total storage capacity in the model deviates heavily " | ||
| 1085 | "from the input data provided by simBEV for current scenario." | ||
| 1086 | ), | ||
| 1087 | ) | ||
| 1088 | |||
| 1089 | # Check SoC storage constraint: e_min_pu < e_max_pu for all timesteps | ||
| 1090 |         print("  Validating SoC constraints...") | ||
| 1091 | stores_with_invalid_soc = [] | ||
| 1092 | for idx, row in model_ts_dict["Store"]["ts"].iterrows(): | ||
| 1093 | ts = row[["e_min_pu", "e_max_pu"]] | ||
| 1094 | x = np.array(ts.e_min_pu) > np.array(ts.e_max_pu) | ||
| 1095 | if x.any(): | ||
| 1096 | stores_with_invalid_soc.append(idx) | ||
| 1097 | |||
| 1098 | np.testing.assert_equal( | ||
| 1099 | len(stores_with_invalid_soc), | ||
| 1100 | 0, | ||
| 1101 | err_msg=( | ||
| 1102 | f"The store constraint e_min_pu < e_max_pu does not apply " | ||
| 1103 |                 f"for some storages in {EgonPfHvStoreTimeseries.__table__}. " | ||
| 1104 |                 f"Invalid store_ids: {stores_with_invalid_soc}" | ||
| 1105 | ), | ||
| 1106 | ) | ||
| 1107 | |||
| 1108 | def check_model_data_lowflex_eGon2035(): | ||
| 1109 | # TODO: Add eGon100RE_lowflex | ||
| 1110 |         print("") | ||
| 1111 |         print("SCENARIO: eGon2035_lowflex") | ||
| 1112 | |||
| 1113 | # Compare driving load and charging load | ||
| 1114 |         print("  Loading eGon2035 model timeseries: driving load...") | ||
| 1115 | with db.session_scope() as session: | ||
| 1116 | query = ( | ||
| 1117 | session.query( | ||
| 1118 | EgonPfHvLoad.load_id, | ||
| 1119 | EgonPfHvLoadTimeseries.p_set, | ||
| 1120 | ) | ||
| 1121 | .join( | ||
| 1122 | EgonPfHvLoadTimeseries, | ||
| 1123 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id, | ||
| 1124 | ) | ||
| 1125 | .filter( | ||
| 1126 | EgonPfHvLoad.carrier == "land transport EV", | ||
| 1127 | EgonPfHvLoad.scn_name == "eGon2035", | ||
| 1128 | EgonPfHvLoadTimeseries.scn_name == "eGon2035", | ||
| 1129 | ) | ||
| 1130 | ) | ||
| 1131 | model_driving_load = pd.read_sql( | ||
| 1132 | query.statement, query.session.bind, index_col=None | ||
| 1133 | ) | ||
| 1134 | driving_load = np.array(model_driving_load.p_set.to_list()).sum(axis=0) | ||
| 1135 | |||
| 1136 | print( | ||
| 1137 | " Loading eGon2035_lowflex model timeseries: dumb charging " | ||
| 1138 | "load..." | ||
| 1139 | ) | ||
| 1140 | with db.session_scope() as session: | ||
| 1141 | query = ( | ||
| 1142 | session.query( | ||
| 1143 | EgonPfHvLoad.load_id, | ||
| 1144 | EgonPfHvLoadTimeseries.p_set, | ||
| 1145 | ) | ||
| 1146 | .join( | ||
| 1147 | EgonPfHvLoadTimeseries, | ||
| 1148 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id, | ||
| 1149 | ) | ||
| 1150 | .filter( | ||
| 1151 | EgonPfHvLoad.carrier == "land transport EV", | ||
| 1152 | EgonPfHvLoad.scn_name == "eGon2035_lowflex", | ||
| 1153 | EgonPfHvLoadTimeseries.scn_name == "eGon2035_lowflex", | ||
| 1154 | ) | ||
| 1155 | ) | ||
| 1156 | model_charging_load_lowflex = pd.read_sql( | ||
| 1157 | query.statement, query.session.bind, index_col=None | ||
| 1158 | ) | ||
| 1159 | charging_load = np.array( | ||
| 1160 | model_charging_load_lowflex.p_set.to_list() | ||
| 1161 | ).sum(axis=0) | ||
| 1162 | |||
| 1163 | # Ratio of driving and charging load should be 0.9 due to charging | ||
| 1164 | # efficiency | ||
| 1165 |         print("  Compare cumulative loads...") | ||
| 1166 |         print(f"    Driving load (eGon2035): {driving_load.sum() / 1e6} TWh") | ||
| 1167 | print( | ||
| 1168 | f" Dumb charging load (eGon2035_lowflex): " | ||
| 1169 |             f"{charging_load.sum() / 1e6} TWh" | ||
| 1170 | ) | ||
| 1171 | driving_load_theoretical = ( | ||
| 1172 | float(meta_run_config.eta_cp) * charging_load.sum() | ||
| 1173 | ) | ||
| 1174 | np.testing.assert_allclose( | ||
| 1175 | driving_load.sum(), | ||
| 1176 | driving_load_theoretical, | ||
| 1177 | rtol=0.01, | ||
| 1178 | err_msg=( | ||
| 1179 | f"The driving load (eGon2035) deviates by more than 1% " | ||
| 1180 | f"from the theoretical driving load calculated from charging " | ||
| 1181 | f"load (eGon2035_lowflex) with an efficiency of " | ||
| 1182 |                 f"{float(meta_run_config.eta_cp)}." | ||
| 1183 | ), | ||
| 1184 | ) | ||
| 1185 | |||
| 1186 |     print("=====================================================") | ||
| 1187 |     print("=== SANITY CHECKS FOR MOTORIZED INDIVIDUAL TRAVEL ===") | ||
| 1188 |     print("=====================================================") | ||
| 1189 | |||
| 1190 | for scenario_name in ["eGon2035", "eGon100RE"]: | ||
| 1191 | scenario_var_name = DATASET_CFG["scenario"]["variation"][scenario_name] | ||
| 1192 | |||
| 1193 |         print("") | ||
| 1194 |         print(f"SCENARIO: {scenario_name}, VARIATION: {scenario_var_name}") | ||
| 1195 | |||
| 1196 | # Load scenario params for scenario and scenario variation | ||
| 1197 | scenario_variation_parameters = get_sector_parameters( | ||
| 1198 | "mobility", scenario=scenario_name | ||
| 1199 | )["motorized_individual_travel"][scenario_var_name] | ||
| 1200 | |||
| 1201 | # Load simBEV run config and tech data | ||
| 1202 | meta_run_config = read_simbev_metadata_file( | ||
| 1203 | scenario_name, "config" | ||
| 1204 | ).loc["basic"] | ||
| 1205 | meta_tech_data = read_simbev_metadata_file(scenario_name, "tech_data") | ||
| 1206 | |||
| 1207 |         print("") | ||
| 1208 |         print("Checking EV counts...") | ||
| 1209 | ev_count_alloc = check_ev_allocation() | ||
| 1210 | |||
| 1211 |         print("") | ||
| 1212 |         print("Checking trip data...") | ||
| 1213 | check_trip_data() | ||
| 1214 | |||
| 1215 |         print("") | ||
| 1216 |         print("Checking model data...") | ||
| 1217 | check_model_data() | ||
| 1218 | |||
| 1219 |     print("") | ||
| 1220 | check_model_data_lowflex_eGon2035() | ||
| 1221 | |||
| 1222 |     print("=====================================================") | ||
| 1223 |