Skip to content

Commit

Permalink
Fix (or simplify) load action support (#741)
Browse files Browse the repository at this point in the history
Signed-off-by: Anne Tilloy <anne.tilloy@rte-france.com>
Co-authored-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@rte-france.com>
  • Loading branch information
annetill and geofjamg authored Mar 7, 2023
1 parent d864b08 commit ac5787b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private boolean switchPvPq(List<PvToPqBus> pvToPqBuses, int remainingPvBusCount,

Reports.reportPvToPqBuses(reporter, pvToPqBuses.size(), modifiedRemainingPvBusCount);

LOGGER.info("{} buses switched PV -> PQ ({} bus remains PV}", pvToPqBuses.size(), modifiedRemainingPvBusCount);
LOGGER.info("{} buses switched PV -> PQ ({} bus remains PV)", pvToPqBuses.size(), modifiedRemainingPvBusCount);

return done;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static void distributeSlack(Collection<LfBus> buses, LoadFlowParameters.
activePowerDistribution.run(buses, mismatch);
}

private static double getActivePowerMismatch(Collection<LfBus> buses) {
public static double getActivePowerMismatch(Collection<LfBus> buses) {
double mismatch = 0;
for (LfBus b : buses) {
mismatch += b.getGenerationTargetP() - b.getLoadTargetP();
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/powsybl/openloadflow/network/LfAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ private static final class LoadShift {

private final String loadId;

private final double p0;

private final PowerShift powerShift;

private LoadShift(LfBus bus, String loadId, double p0, PowerShift powerShift) {
private LoadShift(LfBus bus, String loadId, PowerShift powerShift) {
this.bus = bus;
this.loadId = loadId;
this.p0 = p0;
this.powerShift = powerShift;
}
}
Expand Down Expand Up @@ -127,8 +124,7 @@ private static Optional<LfAction> create(LoadAction action, LfNetwork lfNetwork,
// In case of a power shift, we suppose that the shift on a load P0 is exactly the same on the variable active power
// of P0 that could be described in a LoadDetail extension.
PowerShift powerShift = new PowerShift(activePowerShift / PerUnit.SB, activePowerShift / PerUnit.SB, reactivePowerShift / PerUnit.SB);
return Optional.of(new LfAction(action.getId(), null, null, null,
new LoadShift(lfBus, load.getId(), load.getP0(), powerShift)));
return Optional.of(new LfAction(action.getId(), null, null, null, new LoadShift(lfBus, load.getId(), powerShift)));
}
return Optional.empty(); // could be in another component or in contingency.
}
Expand Down Expand Up @@ -259,12 +255,8 @@ public void apply(LoadFlowParameters.BalanceType balanceType) {
LfBus bus = loadShift.bus;
String loadId = loadShift.loadId;
if (!bus.getAggregatedLoads().isDisabled(loadId)) {
Double loadP0 = loadShift.p0;
PowerShift shift = loadShift.powerShift;
double newP0 = loadP0 / PerUnit.SB + shift.getActive();
double oldUpdatedP0 = LfContingency.getUpdatedLoadP0(bus, balanceType, loadP0 / PerUnit.SB, loadP0 / PerUnit.SB);
double newUpdatedP0 = LfContingency.getUpdatedLoadP0(bus, balanceType, newP0, newP0);
bus.setLoadTargetP(bus.getLoadTargetP() + newUpdatedP0 - oldUpdatedP0);
bus.setLoadTargetP(bus.getLoadTargetP() + shift.getActive());
bus.setLoadTargetQ(bus.getLoadTargetQ() + shift.getReactive());
bus.getAggregatedLoads().setAbsVariableLoadTargetP(bus.getAggregatedLoads().getAbsVariableLoadTargetP()
+ Math.signum(shift.getActive()) * Math.abs(shift.getVariableActive()) * PerUnit.SB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void apply(LoadFlowParameters.BalanceType balanceType) {
}
}

public static double getUpdatedLoadP0(LfBus bus, LoadFlowParameters.BalanceType balanceType, double initialP0, double initialVariableActivePower) {
private static double getUpdatedLoadP0(LfBus bus, LoadFlowParameters.BalanceType balanceType, double initialP0, double initialVariableActivePower) {
double factor = 0.0;
if (bus.getAggregatedLoads().getLoadCount() > 0) {
if (balanceType == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD) {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static com.powsybl.openloadflow.sa.AcSecurityAnalysis.distributedMismatch;

public class DcSecurityAnalysis extends AbstractSecurityAnalysis<DcVariableType, DcEquationType, DcLoadFlowParameters, DcLoadFlowContext> {

private static class DcSecurityAnalysisContext {
Expand Down Expand Up @@ -284,7 +286,11 @@ private List<OperatorStrategyResult> runActionSimulations(DcSecurityAnalysisCont
new DcLoadFlowEngine(lfContext).run();
NetworkState networkState = NetworkState.save(lfNetwork);

OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters());
SecurityAnalysisParameters securityAnalysisParameters = context.getParameters();
OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(securityAnalysisParameters);
LoadFlowParameters loadFlowParameters = securityAnalysisParameters.getLoadFlowParameters();
OpenLoadFlowParameters openLoadFlowParameters = OpenLoadFlowParameters.get(loadFlowParameters);

boolean createResultExtension = openSecurityAnalysisParameters.isCreateResultExtension();

var preContingencyLimitViolationManager = new LimitViolationManager();
Expand All @@ -306,8 +312,10 @@ private List<OperatorStrategyResult> runActionSimulations(DcSecurityAnalysisCont
if (checkCondition(operatorStrategy, context.getLimitViolationsPerContingencyId().get(propagatedContingency.getContingency().getId()))) {
propagatedContingency.toLfContingency(lfNetwork)
.ifPresent(lfContingency -> {
lfContingency.apply(context.getParameters().getLoadFlowParameters().getBalanceType());
OperatorStrategyResult result = runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(),
lfContingency.apply(loadFlowParameters.getBalanceType());
distributedMismatch(lfNetwork, DcLoadFlowEngine.getActivePowerMismatch(lfNetwork.getBuses().stream().filter(bus -> !bus.isDisabled()).collect(Collectors.toSet())),
loadFlowParameters, openLoadFlowParameters);
OperatorStrategyResult result = runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, securityAnalysisParameters.getIncreasedViolationsParameters(),
lfActionById, createResultExtension, lfContingency, parameters.getBalanceType());
operatorStrategyResults.add(result);
networkState.restore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2273,70 +2273,73 @@ void testSaDcPhaseTapChangerTapPositionAction() {
assertEquals(network.getBranch("S_SO_2").getTerminal2().getP(), brRel.getP2(), LoadFlowAssert.DELTA_POWER);
}

@Test
void testLoadAction() {
private void testLoadAction(boolean dc) {
MatrixFactory matrixFactory = new DenseMatrixFactory();
GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory = new NaiveGraphConnectivityFactory<>(LfBus::getNum);
securityAnalysisProvider = new OpenSecurityAnalysisProvider(matrixFactory, connectivityFactory);

Network network = DistributedSlackNetworkFactory.createNetworkWithLoads();
network.getLine("l24").newActivePowerLimits1().setPermanentLimit(150).add();
double initialL4 = network.getLoad("l4").getP0();

List<Contingency> contingencies = Stream.of("l2")
.map(id -> new Contingency(id, new LoadContingency(id)))
.collect(Collectors.toList());

List<Action> actions = List.of(new LoadActionBuilder().withId("action1").withLoadId("l4").withRelativeValue(false).withActivePowerValue(90).build(),
new LoadActionBuilder().withId("action2").withLoadId("l1").withRelativeValue(true).withActivePowerValue(50).build(),
new LoadActionBuilder().withId("action3").withLoadId("l2").withRelativeValue(true).withActivePowerValue(10).build());
new LoadActionBuilder().withId("action2").withLoadId("l1").withRelativeValue(true).withActivePowerValue(50).build(),
new LoadActionBuilder().withId("action3").withLoadId("l2").withRelativeValue(true).withActivePowerValue(10).build());

List<OperatorStrategy> operatorStrategies = List.of(new OperatorStrategy("strategy1", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action1", "action2")),
new OperatorStrategy("strategy2", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action3")));
new OperatorStrategy("strategy2", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action3")));

List<StateMonitor> monitors = createAllBranchesMonitors(network);

LoadFlowParameters parameters = new LoadFlowParameters();
parameters.setDc(dc);
parameters.setBalanceType(LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD);
SecurityAnalysisParameters securityAnalysisParameters = new SecurityAnalysisParameters();
securityAnalysisParameters.setLoadFlowParameters(parameters);

SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, securityAnalysisParameters,
operatorStrategies, actions, Reporter.NO_OP);

PostContingencyResult postContingencyResult = getPostContingencyResult(result, "l2");
assertEquals(200.0, postContingencyResult.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(57.142, postContingencyResult.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-71.43, postContingencyResult.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);
network.getLoad("l2").getTerminal().disconnect();
LoadFlow.run(network, parameters);

PostContingencyResult postContingencyResult = getPostContingencyResult(result, "l2");
assertEquals(network.getLine("l24").getTerminal1().getP(), postContingencyResult.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l14").getTerminal1().getP(), postContingencyResult.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l34").getTerminal1().getP(), postContingencyResult.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);

network.getLoadStream().filter(load -> !load.getId().equals("l2")).forEach(load -> load.setP0(load.getTerminal().getP()));
network.getLoad("l2").setP0(0);
double postContingencyL1 = network.getLoad("l1").getP0();
double postContingencyL2 = network.getLoad("l2").getP0();
double postContingencyL4 = network.getLoad("l4").getP0();

network.getLoad("l4").setP0(90 + postContingencyL4 - initialL4);
network.getLoad("l1").setP0(postContingencyL1 + 50);
LoadFlow.run(network, parameters);
OperatorStrategyResult operatorStrategyResult = getOperatorStrategyResult(result, "strategy1");
assertEquals(200.0, operatorStrategyResult.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-14.287, operatorStrategyResult.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-71.43, operatorStrategyResult.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l24").getTerminal1().getP(), operatorStrategyResult.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l14").getTerminal1().getP(), operatorStrategyResult.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l34").getTerminal1().getP(), operatorStrategyResult.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);

network.getLoad("l1").setP0(postContingencyL1);
network.getLoad("l2").setP0(postContingencyL2); // because in contingency
network.getLoad("l4").setP0(postContingencyL4);
LoadFlow.run(network, parameters);
OperatorStrategyResult operatorStrategyResult2 = getOperatorStrategyResult(result, "strategy2");
assertEquals(200.0, operatorStrategyResult2.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(57.142, operatorStrategyResult2.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-71.43, operatorStrategyResult2.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);

parameters.setDc(true);

SecurityAnalysisResult dcResult = runSecurityAnalysis(network, contingencies, monitors, securityAnalysisParameters,
operatorStrategies, actions, Reporter.NO_OP);

PostContingencyResult dcPostContingencyResult = getPostContingencyResult(dcResult, "l2");
assertEquals(200.0, dcPostContingencyResult.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(57.142, dcPostContingencyResult.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-71.428, dcPostContingencyResult.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);

OperatorStrategyResult dcOperatorStrategyResult = getOperatorStrategyResult(dcResult, "strategy1");
assertEquals(200.0, dcOperatorStrategyResult.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-14.286, dcOperatorStrategyResult.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-71.428, dcOperatorStrategyResult.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l24").getTerminal1().getP(), operatorStrategyResult2.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l14").getTerminal1().getP(), operatorStrategyResult2.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l34").getTerminal1().getP(), operatorStrategyResult2.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);
}

OperatorStrategyResult dcOperatorStrategyResult2 = getOperatorStrategyResult(dcResult, "strategy2");
assertEquals(200.0, dcOperatorStrategyResult2.getNetworkResult().getBranchResult("l24").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(57.142, dcOperatorStrategyResult2.getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(-71.428, dcOperatorStrategyResult2.getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);
@Test
void testLoadAction() {
testLoadAction(true);
testLoadAction(false);
}

@Test
Expand Down

0 comments on commit ac5787b

Please sign in to comment.