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

Zero impedance branch management refactoring #666

Merged
merged 4 commits into from
Dec 8, 2022
Merged
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
16 changes: 7 additions & 9 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.network.impl.LfNetworkLoaderImpl;
import com.powsybl.openloadflow.network.impl.Networks;
import com.powsybl.openloadflow.network.util.ZeroImpedanceFlows;
Expand Down Expand Up @@ -126,7 +125,7 @@ private LoadFlowResult runAc(Network network, LoadFlowParameters parameters, Rep
parameters.isDistributedSlack() && (parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD || parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD) && parametersExt.isLoadPowerFactorConstant(), parameters.isDc());

// zero or low impedance branch flows computation
computeZeroImpedanceFlows(result.getNetwork(), parameters.isDc(), parametersExt.getLowImpedanceThreshold());
computeZeroImpedanceFlows(result.getNetwork());
}

LoadFlowResult.ComponentResult.Status status;
Expand Down Expand Up @@ -158,11 +157,11 @@ private LoadFlowResult runAc(Network network, LoadFlowParameters parameters, Rep
return new LoadFlowResultImpl(ok, Collections.emptyMap(), null, componentResults);
}

private void computeZeroImpedanceFlows(LfNetwork network, boolean dc, double lowImpedanceThreshold) {
Graph<LfBus, LfBranch> zeroImpedanceSubGraph = network.createZeroImpedanceSubGraph(dc, lowImpedanceThreshold);
private void computeZeroImpedanceFlows(LfNetwork network) {
Graph<LfBus, LfBranch> zeroImpedanceSubGraph = network.createZeroImpedanceSubGraph();
if (!zeroImpedanceSubGraph.vertexSet().isEmpty()) {
SpanningTreeAlgorithm.SpanningTree<LfBranch> spanningTree = new KruskalMinimumSpanningTree<>(zeroImpedanceSubGraph).getSpanningTree();
new ZeroImpedanceFlows(zeroImpedanceSubGraph, spanningTree).compute(dc, lowImpedanceThreshold);
new ZeroImpedanceFlows(zeroImpedanceSubGraph, spanningTree).compute();
}
}

Expand All @@ -176,13 +175,12 @@ private LoadFlowResult runDc(Network network, LoadFlowParameters parameters, Rep

Networks.resetState(network);

List<LoadFlowResult.ComponentResult> componentsResult = results.stream().map(r -> processResult(network, r, parameters, dcParameters.getNetworkParameters())).collect(Collectors.toList());
List<LoadFlowResult.ComponentResult> componentsResult = results.stream().map(r -> processResult(network, r, parameters)).collect(Collectors.toList());
boolean ok = results.stream().anyMatch(DcLoadFlowResult::isSucceeded);
return new LoadFlowResultImpl(ok, Collections.emptyMap(), null, componentsResult);
}

private LoadFlowResult.ComponentResult processResult(Network network, DcLoadFlowResult result, LoadFlowParameters parameters,
LfNetworkParameters networkParameters) {
private LoadFlowResult.ComponentResult processResult(Network network, DcLoadFlowResult result, LoadFlowParameters parameters) {
if (result.isSucceeded() && parameters.isWriteSlackBus()) {
SlackTerminal.reset(network);
}
Expand All @@ -196,7 +194,7 @@ private LoadFlowResult.ComponentResult processResult(Network network, DcLoadFlow
false, true);

// zero or low impedance branch flows computation
computeZeroImpedanceFlows(result.getNetwork(), true, networkParameters.getLowImpedanceThreshold());
computeZeroImpedanceFlows(result.getNetwork());
}

return new LoadFlowResultImpl.ComponentResultImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ private AcEquationSystem() {

private static void createBusEquation(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
var p = equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_P);
bus.setP(p);
var q = equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_Q);
Expand All @@ -39,7 +38,7 @@ private static void createBusEquation(LfBus bus,
p.setActive(false);
}

createGeneratorControlEquations(bus, equationSystem, creationParameters, lowImpedanceThreshold);
createGeneratorControlEquations(bus, equationSystem, creationParameters);

createShuntEquations(bus, equationSystem);

Expand All @@ -60,24 +59,22 @@ private static void createBusEquation(LfBus bus,

private static void createBusesEquations(LfNetwork network,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
for (LfBus bus : network.getBuses()) {
createBusEquation(bus, equationSystem, creationParameters, lowImpedanceThreshold);
createBusEquation(bus, equationSystem, creationParameters);
}
}

private static void createGeneratorControlEquations(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
bus.getVoltageControl()
.ifPresent(voltageControl -> {
if (bus.isVoltageControlled()) {
if (voltageControl.isVoltageControlLocal()) {
createLocalVoltageControlEquation(bus, equationSystem, creationParameters, lowImpedanceThreshold);
createLocalVoltageControlEquation(bus, equationSystem, creationParameters);
} else {
createRemoteVoltageControlEquations(voltageControl, equationSystem, creationParameters, lowImpedanceThreshold);
createRemoteVoltageControlEquations(voltageControl, equationSystem, creationParameters);
}
updateGeneratorVoltageControl(voltageControl, equationSystem);
}
Expand All @@ -86,8 +83,7 @@ private static void createGeneratorControlEquations(LfBus bus,

private static void createLocalVoltageControlEquation(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
EquationTerm<AcVariableType, AcEquationType> vTerm = equationSystem.getVariable(bus.getNum(), AcVariableType.BUS_V)
.createTerm();
bus.setCalculatedV(vTerm);
Expand All @@ -100,7 +96,7 @@ private static void createLocalVoltageControlEquation(LfBus bus,
// which is modeled here with: V + slope * (sum_branch qBranch) = TargetV - slope * qLoads + slope * qGenerators
equationSystem.createEquation(bus, AcEquationType.BUS_TARGET_V_WITH_SLOPE)
.addTerm(vTerm)
.addTerms(createReactiveTerms(bus, equationSystem.getVariableSet(), creationParameters, lowImpedanceThreshold)
.addTerms(createReactiveTerms(bus, equationSystem.getVariableSet(), creationParameters)
.stream()
.map(term -> term.multiply(slope))
.collect(Collectors.toList()));
Expand Down Expand Up @@ -157,8 +153,7 @@ private static void createShuntEquations(LfBus bus, EquationSystem<AcVariableTyp

private static void createRemoteVoltageControlEquations(VoltageControl voltageControl,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
LfBus controlledBus = voltageControl.getControlledBus();

// create voltage equation at voltage controlled bus
Expand All @@ -178,12 +173,12 @@ private static void createRemoteVoltageControlEquations(VoltageControl voltageCo
// which can be rewritten in a more simple way
// 0 = (qPercent_i - 1) * q_i + qPercent_i * sum_j(q_j) where j are all the voltage controller buses except i
Equation<AcVariableType, AcEquationType> zero = equationSystem.createEquation(controllerBus, AcEquationType.DISTR_Q)
.addTerms(createReactiveTerms(controllerBus, equationSystem.getVariableSet(), creationParameters, lowImpedanceThreshold).stream()
.addTerms(createReactiveTerms(controllerBus, equationSystem.getVariableSet(), creationParameters).stream()
.map(term -> term.multiply(() -> controllerBus.getRemoteVoltageControlReactivePercent() - 1))
.collect(Collectors.toList()));
for (LfBus otherControllerBus : voltageControl.getControllerBuses()) {
if (otherControllerBus != controllerBus) {
zero.addTerms(createReactiveTerms(otherControllerBus, equationSystem.getVariableSet(), creationParameters, lowImpedanceThreshold).stream()
zero.addTerms(createReactiveTerms(otherControllerBus, equationSystem.getVariableSet(), creationParameters).stream()
.map(term -> term.multiply(controllerBus::getRemoteVoltageControlReactivePercent))
.collect(Collectors.toList()));
}
Expand Down Expand Up @@ -249,12 +244,11 @@ static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, E

private static List<EquationTerm<AcVariableType, AcEquationType>> createReactiveTerms(LfBus controllerBus,
VariableSet<AcVariableType> variableSet,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
List<EquationTerm<AcVariableType, AcEquationType>> terms = new ArrayList<>();
for (LfBranch branch : controllerBus.getBranches()) {
EquationTerm<AcVariableType, AcEquationType> q;
if (branch.isZeroImpedanceBranch(false, lowImpedanceThreshold)) {
if (branch.isZeroImpedance()) {
if (!branch.isSpanningTreeEdge()) {
continue;
}
Expand Down Expand Up @@ -744,10 +738,9 @@ private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem<A

private static void createBranchEquations(LfBranch branch,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
// create zero and non zero impedance branch equations
if (branch.isZeroImpedanceBranch(false, lowImpedanceThreshold)) {
if (branch.isZeroImpedance()) {
if (branch.isSpanningTreeEdge()) {
createNonImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem);
}
Expand All @@ -758,35 +751,32 @@ private static void createBranchEquations(LfBranch branch,

private static void createBranchesEquations(LfNetwork network,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
AcEquationSystemCreationParameters creationParameters) {
for (LfBranch branch : network.getBranches()) {
createBranchEquations(branch, equationSystem, creationParameters, lowImpedanceThreshold);
createBranchEquations(branch, equationSystem, creationParameters);
}
}

public static EquationSystem<AcVariableType, AcEquationType> create(LfNetwork network) {
return create(network, new AcEquationSystemCreationParameters(), LfNetworkParameters.LOW_IMPEDANCE_THRESHOLD_DEFAULT_VALUE);
return create(network, new AcEquationSystemCreationParameters());
}

public static EquationSystem<AcVariableType, AcEquationType> create(LfNetwork network,
AcEquationSystemCreationParameters creationParameters,
double lowImpedanceThreshold) {
public static EquationSystem<AcVariableType, AcEquationType> create(LfNetwork network, AcEquationSystemCreationParameters creationParameters) {
Objects.requireNonNull(network);
Objects.requireNonNull(creationParameters);

EquationSystem<AcVariableType, AcEquationType> equationSystem = new EquationSystem<>(true);

createBusesEquations(network, equationSystem, creationParameters, lowImpedanceThreshold);
createBranchesEquations(network, equationSystem, creationParameters, lowImpedanceThreshold);
createBusesEquations(network, equationSystem, creationParameters);
createBranchesEquations(network, equationSystem, creationParameters);

for (LfHvdc hvdc : network.getHvdcs()) {
createHvdcAcEmulationEquations(hvdc, equationSystem);
}

EquationSystemPostProcessor.findAll().forEach(pp -> pp.onCreate(equationSystem));

network.addListener(new AcEquationSystemUpdater(equationSystem, lowImpedanceThreshold));
network.addListener(new AcEquationSystemUpdater(equationSystem));

return equationSystem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
public class AcEquationSystemUpdater extends AbstractEquationSystemUpdater<AcVariableType, AcEquationType> {

public AcEquationSystemUpdater(EquationSystem<AcVariableType, AcEquationType> equationSystem, double lowImpedanceThreshold) {
super(equationSystem, lowImpedanceThreshold);
public AcEquationSystemUpdater(EquationSystem<AcVariableType, AcEquationType> equationSystem) {
super(equationSystem);
}

@Override
Expand Down Expand Up @@ -61,7 +61,7 @@ protected void updateNonImpedantBranchEquations(LfBranch branch, boolean enable)

@Override
public void onDisableChange(LfElement element, boolean disabled) {
updateElementEquations(element, !disabled, false);
updateElementEquations(element, !disabled);
switch (element.getType()) {
case BUS:
LfBus bus = (LfBus) element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public AcLoadFlowContext(LfNetwork network, AcLoadFlowParameters parameters) {
@Override
public EquationSystem<AcVariableType, AcEquationType> getEquationSystem() {
if (equationSystem == null) {
equationSystem = AcEquationSystem.create(network, parameters.getEquationSystemCreationParameters(),
parameters.getNetworkParameters().getLowImpedanceThreshold());
equationSystem = AcEquationSystem.create(network, parameters.getEquationSystemCreationParameters());
}
return equationSystem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public DcLoadFlowContext(LfNetwork network, DcLoadFlowParameters parameters) {
@Override
public EquationSystem<DcVariableType, DcEquationType> getEquationSystem() {
if (equationSystem == null) {
equationSystem = DcEquationSystem.create(network, parameters.getEquationSystemCreationParameters(), parameters.getNetworkParameters().getLowImpedanceThreshold(), true);
equationSystem = DcEquationSystem.create(network, parameters.getEquationSystemCreationParameters(), true);
}
return equationSystem;
}
Expand Down
Loading