Skip to content

Commit

Permalink
add starting_datetime to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muelleram committed Aug 5, 2024
1 parent 43d935e commit 1242d25
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tests/test_dynamic_biomatrix_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setup_method(self):
database_date_dict=database_date_dict,
)

self.tlca.build_timeline()
self.tlca.build_timeline(starting_datetime=datetime.strptime("2024-01-02", "%Y-%m-%d"))
self.tlca.lci(expand_technosphere=True, build_dynamic_biosphere=True)
self.tlca.static_lcia()

Expand Down
199 changes: 164 additions & 35 deletions tests/test_electric_vehicle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Testing the results of the static LCA and the timexLCA for a simple test case of electric vehicle to see if the new interpolated amounts are correct.
"""

from pathlib import Path
import pytest
import bw2calc as bc
Expand All @@ -17,35 +18,38 @@ def test_vehicle_db_fixture(vehicle_db):
# for now, one test class, but could be set up more modularly
@pytest.mark.usefixtures("vehicle_db")
class TestClass_EV:

@pytest.fixture(autouse=True)
def setup_method(self, vehicle_db):
self.electric_vehicle = bd.get_node(database="foreground", code="EV")


database_date_dict = {
"db_2020": datetime.strptime("2020", "%Y"),
"db_2030": datetime.strptime("2030", "%Y"),
"db_2040": datetime.strptime("2040", "%Y"),
"foreground": "dynamic",
"foreground": "dynamic",
}

self.tlca = TimexLCA(demand={self.electric_vehicle.key: 1}, method=("GWP", "example"), database_date_dict = database_date_dict)
self.tlca = TimexLCA(
demand={self.electric_vehicle.key: 1},
method=("GWP", "example"),
database_date_dict=database_date_dict,
)

self.tlca.build_timeline()
self.tlca.build_timeline(
starting_datetime=datetime.strptime("2024-01-02", "%Y-%m-%d")
)
self.tlca.lci()
self.tlca.static_lcia()


def test_static_lca_score(self):
slca = bc.LCA({self.electric_vehicle.key: 1}, method=("GWP", "example"))
slca.lci()
slca.lcia()
expected_static_score = slca.score

assert self.tlca.static_lca.score == expected_static_score


def test_bw_timex_score(self):
ELECTRICITY_CONSUMPTION = 0.2 # kWh/km
MILEAGE = 150_000 # km
Expand All @@ -56,30 +60,155 @@ def test_bw_timex_score(self):
MASS_POWERTRAIN = 80 # kg
MASS_BATTERY = 280 # kg

expected_glider_score = ( 0.6 * MASS_GLIDER * 0.8 * [exc["amount"] for exc in bd.get_activity(("db_2020", "glider")).biosphere()][0] + # 2022 -> 80% 2020, 20% 2030
0.6 * MASS_GLIDER * 0.2 * [exc["amount"] for exc in bd.get_activity(("db_2030", "glider")).biosphere()][0] +
0.4 * MASS_GLIDER * 0.7 * [exc["amount"] for exc in bd.get_activity(("db_2020", "glider")).biosphere()][0] + # 2023 -> 70% 2020, 30% 2030
0.4 * MASS_GLIDER * 0.3 * [exc["amount"] for exc in bd.get_activity(("db_2030", "glider")).biosphere()][0])

expected_powertrain_score = ( 0.6 * MASS_POWERTRAIN * 0.8 * [exc["amount"] for exc in bd.get_activity(("db_2020", "powertrain")).biosphere()][0] + # 2022 -> 80% 2020, 20% 2030
0.6 * MASS_POWERTRAIN * 0.2 * [exc["amount"] for exc in bd.get_activity(("db_2030", "powertrain")).biosphere()][0] +
0.4 * MASS_POWERTRAIN * 0.7 * [exc["amount"] for exc in bd.get_activity(("db_2020", "powertrain")).biosphere()][0] + # 2023 -> 70% 2020, 30% 2030
0.4 * MASS_POWERTRAIN * 0.3 * [exc["amount"] for exc in bd.get_activity(("db_2030", "powertrain")).biosphere()][0])

expected_battery_score = ( 0.6 * MASS_BATTERY * 0.8 * [exc["amount"] for exc in bd.get_activity(("db_2020", "battery")).biosphere()][0] + # 2022 -> 80% 2020, 20% 2030
0.6 * MASS_BATTERY * 0.2 * [exc["amount"] for exc in bd.get_activity(("db_2030", "battery")).biosphere()][0] +
0.4 * MASS_BATTERY * 0.7 * [exc["amount"] for exc in bd.get_activity(("db_2020", "battery")).biosphere()][0] + # 2023 -> 70% 2020, 30% 2030
0.4 * MASS_BATTERY * 0.3 * [exc["amount"] for exc in bd.get_activity(("db_2030", "battery")).biosphere()][0])

expected_electricity_score = (MILEAGE * ELECTRICITY_CONSUMPTION * 0.8 * [exc["amount"] for exc in bd.get_activity(("db_2030", "electricity")).biosphere()][0] + # electricity in year 2032 -> 80% 2030, 20% 2040
MILEAGE * ELECTRICITY_CONSUMPTION * 0.2 * [exc["amount"] for exc in bd.get_activity(("db_2040", "electricity")).biosphere()][0])

expected_glider_recycling_score = (MASS_GLIDER * [exc["amount"] for exc in bd.get_activity(("db_2040", "dismantling")).biosphere()][0]) # dismantling 2041 -> 100% 2040


expected_battery_recycling_score = -(MASS_BATTERY * [exc["amount"] for exc in bd.get_activity(("db_2040", "battery_recycling")).biosphere()][0]) # dismantling 2041 -> 100% 2040, negative sign because of waste flow

expected_timex_score = expected_glider_score + expected_powertrain_score + expected_battery_score + expected_electricity_score + expected_glider_recycling_score + expected_battery_recycling_score

assert self.tlca.static_score == pytest.approx(expected_timex_score, abs = 0.5) #allow for some rounding errors

expected_glider_score = (
0.6
* MASS_GLIDER
* 0.8
* [
exc["amount"]
for exc in bd.get_activity(("db_2020", "glider")).biosphere()
][
0
] # 2022 -> 80% 2020, 20% 2030
+ 0.6
* MASS_GLIDER
* 0.2
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "glider")).biosphere()
][0]
+ 0.4
* MASS_GLIDER
* 0.7
* [
exc["amount"]
for exc in bd.get_activity(("db_2020", "glider")).biosphere()
][
0
] # 2023 -> 70% 2020, 30% 2030
+ 0.4
* MASS_GLIDER
* 0.3
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "glider")).biosphere()
][0]
)

expected_powertrain_score = (
0.6
* MASS_POWERTRAIN
* 0.8
* [
exc["amount"]
for exc in bd.get_activity(("db_2020", "powertrain")).biosphere()
][
0
] # 2022 -> 80% 2020, 20% 2030
+ 0.6
* MASS_POWERTRAIN
* 0.2
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "powertrain")).biosphere()
][0]
+ 0.4
* MASS_POWERTRAIN
* 0.7
* [
exc["amount"]
for exc in bd.get_activity(("db_2020", "powertrain")).biosphere()
][
0
] # 2023 -> 70% 2020, 30% 2030
+ 0.4
* MASS_POWERTRAIN
* 0.3
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "powertrain")).biosphere()
][0]
)

expected_battery_score = (
0.6
* MASS_BATTERY
* 0.8
* [
exc["amount"]
for exc in bd.get_activity(("db_2020", "battery")).biosphere()
][
0
] # 2022 -> 80% 2020, 20% 2030
+ 0.6
* MASS_BATTERY
* 0.2
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "battery")).biosphere()
][0]
+ 0.4
* MASS_BATTERY
* 0.7
* [
exc["amount"]
for exc in bd.get_activity(("db_2020", "battery")).biosphere()
][
0
] # 2023 -> 70% 2020, 30% 2030
+ 0.4
* MASS_BATTERY
* 0.3
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "battery")).biosphere()
][0]
)

expected_electricity_score = (
MILEAGE
* ELECTRICITY_CONSUMPTION
* 0.8
* [
exc["amount"]
for exc in bd.get_activity(("db_2030", "electricity")).biosphere()
][
0
] # electricity in year 2032 -> 80% 2030, 20% 2040
+ MILEAGE
* ELECTRICITY_CONSUMPTION
* 0.2
* [
exc["amount"]
for exc in bd.get_activity(("db_2040", "electricity")).biosphere()
][0]
)

expected_glider_recycling_score = (
MASS_GLIDER
* [
exc["amount"]
for exc in bd.get_activity(("db_2040", "dismantling")).biosphere()
][0]
) # dismantling 2041 -> 100% 2040

expected_battery_recycling_score = -(
MASS_BATTERY
* [
exc["amount"]
for exc in bd.get_activity(("db_2040", "battery_recycling")).biosphere()
][0]
) # dismantling 2041 -> 100% 2040, negative sign because of waste flow

expected_timex_score = (
expected_glider_score
+ expected_powertrain_score
+ expected_battery_score
+ expected_electricity_score
+ expected_glider_recycling_score
+ expected_battery_recycling_score
)

assert self.tlca.static_score == pytest.approx(
expected_timex_score, abs=0.5
) # allow for some rounding errors
4 changes: 3 additions & 1 deletion tests/test_nonunitary_unitprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def setup_method(self):
database_date_dict=database_date_dict,
)

self.tlca.build_timeline()
self.tlca.build_timeline(
starting_datetime=datetime.strptime("2024-01-02", "%Y-%m-%d"),
)
self.tlca.lci()
self.tlca.static_lcia()

Expand Down
4 changes: 3 additions & 1 deletion tests/test_substitution_exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def setup_method(self):
database_date_dict=database_date_dict,
)

self.tlca.build_timeline()
self.tlca.build_timeline(
starting_datetime=datetime.strptime("2024-01-02", "%Y-%m-%d")
)
self.tlca.lci()
self.tlca.static_lcia()

Expand Down

0 comments on commit 1242d25

Please sign in to comment.