From 721f939fa8a6f320cc69d7584cc46f7693927cec Mon Sep 17 00:00:00 2001 From: Thomas Bouquet Date: Tue, 26 Mar 2024 09:36:11 +0100 Subject: [PATCH 1/7] Bug with cnecName instead of instantId Signed-off-by: Thomas Bouquet --- .../craccreator/cnec/AbstractCnecCreator.java | 10 ++ .../craccreator/cnec/FlowCnecCreator.java | 9 +- .../craccreator/cnec/GeographicalFilter.java | 120 ++++++++++++++++++ 3 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java index 6c1a1d0b7d..2f8a37d056 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java @@ -2,6 +2,7 @@ import com.powsybl.openrao.data.cracapi.Contingency; import com.powsybl.openrao.data.cracapi.Crac; +import com.powsybl.openrao.data.cracapi.NetworkElement; import com.powsybl.openrao.data.cracapi.cnec.CnecAdder; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracCreationContext; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils; @@ -16,6 +17,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import static com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils.getTsoNameFromUrl; @@ -113,4 +115,12 @@ protected void markCnecAsImportedAndHandleRejectedContingencies(String instantId csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.imported(assessedElementId, cnecName, cnecName, "some cnec for the same assessed element are not imported because of incorrect data for assessed elements for contingencies : " + rejectedLinksAssessedElementContingency, true)); } } + + protected boolean incompatibleLocationsBetweenCnecNetworkElementsAndContingency(Set cnecElementsIds, Contingency contingency) { + return contingency != null && !GeographicalFilter.networkElementsShareCommonCountry(cnecElementsIds, contingency.getNetworkElements().stream().map(NetworkElement::getId).collect(Collectors.toSet()), network); + } + + protected boolean incompatibleLocationsBetweenCnecNetworkElementsAndContingency(String cnecElementId, Contingency contingency) { + return incompatibleLocationsBetweenCnecNetworkElementsAndContingency(Set.of(cnecElementId), contingency); + } } diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java index 95920a0ad6..ab3d9321f9 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java @@ -71,8 +71,6 @@ public void addFlowCnecs() { private FlowCnecAdder initFlowCnec() { return crac.newFlowCnec() - .withMonitored(false) - .withOptimized(true) .withReliabilityMargin(0); } @@ -237,7 +235,6 @@ private void addCurativeFlowCnec(Branch networkElement, Contingency contingen if (thresholds.isEmpty()) { return; } - String cnecName = getCnecName(instantId, contingency, tatlDuration); FlowCnecAdder cnecAdder = initFlowCnec(); addCnecBaseInformation(cnecAdder, contingency, instantId, tatlDuration); for (TwoSides side : thresholds.keySet()) { @@ -249,7 +246,7 @@ private void addCurativeFlowCnec(Branch networkElement, Contingency contingen return; } cnecAdder.add(); - markCnecAsImportedAndHandleRejectedContingencies(cnecName, contingency); + markCnecAsImportedAndHandleRejectedContingencies(instantId, contingency); } private void addAllFlowCnecsFromBranchAndOperationalLimits(Branch networkElement, Map> thresholds, boolean useMaxAndMinThresholds, boolean definedWithConductingEquipment) { @@ -268,6 +265,10 @@ private void addAllFlowCnecsFromBranchAndOperationalLimits(Branch networkElem } for (Contingency contingency : linkedContingencies) { + if (incompatibleLocationsBetweenCnecNetworkElementsAndContingency(networkElement.getId(), contingency)) { + csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.notImported(assessedElementId, ImportStatus.INCONSISTENCY_IN_DATA, writeAssessedElementIgnoredReasonMessage("AssessedElement and Contingency " + contingency.getId()) + " do not belong to a common country. FlowCNEC will not be imported.")); + continue; + } // Add PATL if (hasPatl) { addFlowCnec(networkElement, contingency, crac.getInstant(InstantKind.CURATIVE).getId(), patlThresholds, useMaxAndMinThresholds, false); diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java new file mode 100644 index 0000000000..2a9d82e40a --- /dev/null +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2023, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.cnec; + +import com.powsybl.iidm.network.Branch; +import com.powsybl.iidm.network.Bus; +import com.powsybl.iidm.network.Country; +import com.powsybl.iidm.network.HvdcLine; +import com.powsybl.iidm.network.Identifiable; +import com.powsybl.iidm.network.Injection; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.Substation; +import com.powsybl.iidm.network.Switch; +import com.powsybl.iidm.network.VoltageLevel; +import com.powsybl.openrao.commons.OpenRaoException; +import org.apache.commons.lang3.NotImplementedException; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * @author Thomas Bouquet {@literal } + */ +public final class GeographicalFilter { + + private GeographicalFilter() { + } + + public static Set getNetworkElementLocation(String networkElementId, Network network) { + Identifiable networkElement = network.getIdentifiable(networkElementId); + if (Objects.isNull(networkElement)) { + throw new OpenRaoException("Network element " + networkElementId + " was not found in the network."); + } + if (networkElement instanceof Branch branch) { + return getBranchLocation(branch); + } + if (networkElement instanceof Switch switchElement) { + return getSwitchLocation(switchElement); + } + if (networkElement instanceof Injection injection) { + return getInjectionLocation(injection); + } + if (networkElement instanceof Bus bus) { + return getBusLocation(bus); + } + if (networkElement instanceof VoltageLevel voltageLevel) { + return getVoltageLevelLocation(voltageLevel); + } + if (networkElement instanceof Substation substation) { + return getSubstationLocation(substation); + } + if (networkElement instanceof HvdcLine hvdcLine) { + return getHvdcLineLocation(hvdcLine); + } + throw new NotImplementedException("Could not figure out the location of " + networkElement.getId() + " of type " + networkElement.getClass()); + } + + private static Set getBranchLocation(Branch branch) { + Optional country1 = branch.getTerminal1() == null ? Optional.empty() : getSubstationCountry(branch.getTerminal1().getVoltageLevel().getSubstation()); + Optional country2 = branch.getTerminal2() == null ? Optional.empty() : getSubstationCountry(branch.getTerminal2().getVoltageLevel().getSubstation()); + Set locations = new HashSet<>(); + country1.ifPresent(locations::add); + country2.ifPresent(locations::add); + return locations; + } + + private static Set getSwitchLocation(Switch switchElement) { + return getSubstationCountry(switchElement.getVoltageLevel().getSubstation()).map(Set::of).orElseGet(Set::of); + } + + private static Set getInjectionLocation(Injection injection) { + Optional location = injection.getTerminal() == null ? Optional.empty() : getSubstationCountry(injection.getTerminal().getVoltageLevel().getSubstation()); + return location.map(Set::of).orElseGet(Set::of); + } + + private static Set getBusLocation(Bus bus) { + return getSubstationCountry(bus.getVoltageLevel().getSubstation()).map(Set::of).orElseGet(Set::of); + } + + private static Set getVoltageLevelLocation(VoltageLevel voltageLevel) { + return getSubstationCountry(voltageLevel.getSubstation()).map(Set::of).orElseGet(Set::of); + } + + private static Set getSubstationLocation(Substation substation) { + return substation.getCountry().map(Set::of).orElseGet(Set::of); + } + + private static Set getHvdcLineLocation(HvdcLine hvdcLine) { + Optional country1 = hvdcLine.getConverterStation1().getTerminal() == null ? Optional.empty() : getSubstationCountry(hvdcLine.getConverterStation1().getTerminal().getVoltageLevel().getSubstation()); + Optional country2 = hvdcLine.getConverterStation2().getTerminal() == null ? Optional.empty() : getSubstationCountry(hvdcLine.getConverterStation2().getTerminal().getVoltageLevel().getSubstation()); + Set locations = new HashSet<>(); + country1.ifPresent(locations::add); + country2.ifPresent(locations::add); + return locations; + } + + private static Optional getSubstationCountry(Optional substation) { + return substation.isPresent() ? substation.get().getCountry() : Optional.empty(); + } + + public static Set getNetworkElementsLocations(Set networkElementIds, Network network) { + Set locations = new HashSet<>(); + networkElementIds.forEach(networkElement -> locations.addAll(getNetworkElementLocation(networkElement, network))); + return locations; + } + + public static boolean networkElementsShareCommonCountry(Set networkElementsSet1, Set networkElementsSet2, Network network) { + Set locations1 = getNetworkElementsLocations(networkElementsSet1, network); + Set locations2 = getNetworkElementsLocations(networkElementsSet2, network); + locations1.retainAll(locations2); + return !locations1.isEmpty(); + } +} From 106aaa257db6d0f4cc5ac74038f602c14e268d9a Mon Sep 17 00:00:00 2001 From: Thomas Bouquet Date: Tue, 26 Mar 2024 09:37:38 +0100 Subject: [PATCH 2/7] Bug with cnecName instead of instantId Signed-off-by: Thomas Bouquet --- .../creator/csaprofile/craccreator/cnec/FlowCnecCreator.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java index 95920a0ad6..d2501a0761 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java @@ -71,8 +71,6 @@ public void addFlowCnecs() { private FlowCnecAdder initFlowCnec() { return crac.newFlowCnec() - .withMonitored(false) - .withOptimized(true) .withReliabilityMargin(0); } @@ -249,7 +247,7 @@ private void addCurativeFlowCnec(Branch networkElement, Contingency contingen return; } cnecAdder.add(); - markCnecAsImportedAndHandleRejectedContingencies(cnecName, contingency); + markCnecAsImportedAndHandleRejectedContingencies(instantId, contingency); } private void addAllFlowCnecsFromBranchAndOperationalLimits(Branch networkElement, Map> thresholds, boolean useMaxAndMinThresholds, boolean definedWithConductingEquipment) { From c48c58b135c27d8b9b29d49d1512b66ac99927cf Mon Sep 17 00:00:00 2001 From: Thomas Bouquet Date: Tue, 26 Mar 2024 10:32:09 +0100 Subject: [PATCH 3/7] Wrong FlowCnec name with TATL in creation context Signed-off-by: Thomas Bouquet --- .../csaprofile/craccreator/cnec/AbstractCnecCreator.java | 3 +-- .../csaprofile/craccreator/cnec/AngleCnecCreator.java | 4 +--- .../creator/csaprofile/craccreator/cnec/FlowCnecCreator.java | 5 ++--- .../csaprofile/craccreator/cnec/VoltageCnecCreator.java | 2 +- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java index 6c1a1d0b7d..e072ecc3ab 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java @@ -105,8 +105,7 @@ private void initCnecAdder(CnecAdder cnecAdder, Contingency contingency, Stri .withMonitored(aeScannedForRegion); } - protected void markCnecAsImportedAndHandleRejectedContingencies(String instantId, Contingency contingency) { - String cnecName = getCnecName(instantId, contingency); + protected void markCnecAsImportedAndHandleRejectedContingencies(String cnecName) { if (rejectedLinksAssessedElementContingency.isEmpty()) { csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.imported(assessedElementId, cnecName, cnecName, "", false)); } else { diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java index aa60a7d687..044972255b 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java @@ -40,14 +40,12 @@ private void addAngleCnec(String instantId, Contingency contingency) { return; } angleCnecAdder.add(); - markCnecAsImportedAndHandleRejectedContingencies(instantId, contingency); + markCnecAsImportedAndHandleRejectedContingencies(getCnecName(instantId, contingency)); } } private AngleCnecAdder initAngleCnec() { return crac.newAngleCnec() - .withMonitored(true) - .withOptimized(false) .withReliabilityMargin(0); } diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java index d2501a0761..8f2d2f0307 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java @@ -228,14 +228,13 @@ private void addFlowCnec(Branch networkElement, Contingency contingency, Stri csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.imported(assessedElementId, cnecName, cnecName, "the AssessedElement was pointing to a TATL and used inBaseCase. For the preventive instant, this TATL was also used as a PATL to create the CNEC", true)); return; } - markCnecAsImportedAndHandleRejectedContingencies(instantId, contingency); + markCnecAsImportedAndHandleRejectedContingencies(getCnecName(instantId, contingency)); } private void addCurativeFlowCnec(Branch networkElement, Contingency contingency, String instantId, EnumMap thresholds, boolean useMaxAndMinThresholds, int tatlDuration) { if (thresholds.isEmpty()) { return; } - String cnecName = getCnecName(instantId, contingency, tatlDuration); FlowCnecAdder cnecAdder = initFlowCnec(); addCnecBaseInformation(cnecAdder, contingency, instantId, tatlDuration); for (TwoSides side : thresholds.keySet()) { @@ -247,7 +246,7 @@ private void addCurativeFlowCnec(Branch networkElement, Contingency contingen return; } cnecAdder.add(); - markCnecAsImportedAndHandleRejectedContingencies(instantId, contingency); + markCnecAsImportedAndHandleRejectedContingencies(getCnecName(instantId, contingency, tatlDuration)); } private void addAllFlowCnecsFromBranchAndOperationalLimits(Branch networkElement, Map> thresholds, boolean useMaxAndMinThresholds, boolean definedWithConductingEquipment) { diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/VoltageCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/VoltageCnecCreator.java index 664fd78984..d59a653254 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/VoltageCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/VoltageCnecCreator.java @@ -39,7 +39,7 @@ private void addVoltageCnec(String instantId, Contingency contingency) { return; } voltageCnecAdder.add(); - markCnecAsImportedAndHandleRejectedContingencies(instantId, contingency); + markCnecAsImportedAndHandleRejectedContingencies(getCnecName(instantId, contingency)); } } From e7e40de9be5407de6ace4637a9e6b430e1acd3e5 Mon Sep 17 00:00:00 2001 From: Thomas Bouquet Date: Tue, 26 Mar 2024 13:29:16 +0100 Subject: [PATCH 4/7] Don't log geographical filter Signed-off-by: Thomas Bouquet --- .../creator/csaprofile/craccreator/cnec/FlowCnecCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java index a55bd2fd0a..9d001f4a24 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java @@ -266,7 +266,7 @@ private void addAllFlowCnecsFromBranchAndOperationalLimits(Branch networkElem for (Contingency contingency : linkedContingencies) { if (incompatibleLocationsBetweenCnecNetworkElementsAndContingency(networkElement.getId(), contingency)) { - csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.notImported(assessedElementId, ImportStatus.INCONSISTENCY_IN_DATA, writeAssessedElementIgnoredReasonMessage("AssessedElement and Contingency " + contingency.getId()) + " do not belong to a common country. FlowCNEC will not be imported.")); + // csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.notImported(assessedElementId, ImportStatus.INCONSISTENCY_IN_DATA, writeAssessedElementIgnoredReasonMessage("AssessedElement and Contingency " + contingency.getId()) + " do not belong to a common country. FlowCNEC will not be imported.")); continue; } // Add PATL From db89b1469d8f2c369094b4997413c4eb3f688e13 Mon Sep 17 00:00:00 2001 From: Thomas Bouquet Date: Wed, 10 Apr 2024 09:23:09 +0200 Subject: [PATCH 5/7] Merge main Signed-off-by: Thomas Bouquet --- .../csaprofile/craccreator/cnec/AbstractCnecCreator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java index ca00719171..686a16b7c3 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java @@ -1,8 +1,8 @@ package com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.cnec; import com.powsybl.contingency.Contingency; +import com.powsybl.contingency.ContingencyElement; import com.powsybl.openrao.data.cracapi.Crac; -import com.powsybl.openrao.data.cracapi.NetworkElement; import com.powsybl.openrao.data.cracapi.cnec.CnecAdder; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracCreationContext; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils; @@ -116,7 +116,7 @@ protected void markCnecAsImportedAndHandleRejectedContingencies(String cnecName) } protected boolean incompatibleLocationsBetweenCnecNetworkElementsAndContingency(Set cnecElementsIds, Contingency contingency) { - return contingency != null && !GeographicalFilter.networkElementsShareCommonCountry(cnecElementsIds, contingency.getNetworkElements().stream().map(NetworkElement::getId).collect(Collectors.toSet()), network); + return contingency != null && !GeographicalFilter.networkElementsShareCommonCountry(cnecElementsIds, contingency.getElements().stream().map(ContingencyElement::getId).collect(Collectors.toSet()), network); } protected boolean incompatibleLocationsBetweenCnecNetworkElementsAndContingency(String cnecElementId, Contingency contingency) { From ed7756a8ffd951b3f5f98200ed3ef1814237eb8d Mon Sep 17 00:00:00 2001 From: Peter Mitri Date: Mon, 6 May 2024 14:50:24 +0200 Subject: [PATCH 6/7] angle & voltage CNEC import fix Signed-off-by: Peter Mitri --- .../craccreator/cnec/AbstractCnecCreator.java | 10 +++++++--- .../csaprofile/craccreator/cnec/AngleCnecCreator.java | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java index 7390fd202c..aa5f423436 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java @@ -4,6 +4,7 @@ import com.powsybl.contingency.ContingencyElement; import com.powsybl.openrao.data.cracapi.Crac; import com.powsybl.openrao.data.cracapi.cnec.CnecAdder; +import com.powsybl.openrao.data.cracapi.cnec.FlowCnecAdder; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracCreationContext; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileCracUtils; import com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.CsaProfileElementaryCreationContext; @@ -99,9 +100,12 @@ private void initCnecAdder(CnecAdder cnecAdder, Contingency contingency, Stri .withId(cnecName) .withName(cnecName) .withInstant(instantId) - .withOperator(CsaProfileCracUtils.getTsoNameFromUrl(assessedElementOperator)) - .withOptimized(aeSecuredForRegion) - .withMonitored(aeScannedForRegion); + .withOperator(CsaProfileCracUtils.getTsoNameFromUrl(assessedElementOperator)); + if (cnecAdder instanceof FlowCnecAdder) { + // The following 2 lines mustn't be called for angle & voltage CNECs + cnecAdder.withOptimized(aeSecuredForRegion) + .withMonitored(aeScannedForRegion); + } } protected void markCnecAsImportedAndHandleRejectedContingencies(String cnecName) { diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java index f4905879e7..b2096d49e0 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AngleCnecCreator.java @@ -42,7 +42,7 @@ private void addAngleCnec(String instantId, Contingency contingency) { } private AngleCnecAdder initAngleCnec() { - return crac.newAngleCnec().withReliabilityMargin(0); + return crac.newAngleCnec().withReliabilityMargin(0).withOptimized(false).withMonitored(true); } private void addAngleLimit(AngleCnecAdder angleCnecAdder) { From 7f4b6a1d662af4b089bde2d337c992b490be1ddb Mon Sep 17 00:00:00 2001 From: Philippe Edwards Date: Tue, 25 Jun 2024 15:41:43 +0200 Subject: [PATCH 7/7] remove geographical filter Signed-off-by: Philippe Edwards --- .../crac-creator-csa-profiles/pom.xml | 7 +- .../craccreator/cnec/AbstractCnecCreator.java | 10 -- .../craccreator/cnec/FlowCnecCreator.java | 4 - .../craccreator/cnec/GeographicalFilter.java | 120 ------------------ 4 files changed, 1 insertion(+), 140 deletions(-) delete mode 100644 data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java diff --git a/data/crac-creation/crac-creator-csa-profiles/pom.xml b/data/crac-creation/crac-creator-csa-profiles/pom.xml index 09134099a9..cc83ba4347 100644 --- a/data/crac-creation/crac-creator-csa-profiles/pom.xml +++ b/data/crac-creation/crac-creator-csa-profiles/pom.xml @@ -109,12 +109,7 @@ ch.qos.logback logback-classic - - org.junit.platform - junit-platform-launcher - test - 1.10.0 - + \ No newline at end of file diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java index 8502d94173..c8912cdd41 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/AbstractCnecCreator.java @@ -7,7 +7,6 @@ package com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.cnec; import com.powsybl.contingency.Contingency; -import com.powsybl.contingency.ContingencyElement; import com.powsybl.openrao.data.cracapi.Crac; import com.powsybl.openrao.data.cracapi.cnec.CnecAdder; import com.powsybl.openrao.data.cracapi.cnec.FlowCnecAdder; @@ -25,7 +24,6 @@ import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; /** * @author Thomas Bouquet {@literal } @@ -115,12 +113,4 @@ protected void markCnecAsImportedAndHandleRejectedContingencies(String cnecName) csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.imported(nativeAssessedElement.mrid(), cnecName, cnecName, "some cnec for the same assessed element are not imported because of incorrect data for assessed elements for contingencies : " + rejectedLinksAssessedElementContingency, true)); } } - - protected boolean incompatibleLocationsBetweenCnecNetworkElementsAndContingency(Set cnecElementsIds, Contingency contingency) { - return contingency != null && !GeographicalFilter.networkElementsShareCommonCountry(cnecElementsIds, contingency.getElements().stream().map(ContingencyElement::getId).collect(Collectors.toSet()), network); - } - - protected boolean incompatibleLocationsBetweenCnecNetworkElementsAndContingency(String cnecElementId, Contingency contingency) { - return incompatibleLocationsBetweenCnecNetworkElementsAndContingency(Set.of(cnecElementId), contingency); - } } diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java index 2433f35a8e..ee4bbab27f 100644 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java +++ b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/FlowCnecCreator.java @@ -221,10 +221,6 @@ private void addAllFlowCnecsFromBranchAndOperationalLimits(Branch networkElem && (networkElement.getCurrentLimits(twoSides).isEmpty() || networkElement.getCurrentLimits(twoSides).isPresent() && networkElement.getCurrentLimits(twoSides).get().getTemporaryLimits().isEmpty()))); for (Contingency contingency : linkedContingencies) { - if (incompatibleLocationsBetweenCnecNetworkElementsAndContingency(networkElement.getId(), contingency)) { - // csaProfileCnecCreationContexts.add(CsaProfileElementaryCreationContext.notImported(assessedElementId, ImportStatus.INCONSISTENCY_IN_DATA, writeAssessedElementIgnoredReasonMessage("AssessedElement and Contingency " + contingency.getId()) + " do not belong to a common country. FlowCNEC will not be imported.")); - continue; - } thresholds.forEach((acceptableDuration, limitThresholds) -> limitThresholds.forEach((twoSides, threshold) -> addCurativeFlowCnec(networkElement, useMaxAndMinThresholds, instantToDurationMaps, forceUseOfPatl, contingency, acceptableDuration, twoSides, threshold))); } diff --git a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java b/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java deleted file mode 100644 index 2a9d82e40a..0000000000 --- a/data/crac-creation/crac-creator-csa-profiles/src/main/java/com/powsybl/openrao/data/craccreation/creator/csaprofile/craccreator/cnec/GeographicalFilter.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2023, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -package com.powsybl.openrao.data.craccreation.creator.csaprofile.craccreator.cnec; - -import com.powsybl.iidm.network.Branch; -import com.powsybl.iidm.network.Bus; -import com.powsybl.iidm.network.Country; -import com.powsybl.iidm.network.HvdcLine; -import com.powsybl.iidm.network.Identifiable; -import com.powsybl.iidm.network.Injection; -import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.Substation; -import com.powsybl.iidm.network.Switch; -import com.powsybl.iidm.network.VoltageLevel; -import com.powsybl.openrao.commons.OpenRaoException; -import org.apache.commons.lang3.NotImplementedException; - -import java.util.HashSet; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; - -/** - * @author Thomas Bouquet {@literal } - */ -public final class GeographicalFilter { - - private GeographicalFilter() { - } - - public static Set getNetworkElementLocation(String networkElementId, Network network) { - Identifiable networkElement = network.getIdentifiable(networkElementId); - if (Objects.isNull(networkElement)) { - throw new OpenRaoException("Network element " + networkElementId + " was not found in the network."); - } - if (networkElement instanceof Branch branch) { - return getBranchLocation(branch); - } - if (networkElement instanceof Switch switchElement) { - return getSwitchLocation(switchElement); - } - if (networkElement instanceof Injection injection) { - return getInjectionLocation(injection); - } - if (networkElement instanceof Bus bus) { - return getBusLocation(bus); - } - if (networkElement instanceof VoltageLevel voltageLevel) { - return getVoltageLevelLocation(voltageLevel); - } - if (networkElement instanceof Substation substation) { - return getSubstationLocation(substation); - } - if (networkElement instanceof HvdcLine hvdcLine) { - return getHvdcLineLocation(hvdcLine); - } - throw new NotImplementedException("Could not figure out the location of " + networkElement.getId() + " of type " + networkElement.getClass()); - } - - private static Set getBranchLocation(Branch branch) { - Optional country1 = branch.getTerminal1() == null ? Optional.empty() : getSubstationCountry(branch.getTerminal1().getVoltageLevel().getSubstation()); - Optional country2 = branch.getTerminal2() == null ? Optional.empty() : getSubstationCountry(branch.getTerminal2().getVoltageLevel().getSubstation()); - Set locations = new HashSet<>(); - country1.ifPresent(locations::add); - country2.ifPresent(locations::add); - return locations; - } - - private static Set getSwitchLocation(Switch switchElement) { - return getSubstationCountry(switchElement.getVoltageLevel().getSubstation()).map(Set::of).orElseGet(Set::of); - } - - private static Set getInjectionLocation(Injection injection) { - Optional location = injection.getTerminal() == null ? Optional.empty() : getSubstationCountry(injection.getTerminal().getVoltageLevel().getSubstation()); - return location.map(Set::of).orElseGet(Set::of); - } - - private static Set getBusLocation(Bus bus) { - return getSubstationCountry(bus.getVoltageLevel().getSubstation()).map(Set::of).orElseGet(Set::of); - } - - private static Set getVoltageLevelLocation(VoltageLevel voltageLevel) { - return getSubstationCountry(voltageLevel.getSubstation()).map(Set::of).orElseGet(Set::of); - } - - private static Set getSubstationLocation(Substation substation) { - return substation.getCountry().map(Set::of).orElseGet(Set::of); - } - - private static Set getHvdcLineLocation(HvdcLine hvdcLine) { - Optional country1 = hvdcLine.getConverterStation1().getTerminal() == null ? Optional.empty() : getSubstationCountry(hvdcLine.getConverterStation1().getTerminal().getVoltageLevel().getSubstation()); - Optional country2 = hvdcLine.getConverterStation2().getTerminal() == null ? Optional.empty() : getSubstationCountry(hvdcLine.getConverterStation2().getTerminal().getVoltageLevel().getSubstation()); - Set locations = new HashSet<>(); - country1.ifPresent(locations::add); - country2.ifPresent(locations::add); - return locations; - } - - private static Optional getSubstationCountry(Optional substation) { - return substation.isPresent() ? substation.get().getCountry() : Optional.empty(); - } - - public static Set getNetworkElementsLocations(Set networkElementIds, Network network) { - Set locations = new HashSet<>(); - networkElementIds.forEach(networkElement -> locations.addAll(getNetworkElementLocation(networkElement, network))); - return locations; - } - - public static boolean networkElementsShareCommonCountry(Set networkElementsSet1, Set networkElementsSet2, Network network) { - Set locations1 = getNetworkElementsLocations(networkElementsSet1, network); - Set locations2 = getNetworkElementsLocations(networkElementsSet2, network); - locations1.retainAll(locations2); - return !locations1.isEmpty(); - } -}