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 29d5795
Show file tree
Hide file tree
Showing 30 changed files with 508 additions and 54 deletions.
45 changes: 42 additions & 3 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public enum SlackDistributionFailureBehavior {

protected static final List<String> OUTER_LOOP_NAMES_DEFAULT_VALUE = null;

public static final boolean WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE = false;

public static final String SLACK_BUS_SELECTION_MODE_PARAM_NAME = "slackBusSelectionMode";

public static final String SLACK_BUSES_IDS_PARAM_NAME = "slackBusesIds";
Expand Down Expand Up @@ -225,6 +227,10 @@ public enum SlackDistributionFailureBehavior {

public static final String NEWTON_KRYLOV_LINE_SEARCH_PARAM_NAME = "newtonKrylovLineSearch";

public static final String REFERENCE_BUS_SELECTION_MODE_PARAM_NAME = "referenceBusSelectionMode";

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 +293,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(REFERENCE_BUS_SELECTION_MODE_PARAM_NAME, ParameterType.STRING, "Reference bus selection mode", ReferenceBusSelector.DEFAULT_MODE.name(), getEnumPossibleValues(ReferenceBusSelectionMode.class)),
new Parameter(WRITE_REFERENCE_TERMINALS_PARAM_NAME, ParameterType.BOOLEAN, "Write Reference Terminals", WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE)
);

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

private boolean newtonKrylovLineSearch = NewtonKrylovParameters.LINE_SEARCH_DEFAULT_VALUE;

private ReferenceBusSelectionMode referenceBusSelectionMode = ReferenceBusSelector.DEFAULT_MODE;

private boolean writeReferenceTerminals = 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 +1066,24 @@ public OpenLoadFlowParameters setNewtonKrylovLineSearch(boolean newtonKrylovLine
return this;
}

public ReferenceBusSelectionMode getReferenceBusSelectionMode() {
return referenceBusSelectionMode;
}

public OpenLoadFlowParameters setReferenceBusSelectionMode(ReferenceBusSelectionMode referenceBusSelectionMode) {
this.referenceBusSelectionMode = referenceBusSelectionMode;
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 +1150,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))
.setReferenceBusSelectionMode(config.getEnumProperty(REFERENCE_BUS_SELECTION_MODE_PARAM_NAME, ReferenceBusSelectionMode.class, ReferenceBusSelector.DEFAULT_MODE))
.setWriteReferenceTerminals(config.getBooleanProperty(WRITE_REFERENCE_TERMINALS_PARAM_NAME, WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE)));
return parameters;
}

Expand Down Expand Up @@ -1252,6 +1284,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(REFERENCE_BUS_SELECTION_MODE_PARAM_NAME))
.ifPresent(prop -> this.setReferenceBusSelectionMode(ReferenceBusSelectionMode.valueOf(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 +1351,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(REFERENCE_BUS_SELECTION_MODE_PARAM_NAME, referenceBusSelectionMode);
map.put(WRITE_REFERENCE_TERMINALS_PARAM_NAME, writeReferenceTerminals);
return map;
}

Expand Down Expand Up @@ -1438,7 +1476,8 @@ static LfNetworkParameters getNetworkParameters(LoadFlowParameters parameters, O
.setMinNominalVoltageTargetVoltageCheck(parametersExt.getMinNominalVoltageTargetVoltageCheck())
.setLinePerUnitMode(parametersExt.getLinePerUnitMode())
.setUseLoadModel(parametersExt.isUseLoadModel())
.setSimulateAutomationSystems(parametersExt.isSimulateAutomationSystems());
.setSimulateAutomationSystems(parametersExt.isSimulateAutomationSystems())
.setReferenceBusSelector(ReferenceBusSelector.fromMode(parametersExt.getReferenceBusSelectionMode()));
}

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
34 changes: 28 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 ReferenceBusSelector referenceBusSelector;

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, ReferenceBusSelector referenceBusSelector, Reporter reporter) {
this.numCC = numCC;
this.numSC = numSC;
this.slackBusSelector = Objects.requireNonNull(slackBusSelector);
this.maxSlackBusCount = maxSlackBusCount;
this.connectivityFactory = Objects.requireNonNull(connectivityFactory);
this.referenceBusSelector = referenceBusSelector;
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, ReferenceBusSelector referenceBusSelector) {
this(numCC, numSC, slackBusSelector, maxSlackBusCount, connectivityFactory, referenceBusSelector, Reporter.NO_OP);
}

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

public void updateSlackBusesAndReferenceBus() {
if (slackBuses == null || referenceBus == null) {
if (slackBuses == null && referenceBus == null) {
SelectedSlackBus selectedSlackBus = slackBusSelector.select(busesByIndex, maxSlackBusCount);
slackBuses = selectedSlackBus.getBuses();
LOGGER.info("Network {}, slack buses are {} (method='{}')", this, slackBuses, selectedSlackBus.getSelectionMethod());
for (var slackBus : slackBuses) {
slackBus.setSlack(true);
}
referenceBus = slackBuses.get(0);
// reference bus must be selected after slack bus, because of ReferenceBusFirstSlackSelector implementation requiring slackBuses
SelectedReferenceBus selectedReferenceBus = referenceBusSelector.select(this);
referenceBus = selectedReferenceBus.getLfBus();
LOGGER.info("Network {}, reference bus is {} (method='{}')", this, referenceBus, selectedReferenceBus.getSelectionMethod());
referenceBus.setReference(true);
if (selectedReferenceBus instanceof SelectedGeneratorReferenceBus generatorReferenceBus) {
referenceGenerator = generatorReferenceBus.getLfGenerator();
LOGGER.info("Network {}, reference generator is {}", this, referenceGenerator.getId());
referenceGenerator.setReference(true);
}
}
}

Expand Down Expand Up @@ -274,6 +291,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 +353,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 @@ -128,6 +128,8 @@ public class LfNetworkParameters {

private boolean simulateAutomationSystems = SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE;

private ReferenceBusSelector referenceBusSelector = ReferenceBusSelector.DEFAULT_SELECTOR;

public LfNetworkParameters() {
}

Expand All @@ -140,6 +142,7 @@ public LfNetworkParameters(LfNetworkParameters other) {
this.twtSplitShuntAdmittance = other.twtSplitShuntAdmittance;
this.breakers = other.breakers;
this.plausibleActivePowerLimit = other.plausibleActivePowerLimit;
this.useActiveLimits = other.useActiveLimits;
this.computeMainConnectedComponentOnly = other.computeMainConnectedComponentOnly;
this.countriesToBalance = new HashSet<>(other.countriesToBalance);
this.distributedOnConformLoad = other.distributedOnConformLoad;
Expand All @@ -166,6 +169,7 @@ public LfNetworkParameters(LfNetworkParameters other) {
this.linePerUnitMode = other.linePerUnitMode;
this.useLoadModel = other.useLoadModel;
this.simulateAutomationSystems = other.simulateAutomationSystems;
this.referenceBusSelector = other.referenceBusSelector;
}

public SlackBusSelector getSlackBusSelector() {
Expand Down Expand Up @@ -484,6 +488,15 @@ public LfNetworkParameters setSimulateAutomationSystems(boolean simulateAutomati
return this;
}

public ReferenceBusSelector getReferenceBusSelector() {
return referenceBusSelector;
}

public LfNetworkParameters setReferenceBusSelector(ReferenceBusSelector referenceBusSelector) {
this.referenceBusSelector = referenceBusSelector;
return this;
}

@Override
public String toString() {
return "LfNetworkParameters(" +
Expand Down Expand Up @@ -519,6 +532,7 @@ public String toString() {
", linePerUnitMode=" + linePerUnitMode +
", useLoadModel=" + useLoadModel +
", simulateAutomationSystems=" + simulateAutomationSystems +
", referenceBusSelector=" + referenceBusSelector.getClass().getSimpleName() +
')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public class LfNetworkStateUpdateParameters {

private final ReactivePowerDispatchMode reactivePowerDispatchMode;

private final boolean writeReferenceTerminals;

public LfNetworkStateUpdateParameters(boolean reactiveLimits, boolean writeSlackBus, boolean phaseShifterRegulationOn,
boolean transformerVoltageControlOn, boolean loadPowerFactorConstant, boolean dc,
boolean breakers, ReactivePowerDispatchMode reactivePowerDispatchMode) {
boolean breakers, ReactivePowerDispatchMode reactivePowerDispatchMode,
boolean writeReferenceTerminals) {
this.reactiveLimits = reactiveLimits;
this.writeSlackBus = writeSlackBus;
this.phaseShifterRegulationOn = phaseShifterRegulationOn;
Expand All @@ -40,6 +43,7 @@ public LfNetworkStateUpdateParameters(boolean reactiveLimits, boolean writeSlack
this.dc = dc;
this.breakers = breakers;
this.reactivePowerDispatchMode = Objects.requireNonNull(reactivePowerDispatchMode);
this.writeReferenceTerminals = writeReferenceTerminals;
}

public boolean isReactiveLimits() {
Expand Down Expand Up @@ -70,6 +74,10 @@ public boolean isBreakers() {
return breakers;
}

public boolean isWriteReferenceTerminals() {
return writeReferenceTerminals;
}

public ReactivePowerDispatchMode getReactivePowerDispatchMode() {
return reactivePowerDispatchMode;
}
Expand Down
Loading

0 comments on commit 29d5795

Please sign in to comment.