Skip to content

Commit

Permalink
POC security analysis with limits override (missing a run for each co…
Browse files Browse the repository at this point in the history
…nditional action)
  • Loading branch information
Pauline Jean-Marie committed Jul 10, 2024
1 parent 831b908 commit d95df16
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.computation.ComputationManager;
import com.powsybl.contingency.ContingenciesProvider;
import com.powsybl.contingency.Contingency;
import com.powsybl.contingency.ContingencyContext;
import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Switch;
Expand Down Expand Up @@ -43,6 +44,7 @@
import com.powsybl.security.monitor.StateMonitorIndex;
import com.powsybl.security.results.*;
import com.powsybl.security.strategy.ConditionalActions;
import com.powsybl.security.strategy.OperationalLimitOverride;
import com.powsybl.security.strategy.OperatorStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -90,13 +92,14 @@ protected static SecurityAnalysisResult createNoResult() {

public CompletableFuture<SecurityAnalysisReport> run(String workingVariantId, SecurityAnalysisParameters securityAnalysisParameters,
ContingenciesProvider contingenciesProvider, ComputationManager computationManager,
List<OperatorStrategy> operatorStrategies, List<Action> actions, List<LimitReduction> limitReductions) {
List<OperatorStrategy> operatorStrategies, List<Action> actions, List<LimitReduction> limitReductions,
List<OperationalLimitOverride> limitsToOverride) {
Objects.requireNonNull(workingVariantId);
Objects.requireNonNull(securityAnalysisParameters);
Objects.requireNonNull(contingenciesProvider);
return CompletableFutureTask.runAsync(() -> {
network.getVariantManager().setWorkingVariant(workingVariantId);
return runSync(securityAnalysisParameters, contingenciesProvider, operatorStrategies, actions, limitReductions);
return runSync(securityAnalysisParameters, contingenciesProvider, operatorStrategies, actions, limitReductions, limitsToOverride);
}, computationManager.getExecutor());
}

Expand All @@ -107,7 +110,7 @@ public CompletableFuture<SecurityAnalysisReport> run(String workingVariantId, Se
protected abstract P createParameters(LoadFlowParameters lfParameters, OpenLoadFlowParameters lfParametersExt, boolean breakers);

SecurityAnalysisReport runSync(SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider,
List<OperatorStrategy> operatorStrategies, List<Action> actions, List<LimitReduction> limitReductions) {
List<OperatorStrategy> operatorStrategies, List<Action> actions, List<LimitReduction> limitReductions, List<OperationalLimitOverride> limitsToOverride) {
var saReportNode = createSaRootReportNode();

Stopwatch stopwatch = Stopwatch.createStarted();
Expand Down Expand Up @@ -150,7 +153,7 @@ SecurityAnalysisReport runSync(SecurityAnalysisParameters securityAnalysisParame
try (LfNetworkList lfNetworks = Networks.load(network, parameters.getNetworkParameters(), topoConfig, saReportNode)) {
// run simulation on largest network
SecurityAnalysisResult result = lfNetworks.getLargest().filter(n -> n.getValidity() == LfNetwork.Validity.VALID)
.map(largestNetwork -> runSimulations(largestNetwork, propagatedContingencies, parameters, securityAnalysisParameters, operatorStrategies, actions, limitReductions))
.map(largestNetwork -> runSimulations(largestNetwork, propagatedContingencies, parameters, securityAnalysisParameters, operatorStrategies, actions, limitReductions, limitsToOverride))
.orElse(createNoResult());

stopwatch.stop();
Expand Down Expand Up @@ -396,7 +399,7 @@ protected void afterPreContingencySimulation(P acParameters) {

protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List<PropagatedContingency> propagatedContingencies, P acParameters,
SecurityAnalysisParameters securityAnalysisParameters, List<OperatorStrategy> operatorStrategies,
List<Action> actions, List<LimitReduction> limitReductions) {
List<Action> actions, List<LimitReduction> limitReductions, List<OperationalLimitOverride> limitsToOverride) {
Map<String, Action> actionsById = indexActionsById(actions);
Set<Action> neededActions = new HashSet<>(actionsById.size());
Map<String, List<OperatorStrategy>> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions);
Expand All @@ -417,7 +420,8 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List<Propag
.run();

boolean preContingencyComputationOk = preContingencyLoadFlowResult.isSuccess();
var preContingencyLimitViolationManager = new LimitViolationManager(limitReductions);
var limitsToOverridePreContingency = limitsToOverride.stream().filter(l -> l.getContingencyContext() == ContingencyContext.none()).toList();
var preContingencyLimitViolationManager = new LimitViolationManager(limitReductions, limitsToOverridePreContingency);
List<PostContingencyResult> postContingencyResults = new ArrayList<>();
var preContingencyNetworkResult = new PreContingencyNetworkResult(lfNetwork, monitorIndex, createResultExtension);
List<OperatorStrategyResult> operatorStrategyResults = new ArrayList<>();
Expand Down Expand Up @@ -451,7 +455,8 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List<Propag
var postContingencyResult = runPostContingencySimulation(lfNetwork, context, propagatedContingency.getContingency(),
lfContingency, preContingencyLimitViolationManager,
securityAnalysisParameters.getIncreasedViolationsParameters(),
preContingencyNetworkResult, createResultExtension, limitReductions);
preContingencyNetworkResult, createResultExtension, limitReductions,
limitsToOverride);
postContingencyResults.add(postContingencyResult);

List<OperatorStrategy> operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(lfContingency.getId());
Expand All @@ -465,7 +470,7 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List<Propag
operatorStrategy, preContingencyLimitViolationManager,
securityAnalysisParameters.getIncreasedViolationsParameters(), lfActionById,
createResultExtension, lfContingency, postContingencyResult.getLimitViolationsResult(),
acParameters.getNetworkParameters(), limitReductions)
acParameters.getNetworkParameters(), limitReductions, limitsToOverride)
.ifPresent(operatorStrategyResults::add);
} else {
// save post contingency state for later restoration after action
Expand All @@ -477,7 +482,7 @@ protected SecurityAnalysisResult runSimulations(LfNetwork lfNetwork, List<Propag
operatorStrategy, preContingencyLimitViolationManager,
securityAnalysisParameters.getIncreasedViolationsParameters(), lfActionById,
createResultExtension, lfContingency, postContingencyResult.getLimitViolationsResult(),
acParameters.getNetworkParameters(), limitReductions)
acParameters.getNetworkParameters(), limitReductions, limitsToOverride)
.ifPresent(result -> {
operatorStrategyResults.add(result);
postContingencyNetworkState.restore();
Expand Down Expand Up @@ -509,13 +514,13 @@ private Optional<OperatorStrategyResult> runActionSimulation(LfNetwork network,
SecurityAnalysisParameters.IncreasedViolationsParameters violationsParameters,
Map<String, LfAction> lfActionById, boolean createResultExtension, LfContingency contingency,
LimitViolationsResult postContingencyLimitViolations, LfNetworkParameters networkParameters,
List<LimitReduction> limitReductions) {
List<LimitReduction> limitReductions, List<OperationalLimitOverride> limitsToOverride) {
OperatorStrategyResult operatorStrategyResult = null;

List<String> actionIds = checkCondition(operatorStrategy, postContingencyLimitViolations);
if (!actionIds.isEmpty()) {
operatorStrategyResult = runActionSimulation(network, context, operatorStrategy, actionIds, preContingencyLimitViolationManager,
violationsParameters, lfActionById, createResultExtension, contingency, networkParameters, limitReductions);
violationsParameters, lfActionById, createResultExtension, contingency, networkParameters, limitReductions, limitsToOverride);
}

return Optional.ofNullable(operatorStrategyResult);
Expand All @@ -525,7 +530,7 @@ protected PostContingencyResult runPostContingencySimulation(LfNetwork network,
LimitViolationManager preContingencyLimitViolationManager,
SecurityAnalysisParameters.IncreasedViolationsParameters violationsParameters,
PreContingencyNetworkResult preContingencyNetworkResult, boolean createResultExtension,
List<LimitReduction> limitReductions) {
List<LimitReduction> limitReductions, List<OperationalLimitOverride> limitsToOverride) {
LOGGER.info("Start post contingency '{}' simulation on network {}", lfContingency.getId(), network);
LOGGER.debug("Contingency '{}' impact on network {}: remove {} buses, remove {} branches, remove {} generators, shift {} shunts, shift {} loads",
lfContingency.getId(), network, lfContingency.getDisabledNetwork().getBuses(), lfContingency.getDisabledNetwork().getBranchesStatus(),
Expand All @@ -535,7 +540,8 @@ protected PostContingencyResult runPostContingencySimulation(LfNetwork network,

// restart LF on post contingency equation system
PostContingencyComputationStatus status = runActionLoadFlow(context); // FIXME: change name.
var postContingencyLimitViolationManager = new LimitViolationManager(preContingencyLimitViolationManager, limitReductions, violationsParameters);
var limitsToOverridePostContingency = limitsToOverride.stream().filter(l -> Objects.equals(l.getContingencyContext(), ContingencyContext.specificContingency(contingency.getId())) && l.getConditionalActionsId().isEmpty()).toList();
var postContingencyLimitViolationManager = new LimitViolationManager(preContingencyLimitViolationManager, limitReductions, limitsToOverridePostContingency, violationsParameters);
var postContingencyNetworkResult = new PostContingencyNetworkResult(network, monitorIndex, createResultExtension, preContingencyNetworkResult, contingency);

if (status.equals(PostContingencyComputationStatus.CONVERGED)) {
Expand Down Expand Up @@ -568,7 +574,8 @@ protected OperatorStrategyResult runActionSimulation(LfNetwork network, C contex
LimitViolationManager preContingencyLimitViolationManager,
SecurityAnalysisParameters.IncreasedViolationsParameters violationsParameters,
Map<String, LfAction> lfActionById, boolean createResultExtension, LfContingency contingency,
LfNetworkParameters networkParameters, List<LimitReduction> limitReductions) {
LfNetworkParameters networkParameters, List<LimitReduction> limitReductions,
List<OperationalLimitOverride> limitsToOverride) {
LOGGER.info("Start operator strategy {} after contingency '{}' simulation on network {}", operatorStrategy.getId(),
operatorStrategy.getContingencyContext().getContingencyId(), network);

Expand All @@ -586,7 +593,8 @@ protected OperatorStrategyResult runActionSimulation(LfNetwork network, C contex

// restart LF on post contingency and post actions equation system
PostContingencyComputationStatus status = runActionLoadFlow(context);
var postActionsViolationManager = new LimitViolationManager(preContingencyLimitViolationManager, limitReductions, violationsParameters);
var limitsToOverridePostActions = limitsToOverride.stream().filter(l -> Objects.equals(l.getContingencyContext(), ContingencyContext.specificContingency(contingency.getId())) && l.getConditionalActionsId().isPresent()).toList();
var postActionsViolationManager = new LimitViolationManager(preContingencyLimitViolationManager, limitReductions, limitsToOverridePostActions, violationsParameters);
var postActionsNetworkResult = new PreContingencyNetworkResult(network, monitorIndex, createResultExtension);

if (status.equals(PostContingencyComputationStatus.CONVERGED)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.iidm.network.LimitType;
import com.powsybl.iidm.network.ThreeSides;
import com.powsybl.iidm.network.TwoSides;
import com.powsybl.iidm.network.util.LimitViolationUtils;
import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
Expand All @@ -19,6 +20,7 @@
import com.powsybl.security.LimitViolationType;
import com.powsybl.security.SecurityAnalysisParameters;
import com.powsybl.security.limitreduction.LimitReduction;
import com.powsybl.security.strategy.OperationalLimitOverride;
import org.apache.commons.lang3.function.TriFunction;
import org.apache.commons.lang3.tuple.Pair;

Expand All @@ -42,17 +44,25 @@ public class LimitViolationManager {

private final Map<Object, LimitViolation> violations = new LinkedHashMap<>();

private final List<OperationalLimitOverride> limitsToOverride;
//private final Map<String, List<OperationalLimitOverride>> limitsToOverridePerElementId = new LinkedHashMap<>();

public LimitViolationManager(LimitViolationManager reference, List<LimitReduction> limitReductions,
List<OperationalLimitOverride> limitsToOverride,
SecurityAnalysisParameters.IncreasedViolationsParameters parameters) {
this.reference = reference;
if (reference != null) {
this.parameters = Objects.requireNonNull(parameters);
}
this.limitReductionManager = LimitReductionManager.create(limitReductions);
this.limitsToOverride = limitsToOverride;
/*for (OperationalLimitOverride limitToOverride: limitsToOverride) {
this.limitsToOverridePerElementId.computeIfAbsent(limitToOverride.getNetworkElementId(), k -> new ArrayList<>()).add(limitToOverride);
}*/
}

public LimitViolationManager(List<LimitReduction> limitReductions) {
this(null, limitReductions, null);
public LimitViolationManager(List<LimitReduction> limitReductions, List<OperationalLimitOverride> limitsToOverride) {
this(null, limitReductions, limitsToOverride, null);
}

public List<LimitViolation> getLimitViolations() {
Expand Down Expand Up @@ -111,7 +121,7 @@ private void detectBranchSideViolations(LfBranch branch, LfBus bus,
Function<LfBranch, Evaluable> pGetter,
ToDoubleFunction<LfBranch> sGetter,
TwoSides side) {
List<LfBranch.LfLimit> limits = limitsGetter.apply(branch, LimitType.CURRENT, limitReductionManager);
List<LfBranch.LfLimit> limits = getLimits(branch, side, LimitType.CURRENT, limitReductionManager, limitsGetter);
if (!limits.isEmpty()) {
double i = iGetter.apply(branch).eval();
limits.stream()
Expand All @@ -121,7 +131,7 @@ private void detectBranchSideViolations(LfBranch branch, LfBus bus,
.ifPresent(this::addBranchLimitViolation);
}

limits = limitsGetter.apply(branch, LimitType.ACTIVE_POWER, limitReductionManager);
limits = getLimits(branch, side, LimitType.ACTIVE_POWER, limitReductionManager, limitsGetter);
if (!limits.isEmpty()) {
double p = pGetter.apply(branch).eval();
limits.stream()
Expand All @@ -131,7 +141,7 @@ private void detectBranchSideViolations(LfBranch branch, LfBus bus,
.ifPresent(this::addBranchLimitViolation);
}

limits = limitsGetter.apply(branch, LimitType.APPARENT_POWER, limitReductionManager);
limits = getLimits(branch, side, LimitType.APPARENT_POWER, limitReductionManager, limitsGetter);
if (!limits.isEmpty()) {
//Apparent power is not relevant for fictitious branches and may be NaN
double s = sGetter.applyAsDouble(branch);
Expand All @@ -145,6 +155,15 @@ private void detectBranchSideViolations(LfBranch branch, LfBus bus,
}
}

private List<LfBranch.LfLimit> getLimits(LfBranch branch, TwoSides branchSide, LimitType type, LimitReductionManager limitReductionManager, TriFunction<LfBranch, LimitType, LimitReductionManager, List<LfBranch.LfLimit>> limitsGetter) {
for (OperationalLimitOverride limitToOverride : this.limitsToOverride) {
if (Objects.equals(limitToOverride.getNetworkElementId(), branch.getId()) && limitToOverride.getSide().map(side -> side.toTwoSides() == branchSide).orElse(true) && limitToOverride.getType() == type) {
return List.of(new LfBranch.LfLimit(LimitViolationUtils.PERMANENT_LIMIT_NAME, Integer.MAX_VALUE, limitToOverride.getMaxValue(), 1)); // TODO compute reduction
}
}
return limitsGetter.apply(branch, type, limitReductionManager);
}

/**
* Detect violation limits on one branch and add them to the given list
* @param branch branch of interest
Expand All @@ -155,7 +174,6 @@ private void detectBranchViolations(LfBranch branch) {
if (branch.getBus1() != null) {
detectBranchSideViolations(branch, branch.getBus1(), LfBranch::getLimits1, LfBranch::getI1, LfBranch::getP1, LfBranch::computeApparentPower1, TwoSides.ONE);
}

if (branch.getBus2() != null) {
detectBranchSideViolations(branch, branch.getBus2(), LfBranch::getLimits2, LfBranch::getI2, LfBranch::getP2, LfBranch::computeApparentPower2, TwoSides.TWO);
}
Expand Down
Loading

0 comments on commit d95df16

Please sign in to comment.