Skip to content

Commit

Permalink
Remove EquationUtil (#447)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg authored Feb 23, 2022
1 parent a71c098 commit b01b642
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 246 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.powsybl.openloadflow.network.*;

import java.util.Objects;
import java.util.Set;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand All @@ -24,54 +23,15 @@ public AcEquationSystemUpdater(EquationSystem<AcVariableType, AcEquationType> eq
this.equationSystem = Objects.requireNonNull(equationSystem);
}

private void updateVoltageControl(VoltageControl voltageControl) {
LfBus controlledBus = voltageControl.getControlledBus();
Set<LfBus> controllerBuses = voltageControl.getControllerBuses();

LfBus firstControllerBus = controllerBuses.iterator().next();
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());
} else {
if (voltageControl.isVoltageControlLocal()) {
equationSystem.getEquation(controlledBus.getNum(), AcEquationType.BUS_TARGET_V)
.orElseThrow()
.setActive(controlledBus.isVoltageControlEnabled());
} else {
AcEquationSystem.updateRemoteVoltageControlEquations(voltageControl, equationSystem);
}
}
}

private void updateVoltageControl(LfBus controllerBus, boolean newVoltageControllerEnabled) {
// active/de-activate bus target reactive power equation to switch bus PV or PQ
equationSystem.getEquation(controllerBus.getNum(), AcEquationType.BUS_TARGET_Q)
.orElseThrow()
.setActive(!newVoltageControllerEnabled);

updateVoltageControl(controllerBus.getVoltageControl().orElseThrow());
}

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

@Override
public void onTransformerPhaseControlChange(LfBranch branch, boolean phaseControlEnabled) {
DiscretePhaseControl phaseControl = branch.getDiscretePhaseControl().orElseThrow();

// activate/de-activate phase control equation
equationSystem.getEquation(phaseControl.getControlled().getNum(), AcEquationType.BRANCH_TARGET_P)
.orElseThrow()
.setActive(!branch.isDisabled() && branch.isPhaseControlEnabled());

// de-activate/activate constant A1 equation
equationSystem.getEquation(phaseControl.getController().getNum(), AcEquationType.BRANCH_TARGET_ALPHA1)
.orElseThrow()
.setActive(!branch.isDisabled() && !branch.isPhaseControlEnabled());
AcEquationSystem.updateTransformerPhaseControlEquations(branch.getDiscretePhaseControl().orElseThrow(), equationSystem);
}

@Override
Expand All @@ -84,22 +44,49 @@ public void onShuntVoltageControlChange(LfShunt controllerShunt, boolean newVolt
AcEquationSystem.updateShuntVoltageControlEquations(controllerShunt.getVoltageControl().orElseThrow(), equationSystem);
}

private void updateElementEquations(LfElement element, boolean enable) {
// update all equations related to the element
for (var equation : equationSystem.getEquations(element.getType(), element.getNum())) {
if (equation.isActive() != enable) {
equation.setActive(enable);
}
}

// update all equation terms related to the element
for (var equationTerm : equationSystem.getEquationTerms(element.getType(), element.getNum())) {
if (equationTerm.isActive() != enable) {
equationTerm.setActive(enable);
}
}
}

@Override
public void onDisableChange(LfElement element, boolean disabled) {
updateElementEquations(element, !disabled);
switch (element.getType()) {
case BUS:
LfBus bus = (LfBus) element;
if (disabled && bus.isSlack()) {
throw new PowsyblException("Slack bus '" + bus.getId() + "' disabling is not supported");
}
bus.getVoltageControl().ifPresent(this::updateVoltageControl);
equationSystem.getEquation(bus.getNum(), AcEquationType.BUS_TARGET_PHI)
.ifPresent(eq -> eq.setActive(!bus.isDisabled()));
equationSystem.getEquation(bus.getNum(), AcEquationType.BUS_TARGET_P)
.ifPresent(eq -> eq.setActive(!bus.isDisabled() && !bus.isSlack()));
// set voltage target equation inactive, various voltage control will set next to the correct value
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));
break;
case BRANCH:
LfBranch branch = (LfBranch) element;
branch.getVoltageControl().ifPresent(voltageControl -> AcEquationSystem.updateTransformerVoltageControlEquations(voltageControl, equationSystem));
branch.getDiscretePhaseControl().ifPresent(phaseControl -> onTransformerPhaseControlChange(branch, branch.isPhaseControlEnabled()));
branch.getDiscretePhaseControl().ifPresent(phaseControl -> AcEquationSystem.updateTransformerPhaseControlEquations(phaseControl, equationSystem));
branch.getReactivePowerControl().ifPresent(reactivePowerControl -> AcEquationSystem.updateReactivePowerControlBranchEquations(reactivePowerControl, equationSystem));
break;
case SHUNT_COMPENSATOR:
LfShunt shunt = (LfShunt) element;
Expand Down
108 changes: 0 additions & 108 deletions src/main/java/com/powsybl/openloadflow/equations/EquationUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class BranchState extends ElementState<LfBranch> {
private final double r1;
private final boolean phaseControlEnabled;
private final boolean voltageControlEnabled;
private final boolean disabled;

public BranchState(LfBranch branch) {
super(branch);
Expand All @@ -24,17 +23,16 @@ public BranchState(LfBranch branch) {
r1 = piModel.getR1();
phaseControlEnabled = branch.isPhaseControlEnabled();
voltageControlEnabled = branch.isVoltageControlEnabled();
disabled = branch.isDisabled();
}

@Override
public void restore() {
super.restore();
PiModel piModel = element.getPiModel();
piModel.setA1(a1);
piModel.setR1(r1);
element.setPhaseControlEnabled(phaseControlEnabled);
element.setVoltageControlEnabled(voltageControlEnabled);
element.setDisabled(disabled);
}

public static BranchState save(LfBranch branch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public BusDcState(LfBus bus) {

@Override
public void restore() {
super.restore();
element.setLoadTargetP(loadTargetP);
element.getGenerators().forEach(g -> g.setTargetP(generatorsTargetP.get(g.getId())));
element.getGenerators().forEach(g -> g.setParticipating(participatingGenerators.get(g.getId())));
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/powsybl/openloadflow/network/BusState.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class BusState extends BusDcState {
private final double generationTargetQ;
private final boolean voltageControlEnabled;
private final Boolean shuntVoltageControlEnabled;
private final boolean disabled;
private final double shuntB;
private final double controllerShuntB;
private final Map<String, LfGenerator.GeneratorControlType> generatorsControlType;
Expand All @@ -37,7 +36,6 @@ public BusState(LfBus bus) {
controllerShuntB = controllerShunt != null ? controllerShunt.getB() : Double.NaN;
LfShunt shunt = bus.getShunt().orElse(null);
shuntB = shunt != null ? shunt.getB() : Double.NaN;
this.disabled = bus.isDisabled();
this.generatorsControlType = bus.getGenerators().stream().collect(Collectors.toMap(LfGenerator::getId, LfGenerator::getGeneratorControlType));
}

Expand All @@ -59,7 +57,6 @@ public void restore() {
if (!Double.isNaN(shuntB)) {
element.getShunt().orElseThrow().setB(shuntB);
}
element.setDisabled(disabled);
element.getGenerators().forEach(g -> g.setGeneratorControlType(generatorsControlType.get(g.getId())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public class ElementState<T extends LfElement> {

protected final T element;

protected final boolean disabled;

public ElementState(T element) {
this.element = Objects.requireNonNull(element);
disabled = element.isDisabled();
}

public void restore() {
// nothing to restore
element.setDisabled(disabled);
}

public static <T extends LfElement, U extends ElementState<T>> List<U> save(Collection<T> elements, Function<T, U> save) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/powsybl/openloadflow/network/LfContingency.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ public Set<LfBranch> getBranches() {
return branches;
}

public Map<LfShunt, Double> getShuntsShift() {
return shuntsShift;
}

public Map<LfBus, PowerShift> getBusesLoadShift() {
return busesLoadShift;
}

public Set<LfGenerator> getGenerators() {
return generators;
}

public double getActivePowerLoss() {
return activePowerLoss;
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/powsybl/openloadflow/network/NetworkState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2022, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.openloadflow.network;

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

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class NetworkState {

private final List<BusState> busStates;

private final List<BranchState> branchStates;

protected NetworkState(List<BusState> busStates, List<BranchState> branchStates) {
this.busStates = Objects.requireNonNull(busStates);
this.branchStates = Objects.requireNonNull(branchStates);
}

public static NetworkState save(LfNetwork network) {
Objects.requireNonNull(network);
List<BusState> busStates = ElementState.save(network.getBuses(), BusState::save);
List<BranchState> branchStates = ElementState.save(network.getBranches(), BranchState::save);
return new NetworkState(busStates, branchStates);
}

public void restore() {
ElementState.restore(busStates);
ElementState.restore(branchStates);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,9 @@ protected boolean checkActivePowerControl(double targetP, double minP, double ma
}
return participating;
}

@Override
public String toString() {
return getId();
}
}
Loading

0 comments on commit b01b642

Please sign in to comment.