Skip to content

Commit

Permalink
Refactors to use only network for the fixes (idea from CSA)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr0321 committed Dec 25, 2024
1 parent 4a2bb64 commit b3d5fca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
58 changes: 22 additions & 36 deletions emf/loadflow_tool/model_merger/model_merger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import math

import pypowsybl

import config
import json
import time
Expand Down Expand Up @@ -109,6 +112,8 @@ def handle(self, task_object: dict, **kwargs):
pre_temp_fixes = task_properties['pre_temp_fixes']
post_temp_fixes = task_properties['post_temp_fixes']

remove_non_generators_from_slack_participation = True

# Collect valid models from ObjectStorage
downloaded_models = get_latest_models_and_download(time_horizon, scenario_datetime, valid=False)
latest_boundary = get_latest_boundary()
Expand Down Expand Up @@ -207,29 +212,8 @@ def handle(self, task_object: dict, **kwargs):

# Run pre-processing
pre_p_start = datetime.datetime.now(datetime.UTC)

assembled_data = None
to_remove = None
not_condenser = None
remove_non_generators_from_slack_participation = True
if remove_non_generators_from_slack_participation:
try:
assembled_data = load_opdm_data(input_models)
sync_machines = assembled_data.type_tableview('SynchronousMachine').reset_index()
to_remove = sync_machines[~sync_machines['SynchronousMachine.operatingMode'].str.contains('.generator')][
['ID']]
condensers = sync_machines[sync_machines['SynchronousMachine.operatingMode'].str.contains('.condenser')]
not_condenser = condensers[abs(condensers['RotatingMachine.p']) > 0]
except AttributeError as attr:
logger.error(f'Unable to find column for igm-cgm-ssh error: {ex}')
except Exception as ex:
logger.error(f'Unknown igm-cgm-ssh error: {ex}')

if pre_temp_fixes:
if assembled_data is not None:
input_models = run_pre_merge_processing(assembled_data, merging_area)
else:
input_models = run_pre_merge_processing(input_models, merging_area)
input_models = run_pre_merge_processing(input_models, merging_area)
pre_p_end = datetime.datetime.now(datetime.UTC)
logger.debug(f"Pre-processing took: {(pre_p_end - pre_p_start).total_seconds()} seconds")

Expand All @@ -238,23 +222,25 @@ def handle(self, task_object: dict, **kwargs):
merged_model = merge_functions.load_model(input_models)
# TODO - run other LF if default fails

# Remove elements from participating:
# Various fixes from igmsshvscgmssh error
if remove_non_generators_from_slack_participation:
network_pre_instance = merged_model["network"]
# re-initialize condensers with p-value not zero
try:
generators = network_pre_instance.get_generators()
if not_condenser is not None:
condensers_to_initialize = generators.reset_index().merge(not_condenser[['ID']]
.rename(columns={'ID': 'id'}), on='id')
condensers_to_initialize['target_p'] = condensers_to_initialize['p'] * (-1)
condensers_to_initialize = condensers_to_initialize[['id','target_p']].set_index('id')
network_pre_instance.update_generators(condensers_to_initialize)
# remove elements from participating
if to_remove is not None:
not_generators = (generators.reset_index().merge(to_remove[['ID']]
.rename(columns={'ID': 'id'}), on='id')
.set_index('id'))
generators = network_pre_instance.get_elements(element_type=pypowsybl.network.ElementType.GENERATOR,
all_attributes=True)
not_generators = generators[~(generators['CGMES.synchronousMachineOperatingMode']
.str.contains('generator'))]
condensers = generators[(generators['CGMES.synchronousMachineType'].str.contains('condenser'))
& (abs(generators['p']) > 0)
& (abs(generators['target_p']) == 0)]
# Fix condensers that have p not zero by setting their target_p to equal to p
if not condensers.empty:
logger.warning(f"Found {len(condensers.index)} condensers for which p ~= 0 & target_p = 0")
condensers.loc[:, 'target_p'] = condensers['p'] * (-1)
network_pre_instance.update_generators(condensers[['target_p']])
# Remove all not generators from active power distribution
if not not_generators.empty:
logger.warning(f"Removing {len(not_generators.index)} machines from power distribution")
extensions = network_pre_instance.get_extensions('activePowerControl')
remove_not_generators = extensions.merge(not_generators.reset_index()[['id']],
left_index=True, right_on='id')
Expand Down
6 changes: 1 addition & 5 deletions emf/loadflow_tool/model_merger/temporary_fixes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pandas
import triplets
from emf.loadflow_tool.helper import create_opdm_objects
from emf.loadflow_tool.model_merger.merge_functions import (load_opdm_data, create_sv_and_updated_ssh, fix_sv_shunts,
Expand All @@ -18,10 +17,7 @@ def run_pre_merge_processing(input_models, merging_area):
if merging_area == 'BA':
input_models = set_brell_lines_to_zero_in_models(input_models)

if not isinstance(input_models, pandas.DataFrame):
assembled_data = load_opdm_data(input_models)
else:
assembled_data = input_models
assembled_data = load_opdm_data(input_models)

# TODO try to optimize it better
# if merging_area == 'BA':
Expand Down

0 comments on commit b3d5fca

Please sign in to comment.