def test_get_simbench_net(sb_codes=None, n=8, scenarios=None, input_path=None): """ Test nets exctracted from csv (shortened) folder. 'shortened' and 'test' must be set properly in extract_simbench_grids_from_csv.py If sb_codes is None, randomn simbench codes are tested in the number of n. If in input_path is None, no input_path will be given to get_simbench_net() """ shortened = input_path is not None and "shortened" in input_path scenarios = scenarios if scenarios is not None else [0, 1, 2] if not sb_codes: sb_codes = [] for scenario in scenarios: sb_codes += sb.collect_all_simbench_codes(scenario=scenario, shortened=shortened) first = 2 # always test the first 2 (complete_data and complete_grid) np.random.seed(int(time.time())) sb_codes = [ sb_codes[i] for i in np.append( np.random.randint(first, len(sb_codes) - first, n), range(first)) ] else: sb_codes = sb.ensure_iterability(sb_codes) for sb_code_info in sb_codes: sb_code, sb_code_params = sb.get_simbench_code_and_parameters( sb_code_info) logger.info("Get SimBench net '%s'" % sb_code) net = sb.get_simbench_net(sb_code, input_path=input_path) logger.info("Now test validity...") _test_net_validity(net, sb_code_params, shortened, input_path=input_path)
def prepare_experiment( mixed_profiles, simbench_code, pv_ratio, feed_in_limit, community_size, seed, battery_size_kWh_per_kWp=None, prediction_based=False, ): # seed for reproducible randomness np.random.seed(seed) # load simbench network net = sb.get_simbench_net(simbench_code) # distribute pv generators in net according to pv_ratio. sgen_bus_ids = distribute_pv_sgens(net, pv_ratio) # make communities with max community_size members # should be list of lists of bus ids # (with bus being the grid connection point of load/sgen) communities = make_linear_communities(net, community_size) # distribute profiles on network. Just map them by IDs. For loads and sgens profile_mapping = map_profiles(net, mixed_profiles) # make profile of resulting feed-in and demand energy after charging charging_losses_Wh = make_feed_demand_balance( sgen_bus_ids, profile_mapping, battery_size_kWh_per_kWp, prediction_based, feed_in_limit, ) # curtail the profiles community wise (sgens) losses_Wh = curtail( feed_in_limit, communities, profile_mapping, sgen_bus_ids ) absolute_values_dict = convert_profiles_to_simbench_dict( profile_mapping, net ) total_production = sum( [ sum( profile_mapping[bus]['meter_records']['energy_production [Wh]'] ) for bus in net.sgen.bus ] ) return ( net, absolute_values_dict, profile_mapping, charging_losses_Wh, losses_Wh, total_production, )
def test_get_all_simbench_profiles(): for scenario in [0, 1, 2]: profilesA = sb.get_simbench_net("1-complete_data-mixed-all-%s-sw" % str(scenario))["profiles"] profilesB = sb.get_all_simbench_profiles(scenario) for prof_table in ["load", "powerplants", "renewables", "storage"]: assert prof_table in profilesB.keys() for prof_table in profilesB.keys(): assert profilesA[prof_table].shape[0] == profilesB[ prof_table].shape[0] if scenario > 0 or prof_table != "storage": assert profilesB[prof_table].shape[0] > 0 assert not len( set(profilesA[prof_table].columns) - set(profilesB[prof_table].columns)) assert profilesB[prof_table].shape[1] > 0
# copying real states for all runs of simulation real_xs[1:, :, :] = real_xs[0, :, :] end = datetime.now() print('\nruntime: {}'.format(end - start)) return xs, real_xs, wls_xs, nUpdates # In[16]: #retrieving sample grid from simbench # sb_code = "1-MV-comm--0-sw" # sb_code = "1-LV-rural3--0-sw" sb_code = " 1-MV-urban--0-sw" net = sb.get_simbench_net(sb_code) # in simbench sgen is positive for generation but in pandapower sgen is negative for generation # converting positive value to negative for use in pandapower net.sgen.loc[:, ['p_mw', 'q_mvar']] *= -1 # standard deviations std_v_bus = 0.003 # in pu std_pq_pu = 0.0001 power_base = np.average(net.trafo.loc[:, 'sn_mva'].values) std_pq = std_pq_pu * power_base # drop initial measurements iniMeas = np.arange(0, len(net.measurement)) net.measurement.drop(iniMeas, inplace=True)
import simbench as sb grid_code = "1-HV-urban--0-sw" net = sb.get_simbench_net(grid_code) profiles = sb.get_absolute_values(net, profiles_instead_of_study_cases=True) sgen_p = profiles[("sgen", "p_mw")] load_p = profiles[("load", "p_mw")] load_q = profiles[("load", "q_mvar")] import pandas as pd X = pd.concat([sgen_p, load_p, load_q], axis=1) y = pd.read_json("./res_line/loading_percent.json") from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.1) from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) y_train = scaler.fit_transform(y_train) y_test = scaler.transform(y_test) from sklearn.neural_network import MLPRegressor ann = MLPRegressor(verbose=1) ann.fit(X_train, y_train) y_predict = ann.predict(X_test) # y_predict = scaler.inverse_transform(y_predict)