How to use the pypsa.opt.LExpression function in pypsa

To help you get started, we’ve selected a few pypsa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PyPSA / PyPSA / pypsa / opf.py View on Github external
def define_nodal_balances(network,snapshots):
    """Construct the nodal balance for all elements except the passive
    branches.

    Store the nodal balance expression in network._p_balance.
    """

    #dictionary for constraints
    network._p_balance = {(bus,sn) : LExpression()
                          for bus in network.buses.index
                          for sn in snapshots}

    efficiency = get_switchable_as_dense(network, 'Link', 'efficiency', snapshots)

    for cb in network.links.index:
        bus0 = network.links.at[cb,"bus0"]
        bus1 = network.links.at[cb,"bus1"]

        for sn in snapshots:
            network._p_balance[bus0,sn].variables.append((-1,network.model.link_p[cb,sn]))
            network._p_balance[bus1,sn].variables.append((efficiency.at[sn,cb],network.model.link_p[cb,sn]))

    #Add any other buses to which the links are attached
    for i in [int(col[3:]) for col in network.links.columns if col[:3] == "bus" and col not in ["bus0","bus1"]]:
        efficiency = get_switchable_as_dense(network, 'Link', 'efficiency{}'.format(i), snapshots)
github PyPSA / PyPSA / pypsa / opf.py View on Github external
rd = {}


    for gen in rd_gens:
        for sn, sn_prev in zip(snapshots[1:], snapshots[:-1]):
            if network.generators.at[gen, "p_nom_extendable"]:
                lhs = LExpression([(1, network.model.generator_p[gen,sn]),
                                   (-1, network.model.generator_p[gen,sn_prev]),
                                   (network.generators.at[gen, "ramp_limit_down"],
                                    network.model.generator_p_nom[gen])])
            elif not network.generators.at[gen, "committable"]:
                lhs = LExpression([(1, network.model.generator_p[gen,sn]),
                                   (-1, network.model.generator_p[gen,sn_prev])],
                                  network.generators.loc[gen, "ramp_limit_down"]*network.generators.at[gen, "p_nom"])
            else:
                lhs = LExpression([(1, network.model.generator_p[gen,sn]),
                                   (-1, network.model.generator_p[gen,sn_prev]),
                                   ((network.generators.at[gen, "ramp_limit_down"] - network.generators.at[gen, "ramp_limit_shut_down"])*network.generators.at[gen, "p_nom"],
                                    network.model.generator_status[gen,sn]),
                                   (network.generators.at[gen, "ramp_limit_shut_down"]*network.generators.at[gen, "p_nom"],
                                    network.model.generator_status[gen,sn_prev])])

            rd[gen,sn] = LConstraint(lhs,">=")

    l_constraint(network.model, "ramp_down", rd, list(rd_gens), snapshots[1:])
github PyPSA / PyPSA / pypsa / opf.py View on Github external
sub_network_cycle_index.append((subnetwork.name, col_j))


        branch_idx_attributes = []

        for cycle_i in cycle_is:
            branch_idx = branches.index[cycle_i]
            attribute_value = 1e5 * branches.at[ branch_idx, attribute] * subnetwork.C[ cycle_i, col_j]
            branch_idx_attributes.append( (branch_idx, attribute_value))

        for snapshot in snapshots:
            expression_list = [ (attribute_value,
                                 passive_branch_p[branch_idx[0], branch_idx[1], snapshot]) for (branch_idx, attribute_value) in branch_idx_attributes]

            lhs = LExpression(expression_list)
            sub_network_cycle_constraints[subnetwork.name,col_j,snapshot] = LConstraint(lhs,"==",LExpression())

    return( sub_network_cycle_index, sub_network_cycle_constraints)
github PyPSA / PyPSA / pypsa / opf.py View on Github external
min_down_time = network.generators.loc[gen,"min_down_time"]
        initial_status = network.generators.loc[gen,"initial_status"]

        blocks = max(1,len(snapshots)-min_down_time+1)

        gen_down_time = {}

        for i in range(blocks):
            #sum of 1-status
            lhs = LExpression([(-1,network.model.generator_status[gen,snapshots[j]]) for j in range(i,i+min_down_time)],min_down_time)

            if i == 0:
                rhs = LExpression([(-min_down_time,network.model.generator_status[gen,snapshots[i]])],min_down_time*initial_status)
            else:
                rhs = LExpression([(-min_down_time,network.model.generator_status[gen,snapshots[i]]),(min_down_time,network.model.generator_status[gen,snapshots[i-1]])])

            gen_down_time[i] = LConstraint(lhs,">=",rhs)

        l_constraint(network.model, "gen_down_time_{}".format(gen_i), gen_down_time,
                     range(blocks))

    ## Deal with start up costs ##

    suc_gens = fixed_committable_gens_i[network.generators.loc[fixed_committable_gens_i,"start_up_cost"] > 0]

    network.model.generator_start_up_cost = Var(list(suc_gens),snapshots,
                                                domain=NonNegativeReals)

    sucs = {}

    for gen in suc_gens:
github PyPSA / PyPSA / pypsa / opf.py View on Github external
sdc_gens = fixed_committable_gens_i[network.generators.loc[fixed_committable_gens_i,"shut_down_cost"] > 0]

    network.model.generator_shut_down_cost = Var(list(sdc_gens),snapshots,
                                                domain=NonNegativeReals)

    sdcs = {}

    for gen in sdc_gens:
        sdc = network.generators.loc[gen,"shut_down_cost"]
        initial_status = network.generators.loc[gen,"initial_status"]

        for i,sn in enumerate(snapshots):

            if i == 0:
                rhs = LExpression([(-sdc, network.model.generator_status[gen,sn])],sdc*initial_status)
            else:
                rhs = LExpression([(-sdc, network.model.generator_status[gen,sn]),(sdc,network.model.generator_status[gen,snapshots[i-1]])])

            lhs = LExpression([(1,network.model.generator_shut_down_cost[gen,sn])])

            sdcs[gen,sn] = LConstraint(lhs,">=",rhs)

    l_constraint(network.model, "generator_shut_down", sdcs, list(sdc_gens), snapshots)



    ## Deal with ramp limits without unit commitment ##

    ru_gens = network.generators.index[network.generators.ramp_limit_up.notnull()]

    ru = {}
github PyPSA / PyPSA / pypsa / opf.py View on Github external
extendable_passive_branches = passive_branches[passive_branches.s_nom_extendable]

    extendable_links = network.links[network.links.p_nom_extendable]

    suc_gens_i = network.generators.index[~network.generators.p_nom_extendable & network.generators.committable & (network.generators.start_up_cost > 0)]

    sdc_gens_i = network.generators.index[~network.generators.p_nom_extendable & network.generators.committable & (network.generators.shut_down_cost > 0)]


    marginal_cost_it = zip(get_switchable_as_iter(network, 'Generator', 'marginal_cost', snapshots),
                           get_switchable_as_iter(network, 'StorageUnit', 'marginal_cost', snapshots),
                           get_switchable_as_iter(network, 'Store', 'marginal_cost', snapshots),
                           get_switchable_as_iter(network, 'Link', 'marginal_cost', snapshots))

    objective = LExpression()


    for sn, marginal_cost in zip(snapshots, marginal_cost_it):
        gen_mc, su_mc, st_mc, link_mc = marginal_cost

        weight = network.snapshot_weightings[sn]
        for gen in network.generators.index:
            coefficient = gen_mc.at[gen] * weight
            objective.variables.extend([(coefficient, model.generator_p[gen, sn])])

        for su in network.storage_units.index:
            coefficient = su_mc.at[su] * weight
            objective.variables.extend([(coefficient, model.storage_p_dispatch[su,sn])])

        for store in network.stores.index:
            coefficient = st_mc.at[store] * weight
github PyPSA / PyPSA / pypsa / opf.py View on Github external
fixed_links_i = network.links.index[~ network.links.p_nom_extendable]

    p_max_pu = get_switchable_as_dense(network, 'Link', 'p_max_pu', snapshots)
    p_min_pu = get_switchable_as_dense(network, 'Link', 'p_min_pu', snapshots)

    fixed_lower = p_min_pu.loc[:,fixed_links_i].multiply(network.links.loc[fixed_links_i, 'p_nom'])
    fixed_upper = p_max_pu.loc[:,fixed_links_i].multiply(network.links.loc[fixed_links_i, 'p_nom'])

    network.model.link_p = Var(list(network.links.index), snapshots)

    p_upper = {(cb, sn) : LConstraint(LExpression([(1, network.model.link_p[cb, sn])],
                                                 -fixed_upper.at[sn, cb]),"<=")
               for cb in fixed_links_i for sn in snapshots}

    p_upper.update({(cb,sn) : LConstraint(LExpression([(1, network.model.link_p[cb, sn]),
                                                       (-p_max_pu.at[sn, cb], network.model.link_p_nom[cb])]),
                                          "<=")
                    for cb in extendable_links_i for sn in snapshots})

    l_constraint(network.model, "link_p_upper", p_upper,
                 list(network.links.index), snapshots)


    p_lower = {(cb, sn) : LConstraint(LExpression([(1, network.model.link_p[cb, sn])],
                                                  -fixed_lower.at[sn, cb]),">=")
               for cb in fixed_links_i for sn in snapshots}

    p_lower.update({(cb,sn) : LConstraint(LExpression([(1, network.model.link_p[cb, sn]),
                                                       (-p_min_pu.at[sn, cb], network.model.link_p_nom[cb])]),
                                          ">=")
                    for cb in extendable_links_i for sn in snapshots})
github PyPSA / PyPSA / pypsa / opf.py View on Github external
## Deal with minimum down time ##

    down_time_gens = fixed_committable_gens_i[network.generators.loc[fixed_committable_gens_i,"min_down_time"] > 0]

    for gen_i, gen in enumerate(down_time_gens):

        min_down_time = network.generators.loc[gen,"min_down_time"]
        initial_status = network.generators.loc[gen,"initial_status"]

        blocks = max(1,len(snapshots)-min_down_time+1)

        gen_down_time = {}

        for i in range(blocks):
            #sum of 1-status
            lhs = LExpression([(-1,network.model.generator_status[gen,snapshots[j]]) for j in range(i,i+min_down_time)],min_down_time)

            if i == 0:
                rhs = LExpression([(-min_down_time,network.model.generator_status[gen,snapshots[i]])],min_down_time*initial_status)
            else:
                rhs = LExpression([(-min_down_time,network.model.generator_status[gen,snapshots[i]]),(min_down_time,network.model.generator_status[gen,snapshots[i-1]])])

            gen_down_time[i] = LConstraint(lhs,">=",rhs)

        l_constraint(network.model, "gen_down_time_{}".format(gen_i), gen_down_time,
                     range(blocks))

    ## Deal with start up costs ##

    suc_gens = fixed_committable_gens_i[network.generators.loc[fixed_committable_gens_i,"start_up_cost"] > 0]

    network.model.generator_start_up_cost = Var(list(suc_gens),snapshots,