-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactory energy calculation #185
base: main
Are you sure you want to change the base?
Changes from all commits
02d882e
f3b4564
2ec2e45
1601b38
48f2505
6fd3727
56f42a7
0091866
8e1f104
a434568
2228aff
b6ccc87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"""Function colleciton to calculate energy type co2 emissions""" | ||
|
||
from typing import Union | ||
from co2calculator.constants import Unit | ||
from co2calculator.data_handlers import ConversionFactors, EmissionFactors | ||
from co2calculator.parameters import ( | ||
ElectricityEmissionParameters, | ||
ElectricityParameters, | ||
HeatingEmissionParameters, | ||
HeatingParameters, | ||
) | ||
from co2calculator._types import Kilogram | ||
|
||
emission_factors = EmissionFactors() | ||
conversion_factors = ConversionFactors() | ||
|
||
|
||
def calc_co2_heating( | ||
consumption: float, options: Union[HeatingParameters, dict] | ||
) -> Kilogram: | ||
"""Function to compute heating emissions | ||
|
||
:param consumption: energy consumption | ||
:param options: parameters for heating emissions calculation | ||
:type consumption: float | ||
:type options: HeatingParameters | dict | ||
:return: total emissions of heating energy consumption | ||
:rtype: Kilogram | ||
""" | ||
# Validate parameters | ||
if options is None: | ||
options = {} | ||
params = HeatingParameters(**options) | ||
emission_params = HeatingEmissionParameters( | ||
**params.heating_emission_parameters.dict() | ||
) | ||
|
||
# Get the co2 factor | ||
co2e = emission_factors.get(emission_params.dict()) | ||
|
||
if params.unit is not Unit.KWH: | ||
print(emission_params.fuel_type, params.unit) | ||
# Get the conversion factor | ||
conversion_factor = conversion_factors.get( | ||
fuel_type=emission_params.fuel_type, unit=params.unit | ||
) | ||
|
||
consumption_kwh = consumption * conversion_factor | ||
else: | ||
consumption_kwh = consumption | ||
|
||
# co2 equivalents for heating and electricity refer to a consumption of 1 TJ | ||
# so consumption needs to be converted to TJ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments can be removed as we are not converting to TJ anymore |
||
return consumption_kwh * params.area_share * co2e | ||
|
||
|
||
def calc_co2_electricity( | ||
consumption: float, options: Union[ElectricityParameters, dict] | ||
) -> Kilogram: | ||
"""Function to compute electricity emissions | ||
|
||
:param consumption: energy consumption | ||
:param fuel_type: energy (mix) used for electricity [german_energy_mix, solar] | ||
:param energy_share: the research group's approximate share of the total electricity energy consumption | ||
:type consumption: float | ||
:type fuel_type: str | ||
:type energy_share: float | ||
:return: total emissions of electricity energy consumption | ||
:rtype: Kilogram | ||
""" | ||
|
||
# Validate parameters | ||
if options is None: | ||
options = {} | ||
params = ElectricityParameters(**options) | ||
emission_params = ElectricityEmissionParameters( | ||
**params.electricity_emission_parameters.dict() | ||
) | ||
|
||
# Get the co2 factor | ||
co2e = emission_factors.get(emission_params.dict()) | ||
|
||
# co2 equivalents for heating and electricity refer to a consumption of 1 TJ | ||
# so consumption needs to be converted to TJ | ||
return consumption * params.energy_share / co2e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be multiplication not division! (I think we had this fixed somewhere before already?) and comments can be removed, as we are not converting to TJ anymore (with the new emission factors) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
1,liquid_gas,kg,14.1 | ||
2,coal,kg,6.0 | ||
3,pellet,kg,5.4 | ||
4,woodchips,kg,5.2 | ||
4,wood chips,kg,5.2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep it as one word - or alternatively "wood_chips" (similar to "liquid_gas" |
||
5,gas,m^3,10.8 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""tests for energy calculation""" | ||
import co2calculator.energy.calculate_energy as energy | ||
import pytest | ||
|
||
|
||
def test_heating_woodchips(): | ||
"""Test co2e calculation for heating: wood chips""" | ||
# Given parameters | ||
consumption = 250 | ||
co2e_kg_expected = 13.962 | ||
func_options = { | ||
# Given parameters | ||
"heating_emission_parameters": { | ||
"fuel_type": "wood chips" # emission factor: 9322 kg/TJ | ||
}, | ||
"unit": "kg", # conversion factor to kWh = 5.4 | ||
} | ||
# Calculate co2e | ||
co2e = energy.calc_co2_heating(consumption=consumption, options=func_options) | ||
|
||
# Check if expected result matches calculated result | ||
assert co2e == pytest.approx(co2e_kg_expected, rel=0.01) | ||
|
||
|
||
def test_electricity(): | ||
"""Test co2e calculation for electricity""" | ||
# Given parameters | ||
consumption_kwh = 10000 | ||
co2e_kg_expected = 22265 | ||
|
||
func_options = { | ||
"electricity_emission_parameters": { | ||
"fuel_type": "production fuel mix", | ||
"country_code": "DE", | ||
} | ||
} | ||
|
||
# Calculate co2e | ||
co2e = energy.calc_co2_electricity( | ||
consumption=consumption_kwh, options=func_options | ||
) | ||
|
||
# Check if expected result matches calculated result | ||
assert co2e == pytest.approx(co2e_kg_expected, rel=0.01) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall this print statement be kept here?