Skip to content

Commit

Permalink
TEMPORARY Add notebook to troubleshoot test_price_duality
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed May 16, 2024
1 parent 28ad9b4 commit 7b733f6
Showing 1 changed file with 327 additions and 0 deletions.
327 changes: 327 additions & 0 deletions message_ix/tests/feature_price_emission.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ixmp import Platform\n",
"\n",
"from message_ix import Scenario\n",
"\n",
"MODEL = \"test_emissions_price\"\n",
"\n",
"mp = Platform(\"test_feature_price_emission\")\n",
"mp.add_unit(\"MtCO2\")\n",
"mp.add_unit(\"tCO2/kWa\")\n",
"mp.add_unit(\"USD/kW\")\n",
"\n",
"scen = Scenario(\n",
" mp,\n",
" MODEL,\n",
" scenario=\"many_tecs\",\n",
" version=\"new\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from message_ix.tests.test_feature_price_emission import model_setup\n",
"\n",
"# 450 is too much; this bound will not affect the first model year\n",
"# (EMISS is not reduced from without cumulative bound to with it)\n",
"# In a model year without binding bound, no price_emission is produced ->\n",
"# there is one value missing for price_emission in period-specific bound\n",
"cumulative_bound = 1\n",
"# cumulative_bound = 500\n",
"# cumulative_bound = 550\n",
"years = [2020, 2030, 2040, 2050]\n",
"# years = [2020, 2025, 2030, 2040, 2050]\n",
"# years = [2020, 2030, 2040, 2045, 2050]\n",
"\n",
"filters = {\"node\": \"World\"}\n",
"\n",
"model_setup(scen=scen, years=years, simple_tecs=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from message_ix.tests.test_feature_price_emission import solve_args\n",
"\n",
"scen.commit(\"initialize test scenario\")\n",
"scen.solve(quiet=True, **solve_args)\n",
"scen.var(\"EMISS\", filters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenario_cumulative_bound = scen.clone(\n",
" MODEL,\n",
" \"cumulative_emission_bound\",\n",
" \"introducing a cumulative emissions bound\",\n",
" keep_solution=False,\n",
")\n",
"scenario_cumulative_bound.check_out()\n",
"\n",
"scenario_cumulative_bound.add_cat(\"year\", \"cumulative\", years)\n",
"scenario_cumulative_bound.add_par(\n",
" \"bound_emission\",\n",
" [\"World\", \"GHG\", \"all\", \"cumulative\"],\n",
" cumulative_bound,\n",
" \"MtCO2\",\n",
")\n",
"scenario_cumulative_bound.commit(\"initialize test scenario\")\n",
"scenario_cumulative_bound.solve(quiet=True, **solve_args)\n",
"scenario_cumulative_bound.var(\"EMISS\", filters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"emiss = scenario_cumulative_bound.var(\"EMISS\", filters).set_index(\"year\").lvl\n",
"price_emission = (\n",
" scenario_cumulative_bound.var(\"PRICE_EMISSION\", filters).set_index(\"year\").lvl\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# --------------------------------------------------------\n",
"# Run scenario with annual-emission bound based on `EMISS`\n",
"# from cumulative constraint scenario.\n",
"# --------------------------------------------------------\n",
"\n",
"scenario_period_bound = scen.clone(\n",
" MODEL,\n",
" \"period_bound_many_tecs\",\n",
" \"introducing a period-specific emission_bound\",\n",
" keep_solution=False,\n",
")\n",
"scenario_period_bound.check_out()\n",
"for year in years:\n",
" scenario_period_bound.add_cat(\"year\", year, year)\n",
"\n",
"# use emissions from cumulative-constraint scenario as period-emission bounds\n",
"emiss_period_bound = (\n",
" scenario_cumulative_bound.var(\"EMISS\", {\"node\": \"World\"})\n",
" .rename(columns={\"year\": \"type_year\", \"lvl\": \"value\"})\n",
" .drop(\"emission\", axis=1)\n",
")\n",
"emiss_period_bound[\"type_emission\"] = \"GHG\"\n",
"emiss_period_bound[\"unit\"] = \"MtCO2\"\n",
"scenario_period_bound.add_par(\"bound_emission\", emiss_period_bound)\n",
"scenario_period_bound.commit(\"initialize test scenario for periodic emission bound\")\n",
"scenario_period_bound.solve(quiet=True, **solve_args)\n",
"scenario_period_bound.var(\"EMISS\", filters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy.testing as npt\n",
"\n",
"# check -emissions are close between cumulative and yearly-bound scenarios\n",
"emiss_period_bound = scenario_period_bound.var(\"EMISS\", filters).set_index(\"year\").lvl\n",
"npt.assert_allclose(emiss, emiss_period_bound)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenario_cumulative_bound.par(\"emission_factor\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(price_emission)\n",
"scenario_period_bound.var(\"PRICE_EMISSION\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check \"PRICE_EMISSION\" is close between cumulative- and yearly-bound scenarios\n",
"price_emission_period_bound = (\n",
" scenario_period_bound.var(\"PRICE_EMISSION\", filters).set_index(\"year\").lvl\n",
")\n",
"npt.assert_allclose(price_emission, price_emission_period_bound)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenario_cumulative_bound.par(\"bound_emission\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scen_tax = Scenario(\n",
" mp,\n",
" MODEL,\n",
" scenario=\"tax_many_tecs\",\n",
" version=\"new\",\n",
")\n",
"model_setup(scen_tax, years, simple_tecs=False)\n",
"for year in years:\n",
" scen_tax.add_cat(\"year\", year, year)\n",
"# use emission prices from cumulative-constraint scenario as taxes\n",
"taxes = scenario_cumulative_bound.var(\"PRICE_EMISSION\").rename(\n",
" columns={\"year\": \"type_year\", \"lvl\": \"value\"}\n",
")\n",
"taxes[\"unit\"] = \"USD/tCO2\"\n",
"taxes[\"node\"] = \"node\"\n",
"scen_tax.add_par(\"tax_emission\", taxes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scen_tax.commit(\"initialize test scenario for taxes\")\n",
"scen_tax.solve(quiet=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(scenario_cumulative_bound.var(\"PRICE_EMISSION\"))\n",
"print(scen_tax.var(\"PRICE_EMISSION\"))\n",
"print(scen_tax.par(\"tax_emission\"))\n",
"print(scenario_cumulative_bound.var(\"EMISS\"))\n",
"print(scen_tax.var(\"EMISS\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(scenario_cumulative_bound.var(\"ACT\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(scen_tax.var(\"ACT\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenario_cumulative_bound.par(\"demand\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenario_cumulative_bound.equ(\"EMISSION_EQUIVALENCE\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"price_emission_tax = scen_tax.var(\"PRICE_EMISSION\").set_index(\"year\").lvl\n",
"npt.assert_allclose(price_emission, price_emission_tax)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check emissions are close between cumulative and tax scenarios\n",
"emiss_tax = scen_tax.var(\"EMISS\", filters).set_index(\"year\").lvl\n",
"npt.assert_allclose(emiss, emiss_tax, rtol=0.05)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mp.close_db()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "mix312",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 7b733f6

Please sign in to comment.