Skip to content

Commit

Permalink
Merge branch 'main' into add_reports_in_lfnetworkloaderimpl
Browse files Browse the repository at this point in the history
  • Loading branch information
caioluke authored May 2, 2024
2 parents eb4b174 + ce8a693 commit a53116f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 50 deletions.
91 changes: 53 additions & 38 deletions src/main/java/com/powsybl/openloadflow/network/LfAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private record TapPositionChange(LfBranch branch, int value, boolean isRelative)
private record LoadShift(String loadId, LfLoad load, PowerShift powerShift) {
}

private record GeneratorChange(LfGenerator generator, double deltaTargetP) {
private record GeneratorChange(LfGenerator generator, double activePowerValue, boolean isRelative) {
}

private record SectionChange(LfShunt shunt, String controllerId, int value) {
Expand Down Expand Up @@ -224,16 +224,10 @@ private static Optional<LfAction> create(GeneratorAction action, LfNetwork lfNet
OptionalDouble activePowerValue = action.getActivePowerValue();
Optional<Boolean> relativeValue = action.isActivePowerRelativeValue();
if (relativeValue.isPresent() && activePowerValue.isPresent()) {
double deltaTargetP;
if (relativeValue.get().equals(Boolean.TRUE)) {
deltaTargetP = activePowerValue.getAsDouble() / PerUnit.SB;
} else {
deltaTargetP = activePowerValue.getAsDouble() / PerUnit.SB - generator.getInitialTargetP();
}
var generatorChange = new GeneratorChange(generator, deltaTargetP);
var generatorChange = new GeneratorChange(generator, activePowerValue.getAsDouble() / PerUnit.SB, relativeValue.get());
return Optional.of(new LfAction(action.getId(), null, null, null, null, generatorChange, null, null));
} else {
throw new UnsupportedOperationException("Generator action: configuration not supported yet.");
throw new UnsupportedOperationException("Generator action on " + action.getGeneratorId() + " : configuration not supported yet.");
}
}
return Optional.empty();
Expand Down Expand Up @@ -315,49 +309,70 @@ public void updateConnectivity(GraphConnectivity<LfBus, LfBranch> connectivity)
}
}

private void applyTapPositionChange() {
LfBranch branch = tapPositionChange.branch();
int tapPosition = branch.getPiModel().getTapPosition();
int value = tapPositionChange.value();
int newTapPosition = tapPositionChange.isRelative() ? tapPosition + value : value;
branch.getPiModel().setTapPosition(newTapPosition);
}

private void applyLoadShift() {
LfLoad load = loadShift.load();
if (!load.isOriginalLoadDisabled(loadShift.loadId())) {
PowerShift shift = loadShift.powerShift();
load.setTargetP(load.getTargetP() + shift.getActive());
load.setTargetQ(load.getTargetQ() + shift.getReactive());
load.setAbsVariableTargetP(load.getAbsVariableTargetP()
+ Math.signum(shift.getActive()) * Math.abs(shift.getVariableActive()));
}
}

private void applyGeneratorChange(LfNetworkParameters networkParameters) {
LfGenerator generator = generatorChange.generator();
if (!generator.isDisabled()) {
double newTargetP = generatorChange.isRelative() ? generator.getTargetP() + generatorChange.activePowerValue() : generatorChange.activePowerValue();
generator.setTargetP(newTargetP);
if (!AbstractLfGenerator.checkActivePowerControl(generator.getId(), generator.getTargetP(), generator.getMinP(), generator.getMaxP(),
networkParameters.getPlausibleActivePowerLimit(), networkParameters.isUseActiveLimits(), null)) {
generator.setParticipating(false);
}
}
}

private void applyHvdcAction() {
hvdc.setAcEmulation(false);
hvdc.setDisabled(true); // for equations only, but should be hidden
hvdc.getConverterStation1().setTargetP(-hvdc.getP1().eval()); // override
hvdc.getConverterStation2().setTargetP(-hvdc.getP2().eval()); // override
}

private void applySectionChange() {
LfShunt shunt = sectionChange.shunt();
shunt.getControllers().stream().filter(controller -> controller.getId().equals(sectionChange.controllerId())).findAny()
.ifPresentOrElse(controller -> controller.updateSectionB(sectionChange.value()),
() -> LOGGER.warn("No section change: shunt {} not present", sectionChange.controllerId));
}

public void apply(LfNetworkParameters networkParameters) {
if (tapPositionChange != null) {
LfBranch branch = tapPositionChange.branch();
int tapPosition = branch.getPiModel().getTapPosition();
int value = tapPositionChange.value();
int newTapPosition = tapPositionChange.isRelative() ? tapPosition + value : value;
branch.getPiModel().setTapPosition(newTapPosition);
applyTapPositionChange();
}

if (loadShift != null) {
LfLoad load = loadShift.load();
if (!load.isOriginalLoadDisabled(loadShift.loadId())) {
PowerShift shift = loadShift.powerShift();
load.setTargetP(load.getTargetP() + shift.getActive());
load.setTargetQ(load.getTargetQ() + shift.getReactive());
load.setAbsVariableTargetP(load.getAbsVariableTargetP()
+ Math.signum(shift.getActive()) * Math.abs(shift.getVariableActive()));
}
applyLoadShift();
}

if (generatorChange != null) {
LfGenerator generator = generatorChange.generator();
if (!generator.isDisabled()) {
generator.setTargetP(generator.getTargetP() + generatorChange.deltaTargetP());
if (!AbstractLfGenerator.checkActivePowerControl(generator.getId(), generator.getTargetP(), generator.getMinP(), generator.getMaxP(),
networkParameters.getPlausibleActivePowerLimit(), networkParameters.isUseActiveLimits(), null)) {
generator.setParticipating(false);
}
}
applyGeneratorChange(networkParameters);
}

if (hvdc != null) {
hvdc.setAcEmulation(false);
hvdc.setDisabled(true); // for equations only, but should be hidden
hvdc.getConverterStation1().setTargetP(-hvdc.getP1().eval()); // override
hvdc.getConverterStation2().setTargetP(-hvdc.getP2().eval()); // override
applyHvdcAction();
}

if (sectionChange != null) {
LfShunt shunt = sectionChange.shunt();
shunt.getControllers().stream().filter(controller -> controller.getId().equals(sectionChange.controllerId())).findAny()
.ifPresentOrElse(controller -> controller.updateSectionB(sectionChange.value()),
() -> LOGGER.warn("No section change: shunt {} not present", sectionChange.controllerId));
applySectionChange();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void testUnsupportedGeneratorAction() {
try (LfNetworkList lfNetworks = Networks.load(network, acParameters.getNetworkParameters(), new LfTopoConfig(), ReportNode.NO_OP)) {
LfNetwork lfNetwork = lfNetworks.getLargest().orElseThrow();
UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, () -> LfAction.create(generatorAction, lfNetwork, network, acParameters.getNetworkParameters().isBreakers()));
assertEquals("Generator action: configuration not supported yet.", e.getMessage());
assertEquals("Generator action on G : configuration not supported yet.", e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.powsybl.iidm.serde.test.MetrixTutorialSixBusesFactory;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.openloadflow.OpenLoadFlowParameters;
import com.powsybl.openloadflow.ac.solver.NewtonRaphsonStoppingCriteriaType;
import com.powsybl.openloadflow.graph.GraphConnectivityFactory;
import com.powsybl.openloadflow.graph.NaiveGraphConnectivityFactory;
import com.powsybl.openloadflow.network.*;
Expand Down Expand Up @@ -839,7 +840,7 @@ void testLoadAction() {
}

private void testGeneratorAction(boolean dc, LoadFlowParameters.BalanceType balanceType, double deltaG1, double deltaG2,
double targetPG4, double slackBusPMaxMismatch) {
double targetPG4) {
GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory = new NaiveGraphConnectivityFactory<>(LfBus::getNum);
securityAnalysisProvider = new OpenSecurityAnalysisProvider(matrixFactory, connectivityFactory);

Expand Down Expand Up @@ -869,7 +870,9 @@ private void testGeneratorAction(boolean dc, LoadFlowParameters.BalanceType bala
parameters.setDistributedSlack(true).setBalanceType(balanceType);
parameters.setDc(dc);
OpenLoadFlowParameters openLoadFlowParameters = new OpenLoadFlowParameters();
openLoadFlowParameters.setSlackBusPMaxMismatch(slackBusPMaxMismatch);
openLoadFlowParameters.setNewtonRaphsonStoppingCriteriaType(NewtonRaphsonStoppingCriteriaType.PER_EQUATION_TYPE_CRITERIA);
openLoadFlowParameters.setMaxActivePowerMismatch(1e-3);
openLoadFlowParameters.setSlackBusPMaxMismatch(1e-3);
parameters.addExtension(OpenLoadFlowParameters.class, openLoadFlowParameters);
SecurityAnalysisParameters securityAnalysisParameters = new SecurityAnalysisParameters();
securityAnalysisParameters.setLoadFlowParameters(parameters);
Expand All @@ -887,11 +890,9 @@ private void testGeneratorAction(boolean dc, LoadFlowParameters.BalanceType bala
assertEquals(network.getLine("l23").getTerminal1().getP(), getPostContingencyResult(result, lineInContingencyId).getNetworkResult().getBranchResult("l23").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l34").getTerminal1().getP(), getPostContingencyResult(result, lineInContingencyId).getNetworkResult().getBranchResult("l34").getP1(), LoadFlowAssert.DELTA_POWER);

double g4InitialTargetP = network.getGenerator(g4).getTargetP();
network.getGeneratorStream().forEach(gen -> gen.setTargetP(-gen.getTerminal().getP()));
double g1PostContingencyTargetP = network.getGenerator(g1).getTargetP();
double g2PostContingencyTargetP = network.getGenerator(g2).getTargetP();
double g4PostContingencyTargetP = network.getGenerator(g4).getTargetP();

// apply remedial action
network.getGenerator(g1).setTargetP(g1PostContingencyTargetP + deltaG1);
Expand All @@ -912,7 +913,7 @@ private void testGeneratorAction(boolean dc, LoadFlowParameters.BalanceType bala

// reverse action and apply third remedial action
network.getGenerator(g2).setTargetP(g2PostContingencyTargetP);
network.getGenerator(g4).setTargetP(targetPG4 + g4PostContingencyTargetP - g4InitialTargetP);
network.getGenerator(g4).setTargetP(targetPG4);
loadFlowRunner.run(network, parameters);
assertEquals(network.getLine("l12").getTerminal1().getP(), getOperatorStrategyResult(result, "strategyG3").getNetworkResult().getBranchResult("l12").getP1(), LoadFlowAssert.DELTA_POWER);
assertEquals(network.getLine("l14").getTerminal1().getP(), getOperatorStrategyResult(result, "strategyG3").getNetworkResult().getBranchResult("l14").getP1(), LoadFlowAssert.DELTA_POWER);
Expand All @@ -932,12 +933,12 @@ private void testGeneratorAction(boolean dc, LoadFlowParameters.BalanceType bala

@Test
void testGeneratorAction() {
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, 2.0, -1.5, 2, 0.0001);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, 1.0, -1.0, 4, 0.0001);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX, 1.77, -1.0, 0.0, 0.0005);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX, 1.0, -1.0, 2, 0.0005);
testGeneratorAction(true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, 2.0, -1.5, 0, 0.0001);
testGeneratorAction(true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX, 1.0, -1.0, 2, 0.0001);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, 2.0, -1.5, 2);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, 1.0, -1.0, 4);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX, 1.77, -1.0, 0.0);
testGeneratorAction(false, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX, 1.0, -1.0, 2.0);
testGeneratorAction(true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD, 2.0, -1.5, 0);
testGeneratorAction(true, LoadFlowParameters.BalanceType.PROPORTIONAL_TO_GENERATION_P_MAX, 1.0, -1.0, 2);
}

@Test
Expand Down

0 comments on commit a53116f

Please sign in to comment.