def load_model_data(m, d, data_portal, scenario_directory, subproblem, stage): """ Go through each relevant operational type and add load the model data for that operational type. :param m: :param d: :param data_portal: :param scenario_directory: :param subproblem: :param stage: :return: """ # Import needed operational modules df = pd.read_csv(os.path.join(scenario_directory, str(subproblem), str(stage), "inputs", "transmission_lines.tab"), sep="\t", usecols=[ "TRANSMISSION_LINES", "tx_capacity_type", "tx_operational_type" ]) required_tx_operational_modules = df.tx_operational_type.unique() # Import needed transmission operational type modules imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_operational_modules) for op_m in required_tx_operational_modules: if hasattr(imported_tx_operational_modules[op_m], "load_module_specific_data"): imported_tx_operational_modules[op_m].load_module_specific_data( m, data_portal, scenario_directory, subproblem, stage) else: pass
def validate_inputs(scenario_id, subscenarios, subproblem, stage, conn): """ Go through each relevant operational type and validate the database inputs for that operational type. :param subscenarios: SubScenarios object with all subscenario info :param subproblem: :param stage: :param conn: database connection :return: """ # Load in the required operational modules c = conn.cursor() required_tx_opchar_modules = get_required_tx_opchar_modules(scenario_id, c) imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_opchar_modules) # Validate module-specific inputs for op_m in required_tx_opchar_modules: if hasattr(imported_tx_operational_modules[op_m], "validate_module_specific_inputs"): imported_tx_operational_modules[op_m]. \ validate_module_specific_inputs( scenario_id, subscenarios, subproblem, stage, conn) else: pass
def add_model_components(m, d, scenario_directory, subproblem, stage): """ Go through each relevant operational type and add the module components for that operational type. """ # Import needed transmission operational type modules df = pd.read_csv(os.path.join(scenario_directory, str(subproblem), str(stage), "inputs", "transmission_lines.tab"), sep="\t", usecols=[ "TRANSMISSION_LINES", "tx_capacity_type", "tx_operational_type" ]) required_tx_operational_modules = df.tx_operational_type.unique() # Import needed transmission operational type modules imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_operational_modules) # Add any components specific to the transmission operational modules for op_m in required_tx_operational_modules: imp_op_m = imported_tx_operational_modules[op_m] if hasattr(imp_op_m, "add_model_components"): imp_op_m.add_model_components(m, d, scenario_directory, subproblem, stage)
def process_results(db, c, scenario_id, subscenarios, quiet): """ Go through each relevant operational type and process the results for that operational type. :param db: :param c: :param subscenarios: :param quiet: :return: """ # Load in the required operational modules required_tx_opchar_modules = get_required_tx_opchar_modules(scenario_id, c) imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_opchar_modules) # Process module-specific results for op_m in required_tx_opchar_modules: if hasattr(imported_tx_operational_modules[op_m], "process_module_specific_results"): imported_tx_operational_modules[op_m]. \ process_module_specific_results( db, c, scenario_id, subscenarios, quiet) else: pass
def import_results_into_database(scenario_id, subproblem, stage, c, db, results_directory, quiet): """ Go through each relevant operational type and import the results into the database for that operational type. :param scenario_id: :param c: :param db: :param results_directory: :param quiet: :return: """ # Load in the required operational modules required_tx_opchar_modules = get_required_tx_opchar_modules(scenario_id, c) imported_tx_operational_modules = \ load_tx_operational_type_modules(required_tx_opchar_modules) # Import module-specific results for op_m in required_tx_opchar_modules: if hasattr(imported_tx_operational_modules[op_m], "import_module_specific_results_to_database"): imported_tx_operational_modules[op_m]. \ import_module_specific_results_to_database( scenario_id, subproblem, stage, c, db, results_directory, quiet ) else: pass
def write_model_inputs( scenario_directory, scenario_id, subscenarios, subproblem, stage, conn ): """ Go through each relevant operational type and write the model inputs for that operational type based on the database. :param scenario_directory: string, the scenario directory :param subscenarios: SubScenarios object with all subscenario info :param subproblem: :param stage: :param conn: database connection :return: """ # Load in the required operational modules c = conn.cursor() required_tx_opchar_modules = get_required_tx_opchar_modules(scenario_id, c) imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_opchar_modules ) # Write module-specific inputs for op_m in required_tx_opchar_modules: if hasattr(imported_tx_operational_modules[op_m], "write_model_inputs"): imported_tx_operational_modules[op_m].write_model_inputs( scenario_directory, scenario_id, subscenarios, subproblem, stage, conn ) else: pass
def export_results(scenario_directory, subproblem, stage, m, d): """ Export operations results. :param scenario_directory: :param subproblem: :param stage: :param m: The Pyomo abstract model :param d: Dynamic components :return: Nothing """ # Transmission flows for all lines with open(os.path.join(scenario_directory, str(subproblem), str(stage), "results", "transmission_operations.csv"), "w", newline="") as \ tx_op_results_file: writer = csv.writer(tx_op_results_file) writer.writerow([ "tx_line", "lz_from", "lz_to", "timepoint", "period", "timepoint_weight", "number_of_hours_in_timepoint", "transmission_flow_mw", "transmission_losses_lz_from", "transmission_losses_lz_to" ]) for (l, tmp) in m.TX_OPR_TMPS: writer.writerow([ l, m.load_zone_from[l], m.load_zone_to[l], tmp, m.period[tmp], m.tmp_weight[tmp], m.hrs_in_tmp[tmp], value(m.Transmit_Power_MW[l, tmp]), value(m.Tx_Losses_LZ_From_MW[l, tmp]), value(m.Tx_Losses_LZ_To_MW[l, tmp]) ]) # TODO: does this belong here or in operational_types/__init__.py? # (putting it here to be in line with projects/operations/power.py) # Module-specific transmission operational results df = pd.read_csv(os.path.join(scenario_directory, str(subproblem), str(stage), "inputs", "transmission_lines.tab"), sep="\t", usecols=[ "TRANSMISSION_LINES", "tx_capacity_type", "tx_operational_type" ]) required_tx_operational_modules = df.tx_operational_type.unique() # Import needed transmission operational type modules imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_operational_modules) for op_m in required_tx_operational_modules: if hasattr(imported_tx_operational_modules[op_m], "export_module_specific_results"): imported_tx_operational_modules[op_m].\ export_module_specific_results( m, d, scenario_directory, subproblem, stage, ) else: pass
def add_model_components(m, d, scenario_directory, subproblem, stage): """ The following Pyomo model components are defined in this module: +-------------------------------------------------------------------------+ | Expressions | +=========================================================================+ | | :code:`Transmit_Power_MW` | | | *Defined over*: :code:`TX_OPR_TMPS` | | | | The power in MW sent on a transmission line (before losses). | | A positive number means the power flows in the line's defined direction,| | while a negative number means it flows in the opposite direction. | +-------------------------------------------------------------------------+ | | :code:`Transmit_Power_MW` | | | *Defined over*: :code:`TX_OPR_TMPS` | | | | The power in MW received via a transmission line (after losses). | | A positive number means the power flows in the line's defined direction,| | while a negative number means it flows in the opposite direction. | +-------------------------------------------------------------------------+ | | :code:`Tx_Losses_MW` | | | *Defined over*: :code:`TX_OPR_TMPS` | | | | Losses on the transmission line in MW. A positive number means the | | power flows in the line's defined direction when losses incurred, | | while a negative number means it flows in the opposite direction. | +-------------------------------------------------------------------------+ """ # Dynamic Inputs ########################################################################### df = pd.read_csv(os.path.join(scenario_directory, str(subproblem), str(stage), "inputs", "transmission_lines.tab"), sep="\t", usecols=[ "TRANSMISSION_LINES", "tx_capacity_type", "tx_operational_type" ]) required_tx_operational_modules = df.tx_operational_type.unique() # Import needed transmission operational type modules imported_tx_operational_modules = load_tx_operational_type_modules( required_tx_operational_modules) # TODO: should we add the module specific components here or in # operational_types/__init__.py? Doing it in __init__.py to be consistent # with projects/operations/power.py # Expressions ########################################################################### def transmit_power_rule(mod, tx, tmp): tx_op_type = mod.tx_operational_type[tx] return imported_tx_operational_modules[tx_op_type].\ transmit_power_rule(mod, tx, tmp) m.Transmit_Power_MW = Expression(m.TX_OPR_TMPS, rule=transmit_power_rule) def transmit_power_losses_lz_from_rule(mod, tx, tmp): tx_op_type = mod.tx_operational_type[tx] return imported_tx_operational_modules[tx_op_type].\ transmit_power_losses_lz_from_rule(mod, tx, tmp) m.Tx_Losses_LZ_From_MW = Expression( m.TX_OPR_TMPS, rule=transmit_power_losses_lz_from_rule) def transmit_power_losses_lz_to_rule(mod, tx, tmp): tx_op_type = mod.tx_operational_type[tx] return imported_tx_operational_modules[tx_op_type].\ transmit_power_losses_lz_to_rule(mod, tx, tmp) m.Tx_Losses_LZ_To_MW = Expression(m.TX_OPR_TMPS, rule=transmit_power_losses_lz_to_rule)