Commit 6ef9927b authored by Eric Duminil's avatar Eric Duminil
Browse files

Added KPIs and tests for EnergyGrid

parent dfe00f2e
......@@ -2,6 +2,7 @@ import json
from dataclasses import dataclass
from typing import ClassVar
import numpy as np
import pandas as pd
from .base import SimStadtResults
......@@ -22,16 +23,30 @@ class EnergyGridResults(SimStadtResults):
json_output = jsons[0]
with open(json_output, encoding="utf-8") as out:
content = json.load(out)
# TODO: Add infos about buildings. Total consumption for example?
return pd.DataFrame(
{
"ambient_temperature": content["ambient_temperature"],
}
)
buildings = content["buildings"]
df = pd.DataFrame({
"ambient_temperature": content["ambient_temperature"],
"electricity_demand": np.sum([b["building_electricity_demand"] for b in buildings], axis=0),
"heat_demand": np.sum([b["th_heat_demand"] for b in buildings], axis=0),
"pv_production": np.sum([b["pv_production"] for b in buildings], axis=0),
})
df.attrs["number_of_buildings"] = len(buildings)
df.attrs["total_heated_area"] = sum(b["heated_area"] for b in buildings)
return df
@property
def kpis(self) -> list[KPI]:
df = self.dataframe
total_heated_area = df.attrs["total_heated_area"]
total_heat_kwh = df["heat_demand"].sum()
total_elec_kwh = df["electricity_demand"].sum()
return [
KPI("Number of buildings", df.attrs["number_of_buildings"]),
KPI("Total heated area", total_heated_area, "m²", precision=0),
KPI("Total electricity demand", total_elec_kwh / 1000, "MWh / a", precision=1),
KPI("Specific electricity demand", total_elec_kwh / total_heated_area, "kWh / (m² · a)", precision=1),
KPI("Total heat demand", total_heat_kwh / 1000, "MWh / a", precision=1),
KPI("Specific heat demand", total_heat_kwh / total_heated_area, "kWh / (m² · a)", precision=1),
KPI("Total PV production", df["pv_production"].sum() / 1000, "MWh / a", precision=1),
KPI("Average temperature", float(df["ambient_temperature"].mean()), "°C", precision=1),
]
......@@ -644,13 +644,30 @@ def test_energy_grid_output_files_exist(energy_grid_results):
def test_energy_grid_dataframe(energy_grid_results):
df = energy_grid_results.dataframe
assert isinstance(df, pd.DataFrame)
assert df.shape == (8760, 1)
assert list(df.columns) == ["ambient_temperature"]
assert df.shape == (8760, 4)
assert list(df.columns) == ["ambient_temperature", "electricity_demand", "heat_demand", "pv_production"]
assert df.attrs["number_of_buildings"] == 3
def test_energy_grid_kpis(energy_grid_results):
kpis = kpis_by_name(energy_grid_results)
assert kpis["Number of buildings"].value == 3
assert kpis["Number of buildings"].unit is None
assert kpis["Total heated area"].value == pytest.approx(1198.1, abs=0.1)
assert kpis["Total heated area"].unit == "m²"
assert kpis["Total electricity demand"].value == pytest.approx(17.0, abs=0.1)
assert kpis["Total electricity demand"].unit == "MWh / a"
assert kpis["Specific electricity demand"].value == pytest.approx(14.2, abs=0.5)
assert kpis["Specific electricity demand"].unit == "kWh / (m² · a)"
assert kpis["Total heat demand"].value == pytest.approx(117.9, abs=0.1)
assert kpis["Total heat demand"].unit == "MWh / a"
assert kpis["Specific heat demand"].value == pytest.approx(98.4, abs=0.5)
assert kpis["Specific heat demand"].unit == "kWh / (m² · a)"
assert kpis["Total PV production"].value == pytest.approx(34.5, abs=0.1)
assert kpis["Total PV production"].unit == "MWh / a"
assert kpis["Average temperature"].value == pytest.approx(9.95, abs=0.05)
assert kpis["Average temperature"].unit == "°C"
assert len(kpis) == 8
# ---------------------------------------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment