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

Refactor voltage level topology implementation #3196

Merged
merged 3 commits into from
Nov 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void remove() {
for (TerminalExt terminal : terminals) {
terminal.removeAsRegulationPoint();
VoltageLevelExt vl = terminal.getVoltageLevel();
vl.detach(terminal);
vl.getTopologyModel().detach(terminal);
}

network.getListeners().notifyAfterRemoval(id);
Expand Down Expand Up @@ -181,10 +181,10 @@ protected void move(TerminalExt oldTerminal, TopologyPoint oldTopologyPoint, Str
private void attachTerminal(TerminalExt oldTerminal, TopologyPoint oldTopologyPoint, VoltageLevelExt voltageLevel, TerminalExt terminalExt) {
// first, attach new terminal to connectable and to voltage level of destination, to ensure that the new terminal is valid
terminalExt.setConnectable(this);
voltageLevel.attach(terminalExt, false);
voltageLevel.getTopologyModel().attach(terminalExt, false);

// then we can detach the old terminal, as we now know that the new terminal is valid
oldTerminal.getVoltageLevel().detach(oldTerminal);
oldTerminal.getVoltageLevel().getTopologyModel().detach(oldTerminal);

// replace the old terminal by the new terminal in the connectable
int iSide = terminals.indexOf(oldTerminal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public boolean connect(Predicate<Switch> isTypeSwitchToOperate) {
String variantId = getVariantManagerHolder().getVariantManager().getVariantId(variantIndex);
boolean connectedBefore = isConnected();
connectable.notifyUpdate("beginConnect", variantId, connectedBefore, null);
boolean connected = voltageLevel.connect(this, isTypeSwitchToOperate);
boolean connected = voltageLevel.getTopologyModel().connect(this, isTypeSwitchToOperate);
boolean connectedAfter = isConnected();
connectable.notifyUpdate("endConnect", variantId, null, connectedAfter);
return connected;
Expand All @@ -209,7 +209,7 @@ public boolean disconnect(Predicate<Switch> isSwitchOpenable) {
String variantId = getVariantManagerHolder().getVariantManager().getVariantId(variantIndex);
boolean disconnectedBefore = !isConnected();
connectable.notifyUpdate("beginDisconnect", variantId, disconnectedBefore, null);
boolean disconnected = voltageLevel.disconnect(this, isSwitchOpenable);
boolean disconnected = voltageLevel.getTopologyModel().disconnect(this, isSwitchOpenable);
boolean disconnectedAfter = !isConnected();
connectable.notifyUpdate("endDisconnect", variantId, null, disconnectedAfter);
return disconnected;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright (c) 2024, 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/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.impl;

import com.google.common.collect.FluentIterable;
import com.google.common.primitives.Ints;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.util.ShortIdDictionary;

import java.io.IOException;
import java.io.PrintStream;
import java.io.Writer;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Stream;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
abstract class AbstractTopologyModel implements TopologyModel {

public static final int DEFAULT_NODE_INDEX_LIMIT = 1000;

public static final int NODE_INDEX_LIMIT = loadNodeIndexLimit(PlatformConfig.defaultConfig());

protected final VoltageLevelExt voltageLevel;

protected AbstractTopologyModel(VoltageLevelExt voltageLevel) {
this.voltageLevel = Objects.requireNonNull(voltageLevel);
}

protected static int loadNodeIndexLimit(PlatformConfig platformConfig) {
return platformConfig
.getOptionalModuleConfig("iidm")
.map(moduleConfig -> moduleConfig.getIntProperty("node-index-limit", DEFAULT_NODE_INDEX_LIMIT))
.orElse(DEFAULT_NODE_INDEX_LIMIT);
}

protected NetworkImpl getNetwork() {
return voltageLevel.getNetwork();
}

protected static void addNextTerminals(TerminalExt otherTerminal, List<TerminalExt> nextTerminals) {
Objects.requireNonNull(otherTerminal);
Objects.requireNonNull(nextTerminals);
Connectable<?> otherConnectable = otherTerminal.getConnectable();
if (otherConnectable instanceof Branch<?> branch) {
if (branch.getTerminal1() == otherTerminal) {
nextTerminals.add((TerminalExt) branch.getTerminal2());
} else if (branch.getTerminal2() == otherTerminal) {
nextTerminals.add((TerminalExt) branch.getTerminal1());
} else {
throw new IllegalStateException();
}
} else if (otherConnectable instanceof ThreeWindingsTransformer ttc) {
if (ttc.getLeg1().getTerminal() == otherTerminal) {
nextTerminals.add((TerminalExt) ttc.getLeg2().getTerminal());
nextTerminals.add((TerminalExt) ttc.getLeg3().getTerminal());
} else if (ttc.getLeg2().getTerminal() == otherTerminal) {
nextTerminals.add((TerminalExt) ttc.getLeg1().getTerminal());
nextTerminals.add((TerminalExt) ttc.getLeg3().getTerminal());
} else if (ttc.getLeg3().getTerminal() == otherTerminal) {
nextTerminals.add((TerminalExt) ttc.getLeg1().getTerminal());
nextTerminals.add((TerminalExt) ttc.getLeg2().getTerminal());
} else {
throw new IllegalStateException();
}
}
}

public void invalidateCache() {
invalidateCache(false);
}

public abstract Iterable<Terminal> getTerminals();

public abstract Stream<Terminal> getTerminalStream();

public <T extends Connectable> Iterable<T> getConnectables(Class<T> clazz) {
Iterable<Terminal> terminals = getTerminals();
return FluentIterable.from(terminals)
.transform(Terminal::getConnectable)
.filter(clazz)
.toSet();
}

public <T extends Connectable> Stream<T> getConnectableStream(Class<T> clazz) {
return getTerminalStream()
.map(Terminal::getConnectable)
.filter(clazz::isInstance)
.map(clazz::cast)
.distinct();
}

public <T extends Connectable> int getConnectableCount(Class<T> clazz) {
return Ints.checkedCast(getConnectableStream(clazz).count());
}

public Iterable<Connectable> getConnectables() {
return FluentIterable.from(getTerminals())
.transform(Terminal::getConnectable)
.toSet();
}

public Stream<Connectable> getConnectableStream() {
return getTerminalStream()
.map(Terminal::getConnectable)
.distinct();
}

public abstract VoltageLevelExt.NodeBreakerViewExt getNodeBreakerView();

public abstract VoltageLevelExt.BusBreakerViewExt getBusBreakerView();

public abstract VoltageLevelExt.BusViewExt getBusView();

public abstract Iterable<Switch> getSwitches();

public abstract int getSwitchCount();

public abstract TopologyKind getTopologyKind();

public abstract void extendVariantArraySize(int initVariantArraySize, int number, int sourceIndex);

public abstract void reduceVariantArraySize(int number);

public abstract void deleteVariantArrayElement(int index);

public abstract void allocateVariantArrayElement(int[] indexes, int sourceIndex);

protected abstract void removeTopology();

public abstract void printTopology();

public abstract void printTopology(PrintStream out, ShortIdDictionary dict);

public abstract void exportTopology(Path file) throws IOException;

public abstract void exportTopology(Writer writer);

public abstract void exportTopology(Writer writer, Random random);
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public BatteryImpl add() {
BatteryImpl battery = new BatteryImpl(getNetworkRef(), id, getName(), isFictitious(), targetP, targetQ, minP, maxP);

battery.addTerminal(terminal);
voltageLevel.attach(terminal, false);
voltageLevel.getTopologyModel().attach(terminal, false);
network.getIndex().checkAndAdd(battery);
network.getListeners().notifyCreation(battery);
return battery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
class BusAdderImpl extends AbstractIdentifiableAdder<BusAdderImpl> implements BusAdder {

private final BusBreakerVoltageLevel voltageLevel;
private final VoltageLevelExt voltageLevel;

BusAdderImpl(BusBreakerVoltageLevel voltageLevel) {
BusAdderImpl(VoltageLevelExt voltageLevel) {
this.voltageLevel = voltageLevel;
}

Expand All @@ -35,7 +35,7 @@ protected String getTypeDescription() {
public ConfiguredBus add() {
String id = checkAndGetUniqueId();
ConfiguredBusImpl bus = new ConfiguredBusImpl(id, getName(), isFictitious(), voltageLevel);
voltageLevel.addBus(bus);
((BusBreakerTopologyModel) voltageLevel.getTopologyModel()).addBus(bus);
getNetwork().getListeners().notifyCreation(bus);
return bus;
}
Expand Down
Loading