From 8b083557279d17ff7a5ce9e4700c7bf4aea8ea28 Mon Sep 17 00:00:00 2001 From: Songmin Yu Date: Tue, 20 Feb 2024 18:51:09 +0100 Subject: [PATCH] projects folder template --- projects/analysis/calculator.py | 109 -------------------------------- projects/analysis/plotter.py | 2 + projects/analysis/processor.py | 2 + projects/main.py | 15 ++--- 4 files changed, 9 insertions(+), 119 deletions(-) delete mode 100644 projects/analysis/calculator.py create mode 100644 projects/analysis/processor.py diff --git a/projects/analysis/calculator.py b/projects/analysis/calculator.py deleted file mode 100644 index aeea8e8c..00000000 --- a/projects/analysis/calculator.py +++ /dev/null @@ -1,109 +0,0 @@ -import os.path - -import numpy as np -import pandas as pd -from utils.parquet import read_parquet -from utils.config import Config -from utils.db import create_db_conn -from utils.tables import InputTables, OutputTables - -PROJECT_SUMMARY_YEAR = "SummaryYear" -PROJECT_SUMMARY_HOUR = "SummaryHour" - - -def merge_scenario(config: "Config"): - conn = create_db_conn(config) - scenarios = conn.read_dataframe(InputTables.OperationScenario.name) - ref = conn.read_dataframe(OutputTables.OperationResult_RefYear.name).set_index("ID_Scenario") - opt = conn.read_dataframe(OutputTables.OperationResult_OptYear.name).set_index("ID_Scenario") - buildings = conn.read_dataframe(InputTables.OperationScenario_Component_Building.name).set_index("ID_Building") - l = [] - id_merged_scenario = 1 - for index, row in scenarios.iterrows(): - for result_index, result_df in enumerate([ref, opt]): - d = {} - d["Country"] = config.project_name.split("_")[0] - d["Year"] = int(config.project_name.split("_")[1]) - d["ID_MergedScenario"] = id_merged_scenario - d["ID_Scenario"] = row["ID_Scenario"] - d["ID_SEMS"] = result_index + 1 # 1: no prosumaging; 2: with prosumaging - d["ID_Teleworking"] = buildings.at[row["ID_Building"], "id_demand_profile_type"] # 1: no teleworking; 2: with teleworking - d["ID_PV"] = row["ID_PV"] - d["ID_Battery"] = row["ID_Battery"] - d["ID_Boiler"] = row["ID_Boiler"] - d["unit"] = "kWh" - d["Useful_Appliance"] = result_df.at[row["ID_Scenario"], "BaseLoadProfile"] / 1000 - d["Useful_HotWater"] = result_df.at[row["ID_Scenario"], "HotWaterProfile"] / 1000 - d["Useful_SpaceHeating"] = result_df.at[row["ID_Scenario"], "Q_RoomHeating"] / 1000 - d["Final_PVGeneration"] = result_df.at[row["ID_Scenario"], "PhotovoltaicProfile"] / 1000 - d["Final_PVFeed"] = result_df.at[row["ID_Scenario"], "PV2Grid"] / 1000 - d["Final_Electricity"] = result_df.at[row["ID_Scenario"], "Grid"] / 1000 - d["Final_Fuel"] = result_df.at[row["ID_Scenario"], "Fuel"] / 1000 - d["BuildingNumber"] = row["building_num"] - d["Total_Useful_Appliance"] = d["Useful_Appliance"] * row["building_num"] - d["Total_Useful_HotWater"] = d["Useful_HotWater"] * row["building_num"] - d["Total_Useful_SpaceHeating"] = d["Useful_SpaceHeating"] * row["building_num"] - d["Total_Final_PVGeneration"] = d["Final_PVGeneration"] * row["building_num"] - d["Total_Final_PVFeed"] = d["Final_PVFeed"] * row["building_num"] - d["Total_Final_Electricity"] = d["Final_Electricity"] * row["building_num"] - d["Total_Final_Fuel"] = d["Final_Fuel"] * row["building_num"] - d["Total_Final_HeatingSystem"] = d["Total_Final_Fuel"] + d["Total_Final_Electricity"] - d["Total_Useful_Appliance"] - l.append(d) - id_merged_scenario += 1 - pd.DataFrame(l).to_excel(os.path.join(config.output, f"{PROJECT_SUMMARY_YEAR}.xlsx"), index=False) - - -def calc_electricity_profiles(config: "Config"): - conn = create_db_conn(config) - scenarios = conn.read_dataframe(InputTables.OperationScenario.name).set_index("ID_Scenario") - electricity_price = conn.read_dataframe(InputTables.OperationScenario_EnergyPrice.name)["electricity_1"] - summary = pd.read_excel(os.path.join(config.output, f"{PROJECT_SUMMARY_YEAR}.xlsx")) - l = [] - for id_sems in [1, 2]: - for id_teleworking in [1, 2]: - for id_boiler in [1, 2]: - for id_pv in [1, 2]: - for id_battery in [1, 2]: - df = summary.loc[ - (summary["ID_SEMS"] == id_sems) & - (summary["ID_Teleworking"] == id_teleworking) & - (summary["ID_Boiler"] == id_boiler) & - (summary["ID_PV"] == id_pv) & - (summary["ID_Battery"] == id_battery) - ] - scenario_ids = df["ID_Scenario"].to_list() - electricity_profile = np.zeros(8760, ) - bat_discharge = np.zeros(8760, ) - for id_scenario in scenario_ids: - building_num = scenarios.at[id_scenario, "building_num"] - if id_sems == 1: - file_name = f'OperationResult_RefHour_S{id_scenario}' - else: - file_name = f'OperationResult_OptHour_S{id_scenario}' - electricity_profile += building_num * read_parquet(file_name, config.output)["Grid"].to_numpy() - bat_discharge += building_num * read_parquet(file_name, config.output)["BatDischarge"].to_numpy() - for hour in range(1, 8761): - l.append({ - "Country": config.project_name.split("_")[0], - "Year": int(config.project_name.split("_")[1]), - "ID_SEMS": id_sems, - "ID_Teleworking": id_teleworking, - "ID_Boiler": id_boiler, - "ID_PV": id_pv, - "ID_Battery": id_battery, - "YearHour": hour, - "DayHour": hour % 24 if hour % 24 != 0 else 24, - "demand_unit": "GWh", - "ElectricityDemand": electricity_profile[hour - 1]/10**9, - "BatDischarge": bat_discharge[hour - 1]/10**9, - "price_unit": "Euro/kWh", - "ElectricityPrice": electricity_price[hour - 1] * 10 - }) - pd.DataFrame(l).to_excel(os.path.join(config.output, f"{PROJECT_SUMMARY_HOUR}.xlsx"), index=False) - - -def get_building_hour_profile(config: "Config", id_scenario: int, model_name: str, var_name: str): - file_name = f'OperationResult_{model_name}Hour_S{id_scenario}' - return read_parquet(file_name, config.output)[var_name].to_numpy() - - diff --git a/projects/analysis/plotter.py b/projects/analysis/plotter.py index e69de29b..56aa688a 100644 --- a/projects/analysis/plotter.py +++ b/projects/analysis/plotter.py @@ -0,0 +1,2 @@ + +# This file holds the plotting functions for the projects diff --git a/projects/analysis/processor.py b/projects/analysis/processor.py new file mode 100644 index 00000000..60933c32 --- /dev/null +++ b/projects/analysis/processor.py @@ -0,0 +1,2 @@ + +# This file holds the functions for processing results of the projects diff --git a/projects/main.py b/projects/main.py index d080a9af..31a7d3ee 100644 --- a/projects/main.py +++ b/projects/main.py @@ -1,11 +1,9 @@ import os -import random + from models.operation.main import run_operation_model -from models.operation.main import run_operation_model_parallel from plotters.operation import household_load_balance from utils.config import Config from utils.db import init_project_db -from projects.analysis import calculator as calc def get_config(project_name: str): @@ -17,18 +15,15 @@ def get_config(project_name: str): def run_flex_operation_model(config: "Config"): init_project_db(config) - # run_operation_model(config=config, save_hour=True, scenario_ids=[1]) - run_operation_model_parallel(config=config, task_num=8, save_hour=True) + run_operation_model(config=config, save_hour=True, scenario_ids=[1]) def run_flex_operation_plotter(config: "Config"): - household_load_balance(config, scenario_ids=random.sample(range(1, 4375), 100)) + household_load_balance(config, scenario_ids=[1]) if __name__ == "__main__": - cfg = get_config("DEU_2020") + cfg = get_config("PROJECT_FOLDER_NAME") run_flex_operation_model(cfg) - # run_flex_operation_plotter(cfg) - calc.merge_scenario(cfg) - calc.calc_electricity_profiles(cfg) + run_flex_operation_plotter(cfg)