Skip to content

Commit

Permalink
Merge branch 'main' into feature/power-gradient-constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
bqth29 authored Dec 17, 2024
2 parents 8fdae5d + 5e3674c commit dff3b7b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ static SystematicSensitivityResult runSensitivity(Network network,
SensitivityAnalysisResult result;
try {
result = SensitivityAnalysis.find(sensitivityProvider).run(network,
network.getVariantManager().getWorkingVariantId(),
cnecSensitivityProvider.getAllFactors(network),
cnecSensitivityProvider.getContingencies(network),
cnecSensitivityProvider.getVariableSets(),
sensitivityComputationParameters);
network.getVariantManager().getWorkingVariantId(),
cnecSensitivityProvider.getAllFactors(network),
cnecSensitivityProvider.getContingencies(network),
cnecSensitivityProvider.getVariableSets(),
sensitivityComputationParameters);
} catch (Exception e) {
TECHNICAL_LOGS.error(String.format("Systematic sensitivity analysis failed: %s", e.getMessage()));
return new SystematicSensitivityResult(SystematicSensitivityResult.SensitivityComputationStatus.FAILURE);
Expand Down Expand Up @@ -84,12 +84,17 @@ static SystematicSensitivityResult runSensitivity(Network network,
SystematicSensitivityResult result = new SystematicSensitivityResult();
List<SensitivityFactor> allFactorsWithoutRa = cnecSensitivityProvider.getBasecaseFactors(network);
allFactorsWithoutRa.addAll(cnecSensitivityProvider.getContingencyFactors(network, contingenciesWithoutRa));
result.completeData(SensitivityAnalysis.find(sensitivityProvider).run(network,
network.getVariantManager().getWorkingVariantId(),
allFactorsWithoutRa,
contingenciesWithoutRa,
cnecSensitivityProvider.getVariableSets(),
sensitivityComputationParameters), outageInstant.getOrder());
try {
result.completeData(SensitivityAnalysis.find(sensitivityProvider).run(network,
network.getVariantManager().getWorkingVariantId(),
allFactorsWithoutRa,
contingenciesWithoutRa,
cnecSensitivityProvider.getVariableSets(),
sensitivityComputationParameters), outageInstant.getOrder());
} catch (Exception e) {
TECHNICAL_LOGS.error(String.format("Systematic sensitivity analysis failed: %s", e.getMessage()));
return new SystematicSensitivityResult(SystematicSensitivityResult.SensitivityComputationStatus.FAILURE);
}

// systematic analyses for states with RA
cnecSensitivityProvider.disableFactorsForBaseCaseSituation();
Expand Down Expand Up @@ -117,12 +122,17 @@ static SystematicSensitivityResult runSensitivity(Network network,

List<Contingency> contingencyList = Collections.singletonList(optContingency.get());

result.completeData(SensitivityAnalysis.find(sensitivityProvider).run(network,
network.getVariantManager().getWorkingVariantId(),
cnecSensitivityProvider.getContingencyFactors(network, contingencyList),
contingencyList,
cnecSensitivityProvider.getVariableSets(),
sensitivityComputationParameters), state.getInstant().getOrder());
try {
result.completeData(SensitivityAnalysis.find(sensitivityProvider).run(network,
network.getVariantManager().getWorkingVariantId(),
cnecSensitivityProvider.getContingencyFactors(network, contingencyList),
contingencyList,
cnecSensitivityProvider.getVariableSets(),
sensitivityComputationParameters), state.getInstant().getOrder());
} catch (Exception e) {
TECHNICAL_LOGS.error(String.format("Systematic sensitivity analysis failed for state %s : %s", state.getId(), e.getMessage()));
result.completeDataWithFailingPerimeter(state.getInstant().getOrder(), optContingency.get().getId());
}
counterForLogs++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public SystematicSensitivityResult(SensitivityComputationStatus status) {

public SystematicSensitivityResult completeData(SensitivityAnalysisResult results, Integer instantOrder) {
postContingencyResults.putIfAbsent(instantOrder, new HashMap<>());
// if a failing perimeter was already run, then the status would be set to PARTIAL_FAILURE
boolean anyContingencyFailure = this.status == SensitivityComputationStatus.PARTIAL_FAILURE;
// status set to failure initially, and set to success if we find at least one non NaN value
this.status = SensitivityComputationStatus.FAILURE;
if (results == null) {
return this;
}

boolean anyContingencyFailure = false;

results.getPreContingencyValues().forEach(sensitivityValue -> fillIndividualValue(sensitivityValue, nStateResult, results.getFactors(), SensitivityAnalysisResult.Status.SUCCESS));
for (SensitivityAnalysisResult.SensitivityContingencyStatus contingencyStatus : results.getContingencyStatuses()) {
if (contingencyStatus.getStatus() == SensitivityAnalysisResult.Status.FAILURE) {
Expand All @@ -107,6 +107,15 @@ public SystematicSensitivityResult completeData(SensitivityAnalysisResult result
return this;
}

public SystematicSensitivityResult completeDataWithFailingPerimeter(int instantOrder, String contingencyId) {
this.status = SensitivityComputationStatus.PARTIAL_FAILURE;
StateResult contingencyStateResult = new StateResult();
contingencyStateResult.status = SensitivityComputationStatus.FAILURE;
postContingencyResults.putIfAbsent(instantOrder, new HashMap<>());
postContingencyResults.get(instantOrder).put(contingencyId, contingencyStateResult);
return this;
}

public SystematicSensitivityResult postTreatIntensities() {
postTreatIntensitiesOnState(nStateResult);
postContingencyResults.values().forEach(map -> map.values().forEach(this::postTreatIntensitiesOnState));
Expand Down Expand Up @@ -223,9 +232,9 @@ public SensitivityComputationStatus getStatus(State state) {
Optional<Contingency> optionalContingency = state.getContingency();
if (optionalContingency.isPresent()) {
List<Integer> possibleInstants = postContingencyResults.keySet().stream()
.filter(instantOrder -> instantOrder <= state.getInstant().getOrder())
.sorted(Comparator.reverseOrder())
.toList();
.filter(instantOrder -> instantOrder <= state.getInstant().getOrder())
.sorted(Comparator.reverseOrder())
.toList();
for (Integer instantOrder : possibleInstants) {
// Use latest sensi computed on state
if (postContingencyResults.get(instantOrder).containsKey(optionalContingency.get().getId())) {
Expand All @@ -249,8 +258,8 @@ public Set<String> getContingencies() {
public double getReferenceFlow(FlowCnec cnec, TwoSides side) {
StateResult stateResult = getCnecStateResult(cnec);
if (stateResult == null ||
!stateResult.getReferenceFlows().containsKey(cnec.getNetworkElement().getId()) ||
!stateResult.getReferenceFlows().get(cnec.getNetworkElement().getId()).containsKey(side)) {
!stateResult.getReferenceFlows().containsKey(cnec.getNetworkElement().getId()) ||
!stateResult.getReferenceFlows().get(cnec.getNetworkElement().getId()).containsKey(side)) {
return 0.0;
}
return stateResult.getReferenceFlows().get(cnec.getNetworkElement().getId()).get(side);
Expand All @@ -269,8 +278,8 @@ public double getReferenceFlow(FlowCnec cnec, TwoSides side, Instant instant) {
public double getReferenceIntensity(FlowCnec cnec, TwoSides side) {
StateResult stateResult = getCnecStateResult(cnec);
if (stateResult == null ||
!stateResult.getReferenceIntensities().containsKey(cnec.getNetworkElement().getId()) ||
!stateResult.getReferenceIntensities().get(cnec.getNetworkElement().getId()).containsKey(side)) {
!stateResult.getReferenceIntensities().containsKey(cnec.getNetworkElement().getId()) ||
!stateResult.getReferenceIntensities().get(cnec.getNetworkElement().getId()).containsKey(side)) {
return 0.0;
}
return stateResult.getReferenceIntensities().get(cnec.getNetworkElement().getId()).get(side);
Expand All @@ -297,9 +306,9 @@ public double getSensitivityOnFlow(SensitivityVariableSet glsk, FlowCnec cnec, T
public double getSensitivityOnFlow(String variableId, FlowCnec cnec, TwoSides side) {
StateResult stateResult = getCnecStateResult(cnec);
if (stateResult == null ||
!stateResult.getFlowSensitivities().containsKey(cnec.getNetworkElement().getId()) ||
!stateResult.getFlowSensitivities().get(cnec.getNetworkElement().getId()).containsKey(variableId) ||
!stateResult.getFlowSensitivities().get(cnec.getNetworkElement().getId()).get(variableId).containsKey(side)) {
!stateResult.getFlowSensitivities().containsKey(cnec.getNetworkElement().getId()) ||
!stateResult.getFlowSensitivities().get(cnec.getNetworkElement().getId()).containsKey(variableId) ||
!stateResult.getFlowSensitivities().get(cnec.getNetworkElement().getId()).get(variableId).containsKey(side)) {
return 0.0;
}
return stateResult.getFlowSensitivities().get(cnec.getNetworkElement().getId()).get(variableId).get(side);
Expand All @@ -323,9 +332,9 @@ private StateResult getCnecStateResult(Cnec<?> cnec) {
Optional<Contingency> optionalContingency = cnec.getState().getContingency();
if (optionalContingency.isPresent()) {
List<Integer> possibleInstants = postContingencyResults.keySet().stream()
.filter(instantOrder -> instantOrder <= cnec.getState().getInstant().getOrder())
.sorted(Comparator.reverseOrder())
.toList();
.filter(instantOrder -> instantOrder <= cnec.getState().getInstant().getOrder())
.sorted(Comparator.reverseOrder())
.toList();
for (Integer instantOrder : possibleInstants) {
// Use latest sensi computed on the cnec's contingency amidst the last instants before cnec state.
String contingencyId = optionalContingency.get().getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.powsybl.contingency.ContingencyContextType;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.TwoWindingsTransformer;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.sensitivity.*;

import java.util.List;
Expand All @@ -24,10 +25,20 @@
*/
@AutoService(SensitivityAnalysisProvider.class)
public final class MockSensiProvider implements SensitivityAnalysisProvider {
int counter = 0;

@Override
public CompletableFuture<Void> run(Network network, String s, SensitivityFactorReader sensitivityFactorReader, SensitivityResultWriter sensitivityResultWriter, List<Contingency> contingencies, List<SensitivityVariableSet> glsks, SensitivityAnalysisParameters sensitivityAnalysisParameters, ComputationManager computationManager, ReportNode reportNode) {
return CompletableFuture.runAsync(() -> {
if (network.getNameOrId().equals("Mock_Exception")) {
throw new OpenRaoException("Mocked exception");
}
if (network.getNameOrId().equals("Second_Run_Exception")) {
counter++;
if (counter == 2) {
throw new OpenRaoException("Mocked exception on second round");
}
}
TwoWindingsTransformer pst = network.getTwoWindingsTransformer("BBE2AA1 BBE3AA1 1");
if (pst == null || pst.getPhaseTapChanger().getTapPosition() == 0) {
// used for most of the tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,71 @@ void testWithAppliedRa() {
assertEquals(-5, result.getSensitivityOnFlow(crac.getRangeAction("pst"), crac.getFlowCnec("cnec2stateOutageContingency1"), ONE), DOUBLE_TOLERANCE);
assertEquals(5.5, result.getSensitivityOnFlow(crac.getRangeAction("pst"), crac.getFlowCnec("cnec2stateOutageContingency1"), TWO), DOUBLE_TOLERANCE);
}

@Test
void testCatchInRunSensitivity() {
Network network = NetworkImportsUtil.import12NodesNetwork();
network.setName("Mock_Exception");
Crac crac = CommonCracCreation.createWithPreventivePstRange(Set.of(ONE, TWO));
Instant outageInstant = crac.getInstant(OUTAGE_INSTANT_ID);
RangeActionSensitivityProvider factorProvider = new RangeActionSensitivityProvider(crac.getRangeActions(), crac.getFlowCnecs(), Set.of(Unit.MEGAWATT, Unit.AMPERE));
SystematicSensitivityResult result = SystematicSensitivityAdapter.runSensitivity(network, factorProvider, new SensitivityAnalysisParameters(), "MockSensi", outageInstant);
assertEquals(SystematicSensitivityResult.SensitivityComputationStatus.FAILURE, result.getStatus());
}

@Test
void testFirstCatchInRunSensitivityWithAppliedRa() {
Network network = NetworkImportsUtil.import12NodesNetwork();
network.setName("Mock_Exception");
Crac crac = CommonCracCreation.createWithPreventivePstRange(Set.of(ONE, TWO));
Instant curativeInstant = crac.getInstant(CURATIVE_INSTANT_ID);
crac.newFlowCnec()
.withId("cnec2stateOutageContingency1")
.withNetworkElement("FFR2AA1 DDE3AA1 1")
.withInstant(OUTAGE_INSTANT_ID)
.withContingency("Contingency FR1 FR3")
.withOptimized(true)
.withOperator("operator2")
.newThreshold().withUnit(Unit.MEGAWATT).withSide(ONE).withMin(-1500.).withMax(1500.).add()
.newThreshold().withUnit(Unit.MEGAWATT).withSide(TWO).withMin(-1500.).withMax(1500.).add()
.newThreshold().withUnit(Unit.PERCENT_IMAX).withSide(ONE).withMin(-0.3).withMax(0.3).add()
.newThreshold().withUnit(Unit.PERCENT_IMAX).withSide(TWO).withMin(-0.3).withMax(0.3).add()
.withNominalVoltage(380.)
.withIMax(5000.)
.add();
RangeActionSensitivityProvider factorProvider = new RangeActionSensitivityProvider(crac.getRangeActions(), crac.getFlowCnecs(), Set.of(Unit.MEGAWATT, Unit.AMPERE));
AppliedRemedialActions appliedRemedialActions = new AppliedRemedialActions();
appliedRemedialActions.addAppliedRangeAction(crac.getState("Contingency FR1 FR3", curativeInstant), crac.getPstRangeAction("pst"), -3.1);

SystematicSensitivityResult result = SystematicSensitivityAdapter.runSensitivity(network, factorProvider, appliedRemedialActions, new SensitivityAnalysisParameters(), "MockSensi", crac.getOutageInstant());
assertEquals(SystematicSensitivityResult.SensitivityComputationStatus.FAILURE, result.getStatus());
}

@Test
void testSecondCatchInRunSensitivityWithAppliedRa() {
Network network = NetworkImportsUtil.import12NodesNetwork();
network.setName("Second_Run_Exception");
Crac crac = CommonCracCreation.createWithPreventivePstRange(Set.of(ONE, TWO));
Instant curativeInstant = crac.getInstant(CURATIVE_INSTANT_ID);
crac.newFlowCnec()
.withId("cnec2stateOutageContingency1")
.withNetworkElement("FFR2AA1 DDE3AA1 1")
.withInstant(OUTAGE_INSTANT_ID)
.withContingency("Contingency FR1 FR3")
.withOptimized(true)
.withOperator("operator2")
.newThreshold().withUnit(Unit.MEGAWATT).withSide(ONE).withMin(-1500.).withMax(1500.).add()
.newThreshold().withUnit(Unit.MEGAWATT).withSide(TWO).withMin(-1500.).withMax(1500.).add()
.newThreshold().withUnit(Unit.PERCENT_IMAX).withSide(ONE).withMin(-0.3).withMax(0.3).add()
.newThreshold().withUnit(Unit.PERCENT_IMAX).withSide(TWO).withMin(-0.3).withMax(0.3).add()
.withNominalVoltage(380.)
.withIMax(5000.)
.add();
RangeActionSensitivityProvider factorProvider = new RangeActionSensitivityProvider(crac.getRangeActions(), crac.getFlowCnecs(), Set.of(Unit.MEGAWATT, Unit.AMPERE));
AppliedRemedialActions appliedRemedialActions = new AppliedRemedialActions();
appliedRemedialActions.addAppliedRangeAction(crac.getState("Contingency FR1 FR3", curativeInstant), crac.getPstRangeAction("pst"), -3.1);

SystematicSensitivityResult result = SystematicSensitivityAdapter.runSensitivity(network, factorProvider, appliedRemedialActions, new SensitivityAnalysisParameters(), "MockSensi", crac.getOutageInstant());
assertEquals(SystematicSensitivityResult.SensitivityComputationStatus.PARTIAL_FAILURE, result.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,21 @@ void testCurativeResultAtOutageInstant() {

}

@Test
void testCompleteDataWithFailingPerimeter() {
setUpWith12Nodes();
// When
SensitivityAnalysisResult sensitivityAnalysisResult = SensitivityAnalysis.find().run(network,
rangeActionSensitivityProvider.getAllFactors(network),
rangeActionSensitivityProvider.getContingencies(network),
new ArrayList<>(),
SensitivityAnalysisParameters.load());
SystematicSensitivityResult result = new SystematicSensitivityResult().completeData(sensitivityAnalysisResult, outageInstantOrder);
assertEquals(SystematicSensitivityResult.SensitivityComputationStatus.SUCCESS, result.getStatus());

result.completeDataWithFailingPerimeter(outageInstantOrder, "Contingency FR1 FR3");
// after computation failure on contingency
assertEquals(SystematicSensitivityResult.SensitivityComputationStatus.PARTIAL_FAILURE, result.getStatus());
}

}

0 comments on commit dff3b7b

Please sign in to comment.