Skip to content

Commit

Permalink
Refactor equation systems creation (#684)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@gmail.com>
  • Loading branch information
geofjamg authored and Caio Luke committed Jan 10, 2023
1 parent 8db4e9a commit 3889c43
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@
/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public final class AcEquationSystem {
public class AcEquationSystemCreator {

private AcEquationSystem() {
private final LfNetwork network;

private final AcEquationSystemCreationParameters creationParameters;

public AcEquationSystemCreator(LfNetwork network) {
this(network, new AcEquationSystemCreationParameters());
}

public AcEquationSystemCreator(LfNetwork network, AcEquationSystemCreationParameters creationParameters) {
this.network = Objects.requireNonNull(network);
this.creationParameters = Objects.requireNonNull(creationParameters);
}

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

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

createShuntEquations(bus, equationSystem);

Expand All @@ -57,33 +66,29 @@ private static void createBusEquation(LfBus bus,
}
}

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

private static void createGeneratorControlEquations(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters) {
private void createGeneratorControlEquations(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem) {
bus.getVoltageControl()
.ifPresent(voltageControl -> {
if (bus.isVoltageControlled()) {
if (voltageControl.isVoltageControlLocal()) {
createLocalVoltageControlEquation(bus, equationSystem, creationParameters);
createLocalVoltageControlEquation(bus, equationSystem);
} else {
createRemoteVoltageControlEquations(voltageControl, equationSystem, creationParameters);
createRemoteVoltageControlEquations(voltageControl, equationSystem);
}
updateGeneratorVoltageControl(voltageControl, equationSystem);
}
});
}

private static void createLocalVoltageControlEquation(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters) {
private void createLocalVoltageControlEquation(LfBus bus,
EquationSystem<AcVariableType, AcEquationType> equationSystem) {
EquationTerm<AcVariableType, AcEquationType> vTerm = equationSystem.getVariable(bus.getNum(), AcVariableType.BUS_V)
.createTerm();
bus.setCalculatedV(vTerm);
Expand All @@ -96,7 +101,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)
.addTerms(createReactiveTerms(bus, equationSystem.getVariableSet())
.stream()
.map(term -> term.multiply(slope))
.collect(Collectors.toList()));
Expand Down Expand Up @@ -149,9 +154,8 @@ private static void createShuntEquations(LfBus bus, EquationSystem<AcVariableTyp
bus.getSvcShunt().ifPresent(shunt -> createShuntEquation(shunt, bus, equationSystem, false));
}

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

// create voltage equation at voltage controlled bus
Expand All @@ -171,12 +175,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).stream()
.addTerms(createReactiveTerms(controllerBus, equationSystem.getVariableSet()).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).stream()
zero.addTerms(createReactiveTerms(otherControllerBus, equationSystem.getVariableSet()).stream()
.map(term -> term.multiply(controllerBus::getRemoteVoltageControlReactivePercent))
.collect(Collectors.toList()));
}
Expand Down Expand Up @@ -240,9 +244,8 @@ static void updateRemoteVoltageControlEquations(VoltageControl voltageControl, E
}
}

private static List<EquationTerm<AcVariableType, AcEquationType>> createReactiveTerms(LfBus controllerBus,
VariableSet<AcVariableType> variableSet,
AcEquationSystemCreationParameters creationParameters) {
private List<EquationTerm<AcVariableType, AcEquationType>> createReactiveTerms(LfBus controllerBus,
VariableSet<AcVariableType> variableSet) {
List<EquationTerm<AcVariableType, AcEquationType>> terms = new ArrayList<>();
for (LfBranch branch : controllerBus.getBranches()) {
EquationTerm<AcVariableType, AcEquationType> q;
Expand All @@ -257,7 +260,7 @@ private static List<EquationTerm<AcVariableType, AcEquationType>> createReactive
.minus();
}
} else {
boolean deriveA1 = isDeriveA1(branch, creationParameters);
boolean deriveA1 = isDeriveA1(branch);
boolean deriveR1 = isDeriveR1(branch);
if (branch.getBus1() == controllerBus) {
LfBus otherSideBus = branch.getBus2();
Expand Down Expand Up @@ -305,7 +308,7 @@ public static void updateGeneratorVoltageControl(VoltageControl voltageControl,
.orElseThrow()
.setActive(!controlledBus.isDisabled() && !controlledBus.isVoltageControlEnabled());
} else {
AcEquationSystem.updateRemoteVoltageControlEquations(voltageControl, equationSystem);
updateRemoteVoltageControlEquations(voltageControl, equationSystem);
}
}
}
Expand Down Expand Up @@ -633,7 +636,7 @@ static void updateShuntVoltageControlEquations(ShuntVoltageControl voltageContro
}
}

private static boolean isDeriveA1(LfBranch branch, AcEquationSystemCreationParameters creationParameters) {
private boolean isDeriveA1(LfBranch branch) {
return branch.isPhaseController()
|| (creationParameters.isForceA1Var() && branch.hasPhaseControlCapability() && branch.isConnectedAtBothSides());
}
Expand All @@ -642,16 +645,15 @@ private static boolean isDeriveR1(LfBranch branch) {
return branch.isVoltageController();
}

private static void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters) {
private void createImpedantBranch(LfBranch branch, LfBus bus1, LfBus bus2,
EquationSystem<AcVariableType, AcEquationType> equationSystem) {
EquationTerm<AcVariableType, AcEquationType> p1 = null;
EquationTerm<AcVariableType, AcEquationType> q1 = null;
EquationTerm<AcVariableType, AcEquationType> p2 = null;
EquationTerm<AcVariableType, AcEquationType> q2 = null;
EquationTerm<AcVariableType, AcEquationType> i1 = null;
EquationTerm<AcVariableType, AcEquationType> i2 = null;
boolean deriveA1 = isDeriveA1(branch, creationParameters);
boolean deriveA1 = isDeriveA1(branch);
boolean deriveR1 = isDeriveR1(branch);
if (bus1 != null && bus2 != null) {
p1 = new ClosedBranchSide1ActiveFlowEquationTerm(branch, bus1, bus2, equationSystem.getVariableSet(), deriveA1, deriveR1);
Expand Down Expand Up @@ -734,39 +736,30 @@ private static void createHvdcAcEmulationEquations(LfHvdc hvdc, EquationSystem<A
}
}

private static void createBranchEquations(LfBranch branch,
EquationSystem<AcVariableType, AcEquationType> equationSystem,
AcEquationSystemCreationParameters creationParameters) {
private void createBranchEquations(LfBranch branch,
EquationSystem<AcVariableType, AcEquationType> equationSystem) {
// create zero and non zero impedance branch equations
if (branch.isZeroImpedance()) {
if (branch.isSpanningTreeEdge()) {
createNonImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem);
}
} else {
createImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem, creationParameters);
createImpedantBranch(branch, branch.getBus1(), branch.getBus2(), equationSystem);
}
}

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

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

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

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

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

for (LfHvdc hvdc : network.getHvdcs()) {
createHvdcAcEmulationEquations(hvdc, equationSystem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ public AcEquationSystemUpdater(EquationSystem<AcVariableType, AcEquationType> eq

@Override
public void onVoltageControlChange(LfBus controllerBus, boolean newVoltageControllerEnabled) {
controllerBus.getVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateGeneratorVoltageControl(voltageControl, equationSystem));
controllerBus.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystem.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
controllerBus.getVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateGeneratorVoltageControl(voltageControl, equationSystem));
controllerBus.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
}

@Override
public void onTransformerPhaseControlChange(LfBranch branch, boolean phaseControlEnabled) {
AcEquationSystem.updateTransformerPhaseControlEquations(branch.getDiscretePhaseControl().orElseThrow(), equationSystem);
AcEquationSystemCreator.updateTransformerPhaseControlEquations(branch.getDiscretePhaseControl().orElseThrow(), equationSystem);
}

@Override
public void onTransformerVoltageControlChange(LfBranch controllerBranch, boolean newVoltageControllerEnabled) {
AcEquationSystem.updateTransformerVoltageControlEquations(controllerBranch.getVoltageControl().orElseThrow(), equationSystem);
AcEquationSystemCreator.updateTransformerVoltageControlEquations(controllerBranch.getVoltageControl().orElseThrow(), equationSystem);
}

@Override
public void onShuntVoltageControlChange(LfShunt controllerShunt, boolean newVoltageControllerEnabled) {
AcEquationSystem.updateShuntVoltageControlEquations(controllerShunt.getVoltageControl().orElseThrow(), equationSystem);
AcEquationSystemCreator.updateShuntVoltageControlEquations(controllerShunt.getVoltageControl().orElseThrow(), equationSystem);
}

@Override
Expand Down Expand Up @@ -74,20 +74,20 @@ public void onDisableChange(LfElement element, boolean disabled) {
equationSystem.getEquation(bus.getNum(), AcEquationType.BUS_TARGET_V)
.orElseThrow()
.setActive(false);
bus.getVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateGeneratorVoltageControl(voltageControl, equationSystem));
bus.getTransformerVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateTransformerVoltageControlEquations(voltageControl, equationSystem));
bus.getShuntVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateShuntVoltageControlEquations(voltageControl, equationSystem));
bus.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystem.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
bus.getVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateGeneratorVoltageControl(voltageControl, equationSystem));
bus.getTransformerVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateTransformerVoltageControlEquations(voltageControl, equationSystem));
bus.getShuntVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateShuntVoltageControlEquations(voltageControl, equationSystem));
bus.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
break;
case BRANCH:
LfBranch branch = (LfBranch) element;
branch.getVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateTransformerVoltageControlEquations(voltageControl, equationSystem));
branch.getDiscretePhaseControl().ifPresent(phaseControl -> AcEquationSystem.updateTransformerPhaseControlEquations(phaseControl, equationSystem));
branch.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystem.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
branch.getVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateTransformerVoltageControlEquations(voltageControl, equationSystem));
branch.getDiscretePhaseControl().ifPresent(phaseControl -> AcEquationSystemCreator.updateTransformerPhaseControlEquations(phaseControl, equationSystem));
branch.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystemCreator.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
break;
case SHUNT_COMPENSATOR:
LfShunt shunt = (LfShunt) element;
shunt.getVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateShuntVoltageControlEquations(voltageControl, equationSystem));
shunt.getVoltageControl().ifPresent(voltageControl -> AcEquationSystemCreator.updateShuntVoltageControlEquations(voltageControl, equationSystem));
break;
case HVDC:
// nothing to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package com.powsybl.openloadflow.ac.outerloop;

import com.powsybl.openloadflow.lf.AbstractLoadFlowContext;
import com.powsybl.openloadflow.ac.equations.AcEquationSystem;
import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreator;
import com.powsybl.openloadflow.ac.equations.AcEquationType;
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;
Expand Down Expand Up @@ -35,7 +35,7 @@ public AcLoadFlowContext(LfNetwork network, AcLoadFlowParameters parameters) {
@Override
public EquationSystem<AcVariableType, AcEquationType> getEquationSystem() {
if (equationSystem == null) {
equationSystem = AcEquationSystem.create(network, parameters.getEquationSystemCreationParameters());
equationSystem = new AcEquationSystemCreator(network, parameters.getEquationSystemCreationParameters()).create();
}
return equationSystem;
}
Expand Down
Loading

0 comments on commit 3889c43

Please sign in to comment.