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