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 Nov 16, 2023
1 parent 41b3fa2 commit 5e10c67
Show file tree
Hide file tree
Showing 20 changed files with 334 additions and 42 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 @@ -220,6 +220,10 @@ public enum SlackDistributionFailureBehavior {

public static final String SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME = "simulateAutomationSystems";

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 @@ -280,7 +284,9 @@ private static <E extends Enum<E>> List<Object> getEnumPossibleValues(Class<E> e
new Parameter(LINE_PER_UNIT_MODE_PARAM_NAME, ParameterType.STRING, "Line per unit mode", LinePerUnitMode.IMPEDANCE.name(), getEnumPossibleValues(LinePerUnitMode.class)),
new Parameter(USE_LOAD_MODEL_PARAM_NAME, ParameterType.BOOLEAN, "Use load model (with voltage dependency) for simulation", LfNetworkParameters.USE_LOAD_MODE_DEFAULT_VALUE),
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(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME, ParameterType.BOOLEAN, "Automation systems simulation", LfNetworkParameters.SIMULATE_AUTOMATION_SYSTEMS_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 @@ -434,6 +440,10 @@ public enum ReactiveRangeCheckMode {

private boolean simulateAutomationSystems = LfNetworkParameters.SIMULATE_AUTOMATION_SYSTEMS_DEFAULT_VALUE;

private boolean useReferencePriorities = LfNetworkParameters.USE_REFERENCE_PRIORITIES_DEFAULT_VALUE;

private boolean writeReferenceTerminals = LfNetworkParameters.WRITE_REFERENCE_TERMINALS_DEFAULT_VALUE;

@Override
public String getName() {
return "open-load-flow-parameters";
Expand Down Expand Up @@ -985,6 +995,24 @@ public OpenLoadFlowParameters setSimulateAutomationSystems(boolean simulateAutom
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 @@ -1049,7 +1077,9 @@ public static OpenLoadFlowParameters load(PlatformConfig platformConfig) {
.setLinePerUnitMode(config.getEnumProperty(LINE_PER_UNIT_MODE_PARAM_NAME, LinePerUnitMode.class, LfNetworkParameters.LINE_PER_UNIT_MODE_DEFAULT_VALUE))
.setUseLoadModel(config.getBooleanProperty(USE_LOAD_MODEL_PARAM_NAME, LfNetworkParameters.USE_LOAD_MODE_DEFAULT_VALUE))
.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)));
.setSimulateAutomationSystems(config.getBooleanProperty(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME, LfNetworkParameters.SIMULATE_AUTOMATION_SYSTEMS_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 @@ -1177,6 +1207,10 @@ public OpenLoadFlowParameters update(Map<String, String> properties) {
.ifPresent(prop -> this.setDcApproximationType(DcApproximationType.valueOf(prop)));
Optional.ofNullable(properties.get(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME))
.ifPresent(prop -> this.setSimulateAutomationSystems(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 @@ -1238,6 +1272,8 @@ public Map<String, Object> toMap() {
map.put(USE_LOAD_MODEL_PARAM_NAME, useLoadModel);
map.put(DC_APPROXIMATION_TYPE_PARAM_NAME, dcApproximationType);
map.put(SIMULATE_AUTOMATION_SYSTEMS_PARAM_NAME, simulateAutomationSystems);
map.put(USE_REFERENCE_PRIORITIES_PARAM_NAME, useReferencePriorities);
map.put(WRITE_REFERENCE_TERMINALS_PARAM_NAME, writeReferenceTerminals);
return map;
}

Expand Down Expand Up @@ -1361,7 +1397,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
17 changes: 12 additions & 5 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 @@ -109,7 +110,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,14 +153,18 @@ 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());
for (AcLoadFlowResult result : results) {
updateAcState(network, parameters, parametersExt, result, acParameters, atLeastOneComponentHasToBeUpdated);

// FIXME a null slack bus ID should be allowed
String slackBusId = result.getNetwork().isValid() ? result.getNetwork().getSlackBus().getId() : "";
String slackBusId = result.getNetwork().isValid() ? result.getNetwork().getSlackBuses().get(0).getId() : "";
componentResults.add(new LoadFlowResultImpl.ComponentResultImpl(result.getNetwork().getNumCC(),
result.getNetwork().getNumSC(),
result.toComponentResultStatus(),
Expand Down Expand Up @@ -189,12 +195,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())).collect(Collectors.toList());
List<LoadFlowResult.ComponentResult> componentsResult = results.stream().map(r -> processResult(network, r, parameters, parametersExt, dcParameters.getNetworkParameters().isBreakers())).collect(Collectors.toList());
boolean ok = results.stream().anyMatch(DcLoadFlowResult::isSucceeded);
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.isSucceeded() && parameters.isWriteSlackBus()) {
SlackTerminal.reset(network);
}
Expand All @@ -207,7 +213,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 @@ -11,7 +11,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 @@ -93,7 +93,7 @@ default double getParticipationFactor() {

void setCalculatedQ(double calculatedQ);

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

LfBus getControlledBus();

Expand Down
67 changes: 61 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 @@ -52,6 +54,10 @@ public class LfNetwork extends AbstractPropertyBag implements PropertyBag {

private List<LfBus> slackBuses;

private LfBus referenceBus;

private LfGenerator referenceGenerator;

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

private final Map<String, LfBranch> branchesById = new HashMap<>();
Expand Down Expand Up @@ -125,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 All @@ -160,9 +167,16 @@ private void invalidateSlack() {
for (var slackBus : slackBuses) {
slackBus.setSlack(false);
}
slackBuses.get(0).setReference(false);
}
if (referenceBus != null) {
referenceBus.setReference(false);
}
if (referenceGenerator != null) {
referenceGenerator.setReference(false);
}
slackBuses = null;
referenceBus = null;
referenceGenerator = null;
}

public void updateSlackBuses() {
Expand All @@ -173,7 +187,38 @@ public void updateSlackBuses() {
for (var slackBus : slackBuses) {
slackBus.setSlack(true);
}
slackBuses.get(0).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 @@ -255,6 +300,16 @@ public List<LfBus> getSlackBuses() {
return slackBuses;
}

public LfBus getReferenceBus() {
updateSlackBuses();
return referenceBus;
}

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

public List<LfShunt> getShunts() {
return shuntsByIndex;
}
Expand Down Expand Up @@ -308,7 +363,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 @@ -55,6 +55,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 @@ -127,6 +131,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 @@ -483,6 +491,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 5e10c67

Please sign in to comment.