Skip to content

Commit

Permalink
Add bio CO2 fix for biogas activities
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Dec 10, 2023
1 parent db89d0e commit db1eac0
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 78 deletions.
194 changes: 116 additions & 78 deletions dev/Untitled.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"id": "f8e0a4cb-a226-4ab7-a093-015ea48749d0",
"metadata": {
"tags": []
Expand Down Expand Up @@ -140,7 +140,7 @@
" #{\"model\": \"remind\", \"pathway\": \"SSP2-Base\", \"year\": 2100},\n",
" #{\"model\": \"remind\", \"pathway\": \"SSP2-PkBudg1150\", \"year\": 2030},\n",
" #{\"model\": \"remind\", \"pathway\": \"SSP2-PkBudg1150\", \"year\": 2040},\n",
" {\"model\": \"remind\", \"pathway\": \"SSP2-PkBudg500\", \"year\": 2100},\n",
" {\"model\": \"remind\", \"pathway\": \"SSP2-PkBudg500\", \"year\": 2020},\n",
" #{\"model\": \"image\", \"pathway\": \"SSP2-RCP19\", \"year\": 2100},\n",
"]\n",
"\n",
Expand All @@ -159,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"id": "c7996c75-e3e7-42ba-9ecb-c016b6462109",
"metadata": {
"tags": []
Expand All @@ -169,20 +169,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"`update_all()` will skip the following steps:\n",
"update_two_wheelers(), update_cars(), and update_buses()\n",
"If you want to update these steps, please run them separately afterwards.\n",
"Extracted 1 worksheets in 4.61 seconds\n",
"Migrating to 3.8 first\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
"Applying strategy: migrate_datasets\n",
"Applying strategy: migrate_exchanges\n",
"Vehicle fleet data is not available beyond 2050. Hence, 2050 is used as fleet year.\n",
"Anomalies found: check the change report.\n",
"Done!\n",
"\n",
"////////////////////////////// FUELS ///////////////////////////////\n",
"Done!\n",
"Done!\n",
"\n"
Expand All @@ -192,12 +180,12 @@
"source": [
"#ndb.update_external_scenario()\n",
"#ndb.update_dac()\n",
"ndb.update_all()\n",
"#ndb.update_all()\n",
"#ndb.update_biomass()\n",
"#ndb.update_electricity()\n",
"#ndb.update_cement()\n",
"#ndb.update_steel()\n",
"#ndb.update_fuels()\n",
"ndb.update_fuels()\n",
"#ndb.update_heat()\n",
"#ndb.update_heat()\n",
"#ndb.update_buses()\n",
Expand Down Expand Up @@ -266,7 +254,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "a85ca090-8d49-42c2-8b21-ffc69fabc12d",
"metadata": {
"tags": []
Expand All @@ -276,15 +264,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Running all checks...\n",
"Done.\n",
"Running all checks...\n",
"Done.\n",
"Running all checks...\n",
"Done.\n",
"Building superstructure database...\n",
"Dropped 0 duplicate(s).\n",
"Scenario difference file exported to /Users/romain/GitHub/premise/dev/export/scenario diff files!\n"
"Write new database(s) to Brightway.\n",
"Running all checks...\n"
]
},
{
Expand All @@ -293,29 +274,24 @@
"text": [
"Writing activities to SQLite3 database:\n",
"0% [##############################] 100% | ETA: 00:00:00\n",
"Total time elapsed: 00:01:26\n"
"Total time elapsed: 00:00:25\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Title: Writing activities to SQLite3 database:\n",
" Started: 11/29/2023 22:35:14\n",
" Finished: 11/29/2023 22:36:40\n",
" Total time elapsed: 00:01:26\n",
" CPU %: 83.90\n",
" Memory %: 20.21\n",
"Created database: test_sp7\n",
"Generate scenario report.\n",
"Report saved under /Users/romain/GitHub/premise/dev/export/scenario_report.\n",
"Generate change report.\n",
"Report saved under /Users/romain/GitHub/premise/dev.\n"
" Started: 12/10/2023 18:53:59\n",
" Finished: 12/10/2023 18:54:24\n",
" Total time elapsed: 00:00:25\n",
" CPU %: 94.60\n",
" Memory %: 15.01\n"
]
}
],
"source": [
"ndb.write_superstructure_db_to_brightway(\"test_sp7\")"
"ndb.write_db_to_brightway(\"test_biomethane\")"
]
},
{
Expand All @@ -328,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"id": "f0382864-e70c-42ab-886f-4e7b0de00f66",
"metadata": {
"tags": []
Expand All @@ -338,41 +314,103 @@
"name": "stdout",
"output_type": "stream",
"text": [
"EUR market for petrol, low-sulfur\n",
"market for petrol, low-sulfur 1.0 EUR\n",
"Benzene 4.9e-06 None\n",
"Benzene, ethyl- 1.03e-05 None\n",
"Hexane 6.37e-06 None\n",
"Hydrocarbons, aliphatic, alkanes, unspecified 0.000268 None\n",
"Hydrocarbons, aliphatic, unsaturated 4.9e-05 None\n",
"Hydrocarbons, aromatic 4.9e-05 None\n",
"Methane, fossil 1.47e-07 None\n",
"Toluene 2.45e-05 None\n",
"Water 1.0335e-07 None\n",
"Xylene 5.39e-05 None\n",
"t-Butyl methyl ether 2.45e-05 None\n",
"Water 5.8565e-07 None\n",
"infrastructure construction, for regional distribution of oil product 2.78e-10 RER\n",
"market for fly ash and scrubber sludge -0.000168 Europe without Switzerland\n",
"market for heat, central or small-scale, other than natural gas 0.00058374 Europe without Switzerland\n",
"market for rainwater mineral oil storage -7.5e-05 Europe without Switzerland\n",
"market for tap water 0.000689 Europe without Switzerland\n",
"market for transport, freight train 0.044 Europe without Switzerland\n",
"market for transport, freight, inland waterways, barge tanker 0.0323 RER\n",
"market for transport, freight, light commercial vehicle 0.0018 Europe without Switzerland\n",
"market for transport, pipeline, onshore, petroleum 0.559 RER\n",
"market for wastewater, average -6.89e-07 Europe without Switzerland\n",
"market group for electricity, low voltage 0.0067 EUR\n",
"market group for municipal solid waste -3.1050277457211474e-06 Europe without Switzerland\n",
"market group for municipal solid waste -3.1649722542788527e-06 RER\n",
"transport, freight, lorry, unspecified, regional delivery 0.0567 EUR\n",
"petrol production, low-sulfur 0.022161419393931913 Europe without Switzerland\n",
"ethanol production, via fermentation, from poplar, energy allocation 4.775655846292105e-07 EUR\n",
"ethanol production, via fermentation, from poplar, with carbon capture and storage, energy allocation 0.7435165607160134 EUR\n",
"ethanol production, via fermentation, from switchgrass, energy allocation 0.21847891239700018 EUR\n",
"ethanol production, via fermentation, from sugarbeet, energy allocation 0.039014033935755456 EUR\n",
"gasoline production, from methanol, hydrogen from coal gasification, with CCS, energy allocation 1.3925492949685385e-07 EUR\n",
"gasoline production, from methanol, hydrogen from electrolysis, CO2 from DAC, energy allocation 0.355144436837515 EUR\n",
"RoW treatment of sewage sludge by anaerobic digestion\n",
"treatment of sewage sludge by anaerobic digestion 1.0 RoW\n",
"Carbon dioxide, in air 1.96 None\n",
"\n",
"RoW anaerobic digestion of manure\n",
"anaerobic digestion of manure 1.0 RoW\n",
"market for anaerobic digestion plant, agriculture, with methane recovery 2.8600029991482456e-07 GLO\n",
"digester sludge, Recycled Content cut-off -36.18257393164099 GLO\n",
"market for electricity, low voltage 0.000276190662034128 NZ\n",
"market group for electricity, low voltage 0.08513873997904295 RAS\n",
"market group for electricity, low voltage 0.008863026830358674 RLA\n",
"market group for electricity, low voltage 0.030428354940241344 RNA\n",
"market for electricity, low voltage 0.0015187481923203123 AU\n",
"market for electricity, low voltage 0.006210977963085262 RU\n",
"market group for electricity, low voltage 0.021091677506418875 Europe without Switzerland\n",
"market group for electricity, low voltage 0.0044722839264984765 RAF\n",
"market for heat, central or small-scale, other than natural gas 0.0470149534384393 Europe without Switzerland\n",
"market for heat, central or small-scale, other than natural gas 3.422985046561561 RoW\n",
"market for manure, liquid, cattle 20.451988955925966 GLO\n",
"market for manure, liquid, swine 12.27119337355558 GLO\n",
"market for manure, solid, cattle 4.462252135838392 GLO\n",
"Ammonia 0.0017503979810816943 None\n",
"Carbon dioxide, non-fossil 0.07089524938692314 None\n",
"Dinitrogen monoxide 0.0002490386623259582 None\n",
"Hydrogen sulfide 4.127583225650513e-05 None\n",
"Methane, non-fossil 0.01983423218575475 None\n",
"Carbon dioxide, in air 1.96 None\n",
"\n",
"RoW treatment of used vegetable cooking oil by anaerobic digestion\n",
"treatment of used vegetable cooking oil by anaerobic digestion 1.0 RoW\n",
"market for anaerobic digestion plant, agriculture, with methane recovery 2.8600029991482546e-07 GLO\n",
"digester sludge, Recycled Content cut-off -0.014840050607080909 GLO\n",
"market for electricity, low voltage 0.000276190662034128 NZ\n",
"market group for electricity, low voltage 0.08513873997904295 RAS\n",
"market group for electricity, low voltage 0.008863026830358674 RLA\n",
"market group for electricity, low voltage 0.030428354940241344 RNA\n",
"market for electricity, low voltage 0.0015187481923203123 AU\n",
"market for electricity, low voltage 0.006210977963085262 RU\n",
"market group for electricity, low voltage 0.021091677506418875 Europe without Switzerland\n",
"market group for electricity, low voltage 0.0044722839264984765 RAF\n",
"market for heat, central or small-scale, other than natural gas 0.04701495343843929 Europe without Switzerland\n",
"market for heat, central or small-scale, other than natural gas 3.4229850465615606 RoW\n",
"market for used vegetable cooking oil 1.0203040506070808 GLO\n",
"Carbon dioxide, non-fossil 0.014484425350580084 None\n",
"Methane, non-fossil 0.002371442384653515 None\n",
"\n",
"CH treatment of biowaste by anaerobic digestion\n",
"treatment of biowaste by anaerobic digestion 1.0 CH\n",
"Carbon dioxide, in air 1.96 None\n",
"\n",
"CH market for biogas\n",
"market for biogas 1.0 CH\n",
"treatment of biowaste by anaerobic digestion 0.256065714034962 CH\n",
"treatment of used vegetable cooking oil by anaerobic digestion 0.00489496169155372 CH\n",
"anaerobic digestion of manure 0.505966091805325 CH\n",
"treatment of sewage sludge by anaerobic digestion 0.233073232468159 CH\n",
"\n",
"CH treatment of sewage sludge by anaerobic digestion\n",
"treatment of sewage sludge by anaerobic digestion 1.0 CH\n",
"Carbon dioxide, in air 1.96 None\n",
"\n",
"CH anaerobic digestion of manure\n",
"anaerobic digestion of manure 1.0 CH\n",
"anaerobic digestion plant construction, agriculture, with methane recovery 2.8600029991482456e-07 CH\n",
"digester sludge, Recycled Content cut-off -36.18257393164099 GLO\n",
"market for electricity, low voltage 0.158 CH\n",
"market for heat, central or small-scale, other than natural gas 3.47 CH\n",
"market for manure, liquid, cattle 20.451988955925966 GLO\n",
"market for manure, liquid, swine 12.27119337355558 GLO\n",
"market for manure, solid, cattle 4.462252135838392 GLO\n",
"Ammonia 0.0017503979810816943 None\n",
"Carbon dioxide, non-fossil 0.07089524938692314 None\n",
"Dinitrogen monoxide 0.0002490386623259582 None\n",
"Hydrogen sulfide 4.127583225650513e-05 None\n",
"Methane, non-fossil 0.01983423218575475 None\n",
"Carbon dioxide, in air 1.96 None\n",
"\n",
"RoW treatment of biowaste by anaerobic digestion\n",
"treatment of biowaste by anaerobic digestion 1.0 RoW\n",
"Carbon dioxide, in air 1.96 None\n",
"\n",
"CH treatment of used vegetable cooking oil by anaerobic digestion\n",
"treatment of used vegetable cooking oil by anaerobic digestion 1.0 CH\n",
"anaerobic digestion plant construction, agriculture, with methane recovery 2.8600029991482546e-07 CH\n",
"digester sludge, Recycled Content cut-off -0.014840050607080909 GLO\n",
"market for electricity, low voltage 0.158 CH\n",
"market for heat, central or small-scale, other than natural gas 3.4699999999999998 CH\n",
"market for used vegetable cooking oil 1.0203040506070808 GLO\n",
"Carbon dioxide, non-fossil 0.014484425350580084 None\n",
"Methane, non-fossil 0.002371442384653515 None\n",
"\n",
"RoW market for biogas\n",
"market for biogas 1.0 RoW\n",
"treatment of used vegetable cooking oil by anaerobic digestion 0.00489624552565748 RoW\n",
"treatment of sewage sludge by anaerobic digestion 0.233134609422851 RoW\n",
"anaerobic digestion of manure 0.505836008628021 RoW\n",
"treatment of biowaste by anaerobic digestion 0.256133136423471 RoW\n",
"\n"
]
}
Expand All @@ -381,7 +419,7 @@
"from collections import defaultdict\n",
"res = defaultdict(float)\n",
"for ds in ndb.scenarios[0][\"database\"]:\n",
" if ds[\"name\"]==\"market for petrol, low-sulfur\" and ds[\"location\"] == \"EUR\":\n",
" if ds[\"reference product\"]==\"biogas\":\n",
" print(ds[\"location\"], ds[\"name\"])\n",
" for e in ds[\"exchanges\"]:\n",
" print(e[\"name\"], e[\"amount\"], e.get(\"location\"))\n",
Expand Down
4 changes: 4 additions & 0 deletions premise/data/fuels/biomethane_correction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- treatment of sewage sludge by anaerobic digestion
- anaerobic digestion of manure
- treatment of biowaste by anaerobic digestion
- treatment of used vegetable cooking oil by anaerobic digestion
62 changes: 62 additions & 0 deletions premise/fuels.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
BIOFUEL_SOURCES = DATA_DIR / "fuels" / "biofuels_activities.yml"
FUEL_GROUPS = DATA_DIR / "fuels" / "fuel_groups.yaml"

def load_methane_correction_list():
"""
Load biomethane_correction.yaml file and return a list
"""
with open(DATA_DIR / "fuels" / "biomethane_correction.yaml", "r") as f:
methane_correction_list = yaml.safe_load(f)
return methane_correction_list

def fetch_mapping(filepath: str) -> dict:
"""Returns a dictionary from a YML file"""
Expand Down Expand Up @@ -295,6 +302,7 @@ def _update_fuels(scenario, version, system_model, cache=None):
scenario["iam data"].hydrogen_markets,
)
):
fuels.correct_biogas_activities()
fuels.generate_fuel_markets()
scenario["database"] = fuels.database
cache = fuels.cache
Expand Down Expand Up @@ -376,6 +384,60 @@ def __init__(
dim="variables",
)

def correct_biogas_activities(self):
"""
Some activities producing biogas are not given any
biogenic CO2 input, leading to imbalanced carbon flows
when combusted.
"""

list_biogas_activities = load_methane_correction_list()

# find datasets that have a name in the list
filters = [
ws.either(*[ws.equals("name", name)
for name in list_biogas_activities]),
]

biogas_datasets = ws.get_many(
self.database,
*filters,
ws.equals("reference product", "biogas"),
ws.equals("unit", "cubic meter"),
)

# add a flow of "Carbon dioxide, in air" to the dataset
# if not present. We add 1.96 kg CO2/m3 biogas.

for ds in biogas_datasets:
if not any(
exc
for exc in ws.biosphere(ds)
if exc["name"] == "Carbon dioxide, in air"
):
ds["exchanges"].append(
{
"uncertainty type": 0,
"amount": 1.96,
"type": "biosphere",
"name": "Carbon dioxide, in air",
"unit": "kilogram",
"categories": ("natural resource", "in air"),
"input": (
"biosphere3",
self.biosphere_flows[
(
"Carbon dioxide, in air",
"natural resource",
"in air",
"kilogram",
)
],
),
}
)


def find_transport_activity(
self, items_to_look_for: List[str], items_to_exclude: List[str], loc: str
) -> Tuple[str, str, str, str]:
Expand Down

0 comments on commit db1eac0

Please sign in to comment.