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 DC Outer Loops creation #1158

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
import com.powsybl.openloadflow.ac.solver.*;
import com.powsybl.openloadflow.ac.outerloop.AcOuterLoop;
import com.powsybl.openloadflow.ac.outerloop.ReactiveLimitsOuterLoop;
import com.powsybl.openloadflow.dc.DcAreaInterchangeControlOuterLoop;
import com.powsybl.openloadflow.dc.DcIncrementalPhaseControlOuterLoop;
import com.powsybl.openloadflow.dc.DcLoadFlowParameters;
import com.powsybl.openloadflow.dc.DcOuterLoop;
import com.powsybl.openloadflow.dc.DcValueVoltageInitializer;
import com.powsybl.openloadflow.dc.equations.DcApproximationType;
import com.powsybl.openloadflow.dc.equations.DcEquationSystemCreationParameters;
import com.powsybl.openloadflow.graph.GraphConnectivityFactory;
import com.powsybl.openloadflow.lf.AbstractLoadFlowParameters;
import com.powsybl.openloadflow.network.*;
import com.powsybl.openloadflow.network.util.ActivePowerDistribution;
import com.powsybl.openloadflow.network.util.PreviousValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.UniformValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.VoltageInitializer;
Expand Down Expand Up @@ -1816,7 +1820,7 @@ public static AcLoadFlowParameters createAcParameters(Network network, LoadFlowP
return acParameters;
}

static List<AcOuterLoop> createOuterLoops(LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt) {
static List<AcOuterLoop> createAcOuterLoops(LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt) {
AcOuterLoopConfig outerLoopConfig = AcOuterLoopConfig.findOuterLoopConfig()
.orElseGet(() -> parametersExt.getOuterLoopNames() != null ? new ExplicitAcOuterLoopConfig()
: new DefaultAcOuterLoopConfig());
Expand All @@ -1835,7 +1839,7 @@ public static AcLoadFlowParameters createAcParameters(LoadFlowParameters paramet

VoltageInitializer voltageInitializer = getExtendedVoltageInitializer(parameters, parametersExt, networkParameters, matrixFactory);

List<AcOuterLoop> outerLoops = createOuterLoops(parameters, parametersExt);
List<AcOuterLoop> outerLoops = createAcOuterLoops(parameters, parametersExt);

AcSolverFactory solverFactory = AcSolverFactory.find(parametersExt.getAcSolverType());

Expand All @@ -1851,6 +1855,20 @@ public static AcLoadFlowParameters createAcParameters(LoadFlowParameters paramet
.setSolverFactory(solverFactory, parameters);
}

static List<DcOuterLoop> createDcOuterLoops(LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt) {
List<DcOuterLoop> outerLoops = new ArrayList<>();
if (parameters.isPhaseShifterRegulationOn()) {
DcIncrementalPhaseControlOuterLoop phaseShifterControlOuterLoop = new DcIncrementalPhaseControlOuterLoop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, a small remark here, maybe a little bit out of the scope of this PR : when comparing with createAcOuterLoops, it shows that the parameter phaseShifterControlMode is not taken into account in DC mode. Maybe it should be mentioned in the doc (or even with a log message, because default value of this param is CONTINUOUS_WITH_DISCRETISATION and not INCREMENTAL)

outerLoops.add(phaseShifterControlOuterLoop);
}
if (parametersExt.isAreaInterchangeControl()) {
ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(parameters.getBalanceType(), false, parametersExt.isUseActiveLimits());
DcAreaInterchangeControlOuterLoop areaInterchangeControlOuterLoop = new DcAreaInterchangeControlOuterLoop(activePowerDistribution, parametersExt.getSlackBusPMaxMismatch(), parametersExt.getAreaInterchangePMaxMismatch());
outerLoops.add(areaInterchangeControlOuterLoop);
}
return outerLoops;
}

public static DcLoadFlowParameters createDcParameters(Network network, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt,
MatrixFactory matrixFactory, GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory,
boolean forcePhaseControlOffAndAddAngle1Var) {
Expand Down Expand Up @@ -1911,9 +1929,9 @@ public static DcLoadFlowParameters createDcParameters(LoadFlowParameters paramet
.setSlackDistributionFailureBehavior(parametersExt.getSlackDistributionFailureBehavior())
.setMatrixFactory(matrixFactory)
.setDistributedSlack(parameters.isDistributedSlack())
.setAreaInterchangeControl(parametersExt.isAreaInterchangeControl())
.setBalanceType(parameters.getBalanceType())
.setSetVToNan(true)
.setOuterLoops(createDcOuterLoops(parameters, parametersExt))
.setMaxOuterLoopIterations(parametersExt.getMaxOuterLoopIterations())
.setSlackBusPMaxMismatch(parametersExt.getSlackBusPMaxMismatch())
.setAreaInterchangePMaxMismatch(parametersExt.getAreaInterchangePMaxMismatch());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
/**
* @author Valentin Mouradian {@literal <valentin.mouradian at artelys.com>}
*/
public class DcAreaInterchangeControlControlOuterLoop extends AbstractAreaInterchangeControlOuterLoop<DcVariableType, DcEquationType, DcLoadFlowParameters, DcLoadFlowContext, DcOuterLoopContext> implements DcOuterLoop {
public class DcAreaInterchangeControlOuterLoop extends AbstractAreaInterchangeControlOuterLoop<DcVariableType, DcEquationType, DcLoadFlowParameters, DcLoadFlowContext, DcOuterLoopContext> implements DcOuterLoop {

private static final Logger LOGGER = LoggerFactory.getLogger(DcAreaInterchangeControlControlOuterLoop.class);
private static final Logger LOGGER = LoggerFactory.getLogger(DcAreaInterchangeControlOuterLoop.class);

protected DcAreaInterchangeControlControlOuterLoop(ActivePowerDistribution activePowerDistribution, double slackBusPMaxMismatch, double areaInterchangePMaxMismatch) {
public DcAreaInterchangeControlOuterLoop(ActivePowerDistribution activePowerDistribution, double slackBusPMaxMismatch, double areaInterchangePMaxMismatch) {
super(activePowerDistribution, null, slackBusPMaxMismatch, areaInterchangePMaxMismatch, LOGGER);
}

Expand Down
35 changes: 17 additions & 18 deletions src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -184,37 +183,37 @@ public DcLoadFlowResult run() {
DcLoadFlowParameters parameters = context.getParameters();
TargetVector<DcVariableType, DcEquationType> targetVector = context.getTargetVector();
RunningContext runningContext = new RunningContext();
List<DcOuterLoop> outerLoops = parameters.getOuterLoops();

// outer loop initialization
List<Pair<DcOuterLoop, DcOuterLoopContext>> outerLoopsAndContexts = new ArrayList<>();

if (parameters.getNetworkParameters().isPhaseControl()) {
DcIncrementalPhaseControlOuterLoop phaseShifterControlOuterLoop = new DcIncrementalPhaseControlOuterLoop();
DcOuterLoopContext phaseShifterControlOuterLoopContext = new DcOuterLoopContext(network);
outerLoopsAndContexts.add(Pair.of(phaseShifterControlOuterLoop, phaseShifterControlOuterLoopContext));
phaseShifterControlOuterLoop.initialize(phaseShifterControlOuterLoopContext);
// remove area interchange control outer loop if no area in the network, active power will be distributed with classical slack distribution
boolean areaInterchangeControl = outerLoops.stream().anyMatch(DcAreaInterchangeControlOuterLoop.class::isInstance);
if (!network.hasArea()) {
outerLoops.removeIf(DcAreaInterchangeControlOuterLoop.class::isInstance);
Comment on lines +188 to +191
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't remove the outerloop here.
the outerloop should handle the 'no area' situation by itself.

}

if (parameters.isAreaInterchangeControl() && network.hasArea()) {
ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(parameters.getBalanceType(), false, parameters.getNetworkParameters().isUseActiveLimits());
DcAreaInterchangeControlControlOuterLoop areaInterchangeControlOuterLoop = new DcAreaInterchangeControlControlOuterLoop(activePowerDistribution, parameters.getSlackBusPMaxMismatch(), parameters.getAreaInterchangePMaxMismatch());
DcOuterLoopContext areaInterchangeControlOuterLoopContext = new DcOuterLoopContext(network);
outerLoopsAndContexts.add(Pair.of(areaInterchangeControlOuterLoop, areaInterchangeControlOuterLoopContext));
areaInterchangeControlOuterLoop.initialize(areaInterchangeControlOuterLoopContext);
List<Pair<DcOuterLoop, DcOuterLoopContext>> outerLoopsAndContexts = outerLoops.stream()
.map(outerLoop -> Pair.of(outerLoop, new DcOuterLoopContext(network)))
.toList();

// outer loops initialization
for (var outerLoopAndContext : outerLoopsAndContexts) {
var outerLoop = outerLoopAndContext.getLeft();
var outerLoopContext = outerLoopAndContext.getRight();
outerLoop.initialize(outerLoopContext);
}

initStateVector(network, equationSystem, new UniformValueVoltageInitializer());

double initialSlackBusActivePowerMismatch = getActivePowerMismatch(network.getBuses());
double distributedActivePower = 0.0;
if (parameters.isDistributedSlack() || parameters.isAreaInterchangeControl()) {
if (parameters.isDistributedSlack() || areaInterchangeControl) {
LoadFlowParameters.BalanceType balanceType = parameters.getBalanceType();
boolean useActiveLimits = parameters.getNetworkParameters().isUseActiveLimits();
ActivePowerDistribution activePowerDistribution = ActivePowerDistribution.create(balanceType, false, useActiveLimits);
var result = activePowerDistribution.run(network, initialSlackBusActivePowerMismatch);
final LfGenerator referenceGenerator;
final OpenLoadFlowParameters.SlackDistributionFailureBehavior behavior;
if (parameters.isAreaInterchangeControl()) {
if (areaInterchangeControl) {
// actual behavior will be handled by the outerloop itself, just leave on slack bus here
behavior = OpenLoadFlowParameters.SlackDistributionFailureBehavior.LEAVE_ON_SLACK_BUS;
referenceGenerator = null;
Expand Down Expand Up @@ -284,7 +283,7 @@ public DcLoadFlowResult run() {
for (var outerLoopAndContext : Lists.reverse(outerLoopsAndContexts)) {
var outerLoop = outerLoopAndContext.getLeft();
var outerLoopContext = outerLoopAndContext.getRight();
if (outerLoop instanceof DcAreaInterchangeControlControlOuterLoop activePowerDistributionOuterLoop) {
if (outerLoop instanceof DcAreaInterchangeControlOuterLoop activePowerDistributionOuterLoop) {
distributedActivePower += activePowerDistributionOuterLoop.getDistributedActivePower(outerLoopContext);
}
outerLoop.cleanup(outerLoopContext);
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/powsybl/openloadflow/dc/DcLoadFlowParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.powsybl.openloadflow.dc.equations.DcEquationSystemCreationParameters;
import com.powsybl.openloadflow.lf.AbstractLoadFlowParameters;

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

/**
Expand All @@ -23,12 +25,12 @@ public class DcLoadFlowParameters extends AbstractLoadFlowParameters<DcLoadFlowP

private boolean distributedSlack = LoadFlowParameters.DEFAULT_DISTRIBUTED_SLACK;

private boolean areaInterchangeControl = OpenLoadFlowParameters.AREA_INTERCHANGE_CONTROL_DEFAULT_VALUE;

private LoadFlowParameters.BalanceType balanceType = LoadFlowParameters.DEFAULT_BALANCE_TYPE;

private boolean setVToNan = false;

protected List<DcOuterLoop> outerLoops = Collections.emptyList();

private int maxOuterLoopIterations = DEFAULT_MAX_OUTER_LOOP_ITERATIONS;

private double slackBusPMaxMismatch = OpenLoadFlowParameters.SLACK_BUS_P_MAX_MISMATCH_DEFAULT_VALUE;
Expand Down Expand Up @@ -62,15 +64,6 @@ public DcLoadFlowParameters setDistributedSlack(boolean distributedSlack) {
return this;
}

public boolean isAreaInterchangeControl() {
return areaInterchangeControl;
}

public DcLoadFlowParameters setAreaInterchangeControl(boolean areaInterchangeControl) {
this.areaInterchangeControl = areaInterchangeControl;
return this;
}

public LoadFlowParameters.BalanceType getBalanceType() {
return balanceType;
}
Expand All @@ -89,6 +82,15 @@ public DcLoadFlowParameters setSetVToNan(boolean setVToNan) {
return this;
}

public List<DcOuterLoop> getOuterLoops() {
return outerLoops;
}

public DcLoadFlowParameters setOuterLoops(List<DcOuterLoop> outerLoops) {
this.outerLoops = Objects.requireNonNull(outerLoops);
return this;
}

public double getSlackBusPMaxMismatch() {
return slackBusPMaxMismatch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class AbstractAreaInterchangeControlOuterLoop<
C extends LoadFlowContext<V, E, P>,
O extends AbstractOuterLoopContext<V, E, P, C>>
extends AbstractActivePowerDistributionOuterLoop<V, E, P, C, O>
implements OuterLoop<V, E, P, C, O>, ActivePowerDistributionOuterLoop<V, E, P, C, O> {
implements ActivePowerDistributionOuterLoop<V, E, P, C, O> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since AbstractActivePowerDistributionOuterLoop implements ActivePowerDistributionOuterLoop, this part is redundant and can be removed too

implements ActivePowerDistributionOuterLoop<V, E, P, C, O>


private final Logger logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,17 @@ void testExplicitOuterLoopsParameter() {
OpenLoadFlowParameters parametersExt = new OpenLoadFlowParameters()
.setSecondaryVoltageControl(true);

assertEquals(List.of("DistributedSlack", "SecondaryVoltageControl", "VoltageMonitoring", "ReactiveLimits", "ShuntVoltageControl"), OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());
assertEquals(List.of("DistributedSlack", "SecondaryVoltageControl", "VoltageMonitoring", "ReactiveLimits", "ShuntVoltageControl"), OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());

parametersExt.setOuterLoopNames(List.of("ReactiveLimits", "SecondaryVoltageControl"));
assertEquals(List.of("ReactiveLimits", "SecondaryVoltageControl"), OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());
assertEquals(List.of("ReactiveLimits", "SecondaryVoltageControl"), OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());

parametersExt.setOuterLoopNames(ExplicitAcOuterLoopConfig.NAMES);
PowsyblException e = assertThrows(PowsyblException.class, () -> OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt));
PowsyblException e = assertThrows(PowsyblException.class, () -> OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt));
assertEquals("Multiple (2) outer loops with same type: ShuntVoltageControl", e.getMessage());

parametersExt.setOuterLoopNames(List.of("ReactiveLimits", "Foo"));
e = assertThrows(PowsyblException.class, () -> OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt));
e = assertThrows(PowsyblException.class, () -> OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt));
assertEquals("Unknown outer loop 'Foo'", e.getMessage());

assertEquals("Ordered explicit list of outer loop names, supported outer loops are IncrementalPhaseControl, DistributedSlack, IncrementalShuntVoltageControl, IncrementalTransformerVoltageControl, VoltageMonitoring, PhaseControl, ReactiveLimits, SecondaryVoltageControl, ShuntVoltageControl, SimpleTransformerVoltageControl, TransformerVoltageControl, AutomationSystem, IncrementalTransformerReactivePowerControl, AreaInterchangeControl",
Expand All @@ -430,17 +430,16 @@ void testSlackDistributionOuterLoops() {
.setDistributedSlack(true);
OpenLoadFlowParameters parametersExt = new OpenLoadFlowParameters();

assertEquals(List.of("DistributedSlack", "VoltageMonitoring", "ReactiveLimits"), OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());
assertEquals(List.of("DistributedSlack", "VoltageMonitoring", "ReactiveLimits"), OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());

parametersExt.setAreaInterchangeControl(true);
assertEquals(List.of("AreaInterchangeControl", "VoltageMonitoring", "ReactiveLimits"), OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());
assertEquals(List.of("AreaInterchangeControl", "VoltageMonitoring", "ReactiveLimits"), OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());

parametersExt.setOuterLoopNames(List.of("DistributedSlack", "AreaInterchangeControl"));
assertEquals(List.of("AreaInterchangeControl"), OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());
assertEquals(List.of("AreaInterchangeControl"), OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());

parametersExt.setOuterLoopNames(List.of("DistributedSlack"));
assertEquals(List.of("DistributedSlack"), OpenLoadFlowParameters.createOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());

assertEquals(List.of("DistributedSlack"), OpenLoadFlowParameters.createAcOuterLoops(parameters, parametersExt).stream().map(OuterLoop::getType).toList());
}

@Test
Expand Down
Loading