diff --git a/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java index 379f8ad529..f028ad2f85 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java @@ -615,10 +615,14 @@ protected static void findAllBranchesToClose(Network network, List actio } protected static void distributedMismatch(LfNetwork network, double mismatch, LoadFlowParameters loadFlowParameters, - OpenLoadFlowParameters openLoadFlowParameters, ContingencyLoadFlowParameters contingencyParameters) { - ContingencyLoadFlowParameters parameters = Objects.requireNonNullElse(contingencyParameters, new ContingencyLoadFlowParameters(loadFlowParameters.isDistributedSlack(), openLoadFlowParameters.isAreaInterchangeControl(), loadFlowParameters.getBalanceType())); - if ((parameters.isDistributedSlack() || parameters.isAreaInterchangeControl()) && Math.abs(mismatch) > 0) { - ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(parameters.getBalanceType(), openLoadFlowParameters.isLoadPowerFactorConstant(), openLoadFlowParameters.isUseActiveLimits()); + OpenLoadFlowParameters openLoadFlowParameters, ContingencyLoadFlowParameters nullableContingencyLfParameters) { + ContingencyLoadFlowParameters contingencyLfParameters = Objects.requireNonNullElse(nullableContingencyLfParameters, new ContingencyLoadFlowParameters()); + boolean distributedSlack = contingencyLfParameters.isDistributedSlack().orElse(loadFlowParameters.isDistributedSlack()); + boolean areaInterchangeControl = contingencyLfParameters.isAreaInterchangeControl().orElse(openLoadFlowParameters.isAreaInterchangeControl()); + LoadFlowParameters.BalanceType balanceType = contingencyLfParameters.getBalanceType().orElse(loadFlowParameters.getBalanceType()); + + if ((distributedSlack || areaInterchangeControl) && Math.abs(mismatch) > 0) { + ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(balanceType, openLoadFlowParameters.isLoadPowerFactorConstant(), openLoadFlowParameters.isUseActiveLimits()); activePowerDistribution.run(network, mismatch); } } @@ -671,6 +675,9 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List parametersResetter = createParametersResetter(acParameters); + // start a simulation for each of the contingency Iterator contingencyIt = propagatedContingencies.iterator(); while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) { @@ -681,7 +688,7 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List parametersResetter = applyContingencyParameters(context.getParameters(), contingencyLoadFlowParameters, openLoadFlowParameters); + applyContingencyParameters(context.getParameters(), contingencyLoadFlowParameters, loadFlowParameters, openLoadFlowParameters); lfContingency.apply(loadFlowParameters.getBalanceType()); @@ -727,10 +734,13 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List applyContingencyParameters(P parameters, ContingencyLoadFlowParameters contingencyLoadFlowParameters, OpenLoadFlowParameters parametersExt) { + private Consumer

createParametersResetter(P parameters) { + if (parameters instanceof DcLoadFlowParameters dcLoadFlowParameters) { + boolean oldDistributedSlack = dcLoadFlowParameters.isDistributedSlack(); + LoadFlowParameters.BalanceType oldBalanceType = dcLoadFlowParameters.getBalanceType(); + List oldOuterLoops = new ArrayList<>(dcLoadFlowParameters.getOuterLoops()); + return p -> { + ((DcLoadFlowParameters) p).setDistributedSlack(oldDistributedSlack); + ((DcLoadFlowParameters) p).setBalanceType(oldBalanceType); + ((DcLoadFlowParameters) p).setOuterLoops(oldOuterLoops); + }; + } else if (parameters instanceof AcLoadFlowParameters acLoadFlowParameters) { + List oldOuterLoops = new ArrayList<>(acLoadFlowParameters.getOuterLoops()); + return p -> ((AcLoadFlowParameters) p).setOuterLoops(oldOuterLoops); + } else { + return p -> { }; + } + } + + private void applyContingencyParameters(P parameters, ContingencyLoadFlowParameters contingencyLoadFlowParameters, LoadFlowParameters loadFlowParameters, OpenLoadFlowParameters parametersExt) { if (contingencyLoadFlowParameters != null) { + LoadFlowParameters.BalanceType balanceType = contingencyLoadFlowParameters.getBalanceType().orElse(loadFlowParameters.getBalanceType()); if (parameters instanceof DcLoadFlowParameters dcLoadFlowParameters) { - return applyDcContingencyLoadFlowParameters(dcLoadFlowParameters, parametersExt, contingencyLoadFlowParameters); + applyDcContingencyLoadFlowParameters(dcLoadFlowParameters, parametersExt, contingencyLoadFlowParameters, balanceType); } else if (parameters instanceof AcLoadFlowParameters acLoadFlowParameters) { - return applyAcContingencyLoadFlowParameters(acLoadFlowParameters, parametersExt, contingencyLoadFlowParameters); + applyAcContingencyLoadFlowParameters(acLoadFlowParameters, contingencyLoadFlowParameters, parametersExt, balanceType); } else { LOGGER.error("Unsupported load flow parameters type {} to apply contingency parameters", parameters.getClass()); } } - return p -> { }; } - private Consumer

applyAcContingencyLoadFlowParameters(AcLoadFlowParameters acLoadFlowParameters, OpenLoadFlowParameters parametersExt, ContingencyLoadFlowParameters contingencyLoadFlowParameters) { - List oldOuterLoops = acLoadFlowParameters.getOuterLoops(); - List newOuterLoops = new ArrayList<>(oldOuterLoops.stream().filter(o -> !(o instanceof AcActivePowerDistributionOuterLoop)).toList()); - if (contingencyLoadFlowParameters.isAreaInterchangeControl()) { - AcAreaInterchangeControlOuterLoop outerLoop = AcAreaInterchangeControlOuterLoop.create(contingencyLoadFlowParameters.getBalanceType(), parametersExt.isLoadPowerFactorConstant(), parametersExt.isUseActiveLimits(), - parametersExt.getSlackBusPMaxMismatch(), parametersExt.getAreaInterchangePMaxMismatch()); - newOuterLoops.add(outerLoop); - } else if (contingencyLoadFlowParameters.isDistributedSlack()) { - DistributedSlackOuterLoop outerLoop = DistributedSlackOuterLoop.create(contingencyLoadFlowParameters.getBalanceType(), parametersExt.isLoadPowerFactorConstant(), parametersExt.isUseActiveLimits(), - parametersExt.getSlackBusPMaxMismatch()); + private void applyAcContingencyLoadFlowParameters(AcLoadFlowParameters acLoadFlowParameters, ContingencyLoadFlowParameters contingencyLoadFlowParameters, OpenLoadFlowParameters parametersExt, LoadFlowParameters.BalanceType balanceType) { + boolean createAreaInterchangeControl = contingencyLoadFlowParameters.isAreaInterchangeControl().orElse(false); + boolean createDistributedSlack = contingencyLoadFlowParameters.isDistributedSlack().orElse(false); + if (createAreaInterchangeControl || createDistributedSlack) { + List newOuterLoops = new ArrayList<>(acLoadFlowParameters.getOuterLoops().stream().filter(o -> !(o instanceof AcActivePowerDistributionOuterLoop)).toList()); + AcOuterLoop outerLoop; + if (createAreaInterchangeControl) { + outerLoop = AcAreaInterchangeControlOuterLoop.create(balanceType, parametersExt.isLoadPowerFactorConstant(), parametersExt.isUseActiveLimits(), + parametersExt.getSlackBusPMaxMismatch(), parametersExt.getAreaInterchangePMaxMismatch()); + } else { + outerLoop = DistributedSlackOuterLoop.create(balanceType, parametersExt.isLoadPowerFactorConstant(), parametersExt.isUseActiveLimits(), + parametersExt.getSlackBusPMaxMismatch()); + } newOuterLoops.add(outerLoop); + acLoadFlowParameters.setOuterLoops(newOuterLoops); } - - acLoadFlowParameters.setOuterLoops(newOuterLoops); - return p -> ((AcLoadFlowParameters) p).setOuterLoops(oldOuterLoops); } - private Consumer

applyDcContingencyLoadFlowParameters(DcLoadFlowParameters dcLoadFlowParameters, OpenLoadFlowParameters parametersExt, ContingencyLoadFlowParameters contingencyLoadFlowParameters) { - boolean oldDistributedSlack = dcLoadFlowParameters.isDistributedSlack(); - List oldOuterLoops = dcLoadFlowParameters.getOuterLoops(); - - List newOuterLoops = new ArrayList<>(oldOuterLoops.stream().filter(o -> !(o instanceof DcAreaInterchangeControlOuterLoop)).toList()); - if (contingencyLoadFlowParameters.isAreaInterchangeControl()) { - DcAreaInterchangeControlOuterLoop outerLoop = DcAreaInterchangeControlOuterLoop.create(contingencyLoadFlowParameters.getBalanceType(), parametersExt.isLoadPowerFactorConstant(), parametersExt.isUseActiveLimits(), + private void applyDcContingencyLoadFlowParameters(DcLoadFlowParameters dcLoadFlowParameters, OpenLoadFlowParameters parametersExt, ContingencyLoadFlowParameters contingencyLoadFlowParameters, LoadFlowParameters.BalanceType balanceType) { + if (contingencyLoadFlowParameters.isAreaInterchangeControl().orElse(false)) { + List newOuterLoops = new ArrayList<>(dcLoadFlowParameters.getOuterLoops().stream().filter(o -> !(o instanceof DcAreaInterchangeControlOuterLoop)).toList()); + DcAreaInterchangeControlOuterLoop outerLoop = DcAreaInterchangeControlOuterLoop.create(balanceType, parametersExt.isLoadPowerFactorConstant(), parametersExt.isUseActiveLimits(), parametersExt.getSlackBusPMaxMismatch(), parametersExt.getAreaInterchangePMaxMismatch()); newOuterLoops.add(outerLoop); + dcLoadFlowParameters.setOuterLoops(newOuterLoops); } - dcLoadFlowParameters.setDistributedSlack(contingencyLoadFlowParameters.isDistributedSlack()); - dcLoadFlowParameters.setBalanceType(contingencyLoadFlowParameters.getBalanceType()); - dcLoadFlowParameters.setOuterLoops(newOuterLoops); - return p -> { - ((DcLoadFlowParameters) p).setDistributedSlack(oldDistributedSlack); - ((DcLoadFlowParameters) p).setOuterLoops(oldOuterLoops); - }; + contingencyLoadFlowParameters.isDistributedSlack().ifPresent(dcLoadFlowParameters::setDistributedSlack); + dcLoadFlowParameters.setBalanceType(balanceType); } private Optional runActionSimulation(LfNetwork network, C context, OperatorStrategy operatorStrategy,