Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
glcs = n.global_constraints.query('type == '
'"transmission_volume_expansion_limit"')
substr = lambda s: re.sub('[\[\]\(\)]', '', s)
for name, glc in glcs.iterrows():
car = [substr(c.strip()) for c in glc.carrier_attribute.split(',')]
lhs = ''
for c, attr in (('Line', 's_nom'), ('Link', 'p_nom')):
ext_i = n.df(c).query(f'carrier in @car and {attr}_extendable').index
if ext_i.empty: continue
v = linexpr((n.df(c).length[ext_i], get_var(n, c, attr)[ext_i]),
as_pandas=False)
lhs += '\n' + join_exprs(v)
if lhs == '': continue
sense = glc.sense
rhs = glc.constant
con = write_constraint(n, lhs, sense, rhs, axes=pd.Index([name]))
set_conref(n, con, 'GlobalConstraint', 'mu', name)
# expansion cost limits
glcs = n.global_constraints.query('type == '
'"transmission_expansion_cost_limit"')
for name, glc in glcs.iterrows():
car = [substr(c.strip()) for c in glc.carrier_attribute.split(',')]
lhs = ''
for c, attr in (('Line', 's_nom'), ('Link', 'p_nom')):
ext_i = n.df(c).query(f'carrier in @car and {attr}_extendable').index
if ext_i.empty: continue
v = linexpr((n.df(c).capital_cost[ext_i], get_var(n, c, attr)[ext_i]),
as_pandas=False)
lhs += '\n' + join_exprs(v)
if lhs == '': continue
sense = glc.sense
'state_of_charge').loc[sns[-1], sus_i])
vals = linexpr(coeff_val, as_pandas=False)
lhs = lhs + '\n' + join_exprs(vals)
rhs -= sus.carrier.map(emissions) @ sus.state_of_charge_initial
# stores
n.stores['carrier'] = n.stores.bus.map(n.buses.carrier)
stores = n.stores.query('carrier in @emissions.index and not e_cyclic')
if not stores.empty:
coeff_val = (-stores.carrier.map(emissions), get_var(n, 'Store', 'e')
.loc[sns[-1], stores.index])
vals = linexpr(coeff_val, as_pandas=False)
lhs = lhs + '\n' + join_exprs(vals)
rhs -= stores.carrier.map(emissions) @ stores.e_initial
con = write_constraint(n, lhs, glc.sense, rhs, axes=pd.Index([name]))
set_conref(n, con, 'GlobalConstraint', 'mu', name)
# for the next two to we need a line carrier
if len(n.global_constraints) > len(glcs):
n.lines['carrier'] = n.lines.bus0.map(n.buses.carrier)
# expansion limits
glcs = n.global_constraints.query('type == '
'"transmission_volume_expansion_limit"')
substr = lambda s: re.sub('[\[\]\(\)]', '', s)
for name, glc in glcs.iterrows():
car = [substr(c.strip()) for c in glc.carrier_attribute.split(',')]
lhs = ''
for c, attr in (('Line', 's_nom'), ('Link', 'p_nom')):
ext_i = n.df(c).query(f'carrier in @car and {attr}_extendable').index
if ext_i.empty: continue
v = linexpr((n.df(c).length[ext_i], get_var(n, c, attr)[ext_i]),
ds = ds[lambda ds: ds!=0.].dropna()
vals = linexpr((ds, branch_vars[ds.index]), as_pandas=False)
return vals.sum(1)
constraints = []
for sub in n.sub_networks.obj:
branches = sub.branches()
C = pd.DataFrame(sub.C.todense(), index=branches.index)
if C.empty:
continue
carrier = n.sub_networks.carrier[sub.name]
weightings = branches.x_pu_eff if carrier == 'AC' else branches.r_pu_eff
C_weighted = 1e5 * C.mul(weightings, axis=0)
cycle_sum = C_weighted.apply(cycle_flow)
cycle_sum.index = sns
con = write_constraint(n, cycle_sum, '=', 0)
constraints.append(con)
constraints = pd.concat(constraints, axis=1, ignore_index=True)
set_conref(n, constraints, 'SubNetwork', 'mu_kirchhoff_voltage_law')
n : pypsa.Network
c : str
name of the network component
attr : str
name of the attribute, e.g. 'p'
pnl : bool, default True
Whether variable which should be fixed is time-dependent
"""
if pnl:
if attr + '_set' not in n.pnl(c): return
fix = n.pnl(c)[attr + '_set'].unstack().dropna()
if fix.empty: return
lhs = linexpr((1, get_var(n, c, attr).unstack()[fix.index]), as_pandas=False)
constraints = write_constraint(n, lhs, '=', fix).unstack().T
else:
if attr + '_set' not in n.df(c): return
fix = n.df(c)[attr + '_set'].dropna()
if fix.empty: return
lhs = linexpr((1, get_var(n, c, attr)[fix.index]), as_pandas=False)
constraints = write_constraint(n, lhs, '=', fix)
set_conref(n, constraints, c, f'mu_{attr}_set')