Hi
I would to custom a constraint in the pypsa code to to force the link export to distribute a total of 10 GWh and the link import to distribute a total of 9 GWh, in the simulation.
I would like to get an output not concentrate in time, it mean that out put should be variable between 0 and value for all snapshots
The code
import pypsa
from
pypsa.optimization.compat import get_var, define_constraints, linexpr
IES2 =
pypsa.Network()
start_date
= pd.Timestamp("2018-01-01 00:00:00")
end_date =
pd.Timestamp("2018-12-31 23:00:00")
num_snapshots
= (end_date - start_date).days * 24 + 1
IES2.set_snapshots(pd.date_range(start_date,
end_date, freq='H'))
IES2.add("Bus","power_generation2")
IES2.add("Load",
"IE_Consumptio”, bus = "power_generation2",p_set = Irish_electrical_demand_2018)
IES2.add("Generator",
"Onshore_generators", bus = "power_generation2",
marginal_cost = Marginal_cost_onshore_generators,
capital_cost = Capital_cost_onshore_generators, p_nom = Max_Irish_wind_generation_2018,
p_max_pu = Electrical_demand_2018['p_max_pu_onshore'],
p_min_pu =
Electrical_demand_2018['p_max_pu_onshore'])
# Define bus of electricity generation (All gas turbines, gas fuel)
IES2.add("Bus",
"All_gas_turbines_gas_fuel")
# Define store of annual electricity generation (All gas turbines, gas fuel)
IES2.add("Store",
"Gas_elec_store",
bus =
"All_gas_turbines_gas_fuel",
e_nom =
Total_dispatch_all_gas_generator_2018_SEAI,
e_initial = Total_dispatch_all_gas_generator_2018_SEAI)
# Define all gas turbines generators, gas fuel)
IES2.add("Link",
"OCGT_and_CCGT",
bus0 =
"All_gas_turbines_gas_fuel",
bus1 =
"power_generation2",
p_nom_extendable = True,
marginal_cost =
Marginal_cost_OCGT_and_CCGT,
capital_cost =
Capital_cost_OCGT_and_CCGT)
####################################
# Add bus hydro pump storage
IES2.add("Bus",
"pumped_storage_hydro")
# Add storage "Turlough Hill"
IES2.add("Store",
"TH ",
bus
= "pumped_storage_hydro",
capital_cost =
Capital_cost_pump_hydro_storage,
e_nom_extendable = True,
e_nom_max =
e_nom_max_TH,
e_cyclic = True)
# Add link charger
IES2.add("Link",
"charging",
bus0 =
"power_generation2",
bus1 =
"pumped_storage_hydro",
p_nom_extendable = True,
p_nom_max = p_nom_TH,
)
# Add link discharger
IES2.add("Link",
"discharging",
bus0 =
"pumped_storage_hydro",
bus1 =
"power_generation2",
p_nom_extendable = True,
p_nom_max = p_nom_TH)
# Add bus interconnector
IES2.add("Bus", "Inteconnection2")
# Add "Elect_interchange" store
IES2.add("Store",
"Elect_interchange2",
bus
= "Inteconnection2",
e_nom_extendable = True,
e_cyclic = True)
# Add "Export" link with max available output power
IES2.add("Link",
"Export2",
bus0 =
"power_generation2",
bus1 =
"Inteconnection2",
capital_cost =
Capital_cost_inerconnector,
p_nom_extendable = True,
p_nom_max =
p_nom_Interconnector,
p_min_pu = 0.1)
# Add "Import" link with max available output power
IES2.add("Link",
"Import2",
bus0 =
"Inteconnection2",
bus1 =
"power_generation2",
p_nom_extendable = True,
p_nom_max =
p_nom_Interconnector)
# Custom constraints:
# Custom contraint for import and export
def ratio_functionality(IES2,snapshots):
link_p_nom = get_var(IES2, "Link", "p_nom")
lhs = linexpr((1.0, link_p_nom["Export2"]),(-1.0, link_p_nom["Import2"]))
define_constraints(IES2, lhs, "=", 0.0, 'Link',
'ratio')
def fix_ratio(IES2, snapshots):
vars_link = get_var(IES2, "Link", "p_nom")
lhs = linexpr((1.0, vars_link.loc["charging"]), (-1.0,
vars_link.loc["discharging"]))
define_constraints(IES2, lhs, "=", 0.0, 'Link',
'fix_ratio')
def extra_functionality2(IES2, snapshots):
ratio_functionality(IES2, snapshots)
fix_ratio(IES2, snapshots)
IES2.optimize(extra_functionality=extra_functionality2)