diff --git a/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImpl.java b/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImpl.java index 42f70a88bf..01b39e4d9f 100644 --- a/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImpl.java +++ b/data/crac/crac-impl/src/main/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImpl.java @@ -47,6 +47,9 @@ public Optional getContingency() { @Override public UsageMethod getUsageMethod(State state) { + if (contingency.isPresent() && !contingency.get().equals(state.getContingency().orElse(null))) { + return UsageMethod.UNDEFINED; + } return state.getInstant().equals(instant) ? usageMethod : UsageMethod.UNDEFINED; } diff --git a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImplTest.java b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImplTest.java index 2c8c2c028f..6d649f9edd 100644 --- a/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImplTest.java +++ b/data/crac/crac-impl/src/test/java/com/powsybl/openrao/data/cracimpl/OnFlowConstraintInCountryImplTest.java @@ -6,6 +6,7 @@ */ package com.powsybl.openrao.data.cracimpl; +import com.powsybl.contingency.Contingency; import com.powsybl.openrao.data.cracapi.Instant; import com.powsybl.openrao.data.cracapi.InstantKind; import com.powsybl.openrao.data.cracapi.State; @@ -73,4 +74,41 @@ void testEquals() { assertNotEquals(onFlowConstraint1, onFlowConstraint2); assertNotEquals(onFlowConstraint1.hashCode(), onFlowConstraint2.hashCode()); } + + @Test + void testGetUsageMethodWithContringency() { + Contingency contingency1 = Mockito.mock(Contingency.class); + Contingency contingency2 = Mockito.mock(Contingency.class); + + State stateAuto1 = new PostContingencyState(contingency1, AUTO_INSTANT); + State stateCur1 = new PostContingencyState(contingency1, CURATIVE_INSTANT); + State stateAuto2 = new PostContingencyState(contingency2, AUTO_INSTANT); + State stateCur2 = new PostContingencyState(contingency2, CURATIVE_INSTANT); + + OnFlowConstraintInCountry ur; + + ur = new OnFlowConstraintInCountryImpl(UsageMethod.AVAILABLE, AUTO_INSTANT, Optional.of(contingency1), Country.ES); + assertEquals(UsageMethod.AVAILABLE, ur.getUsageMethod(stateAuto1)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateCur1)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateAuto2)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateCur2)); + + ur = new OnFlowConstraintInCountryImpl(UsageMethod.AVAILABLE, AUTO_INSTANT, Optional.empty(), Country.ES); + assertEquals(UsageMethod.AVAILABLE, ur.getUsageMethod(stateAuto1)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateCur1)); + assertEquals(UsageMethod.AVAILABLE, ur.getUsageMethod(stateAuto2)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateCur2)); + + ur = new OnFlowConstraintInCountryImpl(UsageMethod.AVAILABLE, CURATIVE_INSTANT, Optional.of(contingency1), Country.ES); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateAuto1)); + assertEquals(UsageMethod.AVAILABLE, ur.getUsageMethod(stateCur1)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateAuto2)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateCur2)); + + ur = new OnFlowConstraintInCountryImpl(UsageMethod.AVAILABLE, CURATIVE_INSTANT, Optional.empty(), Country.ES); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateAuto1)); + assertEquals(UsageMethod.AVAILABLE, ur.getUsageMethod(stateCur1)); + assertEquals(UsageMethod.UNDEFINED, ur.getUsageMethod(stateAuto2)); + assertEquals(UsageMethod.AVAILABLE, ur.getUsageMethod(stateCur2)); + } } diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/commons/RaoUtilTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/commons/RaoUtilTest.java index a35cad9678..b0feed4731 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/commons/RaoUtilTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/commons/RaoUtilTest.java @@ -340,6 +340,7 @@ void testIsOnFlowConstraintInCountryAvailableWithContingency() { Instant curativeInstant = crac.getInstant(CURATIVE_INSTANT_ID); State optimizedState = Mockito.mock(State.class); when(optimizedState.getInstant()).thenReturn(curativeInstant); + when(optimizedState.getContingency()).thenReturn(Optional.of(crac.getContingency("Contingency FR1 FR3"))); FlowCnec cnecCont1 = crac.getFlowCnec("cnec1stateCurativeContingency1"); FlowCnec cnecCont2 = crac.getFlowCnec("cnec2stateCurativeContingency2");