Esempio n. 1
0
def power_flow_worker(variation: int, nbus, nbr, n_tr, bus_names, branch_names,
                      transformer_names, bus_types,
                      calculation_inputs: List[SnapshotData],
                      options: PowerFlowOptions, dP, return_dict):
    """
    Run asynchronous power flow
    :param variation: variation id
    :param nbus: number of buses
    :param nbr: number of branches
    :param n_tr:
    :param bus_names:
    :param branch_names:
    :param transformer_names:
    :param bus_types:
    :param calculation_inputs: list of CalculationInputs' instances
    :param options: PowerFlowOptions instance
    :param dP: delta of active power (array of values of size nbus)
    :param return_dict: dictionary to return values
    :return: Nothing because it is a worker, the return is done via the return_dict variable
    """
    # create new results
    pf_results = PowerFlowResults(n=nbus,
                                  m=nbr,
                                  n_tr=n_tr,
                                  n_hvdc=0,
                                  bus_names=bus_names,
                                  branch_names=branch_names,
                                  transformer_names=transformer_names,
                                  hvdc_names=(),
                                  bus_types=bus_types)

    logger = Logger()

    # simulate each island and merge the results
    for i, calculation_input in enumerate(calculation_inputs):

        if len(calculation_input.vd) > 0:

            # run circuit power flow
            res = single_island_pf(circuit=calculation_input,
                                   Vbus=calculation_input.Vbus,
                                   Sbus=calculation_input.Sbus -
                                   dP[calculation_input.original_bus_idx],
                                   Ibus=calculation_input.Ibus,
                                   branch_rates=calculation_input.branch_rates,
                                   options=options,
                                   logger=Logger())

            # merge the results from this island
            pf_results.apply_from_island(
                results=res,
                b_idx=calculation_input.original_bus_idx,
                br_idx=calculation_input.original_branch_idx,
                tr_idx=calculation_input.original_tr_idx)

        else:
            logger.add_info('No slack nodes in the island', str(i))

    return_dict[variation] = (pf_results, logger)
Esempio n. 2
0
class ExportAllThread(QThread):
    progress_signal = Signal(float)
    progress_text = Signal(str)
    done_signal = Signal()

    def __init__(self, circuit, simulations_list, file_name):
        """
        Constructor
        :param simulations_list: list of GridCal simulation drivers
        :param file_name: name of the file where to save (.zip)
        """
        QThread.__init__(self)

        self.circuit = circuit

        self.simulations_list = simulations_list

        self.file_name = file_name

        self.valid = False

        self.logger = Logger()

        self.error_msg = ''

        self.__cancel__ = False

    def run(self):
        """
        run the file save procedure
        """

        # try:
        path, fname = os.path.split(self.file_name)

        self.progress_text.emit('Flushing ' + fname + ' into ' + fname + '...')

        self.logger = Logger()

        names_dict = {
            DeviceType.BusDevice:
            self.circuit.bus_names,
            DeviceType.BranchDevice:
            self.circuit.branch_names,
            DeviceType.BusDevice.LoadDevice:
            self.circuit.get_load_names(),
            DeviceType.BusDevice.GeneratorDevice:
            self.circuit.get_controlled_generator_names(),
            DeviceType.BusDevice.BatteryDevice:
            self.circuit.get_battery_names()
        }

        # open zip file for writing
        try:
            with zipfile.ZipFile(self.file_name, 'w',
                                 zipfile.ZIP_DEFLATED) as myzip:

                n = len(self.simulations_list)

                for k, driver in enumerate(self.simulations_list):

                    self.progress_signal.emit((k + 1) / n * 100.0)

                    for available_result in driver.results.available_results:

                        # ge the result type definition
                        result_name, device_type = available_result.value

                        self.progress_text.emit('flushing ' +
                                                driver.results.name + ' ' +
                                                result_name)

                        # save the DataFrame to the buffer
                        mdl = driver.results.mdl(result_type=available_result)

                        if mdl is not None:
                            with StringIO() as buffer:
                                filename = driver.results.name + ' ' + result_name + '.csv'
                                try:
                                    mdl.save_to_csv(buffer)
                                    myzip.writestr(filename, buffer.getvalue())
                                except ValueError:
                                    self.logger.add_error(
                                        'Value error', filename)
                        else:
                            self.logger.add_info('No results for ' +
                                                 driver.results.name + ' - ' +
                                                 result_name)

        except PermissionError:
            self.logger.add('Permission error.\nDo you have the file open?')

        self.valid = True

        # post events
        self.progress_text.emit('Done!')

        self.done_signal.emit()

    def cancel(self):
        self.__cancel__ = True