Skip to content

Commit

Permalink
Loader post processor selection (#589)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@rte-france.com>
  • Loading branch information
geofjamg authored Aug 17, 2022
1 parent 8b5b581 commit c6af3d4
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public abstract class AbstractLfNetworkLoaderPostProcessor implements LfNetworkLoaderPostProcessor {

@Override
public LoadingPolicy getLoadingPolicy() {
return LoadingPolicy.ALWAYS;
}

@Override
public void onBusAdded(Object element, LfBus lfBus) {
// to implement
}

@Override
public void onBranchAdded(Object element, LfBranch lfBranch) {
// to implement
}

@Override
public void onInjectionAdded(Object element, LfBus lfBus) {
// to implement
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@
*/
public interface LfNetworkLoaderPostProcessor {

enum LoadingPolicy {
ALWAYS,
SELECTION
}

static List<LfNetworkLoaderPostProcessor> findAll() {
return Lists.newArrayList(ServiceLoader.load(LfNetworkLoaderPostProcessor.class, LfNetworkLoaderPostProcessor.class.getClassLoader()).iterator());
}

String getName();

LoadingPolicy getLoadingPolicy();

void onBusAdded(Object element, LfBus lfBus);

void onBranchAdded(Object element, LfBranch lfBranch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ public class LfNetworkParameters {

private final boolean hvdcAcEmulation;

private double minPlausibleTargetVoltage;
private final double minPlausibleTargetVoltage;

private double maxPlausibleTargetVoltage;
private final double maxPlausibleTargetVoltage;

private Set<String> loaderPostProcessorSelection = Collections.emptySet();

public LfNetworkParameters() {
this(new FirstSlackBusSelector());
Expand Down Expand Up @@ -213,6 +215,14 @@ public double getMaxPlausibleTargetVoltage() {
return maxPlausibleTargetVoltage;
}

public Set<String> getLoaderPostProcessorSelection() {
return loaderPostProcessorSelection;
}

public void setLoaderPostProcessorSelection(Set<String> loaderPostProcessorSelection) {
this.loaderPostProcessorSelection = Objects.requireNonNull(loaderPostProcessorSelection);
}

@Override
public String toString() {
return "LfNetworkParameters(" +
Expand All @@ -236,6 +246,7 @@ public String toString() {
", hvdcAcEmulation=" + hvdcAcEmulation +
", minPlausibleTargetVoltage=" + minPlausibleTargetVoltage +
", maxPlausibleTargetVoltage=" + maxPlausibleTargetVoltage +
", loaderPostProcessorSelection=" + loaderPostProcessorSelection +
')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -52,6 +53,16 @@ private static class LoadingContext {
private final Set<HvdcLine> hvdcLineSet = new LinkedHashSet<>();
}

private final Supplier<List<LfNetworkLoaderPostProcessor>> postProcessorsSupplier;

public LfNetworkLoaderImpl() {
this(LfNetworkLoaderPostProcessor::findAll);
}

public LfNetworkLoaderImpl(Supplier<List<LfNetworkLoaderPostProcessor>> postProcessorsSupplier) {
this.postProcessorsSupplier = Objects.requireNonNull(postProcessorsSupplier);
}

private static void createBuses(List<Bus> buses, LfNetworkParameters parameters, LfNetwork lfNetwork, List<LfBus> lfBuses,
LoadingContext loadingContext, LfNetworkLoadingReport report, List<LfNetworkLoaderPostProcessor> postProcessors) {
for (Bus bus : buses) {
Expand Down Expand Up @@ -703,13 +714,16 @@ private static LfBus getLfBus(Terminal terminal, LfNetwork lfNetwork, boolean br
return bus != null ? lfNetwork.getBusById(bus.getId()) : null;
}

private static LfNetwork create(int numCC, int numSC, List<Bus> buses, List<Switch> switches, LfNetworkParameters parameters, Reporter reporter) {
private LfNetwork create(int numCC, int numSC, List<Bus> buses, List<Switch> switches, LfNetworkParameters parameters, Reporter reporter) {
LfNetwork lfNetwork = new LfNetwork(numCC, numSC, parameters.getSlackBusSelector(),
parameters.getConnectivityFactory(), reporter);

LoadingContext loadingContext = new LoadingContext();
LfNetworkLoadingReport report = new LfNetworkLoadingReport();
List<LfNetworkLoaderPostProcessor> postProcessors = LfNetworkLoaderPostProcessor.findAll();
List<LfNetworkLoaderPostProcessor> postProcessors = postProcessorsSupplier.get().stream()
.filter(pp -> pp.getLoadingPolicy() == LfNetworkLoaderPostProcessor.LoadingPolicy.ALWAYS
|| (pp.getLoadingPolicy() == LfNetworkLoaderPostProcessor.LoadingPolicy.SELECTION && parameters.getLoaderPostProcessorSelection().contains(pp.getName())))
.collect(Collectors.toList());

List<LfBus> lfBuses = new ArrayList<>();
createBuses(buses, parameters, lfNetwork, lfBuses, loadingContext, report, postProcessors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ void test() {
void testDcParameters() {
Network network = Mockito.mock(Network.class);
DcLoadFlowParameters dcParameters = OpenLoadFlowParameters.createDcParameters(network, new LoadFlowParameters().setReadSlackBus(true), new OpenLoadFlowParameters(), new DenseMatrixFactory(), new EvenShiloachGraphDecrementalConnectivityFactory<>(), true);
assertEquals("DcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=false, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, addRatioToLinesWithDifferentNominalVoltageAtBothEnds=true, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, isDc=true, reactiveLimits=false, hvdcAcEmulation=false, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2), equationSystemCreationParameters=DcEquationSystemCreationParameters(updateFlows=true, indexTerms=false, forcePhaseControlOffAndAddAngle1Var=true, useTransformerRatio=true), matrixFactory=DenseMatrixFactory, distributedSlack=true, balanceType=PROPORTIONAL_TO_GENERATION_P_MAX, setVToNan=true)",
assertEquals("DcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=false, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, addRatioToLinesWithDifferentNominalVoltageAtBothEnds=true, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, isDc=true, reactiveLimits=false, hvdcAcEmulation=false, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2, loaderPostProcessorSelection=[]), equationSystemCreationParameters=DcEquationSystemCreationParameters(updateFlows=true, indexTerms=false, forcePhaseControlOffAndAddAngle1Var=true, useTransformerRatio=true), matrixFactory=DenseMatrixFactory, distributedSlack=true, balanceType=PROPORTIONAL_TO_GENERATION_P_MAX, setVToNan=true)",
dcParameters.toString());
}

@Test
void testAcParameters() {
Network network = Mockito.mock(Network.class);
AcLoadFlowParameters acParameters = OpenLoadFlowParameters.createAcParameters(network, new LoadFlowParameters().setReadSlackBus(true), new OpenLoadFlowParameters(), new DenseMatrixFactory(), new EvenShiloachGraphDecrementalConnectivityFactory<>(), Reporter.NO_OP, false, false);
assertEquals("AcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=true, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, addRatioToLinesWithDifferentNominalVoltageAtBothEnds=true, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, isDc=false, reactiveLimits=true, hvdcAcEmulation=true, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2), equationSystemCreationParameters=AcEquationSystemCreationParameters(forceA1Var=false), newtonRaphsonParameters=NewtonRaphsonParameters(maxIteration=30, stoppingCriteria=DefaultNewtonRaphsonStoppingCriteria), outerLoops=[DistributedSlackOuterLoop, ReactiveLimitsOuterLoop], matrixFactory=DenseMatrixFactory, voltageInitializer=UniformValueVoltageInitializer)",
assertEquals("AcLoadFlowParameters(networkParameters=LfNetworkParameters(slackBusSelector=NetworkSlackBusSelector, connectivityFactory=EvenShiloachGraphDecrementalConnectivityFactory, generatorVoltageRemoteControl=true, minImpedance=false, twtSplitShuntAdmittance=false, breakers=false, plausibleActivePowerLimit=5000.0, addRatioToLinesWithDifferentNominalVoltageAtBothEnds=true, computeMainConnectedComponentOnly=true, countriesToBalance=[], distributedOnConformLoad=false, phaseControl=false, transformerVoltageControl=false, voltagePerReactivePowerControl=false, reactivePowerRemoteControl=false, isDc=false, reactiveLimits=true, hvdcAcEmulation=true, minPlausibleTargetVoltage=0.8, maxPlausibleTargetVoltage=1.2, loaderPostProcessorSelection=[]), equationSystemCreationParameters=AcEquationSystemCreationParameters(forceA1Var=false), newtonRaphsonParameters=NewtonRaphsonParameters(maxIteration=30, stoppingCriteria=DefaultNewtonRaphsonStoppingCriteria), outerLoops=[DistributedSlackOuterLoop, ReactiveLimitsOuterLoop], matrixFactory=DenseMatrixFactory, voltageInitializer=UniformValueVoltageInitializer)",
acParameters.toString());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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 com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.openloadflow.network.impl.LfNetworkLoaderImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

private static final String PP_1 = "PP1";
private static final String PP_2 = "PP2";

private Network network;
private LfNetworkParameters parameters;

private LfNetworkLoaderPostProcessor pp1;
private LfNetworkLoaderPostProcessor pp2;

private boolean pp1Activated;
private boolean pp2Activated;

@BeforeEach
void setUp() {
network = EurostagTutorialExample1Factory.create();
parameters = new LfNetworkParameters();

pp1 = new AbstractLfNetworkLoaderPostProcessor() {
@Override
public String getName() {
return PP_1;
}

@Override
public void onBusAdded(Object element, LfBus lfBus) {
pp1Activated = true;
}
};

pp2 = new AbstractLfNetworkLoaderPostProcessor() {
@Override
public String getName() {
return PP_2;
}

@Override
public LoadingPolicy getLoadingPolicy() {
return LoadingPolicy.SELECTION;
}

@Override
public void onBusAdded(Object element, LfBus lfBus) {
pp2Activated = true;
}
};

pp1Activated = false;
pp2Activated = false;
}

@Test
void test1() {
LfNetwork.load(network, new LfNetworkLoaderImpl(() -> List.of(pp1, pp2)), parameters);
assertTrue(pp1Activated);
assertFalse(pp2Activated);
}

@Test
void test2() {
parameters.setLoaderPostProcessorSelection(Set.of(PP_2));
LfNetwork.load(network, new LfNetworkLoaderImpl(() -> List.of(pp1, pp2)), parameters);
assertTrue(pp1Activated);
assertTrue(pp2Activated);
}
}

0 comments on commit c6af3d4

Please sign in to comment.