Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cycle_index = []
cycle_constraints = {}
for subnetwork in network.sub_networks.obj:
branches = subnetwork.branches()
attribute = "r_pu_eff" if network.sub_networks.at[subnetwork.name,"carrier"] == "DC" else "x_pu_eff"
sub_network_cycle_index, sub_network_cycle_constraints = define_sub_network_cycle_constraints( subnetwork,
snapshots,
network.model.passive_branch_p, attribute)
cycle_index.extend( sub_network_cycle_index)
cycle_constraints.update( sub_network_cycle_constraints)
l_constraint(network.model, "cycle_constraints", cycle_constraints,
cycle_index, snapshots)
network.model.cycles = Var(cycle_index, snapshots, domain=Reals, bounds=(None,None))
flows = {}
for subnetwork in network.sub_networks.obj:
branches = subnetwork.branches()
buses = subnetwork.buses()
for i,branch in enumerate(branches.index):
bt = branch[0]
bn = branch[1]
cycle_is = subnetwork.C[i,:].nonzero()[1]
tree_is = subnetwork.T[i,:].nonzero()[1]
s_max_pu = pd.concat({c : get_switchable_as_dense(network, c, 's_max_pu', snapshots)
for c in network.passive_branch_components}, axis=1, sort=False)
flow_upper = {(b[0],b[1],sn) : [[(1,network.model.passive_branch_p[b[0],b[1],sn])],
"<=", s_max_pu.at[sn,b]*fixed_branches.at[b,"s_nom"]]
for b in fixed_branches.index
for sn in snapshots}
flow_upper.update({(b[0],b[1],sn) : [[(1,network.model.passive_branch_p[b[0],b[1],sn]),
(-s_max_pu.at[sn,b],network.model.passive_branch_s_nom[b[0],b[1]])],"<=",0]
for b in extendable_branches.index
for sn in snapshots})
l_constraint(network.model, "flow_upper", flow_upper,
list(passive_branches.index), snapshots)
flow_lower = {(b[0],b[1],sn) : [[(1,network.model.passive_branch_p[b[0],b[1],sn])],
">=", -s_max_pu.at[sn,b]*fixed_branches.at[b,"s_nom"]]
for b in fixed_branches.index
for sn in snapshots}
flow_lower.update({(b[0],b[1],sn): [[(1,network.model.passive_branch_p[b[0],b[1],sn]),
(s_max_pu.at[sn,b],network.model.passive_branch_s_nom[b[0],b[1]])],">=",0]
for b in extendable_branches.index
for sn in snapshots})
l_constraint(network.model, "flow_lower", flow_lower,
list(passive_branches.index), snapshots)
cycle_index = []
cycle_constraints = {}
for subnetwork in network.sub_networks.obj:
branches = subnetwork.branches()
attribute = "r_pu_eff" if network.sub_networks.at[subnetwork.name,"carrier"] == "DC" else "x_pu_eff"
sub_network_cycle_index, sub_network_cycle_constraints = define_sub_network_cycle_constraints( subnetwork,
snapshots,
network.model.passive_branch_p, attribute)
cycle_index.extend( sub_network_cycle_index)
cycle_constraints.update( sub_network_cycle_constraints)
l_constraint(network.model, "cycle_constraints", cycle_constraints,
cycle_index, snapshots)
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})
l_constraint(network.model, "link_p_lower", p_lower,
list(network.links.index), snapshots)
for sn in snapshots})
l_constraint(network.model, "flow_upper", flow_upper,
list(passive_branches.index), snapshots)
flow_lower = {(b[0],b[1],sn) : [[(1,network.model.passive_branch_p[b[0],b[1],sn])],
">=", -s_max_pu.at[sn,b]*fixed_branches.at[b,"s_nom"]]
for b in fixed_branches.index
for sn in snapshots}
flow_lower.update({(b[0],b[1],sn): [[(1,network.model.passive_branch_p[b[0],b[1],sn]),
(s_max_pu.at[sn,b],network.model.passive_branch_s_nom[b[0],b[1]])],">=",0]
for b in extendable_branches.index
for sn in snapshots})
l_constraint(network.model, "flow_lower", flow_lower,
list(passive_branches.index), snapshots)
blocks = max(1,len(snapshots)-min_up_time+1)
gen_up_time = {}
for i in range(blocks):
lhs = LExpression([(1,network.model.generator_status[gen,snapshots[j]]) for j in range(i,i+min_up_time)])
if i == 0:
rhs = LExpression([(min_up_time,network.model.generator_status[gen,snapshots[i]])],-min_up_time*initial_status)
else:
rhs = LExpression([(min_up_time,network.model.generator_status[gen,snapshots[i]]),(-min_up_time,network.model.generator_status[gen,snapshots[i-1]])])
gen_up_time[i] = LConstraint(lhs,">=",rhs)
l_constraint(network.model, "gen_up_time_{}".format(gen_i), gen_up_time,
range(blocks))
## 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 = {}
#kill small PTDF values
sub_network.PTDF[abs(sub_network.PTDF) < ptdf_tolerance] = 0
for i,branch in enumerate(branches_i):
bt = branch[0]
bn = branch[1]
for sn in snapshots:
lhs = sum(sub_network.PTDF[i,j]*network._p_balance[bus,sn]
for j,bus in enumerate(sub_network.buses_o)
if sub_network.PTDF[i,j] != 0)
rhs = LExpression([(1,network.model.passive_branch_p[bt,bn,sn])])
flows[bt,bn,sn] = LConstraint(lhs,"==",rhs)
l_constraint(network.model, "passive_branch_p_def", flows,
list(passive_branches.index), snapshots)
def define_passive_branch_flows_with_angles(network,snapshots):
network.model.voltage_angles = Var(list(network.buses.index), snapshots)
slack = {(sub,sn) :
[[(1,network.model.voltage_angles[network.sub_networks.slack_bus[sub],sn])], "==", 0.]
for sub in network.sub_networks.index for sn in snapshots}
l_constraint(network.model,"slack_angle",slack,list(network.sub_networks.index),snapshots)
passive_branches = network.passive_branches()
network.model.passive_branch_p = Var(list(passive_branches.index), snapshots)
flows = {}
for branch in passive_branches.index:
bus0 = passive_branches.at[branch,"bus0"]
bus1 = passive_branches.at[branch,"bus1"]
bt = branch[0]
bn = branch[1]
sub = passive_branches.at[branch,"sub_network"]
attribute = "r_pu_eff" if network.sub_networks.at[sub,"carrier"] == "DC" else "x_pu_eff"
y = 1/ passive_branches.at[ branch, attribute]
for sn in snapshots:
list(extendable_gens_i), snapshots)
## Define committable generator statuses ##
network.model.generator_status = Var(list(fixed_committable_gens_i), snapshots,
within=Binary)
var_lower = p_min_pu.loc[:,fixed_committable_gens_i].multiply(network.generators.loc[fixed_committable_gens_i, 'p_nom'])
var_upper = p_max_pu.loc[:,fixed_committable_gens_i].multiply(network.generators.loc[fixed_committable_gens_i, 'p_nom'])
committable_gen_p_lower = {(gen,sn) : LConstraint(LExpression([(var_lower[gen][sn],network.model.generator_status[gen,sn]),(-1.,network.model.generator_p[gen,sn])]),"<=") for gen in fixed_committable_gens_i for sn in snapshots}
l_constraint(network.model, "committable_gen_p_lower", committable_gen_p_lower,
list(fixed_committable_gens_i), snapshots)
committable_gen_p_upper = {(gen,sn) : LConstraint(LExpression([(var_upper[gen][sn],network.model.generator_status[gen,sn]),(-1.,network.model.generator_p[gen,sn])]),">=") for gen in fixed_committable_gens_i for sn in snapshots}
l_constraint(network.model, "committable_gen_p_upper", committable_gen_p_upper,
list(fixed_committable_gens_i), snapshots)
## Deal with minimum up time ##
up_time_gens = fixed_committable_gens_i[network.generators.loc[fixed_committable_gens_i,"min_up_time"] > 0]
for gen_i, gen in enumerate(up_time_gens):
min_up_time = network.generators.loc[gen,"min_up_time"]