Skip to content

Commit

Permalink
Refactor existing changes to fit latest code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Dec 1, 2023
1 parent f8101b7 commit 1c30bb0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
14 changes: 11 additions & 3 deletions message_ix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import InitVar, dataclass, field
from functools import partial
from pathlib import Path
from typing import Mapping, MutableMapping, Optional, Tuple
from typing import MutableMapping, Optional, Tuple
from warnings import warn

import ixmp.model.gams
Expand Down Expand Up @@ -173,7 +173,7 @@ class GAMSModel(ixmp.model.gams.GAMSModel):

#: Mapping from model item (equation, parameter, set, or variable) names to
#: :class:`.Item` describing the item.
items: Mapping[str, Item]
items: MutableMapping[str, Item]

def __init__(self, name=None, **model_options):
# Update the default options with any user-provided options
Expand Down Expand Up @@ -405,6 +405,8 @@ def initialize(cls, scenario):
par("commodity_stock", "n c l y")
par("construction_time", "nl t yv")
par("demand", "n c l y h")
par("df_year", "y")
par("df_period", "y")
par("duration_period", "y")
par("duration_time", "h")
par("dynamic_land_lo", "n s y u")
Expand Down Expand Up @@ -453,6 +455,7 @@ def initialize(cls, scenario):
par("level_cost_activity_soft_up", "nl t ya h")
par("level_cost_new_capacity_soft_lo", "nl t yv")
par("level_cost_new_capacity_soft_up", "nl t yv")
par("levelized_cost", "n t y h")
par("min_utilization_factor", "nl t yv ya")
par("operation_factor", "nl t yv ya")
par("output", "nl t yv ya m nd c l h hd")
Expand Down Expand Up @@ -557,6 +560,11 @@ def initialize(cls, scenario):
"n type_emission type_tec y",
"Emission price (derived from marginals of EMISSION_BOUND constraint)",
)
var(
"PRICE_EMISSION_NEW",
"n type_emission type_tec y",
"TEMPORARY test for Emission price fix",
)
var(
"REL",
"r nr yr",
Expand Down Expand Up @@ -766,7 +774,7 @@ def initialize(cls, scenario):
)
equ(
"EMISSION_EQUIVALENCE",
"",
"n e type_tec y",
"Auxiliary equation to simplify the notation of emissions",
)
equ("EXTRACTION_BOUND_UP", "", "Upper bound on extraction (by grade)")
Expand Down
46 changes: 27 additions & 19 deletions message_ix/tests/test_feature_price_emission.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,45 @@ def add_many_tecs(scen, years, n=50):
"""add a range of dirty-to-clean technologies to the scenario"""
output_specs = ["node", "comm", "level", "year", "year"]

# tec: [emissions, var_cost, bound_activity_up]
# Add some hardcoded tecs for temporary testing
# tec: [emission_factor, var_cost, bound_activity_up]
# tecs = {
# "tec1": [10, 5, 1],
# "tec2": [-1, 10, 0.4],
# "tec3": [-5, 200, 0.3],
# "tec4": [-15, 1200, 0.2],
# "tec5": [-50, 6000, 0.1],
# }
# tecs = {
# "tec1": [10, 5, 1],
# "tec2": [-10, 10, 0.4],
# "tec3": [-12, 20, 0.3],
# "tec4": [-14, 30, 0.2],
# "tec5": [-16, 40, 0.1],
# }
for i in range(1, n + 1):
t = f"tec{i}"
tecs = {
"tec1": [10, 5, 1],
"tec2": [-10, 10, 0.4],
"tec3": [-12, 20, 0.3],
"tec4": [-14, 30, 0.2],
"tec5": [-16, 40, 0.1],
}
# This is the original and we want to convert back to it after testing:
# for i in range(1, n + 1):
# t = f"tec{i}"
# -----------------
for t in tecs:
scen.add_set("technology", t)
for y in years:
tec_specs = ["node", t, y, y, "mode"]
# variable costs grow quadratically over technologies
# to get rid of the curse of linearity
c = (10 * i / n) ** 2 * (1.045) ** (y - years[0])
e = 1 - i / n
# This is the original, convert back after testing:
# # variable costs grow quadratically over technologies
# # to get rid of the curse of linearity
# c = (10 * i / n) ** 2 * (1.045) ** (y - years[0])
# e = 1 - i / n
# scen.add_par("var_cost", tec_specs + ["year"], c, "USD/GWa")
# scen.add_par("emission_factor", tec_specs + ["co2"], e, "tCO2")
# -------------
scen.add_par("output", tec_specs + output_specs, 1, "GWa")
scen.add_par("var_cost", tec_specs + ["year"], c, "USD/GWa")
scen.add_par("emission_factor", tec_specs + ["co2"], e, "tCO2")

# scen.add_par("var_cost", tec_specs + ["year"], tecs[t][1], "USD/GWa")
# scen.add_par("emission_factor", tec_specs + ["co2"], tecs[t][0], "tCO2")
# scen.add_par("bound_activity_up", ["node", t, y, "mode", "year"], tecs[t][2], "GWa")
scen.add_par("var_cost", tec_specs + ["year"], tecs[t][1], "USD/GWa")
scen.add_par("emission_factor", tec_specs + ["co2"], tecs[t][0], "tCO2")
scen.add_par(
"bound_activity_up", ["node", t, y, "mode", "year"], tecs[t][2], "GWa"
)

scen.add_set("type_addon", "mitigation")
scen.add_set("map_tec_addon", ["tec1", "mitigation"])
Expand Down

0 comments on commit 1c30bb0

Please sign in to comment.