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

Generator reference priorities #899

Merged
merged 5 commits into from
Jan 22, 2024
Merged
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
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 @@ -112,6 +112,8 @@ public enum SlackDistributionFailureBehavior {

protected static final int INCREMENTAL_TRANSFORMER_RATIO_TAP_CONTROL_OUTER_LOOP_MAX_TAP_SHIFT_DEFAULT_VALUE = 3;

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 @@ -230,6 +232,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 @@ -293,7 +299,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 @@ -455,6 +463,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 @@ -1071,6 +1083,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 @@ -1138,7 +1168,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 @@ -1272,6 +1304,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 @@ -1336,6 +1372,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 @@ -1460,7 +1498,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 @@ -111,7 +112,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 @@ -152,6 +154,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 @@ -208,12 +214,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 @@ -227,7 +233,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 @@ -130,6 +130,8 @@ public class LfNetworkParameters {

private boolean simulateAutomationSystems = SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE;

private ReferenceBusSelector referenceBusSelector = ReferenceBusSelector.DEFAULT_SELECTOR;

public LfNetworkParameters() {
}

Expand All @@ -142,6 +144,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 Down Expand Up @@ -169,6 +172,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 @@ -496,6 +500,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 @@ -532,6 +545,7 @@ public String toString() {
", linePerUnitMode=" + linePerUnitMode +
", useLoadModel=" + useLoadModel +
", simulateAutomationSystems=" + simulateAutomationSystems +
", referenceBusSelector=" + referenceBusSelector.getClass().getSimpleName() +
')';
}
}
Loading