Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Returning real slack buses mismatches in SlackBusResult when using multiple slacks #1161

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -120,7 +121,7 @@ private static AcLoadFlowResult run(AcLoadFlowContext context) {
context.setNetworkUpdated(false);
return result;
}
return new AcLoadFlowResult(context.getNetwork(), 0, 0, AcSolverStatus.CONVERGED, OuterLoopResult.stable(), 0d, 0d);
return new AcLoadFlowResult(context.getNetwork(), 0, 0, AcSolverStatus.CONVERGED, OuterLoopResult.stable(), Collections.EMPTY_LIST, 0d);
}

public List<AcLoadFlowResult> run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,9 @@ private LoadFlowResult runAc(Network network, LoadFlowParameters parameters, Ope
private static ReferenceBusAndSlackBusesResults buildReferenceBusAndSlackBusesResults(AbstractLoadFlowResult result) {
String referenceBusId = null;
List<LoadFlowResult.SlackBusResult> slackBusResultList = new ArrayList<>();
double slackBusActivePowerMismatch = result.getSlackBusActivePowerMismatch() * PerUnit.SB;
if (result.getNetwork().getValidity() == LfNetwork.Validity.VALID) {
referenceBusId = result.getNetwork().getReferenceBus().getId();
List<LfBus> slackBuses = result.getNetwork().getSlackBuses();
slackBusResultList = slackBuses.stream().map(
b -> (LoadFlowResult.SlackBusResult) new LoadFlowResultImpl.SlackBusResultImpl(b.getId(),
slackBusActivePowerMismatch / slackBuses.size())).toList();
slackBusResultList = result.getSlackBusResults();
}
return new ReferenceBusAndSlackBusesResults(referenceBusId, slackBusResultList);
}
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/powsybl/openloadflow/ac/AcLoadFlowResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.util.PerUnit;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class AcLoadFlowResult extends AbstractLoadFlowResult {

public static AcLoadFlowResult createNoCalculationResult(LfNetwork network) {
return new AcLoadFlowResult(network, 0, 0, AcSolverStatus.NO_CALCULATION, OuterLoopResult.stable(), Double.NaN, Double.NaN);
return new AcLoadFlowResult(network, 0, 0, AcSolverStatus.NO_CALCULATION, OuterLoopResult.stable(), Collections.EMPTY_LIST, Double.NaN);
}

private final int solverIterations;
Expand All @@ -32,8 +35,8 @@ public static AcLoadFlowResult createNoCalculationResult(LfNetwork network) {

public AcLoadFlowResult(LfNetwork network, int outerLoopIterations, int solverIterations,
AcSolverStatus solverStatus, OuterLoopResult outerLoopResult,
double slackBusActivePowerMismatch, double distributedActivePower) {
super(network, slackBusActivePowerMismatch, outerLoopIterations, outerLoopResult, distributedActivePower);
List<LoadFlowResult.SlackBusResult> slackBusResults, double distributedActivePower) {
super(network, slackBusResults, outerLoopIterations, outerLoopResult, distributedActivePower);
this.solverIterations = solverIterations;
this.solverStatus = Objects.requireNonNull(solverStatus);
}
Expand Down Expand Up @@ -85,7 +88,11 @@ public String toString() {
+ ", newtonRaphsonIterations=" + solverIterations
+ ", solverStatus=" + solverStatus
+ ", outerLoopStatus=" + outerLoopResult.status()
+ ", slackBusActivePowerMismatch=" + slackBusActivePowerMismatch * PerUnit.SB
+ ", slackBusResults=SlackBusResult("
+ slackBusResults.stream()
.map(s -> "(id=" + s.getId() + ", activePowerMismatch=" + s.getActivePowerMismatch() + ")")
.collect(Collectors.joining(""))
+ ")"
+ ", distributedActivePower=" + distributedActivePower * PerUnit.SB
+ ")";
}
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/powsybl/openloadflow/ac/AcloadFlowEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import com.google.common.collect.Lists;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.loadflow.LoadFlowResultImpl;
import com.powsybl.openloadflow.ac.equations.AcEquationType;
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.ac.outerloop.AcActivePowerDistributionOuterLoop;
Expand All @@ -20,6 +22,7 @@
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.util.PreviousValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.VoltageInitializer;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.util.Reports;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -63,6 +66,8 @@ private static class RunningContext {
private final MutableInt nrTotalIterations = new MutableInt();

private OuterLoopResult lastOuterLoopResult = OuterLoopResult.stable();

private List<LoadFlowResult.SlackBusResult> slackBusResults;
}

private void runOuterLoop(AcOuterLoop outerLoop, AcOuterLoopContext outerLoopContext, AcSolver solver, RunningContext runningContext) {
Expand Down Expand Up @@ -138,7 +143,8 @@ public AcLoadFlowResult run() {
LOGGER.info("Network must have at least one bus with generator voltage control enabled");
Reports.reportNetworkMustHaveAtLeastOneBusGeneratorVoltageControlEnabled(reportNode);
runningContext.lastSolverResult = new AcSolverResult(AcSolverStatus.SOLVER_FAILED, 0, Double.NaN);
return buildAcLoadFlowResult(runningContext, OuterLoopResult.stable(), distributedActivePower);
List<LoadFlowResult.SlackBusResult> slackBusResults = getSlackBusResults(context.getNetwork());
return buildAcLoadFlowResult(runningContext, OuterLoopResult.stable(), slackBusResults, distributedActivePower);
}

AcSolver solver = solverFactory.create(context.getNetwork(),
Expand Down Expand Up @@ -217,17 +223,24 @@ public AcLoadFlowResult run() {
? new OuterLoopResult(runningContext.lastOuterLoopResult.outerLoopName(), OuterLoopStatus.STABLE, runningContext.lastOuterLoopResult.statusText()) :
new OuterLoopResult(runningContext.lastOuterLoopResult.outerLoopName(), OuterLoopStatus.UNSTABLE, runningContext.lastOuterLoopResult.statusText());
}
List<LoadFlowResult.SlackBusResult> slackBusResults = getSlackBusResults(context.getNetwork());

return buildAcLoadFlowResult(runningContext, outerLoopFinalResult, slackBusResults, distributedActivePower);
}

return buildAcLoadFlowResult(runningContext, outerLoopFinalResult, distributedActivePower);
public static List<LoadFlowResult.SlackBusResult> getSlackBusResults(LfNetwork network) {
return network.getSlackBuses().stream().map(
b -> (LoadFlowResult.SlackBusResult) new LoadFlowResultImpl.SlackBusResultImpl(b.getId(),
b.getMismatchP() * PerUnit.SB)).toList();
}

private AcLoadFlowResult buildAcLoadFlowResult(RunningContext runningContext, OuterLoopResult outerLoopFinalResult, double distributedActivePower) {
private AcLoadFlowResult buildAcLoadFlowResult(RunningContext runningContext, OuterLoopResult outerLoopFinalResult, List<LoadFlowResult.SlackBusResult> slackBusResults, double distributedActivePower) {
AcLoadFlowResult result = new AcLoadFlowResult(context.getNetwork(),
runningContext.outerLoopTotalIterations,
runningContext.nrTotalIterations.getValue(),
runningContext.lastSolverResult.getStatus(),
outerLoopFinalResult,
runningContext.lastSolverResult.getSlackBusActivePowerMismatch(),
slackBusResults,
distributedActivePower
);

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.google.common.collect.Lists;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.loadflow.LoadFlowResultImpl;
import com.powsybl.math.matrix.MatrixException;
import com.powsybl.openloadflow.OpenLoadFlowParameters;
import com.powsybl.openloadflow.dc.equations.DcEquationType;
Expand Down Expand Up @@ -80,6 +82,12 @@ public static double getActivePowerMismatch(Collection<LfBus> buses) {
return -mismatch;
}

public static List<LoadFlowResult.SlackBusResult> getSlackBusResults(LfNetwork network) {
return network.getSlackBuses().stream().map(
b -> (LoadFlowResult.SlackBusResult) new LoadFlowResultImpl.SlackBusResultImpl(b.getId(),
b.getMismatchP() * PerUnit.SB)).toList();
}

public static void initStateVector(LfNetwork network, EquationSystem<DcVariableType, DcEquationType> equationSystem, VoltageInitializer initializer) {
double[] x = new double[equationSystem.getIndex().getSortedVariablesToFind().size()];
for (Variable<DcVariableType> v : equationSystem.getIndex().getSortedVariablesToFind()) {
Expand Down Expand Up @@ -241,7 +249,7 @@ public DcLoadFlowResult run() {
runningContext.lastSolverSuccess = false;
runningContext.lastOuterLoopResult = new OuterLoopResult("DistributedSlack", OuterLoopStatus.FAILED, resultWbh.failedMessage());
Reports.reportDcLfComplete(reportNode, runningContext.lastSolverSuccess, runningContext.lastOuterLoopResult.status().name());
return buildDcLoadFlowResult(network, runningContext, initialSlackBusActivePowerMismatch, distributedActivePower);
return buildDcLoadFlowResult(network, runningContext, distributedActivePower);
}
}

Expand Down Expand Up @@ -299,20 +307,19 @@ public DcLoadFlowResult run() {

Reports.reportDcLfComplete(reportNode, runningContext.lastSolverSuccess, runningContext.lastOuterLoopResult.status().name());

return buildDcLoadFlowResult(network, runningContext, initialSlackBusActivePowerMismatch, distributedActivePower);
return buildDcLoadFlowResult(network, runningContext, distributedActivePower);
}

DcLoadFlowResult buildDcLoadFlowResult(LfNetwork network, RunningContext runningContext, double initialSlackBusActivePowerMismatch, double finalDistributedActivePower) {
double slackBusActivePowerMismatch;
DcLoadFlowResult buildDcLoadFlowResult(LfNetwork network, RunningContext runningContext, double finalDistributedActivePower) {
List<LoadFlowResult.SlackBusResult> slackBusResults;
double distributedActivePower;
if (runningContext.lastSolverSuccess && runningContext.lastOuterLoopResult.status() == OuterLoopStatus.STABLE) {
slackBusActivePowerMismatch = getActivePowerMismatch(network.getBuses());
distributedActivePower = finalDistributedActivePower;
} else {
slackBusActivePowerMismatch = initialSlackBusActivePowerMismatch;
distributedActivePower = 0.0;
}
DcLoadFlowResult result = new DcLoadFlowResult(network, runningContext.outerLoopTotalIterations, runningContext.lastSolverSuccess, runningContext.lastOuterLoopResult, slackBusActivePowerMismatch, distributedActivePower);
slackBusResults = getSlackBusResults(network);
DcLoadFlowResult result = new DcLoadFlowResult(network, runningContext.outerLoopTotalIterations, runningContext.lastSolverSuccess, runningContext.lastOuterLoopResult, slackBusResults, distributedActivePower);
LOGGER.info("DC loadflow complete on network {} (result={})", context.getNetwork(), result);
return result;
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.util.PerUnit;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
Expand All @@ -22,11 +26,11 @@ public class DcLoadFlowResult extends AbstractLoadFlowResult {
private final boolean solverSuccess;

public static DcLoadFlowResult createNoCalculationResult(LfNetwork network) {
return new DcLoadFlowResult(network, 0, false, OuterLoopResult.stable(), Double.NaN, Double.NaN);
return new DcLoadFlowResult(network, 0, false, OuterLoopResult.stable(), Collections.EMPTY_LIST, Double.NaN);
}

public DcLoadFlowResult(LfNetwork network, int outerLoopIterations, boolean solverSuccess, OuterLoopResult outerLoopResult, double slackBusActivePowerMismatch, double distributedActivePower) {
super(network, slackBusActivePowerMismatch, outerLoopIterations, outerLoopResult, distributedActivePower);
public DcLoadFlowResult(LfNetwork network, int outerLoopIterations, boolean solverSuccess, OuterLoopResult outerLoopResult, List<LoadFlowResult.SlackBusResult> slackBusResults, double distributedActivePower) {
super(network, slackBusResults, outerLoopIterations, outerLoopResult, distributedActivePower);
this.solverSuccess = solverSuccess;
}

Expand Down Expand Up @@ -56,7 +60,11 @@ public String toString() {
return "DcLoadFlowResult(outerLoopIterations=" + outerLoopIterations
+ ", solverSuccess=" + solverSuccess
+ ", outerLoopStatus=" + outerLoopResult.status()
+ ", slackBusActivePowerMismatch=" + slackBusActivePowerMismatch * PerUnit.SB
+ ", slackBusResults=SlackBusResult("
+ slackBusResults.stream()
.map(s -> "(id=" + s.getId() + ", activePowerMismatch=" + s.getActivePowerMismatch() + ")")
.collect(Collectors.joining(""))
+ ")"
+ ", distributedActivePower=" + distributedActivePower * PerUnit.SB
+ ")";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.openloadflow.lf.outerloop.OuterLoopResult;
import com.powsybl.openloadflow.network.LfNetwork;

import java.util.List;
import java.util.Objects;

/**
Expand All @@ -18,15 +19,18 @@
public abstract class AbstractLoadFlowResult implements LoadFlowResult {

protected final LfNetwork network;

protected final double slackBusActivePowerMismatch;
protected final List<com.powsybl.loadflow.LoadFlowResult.SlackBusResult> slackBusResults;
protected final int outerLoopIterations;
protected final OuterLoopResult outerLoopResult;
protected final double distributedActivePower;

protected AbstractLoadFlowResult(LfNetwork network, double slackBusActivePowerMismatch, int outerLoopIterations, OuterLoopResult outerLoopResult, double distributedActivePower) {
protected AbstractLoadFlowResult(LfNetwork network,
List<com.powsybl.loadflow.LoadFlowResult.SlackBusResult> slackBusResults,
int outerLoopIterations,
OuterLoopResult outerLoopResult,
double distributedActivePower) {
this.network = Objects.requireNonNull(network);
this.slackBusActivePowerMismatch = slackBusActivePowerMismatch;
this.slackBusResults = slackBusResults;
this.outerLoopIterations = outerLoopIterations;
this.outerLoopResult = Objects.requireNonNull(outerLoopResult);
this.distributedActivePower = distributedActivePower;
Expand All @@ -37,9 +41,14 @@ public LfNetwork getNetwork() {
return network;
}

@Override
/*@Override
public double getSlackBusActivePowerMismatch() {
return slackBusActivePowerMismatch;
}*/

@Override
public List<com.powsybl.loadflow.LoadFlowResult.SlackBusResult> getSlackBusResults() {
return slackBusResults;
}

public int getOuterLoopIterations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import com.powsybl.openloadflow.network.LfNetwork;

import java.util.List;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
Expand All @@ -21,7 +23,7 @@ record Status(com.powsybl.loadflow.LoadFlowResult.ComponentResult.Status status,

LfNetwork getNetwork();

double getSlackBusActivePowerMismatch();
List<com.powsybl.loadflow.LoadFlowResult.SlackBusResult> getSlackBusResults();

Status toComponentResultStatus();
}
Loading
Loading