Exemple #1
0
def write_model_inputs(scenario_directory, scenario_id, subscenarios,
                       subproblem, stage, conn):
    """
    Get inputs from database and write out the model input .tab file.
    :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 capacity type modules
    required_capacity_type_modules = get_required_capacity_type_modules(
        scenario_id, subscenarios, conn)
    imported_capacity_type_modules = load_tx_capacity_type_modules(
        required_capacity_type_modules)

    # Write module-specific inputs
    for op_m in required_capacity_type_modules:
        if hasattr(imported_capacity_type_modules[op_m], "write_model_inputs"):
            imported_capacity_type_modules[op_m].write_model_inputs(
                scenario_directory, scenario_id, subscenarios, subproblem,
                stage, conn)
        else:
            pass
Exemple #2
0
def validate_inputs(scenario_id, subscenarios, subproblem, stage, conn):
    """
    Get inputs from database and validate the inputs
    :param subscenarios: SubScenarios object with all subscenario info
    :param subproblem:
    :param stage:
    :param conn: database connection
    :return:
    """

    # Load in the required tx capacity type modules

    required_capacity_type_modules = get_required_capacity_type_modules(
        scenario_id, subscenarios, conn)
    imported_capacity_type_modules = load_tx_capacity_type_modules(
        required_capacity_type_modules)

    # Validate module-specific inputs
    for op_m in required_capacity_type_modules:
        if hasattr(imported_capacity_type_modules[op_m],
                   "validate_module_specific_inputs"):
            imported_capacity_type_modules[op_m]. \
                validate_module_specific_inputs(
                    scenario_id, subscenarios, subproblem, stage, conn)
        else:
            pass
Exemple #3
0
def load_model_data(m, d, data_portal, scenario_directory, subproblem, stage):
    """

    :param m:
    :param d:
    :param data_portal:
    :param scenario_directory:
    :param subproblem:
    :param stage:
    :return:
    """
    df = pd.read_csv(
        os.path.join(
            scenario_directory,
            str(subproblem),
            str(stage),
            "inputs",
            "transmission_lines.tab",
        ),
        sep="\t",
        usecols=[
            "transmission_line", "tx_capacity_type", "tx_operational_type"
        ],
    )

    # Required capacity modules are the unique set of tx capacity types
    # This list will be used to know which capacity modules to load
    required_tx_capacity_modules = df.tx_capacity_type.unique()

    # Import needed transmission capacity type modules for expression rules
    imported_tx_capacity_modules = load_tx_capacity_type_modules(
        required_tx_capacity_modules)

    # Add model components for each of the transmission capacity modules
    for op_m in required_tx_capacity_modules:
        if hasattr(imported_tx_capacity_modules[op_m], "load_model_data"):
            imported_tx_capacity_modules[op_m].load_model_data(
                m, d, data_portal, scenario_directory, subproblem, stage)
        else:
            pass
Exemple #4
0
def add_model_components(m, d, scenario_directory, subproblem, stage):
    """
    Before adding any components, this module will go through each relevant
    capacity type and add the module components for that capacity type.

    Then the following Pyomo model components are defined in this module:

    +-------------------------------------------------------------------------+
    | Sets                                                                    |
    +=========================================================================+
    | | :code:`TX_OPR_PRDS`                                                   |
    |                                                                         |
    | Two-dimensional set of the transmission lines and their operational     |
    | periods (capacity exists and is available).                             |
    +-------------------------------------------------------------------------+
    | | :code:`TX_LINES_OPR_IN_PRD`                                           |
    | | *Defined over*: :code:`PERIODS`                                       |
    |                                                                         |
    | Indexed set of transmission lines operational in each period.           |
    +-------------------------------------------------------------------------+
    | | :code:`OPR_PRDS_BY_TX_LINE`                                           |
    | | *Defined over*: :code:`TX_LINES`                                      |
    |                                                                         |
    | Indexed set of operational period for each transmission line.           |
    +-------------------------------------------------------------------------+
    | | :code:`TX_OPR_TMPS`                                                   |
    |                                                                         |
    | Two-dimensional set of the transmission lines and their operational     |
    | timepoints, derived from :code:`TX_OPR_PRDS` and the timepoitns in each |
    | period.                                                                 |
    +-------------------------------------------------------------------------+
    | | :code:`TX_LINES_OPR_IN_TMP`                                           |
    | | *Defined over*: :code:`TIMEPOINTS`                                    |
    |                                                                         |
    | Indexed set of transmission lines operatoinal in each timepoint.        |
    +-------------------------------------------------------------------------+

    |

    +-------------------------------------------------------------------------+
    | Expressions                                                             |
    +=========================================================================+
    | | :code:`Tx_Min_Capacity_MW`                                            |
    | | *Defined over*: :code:`TX_OPR_PRDS`                                   |
    |                                                                         |
    | The transmission line's minimum flow in MW (negative number indicates   |
    | flow in the opposite direction of the line's defined flow direction).   |
    | Depending on the capacity type, this can be a pre-specified amount or   |
    | a decision variable (with an associated cost).                          |
    +-------------------------------------------------------------------------+
    | | :code:`Tx_Max_Capacity_MW`                                            |
    | | *Defined over*: :code:`TX_OPR_PRDS`                                   |
    |                                                                         |
    | The transmission line's maximum flow in MW (negative number indicates   |
    | flow in the opposite direction of the line's defined flow direction).   |
    | Depending on the capacity type, this can be a pre-specified amount or   |
    | a decision variable (with an associated cost).                          |
    +-------------------------------------------------------------------------+
    | | :code:`Tx_Capacity_Cost_in_Prd`                                       |
    | | *Defined over*: :code:`TX_OPR_PRDS`                                   |
    |                                                                         |
    | The cost to have the transmission capacity available in the period.     |
    | Depending on the capacity type, this could be zero.                     |
    | If the subproblem is less than a full year (e.g. in production-         |
    | cost mode with 365 daily subproblems), the costs are scaled down        |
    | proportionally.                                                         |
    +-------------------------------------------------------------------------+
    | | :code:`Total_Tx_Capacity_Costs`                                       |
    |                                                                         |
    | The total cost of the system's transmission capacity across all periods.|
    +-------------------------------------------------------------------------+

    """

    # 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 capacity modules are the unique set of tx capacity types
    # This list will be used to know which capacity modules to load
    required_tx_capacity_modules = df.tx_capacity_type.unique()

    # Import needed transmission capacity type modules for expression rules
    imported_tx_capacity_modules = load_tx_capacity_type_modules(
        required_tx_capacity_modules)

    # Add model components for each of the transmission capacity modules
    for op_m in required_tx_capacity_modules:
        imp_op_m = imported_tx_capacity_modules[op_m]
        if hasattr(imp_op_m, "add_model_components"):
            imp_op_m.add_model_components(m, d, scenario_directory, subproblem,
                                          stage)

    # Sets
    ###########################################################################

    m.TX_OPR_PRDS = Set(
        dimen=2,
        within=m.TX_LINES * m.PERIODS,
        initialize=lambda mod: join_sets(
            mod,
            getattr(d, tx_capacity_type_operational_period_sets),
        ),
    )  # assumes capacity types model components are already added!

    m.TX_LINES_OPR_IN_PRD = Set(m.PERIODS,
                                initialize=lambda mod, period: list(
                                    set(tx for (tx, p) in mod.TX_OPR_PRDS
                                        if p == period)))

    m.OPR_PRDS_BY_TX_LINE = Set(m.TX_LINES,
                                initialize=lambda mod, tx: list(
                                    set(p for (l, p) in mod.TX_OPR_PRDS
                                        if l == tx)))

    m.TX_OPR_TMPS = Set(
        dimen=2,
        initialize=lambda mod: [(tx, tmp) for tx in mod.TX_LINES
                                for p in mod.OPR_PRDS_BY_TX_LINE[tx]
                                for tmp in mod.TMPS_IN_PRD[p]])

    m.TX_LINES_OPR_IN_TMP = Set(m.TMPS,
                                initialize=lambda mod, tmp: list(
                                    set(tx for (tx, t) in mod.TX_OPR_TMPS
                                        if t == tmp)))

    # Expressions
    ###########################################################################

    def tx_min_capacity_rule(mod, tx, p):
        tx_cap_type = mod.tx_capacity_type[tx]
        return imported_tx_capacity_modules[tx_cap_type]. \
            min_transmission_capacity_rule(mod, tx, p)

    def tx_max_capacity_rule(mod, tx, p):
        tx_cap_type = mod.tx_capacity_type[tx]
        return imported_tx_capacity_modules[tx_cap_type]. \
            max_transmission_capacity_rule(mod, tx, p)

    def tx_capacity_cost_rule(mod, tx, p):
        tx_cap_type = mod.tx_capacity_type[tx]
        return imported_tx_capacity_modules[tx_cap_type].\
            tx_capacity_cost_rule(mod, tx, p) \
            * mod.hours_in_subproblem_period[p] \
            / mod.hours_in_full_period[p]

    m.Tx_Min_Capacity_MW = Expression(m.TX_OPR_PRDS, rule=tx_min_capacity_rule)

    m.Tx_Max_Capacity_MW = Expression(m.TX_OPR_PRDS, rule=tx_max_capacity_rule)

    m.Tx_Capacity_Cost_in_Prd = Expression(m.TX_OPR_PRDS,
                                           rule=tx_capacity_cost_rule)
Exemple #5
0
def export_results(scenario_directory, subproblem, stage, m, d):
    """

    :param scenario_directory:
    :param subproblem:
    :param stage:
    :param m:
    :param d:
    :return:
    """

    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 capacity modules are the unique set of tx capacity types
    # This list will be used to know which capacity modules to load
    # Module-specific results
    required_tx_capacity_modules = df.tx_capacity_type.unique()

    # Import needed transmission capacity type modules for expression rules
    imported_tx_capacity_modules = load_tx_capacity_type_modules(
        required_tx_capacity_modules)

    # Add model components for each of the transmission capacity modules
    for op_m in required_tx_capacity_modules:
        if hasattr(imported_tx_capacity_modules[op_m],
                   "export_module_specific_results"):
            imported_tx_capacity_modules[op_m].export_module_specific_results(
                m, d, scenario_directory, subproblem, stage)
        else:
            pass

    # Export transmission capacity
    with open(os.path.join(scenario_directory, str(subproblem), str(stage),
                           "results", "transmission_capacity.csv"),
              "w",
              newline="") as f:
        writer = csv.writer(f)
        writer.writerow([
            "tx_line", "period", "load_zone_from", "load_zone_to",
            "transmission_min_capacity_mw", "transmission_max_capacity_mw"
        ])
        for (tx_line, p) in m.TX_OPR_PRDS:
            writer.writerow([
                tx_line, p, m.load_zone_from[tx_line], m.load_zone_to[tx_line],
                value(m.Tx_Min_Capacity_MW[tx_line, p]),
                value(m.Tx_Max_Capacity_MW[tx_line, p])
            ])

    # Export transmission capacity costs
    with open(os.path.join(scenario_directory, str(subproblem), str(stage),
                           "results", "costs_transmission_capacity.csv"),
              "w",
              newline="") as f:
        writer = csv.writer(f)
        writer.writerow([
            "tx_line", "period", "hours_in_full_period",
            "hours_in_subproblem_period", "load_zone_from", "load_zone_to",
            "capacity_cost"
        ])
        for (l, p) in m.TX_OPR_PRDS:
            writer.writerow([
                l, p, m.hours_in_full_period[p],
                m.hours_in_subproblem_period[p], m.load_zone_from[l],
                m.load_zone_to[l],
                value(m.Tx_Capacity_Cost_in_Prd[l, p])
            ])