Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 2, 2024
1 parent 1b569dd commit f494dd8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
31 changes: 19 additions & 12 deletions scripts/add_brownfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,32 +119,39 @@ def add_brownfield(n, n_p, year):
n.links.loc[new_pipes, "p_nom"] = 0.0
n.links.loc[new_pipes, "p_nom_min"] = 0.0


def disable_grid_expansion_if_LV_limit_hit(n):
if not "lv_limit" in n.global_constraints.index:
return

#calculate minimum LV
# calculate minimum LV
attr = "nom_min"
dc = n.links.index[n.links.carrier == "DC"]
tot = (n.lines["s_" + attr]*n.lines["length"]).sum() + (n.links.loc[dc,"p_" + attr]*n.links.loc[dc,"length"]).sum()
tot = (n.lines["s_" + attr] * n.lines["length"]).sum() + (
n.links.loc[dc, "p_" + attr] * n.links.loc[dc, "length"]
).sum()

diff = n.global_constraints.at["lv_limit","constant"]-tot
diff = n.global_constraints.at["lv_limit", "constant"] - tot

#allow small numerical differences
# allow small numerical differences
limit = 1

if diff < limit:
logger.info(f"LV is already reached (gap {diff}), disabling expansion and LV limit")
logger.info(
f"LV is already reached (gap {diff}), disabling expansion and LV limit"
)
expandable_acs = n.lines.index[n.lines.s_nom_extendable]
n.lines.loc[expandable_acs,"s_nom_extendable"] = False
n.lines.loc[expandable_acs,"s_nom"] = n.lines.loc[expandable_acs,"s_nom_min"]
n.lines.loc[expandable_acs, "s_nom_extendable"] = False
n.lines.loc[expandable_acs, "s_nom"] = n.lines.loc[expandable_acs, "s_nom_min"]

expandable_dcs = n.links.index[
n.links.p_nom_extendable & (n.links.carrier == "DC")
]
n.links.loc[expandable_dcs, "p_nom_extendable"] = False
n.links.loc[expandable_dcs, "p_nom"] = n.links.loc[expandable_dcs, "p_nom_min"]

expandable_dcs = n.links.index[n.links.p_nom_extendable & (n.links.carrier == "DC")]
n.links.loc[expandable_dcs,"p_nom_extendable"] = False
n.links.loc[expandable_dcs,"p_nom"] = n.links.loc[expandable_dcs,"p_nom_min"]
n.global_constraints.drop("lv_limit", inplace=True)

n.global_constraints.drop("lv_limit",
inplace=True)

if __name__ == "__main__":
if "snakemake" not in globals():
Expand Down
69 changes: 45 additions & 24 deletions scripts/prepare_sector_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def define_spatial(nodes, options):

# methanol

#beware: unlike other carriers, uses locations rather than locations+carriername
#this allows to avoid separation between nodes and locations
# beware: unlike other carriers, uses locations rather than locations+carriername
# this allows to avoid separation between nodes and locations

spatial.methanol = SimpleNamespace()

Expand Down Expand Up @@ -1595,10 +1595,16 @@ def add_land_transport(n, costs):

ice_efficiency = options["transport_internal_combustion_efficiency"]

p_set_land_transport_oil = ice_share / ice_efficiency * transport[nodes].rename(columns=lambda x: x + " land transport oil")
p_set_land_transport_oil = (
ice_share
/ ice_efficiency
* transport[nodes].rename(columns=lambda x: x + " land transport oil")
)

if not options["regional_oil_demand"]:
p_set_land_transport_oil = p_set_land_transport_oil.sum(axis=1).to_frame(name="EU land transport oil")
p_set_land_transport_oil = p_set_land_transport_oil.sum(axis=1).to_frame(
name="EU land transport oil"
)

n.madd(
"Bus",
Expand Down Expand Up @@ -2454,7 +2460,7 @@ def add_industry(n, costs):
efficiency=1.0,
)

if len(spatial.biomass.industry_cc)<=1 and len(spatial.co2.nodes)>1:
if len(spatial.biomass.industry_cc) <= 1 and len(spatial.co2.nodes) > 1:
link_names = nodes + " " + spatial.biomass.industry_cc
else:
link_names = spatial.biomass.industry_cc
Expand Down Expand Up @@ -2650,7 +2656,11 @@ def add_industry(n, costs):
options["shipping_oil_efficiency"] / options["shipping_methanol_efficiency"]
)

p_set_methanol = shipping_methanol_share * p_set.rename(lambda x : x + " shipping methanol") * efficiency
p_set_methanol = (
shipping_methanol_share
* p_set.rename(lambda x: x + " shipping methanol")
* efficiency
)

if not options["regional_methanol_demand"]:
p_set_methanol = p_set_methanol.sum()
Expand Down Expand Up @@ -2679,7 +2689,10 @@ def add_industry(n, costs):
bus2="co2 atmosphere",
carrier="shipping methanol",
p_nom_extendable=True,
efficiency2=1 / options["MWh_MeOH_per_tCO2"], # CO2 intensity methanol based on stoichiometric calculation with 22.7 GJ/t methanol (32 g/mol), CO2 (44 g/mol), 277.78 MWh/TJ = 0.218 t/MWh
efficiency2=1
/ options[
"MWh_MeOH_per_tCO2"
], # CO2 intensity methanol based on stoichiometric calculation with 22.7 GJ/t methanol (32 g/mol), CO2 (44 g/mol), 277.78 MWh/TJ = 0.218 t/MWh
)

if "oil" not in n.buses.carrier.unique():
Expand Down Expand Up @@ -2714,7 +2727,6 @@ def add_industry(n, costs):
)

if shipping_oil_share:

p_set_oil = shipping_oil_share * p_set.rename(lambda x: x + " shipping oil")

if not options["regional_oil_demand"]:
Expand Down Expand Up @@ -2793,7 +2805,13 @@ def add_industry(n, costs):
if demand_factor != 1:
logger.warning(f"Changing HVC demand by {demand_factor*100-100:+.2f}%.")

p_set_plastics = demand_factor * industrial_demand.loc[nodes, "naphtha"].rename(lambda x: x + " naphtha for industry") / nhours
p_set_plastics = (
demand_factor
* industrial_demand.loc[nodes, "naphtha"].rename(
lambda x: x + " naphtha for industry"
)
/ nhours
)

if not options["regional_oil_demand"]:
p_set_plastics = p_set_plastics.sum()
Expand All @@ -2816,7 +2834,10 @@ def add_industry(n, costs):

# some CO2 from naphtha are process emissions from steam cracker
# rest of CO2 released to atmosphere either in waste-to-energy or decay
process_co2_per_naphtha = industrial_demand.loc[nodes, "process emission from feedstock"].sum() / industrial_demand.loc[nodes, "naphtha"].sum()
process_co2_per_naphtha = (
industrial_demand.loc[nodes, "process emission from feedstock"].sum()
/ industrial_demand.loc[nodes, "naphtha"].sum()
)
emitted_co2_per_naphtha = costs.at["oil", "CO2 intensity"] - process_co2_per_naphtha

n.madd(
Expand All @@ -2840,11 +2861,11 @@ def add_industry(n, costs):
all_aviation = ["total international aviation", "total domestic aviation"]

p_set = (
demand_factor
* pop_weighted_energy_totals.loc[nodes, all_aviation].sum(axis=1)
* 1e6
/ nhours
).rename(lambda x: x + " kerosene for aviation")
demand_factor
* pop_weighted_energy_totals.loc[nodes, all_aviation].sum(axis=1)
* 1e6
/ nhours
).rename(lambda x: x + " kerosene for aviation")

if not options["regional_oil_demand"]:
p_set = p_set.sum()
Expand Down Expand Up @@ -3095,9 +3116,9 @@ def add_agriculture(n, costs):
f"Total agriculture machinery shares sum up to {total_share:.2%}, corresponding to increased or decreased demand assumptions."
)

machinery_nodal_energy = pop_weighted_energy_totals.loc[
nodes, "total agriculture machinery"
] * 1e6
machinery_nodal_energy = (
pop_weighted_energy_totals.loc[nodes, "total agriculture machinery"] * 1e6
)

if electric_share > 0:
efficiency_gain = (
Expand All @@ -3111,15 +3132,15 @@ def add_agriculture(n, costs):
suffix=" agriculture machinery electric",
bus=nodes,
carrier="agriculture machinery electric",
p_set=electric_share
/ efficiency_gain
* machinery_nodal_energy
/ nhours,
p_set=electric_share / efficiency_gain * machinery_nodal_energy / nhours,
)

if oil_share > 0:

p_set = oil_share * machinery_nodal_energy.rename(lambda x: x + " agriculture machinery oil") / nhours
p_set = (
oil_share
* machinery_nodal_energy.rename(lambda x: x + " agriculture machinery oil")
/ nhours
)

if not options["regional_oil_demand"]:
p_set = p_set.sum()
Expand Down
11 changes: 9 additions & 2 deletions scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,16 @@ def extra_functionality(n, snapshots):
add_retrofit_gas_boiler_constraint(n, snapshots)

if "additional_functionality" in snakemake.input.keys():
import importlib, os, sys
import importlib
import os
import sys

sys.path.append(os.path.dirname(snakemake.input.additional_functionality))
additional_functionality = importlib.import_module(os.path.splitext(os.path.basename(snakemake.input.additional_functionality))[0])
additional_functionality = importlib.import_module(
os.path.splitext(
os.path.basename(snakemake.input.additional_functionality)
)[0]
)

additional_functionality.additional_functionality(n, snapshots, snakemake)

Expand Down

0 comments on commit f494dd8

Please sign in to comment.