Skip to content

Commit

Permalink
Generator Reference Priorities
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Jeandemange <damien.jeandemange@artelys.com>
  • Loading branch information
jeandemanged committed Jan 18, 2024
1 parent 8990bbd commit 858d9e7
Show file tree
Hide file tree
Showing 24 changed files with 337 additions and 46 deletions.
44 changes: 41 additions & 3 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public enum SlackDistributionFailureBehavior {

public static final String NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME = "newtonKrylovLineSearch";

public static final String USE_REFERENCE_PRIORITIES_PARAM_NAME = "useReferencePriorities";

public static final String WRITE_REFERENCE_TERMINALS_PARAM_NAME = "writeReferenceTerminals";

private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> enumClass) {
return EnumSet.allOf(enumClass).stream().map(Enum::name).collect(Collectors.toList());
}
Expand Down Expand Up @@ -287,7 +291,9 @@ private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> e
new Parameter(DC_APPROXIMATION_TYPE_PARAM_NAME, ParameterType.STRING, "DC approximation type", DcEquationSystemCreationParameters.DC_APPROXIMATION_TYPE_DEFAULT_VALUE.name(), getEnumPossibleValues(DcApproximationType.class)),
new Parameter(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME, ParameterType.BOOLEAN, "Automation systems simulation", LfNetworkParameters.SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE),
new Parameter(MAX_NEWTON_KRYLOV_ITERATIONS_PARAM_NAME, ParameterType.INTEGER, "Newton Krylov max number of iterations", NewtonKrylovParameters.DEFAULT_MAX_ITERATIONS),
new Parameter(NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME, ParameterType.BOOLEAN, "Newton Krylov line search activation", NewtonKrylovParameters.LINE_SEARCH_DEFAULT_VALUE)
new Parameter(NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME, ParameterType.BOOLEAN, "Newton Krylov line search activation", NewtonKrylovParameters.LINE_SEARCH_DEFAULT_VALUE),
new Parameter(USE_REFERENCE_PRIORITIES_PARAM_NAME, ParameterType.BOOLEAN, "Use Reference Priorities", LfNetworkParameters.USE_REFERENCE_PRIORITIES_DEFAULT_VALUE),
new Parameter(WRITE_REFERENCE_TERMINALS_PARAM_NAME, ParameterType.BOOLEAN, "Write Reference Terminals", LfNetworkParameters.WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE)
);

public enum VoltageInitModeOverride {
Expand Down Expand Up @@ -447,6 +453,10 @@ public enum ReactiveRangeCheckMode {

private boolean newtonKrylovLineSearch = NewtonKrylovParameters.LINE_SEARCH_DEFAULT_VALUE;

private boolean useReferencePriorities = LfNetworkParameters.USE_REFERENCE_PRIORITIES_DEFAULT_VALUE;

private boolean writeReferenceTerminals = LfNetworkParameters.WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE;

public static double checkParameterValue(double parameterValue, boolean condition, String parameterName) {
if (!condition) {
throw new IllegalArgumentException("Invalid value for parameter " + parameterName + ": " + parameterValue);
Expand Down Expand Up @@ -1054,6 +1064,24 @@ public OpenLoadFlowParameters setNewtonKrylovLineSearch(boolean newtonKrylovLine
return this;
}

public boolean isUseReferencePriorities() {
return useReferencePriorities;
}

public OpenLoadFlowParameters setUseReferencePriorities(boolean useReferencePriorities) {
this.useReferencePriorities = useReferencePriorities;
return this;
}

public boolean isWriteReferenceTerminals() {
return writeReferenceTerminals;
}

public OpenLoadFlowParameters setWriteReferenceTerminals(boolean writeReferenceTerminals) {
this.writeReferenceTerminals = writeReferenceTerminals;
return this;
}

public static OpenLoadFlowParameters load() {
return load(PlatformConfig.defaultConfig());
}
Expand Down Expand Up @@ -1120,7 +1148,9 @@ public static OpenLoadFlowParameters load(PlatformConfig platformConfig) {
.setDcApproximationType(config.getEnumProperty(DC_APPROXIMATION_TYPE_PARAM_NAME, DcApproximationType.class, DcEquationSystemCreationParameters.DC_APPROXIMATION_TYPE_DEFAULT_VALUE))
.setSimulateAutomationSystems(config.getBooleanProperty(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME, LfNetworkParameters.SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE))
.setMaxNewtonKrylovIterations(config.getIntProperty(MAX_NEWTON_KRYLOV_ITERATIONS_PARAM_NAME, NewtonKrylovParameters.DEFAULT_MAX_ITERATIONS))
.setNewtonKrylovLineSearch(config.getBooleanProperty(NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME, NewtonKrylovParameters.LINE_SEARCH_DEFAULT_VALUE)));
.setNewtonKrylovLineSearch(config.getBooleanProperty(NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME, NewtonKrylovParameters.LINE_SEARCH_DEFAULT_VALUE))
.setUseReferencePriorities(config.getBooleanProperty(USE_REFERENCE_PRIORITIES_PARAM_NAME, LfNetworkParameters.USE_REFERENCE_PRIORITIES_DEFAULT_VALUE))
.setWriteReferenceTerminals(config.getBooleanProperty(WRITE_REFERENCE_TERMINALS_PARAM_NAME, LfNetworkParameters.WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE)));
return parameters;
}

Expand Down Expand Up @@ -1252,6 +1282,10 @@ public OpenLoadFlowParameters update(Map<String, String> properties) {
.ifPresent(prop -> this.setMaxNewtonKrylovIterations(Integer.parseInt(prop)));
Optional.ofNullable(properties.get(NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME))
.ifPresent(prop -> this.setNewtonKrylovLineSearch(Boolean.parseBoolean(prop)));
Optional.ofNullable(properties.get(USE_REFERENCE_PRIORITIES_PARAM_NAME))
.ifPresent(prop -> this.setUseReferencePriorities(Boolean.parseBoolean(prop)));
Optional.ofNullable(properties.get(WRITE_REFERENCE_TERMINALS_PARAM_NAME))
.ifPresent(prop -> this.setWriteReferenceTerminals(Boolean.parseBoolean(prop)));
return this;
}

Expand Down Expand Up @@ -1315,6 +1349,8 @@ public Map<String, Object> toMap() {
map.put(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME, simulateAutomationSystems);
map.put(MAX_NEWTON_KRYLOV_ITERATIONS_PARAM_NAME, maxNewtonKrylovIterations);
map.put(NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME, newtonKrylovLineSearch);
map.put(USE_REFERENCE_PRIORITIES_PARAM_NAME, useReferencePriorities);
map.put(WRITE_REFERENCE_TERMINALS_PARAM_NAME, writeReferenceTerminals);
return map;
}

Expand Down Expand Up @@ -1438,7 +1474,9 @@ static LfNetworkParameters getNetworkParameters(LoadFlowParameters parameters, O
.setMinNominalVoltageTargetVoltageCheck(parametersExt.getMinNominalVoltageTargetVoltageCheck())
.setLinePerUnitMode(parametersExt.getLinePerUnitMode())
.setUseLoadModel(parametersExt.isUseLoadModel())
.setSimulateAutomationSystems(parametersExt.isSimulateAutomationSystems());
.setSimulateAutomationSystems(parametersExt.isSimulateAutomationSystems())
.setUseReferencePriorities(parametersExt.isUseReferencePriorities())
.setWriteReferenceTerminals(parametersExt.isWriteReferenceTerminals());
}

public static AcLoadFlowParameters createAcParameters(Network network, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt,
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.computation.ComputationManager;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.extensions.ReferenceTerminals;
import com.powsybl.iidm.network.extensions.SlackTerminal;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowProvider;
Expand Down Expand Up @@ -110,7 +111,8 @@ private void updateAcState(Network network, LoadFlowParameters parameters, OpenL
parameters.isDistributedSlack() && (parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD || parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD) && parametersExt.isLoadPowerFactorConstant(),
parameters.isDc(),
acParameters.getNetworkParameters().isBreakers(),
parametersExt.getReactivePowerDispatchMode());
parametersExt.getReactivePowerDispatchMode(),
parametersExt.isWriteReferenceTerminals());
result.getNetwork().updateState(updateParameters);

// zero or low impedance branch flows computation
Expand Down Expand Up @@ -151,6 +153,10 @@ private LoadFlowResult runAc(Network network, LoadFlowParameters parameters, Ope
if (parameters.isWriteSlackBus()) {
SlackTerminal.reset(network);
}
// reset reference terminals if at least one component has converged
if (parametersExt.isWriteReferenceTerminals()) {
ReferenceTerminals.reset(network);
}
}

List<LoadFlowResult.ComponentResult> componentResults = new ArrayList<>(results.size());
Expand Down Expand Up @@ -207,12 +213,12 @@ private LoadFlowResult runDc(Network network, LoadFlowParameters parameters, Ope

Networks.resetState(network);

List<LoadFlowResult.ComponentResult> componentsResult = results.stream().map(r -> processResult(network, r, parameters, dcParameters.getNetworkParameters().isBreakers())).toList();
List<LoadFlowResult.ComponentResult> componentsResult = results.stream().map(r -> processResult(network, r, parameters, parametersExt, dcParameters.getNetworkParameters().isBreakers())).toList();
boolean ok = results.stream().anyMatch(DcLoadFlowResult::isSuccess);
return new LoadFlowResultImpl(ok, Collections.emptyMap(), null, componentsResult);
}

private LoadFlowResult.ComponentResult processResult(Network network, DcLoadFlowResult result, LoadFlowParameters parameters, boolean breakers) {
private LoadFlowResult.ComponentResult processResult(Network network, DcLoadFlowResult result, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt, boolean breakers) {
if (result.isSuccess() && parameters.isWriteSlackBus()) {
SlackTerminal.reset(network);
}
Expand All @@ -225,7 +231,8 @@ private LoadFlowResult.ComponentResult processResult(Network network, DcLoadFlow
false,
true,
breakers,
ReactivePowerDispatchMode.Q_EQUAL_PROPORTION);
ReactivePowerDispatchMode.Q_EQUAL_PROPORTION,
parametersExt.isWriteReferenceTerminals());
result.getNetwork().updateState(updateParameters);

// zero or low impedance branch flows computation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public interface LfGenerator extends PropertyBag {
public interface LfGenerator extends PropertyBag, LfReferencePriorityInjection {

enum GeneratorControlType {
OFF, REMOTE_REACTIVE_POWER, VOLTAGE, MONITORING_VOLTAGE
Expand Down Expand Up @@ -99,7 +99,7 @@ default double getParticipationFactor() {

void setCalculatedQ(double calculatedQ);

void updateState();
void updateState(LfNetworkStateUpdateParameters parameters);

LfBus getControlledBus();

Expand Down
56 changes: 50 additions & 6 deletions src/main/java/com/powsybl/openloadflow/network/LfNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class LfNetwork extends AbstractPropertyBag implements PropertyBag {

private final SlackBusSelector slackBusSelector;

private final boolean useReferencePriorities;

private final int maxSlackBusCount;

private final Map<String, LfBus> busesById = new LinkedHashMap<>();
Expand All @@ -54,6 +56,8 @@ public class LfNetwork extends AbstractPropertyBag implements PropertyBag {

private List<LfBus> slackBuses;

private LfGenerator referenceGenerator;

private final List<LfBranch> branches = new ArrayList<>();

private final Map<String, LfBranch> branchesById = new HashMap<>();
Expand Down Expand Up @@ -127,18 +131,19 @@ public double getLowValue() {
protected final List<LfOverloadManagementSystem> overloadManagementSystems = new ArrayList<>();

public LfNetwork(int numCC, int numSC, SlackBusSelector slackBusSelector, int maxSlackBusCount,
GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory, Reporter reporter) {
GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory, boolean useReferencePriorities, Reporter reporter) {
this.numCC = numCC;
this.numSC = numSC;
this.slackBusSelector = Objects.requireNonNull(slackBusSelector);
this.maxSlackBusCount = maxSlackBusCount;
this.connectivityFactory = Objects.requireNonNull(connectivityFactory);
this.useReferencePriorities = useReferencePriorities;
this.reporter = Objects.requireNonNull(reporter);
}

public LfNetwork(int numCC, int numSC, SlackBusSelector slackBusSelector, int maxSlackBusCount,
GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory) {
this(numCC, numSC, slackBusSelector, maxSlackBusCount, connectivityFactory, Reporter.NO_OP);
GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory, boolean useReferencePriorities) {
this(numCC, numSC, slackBusSelector, maxSlackBusCount, connectivityFactory, useReferencePriorities, Reporter.NO_OP);
}

public int getNumCC() {
Expand Down Expand Up @@ -177,6 +182,10 @@ private void invalidateSlackAndReference() {
referenceBus.setReference(false);
}
referenceBus = null;
if (referenceGenerator != null) {
referenceGenerator.setReference(false);
}
referenceGenerator = null;
}

public void updateSlackBusesAndReferenceBus() {
Expand All @@ -187,8 +196,38 @@ public void updateSlackBusesAndReferenceBus() {
for (var slackBus : slackBuses) {
slackBus.setSlack(true);
}
referenceBus = slackBuses.get(0);
referenceBus.setReference(true);
if (!useReferencePriorities) {
// just use first slack as reference bus
referenceBus = slackBuses.get(0);
referenceBus.setReference(true);
LOGGER.info("Network {}, reference bus is {}", this, referenceBus);
} else {
List<LfGenerator> lfGenerators = busesByIndex.stream()
.filter(bus -> !bus.isFictitious())
.flatMap(bus -> bus.getGenerators().stream())
.filter(g -> !g.isFictitious())
.toList();
List<LfGenerator> lfGeneratorsPrioritized = lfGenerators.stream()
.filter(g -> g.getReferencePriority() > 0)
.sorted(Comparator.comparingInt(LfReferencePriorityInjection::getReferencePriority))
.toList();
final int priority;
if (lfGeneratorsPrioritized.isEmpty()) {
priority = 0;
} else {
priority = lfGeneratorsPrioritized.get(0).getReferencePriority();
}
LOGGER.info("Network {}, will select reference generator among generators of priority {}", this, priority);
// if multiple generators of same priority, select based on highest maxP
referenceGenerator = lfGenerators.stream()
.filter(g -> g.getReferencePriority() == priority)
.min(Comparator.comparingDouble(LfGenerator::getMaxP).reversed().thenComparing(LfGenerator::getId)
).orElseThrow(() -> new IllegalStateException("No reference Generator"));
referenceBus = referenceGenerator.getBus();
referenceBus.setReference(true);
referenceGenerator.setReference(true);
LOGGER.info("Network {}, reference bus is {}, reference generator is {}", this, referenceBus, referenceGenerator);
}
}
}

Expand Down Expand Up @@ -274,6 +313,11 @@ public List<LfBus> getSlackBuses() {
return slackBuses;
}

public LfGenerator getReferenceGenerator() {
updateSlackBusesAndReferenceBus();
return referenceGenerator;
}

public List<LfShunt> getShunts() {
return shuntsByIndex;
}
Expand Down Expand Up @@ -331,7 +375,7 @@ public void updateState(LfNetworkStateUpdateParameters parameters) {
for (LfBus bus : busesById.values()) {
bus.updateState(parameters);
for (LfGenerator generator : bus.getGenerators()) {
generator.updateState();
generator.updateState(parameters);
}
bus.getShunt().ifPresent(shunt -> shunt.updateState(parameters));
bus.getControllerShunt().ifPresent(shunt -> shunt.updateState(parameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class LfNetworkParameters {

public static final boolean SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE = false;

public static final boolean USE_REFERENCE_PRIORITIES_DEFAULT_VALUE = false;

public static final boolean WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE = false;

private SlackBusSelector slackBusSelector = new FirstSlackBusSelector(SLACK_BUS_COUNTRY_FILTER_DEFAULT_VALUE);

private GraphConnectivityFactory<LfBus, LfBranch> connectivityFactory = new EvenShiloachGraphDecrementalConnectivityFactory<>();
Expand Down Expand Up @@ -128,6 +132,10 @@ public class LfNetworkParameters {

private boolean simulateAutomationSystems = SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE;

private boolean useReferencePriorities = USE_REFERENCE_PRIORITIES_DEFAULT_VALUE;

private boolean writeReferenceTerminals = WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE;

public LfNetworkParameters() {
}

Expand Down Expand Up @@ -484,6 +492,24 @@ public LfNetworkParameters setSimulateAutomationSystems(boolean simulateAutomati
return this;
}

public boolean isUseReferencePriorities() {
return useReferencePriorities;
}

public LfNetworkParameters setUseReferencePriorities(boolean useReferencePriorities) {
this.useReferencePriorities = useReferencePriorities;
return this;
}

public boolean isWriteReferenceTerminals() {
return writeReferenceTerminals;
}

public LfNetworkParameters setWriteReferenceTerminals(boolean writeReferenceTerminals) {
this.writeReferenceTerminals = writeReferenceTerminals;
return this;
}

@Override
public String toString() {
return "LfNetworkParameters(" +
Expand Down
Loading

0 comments on commit 858d9e7

Please sign in to comment.