def test_pv_reports(cleanup_project): PyDssProject.run_project( PV_REPORTS_PROJECT_PATH, simulation_file=SIMULATION_SETTINGS_FILENAME, ) results = PyDssResults(PV_REPORTS_PROJECT_PATH) # This test data doesn't have changes for Capacitors or RegControls. capacitor_change_counts = results.read_report( "Capacitor State Change Counts") assert len(capacitor_change_counts["scenarios"]) == 2 assert not capacitor_change_counts["scenarios"][1]["capacitors"] reg_control_change_counts = results.read_report( "RegControl Tap Number Change Counts") assert len(reg_control_change_counts["scenarios"]) == 2 assert not reg_control_change_counts["scenarios"][1]["reg_controls"] pv_clipping = results.read_report("PV Clipping") assert len(pv_clipping["pv_systems"]) == 5 for pv_system in pv_clipping["pv_systems"]: assert "pv_clipping" in pv_system pv_curtailment = results.read_report("PV Curtailment") assert isinstance(pv_curtailment, pd.DataFrame)
def run_example(example_name, scenarios): proc = None assert isinstance(example_name, str) assert isinstance(scenarios, list) base_projects_path = copy_examples_to_temp_folder(example_name) for S in scenarios: assert isinstance(S, dict) sim_file = S["TOML"] sup_file = S["file"] logger.info('Running scenario %s for example %s', example_name, sim_file) if sup_file != None: sup_file_path = os.path.join(base_projects_path, sup_file) assert os.path.exists(sup_file_path) dir_path = os.path.dirname(sup_file_path) dir_main = os.getcwd() try: os.chdir(dir_path) proc = subprocess.Popen([sys.executable, sup_file_path], shell=True) finally: os.chdir(dir_main) try: if sim_file: project_path = os.path.join(base_projects_path, example_name) assert os.path.exists(base_projects_path) PyDssProject.run_project(project_path, options=None, tar_project=False, zip_project=False, simulation_file=sim_file) finally: if proc != None: proc.terminate() return
def test_auto_snapshot_time_point(cleanup_project): PyDssProject.run_project( AUTO_SNAPSHOT_TIME_POINT_PROJECT_PATH, simulation_file=SIMULATION_SETTINGS_FILENAME, ) project = PyDssProject.load_project(AUTO_SNAPSHOT_TIME_POINT_PROJECT_PATH) settings = project.read_scenario_time_settings("max_pv_load_ratio") assert str(settings["start_time"]) == "2020-01-01 11:15:00"
def run_project_with_custom_exports(path, scenario, sim_file, data): """Runs a project while overriding an export config file.""" exports = f"{path}/Scenarios/{scenario}/ExportLists/Exports.toml" backup = exports + ".bk" shutil.copyfile(exports, backup) dump_data(data, exports) try: PyDssProject.run_project(path, simulation_file=sim_file) finally: os.remove(exports) os.rename(backup, exports)
def test_export_moving_averages(cleanup_project): # Compares the moving average storage/calculation with a rolling average # computed on dataset with every time point. path = CUSTOM_EXPORTS_PROJECT_PATH sim_file = SIMULATION_SETTINGS_FILENAME circuit = "Circuit.heco19021" window_size = 10 PyDssProject.run_project(path, simulation_file=sim_file) # This DataFrame will have values at every time point. df1 = _get_dataframe(path, "Circuits", "LineLosses", circuit, real_only=True) assert len(df1) == 96 df1_rm = df1.rolling(window_size).mean() data = { "Circuits": { "LineLosses": { "store_values_type": "moving_average", "window_size": window_size, }, } } run_project_with_custom_exports(path, "scenario1", sim_file, data) results = PyDssResults(path) assert len(results.scenarios) == 1 scenario = results.scenarios[0] # This DataFrame will have moving averages. df2 = _get_dataframe(path, "Circuits", "LineLossesAvg", circuit, real_only=True) assert len(df2) == 96 for val1, val2 in zip(df1_rm.iloc[:, 0].values, df2.iloc[:, 0].values): if np.isnan(val1): assert np.isnan(val2) else: assert round(val2, 5) == round(val1, 5)
def run_example(example_name, scenarios): proc = None assert isinstance(example_name, str) assert isinstance(scenarios, list) base_projects_path = copy_examples_to_temp_folder(example_name) for S in scenarios: assert isinstance(S, dict) sim_file = S["TOML"] sup_file = S["file"] print(f'Running scenario {example_name} for example {sim_file}') if sup_file != None: sup_file_path = os.path.join(base_projects_path, sup_file) assert os.path.exists(sup_file_path) dir_path = os.path.dirname(sup_file_path) dir_main = os.getcwd() try: os.chdir(dir_path) print(dir_path) print(f"Running {sup_file_path} in a subprocess") print(sys.executable) proc = subprocess.Popen([sys.executable, sup_file_path], shell=True) finally: os.chdir(dir_main) try: if sim_file: project_path = os.path.join(base_projects_path, example_name) assert os.path.exists(base_projects_path) PyDssProject.run_project(project_path, options=None, tar_project=False, zip_project=False, simulation_file=sim_file) finally: print("Run complete") if proc != None: proc.terminate() return #test_external_interfaces_example()
def test_pv_reports_per_element_per_time_point(cleanup_project): # Generates reports from data stored at every time point and then # use those to compare with the in-memory metrics. PyDssProject.run_project( PV_REPORTS_PROJECT_STORE_ALL_PATH, simulation_file=SIMULATION_SETTINGS_FILENAME, ) baseline_thermal = SimulationThermalMetricsModel(**load_data( Path(PV_REPORTS_PROJECT_STORE_ALL_PATH) / "Reports" / "thermal_metrics.json")) baseline_voltage = SimulationVoltageMetricsModel(**load_data( Path(PV_REPORTS_PROJECT_STORE_ALL_PATH) / "Reports" / "voltage_metrics.json")) baseline_feeder_losses = SimulationFeederLossesMetricsModel(**load_data( Path(PV_REPORTS_PROJECT_STORE_ALL_PATH) / "Reports" / "feeder_losses.json")) granularities = [x for x in ReportGranularity] for granularity in granularities: settings = load_data(BASE_FILENAME) settings["Reports"]["Granularity"] = granularity.value dump_data(settings, TEST_FILENAME) try: PyDssProject.run_project( PV_REPORTS_PROJECT_PATH, simulation_file=TEST_SIM_BASE_NAME, ) if granularity == ReportGranularity.PER_ELEMENT_PER_TIME_POINT: verify_skip_night() assert verify_thermal_metrics(baseline_thermal) assert verify_voltage_metrics(baseline_voltage) assert verify_feeder_losses(baseline_feeder_losses) verify_pv_reports(granularity) verify_feeder_head_metrics() finally: os.remove(TEST_FILENAME) for artifact in ARTIFACTS: if os.path.exists(artifact): os.remove(artifact)
def test_export_moving_averages(cleanup_project): # Compares the moving average storage/calculation with a rolling average # computed on dataset with every time point. path = CUSTOM_EXPORTS_PROJECT_PATH sim_file = SIMULATION_SETTINGS_FILENAME circuit = "Circuit.heco19021" window_size = 10 PyDssProject.run_project(path, simulation_file=sim_file) # This DataFrame will have values at every time point. df1 = _get_dataframe(path, "Circuits", "LineLosses", circuit) assert len(df1) == 96 df1_rm = df1.rolling(window_size).mean() data = { "Circuits": { "LineLosses": { "store_values_type": "moving_average", "window_size": window_size, }, } } run_project_with_custom_exports(path, "scenario1", sim_file, data) results = PyDssResults(path) assert len(results.scenarios) == 1 scenario = results.scenarios[0] # This DataFrame will have moving averages. df2 = _get_dataframe(path, "Circuits", "LineLossesAvg", circuit) assert len(df2) == 9 df1_index = window_size - 1 for df2_index in range(len(df2)): val1 = round(df1_rm.iloc[df1_index, 0], 5) val2 = round(df2.iloc[df2_index, 0], 5) assert val1 == val2 df1_index += window_size
def test_custom_exports(cleanup_project): all_node_voltages = _get_all_node_voltages() PyDssProject.run_project( CUSTOM_EXPORTS_PROJECT_PATH, simulation_file=SIMULATION_SETTINGS_FILENAME, ) results = PyDssResults(CUSTOM_EXPORTS_PROJECT_PATH) assert len(results.scenarios) == 1 scenario = results.scenarios[0] # Property stored at all time points. df = scenario.get_full_dataframe("Buses", "puVmagAngle") assert isinstance(df, pd.DataFrame) assert len(df) == 96 # Property stored with a moving average. df = scenario.get_dataframe("Buses", "DistanceAvg", "t9") assert isinstance(df, pd.DataFrame) assert len(df) == int(96) #assert len(df) == int(96 / 5) for val in df.iloc[9:, 0]: assert round(val, 3) == 0.082 # TODO DT: these values are no longer correct. What should they be? # Filtered value on custom function. #df = scenario.get_dataframe("Lines", "LoadingPercent", "Line.sl_22") #assert len(df) == 14 #df = scenario.get_dataframe("Lines", "LoadingPercentAvg", "Line.sl_22") # This was computed from raw data. #assert len(df) == 9 # TODO incorrect after more decimal points #assert round(df.iloc[:, 0].values[8], 2) == 22.79 # Subset of names. VoltagesMagAng has specific names, CurrentsMagAng has regex for name in ("Line.pvl_110", "Line.pvl_111", "Line.pvl_112", "Line.pvl_113"): properties = scenario.list_element_properties("Lines", element_name=name) assert "VoltagesMagAng" in properties assert "CurrentsMagAng" in properties properties = scenario.list_element_properties("Lines", element_name="Line.SL_14") assert "VoltagesMagAng" not in properties assert "CurrentsMagAng" not in properties # TODO: This metric no longer stores voltages in a dataframe. # That functionality could be recovered in PyDSS/metrics.py or we could implement this with # a different export property. #node_names = scenario.list_element_names("Nodes", "VoltageMetric") #dfs = scenario.get_filtered_dataframes("Nodes", "VoltageMetric") #assert len(node_names) == len(dfs) #assert sorted(node_names) == sorted(dfs.keys()) #for i, node_name in enumerate(node_names): # column = node_name + "__Voltage" # df = dfs[node_name] # # TODO: Slight rounding errors make this intermittent. # #expected = all_node_voltages[column] # #expected = expected[(expected < 1.02) | (expected > 1.04)] # #assert len(df[column]) == len(expected) # #assert_series_equal(df[column], expected, check_names=False) # df2 = scenario.get_dataframe("Nodes", "VoltageMetric", node_name) # assert_series_equal(df[column], df2[column], check_names=False) ## Two types of sums are stored. normal_amps_sum = scenario.get_element_property_value( "Lines", "NormalAmpsSum", "Line.pvl_110") assert normal_amps_sum == 96 * 65.0 scenario.get_element_property_value("Lines", "CurrentsSum", "Line.pvl_110") scenario.get_element_property_value("Circuits", "LossesSum", "Circuit.heco19021") sums_json = os.path.join(CUSTOM_EXPORTS_PROJECT_PATH, "Exports", "scenario1", "element_property_values.json") assert os.path.exists(sums_json) data = load_data(sums_json) assert data pv_profiles = scenario.read_pv_profiles() assert pv_profiles["pv_systems"] for info in pv_profiles["pv_systems"]: assert isinstance(info["name"], str) assert isinstance(info["irradiance"], float) assert isinstance(info["pmpp"], float) assert isinstance(info["load_shape_profile"], str) assert isinstance(info["load_shape_pmult_sum"], float)
def test_pv_powers_by_customer_type(cleanup_project): """Verify that PVSystem power output values collected by all variations match.""" path = CUSTOM_EXPORTS_PROJECT_PATH PyDssProject.run_project(path, simulation_file=SIMULATION_SETTINGS_FILENAME) com_pv_systems = set(["pvgnem_mpx000635970", "pvgnem_mpx000460267"]) res_pv_systems = set( ["pvgnem_mpx000594341", "pvgui_mpx000637601", "pvgui_mpx000460267"]) # Collect power for every PVSystem at every time point. df = _get_full_dataframe(path, "PVSystems", "Powers") com_cols, res_cols = _get_customer_type_columns(df, com_pv_systems, res_pv_systems) com_sum1 = df[com_cols].sum().sum() res_sum1 = df[res_cols].sum().sum() total_sum1 = df.sum().sum() assert total_sum1 == com_sum1 + res_sum1 # Collect a running sum for all PVSystem power output. data = { "PVSystems": { "Powers": { "store_values_type": "sum", "sum_elements": True, }, } } run_project_with_custom_exports(path, "scenario1", SIMULATION_SETTINGS_FILENAME, data) total_sum2 = sum( _get_summed_element_total(path, "PVSystems", "PowersSum").values()) assert math.isclose(total_sum1.real, total_sum2.real) and math.isclose( total_sum1.imag, total_sum2.imag) # Collect power for PVSystems aggregated by customer type at every time point. data = { "PVSystems": { "Powers": { "store_values_type": "all", "sum_groups": [{ "name": "com", "elements": list(com_pv_systems), }, { "name": "res", "elements": list(res_pv_systems), }], }, } } run_project_with_custom_exports(path, "scenario1", SIMULATION_SETTINGS_FILENAME, data) com_sum3 = _get_summed_element_dataframe(path, "PVSystems", "Powers", group="com").sum().sum() res_sum3 = _get_summed_element_dataframe(path, "PVSystems", "Powers", group="res").sum().sum() assert math.isclose(com_sum1.real, com_sum3.real) and math.isclose( com_sum1.imag, com_sum3.imag) assert math.isclose(res_sum1.real, res_sum3.real) and math.isclose( res_sum1.imag, res_sum3.imag) # Collect a running sum for all PVSystems by customer type. data = { "PVSystems": { "Powers": { "store_values_type": "sum", "sum_groups": [{ "name": "com", "elements": list(com_pv_systems), }, { "name": "res", "elements": list(res_pv_systems), }], }, } } run_project_with_custom_exports(path, "scenario1", SIMULATION_SETTINGS_FILENAME, data) com_sum4 = sum( _get_summed_element_total(path, "PVSystems", "PowersSum", group="com").values()) res_sum4 = sum( _get_summed_element_total(path, "PVSystems", "PowersSum", group="res").values()) assert math.isclose(com_sum1.real, com_sum4.real) and math.isclose( com_sum1.imag, com_sum4.imag) assert math.isclose(res_sum1.real, res_sum4.real) and math.isclose( res_sum1.imag, res_sum4.imag)
def run_test_project_by_property(tar_project, zip_project): project = PyDssProject.load_project(RUN_PROJECT_PATH) PyDssProject.run_project( RUN_PROJECT_PATH, tar_project=tar_project, zip_project=zip_project, simulation_file=SIMULATION_SETTINGS_FILENAME, ) results = PyDssResults(RUN_PROJECT_PATH) assert len(results.scenarios) == 1 assert results._hdf_store.attrs["version"] == DATA_FORMAT_VERSION scenario = results.scenarios[0] assert isinstance(scenario, PyDssScenarioResults) elem_classes = scenario.list_element_classes() expected_elem_classes = list(EXPECTED_ELEM_CLASSES_PROPERTIES.keys()) expected_elem_classes.sort() assert elem_classes == expected_elem_classes for elem_class in elem_classes: expected_properties = EXPECTED_ELEM_CLASSES_PROPERTIES[elem_class] expected_properties.sort() properties = scenario.list_element_properties(elem_class) assert properties == expected_properties for prop in properties: element_names = scenario.list_element_names(elem_class, prop) for name in element_names: df = scenario.get_dataframe(elem_class, prop, name) assert isinstance(df, pd.DataFrame) assert len(df) == 96 for name, df in scenario.iterate_dataframes(elem_class, prop): assert name in element_names assert isinstance(df, pd.DataFrame) # Test with an option. assert scenario.list_element_property_options( "Lines", "Currents") == ["phase_terminal"] df = scenario.get_dataframe("Lines", "Currents", "Line.sw0", phase_terminal="A1") assert isinstance(df, pd.DataFrame) assert len(df) == 96 assert len(df.columns) == 1 step = datetime.timedelta( seconds=project.simulation_config["Project"]["Step resolution (sec)"]) assert df.index[1] - df.index[0] == step df = scenario.get_dataframe("Lines", "CurrentsMagAng", "Line.sw0", phase_terminal="A1", mag_ang="mag") assert isinstance(df, pd.DataFrame) assert len(df) == 96 assert len(df.columns) == 1 df = scenario.get_dataframe("Lines", "CurrentsMagAng", "Line.sw0", phase_terminal=None, mag_ang="ang") assert isinstance(df, pd.DataFrame) assert len(df.columns) == 2 assert len(df) == 96 regex = re.compile(r"[ABCN]1") df = scenario.get_dataframe("Lines", "Currents", "Line.sw0", phase_terminal=regex) assert isinstance(df, pd.DataFrame) assert len(df.columns) == 1 assert len(df) == 96 option_values = scenario.get_option_values("Lines", "Currents", "Line.sw0") assert option_values == ["A1", "A2"] prop = "Currents" full_df = scenario.get_full_dataframe("Lines", prop) assert len(full_df.columns) >= len( scenario.list_element_names("Lines", prop)) for column in full_df.columns: assert "Unnamed" not in column assert len(full_df) == 96 element_info_files = scenario.list_element_info_files() assert element_info_files for filename in element_info_files: df = scenario.read_element_info_file(filename) assert isinstance(df, pd.DataFrame) # Test the shortcut. df = scenario.read_element_info_file("PVSystems") assert isinstance(df, pd.DataFrame) cap_changes = scenario.read_capacitor_changes()
def test_custom_exports(cleanup_project): PyDssProject.run_project( CUSTOM_EXPORTS_PROJECT_PATH, simulation_file=SIMULATION_SETTINGS_FILENAME, ) results = PyDssResults(CUSTOM_EXPORTS_PROJECT_PATH) assert len(results.scenarios) == 1 scenario = results.scenarios[0] # Property stored at all time points. df = scenario.get_full_dataframe("Buses", "puVmagAngle") assert isinstance(df, pd.DataFrame) assert len(df) == 96 # Property stored with a moving average. df = scenario.get_dataframe("Buses", "DistanceAvg", "t9") assert isinstance(df, pd.DataFrame) assert len(df) == int(96 / 5) for i, row in df.iterrows(): assert round(row["t9__DistanceAvg"], 3) == 0.082 transformers = scenario.list_element_names("Transformers") df = scenario.get_dataframe("Transformers", "CurrentsAvg", transformers[0]) assert len(df) < 96 df = scenario.get_dataframe("Lines", "LoadingPercentAvg", "Line.sl_22") assert len(df) == 2 # Filtered value on custom function. df = scenario.get_dataframe("Lines", "LoadingPercent", "Line.sl_22") assert len(df) == 17 # Subset of names. VoltagesMagAng has specific names, CurrentsMagAng has regex for name in ("Line.pvl_110", "Line.pvl_111", "Line.pvl_112", "Line.pvl_113"): properties = scenario.list_element_properties("Lines", element_name=name) assert "VoltagesMagAng" in properties assert "CurrentsMagAng" in properties properties = scenario.list_element_properties("Lines", element_name="Line.SL_14") assert "VoltagesMagAng" not in properties assert "CurrentsMagAng" not in properties # Two types of sums are stored. normal_amps_sum = scenario.get_element_property_number( "Lines", "NormalAmpsSum", "Line.pvl_110") assert normal_amps_sum == 96 * 65.0 scenario.get_element_property_number("Lines", "CurrentsSum", "Line.pvl_110") scenario.get_element_property_number("Circuits", "LossesSum", "Circuit.heco19021") sums_json = os.path.join(CUSTOM_EXPORTS_PROJECT_PATH, "Exports", "scenario1", "element_property_numbers.json") assert os.path.exists(sums_json) data = load_data(sums_json) assert data pv_profiles = scenario.read_pv_profiles() assert pv_profiles["pv_systems"] for info in pv_profiles["pv_systems"]: assert isinstance(info["name"], str) assert isinstance(info["irradiance"], float) assert isinstance(info["pmpp"], float) assert isinstance(info["load_shape_profile"], str) assert isinstance(info["load_shape_pmult_sum"], float)