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

Refactor voltage controls #735

Merged
merged 11 commits into from
Feb 20, 2023
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
4 changes: 2 additions & 2 deletions src/main/java/com/powsybl/openloadflow/NetworkCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.powsybl.openloadflow.ac.AcLoadFlowResult;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfShunt;
import com.powsybl.openloadflow.network.VoltageControl;
import com.powsybl.openloadflow.network.GeneratorVoltageControl;
import com.powsybl.openloadflow.network.impl.LfNetworkList;
import com.powsybl.openloadflow.network.util.PreviousValueVoltageInitializer;
import org.slf4j.Logger;
Expand Down Expand Up @@ -134,7 +134,7 @@ private boolean onGeneratorUpdate(Generator generator, String attribute, Object
return onInjectionUpdate(generator, attribute, (context, lfBus) -> {
if (attribute.equals("targetV")) {
double valueShift = (double) newValue - (double) oldValue;
VoltageControl voltageControl = lfBus.getVoltageControl().orElseThrow();
GeneratorVoltageControl voltageControl = lfBus.getGeneratorVoltageControl().orElseThrow();
double newTargetV = voltageControl.getTargetValue() + valueShift / lfBus.getNominalV();
voltageControl.setTargetValue(newTargetV);
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/powsybl/openloadflow/ac/AcTargetVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private static double getBusTargetV(LfBus bus) {
}

private static Optional<Double> getVoltageControlledTargetValue(LfBus bus) {
return bus.getVoltageControl().filter(vc -> bus.isVoltageControlled()).map(vc -> {
if (vc.getControllerBuses().stream().noneMatch(LfBus::isVoltageControlEnabled)) {
return bus.getGeneratorVoltageControl().filter(vc -> bus.isGeneratorVoltageControlled()).map(vc -> {
if (vc.getControllerElements().stream().noneMatch(LfBus::isGeneratorVoltageControlEnabled)) {
throw new IllegalStateException("None of the controller buses of bus '" + bus.getId() + "'has voltage control on");
}
return vc.getTargetValue();
Expand All @@ -43,7 +43,7 @@ private static Optional<Double> getVoltageControlledTargetValue(LfBus bus) {
private static double getReactivePowerDistributionTarget(LfNetwork network, int busNum) {
LfBus controllerBus = network.getBus(busNum);
double target = (controllerBus.getRemoteVoltageControlReactivePercent() - 1) * controllerBus.getTargetQ();
for (LfBus otherControllerBus : controllerBus.getVoltageControl().orElseThrow().getControllerBuses()) {
for (LfBus otherControllerBus : controllerBus.getGeneratorVoltageControl().orElseThrow().getControllerElements()) {
if (otherControllerBus != controllerBus) {
target += controllerBus.getRemoteVoltageControlReactivePercent() * otherControllerBus.getTargetQ();
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public static void init(Equation<AcVariableType, AcEquationType> equation, LfNet
break;

case BRANCH_TARGET_P:
targets[equation.getColumn()] = LfBranch.getDiscretePhaseControlTarget(network.getBranch(equation.getElementNum()), DiscretePhaseControl.Unit.MW);
targets[equation.getColumn()] = LfBranch.getDiscretePhaseControlTarget(network.getBranch(equation.getElementNum()), TransformerPhaseControl.Unit.MW);
break;

case BRANCH_TARGET_Q:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ private static void initTarget(Equation<InitVmVariableType, InitVmEquationType>
switch (equation.getType()) {
case BUS_TARGET_V:
LfBus bus = network.getBus(equation.getElementNum());
targets[equation.getColumn()] = bus.getVoltageControl()
.map(VoltageControl::getTargetValue)
targets[equation.getColumn()] = bus.getGeneratorVoltageControl()
.map(GeneratorVoltageControl::getTargetValue)
.orElseGet(() -> bus.getTransformerVoltageControl()
.map(DiscreteVoltageControl::getTargetValue)
.orElseThrow());
Expand Down Expand Up @@ -200,7 +200,7 @@ public void prepare(LfNetwork network) {
for (LfBus bus : network.getBuses()) {
EquationTerm<InitVmVariableType, InitVmEquationType> v = equationSystem.getVariable(bus.getNum(), InitVmVariableType.BUS_V)
.createTerm();
if (bus.isVoltageControlled() || (transformerVoltageControlOn && bus.isTransformerVoltageControlled())) {
if (bus.isGeneratorVoltageControlled() || (transformerVoltageControlOn && bus.isTransformerVoltageControlled())) {
equationSystem.createEquation(bus.getNum(), InitVmEquationType.BUS_TARGET_V)
.addTerm(v);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.openloadflow.equations.*;
import com.powsybl.openloadflow.network.*;
import com.powsybl.openloadflow.network.DiscretePhaseControl.Mode;
import com.powsybl.openloadflow.network.TransformerPhaseControl.Mode;

import java.util.*;
import java.util.function.Predicate;
Expand Down Expand Up @@ -76,10 +76,10 @@ private void createBusesEquations(EquationSystem<AcVariableType, AcEquationType>

private void createGeneratorControlEquations(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem) {
bus.getVoltageControl()
bus.getGeneratorVoltageControl()
.ifPresent(voltageControl -> {
if (bus.isVoltageControlled()) {
if (voltageControl.isVoltageControlLocal()) {
if (bus.isGeneratorVoltageControlled()) {
if (voltageControl.isLocalControl()) {
createLocalVoltageControlEquation(bus, equationSystem);
} else {
createRemoteVoltageControlEquations(voltageControl, equationSystem);
Expand Down Expand Up @@ -119,7 +119,7 @@ private static void createReactivePowerControlBranchEquation(LfBranch branch, Lf
boolean deriveA1, boolean deriveR1) {
if (bus1 != null && bus2 != null) {
branch.getReactivePowerControl().ifPresent(rpc -> {
EquationTerm<AcVariableType, AcEquationType> q = rpc.getControlledSide() == ReactivePowerControl.ControlledSide.ONE
EquationTerm<AcVariableType, AcEquationType> q = rpc.getControlledSide() == ControlledSide.ONE
? new ClosedBranchSide1ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1)
: new ClosedBranchSide2ReactiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1);
equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_Q)
Expand Down Expand Up @@ -157,7 +157,7 @@ private static void createShuntEquations(LfBus bus, EquationSystem<AcVariableTyp
bus.getSvcShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, equationSystem, false));
}

private void createRemoteVoltageControlEquations(VoltageControl voltageControl,
private void createRemoteVoltageControlEquations(GeneratorVoltageControl voltageControl,
EquationSystem<AcVariableType, AcEquationType> equationSystem) {
LfBus controlledBus = voltageControl.getControlledBus();

Expand All @@ -167,7 +167,7 @@ private void createRemoteVoltageControlEquations(VoltageControl voltageControl,
.addTerm(vTerm);
controlledBus.setCalculatedV(vTerm);

for (LfBus controllerBus : voltageControl.getControllerBuses()) {
for (LfBus controllerBus : voltageControl.getControllerElements()) {
equationSystem.createEquation(controllerBus, AcEquationType.BUS_TARGET_Q);

// create reactive power distribution equations at voltage controller buses
Expand All @@ -181,7 +181,7 @@ private void createRemoteVoltageControlEquations(VoltageControl voltageControl,
.addTerms(createReactiveTerms(controllerBus, equationSystem.getVariableSet()).stream()
.map(term -> term.multiply(() -> controllerBus.getRemoteVoltageControlReactivePercent() - 1))
.collect(Collectors.toList()));
for (LfBus otherControllerBus : voltageControl.getControllerBuses()) {
for (LfBus otherControllerBus : voltageControl.getControllerElements()) {
if (otherControllerBus != controllerBus) {
zero.addTerms(createReactiveTerms(otherControllerBus, equationSystem.getVariableSet()).stream()
.map(term -> term.multiply(controllerBus::getRemoteVoltageControlReactivePercent))
Expand All @@ -191,11 +191,11 @@ private void createRemoteVoltageControlEquations(VoltageControl voltageControl,
}
}

static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
static void updateRemoteVoltageControlEquations(GeneratorVoltageControl voltageControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
// ensure reactive keys are up-to-date
voltageControl.updateReactiveKeys();

List<LfBus> controllerBuses = voltageControl.getControllerBuses()
List<LfBus> controllerBuses = voltageControl.getControllerElements()
.stream()
.filter(b -> !b.isDisabled()) // discard disabled controller buses
.collect(Collectors.toList());
Expand All @@ -216,9 +216,9 @@ static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, E
}
} else {
List<LfBus> enabledControllerBuses = controllerBuses.stream()
.filter(LfBus::isVoltageControlEnabled).collect(Collectors.toList());
.filter(LfBus::isGeneratorVoltageControlEnabled).collect(Collectors.toList());
List<LfBus> disabledControllerBuses = controllerBuses.stream()
.filter(Predicate.not(LfBus::isVoltageControlEnabled)).collect(Collectors.toList());
.filter(Predicate.not(LfBus::isGeneratorVoltageControlEnabled)).collect(Collectors.toList());

// activate voltage control at controlled bus only if at least one controller bus is enabled
vEq.setActive(!enabledControllerBuses.isEmpty());
Expand Down Expand Up @@ -288,28 +288,28 @@ private List<EquationTerm<AcVariableType, AcEquationType>> createReactiveTerms(L
return terms;
}

public static void updateGeneratorVoltageControl(VoltageControl voltageControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
public static void updateGeneratorVoltageControl(GeneratorVoltageControl voltageControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
LfBus controlledBus = voltageControl.getControlledBus();
Set<LfBus> controllerBuses = voltageControl.getControllerBuses();
List<LfBus> controllerBuses = voltageControl.getControllerElements();

LfBus firstControllerBus = controllerBuses.iterator().next();
LfBus firstControllerBus = controllerBuses.get(0);
if (firstControllerBus.hasGeneratorsWithSlope()) {
// we only support one controlling static var compensator without any other controlling generators
// we don't support controller bus that wants to control back voltage with slope.
equationSystem.getEquation(controlledBus.getNum(), AcEquationType.BUS_TARGET_V_WITH_SLOPE)
.orElseThrow()
.setActive(firstControllerBus.isVoltageControlEnabled());
.setActive(firstControllerBus.isGeneratorVoltageControlEnabled());
equationSystem.getEquation(firstControllerBus.getNum(), AcEquationType.BUS_TARGET_Q)
.orElseThrow()
.setActive(!firstControllerBus.isVoltageControlEnabled());
.setActive(!firstControllerBus.isGeneratorVoltageControlEnabled());
} else {
if (voltageControl.isVoltageControlLocal()) {
if (voltageControl.isLocalControl()) {
equationSystem.getEquation(controlledBus.getNum(), AcEquationType.BUS_TARGET_V)
.orElseThrow()
.setActive(!controlledBus.isDisabled() && controlledBus.isVoltageControlEnabled());
.setActive(!controlledBus.isDisabled() && controlledBus.isGeneratorVoltageControlEnabled());
equationSystem.getEquation(controlledBus.getNum(), AcEquationType.BUS_TARGET_Q)
.orElseThrow()
.setActive(!controlledBus.isDisabled() && !controlledBus.isVoltageControlEnabled());
.setActive(!controlledBus.isDisabled() && !controlledBus.isGeneratorVoltageControlEnabled());
} else {
updateRemoteVoltageControlEquations(voltageControl, equationSystem);
}
Expand Down Expand Up @@ -400,13 +400,13 @@ private static void createTransformerPhaseControlEquations(LfBranch branch, LfBu
}

if (branch.isPhaseControlled()) {
DiscretePhaseControl phaseControl = branch.getDiscretePhaseControl().orElseThrow();
TransformerPhaseControl phaseControl = branch.getPhaseControl().orElseThrow();
if (phaseControl.getMode() == Mode.CONTROLLER) {
if (phaseControl.getUnit() == DiscretePhaseControl.Unit.A) {
if (phaseControl.getUnit() == TransformerPhaseControl.Unit.A) {
throw new PowsyblException("Phase control in A is not yet supported");
}

EquationTerm<AcVariableType, AcEquationType> p = phaseControl.getControlledSide() == DiscretePhaseControl.ControlledSide.ONE
EquationTerm<AcVariableType, AcEquationType> p = phaseControl.getControlledSide() == ControlledSide.ONE
? new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1)
: new ClosedBranchSide2ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1);
equationSystem.createEquation(branch, AcEquationType.BRANCH_TARGET_P)
Expand All @@ -416,9 +416,9 @@ private static void createTransformerPhaseControlEquations(LfBranch branch, LfBu
}
}

public static void updateTransformerPhaseControlEquations(DiscretePhaseControl phaseControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
LfBranch controllerBranch = phaseControl.getController();
LfBranch controlledBranch = phaseControl.getControlled();
public static void updateTransformerPhaseControlEquations(TransformerPhaseControl phaseControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
LfBranch controllerBranch = phaseControl.getControllerBranch();
LfBranch controlledBranch = phaseControl.getControlledBranch();

if (phaseControl.getMode() == Mode.CONTROLLER) {
boolean enabled = !controllerBranch.isDisabled() && !controlledBranch.isDisabled();
Expand Down Expand Up @@ -449,10 +449,10 @@ private static void createTransformerVoltageControlEquations(LfBus bus, Equation
bus.setCalculatedV(vTerm);

// add transformer ratio distribution equations
createR1DistributionEquations(voltageControl.getControllers(), equationSystem);
createR1DistributionEquations(voltageControl.getControllerElements(), equationSystem);

// we also create an equation per controller that will be used later to maintain R1 variable constant
for (LfBranch controllerBranch : voltageControl.getControllers()) {
for (LfBranch controllerBranch : voltageControl.getControllerElements()) {
equationSystem.createEquation(controllerBranch, AcEquationType.BRANCH_TARGET_RHO1)
.addTerm(equationSystem.getVariable(controllerBranch.getNum(), AcVariableType.BRANCH_RHO1).createTerm());
}
Expand Down Expand Up @@ -485,16 +485,16 @@ public static void createR1DistributionEquations(List<LfBranch> controllerBranch
}

static void updateTransformerVoltageControlEquations(TransformerVoltageControl voltageControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
List<LfBranch> controllerBranches = voltageControl.getControllers()
List<LfBranch> controllerBranches = voltageControl.getControllerElements()
.stream()
.filter(b -> !b.isDisabled()) // discard disabled controller branches
.collect(Collectors.toList());

// activate voltage target equation if control is on
Equation<AcVariableType, AcEquationType> vEq = equationSystem.getEquation(voltageControl.getControlled().getNum(), AcEquationType.BUS_TARGET_V)
Equation<AcVariableType, AcEquationType> vEq = equationSystem.getEquation(voltageControl.getControlledBus().getNum(), AcEquationType.BUS_TARGET_V)
.orElseThrow();

if (voltageControl.getControlled().isDisabled()) {
if (voltageControl.getControlledBus().isDisabled()) {
// if controlled bus is disabled, we deactivate voltage target and rho distribution equations
// and activate rho target equations
vEq.setActive(false);
Expand Down Expand Up @@ -550,9 +550,9 @@ private static void createShuntVoltageControlEquations(LfBus bus, EquationSystem
bus.setCalculatedV(vTerm);

// add shunt distribution equations
createShuntSusceptanceDistributionEquations(voltageControl.getControllers(), equationSystem);
createShuntSusceptanceDistributionEquations(voltageControl.getControllerElements(), equationSystem);

for (LfShunt controllerShunt : voltageControl.getControllers()) {
for (LfShunt controllerShunt : voltageControl.getControllerElements()) {
// we also create an equation that will be used later to maintain B variable constant
// this equation is now inactive
equationSystem.createEquation(controllerShunt, AcEquationType.SHUNT_TARGET_B)
Expand Down Expand Up @@ -586,15 +586,15 @@ public static void createShuntSusceptanceDistributionEquations(List<LfShunt> con
}

static void updateShuntVoltageControlEquations(ShuntVoltageControl voltageControl, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
List<LfShunt> controllerShunts = voltageControl.getControllers()
List<LfShunt> controllerShunts = voltageControl.getControllerElements()
.stream()
.filter(b -> !b.isDisabled()) // discard disabled controller shunts
.collect(Collectors.toList());

Equation<AcVariableType, AcEquationType> vEq = equationSystem.getEquation(voltageControl.getControlled().getNum(), AcEquationType.BUS_TARGET_V)
Equation<AcVariableType, AcEquationType> vEq = equationSystem.getEquation(voltageControl.getControlledBus().getNum(), AcEquationType.BUS_TARGET_V)
.orElseThrow();

if (voltageControl.getControlled().isDisabled()) {
if (voltageControl.getControlledBus().isDisabled()) {
// if controlled bus is disabled, we deactivate voltage target equation and all susceptance distribution
// equations and activate susceptance target equations
vEq.setActive(false);
Expand Down Expand Up @@ -641,7 +641,7 @@ static void updateShuntVoltageControlEquations(ShuntVoltageControl voltageContro

private boolean isDeriveA1(LfBranch branch) {
return branch.isPhaseController()
|| (creationParameters.isForceA1Var() && branch.hasPhaseControlCapability() && branch.isConnectedAtBothSides());
|| (creationParameters.isForceA1Var() && branch.hasPhaseControllerCapability() && branch.isConnectedAtBothSides());
}

private static boolean isDeriveR1(LfBranch branch) {
Expand Down
Loading