Fix code style

This commit is contained in:
Michael Osthege 2024-10-04 11:46:52 +02:00 committed by Andreas
parent 4458b9f841
commit 2fce372029
2 changed files with 214 additions and 46 deletions

View File

@ -31,7 +31,6 @@ opt_class = optimization_problem(
)
@app.route("/soc", methods=["GET"])
def flask_soc():
# MariaDB connection details
@ -381,4 +380,3 @@ if __name__ == "__main__":
print(
f"Could not bind to host {host}:{port}. Error: {e}"
) # Error handling for binding issues

View File

@ -1,14 +1,13 @@
import pytest
import numpy as np
from modules.class_ems import EnergieManagementSystem
from modules.class_akku import PVAkku
from modules.class_haushaltsgeraet import Haushaltsgeraet # Example import
from modules.class_ems import EnergieManagementSystem
from modules.class_inverter import Wechselrichter # Example import
from modules.class_load_container import Gesamtlast # Example import
prediction_hours = 48
optimization_hours = 24
# Example initialization of necessary components
@pytest.fixture
def create_ems_instance():
@ -28,31 +27,158 @@ def create_ems_instance():
# Parameters based on previous example data
pv_prognose_wh = [
0, 0, 0, 0, 0, 0, 0, 8.05, 352.91, 728.51, 930.28, 1043.25, 1106.74, 1161.69,
6018.82, 5519.07, 3969.88, 3017.96, 1943.07, 1007.17, 319.67, 7.88, 0, 0, 0, 0,
0, 0, 0, 0, 0, 5.04, 335.59, 705.32, 1121.12, 1604.79, 2157.38, 1433.25, 5718.49,
4553.96, 3027.55, 2574.46, 1720.4, 963.4, 383.3, 0, 0, 0,
0,
0,
0,
0,
0,
0,
0,
8.05,
352.91,
728.51,
930.28,
1043.25,
1106.74,
1161.69,
6018.82,
5519.07,
3969.88,
3017.96,
1943.07,
1007.17,
319.67,
7.88,
0,
0,
0,
0,
0,
0,
0,
0,
0,
5.04,
335.59,
705.32,
1121.12,
1604.79,
2157.38,
1433.25,
5718.49,
4553.96,
3027.55,
2574.46,
1720.4,
963.4,
383.3,
0,
0,
0,
]
strompreis_euro_pro_wh = [
0.0003384, 0.0003318, 0.0003284, 0.0003283, 0.0003289, 0.0003334, 0.0003290,
0.0003302, 0.0003042, 0.0002430, 0.0002280, 0.0002212, 0.0002093, 0.0001879,
0.0001838, 0.0002004, 0.0002198, 0.0002270, 0.0002997, 0.0003195, 0.0003081,
0.0002969, 0.0002921, 0.0002780, 0.0003384, 0.0003318, 0.0003284, 0.0003283,
0.0003289, 0.0003334, 0.0003290, 0.0003302, 0.0003042, 0.0002430, 0.0002280,
0.0002212, 0.0002093, 0.0001879, 0.0001838, 0.0002004, 0.0002198, 0.0002270,
0.0002997, 0.0003195, 0.0003081, 0.0002969, 0.0002921, 0.0002780,
0.0003384,
0.0003318,
0.0003284,
0.0003283,
0.0003289,
0.0003334,
0.0003290,
0.0003302,
0.0003042,
0.0002430,
0.0002280,
0.0002212,
0.0002093,
0.0001879,
0.0001838,
0.0002004,
0.0002198,
0.0002270,
0.0002997,
0.0003195,
0.0003081,
0.0002969,
0.0002921,
0.0002780,
0.0003384,
0.0003318,
0.0003284,
0.0003283,
0.0003289,
0.0003334,
0.0003290,
0.0003302,
0.0003042,
0.0002430,
0.0002280,
0.0002212,
0.0002093,
0.0001879,
0.0001838,
0.0002004,
0.0002198,
0.0002270,
0.0002997,
0.0003195,
0.0003081,
0.0002969,
0.0002921,
0.0002780,
]
einspeiseverguetung_euro_pro_wh = [0.00007] * len(strompreis_euro_pro_wh)
gesamtlast = [
676.71, 876.19, 527.13, 468.88, 531.38, 517.95, 483.15, 472.28,
1011.68, 995.00, 1053.07, 1063.91, 1320.56, 1132.03, 1163.67, 1176.82,
1216.22, 1103.78, 1129.12, 1178.71, 1050.98, 988.56, 912.38, 704.61,
516.37, 868.05, 694.34, 608.79, 556.31, 488.89, 506.91, 804.89,
1141.98, 1056.97, 992.46, 1155.99, 827.01, 1257.98, 1232.67, 871.26,
860.88, 1158.03, 1222.72, 1221.04, 949.99, 987.01, 733.99, 592.97,
676.71,
876.19,
527.13,
468.88,
531.38,
517.95,
483.15,
472.28,
1011.68,
995.00,
1053.07,
1063.91,
1320.56,
1132.03,
1163.67,
1176.82,
1216.22,
1103.78,
1129.12,
1178.71,
1050.98,
988.56,
912.38,
704.61,
516.37,
868.05,
694.34,
608.79,
556.31,
488.89,
506.91,
804.89,
1141.98,
1056.97,
992.46,
1155.99,
827.01,
1257.98,
1232.67,
871.26,
860.88,
1158.03,
1222.72,
1221.04,
949.99,
987.01,
733.99,
592.97,
]
# Initialize the energy management system with the respective parameters
@ -94,48 +220,92 @@ def test_simulation(create_ems_instance):
# Verify that the expected keys are present in the result
expected_keys = [
'Last_Wh_pro_Stunde', 'Netzeinspeisung_Wh_pro_Stunde', 'Netzbezug_Wh_pro_Stunde',
'Kosten_Euro_pro_Stunde', 'akku_soc_pro_stunde', 'Einnahmen_Euro_pro_Stunde',
'Gesamtbilanz_Euro', 'E-Auto_SoC_pro_Stunde', 'Gesamteinnahmen_Euro',
'Gesamtkosten_Euro', 'Verluste_Pro_Stunde', 'Gesamt_Verluste',
'Haushaltsgeraet_wh_pro_stunde'
"Last_Wh_pro_Stunde",
"Netzeinspeisung_Wh_pro_Stunde",
"Netzbezug_Wh_pro_Stunde",
"Kosten_Euro_pro_Stunde",
"akku_soc_pro_stunde",
"Einnahmen_Euro_pro_Stunde",
"Gesamtbilanz_Euro",
"E-Auto_SoC_pro_Stunde",
"Gesamteinnahmen_Euro",
"Gesamtkosten_Euro",
"Verluste_Pro_Stunde",
"Gesamt_Verluste",
"Haushaltsgeraet_wh_pro_stunde",
]
for key in expected_keys:
assert key in result, f"The key '{key}' should be present in the result."
# Check the length of the main arrays
assert len(result['Last_Wh_pro_Stunde']) == 47, "The length of 'Last_Wh_pro_Stunde' should be 48."
assert len(result['Netzeinspeisung_Wh_pro_Stunde']) == 47, "The length of 'Netzeinspeisung_Wh_pro_Stunde' should be 48."
assert len(result['Netzbezug_Wh_pro_Stunde']) == 47, "The length of 'Netzbezug_Wh_pro_Stunde' should be 48."
assert len(result['Kosten_Euro_pro_Stunde']) == 47, "The length of 'Kosten_Euro_pro_Stunde' should be 48."
assert len(result['akku_soc_pro_stunde']) == 47, "The length of 'akku_soc_pro_stunde' should be 48."
assert (
len(result["Last_Wh_pro_Stunde"]) == 47
), "The length of 'Last_Wh_pro_Stunde' should be 48."
assert (
len(result["Netzeinspeisung_Wh_pro_Stunde"]) == 47
), "The length of 'Netzeinspeisung_Wh_pro_Stunde' should be 48."
assert (
len(result["Netzbezug_Wh_pro_Stunde"]) == 47
), "The length of 'Netzbezug_Wh_pro_Stunde' should be 48."
assert (
len(result["Kosten_Euro_pro_Stunde"]) == 47
), "The length of 'Kosten_Euro_pro_Stunde' should be 48."
assert (
len(result["akku_soc_pro_stunde"]) == 47
), "The length of 'akku_soc_pro_stunde' should be 48."
# Verify specific values in the 'Last_Wh_pro_Stunde' array
assert result['Last_Wh_pro_Stunde'][1] == 23759.13, "The value at index 1 of 'Last_Wh_pro_Stunde' should be 23759.13."
assert result['Last_Wh_pro_Stunde'][2] == 996.88, "The value at index 2 of 'Last_Wh_pro_Stunde' should be 996.88."
assert result['Last_Wh_pro_Stunde'][12] == 1132.03, "The value at index 12 of 'Last_Wh_pro_Stunde' should be 1132.03."
assert (
result["Last_Wh_pro_Stunde"][1] == 23759.13
), "The value at index 1 of 'Last_Wh_pro_Stunde' should be 23759.13."
assert (
result["Last_Wh_pro_Stunde"][2] == 996.88
), "The value at index 2 of 'Last_Wh_pro_Stunde' should be 996.88."
assert (
result["Last_Wh_pro_Stunde"][12] == 1132.03
), "The value at index 12 of 'Last_Wh_pro_Stunde' should be 1132.03."
# Verify that the value at index 0 is 'None'
assert result['Last_Wh_pro_Stunde'][0] is None, "The value at index 0 of 'Last_Wh_pro_Stunde' should be None."
assert (
result["Last_Wh_pro_Stunde"][0] is None
), "The value at index 0 of 'Last_Wh_pro_Stunde' should be None."
# Check that 'Netzeinspeisung_Wh_pro_Stunde' and 'Netzbezug_Wh_pro_Stunde' are consistent
assert result['Netzeinspeisung_Wh_pro_Stunde'][0] is None, "The value at index 0 of 'Netzeinspeisung_Wh_pro_Stunde' should be None."
assert result['Netzbezug_Wh_pro_Stunde'][0] is None, "The value at index 0 of 'Netzbezug_Wh_pro_Stunde' should be None."
assert result['Netzbezug_Wh_pro_Stunde'][1] == 20239.13, "The value at index 1 of 'Netzbezug_Wh_pro_Stunde' should be 20239.13."
assert (
result["Netzeinspeisung_Wh_pro_Stunde"][0] is None
), "The value at index 0 of 'Netzeinspeisung_Wh_pro_Stunde' should be None."
assert (
result["Netzbezug_Wh_pro_Stunde"][0] is None
), "The value at index 0 of 'Netzbezug_Wh_pro_Stunde' should be None."
assert (
result["Netzbezug_Wh_pro_Stunde"][1] == 20239.13
), "The value at index 1 of 'Netzbezug_Wh_pro_Stunde' should be 20239.13."
# Verify the total balance
assert abs(result['Gesamtbilanz_Euro'] - 8.434942129454546) < 1e-5, "Total balance should be 8.434942129454546."
assert (
abs(result["Gesamtbilanz_Euro"] - 8.434942129454546) < 1e-5
), "Total balance should be 8.434942129454546."
# Check total revenue and total costs
assert abs(result['Gesamteinnahmen_Euro'] - 1.237432954545454) < 1e-5, "Total revenue should be 1.237432954545454."
assert abs(result['Gesamtkosten_Euro'] - 9.672375084) < 1e-5, "Total costs should be 9.672375084."
assert (
abs(result["Gesamteinnahmen_Euro"] - 1.237432954545454) < 1e-5
), "Total revenue should be 1.237432954545454."
assert (
abs(result["Gesamtkosten_Euro"] - 9.672375084) < 1e-5
), "Total costs should be 9.672375084."
# Check the losses
assert abs(result['Gesamt_Verluste'] - 6111.586363636363) < 1e-5, "Total losses should be 6111.586363636363."
assert (
abs(result["Gesamt_Verluste"] - 6111.586363636363) < 1e-5
), "Total losses should be 6111.586363636363."
# Check the values in 'akku_soc_pro_stunde'
assert result['akku_soc_pro_stunde'][-1] == 28.675, "The value at index -1 of 'akku_soc_pro_stunde' should be 28.675."
assert result['akku_soc_pro_stunde'][1] == 0.0, "The value at index 1 of 'akku_soc_pro_stunde' should be 0.0."
assert (
result["akku_soc_pro_stunde"][-1] == 28.675
), "The value at index -1 of 'akku_soc_pro_stunde' should be 28.675."
assert (
result["akku_soc_pro_stunde"][1] == 0.0
), "The value at index 1 of 'akku_soc_pro_stunde' should be 0.0."
print("All tests passed successfully.")