From 3e10b3533428f3eff5b7ad3a640f30d8dd87145e Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 11 Jul 2023 13:55:07 +0200 Subject: [PATCH 01/12] [CSA-25] Implement voltage remedial actions in crac API --- .../RemedialActionSeriesCreator.java | 40 +- ...sonVoltageCnecsCreationParametersTest.java | 2 +- .../JsonSerializationConstants.java | 6 +- .../HvdcRangeActionArrayDeserializer.java | 8 + ...InjectionRangeActionArrayDeserializer.java | 8 + .../NetworkActionArrayDeserializer.java | 8 + .../OnVoltageConstraintArrayDeserializer.java | 45 ++ .../PstRangeActionArrayDeserializer.java | 8 + .../serializers/CracJsonSerializerModule.java | 3 +- .../OnVoltageConstraintSerializer.java | 29 ++ .../serializers/UsageRulesSerializer.java | 1 + .../crac_io_json/CracImportExportTest.java | 11 +- .../JsonRetrocompatibilityTest.java | 28 ++ .../retrocompatibility/v1/crac-v1.7.json | 406 ++++++++++++++++++ .../data/crac_api/RemedialActionAdder.java | 2 + .../usage_rule/OnVoltageConstraint.java | 29 ++ .../usage_rule/OnVoltageConstraintAdder.java | 22 + .../AbstractRemedialActionAdder.java | 7 +- .../OnVoltageConstraintAdderImpl.java | 69 +++ .../crac_impl/OnVoltageConstraintImpl.java | 62 +++ .../OnVoltageConstraintImplTest.java | 91 ++++ .../utils/ExhaustiveCracCreation.java | 12 + .../IteratingLinearOptimizerTest.java | 2 +- 23 files changed, 887 insertions(+), 12 deletions(-) create mode 100644 data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java create mode 100644 data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java create mode 100644 data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json create mode 100644 data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java create mode 100644 data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java create mode 100644 data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java create mode 100644 data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java create mode 100644 data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java diff --git a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java index 1c09eff7d8..ec7c88e1a0 100644 --- a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java +++ b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java @@ -12,7 +12,9 @@ import com.farao_community.farao.data.crac_api.Instant; import com.farao_community.farao.data.crac_api.RemedialActionAdder; import com.farao_community.farao.data.crac_api.cnec.AngleCnec; +import com.farao_community.farao.data.crac_api.cnec.Cnec; import com.farao_community.farao.data.crac_api.cnec.FlowCnec; +import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.crac_creation.creator.api.ImportStatus; import com.farao_community.farao.data.crac_creation.creator.cim.crac_creator.CimCracCreationContext; @@ -46,6 +48,8 @@ public class RemedialActionSeriesCreator { private HvdcRangeActionCreator hvdcRangeActionCreator = null; private Set flowCnecs; private AngleCnec angleCnec; + + private VoltageCnec voltageCnec; private final CimCracCreationParameters cimCracCreationParameters; private final Map pstRangeActionCreators = new HashMap<>(); private final Map networkActionCreators = new HashMap<>(); @@ -291,7 +295,7 @@ private boolean identifyAndReadPstRangeAction(String createdRemedialActionId, St private void addExtraUsageRules(String applicationModeMarketObjectStatus, String remedialActionId, RemedialActionAdder adder) { try { RemedialActionSeriesCreator.addUsageRules( - applicationModeMarketObjectStatus, adder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain + applicationModeMarketObjectStatus, adder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec, sharedDomain ); } catch (FaraoImportException e) { cracCreationContext.getCreationReport().warn(String.format("Extra usage rules for RA %s could not be imported: %s", remedialActionId, e.getMessage())); @@ -372,18 +376,28 @@ public static void addUsageRules(String applicationModeMarketObjectStatus, Set flowCnecs, AngleCnec angleCnec, Country sharedDomain) { + addUsageRules(applicationModeMarketObjectStatus,remedialActionAdder,contingencies,invalidContingencies,flowCnecs,angleCnec,null,sharedDomain); + } + public static void addUsageRules(String applicationModeMarketObjectStatus, + RemedialActionAdder remedialActionAdder, + List contingencies, + List invalidContingencies, + Set flowCnecs, + AngleCnec angleCnec, + VoltageCnec voltageCnec, + Country sharedDomain) { if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec,sharedDomain, Instant.PREVENTIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.CURATIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA_AND_CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, null, null, flowCnecs, angleCnec, sharedDomain, Instant.PREVENTIVE); - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.CURATIVE); + addUsageRulesAtInstant(remedialActionAdder, null, null, flowCnecs, angleCnec,voltageCnec, sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.AUTO.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.AUTO); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec,sharedDomain, Instant.AUTO); } } @@ -392,6 +406,7 @@ private static void addUsageRulesAtInstant(RemedialActionAdder remedialAction List invalidContingencies, Set flowCnecs, AngleCnec angleCnec, + VoltageCnec voltageCnec, Country sharedDomain, Instant instant) { if (!flowCnecs.isEmpty()) { @@ -402,6 +417,10 @@ private static void addUsageRulesAtInstant(RemedialActionAdder remedialAction addOnAngleConstraintUsageRule(remedialActionAdder, angleCnec); return; } + if (Objects.nonNull(voltageCnec)) { + addOnVoltageConstraintUsageRule(remedialActionAdder, voltageCnec); + return; + } if (!Objects.isNull(sharedDomain)) { remedialActionAdder.newOnFlowConstraintInCountryUsageRule().withInstant(instant).withCountry(sharedDomain).add(); return; @@ -483,6 +502,15 @@ private static void addOnAngleConstraintUsageRule(RemedialActionAdder adder, .add(); } + private static void addOnVoltageConstraintUsageRule(RemedialActionAdder adder, VoltageCnec voltageCnec) { + adder.newOnVoltageConstraintUsageRule() + .withVoltageCnec(voltageCnec.getId()) + .withInstant(Instant.CURATIVE) + .add(); + } + + + /*-------------- SERIES CHECKS ------------------------------*/ private boolean checkRemedialActionSeries(Series cimSerie) { // --- Check optimizationStatus diff --git a/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java b/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java index 5e2e0972b8..06e3e0803d 100644 --- a/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java +++ b/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java @@ -55,7 +55,7 @@ void roundTripOnVoltageMonitoringParameters() throws IOException { String exportedString = os.toString(); InputStream inputStream = getClass().getResourceAsStream("/parameters/voltage-cnecs-creation-parameters-for-round-trip.json"); - assertEquals(new String(inputStream.readAllBytes()), exportedString); + assertEquals(new String(inputStream.readAllBytes()).replaceAll("\r",""), exportedString.replaceAll("\r","")); } @ParameterizedTest diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java index 9e1e902b36..e64a8c25cd 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java @@ -30,13 +30,14 @@ public final class JsonSerializationConstants { private JsonSerializationConstants() { } - public static final String CRAC_IO_VERSION = "1.6"; + public static final String CRAC_IO_VERSION = "1.7"; /* v1.1: addition of switchPairs v1.2: addition of injectionRangeAction v1.3: addition of hvdcRangeAction's and injectionRangeAction's initial setpoints v1.4: addition of AngleCnecs; frm renamed to reliabilityMargin v1.5: addition of VoltageCnecs + v1.7 addition of VoltageConstraints */ // headers @@ -70,6 +71,8 @@ private JsonSerializationConstants() { public static final String VOLTAGE_CNECS = "voltageCnecs"; + public static final String VOLTAGE_CNEC_ID = "voltageCnecId"; + public static final String THRESHOLDS = "thresholds"; public static final String RELIABILITY_MARGIN = "reliabilityMargin"; public static final String FRM = "frm"; @@ -95,6 +98,7 @@ private JsonSerializationConstants() { public static final String ON_STATE_USAGE_RULES = "onStateUsageRules"; // retro-compatibility only public static final String ON_FLOW_CONSTRAINT_USAGE_RULES = "onFlowConstraintUsageRules"; public static final String ON_ANGLE_CONSTRAINT_USAGE_RULES = "onAngleConstraintUsageRules"; + public static final String ON_VOLTAGE_CONSTRAINT_USAGE_RULES = "onVoltageConstraintUsageRules"; public static final String ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES = "onFlowConstraintInCountryUsageRules"; public static final String ID = "id"; diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java index 838e2f2961..55554e6ee3 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java @@ -82,6 +82,14 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des jsonParser.nextToken(); OnAngleConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); break; + case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); + } else { + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); + } + break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); OnFlowConstraintInCountryArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java index 0a8ec8d18c..4857c05b06 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java @@ -83,6 +83,14 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des jsonParser.nextToken(); OnAngleConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); break; + case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); + } else { + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); + } + break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); OnFlowConstraintInCountryArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java index 208a8ef2d5..831a3ce050 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java @@ -82,6 +82,14 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des jsonParser.nextToken(); OnAngleConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); break; + case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); + } else { + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); + } + break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); OnFlowConstraintInCountryArrayDeserializer.deserialize(jsonParser, networkActionAdder); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java new file mode 100644 index 0000000000..34581ab7de --- /dev/null +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_io_json.deserializers; + +import com.farao_community.farao.commons.FaraoException; +import com.farao_community.farao.data.crac_api.RemedialActionAdder; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraintAdder; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +import java.io.IOException; + +import static com.farao_community.farao.data.crac_io_json.JsonSerializationConstants.*; + +/** + * @author Fabrice Buscaylet {@literal } + */ +public final class OnVoltageConstraintArrayDeserializer { + private OnVoltageConstraintArrayDeserializer() { + } + + public static void deserialize(JsonParser jsonParser, RemedialActionAdder ownerAdder) throws IOException { + while (jsonParser.nextToken() != JsonToken.END_ARRAY) { + OnVoltageConstraintAdder adder = ownerAdder.newOnVoltageConstraintUsageRule(); + while (!jsonParser.nextToken().isStructEnd()) { + switch (jsonParser.getCurrentName()) { + case INSTANT: + adder.withInstant(deserializeInstant(jsonParser.nextTextValue())); + break; + case VOLTAGE_CNEC_ID: + adder.withVoltageCnec(jsonParser.nextTextValue()); + break; + default: + throw new FaraoException("Unexpected field in OnVoltageConstraint: " + jsonParser.getCurrentName()); + } + } + adder.add(); + } + } +} diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java index d6feb6ea14..e8af268c00 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java @@ -84,6 +84,14 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des jsonParser.nextToken(); OnAngleConstraintArrayDeserializer.deserialize(jsonParser, pstRangeActionAdder); break; + case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); + } else { + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, pstRangeActionAdder); + } + break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); OnFlowConstraintInCountryArrayDeserializer.deserialize(jsonParser, pstRangeActionAdder); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/CracJsonSerializerModule.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/CracJsonSerializerModule.java index 8a0871fef9..6bdbfac0a4 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/CracJsonSerializerModule.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/CracJsonSerializerModule.java @@ -13,10 +13,10 @@ import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; import com.farao_community.farao.data.crac_api.network_action.*; import com.farao_community.farao.data.crac_api.range.StandardRange; +import com.farao_community.farao.data.crac_api.range.TapRange; import com.farao_community.farao.data.crac_api.range_action.HvdcRangeAction; import com.farao_community.farao.data.crac_api.range_action.InjectionRangeAction; import com.farao_community.farao.data.crac_api.range_action.PstRangeAction; -import com.farao_community.farao.data.crac_api.range.TapRange; import com.farao_community.farao.data.crac_api.threshold.BranchThreshold; import com.farao_community.farao.data.crac_api.threshold.Threshold; import com.farao_community.farao.data.crac_api.usage_rule.*; @@ -43,6 +43,7 @@ public CracJsonSerializerModule() { this.addSerializer(OnContingencyState.class, new OnStateSerializer()); this.addSerializer(OnFlowConstraint.class, new OnFlowConstraintSerializer()); this.addSerializer(OnAngleConstraint.class, new OnAngleConstraintSerializer()); + this.addSerializer(OnVoltageConstraint.class, new OnVoltageConstraintSerializer()); this.addSerializer(OnFlowConstraintInCountry.class, new OnFlowConstraintInCountrySerializer()); this.addSerializer(TapRange.class, new TapRangeSerializer()); this.addSerializer(StandardRange.class, new StandardRangeSerializer()); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java new file mode 100644 index 0000000000..9fb0fdc169 --- /dev/null +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_io_json.serializers; + +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; + +import static com.farao_community.farao.data.crac_io_json.JsonSerializationConstants.*; + +/** + * @author Peter Mitri {@literal } + */ +public class OnVoltageConstraintSerializer extends AbstractJsonSerializer { + @Override + public void serialize(OnVoltageConstraint value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeStartObject(); + gen.writeStringField(INSTANT, serializeInstant(value.getInstant())); + gen.writeStringField(VOLTAGE_CNEC_ID, value.getVoltageCnec().getId()); + gen.writeEndObject(); + } +} diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/UsageRulesSerializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/UsageRulesSerializer.java index 6c9b53a88c..782cfb8a9b 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/UsageRulesSerializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/UsageRulesSerializer.java @@ -29,6 +29,7 @@ public static void serializeUsageRules(RemedialAction remedialAction, JsonGen serializeUsageRules(remedialAction, OnContingencyState.class, ON_CONTINGENCY_STATE_USAGE_RULES, gen); serializeUsageRules(remedialAction, OnFlowConstraint.class, ON_FLOW_CONSTRAINT_USAGE_RULES, gen); serializeUsageRules(remedialAction, OnAngleConstraint.class, ON_ANGLE_CONSTRAINT_USAGE_RULES, gen); + serializeUsageRules(remedialAction, OnVoltageConstraint.class, ON_VOLTAGE_CONSTRAINT_USAGE_RULES, gen); serializeUsageRules(remedialAction, OnFlowConstraintInCountry.class, ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES, gen); } diff --git a/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/CracImportExportTest.java b/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/CracImportExportTest.java index b2762be1fa..3245e68c5e 100644 --- a/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/CracImportExportTest.java +++ b/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/CracImportExportTest.java @@ -50,7 +50,7 @@ void roundTripTest() { assertEquals(7, importedCrac.getFlowCnecs().size()); assertEquals(1, importedCrac.getAngleCnecs().size()); assertEquals(1, importedCrac.getVoltageCnecs().size()); - assertEquals(6, importedCrac.getRangeActions().size()); + assertEquals(7, importedCrac.getRangeActions().size()); assertEquals(4, importedCrac.getNetworkActions().size()); // -------------------------- @@ -274,6 +274,15 @@ void roundTripTest() { assertEquals(CURATIVE, onAngleConstraint.getInstant()); assertSame(crac.getCnec("angleCnecId"), onAngleConstraint.getAngleCnec()); + // check OnVoltageConstraint usage rule + List pstRange4IdUsageRules = crac.getPstRangeAction("pstRange4Id").getUsageRules(); + assertEquals(1, pstRange4IdUsageRules.size()); + UsageRule pstRange4IdFirstUsageRules = pstRange4IdUsageRules.get(0); + assertTrue(pstRange4IdFirstUsageRules instanceof OnVoltageConstraint); + OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) pstRange4IdFirstUsageRules; + assertEquals(CURATIVE, onVoltageConstraint.getInstant()); + assertSame(crac.getCnec("voltageCnecId"), onVoltageConstraint.getVoltageCnec()); + // ----------------------------- // --- test HvdcRangeActions --- // ----------------------------- diff --git a/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java b/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java index 6419c723e3..e22221e46f 100644 --- a/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java +++ b/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java @@ -186,6 +186,27 @@ void importV1Point6Test() { testContentOfV1Point6Crac(crac); } + @Test + void importV1Point7Test() { + + // renaming usage rules + // Branch threshold rule no longer handled + + InputStream cracFile = getClass().getResourceAsStream("/retrocompatibility/v1/crac-v1.7.json"); + + Crac crac = new JsonImport().importCrac(cracFile); + + assertEquals(2, crac.getContingencies().size()); + assertEquals(7, crac.getFlowCnecs().size()); + assertEquals(1, crac.getAngleCnecs().size()); + assertEquals(1, crac.getVoltageCnecs().size()); + assertEquals(4, crac.getNetworkActions().size()); + assertEquals(4, crac.getPstRangeActions().size()); + assertEquals(2, crac.getHvdcRangeActions().size()); + assertEquals(1, crac.getInjectionRangeActions().size()); + testContentOfV1Point7Crac(crac); + } + private void testContentOfV1Point0Crac(Crac crac) { // -------------------------- @@ -492,4 +513,11 @@ void testContentOfV1Point6Crac(Crac crac) { assertEquals(3, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnFlowConstraint.class::isInstance).collect(Collectors.toList()).size()); assertEquals(1, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnAngleConstraint.class::isInstance).collect(Collectors.toList()).size()); } + + void testContentOfV1Point7Crac(Crac crac) { + + testContentOfV1Point6Crac(crac); + // test usage rules + assertEquals(1, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnVoltageConstraint.class::isInstance).collect(Collectors.toList()).size()); + } } diff --git a/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json b/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json new file mode 100644 index 0000000000..2e5b5b7abf --- /dev/null +++ b/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json @@ -0,0 +1,406 @@ +{ + "type" : "CRAC", + "version" : "1.7", + "info" : "Generated by FARAO http://farao-community.github.io", + "id" : "exhaustiveCracId", + "name" : "exhaustiveCracName", + "networkElementsNamePerId" : { + "ne5Id" : "ne5Name", + "ne2Id" : "ne2Name", + "to-close" : "to-close-name", + "generator2Id" : "generator2Name" + }, + "contingencies" : [ { + "id" : "contingency1Id", + "networkElementsIds" : [ "ne1Id" ] + }, { + "id" : "contingency2Id", + "networkElementsIds" : [ "ne2Id", "ne3Id" ] + } ], + "flowCnecs" : [ { + "id" : "cnec1outageId", + "name" : "cnec1outageId", + "networkElementId" : "ne4Id", + "operator" : "operator1", + "instant" : "outage", + "contingencyId" : "contingency1Id", + "optimized" : true, + "monitored" : false, + "reliabilityMargin" : 0.0, + "iMax" : [ NaN ], + "nominalV" : [ 220.0 ], + "thresholds" : [ { + "unit" : "ampere", + "min" : -800.0, + "side" : "left" + } ] + }, { + "id" : "cnec1prevId", + "name" : "cnec1prevId", + "networkElementId" : "ne4Id", + "operator" : "operator1", + "instant" : "preventive", + "optimized" : true, + "monitored" : false, + "reliabilityMargin" : 0.0, + "iMax" : [ NaN, 1000.0 ], + "nominalV" : [ 220.0 ], + "thresholds" : [ { + "unit" : "ampere", + "min" : -500.0, + "side" : "right" + } ] + }, { + "id" : "cnec2prevId", + "name" : "cnec2prevId", + "networkElementId" : "ne5Id", + "operator" : "operator2", + "instant" : "preventive", + "optimized" : true, + "monitored" : false, + "reliabilityMargin" : 0.0, + "iMax" : [ 2000.0 ], + "nominalV" : [ 380.0, 220.0 ], + "thresholds" : [ { + "unit" : "ampere", + "min" : -800.0, + "side" : "left" + }, { + "unit" : "ampere", + "min" : -800.0, + "side" : "left" + }, { + "unit" : "ampere", + "max" : 1200.0, + "side" : "right" + }, { + "unit" : "percent_imax", + "min" : -0.3, + "side" : "left" + } ] + }, { + "id" : "cnec3autoId", + "name" : "cnec3autoName", + "networkElementId" : "ne2Id", + "operator" : "operator3", + "instant" : "auto", + "contingencyId" : "contingency2Id", + "optimized" : false, + "monitored" : true, + "reliabilityMargin" : 20.0, + "iMax" : [ NaN ], + "nominalV" : [ NaN ], + "thresholds" : [ { + "unit" : "megawatt", + "max" : 500.0, + "side" : "left" + } ] + }, { + "id" : "cnec3curId", + "name" : "cnec3curId", + "networkElementId" : "ne2Id", + "operator" : "operator3", + "instant" : "curative", + "contingencyId" : "contingency2Id", + "optimized" : false, + "monitored" : true, + "reliabilityMargin" : 20.0, + "iMax" : [ NaN ], + "nominalV" : [ NaN ], + "thresholds" : [ { + "unit" : "megawatt", + "max" : 500.0, + "side" : "left" + } ] + }, { + "id" : "cnec3prevId", + "name" : "cnec3prevName", + "networkElementId" : "ne2Id", + "operator" : "operator3", + "instant" : "preventive", + "optimized" : false, + "monitored" : true, + "reliabilityMargin" : 20.0, + "iMax" : [ NaN ], + "nominalV" : [ NaN ], + "thresholds" : [ { + "unit" : "megawatt", + "max" : 500.0, + "side" : "left" + } ] + }, { + "id" : "cnec4prevId", + "name" : "cnec4prevName", + "networkElementId" : "ne3Id", + "operator" : "operator4", + "instant" : "preventive", + "optimized" : true, + "monitored" : true, + "reliabilityMargin" : 0.0, + "iMax" : [ NaN ], + "nominalV" : [ NaN ], + "thresholds" : [ { + "unit" : "megawatt", + "max" : 500.0, + "side" : "left" + } ] + } ], + "angleCnecs" : [ { + "id" : "angleCnecId", + "name" : "angleCnecName", + "exportingNetworkElementId" : "eneId", + "importingNetworkElementId" : "ineId", + "operator" : "operator1", + "instant" : "curative", + "contingencyId" : "contingency1Id", + "optimized" : false, + "monitored" : true, + "reliabilityMargin" : 0.0, + "thresholds" : [ { + "unit" : "degree", + "min" : -100.0 + } ] + } ], + "voltageCnecs" : [ { + "id" : "voltageCnecId", + "name" : "voltageCnecName", + "networkElementId" : "voltageCnecNeId", + "operator" : "operator1", + "instant" : "curative", + "contingencyId" : "contingency1Id", + "optimized" : false, + "monitored" : true, + "reliabilityMargin" : 0.0, + "thresholds" : [ { + "unit" : "kilovolt", + "min" : 380.0 + } ] + } ], + "pstRangeActions" : [ { + "id" : "pstRange1Id", + "name" : "pstRange1Name", + "operator" : "RTE", + "onInstantUsageRules" : [ { + "instant" : "preventive", + "usageMethod" : "available" + } ], + "networkElementId" : "pst", + "initialTap" : 2, + "tapToAngleConversionMap" : { + "-1" : 1.0, + "0" : 1.5, + "1" : 2.0, + "2" : 2.5, + "3" : 3.0, + "-3" : 0.0, + "-2" : 0.5 + }, + "ranges" : [ { + "min" : 1, + "max" : 7, + "rangeType" : "absolute" + }, { + "min" : -3, + "max" : 3, + "rangeType" : "relativeToInitialNetwork" + } ] + }, { + "id" : "pstRange2Id", + "name" : "pstRange2Name", + "operator" : "RTE", + "onFlowConstraintUsageRules" : [ { + "instant" : "preventive", + "flowCnecId" : "cnec3prevId" + } ], + "networkElementId" : "pst2", + "groupId" : "group-1-pst", + "initialTap" : 1, + "tapToAngleConversionMap" : { + "-1" : 1.0, + "0" : 1.5, + "1" : 2.0, + "2" : 2.5, + "3" : 3.0, + "-3" : 0.0, + "-2" : 0.5 + }, + "ranges" : [ { + "min" : 1, + "max" : 7, + "rangeType" : "absolute" + }, { + "min" : -3, + "max" : 3, + "rangeType" : "relativeToInitialNetwork" + } ] + }, { + "id" : "pstRange3Id", + "name" : "pstRange3Name", + "operator" : "RTE", + "onAngleConstraintUsageRules" : [ { + "instant" : "curative", + "angleCnecId" : "angleCnecId" + } ], + "networkElementId" : "pst3", + "groupId" : "group-3-pst", + "initialTap" : 1, + "tapToAngleConversionMap" : { + "-1" : 1.0, + "0" : 1.5, + "1" : 2.0, + "2" : 2.5, + "3" : 3.0, + "-3" : 0.0, + "-2" : 0.5 + }, + "ranges" : [ { + "min" : 1, + "max" : 7, + "rangeType" : "absolute" + }, { + "min" : -3, + "max" : 3, + "rangeType" : "relativeToInitialNetwork" + } ] + },{ + "id" : "pstRange4Id", + "name" : "pstRange4Name", + "operator" : "RTE", + "onVoltageConstraintUsageRules" : [ { + "instant" : "curative", + "voltageCnecId" : "voltageCnecId" + } ], + "networkElementId" : "pst3", + "groupId" : "group-3-pst", + "initialTap" : 1, + "tapToAngleConversionMap" : { + "-1" : 1.0, + "0" : 1.5, + "1" : 2.0, + "2" : 2.5, + "3" : 3.0, + "-3" : 0.0, + "-2" : 0.5 + }, + "ranges" : [ { + "min" : 1, + "max" : 7, + "rangeType" : "absolute" + }, { + "min" : -3, + "max" : 3, + "rangeType" : "relativeToInitialNetwork" + } ] + }], + "hvdcRangeActions" : [ { + "id" : "hvdcRange1Id", + "name" : "hvdcRange1Name", + "operator" : "RTE", + "onInstantUsageRules" : [ { + "instant" : "preventive", + "usageMethod" : "available" + } ], + "networkElementId" : "hvdc", + "initialSetpoint" : 100, + "ranges" : [ { + "min" : -1000.0, + "max" : 1000.0 + } ] + }, { + "id" : "hvdcRange2Id", + "name" : "hvdcRange2Name", + "operator" : "RTE", + "onFlowConstraintUsageRules" : [ { + "instant" : "preventive", + "flowCnecId" : "cnec3curId" + } ], + "networkElementId" : "hvdc2", + "groupId" : "group-1-hvdc", + "initialSetpoint" : -100, + "ranges" : [ { + "min" : -1000.0, + "max" : 1000.0 + } ] + } ], + "injectionRangeActions" : [ { + "id" : "injectionRange1Id", + "name" : "injectionRange1Name", + "operator" : null, + "onContingencyStateUsageRules" : [ { + "instant" : "curative", + "contingencyId" : "contingency1Id", + "usageMethod" : "available" + } ], + "networkElementIdsAndKeys" : { + "generator1Id" : 1.0, + "generator2Id" : -1.0 + }, + "initialSetpoint" : 50, + "ranges" : [ { + "min" : -500.0, + "max" : 500.0 + }, { + "min" : -1000.0, + "max" : 1000.0 + } ] + } ], + "networkActions" : [ { + "id" : "complexNetworkActionId", + "name" : "complexNetworkActionName", + "operator" : "RTE", + "onInstantUsageRules" : [ { + "instant" : "preventive", + "usageMethod" : "available" + } ], + "topologicalActions" : [ { + "networkElementId" : "ne1Id", + "actionType" : "close" + } ], + "pstSetpoints" : [ { + "networkElementId" : "pst", + "setpoint" : 5 + } ] + }, { + "id" : "injectionSetpointRaId", + "name" : "injectionSetpointRaName", + "operator" : "RTE", + "onFlowConstraintUsageRules" : [ { + "instant" : "auto", + "flowCnecId" : "cnec3autoId" + } ], + "injectionSetpoints" : [ { + "networkElementId" : "injection", + "setpoint" : 260.0 + } ] + }, { + "id" : "pstSetpointRaId", + "name" : "pstSetpointRaName", + "operator" : "RTE", + "onInstantUsageRules" : [ { + "instant" : "preventive", + "usageMethod" : "available" + } ], + "onContingencyStateUsageRules" : [ { + "instant" : "curative", + "contingencyId" : "contingency1Id", + "usageMethod" : "forced" + } ], + "pstSetpoints" : [ { + "networkElementId" : "pst", + "setpoint" : 15 + } ] + }, { + "id" : "switchPairRaId", + "name" : "switchPairRaName", + "operator" : "RTE", + "onContingencyStateUsageRules" : [ { + "instant" : "curative", + "contingencyId" : "contingency2Id", + "usageMethod" : "available" + } ], + "switchPairs" : [ { + "open" : "to-open", + "close" : "to-close" + } ] + } ] +} \ No newline at end of file diff --git a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/RemedialActionAdder.java b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/RemedialActionAdder.java index 9022524330..e0abad09f3 100644 --- a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/RemedialActionAdder.java +++ b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/RemedialActionAdder.java @@ -24,5 +24,7 @@ public interface RemedialActionAdder> extends I OnAngleConstraintAdder newOnAngleConstraintUsageRule(); + OnVoltageConstraintAdder newOnVoltageConstraintUsageRule(); + OnFlowConstraintInCountryAdder newOnFlowConstraintInCountryUsageRule(); } diff --git a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java new file mode 100644 index 0000000000..8cdb8d6d04 --- /dev/null +++ b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_api.usage_rule; + +import com.farao_community.farao.data.crac_api.Instant; +import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; + +/** + * The OnVoltageConstraint UsageRule is defined on a given VoltageCnec. For instance, if a RemedialAction + * has a OnVoltageConstraint UsageRule with State "cnec1" and UsageMethod TO_BE_EVALUATED, this + * RemedialAction will only be available if "cnec1" is constrained (= has a negative margin). + * + * @author Fabrice Buscaylet {@literal } + */ +public interface OnVoltageConstraint extends UsageRule { + /** + * Get the VoltageCnec that should be constrained + */ + VoltageCnec getVoltageCnec(); + + /** + * Get the Instant of the free to use + */ + Instant getInstant(); +} diff --git a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java new file mode 100644 index 0000000000..845c3013c1 --- /dev/null +++ b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_api.usage_rule; + +import com.farao_community.farao.data.crac_api.Instant; +import com.farao_community.farao.data.crac_api.RemedialActionAdder; + +/** + * @author Fabrice Buscaylet {@literal } + */ +public interface OnVoltageConstraintAdder> { + + OnVoltageConstraintAdder withInstant(Instant instant); + + OnVoltageConstraintAdder withVoltageCnec(String voltageCnecId); + + T add(); +} diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java index 4e7a6755b4..e66b73e964 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java @@ -22,7 +22,7 @@ public abstract class AbstractRemedialActionAdder usageRules = new ArrayList<>(); - private CracImpl crac; + private final CracImpl crac; AbstractRemedialActionAdder(CracImpl crac) { Objects.requireNonNull(crac); @@ -55,6 +55,11 @@ public OnAngleConstraintAdder newOnAngleConstraintUsageRule() { return new OnAngleConstraintAdderImpl(this); } + @Override + public OnVoltageConstraintAdder newOnVoltageConstraintUsageRule() { + return new OnVoltageConstraintAdderImpl(this); + } + @Override public OnFlowConstraintInCountryAdder newOnFlowConstraintInCountryUsageRule() { return new OnFlowConstraintInCountryAdderImpl(this); diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java new file mode 100644 index 0000000000..4befbbe7ec --- /dev/null +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_impl; + +import com.farao_community.farao.commons.FaraoException; +import com.farao_community.farao.data.crac_api.Instant; +import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraintAdder; + +import java.util.Objects; + +import static com.farao_community.farao.data.crac_impl.AdderUtils.assertAttributeNotNull; + +/** + * @author Fabrice Buscaylet {@literal } + */ +public class OnVoltageConstraintAdderImpl> implements OnVoltageConstraintAdder { + + private final T owner; + private Instant instant; + private String voltageCnecId; + + OnVoltageConstraintAdderImpl(AbstractRemedialActionAdder owner) { + this.owner = (T) owner; + } + + @Override + public OnVoltageConstraintAdder withInstant(Instant instant) { + this.instant = instant; + return this; + } + + @Override + public OnVoltageConstraintAdder withVoltageCnec(String voltageCnecId) { + this.voltageCnecId = voltageCnecId; + return this; + } + + @Override + public T add() { + assertAttributeNotNull(instant, "OnInstant", "instant", "withInstant()"); + assertAttributeNotNull(voltageCnecId, "OnVoltageConstraint", "voltage cnec", "withVoltageCnec()"); + + if (instant.equals(Instant.OUTAGE)) { + throw new FaraoException("OnVoltageConstraint usage rules are not allowed for OUTAGE instant."); + } + if (instant.equals(Instant.PREVENTIVE)) { + owner.getCrac().addPreventiveState(); + } + // TODO: when Instant.AUTO will be handled by FARAO, consider adding some states in the CRAC here. + // not required as as soon as there is no RA on AUTO instant + + VoltageCnec voltageCnec = owner.getCrac().getVoltageCnec(voltageCnecId); + if (Objects.isNull(voltageCnec)) { + throw new FaraoException(String.format("VoltageCnec %s does not exist in crac. Consider adding it first.", voltageCnecId)); + } + + AbstractRemedialActionAdder.checkOnConstraintUsageRules(instant, voltageCnec); + + OnVoltageConstraint onVoltageConstraint = new OnVoltageConstraintImpl(instant, voltageCnec); + owner.addUsageRule(onVoltageConstraint); + return owner; + } +} diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java new file mode 100644 index 0000000000..65b7cce310 --- /dev/null +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_impl; + +import com.farao_community.farao.data.crac_api.Instant; +import com.farao_community.farao.data.crac_api.State; +import com.farao_community.farao.data.crac_api.cnec.AngleCnec; +import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; +import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; + +/** + * @author Fabrice Buscaylet {@literal } + */ +public class OnVoltageConstraintImpl extends AbstractUsageRule implements OnVoltageConstraint { + private final Instant instant; + + private final VoltageCnec voltageCnec; + + OnVoltageConstraintImpl(Instant instant, VoltageCnec angleCnec) { + super(UsageMethod.TO_BE_EVALUATED); + this.instant = instant; + this.voltageCnec = angleCnec; + } + + @Override + public VoltageCnec getVoltageCnec() { + return voltageCnec; + } + + @Override + public Instant getInstant() { + return instant; + } + + @Override + public UsageMethod getUsageMethod(State state) { + return UsageMethod.TO_BE_EVALUATED; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + OnVoltageConstraintImpl rule = (OnVoltageConstraintImpl) o; + return super.equals(o) && rule.getInstant().equals(instant) && rule.getVoltageCnec().equals(voltageCnec); + } + + @Override + public int hashCode() { + return voltageCnec.hashCode() * 19 + instant.hashCode() * 47; + } +} diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java new file mode 100644 index 0000000000..014dd845ad --- /dev/null +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2022, 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.farao_community.farao.data.crac_impl; + +import com.farao_community.farao.data.crac_api.Instant; +import com.farao_community.farao.data.crac_api.State; +import com.farao_community.farao.data.crac_api.cnec.AngleCnec; +import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; +import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Philippe Edwards {@literal } + */ +class OnVoltageConstraintImplTest { + VoltageCnec voltageCnec; + State preventiveState; + State curativeState; + + @BeforeEach + public void setUp() { + voltageCnec = Mockito.mock(VoltageCnec.class); + preventiveState = Mockito.mock(State.class); + Mockito.when(preventiveState.getInstant()).thenReturn(Instant.PREVENTIVE); + Mockito.when(preventiveState.isPreventive()).thenReturn(true); + curativeState = Mockito.mock(State.class); + Mockito.when(curativeState.getInstant()).thenReturn(Instant.CURATIVE); + Mockito.when(curativeState.isPreventive()).thenReturn(false); + } + + @Test + void testConstructor() { + OnVoltageConstraint onVoltageConstraint = new OnVoltageConstraintImpl(Instant.PREVENTIVE, voltageCnec); + + assertEquals(Instant.PREVENTIVE, onVoltageConstraint.getInstant()); + assertSame(voltageCnec, onVoltageConstraint.getVoltageCnec()); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod()); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState)); + } + + @Test + void testEquals() { + OnVoltageConstraint onVoltageConstraint1 = new OnVoltageConstraintImpl(Instant.PREVENTIVE, voltageCnec); + assertEquals(onVoltageConstraint1, onVoltageConstraint1); + assertEquals(onVoltageConstraint1.hashCode(), onVoltageConstraint1.hashCode()); + + assertNotNull(onVoltageConstraint1); + assertNotEquals(onVoltageConstraint1, Mockito.mock(OnInstantImpl.class)); + + OnVoltageConstraint onVoltageConstraint2 = new OnVoltageConstraintImpl(Instant.PREVENTIVE, voltageCnec); + assertEquals(onVoltageConstraint1, onVoltageConstraint2); + assertEquals(onVoltageConstraint1.hashCode(), onVoltageConstraint2.hashCode()); + + onVoltageConstraint2 = new OnVoltageConstraintImpl(Instant.CURATIVE, voltageCnec); + assertNotEquals(onVoltageConstraint1, onVoltageConstraint2); + assertNotEquals(onVoltageConstraint1.hashCode(), onVoltageConstraint2.hashCode()); + + onVoltageConstraint2 = new OnVoltageConstraintImpl(Instant.PREVENTIVE, Mockito.mock(VoltageCnec.class)); + assertNotEquals(onVoltageConstraint1, onVoltageConstraint2); + assertNotEquals(onVoltageConstraint1.hashCode(), onVoltageConstraint2.hashCode()); + } + + @Test + void testGetUsageMethod() { + State curativeState2 = Mockito.mock(State.class); + Mockito.when(curativeState2.getInstant()).thenReturn(Instant.CURATIVE); + Mockito.when(curativeState2.isPreventive()).thenReturn(false); + + OnVoltageConstraint onVoltageConstraint = new OnVoltageConstraintImpl(Instant.PREVENTIVE, voltageCnec); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState2)); + + Mockito.when(voltageCnec.getState()).thenReturn(curativeState); + onVoltageConstraint = new OnVoltageConstraintImpl(Instant.CURATIVE, voltageCnec); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState2)); + } +} diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/utils/ExhaustiveCracCreation.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/utils/ExhaustiveCracCreation.java index 739d5c506f..bfa3ab7f08 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/utils/ExhaustiveCracCreation.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/utils/ExhaustiveCracCreation.java @@ -222,6 +222,18 @@ public static Crac create(CracFactory cracFactory) { .newOnAngleConstraintUsageRule().withInstant(Instant.CURATIVE).withAngleCnec("angleCnecId").add() .add(); + crac.newPstRangeAction().withId("pstRange4Id") + .withName("pstRange4Name") + .withOperator("RTE") + .withNetworkElement("pst3") + .withGroupId("group-3-pst") + .withInitialTap(1) + .withTapToAngleConversionMap(Map.of(-3, 0., -2, .5, -1, 1., 0, 1.5, 1, 2., 2, 2.5, 3, 3.)) + .newTapRange().withRangeType(RangeType.ABSOLUTE).withMinTap(1).withMaxTap(7).add() + .newTapRange().withRangeType(RangeType.RELATIVE_TO_INITIAL_NETWORK).withMinTap(-3).withMaxTap(3).add() + .newOnVoltageConstraintUsageRule().withInstant(Instant.CURATIVE).withVoltageCnec("voltageCnecId").add() + .add(); + crac.newHvdcRangeAction().withId("hvdcRange1Id") .withName("hvdcRange1Name") .withOperator("RTE") diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java index 523d1c10d9..fc36595007 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java @@ -175,7 +175,7 @@ public Object answer(InvocationOnMock invocation) { return statuses.get(count - 1); } }).when(linearProblem).solve(); - } + } @Test void firstOptimizationFails() { From e5b0e1813b69b8357b9a0dafc0571437d964c47e Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 11 Jul 2023 14:15:44 +0200 Subject: [PATCH 02/12] [CSA-25] Removed unused imports --- .../farao/data/crac_impl/OnVoltageConstraintImpl.java | 2 -- .../farao/data/crac_impl/OnVoltageConstraintImplTest.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java index 65b7cce310..0e727f2c22 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java @@ -8,9 +8,7 @@ import com.farao_community.farao.data.crac_api.Instant; import com.farao_community.farao.data.crac_api.State; -import com.farao_community.farao.data.crac_api.cnec.AngleCnec; import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; -import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java index 014dd845ad..33bcf48885 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java @@ -8,9 +8,7 @@ import com.farao_community.farao.data.crac_api.Instant; import com.farao_community.farao.data.crac_api.State; -import com.farao_community.farao.data.crac_api.cnec.AngleCnec; import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; -import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import org.junit.jupiter.api.BeforeEach; From 255ff24e6371410646443b0be0285fde07b8f8aa Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 11 Jul 2023 14:19:16 +0200 Subject: [PATCH 03/12] [CSA-25] Fixed indentation level --- .../farao/data/crac_impl/OnVoltageConstraintImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java index 0e727f2c22..bd24f1ee96 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java @@ -38,7 +38,7 @@ public Instant getInstant() { @Override public UsageMethod getUsageMethod(State state) { - return UsageMethod.TO_BE_EVALUATED; + return UsageMethod.TO_BE_EVALUATED; } @Override From cde019849cd72013589a692c2e22ecb5dadfee2c Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 11 Jul 2023 14:49:57 +0200 Subject: [PATCH 04/12] [CSA-25] Fixed Unit Tests --- .../src/test/resources/retrocompatibility/v1/crac-v1.7.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json b/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json index 2e5b5b7abf..cf9fdd906a 100644 --- a/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json +++ b/data/crac-io/crac-io-json/src/test/resources/retrocompatibility/v1/crac-v1.7.json @@ -180,6 +180,7 @@ "id" : "pstRange1Id", "name" : "pstRange1Name", "operator" : "RTE", + "speed" : 10, "onInstantUsageRules" : [ { "instant" : "preventive", "usageMethod" : "available" @@ -291,11 +292,12 @@ "max" : 3, "rangeType" : "relativeToInitialNetwork" } ] - }], + } ], "hvdcRangeActions" : [ { "id" : "hvdcRange1Id", "name" : "hvdcRange1Name", "operator" : "RTE", + "speed" : 20, "onInstantUsageRules" : [ { "instant" : "preventive", "usageMethod" : "available" @@ -326,6 +328,7 @@ "id" : "injectionRange1Id", "name" : "injectionRange1Name", "operator" : null, + "speed" : 30, "onContingencyStateUsageRules" : [ { "instant" : "curative", "contingencyId" : "contingency1Id", @@ -348,6 +351,7 @@ "id" : "complexNetworkActionId", "name" : "complexNetworkActionName", "operator" : "RTE", + "speed" : 40, "onInstantUsageRules" : [ { "instant" : "preventive", "usageMethod" : "available" From d89c02c704670b72cee92e57af85962a7df7c0b3 Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 11 Jul 2023 14:58:01 +0200 Subject: [PATCH 05/12] [CSA-25] Fixed Linter Issues --- .../RemedialActionSeriesCreator.java | 18 ++++++++---------- ...JsonVoltageCnecsCreationParametersTest.java | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java index ec7c88e1a0..e9a57dff1d 100644 --- a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java +++ b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java @@ -12,7 +12,6 @@ import com.farao_community.farao.data.crac_api.Instant; import com.farao_community.farao.data.crac_api.RemedialActionAdder; import com.farao_community.farao.data.crac_api.cnec.AngleCnec; -import com.farao_community.farao.data.crac_api.cnec.Cnec; import com.farao_community.farao.data.crac_api.cnec.FlowCnec; import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; @@ -295,7 +294,7 @@ private boolean identifyAndReadPstRangeAction(String createdRemedialActionId, St private void addExtraUsageRules(String applicationModeMarketObjectStatus, String remedialActionId, RemedialActionAdder adder) { try { RemedialActionSeriesCreator.addUsageRules( - applicationModeMarketObjectStatus, adder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec, sharedDomain + applicationModeMarketObjectStatus, adder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain ); } catch (FaraoImportException e) { cracCreationContext.getCreationReport().warn(String.format("Extra usage rules for RA %s could not be imported: %s", remedialActionId, e.getMessage())); @@ -376,8 +375,9 @@ public static void addUsageRules(String applicationModeMarketObjectStatus, Set flowCnecs, AngleCnec angleCnec, Country sharedDomain) { - addUsageRules(applicationModeMarketObjectStatus,remedialActionAdder,contingencies,invalidContingencies,flowCnecs,angleCnec,null,sharedDomain); + addUsageRules(applicationModeMarketObjectStatus, remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, null, sharedDomain); } + public static void addUsageRules(String applicationModeMarketObjectStatus, RemedialActionAdder remedialActionAdder, List contingencies, @@ -387,17 +387,17 @@ public static void addUsageRules(String applicationModeMarketObjectStatus, VoltageCnec voltageCnec, Country sharedDomain) { if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec,sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.PREVENTIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec, sharedDomain, Instant.CURATIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA_AND_CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, null, null, flowCnecs, angleCnec,voltageCnec, sharedDomain, Instant.PREVENTIVE); - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec, sharedDomain, Instant.CURATIVE); + addUsageRulesAtInstant(remedialActionAdder, null, null, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.AUTO.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec,voltageCnec,sharedDomain, Instant.AUTO); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.AUTO); } } @@ -509,8 +509,6 @@ private static void addOnVoltageConstraintUsageRule(RemedialActionAdder adder .add(); } - - /*-------------- SERIES CHECKS ------------------------------*/ private boolean checkRemedialActionSeries(Series cimSerie) { // --- Check optimizationStatus diff --git a/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java b/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java index 06e3e0803d..383c38cedf 100644 --- a/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java +++ b/data/crac-creation/crac-creator-cim/src/test/java/com/farao_community/farao/data/crac_creation/creator/cim/parameters/JsonVoltageCnecsCreationParametersTest.java @@ -55,7 +55,7 @@ void roundTripOnVoltageMonitoringParameters() throws IOException { String exportedString = os.toString(); InputStream inputStream = getClass().getResourceAsStream("/parameters/voltage-cnecs-creation-parameters-for-round-trip.json"); - assertEquals(new String(inputStream.readAllBytes()).replaceAll("\r",""), exportedString.replaceAll("\r","")); + assertEquals(new String(inputStream.readAllBytes()).replaceAll("\r", ""), exportedString.replaceAll("\r", "")); } @ParameterizedTest From 124d128dddc1688e2f1de143ddc95d4326e99ae4 Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 11 Jul 2023 15:04:49 +0200 Subject: [PATCH 06/12] [CSA-25] Fixed Linter Issues --- .../algorithms/IteratingLinearOptimizerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java index fc36595007..523d1c10d9 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/farao_community/farao/search_tree_rao/linear_optimisation/algorithms/IteratingLinearOptimizerTest.java @@ -175,7 +175,7 @@ public Object answer(InvocationOnMock invocation) { return statuses.get(count - 1); } }).when(linearProblem).solve(); - } + } @Test void firstOptimizationFails() { From bfd409834a9e972247d1368e4220d630fa0a2489 Mon Sep 17 00:00:00 2001 From: Gabriel Plante Date: Thu, 20 Jul 2023 15:01:46 +0200 Subject: [PATCH 07/12] [CSA-25] Fixed version rule for voltage constraint --- .../deserializers/HvdcRangeActionArrayDeserializer.java | 6 +++--- .../InjectionRangeActionArrayDeserializer.java | 6 +++--- .../deserializers/NetworkActionArrayDeserializer.java | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java index 55554e6ee3..33da8097b3 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java @@ -83,11 +83,11 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } else { + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) > 6) { jsonParser.nextToken(); OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); + } else { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); } break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java index 4857c05b06..b10cf4339b 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java @@ -84,11 +84,11 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } else { + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) > 6) { jsonParser.nextToken(); OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); + } else { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); } break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java index becdce0534..02bdb75538 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java @@ -83,11 +83,11 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } else { + if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) > 6) { jsonParser.nextToken(); OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); + } else { + throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); } break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: From 2ba10dcbb9e75437c870b9e307fbdf9a89246e0f Mon Sep 17 00:00:00 2001 From: Gabriel Plante Date: Fri, 28 Jul 2023 12:34:38 +0200 Subject: [PATCH 08/12] [CSA-25] Fixed null pointer exception bug --- .../remedial_action/RemedialActionSeriesCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java index e9a57dff1d..809f78f022 100644 --- a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java +++ b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java @@ -393,7 +393,7 @@ public static void addUsageRules(String applicationModeMarketObjectStatus, addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA_AND_CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, null, null, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, new ArrayList<>(), new ArrayList<>(), flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.PREVENTIVE); addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.AUTO.getStatus())) { From 409ad893cdcd3b26d09098c8c92edce14efbd5c4 Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 1 Aug 2023 13:30:46 +0200 Subject: [PATCH 09/12] [CSA-25] Cumulative Fixes following code review on Pull Request #775 by MarthinBelthle --- .../RemedialActionSeriesCreator.java | 38 +++---------------- .../JsonSerializationConstants.java | 2 +- .../HvdcRangeActionArrayDeserializer.java | 8 +--- ...InjectionRangeActionArrayDeserializer.java | 8 +--- .../NetworkActionArrayDeserializer.java | 8 +--- .../OnVoltageConstraintArrayDeserializer.java | 2 +- .../PstRangeActionArrayDeserializer.java | 8 +--- .../OnVoltageConstraintSerializer.java | 4 +- .../JsonRetrocompatibilityTest.java | 22 +++++------ .../usage_rule/OnVoltageConstraint.java | 2 +- .../usage_rule/OnVoltageConstraintAdder.java | 2 +- .../OnVoltageConstraintAdderImpl.java | 2 +- .../crac_impl/OnVoltageConstraintImpl.java | 8 +++- .../OnVoltageConstraintImplTest.java | 14 +++---- .../json/JsonAngleMonitoringResultTest.java | 2 +- .../json/JsonVoltageMonitoringResultTest.java | 2 +- 16 files changed, 46 insertions(+), 86 deletions(-) diff --git a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java index 809f78f022..1c09eff7d8 100644 --- a/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java +++ b/data/crac-creation/crac-creator-cim/src/main/java/com/farao_community/farao/data/crac_creation/creator/cim/crac_creator/remedial_action/RemedialActionSeriesCreator.java @@ -13,7 +13,6 @@ import com.farao_community.farao.data.crac_api.RemedialActionAdder; import com.farao_community.farao.data.crac_api.cnec.AngleCnec; import com.farao_community.farao.data.crac_api.cnec.FlowCnec; -import com.farao_community.farao.data.crac_api.cnec.VoltageCnec; import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; import com.farao_community.farao.data.crac_creation.creator.api.ImportStatus; import com.farao_community.farao.data.crac_creation.creator.cim.crac_creator.CimCracCreationContext; @@ -47,8 +46,6 @@ public class RemedialActionSeriesCreator { private HvdcRangeActionCreator hvdcRangeActionCreator = null; private Set flowCnecs; private AngleCnec angleCnec; - - private VoltageCnec voltageCnec; private final CimCracCreationParameters cimCracCreationParameters; private final Map pstRangeActionCreators = new HashMap<>(); private final Map networkActionCreators = new HashMap<>(); @@ -294,7 +291,7 @@ private boolean identifyAndReadPstRangeAction(String createdRemedialActionId, St private void addExtraUsageRules(String applicationModeMarketObjectStatus, String remedialActionId, RemedialActionAdder adder) { try { RemedialActionSeriesCreator.addUsageRules( - applicationModeMarketObjectStatus, adder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain + applicationModeMarketObjectStatus, adder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain ); } catch (FaraoImportException e) { cracCreationContext.getCreationReport().warn(String.format("Extra usage rules for RA %s could not be imported: %s", remedialActionId, e.getMessage())); @@ -375,29 +372,18 @@ public static void addUsageRules(String applicationModeMarketObjectStatus, Set flowCnecs, AngleCnec angleCnec, Country sharedDomain) { - addUsageRules(applicationModeMarketObjectStatus, remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, null, sharedDomain); - } - - public static void addUsageRules(String applicationModeMarketObjectStatus, - RemedialActionAdder remedialActionAdder, - List contingencies, - List invalidContingencies, - Set flowCnecs, - AngleCnec angleCnec, - VoltageCnec voltageCnec, - Country sharedDomain) { if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.PREVENTIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.CURATIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.PRA_AND_CRA.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, new ArrayList<>(), new ArrayList<>(), flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.PREVENTIVE); - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.CURATIVE); + addUsageRulesAtInstant(remedialActionAdder, null, null, flowCnecs, angleCnec, sharedDomain, Instant.PREVENTIVE); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.CURATIVE); } if (applicationModeMarketObjectStatus.equals(ApplicationModeMarketObjectStatus.AUTO.getStatus())) { - addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, voltageCnec, sharedDomain, Instant.AUTO); + addUsageRulesAtInstant(remedialActionAdder, contingencies, invalidContingencies, flowCnecs, angleCnec, sharedDomain, Instant.AUTO); } } @@ -406,7 +392,6 @@ private static void addUsageRulesAtInstant(RemedialActionAdder remedialAction List invalidContingencies, Set flowCnecs, AngleCnec angleCnec, - VoltageCnec voltageCnec, Country sharedDomain, Instant instant) { if (!flowCnecs.isEmpty()) { @@ -417,10 +402,6 @@ private static void addUsageRulesAtInstant(RemedialActionAdder remedialAction addOnAngleConstraintUsageRule(remedialActionAdder, angleCnec); return; } - if (Objects.nonNull(voltageCnec)) { - addOnVoltageConstraintUsageRule(remedialActionAdder, voltageCnec); - return; - } if (!Objects.isNull(sharedDomain)) { remedialActionAdder.newOnFlowConstraintInCountryUsageRule().withInstant(instant).withCountry(sharedDomain).add(); return; @@ -502,13 +483,6 @@ private static void addOnAngleConstraintUsageRule(RemedialActionAdder adder, .add(); } - private static void addOnVoltageConstraintUsageRule(RemedialActionAdder adder, VoltageCnec voltageCnec) { - adder.newOnVoltageConstraintUsageRule() - .withVoltageCnec(voltageCnec.getId()) - .withInstant(Instant.CURATIVE) - .add(); - } - /*-------------- SERIES CHECKS ------------------------------*/ private boolean checkRemedialActionSeries(Series cimSerie) { // --- Check optimizationStatus diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java index e64a8c25cd..0d386c8d07 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/JsonSerializationConstants.java @@ -37,7 +37,7 @@ private JsonSerializationConstants() { v1.3: addition of hvdcRangeAction's and injectionRangeAction's initial setpoints v1.4: addition of AngleCnecs; frm renamed to reliabilityMargin v1.5: addition of VoltageCnecs - v1.7 addition of VoltageConstraints + v1.7: addition of VoltageConstraints usage rules */ // headers diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java index 33da8097b3..fdb1fbea5d 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/HvdcRangeActionArrayDeserializer.java @@ -83,12 +83,8 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) > 6) { - jsonParser.nextToken(); - OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); - } else { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, hvdcRangeActionAdder); break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java index b10cf4339b..64a9fde424 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/InjectionRangeActionArrayDeserializer.java @@ -84,12 +84,8 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) > 6) { - jsonParser.nextToken(); - OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); - } else { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, injectionRangeActionAdder); break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java index 02bdb75538..26cd02f9e2 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/NetworkActionArrayDeserializer.java @@ -83,12 +83,8 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) > 6) { - jsonParser.nextToken(); - OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); - } else { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, networkActionAdder); break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java index 34581ab7de..dcd01bf6b5 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/OnVoltageConstraintArrayDeserializer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java index e8af268c00..5c123f61f5 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/deserializers/PstRangeActionArrayDeserializer.java @@ -85,12 +85,8 @@ public static void deserialize(JsonParser jsonParser, DeserializationContext des OnAngleConstraintArrayDeserializer.deserialize(jsonParser, pstRangeActionAdder); break; case ON_VOLTAGE_CONSTRAINT_USAGE_RULES: - if (getPrimaryVersionNumber(version) > 1 || getSubVersionNumber(version) < 7) { - throw new FaraoException("onVoltageConstraintUsageRules does not exists before CRAC version 1.7"); - } else { - jsonParser.nextToken(); - OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, pstRangeActionAdder); - } + jsonParser.nextToken(); + OnVoltageConstraintArrayDeserializer.deserialize(jsonParser, pstRangeActionAdder); break; case ON_FLOW_CONSTRAINT_IN_COUNTRY_USAGE_RULES: jsonParser.nextToken(); diff --git a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java index 9fb0fdc169..29fac63a8f 100644 --- a/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java +++ b/data/crac-io/crac-io-json/src/main/java/com/farao_community/farao/data/crac_io_json/serializers/OnVoltageConstraintSerializer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. @@ -16,7 +16,7 @@ import static com.farao_community.farao.data.crac_io_json.JsonSerializationConstants.*; /** - * @author Peter Mitri {@literal } + * @author Fabrice Buscaylet {@literal } */ public class OnVoltageConstraintSerializer extends AbstractJsonSerializer { @Override diff --git a/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java b/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java index c397653f63..8909d16af3 100644 --- a/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java +++ b/data/crac-io/crac-io-json/src/test/java/com/farao_community/farao/data/crac_io_json/JsonRetrocompatibilityTest.java @@ -28,13 +28,11 @@ import java.io.InputStream; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import static com.farao_community.farao.data.crac_api.Instant.*; import static com.farao_community.farao.data.crac_api.usage_rule.UsageMethod.AVAILABLE; import static com.farao_community.farao.data.crac_api.usage_rule.UsageMethod.FORCED; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Baptiste Seguinot {@literal } @@ -399,7 +397,7 @@ private void testContentOfV1Point0Crac(Crac crac) { assertEquals(Unit.MEGAWATT, hvdcRange.getUnit()); // check usage rules - assertEquals(4, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnInstant.class::isInstance).collect(Collectors.toList()).size()); + assertEquals(4, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnInstant.class::isInstance).count()); } void testContentOfV1Point1Crac(Crac crac) { @@ -437,8 +435,8 @@ void testContentOfV1Point2Crac(Crac crac) { assertEquals(2, crac.getInjectionRangeAction("injectionRange1Id").getRanges().size()); // check usage rules - assertEquals(3, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnContingencyState.class::isInstance).collect(Collectors.toList()).size()); - assertEquals(3, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnFlowConstraint.class::isInstance).collect(Collectors.toList()).size()); + assertEquals(3, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnContingencyState.class::isInstance).count()); + assertEquals(3, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnFlowConstraint.class::isInstance).count()); } void testContentOfV1Point3Crac(Crac crac) { @@ -480,7 +478,7 @@ void testContentOfV1Point4Crac(Crac crac) { assertEquals(CURATIVE, onAngleConstraint.getInstant()); // check usage rules - assertEquals(1, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnAngleConstraint.class::isInstance).collect(Collectors.toList()).size()); + assertEquals(1, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnAngleConstraint.class::isInstance).count()); } void testContentOfV1Point5Crac(Crac crac) { @@ -508,10 +506,10 @@ void testContentOfV1Point6Crac(Crac crac) { testContentOfV1Point5Crac(crac); // test usage rules - assertEquals(4, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnInstant.class::isInstance).collect(Collectors.toList()).size()); - assertEquals(3, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnContingencyState.class::isInstance).collect(Collectors.toList()).size()); - assertEquals(3, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnFlowConstraint.class::isInstance).collect(Collectors.toList()).size()); - assertEquals(1, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnAngleConstraint.class::isInstance).collect(Collectors.toList()).size()); + assertEquals(4, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnInstant.class::isInstance).count()); + assertEquals(3, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnContingencyState.class::isInstance).count()); + assertEquals(3, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnFlowConstraint.class::isInstance).count()); + assertEquals(1, (int) crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnAngleConstraint.class::isInstance).count()); // test speed assertEquals(10, crac.getPstRangeAction("pstRange1Id").getSpeed().get().intValue()); assertEquals(20, crac.getHvdcRangeAction("hvdcRange1Id").getSpeed().get().intValue()); @@ -522,7 +520,7 @@ void testContentOfV1Point6Crac(Crac crac) { void testContentOfV1Point7Crac(Crac crac) { testContentOfV1Point6Crac(crac); - // test usage rules - assertEquals(1, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnVoltageConstraint.class::isInstance).collect(Collectors.toList()).size()); + // test new voltage constraint usage rules + assertEquals(1, crac.getRemedialActions().stream().map(RemedialAction::getUsageRules).flatMap(List::stream).filter(OnVoltageConstraint.class::isInstance).count()); } } diff --git a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java index 8cdb8d6d04..c46223f8c1 100644 --- a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java +++ b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. diff --git a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java index 845c3013c1..15b6f2f2e3 100644 --- a/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java +++ b/data/crac/crac-api/src/main/java/com/farao_community/farao/data/crac_api/usage_rule/OnVoltageConstraintAdder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java index 4befbbe7ec..bbfb9aa504 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java index bd24f1ee96..7ac7c5f31e 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. @@ -38,7 +38,11 @@ public Instant getInstant() { @Override public UsageMethod getUsageMethod(State state) { - return UsageMethod.TO_BE_EVALUATED; + if (state.isPreventive()) { + return state.getInstant().equals(instant) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + } else { + return state.getInstant().equals(instant) && state.equals(this.voltageCnec.getState()) ? UsageMethod.TO_BE_EVALUATED : UsageMethod.UNDEFINED; + } } @Override diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java index 33bcf48885..7a23757536 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, RTE (http://www.rte-france.com) + * 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/. @@ -18,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.*; /** - * @author Philippe Edwards {@literal } + * @author Fabrice Buscaylet {@literal } */ class OnVoltageConstraintImplTest { VoltageCnec voltageCnec; @@ -44,7 +44,7 @@ void testConstructor() { assertSame(voltageCnec, onVoltageConstraint.getVoltageCnec()); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod()); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(preventiveState)); - assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.UNDEFINED, onVoltageConstraint.getUsageMethod(curativeState)); } @Test @@ -77,13 +77,13 @@ void testGetUsageMethod() { OnVoltageConstraint onVoltageConstraint = new OnVoltageConstraintImpl(Instant.PREVENTIVE, voltageCnec); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(preventiveState)); - assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState)); - assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState2)); + assertEquals(UsageMethod.UNDEFINED, onVoltageConstraint.getUsageMethod(curativeState)); + assertEquals(UsageMethod.UNDEFINED, onVoltageConstraint.getUsageMethod(curativeState2)); Mockito.when(voltageCnec.getState()).thenReturn(curativeState); onVoltageConstraint = new OnVoltageConstraintImpl(Instant.CURATIVE, voltageCnec); - assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(preventiveState)); + assertEquals(UsageMethod.UNDEFINED, onVoltageConstraint.getUsageMethod(preventiveState)); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState)); - assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(curativeState2)); + assertEquals(UsageMethod.UNDEFINED, onVoltageConstraint.getUsageMethod(curativeState2)); } } diff --git a/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java b/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java index 616cfa3d9b..4321835e64 100644 --- a/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java +++ b/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java @@ -102,7 +102,7 @@ void testRoundTrip() throws IOException { OutputStream os = new ByteArrayOutputStream(); new AngleMonitoringResultExporter().export(angleMonitoringResult, os); String expected = new String(getClass().getResourceAsStream("/result-roundTrip.json").readAllBytes()); - assertEquals(expected, os.toString()); + assertEquals(expected, os.toString().replaceAll("\r", "")); } private boolean compareAngleResults(AngleMonitoringResult.AngleResult ar1, AngleMonitoringResult.AngleResult ar2) { diff --git a/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java b/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java index 939bb09817..c3acaabcc4 100644 --- a/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java +++ b/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java @@ -78,7 +78,7 @@ void testRoundTrip() throws IOException { OutputStream os = new ByteArrayOutputStream(); new VoltageMonitoringResultExporter().export(voltageMonitoringResult, os); String expected = new String(getClass().getResourceAsStream("/result.json").readAllBytes()); - assertEquals(expected, os.toString()); + assertEquals(expected, os.toString().replaceAll("\r", "")); } @ParameterizedTest From 0c3624c35c02e708927f73ad3c6776a0cd33b761 Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 1 Aug 2023 13:41:44 +0200 Subject: [PATCH 10/12] [CSA-25] Fixed Line Feed / Carriage Returns issues on windows hosts --- .../angle_monitoring/json/JsonAngleMonitoringResultTest.java | 2 +- .../json/JsonVoltageMonitoringResultTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java b/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java index 4321835e64..4c375dae0d 100644 --- a/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java +++ b/monitoring/angle-monitoring/src/test/java/com/farao_community/farao/monitoring/angle_monitoring/json/JsonAngleMonitoringResultTest.java @@ -102,7 +102,7 @@ void testRoundTrip() throws IOException { OutputStream os = new ByteArrayOutputStream(); new AngleMonitoringResultExporter().export(angleMonitoringResult, os); String expected = new String(getClass().getResourceAsStream("/result-roundTrip.json").readAllBytes()); - assertEquals(expected, os.toString().replaceAll("\r", "")); + assertEquals(expected.replaceAll("\r", ""), os.toString().replaceAll("\r", "")); } private boolean compareAngleResults(AngleMonitoringResult.AngleResult ar1, AngleMonitoringResult.AngleResult ar2) { diff --git a/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java b/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java index c3acaabcc4..7eb5447e03 100644 --- a/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java +++ b/monitoring/voltage-monitoring/src/test/java/com/farao_community/farao/monitoring/voltage_monitoring/json/JsonVoltageMonitoringResultTest.java @@ -78,7 +78,7 @@ void testRoundTrip() throws IOException { OutputStream os = new ByteArrayOutputStream(); new VoltageMonitoringResultExporter().export(voltageMonitoringResult, os); String expected = new String(getClass().getResourceAsStream("/result.json").readAllBytes()); - assertEquals(expected, os.toString().replaceAll("\r", "")); + assertEquals(expected.replaceAll("\r", ""), os.toString().replaceAll("\r", "")); } @ParameterizedTest From b818d73ae649308339d2ead99f33bb88dd0c18c5 Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Tue, 1 Aug 2023 15:23:35 +0200 Subject: [PATCH 11/12] [CSA-25] Added unit test on VoltageConstraintAdder for increased code coverage --- .../OnVoltageConstraintAdderImplTest.java | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java new file mode 100644 index 0000000000..0804be29eb --- /dev/null +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java @@ -0,0 +1,169 @@ +/* + * 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.farao_community.farao.data.crac_impl; + +import com.farao_community.farao.commons.FaraoException; +import com.farao_community.farao.commons.Unit; +import com.farao_community.farao.data.crac_api.Crac; +import com.farao_community.farao.data.crac_api.Instant; +import com.farao_community.farao.data.crac_api.RemedialAction; +import com.farao_community.farao.data.crac_api.cnec.VoltageCnecAdder; +import com.farao_community.farao.data.crac_api.network_action.ActionType; +import com.farao_community.farao.data.crac_api.network_action.NetworkActionAdder; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; +import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraintAdder; +import com.farao_community.farao.data.crac_api.usage_rule.UsageMethod; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * @author Fabrice Buscaylet {@literal } + */ +class OnVoltageConstraintAdderImplTest { + private Crac crac; + private NetworkActionAdder remedialActionAdder; + + @BeforeEach + public void setUp() { + crac = new CracImplFactory().create("cracId"); + + crac.newContingency() + .withId("Contingency FR1 FR3") + .withName("Trip of FFR1AA1 FFR3AA1 1") + .withNetworkElement("FFR1AA1 FFR3AA1 1") + .add(); + + crac.newVoltageCnec() + .withId("cnec2stateCurativeContingency1") + .withInstant(Instant.CURATIVE) + .withContingency("Contingency FR1 FR3") + .withNetworkElement("FFR1AA1") + .withOperator("operator2") + .withOptimized(false) + .withReliabilityMargin(55.0) + .newThreshold().withUnit(Unit.KILOVOLT).withMin(-1500.).withMax(1500.).add() + .add(); + + remedialActionAdder = crac.newNetworkAction() + .withId("networkActionId") + .withName("networkActionName") + .withOperator("operator") + .newTopologicalAction().withActionType(ActionType.OPEN).withNetworkElement("action-elementId").add(); + } + + @Test + void testOkPreventive() { + RemedialAction remedialAction = remedialActionAdder.newOnVoltageConstraintUsageRule() + .withInstant(Instant.PREVENTIVE) + .withVoltageCnec("cnec2stateCurativeContingency1") + .add() + .add(); + + assertEquals(1, remedialAction.getUsageRules().size()); + assertTrue(remedialAction.getUsageRules().get(0) instanceof OnVoltageConstraint); + OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) remedialAction.getUsageRules().get(0); + assertEquals(Instant.PREVENTIVE, onVoltageConstraint.getInstant()); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod()); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(crac.getPreventiveState())); + assertEquals(UsageMethod.UNDEFINED, onVoltageConstraint.getUsageMethod(crac.getState(crac.getContingency("Contingency FR1 FR3"), Instant.CURATIVE))); + assertEquals(2, crac.getStates().size()); + assertNotNull(crac.getPreventiveState()); + } + + @Test + void testOkCurative() { + RemedialAction remedialAction = remedialActionAdder.newOnVoltageConstraintUsageRule() + .withInstant(Instant.CURATIVE) + .withVoltageCnec("cnec2stateCurativeContingency1") + .add() + .add(); + + assertEquals(1, remedialAction.getUsageRules().size()); + assertTrue(remedialAction.getUsageRules().get(0) instanceof OnVoltageConstraint); + OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) remedialAction.getUsageRules().get(0); + assertEquals(Instant.CURATIVE, onVoltageConstraint.getInstant()); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod()); + assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(crac.getState(crac.getContingency("Contingency FR1 FR3"), Instant.CURATIVE))); + assertEquals(1, crac.getStates().size()); + } + + @Test + void testOutageException() { + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.OUTAGE).withVoltageCnec("cnec2stateCurativeContingency1"); + assertThrows(FaraoException.class, adder::add); + } + + @Test + void testAbsentCnecException() { + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE) + .withVoltageCnec("fake_cnec"); + assertThrows(FaraoException.class, adder::add); + } + + @Test + void testNoCnecException() { + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE); + assertThrows(FaraoException.class, adder::add); + } + + @Test + void testNoInstantException() { + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withVoltageCnec("cnec2stateCurativeContingency1"); + assertThrows(FaraoException.class, adder::add); + } + + private void addCnec(String id, Instant instant) { + VoltageCnecAdder adder = crac.newVoltageCnec() + .withId(id) + .withInstant(instant) + .withOperator("operator2") + .withNetworkElement(id) + .newThreshold().withUnit(Unit.KILOVOLT).withMin(-1500.).withMax(1500.).add(); + if (!instant.equals(Instant.PREVENTIVE)) { + adder.withContingency("Contingency FR1 FR3"); + } + adder.add(); + } + + @Test + void testOnConstraintInstantCheck() { + + addCnec("cnec-prev", Instant.PREVENTIVE); + addCnec("cnec-out", Instant.OUTAGE); + addCnec("cnec-auto", Instant.AUTO); + addCnec("cnec-cur", Instant.CURATIVE); + + OnVoltageConstraintAdder adder; + + // PREVENTIVE RA + remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE).withVoltageCnec("cnec-prev").add(); // ok + remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE).withVoltageCnec("cnec-out").add(); // ok + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE).withVoltageCnec("cnec-auto"); // nok + assertThrows(FaraoException.class, adder::add); + remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE).withVoltageCnec("cnec-cur").add(); // ok + + // AUTO RA + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.AUTO).withVoltageCnec("cnec-prev"); // nok + assertThrows(FaraoException.class, adder::add); + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.AUTO).withVoltageCnec("cnec-out"); // nok + assertThrows(FaraoException.class, adder::add); + remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.AUTO).withVoltageCnec("cnec-auto").add(); // ok + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.AUTO).withVoltageCnec("cnec-cur"); // nok + assertThrows(FaraoException.class, adder::add); + + // CURATIVE RA + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.CURATIVE).withVoltageCnec("cnec-prev"); // nok + assertThrows(FaraoException.class, adder::add); + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.CURATIVE).withVoltageCnec("cnec-out"); // nok + assertThrows(FaraoException.class, adder::add); + adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.CURATIVE).withVoltageCnec("cnec-auto"); // nok + assertThrows(FaraoException.class, adder::add); + remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.CURATIVE).withVoltageCnec("cnec-cur").add(); // ok + } +} From c6bcc115727cedea5df22b877a370d9f78f81366 Mon Sep 17 00:00:00 2001 From: Fabrice Buscaylet Date: Thu, 3 Aug 2023 11:56:10 +0200 Subject: [PATCH 12/12] [CSA-25] Cumulative enhancements --- .../AbstractRemedialActionAdder.java | 6 ++--- .../crac_impl/OnAngleConstraintAdderImpl.java | 2 -- .../crac_impl/OnFlowConstraintAdderImpl.java | 2 -- .../data/crac_impl/OnInstantAdderImpl.java | 2 -- .../OnVoltageConstraintAdderImpl.java | 2 -- .../OnAngleConstraintAdderImplTest.java | 26 +++++++++---------- .../OnVoltageConstraintAdderImplTest.java | 26 +++++++++---------- 7 files changed, 29 insertions(+), 37 deletions(-) diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java index 9637561283..316424b57d 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/AbstractRemedialActionAdder.java @@ -81,9 +81,9 @@ CracImpl getCrac() { } static void checkOnConstraintUsageRules(Instant instant, Cnec cnec) { - // Only allow PRAs with usage method OnFlowConstraint/OnAngleConstraint, for CNECs of instants PREVENTIVE & OUTAGE & CURATIVE - // Only allow ARAs with usage method OnFlowConstraint/OnAngleConstraint, for CNECs of instant AUTO - // Only allow CRAs with usage method OnFlowConstraint/OnAngleConstraint, for CNECs of instant CURATIVE + // Only allow PRAs with usage method OnFlowConstraint/OnAngleConstraint/OnVoltageConstraint, for CNECs of instants PREVENTIVE & OUTAGE & CURATIVE + // Only allow ARAs with usage method OnFlowConstraint/OnAngleConstraint/OnVoltageConstraint, for CNECs of instant AUTO + // Only allow CRAs with usage method OnFlowConstraint/OnAngleConstraint/OnVoltageConstraint, for CNECs of instant CURATIVE Map> allowedCnecInstantPerRaInstant = Map.of( Instant.PREVENTIVE, Set.of(Instant.PREVENTIVE, Instant.OUTAGE, Instant.CURATIVE), diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImpl.java index f237e231b5..aed27bd7e3 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImpl.java @@ -52,8 +52,6 @@ public T add() { if (instant.equals(Instant.PREVENTIVE)) { owner.getCrac().addPreventiveState(); } - // TODO: when Instant.AUTO will be handled by FARAO, consider adding some states in the CRAC here. - // not required as as soon as there is no RA on AUTO instant AngleCnec angleCnec = owner.getCrac().getAngleCnec(angleCnecId); if (Objects.isNull(angleCnec)) { diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintAdderImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintAdderImpl.java index c46eebd3ee..22a3824c27 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintAdderImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnFlowConstraintAdderImpl.java @@ -52,8 +52,6 @@ public T add() { if (instant.equals(Instant.PREVENTIVE)) { owner.getCrac().addPreventiveState(); } - // TODO: when Instant.AUTO will be handled by FARAO, consider adding some states in the CRAC here. - // not required as as soon as there is no RA on AUTO instant FlowCnec flowCnec = owner.getCrac().getFlowCnec(flowCnecId); if (Objects.isNull(flowCnec)) { diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnInstantAdderImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnInstantAdderImpl.java index ee27a5b27e..3f2a398aba 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnInstantAdderImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnInstantAdderImpl.java @@ -51,8 +51,6 @@ public T add() { if (instant.equals(Instant.PREVENTIVE)) { owner.getCrac().addPreventiveState(); } - // TODO: when Instant.AUTO will be handled by FARAO, consider adding some states in the CRAC here. - // not required as as soon as there is no RA on AUTO instant OnInstant onInstant = new OnInstantImpl(usageMethod, instant); owner.addUsageRule(onInstant); diff --git a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java index bbfb9aa504..90ab7fffd2 100644 --- a/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java +++ b/data/crac/crac-impl/src/main/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImpl.java @@ -52,8 +52,6 @@ public T add() { if (instant.equals(Instant.PREVENTIVE)) { owner.getCrac().addPreventiveState(); } - // TODO: when Instant.AUTO will be handled by FARAO, consider adding some states in the CRAC here. - // not required as as soon as there is no RA on AUTO instant VoltageCnec voltageCnec = owner.getCrac().getVoltageCnec(voltageCnecId); if (Objects.isNull(voltageCnec)) { diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImplTest.java index 556ac262c4..f576894118 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnAngleConstraintAdderImplTest.java @@ -10,9 +10,9 @@ import com.farao_community.farao.commons.Unit; import com.farao_community.farao.data.crac_api.Crac; import com.farao_community.farao.data.crac_api.Instant; -import com.farao_community.farao.data.crac_api.RemedialAction; import com.farao_community.farao.data.crac_api.cnec.AngleCnecAdder; import com.farao_community.farao.data.crac_api.network_action.ActionType; +import com.farao_community.farao.data.crac_api.network_action.NetworkAction; import com.farao_community.farao.data.crac_api.network_action.NetworkActionAdder; import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraint; import com.farao_community.farao.data.crac_api.usage_rule.OnAngleConstraintAdder; @@ -58,15 +58,15 @@ public void setUp() { @Test void testOkPreventive() { - RemedialAction remedialAction = remedialActionAdder.newOnAngleConstraintUsageRule() + NetworkAction networkAction = remedialActionAdder.newOnAngleConstraintUsageRule() .withInstant(Instant.PREVENTIVE) .withAngleCnec("cnec2stateCurativeContingency1") .add() .add(); - assertEquals(1, remedialAction.getUsageRules().size()); - assertTrue(remedialAction.getUsageRules().get(0) instanceof OnAngleConstraint); - OnAngleConstraint onAngleConstraint = (OnAngleConstraint) remedialAction.getUsageRules().get(0); + assertEquals(1, networkAction.getUsageRules().size()); + assertTrue(networkAction.getUsageRules().get(0) instanceof OnAngleConstraint); + OnAngleConstraint onAngleConstraint = (OnAngleConstraint) networkAction.getUsageRules().get(0); assertEquals(Instant.PREVENTIVE, onAngleConstraint.getInstant()); assertEquals(UsageMethod.TO_BE_EVALUATED, onAngleConstraint.getUsageMethod()); assertEquals(UsageMethod.TO_BE_EVALUATED, onAngleConstraint.getUsageMethod(crac.getPreventiveState())); @@ -77,15 +77,15 @@ void testOkPreventive() { @Test void testOkCurative() { - RemedialAction remedialAction = remedialActionAdder.newOnAngleConstraintUsageRule() + NetworkAction networkAction = remedialActionAdder.newOnAngleConstraintUsageRule() .withInstant(Instant.CURATIVE) .withAngleCnec("cnec2stateCurativeContingency1") .add() .add(); - assertEquals(1, remedialAction.getUsageRules().size()); - assertTrue(remedialAction.getUsageRules().get(0) instanceof OnAngleConstraint); - OnAngleConstraint onAngleConstraint = (OnAngleConstraint) remedialAction.getUsageRules().get(0); + assertEquals(1, networkAction.getUsageRules().size()); + assertTrue(networkAction.getUsageRules().get(0) instanceof OnAngleConstraint); + OnAngleConstraint onAngleConstraint = (OnAngleConstraint) networkAction.getUsageRules().get(0); assertEquals(Instant.CURATIVE, onAngleConstraint.getInstant()); assertEquals(UsageMethod.TO_BE_EVALUATED, onAngleConstraint.getUsageMethod()); assertEquals(UsageMethod.TO_BE_EVALUATED, onAngleConstraint.getUsageMethod(crac.getState(crac.getContingency("Contingency FR1 FR3"), Instant.CURATIVE))); @@ -94,26 +94,26 @@ void testOkCurative() { @Test void testOutageException() { - OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withInstant(Instant.OUTAGE).withAngleCnec("cnec2stateCurativeContingency1"); + OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withInstant(Instant.OUTAGE).withAngleCnec("cnec2stateCurativeContingency1"); assertThrows(FaraoException.class, adder::add); } @Test void testAbsentCnecException() { - OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withInstant(Instant.PREVENTIVE) + OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withInstant(Instant.PREVENTIVE) .withAngleCnec("fake_cnec"); assertThrows(FaraoException.class, adder::add); } @Test void testNoCnecException() { - OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withInstant(Instant.PREVENTIVE); + OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withInstant(Instant.PREVENTIVE); assertThrows(FaraoException.class, adder::add); } @Test void testNoInstantException() { - OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withAngleCnec("cnec2stateCurativeContingency1"); + OnAngleConstraintAdder adder = remedialActionAdder.newOnAngleConstraintUsageRule().withAngleCnec("cnec2stateCurativeContingency1"); assertThrows(FaraoException.class, adder::add); } diff --git a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java index 0804be29eb..0f1d6b6d81 100644 --- a/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/farao_community/farao/data/crac_impl/OnVoltageConstraintAdderImplTest.java @@ -10,9 +10,9 @@ import com.farao_community.farao.commons.Unit; import com.farao_community.farao.data.crac_api.Crac; import com.farao_community.farao.data.crac_api.Instant; -import com.farao_community.farao.data.crac_api.RemedialAction; import com.farao_community.farao.data.crac_api.cnec.VoltageCnecAdder; import com.farao_community.farao.data.crac_api.network_action.ActionType; +import com.farao_community.farao.data.crac_api.network_action.NetworkAction; import com.farao_community.farao.data.crac_api.network_action.NetworkActionAdder; import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraint; import com.farao_community.farao.data.crac_api.usage_rule.OnVoltageConstraintAdder; @@ -59,15 +59,15 @@ public void setUp() { @Test void testOkPreventive() { - RemedialAction remedialAction = remedialActionAdder.newOnVoltageConstraintUsageRule() + NetworkAction networkAction = remedialActionAdder.newOnVoltageConstraintUsageRule() .withInstant(Instant.PREVENTIVE) .withVoltageCnec("cnec2stateCurativeContingency1") .add() .add(); - assertEquals(1, remedialAction.getUsageRules().size()); - assertTrue(remedialAction.getUsageRules().get(0) instanceof OnVoltageConstraint); - OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) remedialAction.getUsageRules().get(0); + assertEquals(1, networkAction.getUsageRules().size()); + assertTrue(networkAction.getUsageRules().get(0) instanceof OnVoltageConstraint); + OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) networkAction.getUsageRules().get(0); assertEquals(Instant.PREVENTIVE, onVoltageConstraint.getInstant()); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod()); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(crac.getPreventiveState())); @@ -78,15 +78,15 @@ void testOkPreventive() { @Test void testOkCurative() { - RemedialAction remedialAction = remedialActionAdder.newOnVoltageConstraintUsageRule() + NetworkAction networkAction = remedialActionAdder.newOnVoltageConstraintUsageRule() .withInstant(Instant.CURATIVE) .withVoltageCnec("cnec2stateCurativeContingency1") .add() .add(); - assertEquals(1, remedialAction.getUsageRules().size()); - assertTrue(remedialAction.getUsageRules().get(0) instanceof OnVoltageConstraint); - OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) remedialAction.getUsageRules().get(0); + assertEquals(1, networkAction.getUsageRules().size()); + assertTrue(networkAction.getUsageRules().get(0) instanceof OnVoltageConstraint); + OnVoltageConstraint onVoltageConstraint = (OnVoltageConstraint) networkAction.getUsageRules().get(0); assertEquals(Instant.CURATIVE, onVoltageConstraint.getInstant()); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod()); assertEquals(UsageMethod.TO_BE_EVALUATED, onVoltageConstraint.getUsageMethod(crac.getState(crac.getContingency("Contingency FR1 FR3"), Instant.CURATIVE))); @@ -95,26 +95,26 @@ void testOkCurative() { @Test void testOutageException() { - OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.OUTAGE).withVoltageCnec("cnec2stateCurativeContingency1"); + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.OUTAGE).withVoltageCnec("cnec2stateCurativeContingency1"); assertThrows(FaraoException.class, adder::add); } @Test void testAbsentCnecException() { - OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE) + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE) .withVoltageCnec("fake_cnec"); assertThrows(FaraoException.class, adder::add); } @Test void testNoCnecException() { - OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE); + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withInstant(Instant.PREVENTIVE); assertThrows(FaraoException.class, adder::add); } @Test void testNoInstantException() { - OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withVoltageCnec("cnec2stateCurativeContingency1"); + OnVoltageConstraintAdder adder = remedialActionAdder.newOnVoltageConstraintUsageRule().withVoltageCnec("cnec2stateCurativeContingency1"); assertThrows(FaraoException.class, adder::add); }