From 495303b3fe3c8768f8d9d699aa9d1956051b6752 Mon Sep 17 00:00:00 2001 From: Thomas Bouquet <63302082+bqth29@users.noreply.github.com> Date: Thu, 2 May 2024 11:11:40 +0200 Subject: [PATCH] Fix getUsageMethod bug introduced by #979 (#989) --- .../data/cracimpl/AbstractRemedialAction.java | 12 ++- .../data/cracimpl/RemedialActionTest.java | 83 +++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/AbstractRemedialAction.java b/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/AbstractRemedialAction.java index 5eadbd4c5f..c1e832bd19 100644 --- a/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/AbstractRemedialAction.java +++ b/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/AbstractRemedialAction.java @@ -88,13 +88,19 @@ private void computeUsageMethodPerStateAndInstant() { updateMapWithValue(usageMethodPerInstant, usageRule.getInstant(), usageRule.getUsageMethod()); } else if (usageRule instanceof OnFlowConstraint ofc) { State state = ofc.getFlowCnec().getState(); - updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); + if (usageRule.getInstant().equals(state.getInstant())) { + updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); + } } else if (usageRule instanceof OnAngleConstraint oac) { State state = oac.getAngleCnec().getState(); - updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); + if (usageRule.getInstant().equals(state.getInstant())) { + updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); + } } else if (usageRule instanceof OnVoltageConstraint ovc) { State state = ovc.getVoltageCnec().getState(); - updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); + if (usageRule.getInstant().equals(state.getInstant())) { + updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); + } } else if (usageRule instanceof OnContingencyState ocs) { State state = ocs.getState(); updateMapWithValue(usageMethodPerState, state, usageRule.getUsageMethod()); diff --git a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/RemedialActionTest.java b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/RemedialActionTest.java index 8e11f7acc2..071fa746e2 100644 --- a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/RemedialActionTest.java +++ b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/RemedialActionTest.java @@ -10,7 +10,9 @@ import com.powsybl.iidm.network.Country; import com.powsybl.openrao.data.cracapi.Instant; import com.powsybl.openrao.data.cracapi.State; +import com.powsybl.openrao.data.cracapi.cnec.AngleCnec; import com.powsybl.openrao.data.cracapi.cnec.FlowCnec; +import com.powsybl.openrao.data.cracapi.cnec.VoltageCnec; import com.powsybl.openrao.data.cracapi.usagerule.UsageMethod; import com.powsybl.openrao.data.cracapi.usagerule.UsageRule; import org.junit.jupiter.api.Test; @@ -96,4 +98,85 @@ void testStrongestStateAndInstantUsageRule() { AbstractRemedialAction ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0); assertEquals(UsageMethod.FORCED, ra.getUsageMethod(state)); } + + @Test + void testDifferentInstantsBetweenOnFlowConstraintUsageRuleAndCnec() { + Instant autoInstant = Mockito.mock(Instant.class); + Mockito.when(autoInstant.isPreventive()).thenReturn(false); + Instant curativeInstant = Mockito.mock(Instant.class); + Mockito.when(curativeInstant.isPreventive()).thenReturn(false); + + State autoState = Mockito.mock(State.class); + Mockito.when(autoState.getInstant()).thenReturn(autoInstant); + State curativeState = Mockito.mock(State.class); + Mockito.when(curativeState.getInstant()).thenReturn(curativeInstant); + + FlowCnec autoFlowCnec = Mockito.mock(FlowCnec.class); + Mockito.when(autoFlowCnec.getState()).thenReturn(autoState); + FlowCnec curativeFlowCnec = Mockito.mock(FlowCnec.class); + Mockito.when(curativeFlowCnec.getState()).thenReturn(curativeState); + + Set usageRules = Set.of( + new OnFlowConstraintImpl(UsageMethod.FORCED, autoInstant, autoFlowCnec), + new OnFlowConstraintImpl(UsageMethod.FORCED, autoInstant, curativeFlowCnec) + ); + + AbstractRemedialAction ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0); + assertEquals(UsageMethod.FORCED, ra.getUsageMethod(autoState)); + assertEquals(UsageMethod.UNDEFINED, ra.getUsageMethod(curativeState)); + } + + @Test + void testDifferentInstantsBetweenOnAngleConstraintUsageRuleAndCnec() { + Instant autoInstant = Mockito.mock(Instant.class); + Mockito.when(autoInstant.isPreventive()).thenReturn(false); + Instant curativeInstant = Mockito.mock(Instant.class); + Mockito.when(curativeInstant.isPreventive()).thenReturn(false); + + State autoState = Mockito.mock(State.class); + Mockito.when(autoState.getInstant()).thenReturn(autoInstant); + State curativeState = Mockito.mock(State.class); + Mockito.when(curativeState.getInstant()).thenReturn(curativeInstant); + + AngleCnec autoAngleCnec = Mockito.mock(AngleCnec.class); + Mockito.when(autoAngleCnec.getState()).thenReturn(autoState); + AngleCnec curativeAngleCnec = Mockito.mock(AngleCnec.class); + Mockito.when(curativeAngleCnec.getState()).thenReturn(curativeState); + + Set usageRules = Set.of( + new OnAngleConstraintImpl(UsageMethod.FORCED, autoInstant, autoAngleCnec), + new OnAngleConstraintImpl(UsageMethod.FORCED, autoInstant, curativeAngleCnec) + ); + + AbstractRemedialAction ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0); + assertEquals(UsageMethod.FORCED, ra.getUsageMethod(autoState)); + assertEquals(UsageMethod.UNDEFINED, ra.getUsageMethod(curativeState)); + } + + @Test + void testDifferentInstantsBetweenOnVoltageConstraintUsageRuleAndCnec() { + Instant autoInstant = Mockito.mock(Instant.class); + Mockito.when(autoInstant.isPreventive()).thenReturn(false); + Instant curativeInstant = Mockito.mock(Instant.class); + Mockito.when(curativeInstant.isPreventive()).thenReturn(false); + + State autoState = Mockito.mock(State.class); + Mockito.when(autoState.getInstant()).thenReturn(autoInstant); + State curativeState = Mockito.mock(State.class); + Mockito.when(curativeState.getInstant()).thenReturn(curativeInstant); + + VoltageCnec autoVoltageCnec = Mockito.mock(VoltageCnec.class); + Mockito.when(autoVoltageCnec.getState()).thenReturn(autoState); + VoltageCnec curativeVoltageCnec = Mockito.mock(VoltageCnec.class); + Mockito.when(curativeVoltageCnec.getState()).thenReturn(curativeState); + + Set usageRules = Set.of( + new OnVoltageConstraintImpl(UsageMethod.FORCED, autoInstant, autoVoltageCnec), + new OnVoltageConstraintImpl(UsageMethod.FORCED, autoInstant, curativeVoltageCnec) + ); + + AbstractRemedialAction ra = new NetworkActionImpl("id", "name", "operator", usageRules, Collections.emptySet(), 0); + assertEquals(UsageMethod.FORCED, ra.getUsageMethod(autoState)); + assertEquals(UsageMethod.UNDEFINED, ra.getUsageMethod(curativeState)); + } }