Skip to content
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

Optimized dispatches from HERON to HYBRID #202

Merged
merged 14 commits into from
Aug 23, 2022
Merged
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
This test demonstrated converting the HERON optimized components' variables (optimized dispatch) to a text file that is compatible with HYBRID via two steps:



Step #0: Producing the HERON optimized dispatches
In this step, the HERON optimized dispatches are creates similar to creating the dispatches CSV file in the folder: /HERON/tests/integration_tests/mechanics/debug_mode. You can create the dispactes CSV file running the commands such as:
~/projects/HERON/heron heron_input.xml
~/projects/raven/raven_framework outer.xml



Step #1: "create_user_input.py"
This script is run to create a user-input file for the HYBRID user.
It is an initial step that the HYBRID user needs to create new names, if necessary, to: 1- The optimized dispatch outputs, 2- The components' capacities.
Expand All @@ -9,13 +18,14 @@ It takes the following arguments:
1- The HERON input XML file
2- The optimized dispatch outputs csv file.
For example, To run the script, use the following command:
python create_user_input.py heron_input.xml dispatch_print.csv
python create_user_input.py heron_input.xml Debug_Run_o/dispatch_print.csv
The output will be a user input file: "user_input.txt".
The user needs to review the user input file and change/review the HYRBID variables and capacities there.

Next, the user can run the "export2Hybrid.py" which loads the dispatch outpus from HERON to a new file that HYBRID can use. The variables names in the autoloaded HYBRID file are borrowd from "user_input.txt"



Step #2: "export2Hybrid.py"
A script to convert the HERON optimized components' variables (optimized dispatch) to a text file that is compatible with HYBRID.

Expand All @@ -26,5 +36,5 @@ This script is run with the following arguments:
1- The HERON input XML file.
2- The optimized dispatch outputs CSV file.
For example, to run this script, use the following command:
python export2Hybrid.py heron_input.xml dispatch_print.csv
python export2Hybrid.py heron_input.xml Debug_Run_o/dispatch_print.csv
The output will be a text file compatible with HYBRID: "hybrid_input.txt."
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
1- The HERON input XML file
2- The optimized dispatch outputs csv file.
For example, To run the script, use the following command:
python create_user_input.py heron_input.xml dispatch_print.csv
python create_user_input.py heron_input.xml Debug_Run_o/dispatch_print.csv
The output will be a user input file: "user_input.txt".
The user needs to review the user input file and change/review the HYRBID variables and capacities there.

Expand All @@ -25,18 +25,17 @@
import argparse
from xml.etree import ElementTree as ET
import pandas as pd
from colorama import Fore
from colorama import Style



#####
# Section 1: Extracting components' capacities Function
def extract_capacities_heron_hybrid(heron_input, output_file):
"""
Creates a list of components' capacities from the HERON inpue file plus creating placeholders for the components' capacities in HYRBID
@ In, heron input, str, the HERON input XML file
@ In, output_file, str, the text file where the list of components capacities should be printed.
@ Out, output_file, str, text a file autoloaded with the list of components' capacities from the HERON file and placeholders for the components' capacities in HYRBID
Creates a list of components' capacities from the HERON inpue file plus creating placeholders for the components' capacities in HYRBID
@ In, heron input, str, the HERON input XML file
@ In, output_file, str, the text file where the list of components capacities should be printed.
@ Out, None
"""

# Creating the HERON XML file tree
Expand Down Expand Up @@ -70,13 +69,12 @@ def extract_capacities_heron_hybrid(heron_input, output_file):
+ "\n"
)
print("\n")
print(len(comp_list), "components are found in:", heron_input, "\n")
print(len(comp_list), "components are found in:", heron_input)
print(
"The capacities of",
len(comp_list),
"components are printed at the",
output_file,
"\n",
output_file
)

if not comp_list:
Expand All @@ -87,10 +85,10 @@ def extract_capacities_heron_hybrid(heron_input, output_file):
# Section 2: Extracting dispatch outputs Function
def extract_dispatches_heron_hybrid(dispatch_print, output_file):
"""
Creates a list of dispatches (components' optimized variables) from the HERON inpue file plus creating placeholders for the dispatches in HYRBID
@ In, dispatch_print, str, the distpach csv file with a list of optimized variables
@ In, output_file, str, the text file where the list of optimized dispatches should be printed.
@ Out, output_file, str, a text file autoloaded with the optimized dispatches plus placeholders for the HYBRID dispatches
Creates a list of dispatches (components' optimized variables) from the HERON inpue file plus creating placeholders for the dispatches in HYRBID
@ In, dispatch_print, str, the distpach csv file with a list of optimized variables
@ In, output_file, str, the text file where the list of optimized dispatches should be printed.
@ Out, None
"""
input_dataset = pd.read_csv(dispatch_print)
# Extracting the HERON csv dispatches
Expand Down Expand Up @@ -119,8 +117,7 @@ def extract_dispatches_heron_hybrid(dispatch_print, output_file):
" optimized dispatches are found in:",
dispatch_print,
"and printed at the",
output_file,
"\n",
output_file
)


Expand Down Expand Up @@ -157,7 +154,8 @@ def extract_dispatches_heron_hybrid(dispatch_print, output_file):
extract_capacities_heron_hybrid(args.HERON_input_file, user_input_file)
extract_dispatches_heron_hybrid(args.HERON_dipatch_csv_file, user_input_file)

print(
f"{Fore.RED}The {user_input_file} file is created for the HYBRID user to change/review it{Style.RESET_ALL}",
"\n",
print('\033[94m',
f"The {user_input_file} file is created for the HYBRID user to change/review it",
"\n",
'\033[0m'
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
1- The HERON input XML file.
2- The optimized dispatch outputs CSV file.
For example, to run this script, use the following command:
python export2Hybrid.py heron_input.xml dispatch_print.csv
The output will be a text file compatible with HYBRID: "hybrid_input.txt."
python export2Hybrid.py heron_input.xml Debug_Run_o/dispatch_print.csv
The output will be a text file compatible with HYBRID: "hybrid_compatible_dispatch.txt"
"""

#####
Expand All @@ -23,28 +23,22 @@
import pandas as pd
import numpy as np
from xml.etree import ElementTree as ET
from colorama import Fore
from colorama import Style

#####
# Section 1: A Function to connect HERON/HYRBID capacities and dispatches in the user-input file
# Section 1: A Function to connect HERON/HYRBID capacities in the user-input file

def map_capacities_dispatches_in_the_user_input_file(user_input_file):
def map_capacities_in_the_user_input_file(user_input_file):
"""
Identifies which component dispatch/capacity in HYBRID corresponds to which component dispatch/capacity in HERON.
@ In, user-input file, str, the user-input text file
@ Out, a dictionary that matches HERON and HYBRID capacities, dict, Defining the HYBRID capacity that corresponds to each HERON capacity
@ Out, a dictionary that matches HERON and HYBRID dispatches, dict, Defining the HYBRID dispatch that corresponds to each HERON dispatch
Identifies which component capacity in HYBRID corresponds to which component capacity in HERON.
@ In, user_input_file, str, the user-input text file
@ Out, comp_capacity, dict, a dictionary that matches HERON and HYBRID components capacities
"""
# Iterating over the user-input file
with open(user_input_file, "r") as f:
comp_capacity = {}
heron_hybrid_dispatch = {}

for line in f:
if line.startswith("all_variables_file"):
all_var_hybrid_file = line.split("=")[1].strip().replace('"', "")

if not line.startswith("#"):
# Connect HYBRID/HREON capacities
if "capacity" in line:
Expand All @@ -53,30 +47,53 @@ def map_capacities_dispatches_in_the_user_input_file(user_input_file):
)
hybrid_capacity = line.strip().split("=")[1].strip()
comp_capacity[comp_name] = hybrid_capacity
with open(all_var_hybrid_file) as hyb:
if not hybrid_capacity in hyb.read():
print(
f"{Fore.YELLOW}Warning{Style.RESET_ALL}: '{hybrid_capacity}' is not found in {all_var_hybrid_file}"
)
if os.path.exists(all_var_hybrid_file): # checkin whether HYBRID variables file exists
with open(all_var_hybrid_file) as hyb:
if not hybrid_capacity in hyb.read():
print('\033[91m', f" Warning: {hybrid_capacity} is not found in {all_var_hybrid_file}", '\033[0m')
else:
print ('\033[91m', f" Warning: {all_var_hybrid_file} is not found", '\033[0m')
return (comp_capacity)

#####
# Section 2: A Function to connect HERON/HYRBID dispatches in the user-input file

def map_dispatches_in_the_user_input_file(user_input_file):
"""
Identifies which component dispatch in HYBRID corresponds to which component dispatch in HERON.
@ In, user_input_file, str, the user-input text file
@ Out, heron_hybrid_dispatch, dict, a dictionary that matches HERON and HYBRID dispatches
"""
# Iterating over the user-input file
with open(user_input_file, "r") as f:
heron_hybrid_dispatch = {}
for line in f:
if line.startswith("all_variables_file"):
all_var_hybrid_file = line.split("=")[1].strip().replace('"', "")

# Connect HYBRID/HREON dispatches
if not line.startswith("#"):
# Connect HYBRID/HREON dispatches
if "Dispatch" in line:
heron_dispatch = line.strip().split("=")[0].strip()
hybrid_dispatch = line.strip().split("=")[1].strip()
heron_hybrid_dispatch[heron_dispatch] = hybrid_dispatch
if os.path.exists(all_var_hybrid_file): # checkin whether HYBRID variables file exists
with open(all_var_hybrid_file) as hyb:
if not hybrid_dispatch in hyb.read():
print('\033[91m', f" Warning: {hybrid_dispatch} is not found in {all_var_hybrid_file}",'\033[0m')
else:
print ('\033[91m', f" Warning: {all_var_hybrid_file} is not found", '\033[0m')

return (comp_capacity, heron_hybrid_dispatch)

return (heron_hybrid_dispatch)

#####
# Section 2: A Function to obtain the values of the components'c capacities from the HERON input file

# Section 3: A Function to obtain the values of the components'c capacities from the HERON input file

def get_capacities_from_heron(heron_input_file):
"""
Extracts the values of the components' capacities from the HERON input XML file
@ In, HERON input, str, the HERON input XML file
@ Out, components' capacities, dict, the value of the capacity of each component
Extracts the values of the components' capacities from the HERON input XML file
@ In, heron_input_file, str, the HERON input XML file
@ Out, comp_capacites_values, dict, the values of the components' capacities
"""

# The HERON XML file tree
Expand Down Expand Up @@ -113,13 +130,13 @@ def get_capacities_from_heron(heron_input_file):


#####
# Section 3: A Function to get the most interesting dataset from the HERON optimized dispatch
# Section 4: A Function to get the most interesting dataset from the HERON optimized dispatch

def get_interesting_dispatch(dispatch_print_csv):
"""
Extracts the most interesting dataset from the a CSV file that we get from HERON. This CSV file includes optimized dispatchs that are calaculated at different years and samples.
@ In, dispatch_print, str, a csv file produced by HERON and includes optimized components' time-dependent variables at different years, different samples
@ Out, interesting dataset, pandas.core.frame.DataFrame, time-dependent optimized variables
Extracts the most interesting dataset from the a CSV file that we get from HERON. This CSV file includes optimized dispatchs that are calaculated at different years and samples.
@ In, dispatch_print_csv, str, a csv file produced by HERON and includes optimized components' time-dependent variables at different years, different samples
@ Out, interesting_dataset, pandas.core.frame.DataFrame, time-dependent optimized variables
"""

# Reading the input csv file from HERON
Expand Down Expand Up @@ -177,7 +194,8 @@ def get_interesting_dispatch(dispatch_print_csv):


#####
# Section 4: Specifying terminal command arguments
# Section 5: Specifying terminal command arguments

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert the HERON optimized components' variables (optimized dispatch) to a text file that is compatible with HYBRID"
Expand All @@ -190,12 +208,13 @@ def get_interesting_dispatch(dispatch_print_csv):
args = parser.parse_args()

#####
# Section 5: Creating a text file to be used by the HYBRID user
# Section 6: Creating a text file to be used by the HYBRID user

# Check whether the user-input file exists
file_exists = os.path.exists("user_input.txt")
if file_exists:
print("The user_input.txt file is found")
print("\n",
"The user_input.txt file is found")
else:
print("The user_input.txt file is not found")

Expand All @@ -206,11 +225,11 @@ def get_interesting_dispatch(dispatch_print_csv):

# Get capacities and dispatches
hybrid_heron_capacities = (
map_capacities_dispatches_in_the_user_input_file("user_input.txt")
)[0]
map_capacities_in_the_user_input_file("user_input.txt")
)
hybrid_heron_dispatches = (
map_capacities_dispatches_in_the_user_input_file("user_input.txt")
)[-1]
map_dispatches_in_the_user_input_file("user_input.txt")
)

with open(output_file, "a+") as f:
f.write("# A list of components' capacities" + "\n")
Expand Down
Loading