def run_test(tomlpath): try: settings = load_simulation_settings(Path(tomlpath)) except Exception as e: logger.error(f"Invalid simulation settings passed, {e}") return pydss_obj = OpenDSS(settings) export_path = os.path.join(pydss_obj._dssPath['Export'], settings.project.active_scenario) Steps, sTime, eTime = pydss_obj._dssSolver.SimulationSteps() writer = DataWriter(export_path, format="json", columnLength=Steps) st = time.time() for i in range(Steps): results = pydss_obj.RunStep(i) restructured_results = {} for k, val in results.items(): if "." not in k: class_name = "Bus" elem_name = k else: class_name, elem_name = k.split(".") if class_name not in restructured_results: restructured_results[class_name] = {} if not isinstance(val, complex): restructured_results[class_name][elem_name] = val writer.write(pydss_obj._Options["Helics"]["Federate name"], pydss_obj._dssSolver.GetTotalSeconds(), restructured_results, i) logger.debug("{} seconds".format(time.time() - st))
def __init__(self, project_dir, simulation_file): self._project_dir = project_dir self._scenarios_dir = os.path.join(self._project_dir, SCENARIOS) self._dss_dir = os.path.join(self._project_dir, "DSSfiles") self._settings = load_simulation_settings( os.path.join(self._project_dir, simulation_file)) self._check_scenarios()
def simulation_settings(): project_path = Path(tempfile.gettempdir()) / "pydss_projects" if project_path.exists(): shutil.rmtree(project_path) project_name = "test_project" project_path.mkdir() filename = create_simulation_settings(project_path, project_name, ["s1"]) yield load_simulation_settings(filename) if os.path.exists(STORE_FILENAME): os.remove(STORE_FILENAME) if project_path.exists(): shutil.rmtree(project_path)
def load_simulation_settings(project_path, simulations_file): """Return the simulation settings for a project, using defaults if the file is not defined. Parameters ---------- project_path : Path Returns ------- SimulationSettingsModel """ filename = project_path / simulations_file if not filename.exists(): filename = project_path / DEFAULT_SIMULATION_SETTINGS_FILE assert filename.exists() return load_simulation_settings(filename)
def read_scenario_settings(self, scenario): """Read the simulation settings file for the scenario. Parameters ---------- scenario : str Scenario name Returns ------- SimulationSettingsModel """ scenario_path = Path(self._project_dir) / "Scenarios" / scenario if not scenario_path.exists(): raise InvalidParameter(f"scenario={scenario} is not present") settings_file = scenario_path / RUN_SIMULATION_FILENAME if not settings_file.exists(): raise InvalidConfiguration( f"{RUN_SIMULATION_FILENAME} does not exist. Was the scenario run?" ) return load_simulation_settings(settings_file)
def update_pydss_controllers(project_path, scenario, controller_type, controller, dss_file): """Update a scenario's controllers from an OpenDSS file. Parameters ---------- project_path : str PyDSS project path. scenario : str PyDSS scenario name in project. controller_type : str A type of PyDSS controler controller : str The controller name dss_file : str A DSS file path """ if controller_type not in READ_CONTROLLER_FUNCTIONS: supported_types = list(READ_CONTROLLER_FUNCTIONS.keys()) print( f"Invalid controller_type={controller_type}, supported: {supported_types}" ) sys.exit(1) sim_file = os.path.join(project_path, SIMULATION_SETTINGS_FILENAME) settings = load_simulation_settings(sim_file) if not settings.project.use_controller_registry: print(f"'Use Controller Registry' must be set to true in {sim_file}") sys.exit(1) registry = Registry() if not registry.is_controller_registered(controller_type, controller): print(f"{controller_type} / {controller} is not registered") sys.exit(1) data = {} filename = f"{project_path}/Scenarios/{scenario}/pyControllerList/{controller_type}.toml" if os.path.exists(filename): data = load_data(filename) for val in data.values(): if not isinstance(val, list): print(f"{filename} has an invalid format") sys.exit(1) element_names = READ_CONTROLLER_FUNCTIONS[controller_type](dss_file) num_added = 0 if controller in data: existing = set(data[controller]) final = list(existing.union(set(element_names))) data[controller] = final num_added = len(final) - len(existing) else: data[controller] = element_names num_added = len(element_names) # Remove element_names from any other controllers. set_names = set(element_names) for _controller, values in data.items(): if _controller != controller: final = set(values).difference_update(set_names) if final is None: final_list = None else: final_list = list(final) data[_controller] = final_list dump_data(data, filename) print(f"Added {num_added} names to {filename}")