Skip to content

Commit

Permalink
Updated connectivity based on grid, removed the cancellation of Slage…
Browse files Browse the repository at this point in the history
…lse-Nyborg line, since it makes the clustering algorithm not work which may explain weird connections, and added plotting of line capacities for clustered data
  • Loading branch information
Mathias157 committed Oct 15, 2024
1 parent 4771438 commit 3c3f46b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
53 changes: 46 additions & 7 deletions src/Modules/aggregate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
### 0. Script Settings ###
### ------------------------------- ###

import matplotlib.pyplot as plt
import os
import pandas as pd
import numpy as np
import geopandas as gpd
import click
from typing import Union
from pybalmorel import Balmorel, IncFile
from pybalmorel.utils import symbol_to_df
import gams
Expand Down Expand Up @@ -175,6 +177,10 @@ def aggregate_parameter(db: gams.GamsDatabase,
prefix=prefix, suffix=suffix)
f.body = df

# Plot connection costs in case it is a connection parameter
if 'INVCOST' in symbol:
plot_transmission_invcost(symbol, df)

# Use N-1 sets as index, and the last as columns, where N = length of columns without 'Value' column
if len(symbol_columns) > 2:
index = symbol_columns[:-2]
Expand Down Expand Up @@ -223,6 +229,35 @@ def aggregate_sets(db: gams.GamsDatabase,

f.save()

def plot_transmission_invcost(symbol: str,
df: pd.DataFrame,
year: int = 2050):
print('Plotting connection costs for %s'%symbol)

# f = gpd.read_file('ClusterOutput/%s_%dcluster_geofile.gpkg'%('-'.join(cluster_params_list), cluster_size))
geo_union = gpd.read_file('ClusterOutput/DE-DH-WNDFLH-SOLEFLH_30cluster_geofile.gpkg')
geo_union['coord'] = geo_union.centroid

fig, ax = plt.subplots()
geo_union.plot(ax=ax)

# lines = []
exclusion = []
for line, cap in df.loc[str(year)].iterrows():
if line in exclusion or line[0] == line[1]:
continue

coords = geo_union.query('cluster_name == @line[0] or cluster_name == @line[1]')['coord']
x1, x2 = coords.x
y1, y2 = coords.y

l, = ax.plot([x1,x2], [y1,y2], color = 'r', linewidth = cap['Value']*1e-5)
# lines.append(l)

exclusion.append((line[1], line[0]))
# print(df.drop)
plt.show()


#%% ------------------------------- ###
### 2. Main ###
Expand All @@ -231,13 +266,15 @@ def aggregate_sets(db: gams.GamsDatabase,
@click.command()
@click.option('--model-path', type=str, required=True, help='Balmorel model path')
@click.option('--scenario', type=str, required=True, help='Balmorel scenario')
@click.option('--exceptions', type=str, required=False, help='.inc files that should NOT be generated')
@click.option('--mean-aggfuncs', type=str, required=False, help='Parameters that should be aggregated with an average')
@click.option('--median-aggfuncs', type=str, required=False, help='Parameters that should be aggregated using the median value')
@click.option('--zero-fillnas', type=str, required=False, help='NaN values that should be converted to zero instead of EPS')
def main(model_path: str, scenario: str, exceptions: str = '',
mean_aggfuncs: str = '', median_aggfuncs: str = '',
zero_fillnas: str = '', incfile_folder: str = 'Output'):
@click.option('--exceptions', type=str, required=False, default='', help='.inc files that should NOT be generated')
@click.option('--mean-aggfuncs', type=str, required=False, default='', help='Parameters that should be aggregated with an average')
@click.option('--median-aggfuncs', type=str, required=False, default='', help='Parameters that should be aggregated using the median value')
@click.option('--zero-fillnas', type=str, required=False, default='', help='NaN values that should be converted to zero instead of EPS')
@click.option('--only-symbols', type=str, required=False, help="Only aggregate the symbols, input as comma-separated string")
def main(model_path: str, scenario: str, exceptions: str,
mean_aggfuncs: str, median_aggfuncs: str,
zero_fillnas: str, incfile_folder: str = 'Output',
only_symbols: Union[str, None] = None):

# Make configuration lists
exceptions = exceptions.replace(' ', '').split(',') # Symbols not to aggregate
Expand All @@ -258,6 +295,8 @@ def main(model_path: str, scenario: str, exceptions: str = '',

# Get which .inc-files and how to aggregate based on folder content and configurations
symbols, aggfuncs, fillnas = get_symbols_to_aggregate(incfile_folder, exceptions, mean_aggfuncs, median_aggfuncs, zero_fillnas)
if only_symbols != None:
symbols = only_symbols.replace(' ', '').split(',')

# Aggregating parameters and sets
print('Will attempt to aggregate..\n%s\n'%(','.join(symbols)))
Expand Down
6 changes: 3 additions & 3 deletions src/Modules/clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ def cluster(collected_data: pd.DataFrame,
use_connectivity: bool = True,
manual_corrections: list = [
['Roedovre', 'Frederiksberg', 1],
['Slagelse', 'Nyborg', 0],
],
linkage: str = 'Ward',
connection_remark: str = 'connec. included + artifical',
Expand All @@ -219,7 +218,7 @@ def cluster(collected_data: pd.DataFrame,
connectivity.connection.loc[manual_connection[0], manual_connection[1]] = manual_connection[2]
connectivity.connection.loc[manual_connection[1], manual_connection[0]] = manual_connection[2]


print('Is matrix symmetric?', np.all(connectivity.connection.data == connectivity.connection.data.T))
## Make symmetric index, so the indices fit
collected_data = collected_data.assign_coords(IRRRI=collected_data.coords['IRRRE'].data)

Expand All @@ -229,6 +228,7 @@ def cluster(collected_data: pd.DataFrame,

## Make symmetric connectivity graph
knn_graph = X.connection.data # get numpy array
print('Is matrix still symmetric?', np.all(knn_graph == knn_graph.T))
knn_graph = csr_matrix(knn_graph) # make dense format

## Drop the connection variable again
Expand Down Expand Up @@ -272,7 +272,7 @@ def cluster(collected_data: pd.DataFrame,
clustering.plot(ax=ax, column='cluster_group',
cmap=truncate_colormap(cmap, 0.2, 1))

if knn_graph == None:
if knn_graph is None:
connection_remark = 'no connectivity'

plot_title = '%s, %d clusters, %s\ndata: %s'%(linkage,
Expand Down

0 comments on commit 3c3f46b

Please sign in to comment.