Skip to content
Snippets Groups Projects
Commit 26352465 authored by Christoph von Oy's avatar Christoph von Oy
Browse files

Merge branch 'dev_cvo_refactoring' into 'main'

Refactoring part 2

See merge request ineed-dc/ineed-dc-framework!16
parents 13e82826 b3e8cbf3
No related branches found
No related tags found
No related merge requests found
Showing
with 322 additions and 33 deletions
Subproject commit bcb9d840108bdc819b5f9f78c82acedfaf32bcc0 Subproject commit 314ea7f87c7232da21e67cda2e852a182e3fe930
Subproject commit 3297d3d3bc34991d70c52a7ec72bc93424226b84 Subproject commit 0c0e53de7c208ec319d660072c45e3e206f801b5
"""
This script inspects the input files and modifies them such that they adhere to the new input file specification used by the framework after merge of merge request !16 Refactoring Part 2.
"""
import os.path
import pandas as pd
import math
def read_matrix(df):
matrix = []
for i in df.index:
matrix_row = []
for j in df.index:
matrix_row.append(df[df["name"][j]][i])
matrix.append(matrix_row)
return matrix
def read_components(df):
components = {}
for i in df.index:
components[df["name"][i]] = i
return components
def compare_components(comp_1, comp_2):
different = False
for column in ['name', 'type', 'model', 'min_size', 'max_size', 'current_size']:
if comp_1[column] != comp_2[column]:
if not math.isnan(comp_1[column]) and not math.isnan(comp_2[column]):
different = True
break
return different
def get_connected_components(matrix, components, comp):
connected_components = []
for component in components:
if get_connection(matrix, components, comp, component) == 1:
connected_components.append(component)
return connected_components
def get_connection(matrix, components, comp_from, comp_to):
return matrix[components[comp_from]][components[comp_to]]
def get_bypasses(component_name, connections):
inputs = []
outputs = []
sector = ''
for i in connections.index:
if connections['to'][i] == component_name and connections['from'][i] not in inputs:
inputs.append(connections['from'][i])
if sector == '':
sector = connections['sector'][i]
else:
if sector != connections['sector'][i]:
raise KeyError
if connections['from'][i] == component_name and connections['to'][i] not in outputs:
outputs.append(connections['to'][i])
if sector == '':
sector = connections['sector'][i]
else:
if sector != connections['sector'][i]:
raise KeyError
bypasses = []
for input in inputs:
for output in outputs:
if input != output:
if not ((connections['sector'] == sector) & (connections['from'] == input) & (connections['to'] == output)).any():
bypasses.append((sector, input, output))
return bypasses
def read_config(config_path):
config_df = pd.read_csv(config_path)
config_dict = config_df.to_dict(orient='list')
for k in config_dict:
config_dict[k] = config_dict[k][0]
return config_dict
changed_topologies = []
invalid_topologies = []
renamed_components = {'StandardElectricalConsumption': 'ElectricalConsumption',
'StandardACGrid': 'ElectricalGrid',
'StandardPVGenerator': 'PVGenerator',
'StandardGasGrid': 'GasGrid',
'StandardPEMElectrolyzer': 'PEMElectrolyzer',
'StandardPEMFuelCell': 'PEMFuelCell',
'BasicInverter': 'StaticInverter',
'Inverter': 'DynamicInverter'}
consumption_components = ['CoolConsumption', 'ChargingInfrastructure', 'ElectricalConsumption', 'HeatConsumption', 'Radiator', 'HotWaterConsumption']
storage_components = ['LiionBattery', 'HotWaterStorage', 'PressureStorage']
for dirpath, dirnames, filenames in os.walk(".\\input_files"):
topology_here = False
matrix_files = []
for filename in filenames:
if filename.find('matrix') and filename.endswith('.csv') and (filename.find('elec') is not -1):
topology_here = True
matrix_files.append((filename, 'electricity'))
if filename.find('matrix') and filename.endswith('.csv') and (filename.find('gas') is not -1):
topology_here = True
matrix_files.append((filename, 'gas'))
if filename.find('matrix') and filename.endswith('.csv') and (filename.find('hydro') is not -1):
topology_here = True
matrix_files.append((filename, 'hydrogen'))
if filename.find('matrix') and filename.endswith('.csv') and (filename.find('therm') is not -1):
topology_here = True
matrix_files.append((filename, 'heat'))
if topology_here:
try:
print(f"Inspecting topology {dirpath}")
file_contents = []
for matrix_file, sector in matrix_files:
df = pd.read_csv(os.path.join(dirpath, matrix_file))
df.rename(columns = {'comp_name': 'name', 'comp_type': 'type'}, inplace = True)
for i in df.index:
if df['type'][i] in renamed_components:
df.loc[i, 'type'] = renamed_components[df['type'][i]]
if df['type'][i] in consumption_components:
df.loc[i, 'model'] = ''
df.loc[i, 'min_size'] = ''
df.loc[i, 'max_size'] = ''
df.loc[i, 'current_size'] = ''
file_contents.append((df, read_matrix(df), read_components(df), sector))
all_components = pd.DataFrame(columns = ['name', 'type', 'model', 'min_size', 'max_size', 'current_size'])
all_connections = pd.DataFrame(columns = ['sector', 'from', 'to'])
for df, matrix, components, sector in file_contents:
for component in components:
if component not in all_components.loc[:]['name']:
all_components.loc[component] = df.loc[components[component]][['name', 'type', 'model', 'min_size', 'max_size', 'current_size']]
else:
if compare_components(all_components.loc[component], df.loc[components[component]][['name', 'type', 'model', 'min_size', 'max_size', 'current_size']]):
raise KeyError
for connected_component in get_connected_components(matrix, components, component):
all_connections = pd.concat([all_connections, pd.Series({'sector': sector, 'from': component, 'to': connected_component}).to_frame().T], ignore_index = True)
while True:
number_of_connections_before = len(all_connections)
for i in all_components.index:
if all_components['type'][i] in storage_components:
for sector, from_component, to_component in get_bypasses(all_components['name'][i], all_connections):
all_connections = pd.concat([all_connections, pd.Series({'sector': sector, 'from': from_component, 'to': to_component}).to_frame().T], ignore_index = True)
number_of_connections_after = len(all_connections)
if number_of_connections_before == number_of_connections_after:
break
config = read_config(os.path.join(dirpath, "config.csv"))
changed_topologies.append(dirpath)
new_config = dict()
for name, value in config.items():
# configs to delete
if name in ['injection/pvpeak', 'elec_price_cap_low', 'elec_price_cap_high']:
pass
# configs to keep
elif name in ['yearly_interest', 'planning_horizon']:
new_config[name] = value
# configs that are handled by other configs
elif name in ['elec_price_variable', 'injection_price_variable', 'gas_price_variable', 'injection_price_gas_variable', 'heat_price_variable', 'injection_price_heat_variable', 'cooling_price_variable', 'injection_price_cooling_variable']:
pass
elif name == 'elec_price':
if config['elec_price_variable'] == 1:
print(f"Config of topology {dirpath} has a variable electricity price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'ElectricalGrid':
new_config[all_components['name'][i] + '_price'] = value
elif name == 'injection_price':
if config['injection_price_variable'] == 1:
print(f"Config of topology {dirpath} has a variable electricity injection price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'ElectricalGrid':
new_config[all_components['name'][i] + '_injection_price'] = value
elif name == 'gas_price':
if config['gas_price_variable'] == 1:
print(f"Config of topology {dirpath} has a variable gas price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'GasGrid':
new_config[all_components['name'][i] + '_price'] = value
elif name == 'injection_price_gas':
if config['injection_price_gas_variable'] == 1:
print(f"Config of topology {dirpath} has a variable gas injection price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'GasGrid':
new_config[all_components['name'][i] + '_injection_price'] = value
elif name == 'heat_price':
if config['heat_price_variable'] == 1:
print(f"Config of topology {dirpath} has a variable heat price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'HeatGrid':
new_config[all_components['name'][i] + '_price'] = value
elif name == 'injection_price_heat':
if config['injection_price_heat_variable'] == 1:
print(f"Config of topology {dirpath} has a variable heat injection price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'HeatGrid':
new_config[all_components['name'][i] + '_injection_price'] = value
elif name == 'cooling_price':
if config['cooling_price_variable'] == 1:
print(f"Config of topology {dirpath} has a variable cooling price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'CoolingGrid':
new_config[all_components['name'][i] + '_price'] = value
elif name == 'injection_price_cooling':
if config['injection_price_cooling_variable'] == 1:
print(f"Config of topology {dirpath} has a variable cooling injection price, so be sure to add a price profile!")
else:
for i in all_components.index:
if all_components['type'][i] == 'CoolingGrid':
new_config[all_components['name'][i] + '_injection_price'] = value
elif name == 'elec_emission':
for i in all_components.index:
if all_components['type'][i] == 'ElectricalGrid':
new_config[all_components['name'][i] + '_emission'] = value
elif name == 'gas_emission':
for i in all_components.index:
if all_components['type'][i] == 'GasGrid':
new_config[all_components['name'][i] + '_emission'] = value
else:
new_config[name] = value
all_components.to_csv(os.path.join(dirpath, "temp.csv"), index = False)
df_str = pd.read_csv(os.path.join(dirpath, "temp.csv"), dtype = str, keep_default_na = False)
for i in all_components.columns:
counter = 0
for j in all_components.index:
if issubclass(type(all_components[i][j]), float):
if len(df_str[i][counter]) != 0 and df_str[i][counter].endswith(".0"):
df_str[i][counter] = f"{all_components[i][j]:.0f}"
counter += 1
df_str.to_csv(os.path.join(dirpath, "components.csv"), index = False)
os.remove(os.path.join(dirpath, "temp.csv"))
all_connections.to_csv(os.path.join(dirpath, "connections.csv"), index = False)
for matrix_file, sector in matrix_files:
os.remove(os.path.join(dirpath, matrix_file))
os.remove(os.path.join(dirpath, "config.csv"))
new_config_df = pd.DataFrame(columns = list(new_config.keys()), index = [0])
for key, value in new_config.items():
string = str(value)
if issubclass(type(value), float):
if len(string) != 0 and string.endswith(".0"):
string = f"{value:.0f}"
new_config_df.loc[0, key] = string
new_config_df.to_csv(os.path.join(dirpath, "config.csv"), index = False)
except:
invalid_topologies.append(dirpath)
for directory in changed_topologies:
print(f"Modified topology {directory}!")
for file in invalid_topologies:
print(f"Topology {file} breaks some part of the input file specification!")
name,type,model,min_size,max_size,current_size
inv_pv_bat,DynamicInverter,STP-7000TL-20,0,20,0
battery,LiionBattery,BAT1,0,6,6
grd,ElectricalGrid,GRD1,1000,1000,1000
elec_cns,ElectricalConsumption,,,,
injection_price,injection_price_variable,injection/pvpeak,gas_price,gas_price_variable,elec_price,elec_price_variable,heat_price,heat_price_variable,cooling_price,cooling_price_variable,injection_price_gas,injection_price_gas_variable,injection_price_heat,injection_price_heat_variable,injection_price_cooling,injection_price_cooling_variable,elec_emission,gas_emission,yearly_interest,planning_horizon grd_injection_price,grd_price,grd_emission,yearly_interest,planning_horizon
0.0793,0,0.7,0.0606,0,0.3046,0,0,0,0,0,0,0,0,0,0,0,0.401,0.21,0.03,20 0.0793,0.3046,0.401,0.03,20
sector,from,to
electricity,inv_pv_bat,battery
electricity,inv_pv_bat,grd
electricity,inv_pv_bat,elec_cns
electricity,battery,inv_pv_bat
electricity,grd,inv_pv_bat
electricity,grd,elec_cns
comp_name,comp_type,model,min_size,max_size,current_size,inv_pv_bat,battery,grd,elec_cns
inv_pv_bat,Inverter,STP-7000TL-20,0,20,0,0,1,1,1
battery,LiionBattery,BAT1,0,6,6,1,0,0,0
grd,StandardACGrid,GRD1,1000,1000,1000,1,0,0,1
elec_cns,StandardElectricalConsumption,CNS1,1000,1000,0,0,0,0,0
name,type,model,min_size,max_size,current_size
pv_roof,PVGenerator,PV2,6,6,6
inv_pv_bat,DynamicInverter,STP-7000TL-20,0,20,0
battery,LiionBattery,BAT1,6,6,6
grd,ElectricalGrid,GRD1,1000,1000,1000
elec_cns,ElectricalConsumption,,,,
injection_price,injection_price_variable,injection/pvpeak,gas_price,gas_price_variable,elec_price,elec_price_variable,network_usage_capacity_fee,network_usage_energy_fee,heat_price,levies_int,levies_ext,concession,electricity_tax_int,electricity_tax_ext,VAT,heat_price_variable,cooling_price,cooling_price_variable,injection_price_gas,injection_price_gas_variable,injection_price_heat,injection_price_heat_variable,injection_price_cooling,injection_price_cooling_variable,elec_emission,gas_emission,yearly_interest,planning_horizon grd_injection_price,grd_price,network_usage_capacity_fee,network_usage_energy_fee,levies_int,levies_ext,concession,electricity_tax_int,electricity_tax_ext,VAT,grd_emission,yearly_interest,planning_horizon
0.0793,0,0.7,0.0606,0,0.3046,0,14.79,0.0506,0,0.0276,0.0496,0.0199,0,0.0205,0.190,0,0,0,0,0,0,0,0,0,0.401,0.21,0.03,20 0.0793,0.3046,14.79,0.0506,0.0276,0.0496,0.0199,0,0.0205,0.19,0.401,0.03,20
sector,from,to
electricity,pv_roof,inv_pv_bat
electricity,inv_pv_bat,battery
electricity,inv_pv_bat,grd
electricity,inv_pv_bat,elec_cns
electricity,battery,inv_pv_bat
electricity,grd,inv_pv_bat
electricity,grd,elec_cns
comp_name,comp_type,model,min_size,max_size,current_size,pv_roof,inv_pv_bat,battery,grd,elec_cns
pv_roof,StandardPVGenerator,PV2,6,6,6,0,1,0,0,0
inv_pv_bat,Inverter,STP-7000TL-20,0,20,0,0,0,1,1,1
battery,LiionBattery,BAT1,6,6,6,0,1,0,0,0
grd,StandardACGrid,GRD1,1000,1000,1000,0,1,0,0,1
elec_cns,StandardElectricalConsumption,CNS1,1000,1000,0,0,0,0,0,0
name,type,model,min_size,max_size,current_size
pv_roof,PVGenerator,PV2,6,6,6
inv_pv_bat,DynamicInverter,STP-7000TL-20,0,20,0
battery,LiionBattery,BAT1,6,6,6
grd,ElectricalGrid,GRD1,1000,1000,1000
elec_cns,ElectricalConsumption,,,,
injection_price,injection_price_variable,injection/pvpeak,gas_price,gas_price_variable,elec_price,elec_price_variable,heat_price,heat_price_variable,cooling_price,cooling_price_variable,injection_price_gas,injection_price_gas_variable,injection_price_heat,injection_price_heat_variable,injection_price_cooling,injection_price_cooling_variable,elec_emission,gas_emission,yearly_interest,planning_horizon grd_injection_price,grd_price,grd_emission,yearly_interest,planning_horizon
0.0793,0,0.7,0.0606,0,0.3046,0,0,0,0,0,0,0,0,0,0,0,0.401,0.21,0.03,20 0.0793,0.3046,0.401,0.03,20
sector,from,to
electricity,pv_roof,inv_pv_bat
electricity,inv_pv_bat,battery
electricity,inv_pv_bat,grd
electricity,inv_pv_bat,elec_cns
electricity,battery,inv_pv_bat
electricity,grd,inv_pv_bat
electricity,grd,elec_cns
comp_name,comp_type,model,min_size,max_size,current_size,pv_roof,inv_pv_bat,battery,grd,elec_cns
pv_roof,StandardPVGenerator,PV2,6,6,6,0,1,0,0,0
inv_pv_bat,Inverter,STP-7000TL-20,0,20,0,0,0,1,1,1
battery,LiionBattery,BAT1,6,6,6,0,1,0,0,0
grd,StandardACGrid,GRD1,1000,1000,1000,0,1,0,0,1
elec_cns,StandardElectricalConsumption,CNS1,1000,1000,0,0,0,0,0,0
name,type,model,min_size,max_size,current_size
pv_roof,PVGenerator,PV2,6,6,6
inv_pv_bat,DynamicInverter,STP-7000TL-20,0,20,0
battery,LiionBattery,BAT1,6,6,6
grd,ElectricalGrid,GRD1,1000,1000,1000
elec_cns,ElectricalConsumption,,,,
injection_price,injection_price_variable,injection/pvpeak,gas_price,gas_price_variable,elec_price,elec_price_variable,heat_price,heat_price_variable,cooling_price,cooling_price_variable,injection_price_gas,injection_price_gas_variable,injection_price_heat,injection_price_heat_variable,injection_price_cooling,injection_price_cooling_variable,elec_emission,gas_emission,yearly_interest,planning_horizon grd_injection_price,grd_price,grd_emission,yearly_interest,planning_horizon
0.0793,0,0.7,0.0606,0,0.3046,0,0,0,0,0,0,0,0,0,0,0,0.401,0.21,0.03,20 0.0793,0.3046,0.401,0.03,20
sector,from,to
electricity,pv_roof,inv_pv_bat
electricity,inv_pv_bat,battery
electricity,inv_pv_bat,grd
electricity,inv_pv_bat,elec_cns
electricity,battery,inv_pv_bat
electricity,grd,inv_pv_bat
electricity,grd,elec_cns
comp_name,comp_type,model,min_size,max_size,current_size,pv_roof,inv_pv_bat,battery,grd,elec_cns
pv_roof,StandardPVGenerator,PV2,6,6,6,0,1,0,0,0
inv_pv_bat,Inverter,STP-7000TL-20,0,20,0,0,0,1,1,1
battery,LiionBattery,BAT1,6,6,6,0,1,0,0,0
grd,StandardACGrid,GRD1,1000,1000,1000,0,1,0,0,1
elec_cns,StandardElectricalConsumption,CNS1,1000,1000,0,0,0,0,0,0
name,type,model,min_size,max_size,current_size
grd,ElectricalGrid,GRD1,100000,100000,0
elec_cns,ElectricalConsumption,,,,
gas_boi,GasBoiler,BOI1,10,10,0
gas_grd,GasGrid,GAS1,100000,100000,0
therm_cns,HeatConsumption,,,,
dhw_dmd,HotWaterConsumption,,,,
water_tes,HotWaterStorage,TES1,40,40,0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment