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

Improve performance of incremental outer loops #895

Merged
merged 22 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -23,6 +23,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -86,7 +88,27 @@ private static int[] createControllerShuntIndex(LfNetwork network, List<LfShunt>
private static DenseMatrix calculateSensitivityValues(List<LfShunt> controllerShunts, int[] controllerShuntIndex,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
JacobianMatrix<AcVariableType, AcEquationType> j) {
DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), controllerShunts.size());
int nRows = equationSystem.getIndex().getSortedEquationsToSolve().size();
int nColumns = controllerShunts.size();

if (nColumns == 1) {
caioluke marked this conversation as resolved.
Show resolved Hide resolved
double[] vectorRhs = new double[nRows];
Arrays.fill(vectorRhs, 0);
for (LfShunt controllerShunt : controllerShunts) {
equationSystem.getEquation(controllerShunt.getNum(), AcEquationType.SHUNT_TARGET_B)
.ifPresent(equation -> vectorRhs[equation.getColumn()] = 1d);
}
j.solveTransposed(vectorRhs);

// generate matrix with solution in rhs
DenseMatrix rhs = new DenseMatrix(nRows, 1);
for (int i = 0; i < nRows; i++) {
rhs.set(i, 0, vectorRhs[i]);
}
return rhs;
}

DenseMatrix rhs = new DenseMatrix(nRows, nColumns);
for (LfShunt controllerShunt : controllerShunts) {
equationSystem.getEquation(controllerShunt.getNum(), AcEquationType.SHUNT_TARGET_B)
.ifPresent(equation -> rhs.set(equation.getColumn(), controllerShuntIndex[controllerShunt.getNum()], 1d));
Expand Down Expand Up @@ -148,20 +170,43 @@ public OuterLoopStatus check(AcOuterLoopContext context, Reporter reporter) {
AcLoadFlowContext loadFlowContext = context.getLoadFlowContext();
var contextData = (IncrementalContextData) context.getData();

List<LfShunt> controllerShunts = network.getControllerElements(VoltageControl.Type.SHUNT);
SensitivityContext sensitivityContext = new SensitivityContext(network, controllerShunts,
List<LfBus> controlledBuses = network.getControlledBuses(VoltageControl.Type.SHUNT);

// check which shunts are not within their deadbands
List<LfShunt> controllerShuntsOutsideOfDeadband = new ArrayList<>();
List<LfBus> controlledBusesOutsideOfDeadband = new ArrayList<>();
controlledBuses.forEach(controlledBus -> {
ShuntVoltageControl voltageControl = controlledBus.getShuntVoltageControl().orElseThrow();
double diffV = voltageControl.getTargetValue() - voltageControl.getControlledBus().getV();
double halfTargetDeadband = getHalfTargetDeadband(voltageControl);
if (Math.abs(diffV) > halfTargetDeadband) {
List<LfShunt> controllers = voltageControl.getMergedControllerElements().stream()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a difference here in the way we get controllers from a controlled. In previous code, we use IncrementalContextData.getControllerElements which has a filter on voltage control type. Is it equivalent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is equivalent. I directly call the method for the specific type of voltage control: getShuntVoltageControl or getTransformerVoltageControl. And I also filter out the disabled controllers.

.filter(b -> !b.isDisabled())
.collect(Collectors.toList());

controllerShuntsOutsideOfDeadband.addAll(controllers);
controlledBusesOutsideOfDeadband.add(controlledBus);
}
});

// all shunts are within their deadbands
if (controllerShuntsOutsideOfDeadband.isEmpty()) {
return status.getValue();
}

SensitivityContext sensitivityContext = new SensitivityContext(network, controllerShuntsOutsideOfDeadband,
loadFlowContext.getEquationSystem(), loadFlowContext.getJacobianMatrix());

network.getControlledBuses(VoltageControl.Type.SHUNT)
.forEach(controlledBus -> {
ShuntVoltageControl voltageControl = controlledBus.getShuntVoltageControl().orElseThrow();
double diffV = voltageControl.getTargetValue() - voltageControl.getControlledBus().getV();
List<LfShunt> sortedControllers = voltageControl.getMergedControllerElements().stream()
.filter(shunt -> !shunt.isDisabled())
.sorted(Comparator.comparingDouble(LfShunt::getBMagnitude).reversed())
.collect(Collectors.toList());
adjustB(voltageControl, sortedControllers, controlledBus, contextData, sensitivityContext, diffV, status);
});
controlledBusesOutsideOfDeadband.forEach(controlledBus -> {
ShuntVoltageControl voltageControl = controlledBus.getShuntVoltageControl().orElseThrow();
double diffV = voltageControl.getTargetValue() - voltageControl.getControlledBus().getV();
List<LfShunt> sortedControllers = voltageControl.getMergedControllerElements().stream()
.filter(shunt -> !shunt.isDisabled())
.sorted(Comparator.comparingDouble(LfShunt::getBMagnitude).reversed())
.collect(Collectors.toList());
adjustB(voltageControl, sortedControllers, controlledBus, contextData, sensitivityContext, diffV, status);
});

return status.getValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.openloadflow.ac.outerloop;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.math.matrix.DenseMatrix;
import com.powsybl.openloadflow.lf.outerloop.IncrementalContextData;
Expand All @@ -26,10 +27,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -85,7 +83,27 @@ public SensitivityContext(LfNetwork network, List<LfBranch> controllerBranches,
private static DenseMatrix calculateSensitivityValues(List<LfBranch> controllerBranches, int[] controllerBranchIndex,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
JacobianMatrix<AcVariableType, AcEquationType> j) {
DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), controllerBranches.size());
int nRows = equationSystem.getIndex().getSortedEquationsToSolve().size();
int nColumns = controllerBranches.size();

if (nColumns == 1) {
caioluke marked this conversation as resolved.
Show resolved Hide resolved
double[] vectorRhs = new double[nRows];
Arrays.fill(vectorRhs, 0);
for (LfBranch controllerBranch : controllerBranches) {
equationSystem.getEquation(controllerBranch.getNum(), AcEquationType.BRANCH_TARGET_RHO1)
.ifPresent(equation -> vectorRhs[equation.getColumn()] = 1d);
}
j.solveTransposed(vectorRhs);

// generate matrix with solution from rhs
DenseMatrix rhs = new DenseMatrix(nRows, 1);
for (int i = 0; i < nRows; i++) {
rhs.set(i, 0, vectorRhs[i]);
}
return rhs;
}

DenseMatrix rhs = new DenseMatrix(nRows, nColumns);
for (LfBranch controllerBranch : controllerBranches) {
equationSystem.getEquation(controllerBranch.getNum(), AcEquationType.BRANCH_TARGET_RHO1)
.ifPresent(equation -> rhs.set(equation.getColumn(), controllerBranchIndex[controllerBranch.getNum()], 1d));
Expand Down Expand Up @@ -196,43 +214,64 @@ public OuterLoopStatus check(AcOuterLoopContext context, Reporter reporter) {
AcLoadFlowContext loadFlowContext = context.getLoadFlowContext();
var contextData = (IncrementalContextData) context.getData();

List<LfBranch> controllerBranches = network.getControllerElements(VoltageControl.Type.TRANSFORMER);
SensitivityContext sensitivityContext = new SensitivityContext(network, controllerBranches,
loadFlowContext.getEquationSystem(), loadFlowContext.getJacobianMatrix());

// for synthetics logs
List<String> controlledBusesOutsideOfDeadband = new ArrayList<>();
List<String> controlledBusesAdjusted = new ArrayList<>();
List<String> controlledBusesWithAllItsControllersToLimit = new ArrayList<>();

List<LfBus> controlledBuses = network.getControlledBuses(VoltageControl.Type.TRANSFORMER);

// check which branches are not within their deadbands
List<LfBranch> controllerBranchesOutsideOfDeadband = new ArrayList<>();
List<LfBus> controlledBusesOutsideOfDeadband = new ArrayList<>();
controlledBuses.forEach(controlledBus -> {
TransformerVoltageControl voltageControl = controlledBus.getTransformerVoltageControl().orElseThrow();
double diffV = getDiffV(voltageControl);
double halfTargetDeadband = getHalfTargetDeadband(voltageControl);
if (Math.abs(diffV) > halfTargetDeadband) {
controlledBusesOutsideOfDeadband.add(controlledBus.getId());
List<LfBranch> controllers = voltageControl.getMergedControllerElements().stream()
.filter(b -> !b.isDisabled())
.collect(Collectors.toList());

LOGGER.trace("Controlled bus '{}' ({} controllers) is outside of its deadband (half is {} kV) and could need a voltage adjustment of {} kV",
controlledBus.getId(), controllers.size(), halfTargetDeadband * controlledBus.getNominalV(), diffV * controlledBus.getNominalV());
boolean adjusted;
if (controllers.size() == 1) {
adjusted = adjustWithOneController(controllers.get(0), controlledBus, contextData, sensitivityContext, diffV, controlledBusesWithAllItsControllersToLimit);
} else {
adjusted = adjustWithSeveralControllers(controllers, controlledBus, contextData, sensitivityContext, diffV, halfTargetDeadband, controlledBusesWithAllItsControllersToLimit);
}
if (adjusted) {
controlledBusesAdjusted.add(controlledBus.getId());
status.setValue(OuterLoopStatus.UNSTABLE);
}

controllerBranchesOutsideOfDeadband.addAll(controllers);
controlledBusesOutsideOfDeadband.add(controlledBus);
}
});

// all branches are within their deadbands
if (controllerBranchesOutsideOfDeadband.isEmpty()) {
return status.getValue();
}

SensitivityContext sensitivityContext = new SensitivityContext(network, controllerBranchesOutsideOfDeadband,
loadFlowContext.getEquationSystem(), loadFlowContext.getJacobianMatrix());

// for synthetics logs
List<String> controlledBusesAdjusted = new ArrayList<>();
List<String> controlledBusesWithAllItsControllersToLimit = new ArrayList<>();

controlledBusesOutsideOfDeadband.forEach(controlledBus -> {
TransformerVoltageControl voltageControl = controlledBus.getTransformerVoltageControl().orElseThrow();
double diffV = getDiffV(voltageControl);
double halfTargetDeadband = getHalfTargetDeadband(voltageControl);
if (Math.abs(diffV) <= halfTargetDeadband) { // buses have already been filtered
throw new PowsyblException("Bus should have been outside of deadband");
}
List<LfBranch> controllers = voltageControl.getMergedControllerElements().stream()
annetill marked this conversation as resolved.
Show resolved Hide resolved
.filter(b -> !b.isDisabled())
.collect(Collectors.toList());
boolean adjusted;
if (controllers.size() == 1) {
adjusted = adjustWithOneController(controllers.get(0), controlledBus, contextData, sensitivityContext, diffV, controlledBusesWithAllItsControllersToLimit);
} else {
adjusted = adjustWithSeveralControllers(controllers, controlledBus, contextData, sensitivityContext, diffV, halfTargetDeadband, controlledBusesWithAllItsControllersToLimit);
}
if (adjusted) {
controlledBusesAdjusted.add(controlledBus.getId());
status.setValue(OuterLoopStatus.UNSTABLE);
}
});

if (!controlledBusesOutsideOfDeadband.isEmpty() && LOGGER.isInfoEnabled()) {
Map<String, Double> largestMismatches = controlledBuses.stream()
Map<String, Double> largestMismatches = controlledBusesOutsideOfDeadband.stream()
.map(controlledBus -> Pair.of(controlledBus.getId(), Math.abs(getDiffV(controlledBus.getTransformerVoltageControl().orElseThrow()) * controlledBus.getNominalV())))
.sorted((p1, p2) -> Double.compare(p2.getRight(), p1.getRight()))
.limit(3) // 3 largest
Expand Down
Loading