예제 #1
0
    def run(self, logging_configured=True, tar_project=False, zip_project=False, dry_run=False):
        """Run all scenarios in the project."""
        if isinstance(self._fs_intf, PyDssArchiveFileInterfaceBase):
            raise InvalidConfiguration("cannot run from an archived project")
        if tar_project and zip_project:
            raise InvalidParameter("tar_project and zip_project cannot both be True")
        if self._simulation_config['Project']['DSS File'] == "":
            raise InvalidConfiguration("a valid opendss file needs to be passed")

        inst = instance()
        self._simulation_config["Logging"]["Pre-configured logging"] = logging_configured

        if dry_run:
            store_filename = os.path.join(tempfile.gettempdir(), STORE_FILENAME)
        else:
            store_filename = os.path.join(self._project_dir, STORE_FILENAME)

        driver = None
        if self._simulation_config["Exports"].get("Export Data In Memory", True):
            driver = "core"
        with h5py.File(store_filename, mode="w", driver=driver) as hdf_store:
            self._hdf_store = hdf_store
            self._hdf_store.attrs["version"] = DATA_FORMAT_VERSION
            for scenario in self._scenarios:
                self._simulation_config["Project"]["Active Scenario"] = scenario.name
                inst.run(self._simulation_config, self, scenario, dry_run=dry_run)
                self._estimated_space[scenario.name] = inst.get_estimated_space()

        if not dry_run:
            results = None
            export_tables = self._simulation_config["Exports"].get(
                "Export Data Tables", False
            )
            generate_reports = self._simulation_config.get("Reports", False)
            if export_tables or generate_reports:
                # Hack. Have to import here. Need to re-organize to fix.
                from PyDSS.pydss_results import PyDssResults
                results = PyDssResults(self._project_dir)
                if export_tables:
                    for scenario in results.scenarios:
                        scenario.export_data()

                if generate_reports:
                    results.generate_reports()

        if tar_project:
            self._tar_project_files()
        elif zip_project:
            self._zip_project_files()

        if dry_run and os.path.exists(store_filename):
            os.remove(store_filename)
예제 #2
0
    def run(self,
            logging_configured=True,
            tar_project=False,
            zip_project=False,
            dry_run=False):
        """Run all scenarios in the project."""
        if isinstance(self._fs_intf, PyDssArchiveFileInterfaceBase):
            raise InvalidConfiguration("cannot run from an archived project")
        if tar_project and zip_project:
            raise InvalidParameter(
                "tar_project and zip_project cannot both be True")
        if self._settings.project.dss_file == "":
            raise InvalidConfiguration(
                "a valid opendss file needs to be passed")

        inst = instance()
        if not logging_configured:
            if self._settings.logging.enable_console:
                console_level = logging.INFO
            else:
                console_level = logging.ERROR
            if self._settings.logging.enable_file:
                filename = os.path.join(self._project_dir, "Logs", "pydss.log")
            else:
                filename = None
            file_level = logging.INFO
            setup_logging(
                "PyDSS",
                filename=filename,
                console_level=console_level,
                file_level=file_level,
            )
        if dry_run:
            store_filename = os.path.join(tempfile.gettempdir(),
                                          STORE_FILENAME)
        else:
            store_filename = os.path.join(self._project_dir, STORE_FILENAME)
            self._dump_simulation_settings()

        driver = None
        if self._settings.exports.export_data_in_memory:
            driver = "core"
        if os.path.exists(store_filename):
            os.remove(store_filename)

        try:
            # This ensures that all datasets are flushed and closed after each
            # scenario. If there is an unexpected crash in a later scenario then
            # the file will still be valid for completed scenarios.
            for scenario in self._scenarios:
                with h5py.File(store_filename, mode="a",
                               driver=driver) as hdf_store:
                    self._hdf_store = hdf_store
                    self._hdf_store.attrs["version"] = DATA_FORMAT_VERSION
                    self._settings.project.active_scenario = scenario.name
                    inst.run(self._settings, self, scenario, dry_run=dry_run)
                    self._estimated_space[
                        scenario.name] = inst.get_estimated_space()

            export_tables = self._settings.exports.export_data_tables
            generate_reports = bool(self._settings.reports)
            if not dry_run and (export_tables or generate_reports):
                # Hack. Have to import here. Need to re-organize to fix.
                from PyDSS.pydss_results import PyDssResults
                results = PyDssResults(self._project_dir)
                if export_tables:
                    for scenario in results.scenarios:
                        scenario.export_data()

                if generate_reports:
                    results.generate_reports()

        except Exception:
            logger.exception("Simulation failed")
            raise

        finally:
            logging.shutdown()
            if tar_project:
                self._tar_project_files()
            elif zip_project:
                self._zip_project_files()

            if dry_run and os.path.exists(store_filename):
                os.remove(store_filename)