Skip to content

Commit

Permalink
Merge branch 'main' into fix-dispatch-q
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLt authored May 5, 2022
2 parents 7fcc012 + 2af86c2 commit cf28d58
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.openloadflow.ac;

import com.powsybl.openloadflow.ac.outerloop.OuterLoop;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopContext;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopStatus;
import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.LfNetwork;
Expand Down Expand Up @@ -43,8 +44,8 @@ protected OuterLoopStatus roundVoltageRatios(LfNetwork network) {
}

@Override
public void cleanup(LfNetwork network) {
for (LfBranch branch : network.getBranches()) {
public void cleanup(OuterLoopContext context) {
for (LfBranch branch : context.getNetwork().getBranches()) {
branch.getVoltageControl().ifPresent(voltageControl -> branch.setVoltageControlEnabled(true));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public String getType() {
}

@Override
public void initialize(LfNetwork network) {
public void initialize(OuterLoopContext context) {
List<LfBranch> controllerBranches = new ArrayList<>(1);
List<LfBranch> disabledBranches = new ArrayList<>(1);
LfNetwork network = context.getNetwork();
for (LfBranch branch : network.getBranches()) {
if (!branch.isDisabled() && branch.isPhaseController() && branch.isPhaseControlEnabled()) {
controllerBranches.add(branch);
Expand Down Expand Up @@ -163,8 +164,8 @@ boolean isSensitivityCurrentPerA1Positive(LfBranch controllerBranch, DiscretePha
}

@Override
public void cleanup(LfNetwork network) {
for (LfBranch branch : network.getBranches()) {
public void cleanup(OuterLoopContext context) {
for (LfBranch branch : context.getNetwork().getBranches()) {
if (branch.isPhaseController()) {
branch.setPhaseControlEnabled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import com.powsybl.openloadflow.ac.outerloop.OuterLoopContext;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopStatus;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.util.PerUnit;
import com.powsybl.openloadflow.network.VoltageControl;
import com.powsybl.openloadflow.util.PerUnit;
import org.apache.commons.lang3.mutable.MutableInt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -240,8 +239,8 @@ public OuterLoopStatus check(OuterLoopContext context, Reporter reporter) {
}

@Override
public void cleanup(LfNetwork network) {
for (LfBus bus : network.getBuses()) {
public void cleanup(OuterLoopContext context) {
for (LfBus bus : context.getNetwork().getBuses()) {
bus.setVoltageControlSwitchOffCount(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.powsybl.openloadflow.ac.outerloop.OuterLoopContext;
import com.powsybl.openloadflow.ac.outerloop.OuterLoopStatus;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.LfShunt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -51,8 +50,8 @@ public OuterLoopStatus check(OuterLoopContext context, Reporter reporter) {
}

@Override
public void cleanup(LfNetwork network) {
for (LfBus bus : network.getBuses()) {
public void cleanup(OuterLoopContext context) {
for (LfBus bus : context.getNetwork().getBuses()) {
bus.getShuntVoltageControl().ifPresent(b -> b.getControllers().forEach(controllerShunt -> controllerShunt.setVoltageControlEnabled(true)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,39 @@
import com.powsybl.openloadflow.ac.outerloop.OuterLoopStatus;
import com.powsybl.openloadflow.network.LfBranch;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.TransformerVoltageControl;

/**
* @author Anne Tilloy <anne.tilloy at rte-france.com>
*/
public class TransformerVoltageControlOuterLoop extends AbstractTransformerVoltageControlOuterLoop {

public static final String MAX_CONTROLLED_NOMINAL_VOLTAGE = "maxControlledNominalVoltage";
private static final class ContextData {

private final double maxControlledNominalVoltage;

private ContextData(double maxControlledNominalVoltage) {
this.maxControlledNominalVoltage = maxControlledNominalVoltage;
}

private double getMaxControlledNominalVoltage() {
return maxControlledNominalVoltage;
}
}

@Override
public void initialize(LfNetwork network) {
public void initialize(OuterLoopContext context) {
// All transformer voltage control are disabled for the first equation system resolution.
double[] maxControlledNominalVoltage = new double[1];
maxControlledNominalVoltage[0] = Double.MIN_VALUE;
for (LfBranch branch : network.getBranches()) {
for (LfBranch branch : context.getNetwork().getBranches()) {
branch.getVoltageControl().ifPresent(voltageControl -> {
branch.setVoltageControlEnabled(false);
maxControlledNominalVoltage[0] = Math.max(maxControlledNominalVoltage[0], voltageControl.getControlled().getNominalV());
});
}
network.setProperty(MAX_CONTROLLED_NOMINAL_VOLTAGE, maxControlledNominalVoltage[0]);

context.setData(new ContextData(maxControlledNominalVoltage[0]));
}

@Override
Expand All @@ -44,7 +55,7 @@ public String getType() {
public OuterLoopStatus check(OuterLoopContext context, Reporter reporter) {
OuterLoopStatus status = OuterLoopStatus.STABLE;

double maxControlledNominalVoltage = (Double) context.getNetwork().getProperty(MAX_CONTROLLED_NOMINAL_VOLTAGE);
double maxControlledNominalVoltage = ((ContextData) context.getData()).getMaxControlledNominalVoltage();

// At first outer loop iteration, the voltage control of generators that controlled at nominal voltage of
// the set controlledNominalVoltages are disabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.openloadflow.ac.outerloop;

import com.google.common.collect.Lists;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.openloadflow.ac.nr.NewtonRaphson;
import com.powsybl.openloadflow.ac.nr.NewtonRaphsonResult;
Expand All @@ -15,6 +16,7 @@
import com.powsybl.openloadflow.network.util.PreviousValueVoltageInitializer;
import com.powsybl.openloadflow.network.util.VoltageInitializer;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -48,7 +50,7 @@ private static class RunningContext {
private final Map<String, MutableInt> outerLoopIterationByType = new HashMap<>();
}

private void runOuterLoop(OuterLoop outerLoop, LfNetwork network, NewtonRaphson newtonRaphson, RunningContext runningContext,
private void runOuterLoop(OuterLoop outerLoop, OuterLoopContextImpl outerLoopContext, NewtonRaphson newtonRaphson, RunningContext runningContext,
Reporter reporter) {
Reporter olReporter = reporter.createSubReporter("OuterLoop", "Outer loop ${outerLoopType}", "outerLoopType", outerLoop.getType());

Expand All @@ -58,7 +60,9 @@ private void runOuterLoop(OuterLoop outerLoop, LfNetwork network, NewtonRaphson
MutableInt outerLoopIteration = runningContext.outerLoopIterationByType.computeIfAbsent(outerLoop.getType(), k -> new MutableInt());

// check outer loop status
outerLoopStatus = outerLoop.check(new OuterLoopContext(outerLoopIteration.getValue(), network, runningContext.lastNrResult), olReporter);
outerLoopContext.setIteration(outerLoopIteration.getValue());
outerLoopContext.setLastNewtonRaphsonResult(runningContext.lastNrResult);
outerLoopStatus = outerLoop.check(outerLoopContext, olReporter);

if (outerLoopStatus == OuterLoopStatus.UNSTABLE) {
LOGGER.debug("Start outer loop iteration {} (name='{}')", outerLoopIteration, outerLoop.getType());
Expand Down Expand Up @@ -91,9 +95,16 @@ public AcLoadFlowResult run(Reporter reporter) {
NewtonRaphson newtonRaphson = new NewtonRaphson(context.getNetwork(), context.getParameters().getNewtonRaphsonParameters(),
context.getEquationSystem(), context.getJacobianMatrix(), context.getTargetVector());

List<OuterLoop> outerLoops = context.getParameters().getOuterLoops();
List<Pair<OuterLoop, OuterLoopContextImpl>> outerLoopsAndContexts = outerLoops.stream()
.map(outerLoop -> Pair.of(outerLoop, new OuterLoopContextImpl(context.getNetwork())))
.collect(Collectors.toList());

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

// run initial Newton-Raphson
Expand All @@ -108,8 +119,8 @@ public AcLoadFlowResult run(Reporter reporter) {
oldIterationCount = runningContext.lastNrResult.getIteration();

// outer loops are nested: inner most loop first in the list, outer most loop last
for (OuterLoop outerLoop : context.getParameters().getOuterLoops()) {
runOuterLoop(outerLoop, context.getNetwork(), newtonRaphson, runningContext, reporter);
for (var outerLoopAndContext : outerLoopsAndContexts) {
runOuterLoop(outerLoopAndContext.getLeft(), outerLoopAndContext.getRight(), newtonRaphson, runningContext, reporter);

// continue with next outer loop only if last Newton-Raphson succeed
if (runningContext.lastNrResult.getStatus() != NewtonRaphsonStatus.CONVERGED) {
Expand All @@ -120,9 +131,11 @@ public AcLoadFlowResult run(Reporter reporter) {
&& runningContext.lastNrResult.getStatus() == NewtonRaphsonStatus.CONVERGED);
}

// outer loops finalization
for (OuterLoop outerLoop : context.getParameters().getOuterLoops()) {
outerLoop.cleanup(context.getNetwork());
// outer loops finalization (in reverse order to allow correct cleanup)
for (var outerLoopAndContext : Lists.reverse(outerLoopsAndContexts)) {
var outerLoop = outerLoopAndContext.getLeft();
var outerLoopContext = outerLoopAndContext.getRight();
outerLoop.cleanup(outerLoopContext);
}

int nrIterations = runningContext.lastNrResult.getIteration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.powsybl.openloadflow.ac.outerloop;

import com.powsybl.commons.reporter.Reporter;
import com.powsybl.openloadflow.network.LfNetwork;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand All @@ -16,11 +15,11 @@ public interface OuterLoop {

String getType();

default void initialize(LfNetwork network) {
default void initialize(OuterLoopContext context) {
}

OuterLoopStatus check(OuterLoopContext context, Reporter reporter);

default void cleanup(LfNetwork network) {
default void cleanup(OuterLoopContext context) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,18 @@
import com.powsybl.openloadflow.ac.nr.NewtonRaphsonResult;
import com.powsybl.openloadflow.network.LfNetwork;

import java.util.Objects;

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

private final int iteration;

private final LfNetwork network;
public interface OuterLoopContext {

private final NewtonRaphsonResult lastNewtonRaphsonResult;
LfNetwork getNetwork();

OuterLoopContext(int iteration, LfNetwork network, NewtonRaphsonResult lastNewtonRaphsonResult) {
this.iteration = iteration;
this.network = Objects.requireNonNull(network);
this.lastNewtonRaphsonResult = Objects.requireNonNull(lastNewtonRaphsonResult);
}
int getIteration();

public int getIteration() {
return iteration;
}
NewtonRaphsonResult getLastNewtonRaphsonResult();

public LfNetwork getNetwork() {
return network;
}
Object getData();

public NewtonRaphsonResult getLastNewtonRaphsonResult() {
return lastNewtonRaphsonResult;
}
void setData(Object data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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.ac.outerloop;

import com.powsybl.openloadflow.ac.nr.NewtonRaphsonResult;
import com.powsybl.openloadflow.network.LfNetwork;

import java.util.Objects;

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

private final LfNetwork network;

private int iteration;

private NewtonRaphsonResult lastNewtonRaphsonResult;

private Object data;

OuterLoopContextImpl(LfNetwork network) {
this.network = Objects.requireNonNull(network);
}

@Override
public LfNetwork getNetwork() {
return network;
}

@Override
public int getIteration() {
return iteration;
}

public void setIteration(int iteration) {
this.iteration = iteration;
}

@Override
public NewtonRaphsonResult getLastNewtonRaphsonResult() {
return lastNewtonRaphsonResult;
}

public void setLastNewtonRaphsonResult(NewtonRaphsonResult lastNewtonRaphsonResult) {
this.lastNewtonRaphsonResult = lastNewtonRaphsonResult;
}

@Override
public Object getData() {
return data;
}

@Override
public void setData(Object data) {
this.data = data;
}
}

0 comments on commit cf28d58

Please sign in to comment.