Skip to content

Commit

Permalink
Monitor refactoring (#561)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg authored Jun 19, 2022
1 parent ece3bf8 commit 9aa8502
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ default List<LfLimit> getLimits2(LimitType type) {

void setVoltageControl(TransformerVoltageControl transformerVoltageControl);

BranchResult createBranchResult(double preContingencyP1, double branchInContingencyP1, boolean createExtension);
BranchResult createBranchResult(double preContingencyBranchP1, double preContingencyBranchOfContingencyP1, boolean createExtension);

double computeApparentPower1();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ public Evaluable getI2() {
}

@Override
public BranchResult createBranchResult(double preContingencyP1, double branchInContingencyP1, boolean createExtension) {
public BranchResult createBranchResult(double preContingencyBranchP1, double preContingencyBranchOfContingencyP1, boolean createExtension) {
double flowTransfer = Double.NaN;
if (!Double.isNaN(preContingencyP1) && !Double.isNaN(branchInContingencyP1)) {
flowTransfer = (p1.eval() * PerUnit.SB - preContingencyP1) / branchInContingencyP1;
if (!Double.isNaN(preContingencyBranchP1) && !Double.isNaN(preContingencyBranchOfContingencyP1)) {
flowTransfer = (p1.eval() * PerUnit.SB - preContingencyBranchP1) / preContingencyBranchOfContingencyP1;
}
double currentScale1 = PerUnit.ib(branch.getTerminal1().getVoltageLevel().getNominalV());
double currentScale2 = PerUnit.ib(branch.getTerminal2().getVoltageLevel().getNominalV());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean hasPhaseControlCapability() {
}

@Override
public BranchResult createBranchResult(double preContingencyP1, double branchInContingencyP1, boolean createExtension) {
public BranchResult createBranchResult(double preContingencyBranchP1, double preContingencyBranchOfContingencyP1, boolean createExtension) {
throw new PowsyblException("Unsupported type of branch for branch result: " + getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public boolean hasPhaseControlCapability() {
}

@Override
public BranchResult createBranchResult(double preContingencyP1, double branchInContingencyP1, boolean createExtension) {
public BranchResult createBranchResult(double preContingencyBranchP1, double preContingencyBranchOfContingencyP1, boolean createExtension) {
throw new PowsyblException("Unsupported type of branch for branch result: " + getId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public LfStarBus(LfNetwork network, ThreeWindingsTransformer t3wt) {
nominalV = t3wt.getRatedU0();
}

public static String getId(String id) {
return id + "_BUS0";
}

@Override
public String getId() {
return t3wt.getId() + "_BUS0";
return getId(t3wt.getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Evaluable getI2() {
}

@Override
public BranchResult createBranchResult(double preContingencyP1, double branchInContingencyP1, boolean createExtension) {
public BranchResult createBranchResult(double preContingencyBranchP1, double preContingencyBranchOfContingencyP1, boolean createExtension) {
throw new PowsyblException("Unsupported type of branch for branch result: " + aSwitch.getId());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright (c) 2022, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.openloadflow.sa;

import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.impl.LfLegBranch;
import com.powsybl.openloadflow.network.impl.LfStarBus;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.security.monitor.StateMonitor;
import com.powsybl.security.monitor.StateMonitorIndex;
import com.powsybl.security.results.BranchResult;
import com.powsybl.security.results.BusResult;
import com.powsybl.security.results.ThreeWindingsTransformerResult;

import java.util.*;
import java.util.function.Consumer;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public abstract class AbstractNetworkResult {

protected final LfNetwork network;

protected final StateMonitorIndex monitorIndex;

protected final boolean createResultExtension;

protected final List<BusResult> busResults = new ArrayList<>();

protected final List<ThreeWindingsTransformerResult> threeWindingsTransformerResults = new ArrayList<>();

protected AbstractNetworkResult(LfNetwork network, StateMonitorIndex monitorIndex, boolean createResultExtension) {
this.network = Objects.requireNonNull(network);
this.monitorIndex = Objects.requireNonNull(monitorIndex);
this.createResultExtension = createResultExtension;
}

protected void addResults(StateMonitor monitor, Consumer<LfBranch> branchConsumer) {
Objects.requireNonNull(monitor);
if (!monitor.getBranchIds().isEmpty()) {
network.getBranches().stream()
.filter(lfBranch -> monitor.getBranchIds().contains(lfBranch.getId()))
.filter(lfBranch -> !lfBranch.isDisabled())
.forEach(branchConsumer);
}

if (!monitor.getVoltageLevelIds().isEmpty()) {
network.getBuses().stream()
.filter(lfBus -> monitor.getVoltageLevelIds().contains(lfBus.getVoltageLevelId()))
.filter(lfBus -> !lfBus.isDisabled())
.forEach(lfBus -> busResults.add(lfBus.createBusResult()));
}

if (!monitor.getThreeWindingsTransformerIds().isEmpty()) {
monitor.getThreeWindingsTransformerIds().stream()
.filter(id -> network.getBusById(LfStarBus.getId(id)) != null && !network.getBusById(LfStarBus.getId(id)).isDisabled())
.forEach(id -> threeWindingsTransformerResults.add(createThreeWindingsTransformerResult(id, network)));
}
}

protected void clear() {
busResults.clear();
threeWindingsTransformerResults.clear();
}

private static ThreeWindingsTransformerResult createThreeWindingsTransformerResult(String threeWindingsTransformerId, LfNetwork network) {
LfBranch leg1 = network.getBranchById(LfLegBranch.getId(threeWindingsTransformerId, 1));
LfBranch leg2 = network.getBranchById(LfLegBranch.getId(threeWindingsTransformerId, 2));
LfBranch leg3 = network.getBranchById(LfLegBranch.getId(threeWindingsTransformerId, 3));
double i1Base = PerUnit.ib(leg1.getBus1().getNominalV());
double i2Base = PerUnit.ib(leg2.getBus1().getNominalV());
double i3Base = PerUnit.ib(leg3.getBus1().getNominalV());
return new ThreeWindingsTransformerResult(threeWindingsTransformerId,
leg1.getP1().eval() * PerUnit.SB, leg1.getQ1().eval() * PerUnit.SB, leg1.getI1().eval() * i1Base,
leg2.getP1().eval() * PerUnit.SB, leg2.getQ1().eval() * PerUnit.SB, leg2.getI1().eval() * i2Base,
leg3.getP1().eval() * PerUnit.SB, leg3.getQ1().eval() * PerUnit.SB, leg3.getI1().eval() * i3Base);
}

public List<BusResult> getBusResults() {
return busResults;
}

public List<ThreeWindingsTransformerResult> getThreeWindingsTransformerResults() {
return threeWindingsTransformerResults;
}

public abstract List<BranchResult> getBranchResults();

public abstract void update();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.impl.LfLegBranch;
import com.powsybl.openloadflow.network.util.ActivePowerDistribution;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.security.*;
import com.powsybl.security.interceptors.SecurityAnalysisInterceptor;
import com.powsybl.security.LimitViolation;
import com.powsybl.security.LimitViolationType;
import com.powsybl.security.SecurityAnalysisParameters;
import com.powsybl.security.SecurityAnalysisReport;
import com.powsybl.security.monitor.StateMonitor;
import com.powsybl.security.monitor.StateMonitorIndex;
import com.powsybl.security.results.BranchResult;
import com.powsybl.security.results.BusResult;
import com.powsybl.security.results.ThreeWindingsTransformerResult;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;

Expand All @@ -46,36 +46,20 @@ public abstract class AbstractSecurityAnalysis {

protected final Network network;

protected final LimitViolationDetector detector;

protected final LimitViolationFilter filter;

protected final List<SecurityAnalysisInterceptor> interceptors = new ArrayList<>();

protected final MatrixFactory matrixFactory;

protected final GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory;

protected final StateMonitorIndex monitorIndex;

protected AbstractSecurityAnalysis(Network network, LimitViolationDetector detector, LimitViolationFilter filter,
MatrixFactory matrixFactory, GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory, List<StateMonitor> stateMonitors) {
protected AbstractSecurityAnalysis(Network network, MatrixFactory matrixFactory, GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory,
List<StateMonitor> stateMonitors) {
this.network = Objects.requireNonNull(network);
this.detector = Objects.requireNonNull(detector);
this.filter = Objects.requireNonNull(filter);
this.matrixFactory = Objects.requireNonNull(matrixFactory);
this.connectivityFactory = Objects.requireNonNull(connectivityFactory);
this.monitorIndex = new StateMonitorIndex(stateMonitors);
}

public void addInterceptor(SecurityAnalysisInterceptor interceptor) {
interceptors.add(Objects.requireNonNull(interceptor));
}

public boolean removeInterceptor(SecurityAnalysisInterceptor interceptor) {
return interceptors.remove(Objects.requireNonNull(interceptor));
}

public CompletableFuture<SecurityAnalysisReport> run(String workingVariantId, SecurityAnalysisParameters securityAnalysisParameters,
ContingenciesProvider contingenciesProvider, ComputationManager computationManager) {
Objects.requireNonNull(workingVariantId);
Expand Down Expand Up @@ -247,41 +231,4 @@ protected static boolean violationWeakenedOrEquivalent(LimitViolation violation1
protected static boolean isFlowViolation(LimitViolation limit) {
return limit.getLimitType() == LimitViolationType.CURRENT || limit.getLimitType() == LimitViolationType.ACTIVE_POWER || limit.getLimitType() == LimitViolationType.APPARENT_POWER;
}

protected void addMonitorInfo(LfNetwork network, StateMonitor monitor, Collection<BranchResult> branchResultConsumer,
Collection<BusResult> busResultsConsumer, Collection<ThreeWindingsTransformerResult> threeWindingsTransformerResultConsumer,
Map<String, BranchResult> preContingencyBranchResults, String contingencyId, boolean createResultExtension) {
network.getBranches().stream().filter(lfBranch -> monitor.getBranchIds().contains(lfBranch.getId()))
.filter(lfBranch -> !lfBranch.isDisabled())
.forEach(lfBranch -> {
BranchResult branchResult;
if (contingencyId == null) {
branchResult = lfBranch.createBranchResult(Double.NaN, Double.NaN, createResultExtension);
preContingencyBranchResults.put(lfBranch.getId(), branchResult);
} else {
double preContingencyP1 = preContingencyBranchResults.get(lfBranch.getId()) != null ? preContingencyBranchResults.get(lfBranch.getId()).getP1() : Double.NaN;
double branchInContingencyP1 = preContingencyBranchResults.get(contingencyId) != null ? preContingencyBranchResults.get(contingencyId).getP1() : Double.NaN;
branchResult = lfBranch.createBranchResult(preContingencyP1, branchInContingencyP1, createResultExtension);
}
branchResultConsumer.add(branchResult);
});
network.getBuses().stream().filter(lfBus -> monitor.getVoltageLevelIds().contains(lfBus.getVoltageLevelId()))
.filter(lfBus -> !lfBus.isDisabled())
.forEach(lfBus -> busResultsConsumer.add(lfBus.createBusResult()));
monitor.getThreeWindingsTransformerIds().stream().filter(id -> network.getBusById(id + "_BUS0") != null && !network.getBusById(id + "_BUS0").isDisabled())
.forEach(id -> threeWindingsTransformerResultConsumer.add(createThreeWindingsTransformerResult(id, network)));
}

private ThreeWindingsTransformerResult createThreeWindingsTransformerResult(String threeWindingsTransformerId, LfNetwork network) {
LfBranch leg1 = network.getBranchById(LfLegBranch.getId(threeWindingsTransformerId, 1));
LfBranch leg2 = network.getBranchById(LfLegBranch.getId(threeWindingsTransformerId, 2));
LfBranch leg3 = network.getBranchById(LfLegBranch.getId(threeWindingsTransformerId, 3));
double i1Base = PerUnit.ib(leg1.getBus1().getNominalV());
double i2Base = PerUnit.ib(leg2.getBus1().getNominalV());
double i3Base = PerUnit.ib(leg3.getBus1().getNominalV());
return new ThreeWindingsTransformerResult(threeWindingsTransformerId,
leg1.getP1().eval() * PerUnit.SB, leg1.getQ1().eval() * PerUnit.SB, leg1.getI1().eval() * i1Base,
leg2.getP1().eval() * PerUnit.SB, leg2.getQ1().eval() * PerUnit.SB, leg2.getI1().eval() * i2Base,
leg3.getP1().eval() * PerUnit.SB, leg3.getQ1().eval() * PerUnit.SB, leg3.getI1().eval() * i3Base);
}
}
Loading

0 comments on commit 9aa8502

Please sign in to comment.