Skip to content

Commit

Permalink
Added duplicated idtag protection
Browse files Browse the repository at this point in the history
  • Loading branch information
SanPen committed Sep 19, 2024
1 parent 88b29a1 commit 7b84a72
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 197 deletions.
316 changes: 151 additions & 165 deletions .idea/workspace.xml

Large diffs are not rendered by default.

61 changes: 39 additions & 22 deletions src/GridCal/Gui/Main/SubClasses/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import GridCal.Gui.gui_functions as gf
import GridCal.Session.export_results_driver as exprtdrv
import GridCal.Session.file_handler as filedrv
from GridCalEngine.Devices.multi_circuit import MultiCircuit

from GridCal.Gui.CoordinatesInput.coordinates_dialogue import CoordinatesInputGUI
from GridCal.Gui.general_dialogues import LogsDialogue, CustomQuestionDialogue
from GridCal.Gui.Diagrams.SchematicWidget.schematic_widget import SchematicWidget
Expand All @@ -32,10 +32,12 @@
from GridCal.Gui.RosetaExplorer.RosetaExplorer import RosetaExplorerGUI
from GridCal.Gui.Main.SubClasses.Settings.configuration import ConfigurationMain

from GridCalEngine.Devices.multi_circuit import MultiCircuit
from GridCalEngine.Compilers.circuit_to_newton_pa import NEWTON_PA_AVAILABLE
from GridCalEngine.Compilers.circuit_to_pgm import PGM_AVAILABLE
from GridCalEngine.DataStructures.numerical_circuit import compile_numerical_circuit_at
from GridCalEngine.enumerations import CGMESVersions, SimulationTypes
from GridCalEngine.basic_structures import Logger
from GridCalEngine.IO.gridcal.contingency_parser import import_contingencies_from_json, export_contingencies_json_file
from GridCalEngine.IO.cim.cgmes.cgmes_enums import cgmesProfile
from GridCalEngine.IO.gridcal.remote import RemoteInstruction
Expand Down Expand Up @@ -521,9 +523,17 @@ def export_circuit_differential(self):
"""
Prompt to add another circuit
"""
self.open_file_threaded(post_function=self.post_create_circuit_differential,
allow_diff_file_format=True,
title="Load base grid to compare...")
# check that this circuit is ok
logger = Logger()
_, ok = self.circuit.get_all_elements_dict(logger=logger)

if ok:
self.open_file_threaded(post_function=self.post_create_circuit_differential,
allow_diff_file_format=True,
title="Load base grid to compare...")
else:
dlg = LogsDialogue('This circuit has duplicated idtags :(', logger)
dlg.exec_()

def post_create_circuit_differential(self):
"""
Expand All @@ -548,30 +558,37 @@ def post_create_circuit_differential(self):
# diff the circuit
new_circuit = self.open_file_thread_object.circuit

# create the differential
ok, diff_logger, dgrid = self.circuit.differentiate_circuits(new_circuit)
dict_logger = Logger()
_, dict_ok = new_circuit.get_all_elements_dict(logger=dict_logger)

if diff_logger.has_logs():
dlg = LogsDialogue('Grid differences', diff_logger)
dlg.exec_()
if dict_ok:
# create the differential
ok, diff_logger, dgrid = self.circuit.differentiate_circuits(new_circuit)

# select the file to save
filename, type_selected = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file',
dgrid.name,
"GridCal diff (*.dgridcal)")
if diff_logger.has_logs():
dlg = LogsDialogue('Grid differences', diff_logger)
dlg.exec_()

if filename != '':
# select the file to save
filename, type_selected = QtWidgets.QFileDialog.getSaveFileName(self, 'Save file',
dgrid.name,
"GridCal diff (*.dgridcal)")

# if the user did not enter the extension, add it automatically
name, file_extension = os.path.splitext(filename)
if filename != '':

if file_extension == '':
filename = name + ".dgridcal"
# if the user did not enter the extension, add it automatically
name, file_extension = os.path.splitext(filename)

# we were able to compose the file correctly, now save it
self.save_file_now(filename=filename,
type_selected=type_selected,
grid=dgrid)
if file_extension == '':
filename = name + ".dgridcal"

# we were able to compose the file correctly, now save it
self.save_file_now(filename=filename,
type_selected=type_selected,
grid=dgrid)
else:
dlg = LogsDialogue('The base circuit has duplicated idtags :(', dict_logger)
dlg.exec()

def save_file_as(self):
"""
Expand Down
22 changes: 18 additions & 4 deletions src/GridCalEngine/Devices/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,7 @@ def get_contingency_groups_in(self,
group2index = {g: i for i, g in enumerate(self._contingency_groups)}

# get a dictionary of all objects
all_devices = self.get_all_elements_dict()
all_devices, dict_ok = self.get_all_elements_dict()

# get the buses that match the filtering
buses = self.get_buses_by(filter_elements=grouping_elements)
Expand Down Expand Up @@ -5265,19 +5265,33 @@ def add_or_replace_object(self, api_obj: ALL_DEV_TYPES, logger: Logger) -> bool:

return found

def get_all_elements_dict(self) -> dict[str, ALL_DEV_TYPES]:
def get_all_elements_dict(self, logger = Logger()) -> Tuple[Dict[str, ALL_DEV_TYPES], bool]:
"""
Get a dictionary of all elements
:param: logger: Logger
:return: Dict[idtag] -> object
"""
data = dict()
ok = True
for key, tpe in self.device_type_name_dict.items():
elements = self.get_elements_by_type(device_type=tpe)

for elm in elements:
data[elm.idtag] = elm

return data
e = data.get(elm.idtag, None)
if e is None:
data[elm.idtag] = elm
else:
logger.add_error(
msg="Duplicated idtag!",
device=elm.name,
device_class=elm.device_type.value,
device_property="idtag",
expected_value=f"{e.device_type.value}:{e.idtag}:{e.name}"
)
ok = False

return data, ok

def get_all_elements_dict_by_type(self,
add_locations: bool = False,
Expand Down
6 changes: 3 additions & 3 deletions src/GridCalEngine/Devices/multi_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ def set_investments_status(self,
"""

if all_elements_dict is None:
all_elements_dict = self.get_all_elements_dict()
all_elements_dict, dict_ok = self.get_all_elements_dict()

for inv in investments_list:
device_idtag = inv.device_idtag
Expand Down Expand Up @@ -1916,7 +1916,7 @@ def differentiate_circuits(self, base_grid: "MultiCircuit",
expected_value=self.get_snapshot_time_unix)

# get a dictionary of all the elements of the other circuit
base_elements_dict = base_grid.get_all_elements_dict()
base_elements_dict, dict_ok = base_grid.get_all_elements_dict()

for elm_from_here in self.items(): # for every device...
action = ActionType.NoAction
Expand Down Expand Up @@ -2244,7 +2244,7 @@ def clean(self) -> Logger:
logger = Logger()
bus_set = set(self._buses)
cn_set = set(self._connectivity_nodes)
all_dev = self.get_all_elements_dict()
all_dev, dict_ok = self.get_all_elements_dict()
nt = self.get_time_number()

self.clean_branches(nt=nt, bus_set=bus_set, cn_set=cn_set, logger=logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, grid: MultiCircuit, obj_func, n_obj):

self.grid = grid

all_dict = self.grid.get_all_elements_dict()
all_dict, dict_ok = self.grid.get_all_elements_dict()
self.device_template_dict: Dict[BRANCH_TYPES, List[BRANCH_TEMPLATE_TYPES]] = dict()

# create the decision vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def __init__(self,
self.max_iter = options.max_eval

# gather a dictionary of all the elements, this serves for the investments generation
self.get_all_elements_dict = self.grid.get_all_elements_dict()
self.get_all_elements_dict, dict_ok = self.grid.get_all_elements_dict()

# compose useful arrays
self.vm_cost = np.array([e.Vm_cost for e in grid.get_buses()], dtype=float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, grid: MultiCircuit,
self.nc: Union[NumericalCircuit, None] = None

# gather a dictionary of all the elements, this serves for the investments generation
self.get_all_elements_dict = self.grid.get_all_elements_dict()
self.get_all_elements_dict, dict_ok = self.grid.get_all_elements_dict()

def get_steps(self):
"""
Expand Down

0 comments on commit 7b84a72

Please sign in to comment.