Skip to content

Commit a671698

Browse files
measrainseykhaeru
authored andcommitted
Add functions
1 parent d84f244 commit a671698

File tree

2 files changed

+572
-0
lines changed

2 files changed

+572
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
5+
def modify_rc_bounds(s_original, s_target, mod_years):
6+
# get ACT for biomass_rc in SAS
7+
act_bio = s_original.var(
8+
"ACT", filters={"technology": "biomass_rc", "node_loc": "R12_SAS"}
9+
)
10+
11+
# get ACT for loil_rc in SAS
12+
act_loil_sas = s_original.var(
13+
"ACT", filters={"technology": "loil_rc", "node_loc": "R12_SAS"}
14+
)
15+
16+
val_bound_loil_sas = (
17+
act_loil_sas.loc[act_loil_sas["year_act"] == 2055]
18+
.reset_index()
19+
._get_value(0, "lvl")
20+
.round(2)
21+
)
22+
23+
# get ACT for loil_rc in MEA
24+
act_loil_mea = s_original.var(
25+
"ACT", filters={"technology": "loil_rc", "node_loc": "R12_MEA"}
26+
)
27+
28+
val_bound_loil_mea = (
29+
act_loil_mea.loc[act_loil_mea["year_act"] == 2055]
30+
.reset_index()
31+
._get_value(0, "lvl")
32+
.round(2)
33+
)
34+
35+
# get bound_activity_up for biomass_rc in SAS
36+
orig_bound_bio = s_original.par(
37+
"bound_activity_up", filters={"technology": "biomass_rc", "node_loc": "R12_SAS"}
38+
)
39+
40+
# get bound_activity_up for loil_rc in SAS
41+
orig_bound_loil_sas = s_original.par(
42+
"bound_activity_up", filters={"technology": "loil_rc", "node_loc": "R12_SAS"}
43+
)
44+
45+
# get bound_activity_up for loil_rc in MEA
46+
orig_bound_loil_mea = s_original.par(
47+
"bound_activity_up", filters={"technology": "loil_rc", "node_loc": "R12_MEA"}
48+
)
49+
50+
# function to get first row of bound and create new rows for set_years
51+
# and replace value with specified value
52+
def get_new_bound(df, set_years, value):
53+
new_rows = []
54+
for year in set_years:
55+
new_row = df.iloc[0].copy()
56+
new_row["year_act"] = year
57+
new_row["value"] = value
58+
new_rows.append(new_row)
59+
df_new = pd.DataFrame(new_rows).reset_index(drop=True)
60+
return df_new
61+
62+
new_bound_bio = get_new_bound(orig_bound_bio, mod_years, 0)
63+
new_bound_loil_sas = get_new_bound(
64+
orig_bound_loil_sas, mod_years, val_bound_loil_sas
65+
)
66+
new_bound_loil_mea = get_new_bound(
67+
orig_bound_loil_mea, mod_years, val_bound_loil_mea
68+
)
69+
70+
# check out and add each new bound_activity_up
71+
s_target.check_out()
72+
s_target.add_par("bound_activity_up", new_bound_bio)
73+
s_target.add_par("bound_activity_up", new_bound_loil_sas)
74+
s_target.add_par("bound_activity_up", new_bound_loil_mea)
75+
76+
# commit the changes
77+
s_target.commit("New bounds added to biomass_rc and loil_rc")
78+
79+
80+
def modify_tax_emission(s_original, s_target, scalar_val):
81+
# get PRICE_EMISSION variable
82+
price_emission = s_original.var(
83+
"PRICE_EMISSION", filters={"type_emission": "TCE_non-CO2"}
84+
)
85+
86+
# get tax_emission parameter
87+
tax_emission_old = s_original.par(
88+
"tax_emission", filters={"type_emission": "TCE_non-CO2"}
89+
)
90+
91+
# for PRICE_EMISSION after 2080, multiply by mult_price
92+
price_emission_mod = (
93+
price_emission.copy()
94+
.assign(lvl=lambda x: x.lvl * scalar_val)[["year", "lvl"]]
95+
.rename(columns={"year": "type_year"})
96+
)
97+
98+
# merge with tax_emission
99+
tax_emission_new = (
100+
tax_emission_old.copy()
101+
.assign(type_year=lambda x: x.type_year.astype("int64"))
102+
.merge(price_emission_mod, on="type_year")
103+
.assign(value=lambda x: np.where(x.type_year >= 2080, x.lvl, x.value))
104+
.assign(type_emission="TCE")
105+
.drop(columns="lvl")
106+
)
107+
108+
# check out and add new tax_emission
109+
s_target.check_out()
110+
s_target.remove_par("tax_emission", tax_emission_old)
111+
s_target.add_par("tax_emission", tax_emission_new)
112+
s_target.commit("New tax_emission added")
113+
114+
115+
def remove_bof_steel_lower(s, rem_years):
116+
remove_growth_activity_lo = s.par(
117+
"growth_activity_lo",
118+
filters={"technology": ["bof_steel"], "year_act": rem_years},
119+
)
120+
remove_initial_activity_lo = s.par(
121+
"initial_activity_lo",
122+
filters={"technology": ["bof_steel"], "year_act": rem_years},
123+
)
124+
s.check_out()
125+
s.remove_par("growth_activity_lo", remove_growth_activity_lo)
126+
s.remove_par("initial_activity_lo", remove_initial_activity_lo)
127+
s.commit("bof_steel bounds removed")
128+
129+
130+
def modify_steel_growth(s, techs, rem_years, growth_val):
131+
# get old values
132+
old_growth_activity_up = s.par(
133+
"growth_activity_up",
134+
filters={"technology": techs, "year_act": rem_years},
135+
)
136+
137+
# modify values
138+
new_growth_activity_up = old_growth_activity_up.copy().assign(value=growth_val)
139+
140+
# check out; remove old bounds and add new bounds
141+
s.check_out()
142+
s.remove_par("growth_activity_up", old_growth_activity_up)
143+
s.add_par("growth_activity_up", new_growth_activity_up)
144+
s.commit("Modified bounds for steel alternatives")
145+
146+
147+
def modify_steel_initial(s, techs, rem_years, initial_val):
148+
# get old values
149+
old_initial_activity_up = s.par(
150+
"initial_activity_up",
151+
filters={"technology": techs, "year_act": rem_years},
152+
)
153+
154+
# modify values
155+
new_initial_activity_up = old_initial_activity_up.copy().assign(value=initial_val)
156+
157+
# check out; remove old bounds and add new bounds
158+
s.check_out()
159+
s.remove_par("initial_activity_up", old_initial_activity_up)
160+
s.add_par("initial_activity_up", new_initial_activity_up)
161+
s.commit("Modified bounds for steel alternatives")
162+
163+
164+
def add_steel_sector_nze(s, steel_target_array):
165+
co2_ind = s.par(
166+
"relation_activity",
167+
filters={
168+
"relation": "CO2_ind",
169+
"technology": ["DUMMY_coal_supply", "DUMMY_gas_supply"],
170+
},
171+
)
172+
173+
co2_emi = s.par(
174+
"output",
175+
filters={
176+
"commodity": "fic_co2",
177+
"technology": ["dri_gas_ccs_steel", "bf_ccs_steel"],
178+
},
179+
)
180+
181+
co2_emi["relation"] = "CO2_Emission"
182+
co2_emi.rename(columns={"node_dest": "node_rel"}, inplace=True)
183+
co2_emi["year_rel"] = co2_emi["year_act"]
184+
co2_emi.drop(
185+
["year_vtg", "commodity", "level", "time", "time_dest"], axis=1, inplace=True
186+
)
187+
188+
co2_emi["value"] *= -1
189+
190+
rel_new = pd.concat([co2_ind, co2_emi], ignore_index=True)
191+
192+
rel_new = rel_new[rel_new["year_rel"] >= 2070]
193+
194+
rel_new["node_rel"] = "R12_GLB"
195+
rel_new["relation"] = "steel_sector_target"
196+
197+
rel_new = rel_new.drop_duplicates()
198+
199+
s.check_out()
200+
s.add_set("relation", "steel_sector_target")
201+
202+
relation_upper_df = pd.DataFrame(
203+
{
204+
"relation": "steel_sector_target",
205+
"node_rel": "R12_GLB",
206+
"year_rel": [2070, 2080, 2090, 2100],
207+
"value": steel_target_array, # slack values given from Gamze, added manually
208+
"unit": "???",
209+
}
210+
)
211+
212+
relation_lower_df = pd.DataFrame(
213+
{
214+
"relation": "steel_sector_target",
215+
"node_rel": "R12_GLB",
216+
"year_rel": [2070, 2080, 2090, 2100],
217+
"value": 0,
218+
"unit": "???",
219+
}
220+
)
221+
222+
s.add_par("relation_activity", rel_new)
223+
s.add_par("relation_upper", relation_upper_df)
224+
s.add_par("relation_lower", relation_lower_df)
225+
226+
s.commit("Steel sector target added.")
227+
228+
229+
def add_balance_equality(scen):
230+
with scen.transact(""):
231+
scen.add_set("balance_equality", ["bic_co2", "secondary"])
232+
scen.add_set("balance_equality", ["fic_co2", "secondary"])
233+
scen.add_set("balance_equality", ["dac_co2", "secondary"])
234+
scen.add_set("balance_equality", ["methanol", "final_material"])
235+
scen.add_set("balance_equality", ["HVC", "demand"])
236+
scen.add_set("balance_equality", ["HVC", "export"])
237+
scen.add_set("balance_equality", ["HVC", "import"])
238+
scen.add_set("balance_equality", ["ethylene", "final_material"])
239+
scen.add_set("balance_equality", ["propylene", "final_material"])
240+
scen.add_set("balance_equality", ["BTX", "final_material"])

0 commit comments

Comments
 (0)