Ejemplo n.º 1
0
Archivo: Tester.py Proyecto: NREL/PyDSS
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))
Ejemplo n.º 2
0
def run_test(tomlpath):
    pydss_obj = OpenDSS()
    with open(tomlpath) as f_in:
        args = toml.load(f_in)
    try:
        validate_settings(args)
        print(f'Parameter validation a success')
    except Exception as e:
        print(f"Invalid simulation settings passed, {e}")
        return

    pydss_obj.init(args)
    export_path = os.path.join(pydss_obj._dssPath['Export'],
                               args['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)
    print("{} seconds".format(time.time() - st))
Ejemplo n.º 3
0
class PyDSS:
    commands = {"run": None}

    def __init__(self, event=None, queue=None, parameters=None):

        self.initalized = False
        self.uuid = current_process().name
        ''' TODO: work on logging.yaml file'''

        logging.info("{} - initialized ".format({self.uuid}))

        self.shutdownevent = event
        self.queue = queue

        try:
            params = restructure_dictionary(parameters['parameters'])
            self.pydss_obj = OpenDSS(params)
            export_path = os.path.join(self.pydss_obj._dssPath['Export'],
                                       params['Project']['Active Scenario'])
            Steps, sTime, eTime = self.pydss_obj._dssSolver.SimulationSteps()
            self.a_writer = JSONwriter(export_path, Steps)
            self.initalized = True
        except:
            result = {
                "Status": 500,
                "Message": f"Failed to create a PyDSS instance"
            }
            self.queue.put(result)
            return

        # self.RunSimulation()
        logger.info("{} - pydss dispatched".format(self.uuid))

        result = {
            "Status": 200,
            "Message": "PyDSS {} successfully initialized.".format(self.uuid),
            "UUID": self.uuid
        }

        if self.queue != None: self.queue.put(result)

        self.run_process()

    def run_process(self):
        logger.info("PyDSS simulation starting")
        while not self.shutdownevent.is_set():
            try:
                task = self.queue.get()
                if task == 'END':
                    break
                elif "parameters" not in task:
                    result = {"Status": 500, "Message": "No parameters passed"}
                else:
                    command = task["command"]
                    parameters = task["parameters"]
                    if hasattr(self, command):
                        func = getattr(self, command)
                        status, msg = func(parameters)
                        result = {
                            "Status": status,
                            "Message": msg,
                            "UUID": self.uuid
                        }
                    else:
                        logger.info(f"{command} is not a valid PyDSS command")
                        result = {
                            "Status": 500,
                            "Message":
                            f"{command} is not a valid PyDSS command"
                        }
                self.queue.put(result)

            except Empty:
                continue

            except (KeyboardInterrupt, SystemExit):
                break
        logger.info(f"PyDSS subprocess {self.uuid} has ended")

    def close_instance(self):
        del self.pydss_obj
        logger.info(f'PyDSS case {self.uuid} closed.')

    def run(self, params):
        if self.initalized:
            try:
                Steps, sTime, eTime = self.pydss_obj._dssSolver.SimulationSteps(
                )
                for i in range(Steps):
                    results = self.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
                    self.a_writer.write(
                        self.pydss_obj._Options["Helics"]["Federate name"],
                        self.pydss_obj._dssSolver.GetTotalSeconds(),
                        restructured_results, i)

                self.initalized = False
                return 200, f"Simulation complete..."
            except Exception as e:
                self.initalized = False
                return 500, f"Simulation crashed at at simulation time step: {self.pydss_obj._dssSolver.GetDateTime()}, {e}"
        else:
            return 500, f"No project initialized. Load a project first using the 'init' command"

    def registerPubSubs(self, params):
        subs = params["Subscriptions"]
        pubs = params["Publications"]
        self.pydss_obj._HI.registerPubSubTags(pubs, subs)
        return 200, f"Publications and subscriptions have been registered; Federate has entered execution mode"