Skip to content

Commit

Permalink
Fix CORE CNE exporter (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
pet-mit authored Jan 24, 2023
1 parent 0cce27f commit b9dbbe5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ private double convertPercentImaxToA(double valueInPercent) {
}

private double getCnecFmaxWithoutFrmInA() {
return Math.min(
getExtendable().getUpperBound(Side.LEFT, Unit.AMPERE).orElse(Double.POSITIVE_INFINITY),
-getExtendable().getLowerBound(Side.LEFT, Unit.AMPERE).orElse(Double.NEGATIVE_INFINITY))
+ convertMWToA(getExtendable().getReliabilityMargin());
double minUpperBound = Math.min(
getExtendable().getUpperBound(Side.LEFT, Unit.AMPERE).orElse(Double.POSITIVE_INFINITY),
getExtendable().getUpperBound(Side.RIGHT, Unit.AMPERE).orElse(Double.POSITIVE_INFINITY)
);
double maxLowerBound = Math.max(
getExtendable().getLowerBound(Side.LEFT, Unit.AMPERE).orElse(Double.NEGATIVE_INFINITY),
getExtendable().getLowerBound(Side.RIGHT, Unit.AMPERE).orElse(Double.NEGATIVE_INFINITY)
);
return Math.min(minUpperBound, -maxLowerBound) + convertMWToA(getExtendable().getReliabilityMargin());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ private List<Analog> createMarginMeasurementsOfFlowCnec(FlowCnec cnec, Optimizat
return measurements;
}

private double getCnecFlow(FlowCnec cnec, OptimizationState optimizationState, Unit unit) {
private double getCnecFlow(FlowCnec cnec, Side side, OptimizationState optimizationState, Unit unit) {
OptimizationState resultState = optimizationState;
if (resultState.equals(OptimizationState.AFTER_CRA) && cnec.getState().getInstant().equals(Instant.PREVENTIVE)) {
resultState = OptimizationState.AFTER_PRA;
}
return cneHelper.getRaoResult().getFlow(resultState, cnec, Side.LEFT, unit);
return cneHelper.getRaoResult().getFlow(resultState, cnec, side, unit);
}

private double getCnecMargin(FlowCnec cnec, OptimizationState optimizationState, boolean asMnec, Unit unit, boolean deductFrmFromThreshold) {
Expand All @@ -238,12 +238,12 @@ private double getCnecRelativeMargin(FlowCnec cnec, OptimizationState optimizati
if (resultState.equals(OptimizationState.AFTER_CRA) && cnec.getState().getInstant().equals(Instant.PREVENTIVE)) {
resultState = OptimizationState.AFTER_PRA;
}
return absoluteMargin > 0 ? absoluteMargin / cneHelper.getRaoResult().getPtdfZonalSum(resultState, cnec, Side.LEFT) : absoluteMargin;
return absoluteMargin > 0 ? absoluteMargin / cneHelper.getRaoResult().getPtdfZonalSum(resultState, cnec, getMonitoredSide(cnec)) : absoluteMargin;
}

private Analog createFlowMeasurement(FlowCnec cnec, OptimizationState optimizationState, Unit unit, boolean shouldInvertBranchDirection) {
double invert = shouldInvertBranchDirection ? -1 : 1;
return newFlowMeasurement(FLOW_MEASUREMENT_TYPE, unit, invert * getCnecFlow(cnec, optimizationState, unit));
return newFlowMeasurement(FLOW_MEASUREMENT_TYPE, unit, invert * getCnecFlow(cnec, getMonitoredSide(cnec), optimizationState, unit));
}

private Analog createThresholdMeasurement(FlowCnec cnec, OptimizationState optimizationState, boolean asMnec, Unit unit, String measurementType, boolean shouldInvertBranchDirection) {
Expand Down Expand Up @@ -287,15 +287,14 @@ private double getClosestThreshold(FlowCnec cnec, OptimizationState optimization
*/
private Map<Double, Double> getThresholdToMarginMap(FlowCnec cnec, OptimizationState optimizationState, boolean asMnec, Unit unit, boolean deductFrmFromThreshold) {
Map<Double, Double> thresholdToMarginMap = new HashMap<>();
double flow = getCnecFlow(cnec, optimizationState, unit);
Side side = getMonitoredSide(cnec);
double flow = getCnecFlow(cnec, side, optimizationState, unit);
if (!Double.isNaN(flow)) {
for (Side side : Set.of(Side.LEFT, Side.RIGHT)) {
if (false) {
// TODO : reactivate this for MNECs (if (asMnec)) when we should go back to the full version
getThresholdToMarginMapAsMnec(cnec, unit, thresholdToMarginMap, flow, side);
} else {
getThresholdToMarginMapAsCnec(cnec, unit, deductFrmFromThreshold, thresholdToMarginMap, flow, side);
}
if (false) {
// TODO : reactivate this for MNECs (if (asMnec)) when we should go back to the full version
getThresholdToMarginMapAsMnec(cnec, unit, thresholdToMarginMap, flow, side);
} else {
getThresholdToMarginMapAsCnec(cnec, unit, deductFrmFromThreshold, thresholdToMarginMap, flow, side);
}
}
return thresholdToMarginMap;
Expand All @@ -316,7 +315,7 @@ private void getThresholdToMarginMapAsCnec(FlowCnec cnec, Unit unit, boolean ded

private void getThresholdToMarginMapAsMnec(FlowCnec cnec, Unit unit, Map<Double, Double> thresholdToMarginMap, double flow, Side side) {
// Look at thresholds computed using initial flow
double initialFlow = getCnecFlow(cnec, OptimizationState.INITIAL, unit);
double initialFlow = getCnecFlow(cnec, getMonitoredSide(cnec), OptimizationState.INITIAL, unit);
double tolerance = cneHelper.getMnecAcceptableMarginDiminution() * getFlowUnitMultiplier(cnec, side, Unit.MEGAWATT, unit);

double mnecUpperThreshold = Math.max(cnec.getUpperBound(side, unit).orElse(Double.MAX_VALUE), initialFlow + tolerance);
Expand All @@ -333,7 +332,7 @@ private Analog createFrmMeasurement(FlowCnec cnec) {
}

private Analog createPtdfZonalSumMeasurement(FlowCnec cnec) {
double absPtdfSum = cneHelper.getRaoResult().getPtdfZonalSum(OptimizationState.INITIAL, cnec, Side.LEFT);
double absPtdfSum = cneHelper.getRaoResult().getPtdfZonalSum(OptimizationState.INITIAL, cnec, getMonitoredSide(cnec));
return newPtdfMeasurement(SUM_PTDF_MEASUREMENT_TYPE, absPtdfSum);
}

Expand All @@ -344,7 +343,7 @@ private List<Analog> createLoopflowMeasurements(FlowCnec cnec, OptimizationState
}
List<Analog> measurements = new ArrayList<>();
try {
double loopflow = cneHelper.getRaoResult().getLoopFlow(resultOptimState, cnec, Side.LEFT, Unit.MEGAWATT);
double loopflow = cneHelper.getRaoResult().getLoopFlow(resultOptimState, cnec, getMonitoredSide(cnec), Unit.MEGAWATT);
LoopFlowThreshold loopFlowExtension = cnec.getExtension(LoopFlowThreshold.class);
if (!Objects.isNull(loopFlowExtension) && !Double.isNaN(loopflow)) {
double invert = shouldInvertBranchDirection ? -1 : 1;
Expand All @@ -371,4 +370,8 @@ public static double getFlowUnitMultiplier(FlowCnec cnec, Side voltageSide, Unit
throw new FaraoException("Only conversions between MW and A are supported.");
}
}

private Side getMonitoredSide(FlowCnec cnec) {
return cnec.getMonitoredSides().contains(Side.LEFT) ? Side.LEFT : Side.RIGHT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ private int checkMeasurement(List<Analog> measurements, int index, String expect
}

private void mockCnecResult(FlowCnec cnec, double flowA, double flowMw, double marginA, double marginMw, double relMarginA, double relMarginMw, double ptdf) {
Mockito.when(raoResult.getFlow(any(), eq(cnec), eq(Side.LEFT), eq(Unit.AMPERE))).thenReturn(flowA);
Mockito.when(raoResult.getFlow(any(), eq(cnec), eq(Side.LEFT), eq(Unit.MEGAWATT))).thenReturn(flowMw);
Side monitoredSide = cnec.getMonitoredSides().contains(Side.LEFT) ? Side.LEFT : Side.RIGHT;
Mockito.when(raoResult.getFlow(any(), eq(cnec), eq(monitoredSide), eq(Unit.AMPERE))).thenReturn(flowA);
Mockito.when(raoResult.getFlow(any(), eq(cnec), eq(monitoredSide), eq(Unit.MEGAWATT))).thenReturn(flowMw);
Mockito.when(raoResult.getMargin(any(), eq(cnec), eq(Unit.AMPERE))).thenReturn(marginA);
Mockito.when(raoResult.getMargin(any(), eq(cnec), eq(Unit.MEGAWATT))).thenReturn(marginMw);
Mockito.when(raoResult.getRelativeMargin(any(), eq(cnec), eq(Unit.AMPERE))).thenReturn(relMarginA);
Mockito.when(raoResult.getRelativeMargin(any(), eq(cnec), eq(Unit.MEGAWATT))).thenReturn(relMarginMw);
Mockito.when(raoResult.getPtdfZonalSum(any(), eq(cnec), eq(Side.LEFT))).thenReturn(ptdf);
Mockito.when(raoResult.getPtdfZonalSum(any(), eq(cnec), eq(monitoredSide))).thenReturn(ptdf);
}

@Test
Expand Down Expand Up @@ -376,7 +377,7 @@ public void testWithLoopFlow() {
cnec1.newExtension(LoopFlowThresholdAdder.class).withValue(321.).withUnit(Unit.MEGAWATT).add();

mockCnecResult(cnec1, 40, 80, 10, 20, 100, 200, .1);
Mockito.when(raoResult.getLoopFlow(any(), eq(cnec1), eq(Side.LEFT), eq(Unit.MEGAWATT))).thenReturn(123.);
Mockito.when(raoResult.getLoopFlow(any(), eq(cnec1), eq(Side.RIGHT), eq(Unit.MEGAWATT))).thenReturn(123.);

raoParameters.setObjectiveFunction(RaoParameters.ObjectiveFunction.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.setRaoWithLoopFlowLimitation(true);
Expand Down

0 comments on commit b9dbbe5

Please sign in to comment.