| 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 | """  | 
            ||
| 810 | def sanitycheck_emobility_mit():  | 
            ||
| 811 | """Execute sanity checks for eMobility: motorized individual travel  | 
            ||
| 812 | |||
| 813 | Checks data integrity for eGon2035, eGon2035_lowflex and eGon100RE scenario  | 
            ||
| 814 | using assertions:  | 
            ||
| 815 | 1. Allocated EV numbers and EVs allocated to grid districts  | 
            ||
| 816 | 2. Trip data (original inout data from simBEV)  | 
            ||
| 817 | 3. Model data in eTraGo PF tables (grid.egon_etrago_*)  | 
            ||
| 818 | |||
| 819 | Parameters  | 
            ||
| 820 | ----------  | 
            ||
| 821 | None  | 
            ||
| 822 | |||
| 823 | Returns  | 
            ||
| 824 | -------  | 
            ||
| 825 | None  | 
            ||
| 826 | """  | 
            ||
| 827 | |||
| 828 | def check_ev_allocation():  | 
            ||
| 829 | # Get target number for scenario  | 
            ||
| 830 | ev_count_target = scenario_variation_parameters["ev_count"]  | 
            ||
| 831 |         print(f"  Target count: {str(ev_count_target)}") | 
            ||
| 832 | |||
| 833 | # Get allocated numbers  | 
            ||
| 834 |         ev_counts_dict = {} | 
            ||
| 835 | with db.session_scope() as session:  | 
            ||
| 836 | for table, level in zip(  | 
            ||
| 837 | [  | 
            ||
| 838 | EgonEvCountMvGridDistrict,  | 
            ||
| 839 | EgonEvCountMunicipality,  | 
            ||
| 840 | EgonEvCountRegistrationDistrict,  | 
            ||
| 841 | ],  | 
            ||
| 842 | ["Grid District", "Municipality", "Registration District"],  | 
            ||
| 843 | ):  | 
            ||
| 844 | query = session.query(  | 
            ||
| 845 | func.sum(  | 
            ||
| 846 | table.bev_mini  | 
            ||
| 847 | + table.bev_medium  | 
            ||
| 848 | + table.bev_luxury  | 
            ||
| 849 | + table.phev_mini  | 
            ||
| 850 | + table.phev_medium  | 
            ||
| 851 | + table.phev_luxury  | 
            ||
| 852 |                     ).label("ev_count") | 
            ||
| 853 | ).filter(  | 
            ||
| 854 | table.scenario == scenario_name,  | 
            ||
| 855 | table.scenario_variation == scenario_var_name,  | 
            ||
| 856 | )  | 
            ||
| 857 | |||
| 858 | ev_counts = pd.read_sql(  | 
            ||
| 859 | query.statement, query.session.bind, index_col=None  | 
            ||
| 860 | )  | 
            ||
| 861 | ev_counts_dict[level] = ev_counts.iloc[0].ev_count  | 
            ||
| 862 | print(  | 
            ||
| 863 |                     f"    Count table: Total count for level {level} " | 
            ||
| 864 |                     f"(table: {table.__table__}): " | 
            ||
| 865 |                     f"{str(ev_counts_dict[level])}" | 
            ||
| 866 | )  | 
            ||
| 867 | |||
| 868 | # Compare with scenario target (only if not in testmode)  | 
            ||
| 869 | if TESTMODE_OFF:  | 
            ||
| 870 | for level, count in ev_counts_dict.items():  | 
            ||
| 871 | np.testing.assert_allclose(  | 
            ||
| 872 | count,  | 
            ||
| 873 | ev_count_target,  | 
            ||
| 874 | rtol=0.0001,  | 
            ||
| 875 |                     err_msg=f"EV numbers in {level} seems to be flawed.", | 
            ||
| 876 | )  | 
            ||
| 877 | else:  | 
            ||
| 878 |             print("    Testmode is on, skipping sanity check...") | 
            ||
| 879 | |||
| 880 | # Get allocated EVs in grid districts  | 
            ||
| 881 | with db.session_scope() as session:  | 
            ||
| 882 | query = session.query(  | 
            ||
| 883 | func.count(EgonEvMvGridDistrict.egon_ev_pool_ev_id).label(  | 
            ||
| 884 | "ev_count"  | 
            ||
| 885 | ),  | 
            ||
| 886 | ).filter(  | 
            ||
| 887 | EgonEvMvGridDistrict.scenario == scenario_name,  | 
            ||
| 888 | EgonEvMvGridDistrict.scenario_variation == scenario_var_name,  | 
            ||
| 889 | )  | 
            ||
| 890 | ev_count_alloc = (  | 
            ||
| 891 | pd.read_sql(query.statement, query.session.bind, index_col=None)  | 
            ||
| 892 | .iloc[0]  | 
            ||
| 893 | .ev_count  | 
            ||
| 894 | )  | 
            ||
| 895 | print(  | 
            ||
| 896 | f" EVs allocated to Grid Districts "  | 
            ||
| 897 |             f"(table: {EgonEvMvGridDistrict.__table__}) total count: " | 
            ||
| 898 |             f"{str(ev_count_alloc)}" | 
            ||
| 899 | )  | 
            ||
| 900 | |||
| 901 | # Compare with scenario target (only if not in testmode)  | 
            ||
| 902 | if TESTMODE_OFF:  | 
            ||
| 903 | np.testing.assert_allclose(  | 
            ||
| 904 | ev_count_alloc,  | 
            ||
| 905 | ev_count_target,  | 
            ||
| 906 | rtol=0.0001,  | 
            ||
| 907 | err_msg=(  | 
            ||
| 908 | "EV numbers allocated to Grid Districts seems to be "  | 
            ||
| 909 | "flawed."  | 
            ||
| 910 | ),  | 
            ||
| 911 | )  | 
            ||
| 912 | else:  | 
            ||
| 913 |             print("    Testmode is on, skipping sanity check...") | 
            ||
| 914 | |||
| 915 | return ev_count_alloc  | 
            ||
| 916 | |||
| 917 | def check_trip_data():  | 
            ||
| 918 | # Check if trips start at timestep 0 and have a max. of 35040 steps  | 
            ||
| 919 | # (8760h in 15min steps)  | 
            ||
| 920 |         print("  Checking timeranges...") | 
            ||
| 921 | with db.session_scope() as session:  | 
            ||
| 922 | query = session.query(  | 
            ||
| 923 |                 func.count(EgonEvTrip.event_id).label("cnt") | 
            ||
| 924 | ).filter(  | 
            ||
| 925 | or_(  | 
            ||
| 926 | and_(  | 
            ||
| 927 | EgonEvTrip.park_start > 0,  | 
            ||
| 928 | EgonEvTrip.simbev_event_id == 0,  | 
            ||
| 929 | ),  | 
            ||
| 930 | EgonEvTrip.park_end  | 
            ||
| 931 | > (60 / int(meta_run_config.stepsize)) * 8760,  | 
            ||
| 932 | ),  | 
            ||
| 933 | EgonEvTrip.scenario == scenario_name,  | 
            ||
| 934 | )  | 
            ||
| 935 | invalid_trips = pd.read_sql(  | 
            ||
| 936 | query.statement, query.session.bind, index_col=None  | 
            ||
| 937 | )  | 
            ||
| 938 | np.testing.assert_equal(  | 
            ||
| 939 | invalid_trips.iloc[0].cnt,  | 
            ||
| 940 | 0,  | 
            ||
| 941 | err_msg=(  | 
            ||
| 942 |                 f"{str(invalid_trips.iloc[0].cnt)} trips in table " | 
            ||
| 943 |                 f"{EgonEvTrip.__table__} have invalid timesteps." | 
            ||
| 944 | ),  | 
            ||
| 945 | )  | 
            ||
| 946 | |||
| 947 | # Check if charging demand can be covered by available charging energy  | 
            ||
| 948 | # while parking  | 
            ||
| 949 |         print("  Compare charging demand with available power...") | 
            ||
| 950 | with db.session_scope() as session:  | 
            ||
| 951 | query = session.query(  | 
            ||
| 952 |                 func.count(EgonEvTrip.event_id).label("cnt") | 
            ||
| 953 | ).filter(  | 
            ||
| 954 | func.round(  | 
            ||
| 955 | cast(  | 
            ||
| 956 | (EgonEvTrip.park_end - EgonEvTrip.park_start + 1)  | 
            ||
| 957 | * EgonEvTrip.charging_capacity_nominal  | 
            ||
| 958 | * (int(meta_run_config.stepsize) / 60),  | 
            ||
| 959 | Numeric,  | 
            ||
| 960 | ),  | 
            ||
| 961 | 3,  | 
            ||
| 962 | )  | 
            ||
| 963 | < cast(EgonEvTrip.charging_demand, Numeric),  | 
            ||
| 964 | EgonEvTrip.scenario == scenario_name,  | 
            ||
| 965 | )  | 
            ||
| 966 | invalid_trips = pd.read_sql(  | 
            ||
| 967 | query.statement, query.session.bind, index_col=None  | 
            ||
| 968 | )  | 
            ||
| 969 | np.testing.assert_equal(  | 
            ||
| 970 | invalid_trips.iloc[0].cnt,  | 
            ||
| 971 | 0,  | 
            ||
| 972 | err_msg=(  | 
            ||
| 973 |                 f"In {str(invalid_trips.iloc[0].cnt)} trips (table: " | 
            ||
| 974 |                 f"{EgonEvTrip.__table__}) the charging demand cannot be " | 
            ||
| 975 | f"covered by available charging power."  | 
            ||
| 976 | ),  | 
            ||
| 977 | )  | 
            ||
| 978 | |||
| 979 | def check_model_data():  | 
            ||
| 980 | # Check if model components were fully created  | 
            ||
| 981 |         print("  Check if all model components were created...") | 
            ||
| 982 | # Get MVGDs which got EV allocated  | 
            ||
| 983 | with db.session_scope() as session:  | 
            ||
| 984 | query = (  | 
            ||
| 985 | session.query(  | 
            ||
| 986 | EgonEvMvGridDistrict.bus_id,  | 
            ||
| 987 | )  | 
            ||
| 988 | .filter(  | 
            ||
| 989 | EgonEvMvGridDistrict.scenario == scenario_name,  | 
            ||
| 990 | EgonEvMvGridDistrict.scenario_variation  | 
            ||
| 991 | == scenario_var_name,  | 
            ||
| 992 | )  | 
            ||
| 993 | .group_by(EgonEvMvGridDistrict.bus_id)  | 
            ||
| 994 | )  | 
            ||
| 995 | mvgds_with_ev = (  | 
            ||
| 996 | pd.read_sql(query.statement, query.session.bind, index_col=None)  | 
            ||
| 997 | .bus_id.sort_values()  | 
            ||
| 998 | .to_list()  | 
            ||
| 999 | )  | 
            ||
| 1000 | |||
| 1001 | # Load model components  | 
            ||
| 1002 | with db.session_scope() as session:  | 
            ||
| 1003 | query = (  | 
            ||
| 1004 | session.query(  | 
            ||
| 1005 |                     EgonPfHvLink.bus0.label("mvgd_bus_id"), | 
            ||
| 1006 |                     EgonPfHvLoad.bus.label("emob_bus_id"), | 
            ||
| 1007 |                     EgonPfHvLoad.load_id.label("load_id"), | 
            ||
| 1008 |                     EgonPfHvStore.store_id.label("store_id"), | 
            ||
| 1009 | )  | 
            ||
| 1010 | .select_from(EgonPfHvLoad, EgonPfHvStore)  | 
            ||
| 1011 | .join(  | 
            ||
| 1012 | EgonPfHvLoadTimeseries,  | 
            ||
| 1013 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id,  | 
            ||
| 1014 | )  | 
            ||
| 1015 | .join(  | 
            ||
| 1016 | EgonPfHvStoreTimeseries,  | 
            ||
| 1017 | EgonPfHvStoreTimeseries.store_id == EgonPfHvStore.store_id,  | 
            ||
| 1018 | )  | 
            ||
| 1019 | .filter(  | 
            ||
| 1020 | EgonPfHvLoad.carrier == "land transport EV",  | 
            ||
| 1021 | EgonPfHvLoad.scn_name == scenario_name,  | 
            ||
| 1022 | EgonPfHvLoadTimeseries.scn_name == scenario_name,  | 
            ||
| 1023 | EgonPfHvStore.carrier == "battery storage",  | 
            ||
| 1024 | EgonPfHvStore.scn_name == scenario_name,  | 
            ||
| 1025 | EgonPfHvStoreTimeseries.scn_name == scenario_name,  | 
            ||
| 1026 | EgonPfHvLink.scn_name == scenario_name,  | 
            ||
| 1027 | EgonPfHvLink.bus1 == EgonPfHvLoad.bus,  | 
            ||
| 1028 | EgonPfHvLink.bus1 == EgonPfHvStore.bus,  | 
            ||
| 1029 | )  | 
            ||
| 1030 | )  | 
            ||
| 1031 | model_components = pd.read_sql(  | 
            ||
| 1032 | query.statement, query.session.bind, index_col=None  | 
            ||
| 1033 | )  | 
            ||
| 1034 | |||
| 1035 | # Check number of buses with model components connected  | 
            ||
| 1036 | mvgd_buses_with_ev = model_components.loc[  | 
            ||
| 1037 | model_components.mvgd_bus_id.isin(mvgds_with_ev)  | 
            ||
| 1038 | ]  | 
            ||
| 1039 | np.testing.assert_equal(  | 
            ||
| 1040 | len(mvgds_with_ev),  | 
            ||
| 1041 | len(mvgd_buses_with_ev),  | 
            ||
| 1042 | err_msg=(  | 
            ||
| 1043 | f"Number of Grid Districts with connected model components "  | 
            ||
| 1044 |                 f"({str(len(mvgd_buses_with_ev))} in tables egon_etrago_*) " | 
            ||
| 1045 | f"differ from number of Grid Districts that got EVs "  | 
            ||
| 1046 |                 f"allocated ({len(mvgds_with_ev)} in table " | 
            ||
| 1047 |                 f"{EgonEvMvGridDistrict.__table__})." | 
            ||
| 1048 | ),  | 
            ||
| 1049 | )  | 
            ||
| 1050 | |||
| 1051 | # Check if all required components exist (if no id is NaN)  | 
            ||
| 1052 | np.testing.assert_equal(  | 
            ||
| 1053 | model_components.drop_duplicates().isna().any().any(),  | 
            ||
| 1054 | False,  | 
            ||
| 1055 | err_msg=(  | 
            ||
| 1056 | f"Some components are missing (see True values): "  | 
            ||
| 1057 |                 f"{model_components.drop_duplicates().isna().any()}" | 
            ||
| 1058 | ),  | 
            ||
| 1059 | )  | 
            ||
| 1060 | |||
| 1061 | # Get all model timeseries  | 
            ||
| 1062 |         print("  Loading model timeseries...") | 
            ||
| 1063 | # Get all model timeseries  | 
            ||
| 1064 |         model_ts_dict = { | 
            ||
| 1065 |             "Load": { | 
            ||
| 1066 | "carrier": "land transport EV",  | 
            ||
| 1067 | "table": EgonPfHvLoad,  | 
            ||
| 1068 | "table_ts": EgonPfHvLoadTimeseries,  | 
            ||
| 1069 | "column_id": "load_id",  | 
            ||
| 1070 | "columns_ts": ["p_set"],  | 
            ||
| 1071 | "ts": None,  | 
            ||
| 1072 | },  | 
            ||
| 1073 |             "Link": { | 
            ||
| 1074 | "carrier": "BEV charger",  | 
            ||
| 1075 | "table": EgonPfHvLink,  | 
            ||
| 1076 | "table_ts": EgonPfHvLinkTimeseries,  | 
            ||
| 1077 | "column_id": "link_id",  | 
            ||
| 1078 | "columns_ts": ["p_max_pu"],  | 
            ||
| 1079 | "ts": None,  | 
            ||
| 1080 | },  | 
            ||
| 1081 |             "Store": { | 
            ||
| 1082 | "carrier": "battery storage",  | 
            ||
| 1083 | "table": EgonPfHvStore,  | 
            ||
| 1084 | "table_ts": EgonPfHvStoreTimeseries,  | 
            ||
| 1085 | "column_id": "store_id",  | 
            ||
| 1086 | "columns_ts": ["e_min_pu", "e_max_pu"],  | 
            ||
| 1087 | "ts": None,  | 
            ||
| 1088 | },  | 
            ||
| 1089 | }  | 
            ||
| 1090 | |||
| 1091 | with db.session_scope() as session:  | 
            ||
| 1092 | for node, attrs in model_ts_dict.items():  | 
            ||
| 1093 |                 print(f"    Loading {node} timeseries...") | 
            ||
| 1094 | subquery = (  | 
            ||
| 1095 | session.query(getattr(attrs["table"], attrs["column_id"]))  | 
            ||
| 1096 | .filter(attrs["table"].carrier == attrs["carrier"])  | 
            ||
| 1097 | .filter(attrs["table"].scn_name == scenario_name)  | 
            ||
| 1098 | .subquery()  | 
            ||
| 1099 | )  | 
            ||
| 1100 | |||
| 1101 | cols = [  | 
            ||
| 1102 | getattr(attrs["table_ts"], c) for c in attrs["columns_ts"]  | 
            ||
| 1103 | ]  | 
            ||
| 1104 | query = session.query(  | 
            ||
| 1105 | getattr(attrs["table_ts"], attrs["column_id"]), *cols  | 
            ||
| 1106 | ).filter(  | 
            ||
| 1107 | getattr(attrs["table_ts"], attrs["column_id"]).in_(  | 
            ||
| 1108 | subquery  | 
            ||
| 1109 | ),  | 
            ||
| 1110 | attrs["table_ts"].scn_name == scenario_name,  | 
            ||
| 1111 | )  | 
            ||
| 1112 | attrs["ts"] = pd.read_sql(  | 
            ||
| 1113 | query.statement,  | 
            ||
| 1114 | query.session.bind,  | 
            ||
| 1115 | index_col=attrs["column_id"],  | 
            ||
| 1116 | )  | 
            ||
| 1117 | |||
| 1118 | # Check if all timeseries have 8760 steps  | 
            ||
| 1119 |         print("    Checking timeranges...") | 
            ||
| 1120 | for node, attrs in model_ts_dict.items():  | 
            ||
| 1121 | for col in attrs["columns_ts"]:  | 
            ||
| 1122 | ts = attrs["ts"]  | 
            ||
| 1123 | invalid_ts = ts.loc[ts[col].apply(lambda _: len(_)) != 8760][  | 
            ||
| 1124 | col  | 
            ||
| 1125 | ].apply(len)  | 
            ||
| 1126 | np.testing.assert_equal(  | 
            ||
| 1127 | len(invalid_ts),  | 
            ||
| 1128 | 0,  | 
            ||
| 1129 | err_msg=(  | 
            ||
| 1130 |                         f"{str(len(invalid_ts))} rows in timeseries do not " | 
            ||
| 1131 | f"have 8760 timesteps. Table: "  | 
            ||
| 1132 |                         f"{attrs['table_ts'].__table__}, Column: {col}, IDs: " | 
            ||
| 1133 |                         f"{str(list(invalid_ts.index))}" | 
            ||
| 1134 | ),  | 
            ||
| 1135 | )  | 
            ||
| 1136 | |||
| 1137 | # Compare total energy demand in model with some approximate values  | 
            ||
| 1138 | # (per EV: 14,000 km/a, 0.17 kWh/km)  | 
            ||
| 1139 |         print("  Checking energy demand in model...") | 
            ||
| 1140 | total_energy_model = (  | 
            ||
| 1141 | model_ts_dict["Load"]["ts"].p_set.apply(lambda _: sum(_)).sum()  | 
            ||
| 1142 | / 1e6  | 
            ||
| 1143 | )  | 
            ||
| 1144 |         print(f"    Total energy amount in model: {total_energy_model} TWh") | 
            ||
| 1145 | total_energy_scenario_approx = ev_count_alloc * 14000 * 0.17 / 1e9  | 
            ||
| 1146 | print(  | 
            ||
| 1147 | f" Total approximated energy amount in scenario: "  | 
            ||
| 1148 |             f"{total_energy_scenario_approx} TWh" | 
            ||
| 1149 | )  | 
            ||
| 1150 | np.testing.assert_allclose(  | 
            ||
| 1151 | total_energy_model,  | 
            ||
| 1152 | total_energy_scenario_approx,  | 
            ||
| 1153 | rtol=0.1,  | 
            ||
| 1154 | err_msg=(  | 
            ||
| 1155 | "The total energy amount in the model deviates heavily "  | 
            ||
| 1156 | "from the approximated value for current scenario."  | 
            ||
| 1157 | ),  | 
            ||
| 1158 | )  | 
            ||
| 1159 | |||
| 1160 | # Compare total storage capacity  | 
            ||
| 1161 |         print("  Checking storage capacity...") | 
            ||
| 1162 | # Load storage capacities from model  | 
            ||
| 1163 | with db.session_scope() as session:  | 
            ||
| 1164 | query = session.query(  | 
            ||
| 1165 |                 func.sum(EgonPfHvStore.e_nom).label("e_nom") | 
            ||
| 1166 | ).filter(  | 
            ||
| 1167 | EgonPfHvStore.scn_name == scenario_name,  | 
            ||
| 1168 | EgonPfHvStore.carrier == "battery storage",  | 
            ||
| 1169 | )  | 
            ||
| 1170 | storage_capacity_model = (  | 
            ||
| 1171 | pd.read_sql(  | 
            ||
| 1172 | query.statement, query.session.bind, index_col=None  | 
            ||
| 1173 | ).e_nom.sum()  | 
            ||
| 1174 | / 1e3  | 
            ||
| 1175 | )  | 
            ||
| 1176 | print(  | 
            ||
| 1177 |             f"    Total storage capacity ({EgonPfHvStore.__table__}): " | 
            ||
| 1178 |             f"{round(storage_capacity_model, 1)} GWh" | 
            ||
| 1179 | )  | 
            ||
| 1180 | |||
| 1181 | # Load occurences of each EV  | 
            ||
| 1182 | with db.session_scope() as session:  | 
            ||
| 1183 | query = (  | 
            ||
| 1184 | session.query(  | 
            ||
| 1185 | EgonEvMvGridDistrict.bus_id,  | 
            ||
| 1186 | EgonEvPool.type,  | 
            ||
| 1187 | func.count(EgonEvMvGridDistrict.egon_ev_pool_ev_id).label(  | 
            ||
| 1188 | "count"  | 
            ||
| 1189 | ),  | 
            ||
| 1190 | )  | 
            ||
| 1191 | .join(  | 
            ||
| 1192 | EgonEvPool,  | 
            ||
| 1193 | EgonEvPool.ev_id  | 
            ||
| 1194 | == EgonEvMvGridDistrict.egon_ev_pool_ev_id,  | 
            ||
| 1195 | )  | 
            ||
| 1196 | .filter(  | 
            ||
| 1197 | EgonEvMvGridDistrict.scenario == scenario_name,  | 
            ||
| 1198 | EgonEvMvGridDistrict.scenario_variation  | 
            ||
| 1199 | == scenario_var_name,  | 
            ||
| 1200 | EgonEvPool.scenario == scenario_name,  | 
            ||
| 1201 | )  | 
            ||
| 1202 | .group_by(EgonEvMvGridDistrict.bus_id, EgonEvPool.type)  | 
            ||
| 1203 | )  | 
            ||
| 1204 | count_per_ev_all = pd.read_sql(  | 
            ||
| 1205 | query.statement, query.session.bind, index_col="bus_id"  | 
            ||
| 1206 | )  | 
            ||
| 1207 | count_per_ev_all["bat_cap"] = count_per_ev_all.type.map(  | 
            ||
| 1208 | meta_tech_data.battery_capacity  | 
            ||
| 1209 | )  | 
            ||
| 1210 | count_per_ev_all["bat_cap_total_MWh"] = (  | 
            ||
| 1211 | count_per_ev_all["count"] * count_per_ev_all.bat_cap / 1e3  | 
            ||
| 1212 | )  | 
            ||
| 1213 | storage_capacity_simbev = count_per_ev_all.bat_cap_total_MWh.div(  | 
            ||
| 1214 | 1e3  | 
            ||
| 1215 | ).sum()  | 
            ||
| 1216 | print(  | 
            ||
| 1217 | f" Total storage capacity (simBEV): "  | 
            ||
| 1218 |             f"{round(storage_capacity_simbev, 1)} GWh" | 
            ||
| 1219 | )  | 
            ||
| 1220 | |||
| 1221 | np.testing.assert_allclose(  | 
            ||
| 1222 | storage_capacity_model,  | 
            ||
| 1223 | storage_capacity_simbev,  | 
            ||
| 1224 | rtol=0.01,  | 
            ||
| 1225 | err_msg=(  | 
            ||
| 1226 | "The total storage capacity in the model deviates heavily "  | 
            ||
| 1227 | "from the input data provided by simBEV for current scenario."  | 
            ||
| 1228 | ),  | 
            ||
| 1229 | )  | 
            ||
| 1230 | |||
| 1231 | # Check SoC storage constraint: e_min_pu < e_max_pu for all timesteps  | 
            ||
| 1232 |         print("  Validating SoC constraints...") | 
            ||
| 1233 | stores_with_invalid_soc = []  | 
            ||
| 1234 | for idx, row in model_ts_dict["Store"]["ts"].iterrows():  | 
            ||
| 1235 | ts = row[["e_min_pu", "e_max_pu"]]  | 
            ||
| 1236 | x = np.array(ts.e_min_pu) > np.array(ts.e_max_pu)  | 
            ||
| 1237 | if x.any():  | 
            ||
| 1238 | stores_with_invalid_soc.append(idx)  | 
            ||
| 1239 | |||
| 1240 | np.testing.assert_equal(  | 
            ||
| 1241 | len(stores_with_invalid_soc),  | 
            ||
| 1242 | 0,  | 
            ||
| 1243 | err_msg=(  | 
            ||
| 1244 | f"The store constraint e_min_pu < e_max_pu does not apply "  | 
            ||
| 1245 |                 f"for some storages in {EgonPfHvStoreTimeseries.__table__}. " | 
            ||
| 1246 |                 f"Invalid store_ids: {stores_with_invalid_soc}" | 
            ||
| 1247 | ),  | 
            ||
| 1248 | )  | 
            ||
| 1249 | |||
| 1250 | def check_model_data_lowflex_eGon2035():  | 
            ||
| 1251 | # TODO: Add eGon100RE_lowflex  | 
            ||
| 1252 |         print("") | 
            ||
| 1253 |         print("SCENARIO: eGon2035_lowflex") | 
            ||
| 1254 | |||
| 1255 | # Compare driving load and charging load  | 
            ||
| 1256 |         print("  Loading eGon2035 model timeseries: driving load...") | 
            ||
| 1257 | with db.session_scope() as session:  | 
            ||
| 1258 | query = (  | 
            ||
| 1259 | session.query(  | 
            ||
| 1260 | EgonPfHvLoad.load_id,  | 
            ||
| 1261 | EgonPfHvLoadTimeseries.p_set,  | 
            ||
| 1262 | )  | 
            ||
| 1263 | .join(  | 
            ||
| 1264 | EgonPfHvLoadTimeseries,  | 
            ||
| 1265 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id,  | 
            ||
| 1266 | )  | 
            ||
| 1267 | .filter(  | 
            ||
| 1268 | EgonPfHvLoad.carrier == "land transport EV",  | 
            ||
| 1269 | EgonPfHvLoad.scn_name == "eGon2035",  | 
            ||
| 1270 | EgonPfHvLoadTimeseries.scn_name == "eGon2035",  | 
            ||
| 1271 | )  | 
            ||
| 1272 | )  | 
            ||
| 1273 | model_driving_load = pd.read_sql(  | 
            ||
| 1274 | query.statement, query.session.bind, index_col=None  | 
            ||
| 1275 | )  | 
            ||
| 1276 | driving_load = np.array(model_driving_load.p_set.to_list()).sum(axis=0)  | 
            ||
| 1277 | |||
| 1278 | print(  | 
            ||
| 1279 | " Loading eGon2035_lowflex model timeseries: dumb charging "  | 
            ||
| 1280 | "load..."  | 
            ||
| 1281 | )  | 
            ||
| 1282 | with db.session_scope() as session:  | 
            ||
| 1283 | query = (  | 
            ||
| 1284 | session.query(  | 
            ||
| 1285 | EgonPfHvLoad.load_id,  | 
            ||
| 1286 | EgonPfHvLoadTimeseries.p_set,  | 
            ||
| 1287 | )  | 
            ||
| 1288 | .join(  | 
            ||
| 1289 | EgonPfHvLoadTimeseries,  | 
            ||
| 1290 | EgonPfHvLoadTimeseries.load_id == EgonPfHvLoad.load_id,  | 
            ||
| 1291 | )  | 
            ||
| 1292 | .filter(  | 
            ||
| 1293 | EgonPfHvLoad.carrier == "land transport EV",  | 
            ||
| 1294 | EgonPfHvLoad.scn_name == "eGon2035_lowflex",  | 
            ||
| 1295 | EgonPfHvLoadTimeseries.scn_name == "eGon2035_lowflex",  | 
            ||
| 1296 | )  | 
            ||
| 1297 | )  | 
            ||
| 1298 | model_charging_load_lowflex = pd.read_sql(  | 
            ||
| 1299 | query.statement, query.session.bind, index_col=None  | 
            ||
| 1300 | )  | 
            ||
| 1301 | charging_load = np.array(  | 
            ||
| 1302 | model_charging_load_lowflex.p_set.to_list()  | 
            ||
| 1303 | ).sum(axis=0)  | 
            ||
| 1304 | |||
| 1305 | # Ratio of driving and charging load should be 0.9 due to charging  | 
            ||
| 1306 | # efficiency  | 
            ||
| 1307 |         print("  Compare cumulative loads...") | 
            ||
| 1308 |         print(f"    Driving load (eGon2035): {driving_load.sum() / 1e6} TWh") | 
            ||
| 1309 | print(  | 
            ||
| 1310 | f" Dumb charging load (eGon2035_lowflex): "  | 
            ||
| 1311 |             f"{charging_load.sum() / 1e6} TWh" | 
            ||
| 1312 | )  | 
            ||
| 1313 | driving_load_theoretical = (  | 
            ||
| 1314 | float(meta_run_config.eta_cp) * charging_load.sum()  | 
            ||
| 1315 | )  | 
            ||
| 1316 | np.testing.assert_allclose(  | 
            ||
| 1317 | driving_load.sum(),  | 
            ||
| 1318 | driving_load_theoretical,  | 
            ||
| 1319 | rtol=0.01,  | 
            ||
| 1320 | err_msg=(  | 
            ||
| 1321 | f"The driving load (eGon2035) deviates by more than 1% "  | 
            ||
| 1322 | f"from the theoretical driving load calculated from charging "  | 
            ||
| 1323 | f"load (eGon2035_lowflex) with an efficiency of "  | 
            ||
| 1324 |                 f"{float(meta_run_config.eta_cp)}." | 
            ||
| 1325 | ),  | 
            ||
| 1326 | )  | 
            ||
| 1327 | |||
| 1328 |     print("=====================================================") | 
            ||
| 1329 |     print("=== SANITY CHECKS FOR MOTORIZED INDIVIDUAL TRAVEL ===") | 
            ||
| 1330 |     print("=====================================================") | 
            ||
| 1331 | |||
| 1332 | for scenario_name in ["eGon2035", "eGon100RE"]:  | 
            ||
| 1333 | scenario_var_name = DATASET_CFG["scenario"]["variation"][scenario_name]  | 
            ||
| 1334 | |||
| 1335 |         print("") | 
            ||
| 1336 |         print(f"SCENARIO: {scenario_name}, VARIATION: {scenario_var_name}") | 
            ||
| 1337 | |||
| 1338 | # Load scenario params for scenario and scenario variation  | 
            ||
| 1339 | scenario_variation_parameters = get_sector_parameters(  | 
            ||
| 1340 | "mobility", scenario=scenario_name  | 
            ||
| 1341 | )["motorized_individual_travel"][scenario_var_name]  | 
            ||
| 1342 | |||
| 1343 | # Load simBEV run config and tech data  | 
            ||
| 1344 | meta_run_config = read_simbev_metadata_file(  | 
            ||
| 1345 | scenario_name, "config"  | 
            ||
| 1346 | ).loc["basic"]  | 
            ||
| 1347 | meta_tech_data = read_simbev_metadata_file(scenario_name, "tech_data")  | 
            ||
| 1348 | |||
| 1349 |         print("") | 
            ||
| 1350 |         print("Checking EV counts...") | 
            ||
| 1351 | ev_count_alloc = check_ev_allocation()  | 
            ||
| 1352 | |||
| 1353 |         print("") | 
            ||
| 1354 |         print("Checking trip data...") | 
            ||
| 1355 | check_trip_data()  | 
            ||
| 1356 | |||
| 1357 |         print("") | 
            ||
| 1358 |         print("Checking model data...") | 
            ||
| 1359 | check_model_data()  | 
            ||
| 1360 | |||
| 1361 |     print("") | 
            ||
| 1362 | check_model_data_lowflex_eGon2035()  | 
            ||
| 1363 | |||
| 1364 |     print("=====================================================") | 
            ||
| 1365 |