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

Delete node count #1161

Merged
merged 11 commits into from
Mar 11, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ private int newNode(VoltageLevel vl) {
// If a previous value was stored, new value is computed with "sum" 1
// We want a zero-based index, so -1 + ...
int numNodes = voltageLevelNumNodes.merge(vl, 1, Integer::sum);
if (vl.getNodeBreakerView().getNodeCount() < numNodes) {
// +10 to avoid calling setNodeCount too many times
vl.getNodeBreakerView().setNodeCount(numNodes + 10);
}
return numNodes - 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,19 +358,26 @@ interface InternalConnection {
}

/**
* Get the number of node.
* Get the count of used nodes (nodes attached to an equipment, a switch or an internal connection).
*
* @deprecated Use {@link #getMaximumNodeIndex()} instead.
*/
int getNodeCount();
@Deprecated
default int getNodeCount() {
throw new UnsupportedOperationException();
}

/**
* Get the list of nodes.
* Get the highest index of used nodes (i.e. attached to an equipment, a switch or an internal connection) in the voltage level.
*/
int[] getNodes();
default int getMaximumNodeIndex() {
miovd marked this conversation as resolved.
Show resolved Hide resolved
throw new UnsupportedOperationException();
}

/**
* Set the number of node.
* Get the list of nodes.
*/
NodeBreakerView setNodeCount(int count);
int[] getNodes();

/**
* Get a builder to create a new switch.
Expand All @@ -397,6 +404,13 @@ interface InternalConnection {
*/
Stream<InternalConnection> getInternalConnectionStream();

/**
* Remove <b>all</b> the internal connections between node1 and node2 (not orientated) if they exist.
*/
default void removeInternalConnections(int node1, int node2) {
throw new UnsupportedOperationException();
}

/**
* Get a builder to create a new breaker.
*/
Expand Down Expand Up @@ -1035,5 +1049,4 @@ default void remove() {
* @param writer a writer
*/
void exportTopology(Writer writer) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void remove() {
for (TerminalExt terminal : terminals) {
VoltageLevelExt vl = terminal.getVoltageLevel();
vl.detach(terminal);
vl.clean();
}
network.getListeners().notifyRemoval(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ public void invalidateCache() {
@Override
public Iterable<Terminal> getTerminals() {
return FluentIterable.from(graph.getVerticesObj())
.transformAndConcat(ConfiguredBus::getTerminals)
.transform(Terminal.class::cast);
.transformAndConcat(ConfiguredBus::getTerminals)
.transform(Terminal.class::cast);
}

@Override
Expand All @@ -356,7 +356,7 @@ static PowsyblException createNotSupportedBusBreakerTopologyException() {
private final NodeBreakerViewExt nodeBreakerView = new NodeBreakerViewExt() {

@Override
public int getNodeCount() {
public int getMaximumNodeIndex() {
throw createNotSupportedBusBreakerTopologyException();
}

Expand All @@ -365,11 +365,6 @@ public int[] getNodes() {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public NodeBreakerView setNodeCount(int count) {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public int getNode1(String switchId) {
throw createNotSupportedBusBreakerTopologyException();
Expand Down Expand Up @@ -420,6 +415,11 @@ public Stream<InternalConnection> getInternalConnectionStream() {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public void removeInternalConnections(int node1, int node2) {
throw createNotSupportedBusBreakerTopologyException();
}

@Override
public SwitchAdder newBreaker() {
throw createNotSupportedBusBreakerTopologyException();
Expand Down Expand Up @@ -705,7 +705,7 @@ private void checkTerminal(TerminalExt terminal) {
if (!(terminal instanceof BusTerminal)) {
throw new ValidationException(terminal.getConnectable(),
"voltage level " + BusBreakerVoltageLevel.this.id + " has a bus/breaker topology"
+ ", a bus connection should be specified instead of a node connection");
+ ", a bus connection should be specified instead of a node connection");
}

// check connectable buses exist
Expand Down Expand Up @@ -756,11 +756,6 @@ public void detach(final TerminalExt terminal) {
terminal.setVoltageLevel(null);
}

@Override
public void clean() {
// nothing to do
}

@Override
public boolean connect(TerminalExt terminal) {
assert terminal instanceof BusTerminal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public Switch add() {
}
SwitchImpl aSwitch = new SwitchImpl(NodeBreakerVoltageLevel.this, id, getName(), kind, open, retained, fictitious);
getNetwork().getIndex().checkAndAdd(aSwitch);
graph.addVertexIfNotPresent(node1);
graph.addVertexIfNotPresent(node2);
int e = graph.addEdge(node1, node2, aSwitch);
switches.put(id, e);
invalidateCache();
Expand Down Expand Up @@ -204,7 +206,8 @@ public void add() {
if (node2 == null) {
throw new ValidationException(NodeBreakerVoltageLevel.this, "second connection node is not set");
}

graph.addVertexIfNotPresent(node1);
graph.addVertexIfNotPresent(node2);
graph.addEdge(node1, node2, null);
invalidateCache();
}
Expand Down Expand Up @@ -562,25 +565,23 @@ CalculatedBusTopology getCalculatedBusTopology() {

private final NodeBreakerViewExt nodeBreakerView = new NodeBreakerViewExt() {

/**
* @deprecated Use {@link #getMaximumNodeIndex()} instead.
*/
@Override
@Deprecated
public int getNodeCount() {
return graph.getVertexCount();
}

@Override
public int[] getNodes() {
return graph.getVertices();
public int getMaximumNodeIndex() {
return graph.getVertexCapacity() - 1;
}

@Override
public NodeBreakerView setNodeCount(int count) {
int oldCount = graph.getVertexCount();
if (count > oldCount) {
for (int i = oldCount; i < count; i++) {
graph.addVertex();
}
}
return this;
public int[] getNodes() {
return graph.getVertices();
}

@Override
Expand Down Expand Up @@ -647,6 +648,23 @@ public int getNode2() {
});
}

@Override
public void removeInternalConnections(int node1, int node2) {
int[] internalConnectionsToBeRemoved = Arrays.stream(graph.getEdges())
.filter(e -> graph.getEdgeObject(e) == null)
.filter(e -> (graph.getEdgeVertex1(e) == node1 && graph.getEdgeVertex2(e) == node2) ||
(graph.getEdgeVertex1(e) == node2 && graph.getEdgeVertex2(e) == node1))
.toArray();
if (internalConnectionsToBeRemoved.length == 0) {
throw new PowsyblException("Internal connection not found between " + node1 + " and " + node2);
}
for (int ic : internalConnectionsToBeRemoved) {
graph.removeEdge(ic);
}
clean();
invalidateCache();
}

@Override
public SwitchAdder newBreaker() {
return new SwitchAdderImpl(SwitchKind.BREAKER);
Expand Down Expand Up @@ -879,6 +897,7 @@ public void attach(TerminalExt terminal, boolean test) {
return;
}
int node = ((NodeTerminal) terminal).getNode();
graph.addVertexIfNotPresent(node);
if (graph.getVertexObject(node) != null) {
throw new ValidationException(terminal.getConnectable(),
"an equipment (" + graph.getVertexObject(node).getConnectable().getId()
Expand Down Expand Up @@ -906,10 +925,10 @@ public void detach(TerminalExt terminal) {

// remove the link terminal -> voltage level
terminal.setVoltageLevel(null);
clean();
}

@Override
public void clean() {
private void clean() {
GraphUtil.removeIsolatedVertices(graph);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ interface BusViewExt extends BusView {
*/
void detach(TerminalExt terminal);

void clean();

boolean connect(TerminalExt terminal);

boolean disconnect(TerminalExt terminal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/
package com.powsybl.iidm.network.impl;

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.network.test.NetworkTest1Factory;
import org.junit.Test;

Expand All @@ -15,47 +16,40 @@
public class NodeBreakerCleanTest {

@Test
public void cleanTopology() {
public void removeSwitchAndCleanTopology() {
Network network = NetworkTest1Factory.create();

VoltageLevelExt vl = (VoltageLevelExt) network.getVoltageLevel("voltageLevel1");
VoltageLevel vl = network.getVoltageLevel("voltageLevel1");
assertNotNull(vl);

VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
assertEquals(10, topo.getNodeCount());
vl.clean();

// Check useless nodes have been removed from the topology
assertEquals(6, topo.getNodeCount());
}

@Test
public void removeSwitch() {
Network network = NetworkTest1Factory.create();

VoltageLevelExt vl = (VoltageLevelExt) network.getVoltageLevel("voltageLevel1");
assertNotNull(vl);

VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
vl.clean();

// Check useless nodes have been removed from the topology
assertEquals(6, topo.getNodeCount());
assertEquals(6, topo.getMaximumNodeIndex());

assertNotNull(topo.getSwitch("load1Disconnector1"));
assertNotNull(topo.getSwitch("load1Breaker1"));

// Remove the switches linked to an intermediate node (3)
topo.removeSwitch("load1Disconnector1");
topo.removeSwitch("load1Breaker1");

// Check the 2 switches and the intermediate node have been removed from the topology.
// Check the 2 switches and that the node capacity and maximum node index have not changed
assertNull(topo.getSwitch("load1Disconnector1"));
assertNull(topo.getSwitch("load1Breaker1"));
assertEquals(5, topo.getNodeCount());
assertEquals(6, topo.getMaximumNodeIndex());

assertNull(network.getSwitch("load1Disconnector1"));
assertNull(network.getSwitch("load1Breaker1"));
assertNull(network.getIdentifiable("load1Disconnector1"));
assertNull(network.getIdentifiable("load1Breaker1"));

// Remove the switches linked to the maximum node (6)
topo.removeSwitch("generator1Disconnector1");
topo.removeSwitch("generator1Breaker1");

// Check the 2 switches and that the node capacity and maximum node index have changed
assertNull(topo.getSwitch("generator1Disconnector1"));
assertNull(topo.getSwitch("generator1Breaker1"));
assertEquals(5, topo.getMaximumNodeIndex());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,24 @@ public VoltageLevel.NodeBreakerView.SwitchAdder newDisconnector() {
// -------------------------------
// Simple delegated methods ------
// -------------------------------

/**
* @deprecated Use {@link #getMaximumNodeIndex()} instead.
*/
@Override
@Deprecated
public int getNodeCount() {
return getDelegate().getNodeCount();
}

@Override
public int[] getNodes() {
return getDelegate().getNodes();
public int getMaximumNodeIndex() {
return getDelegate().getMaximumNodeIndex();
}

@Override
public VoltageLevel.NodeBreakerView setNodeCount(final int count) {
getDelegate().setNodeCount(count);
return this;
public int[] getNodes() {
return getDelegate().getNodes();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@ public void nodeBreakerViewTests() {

final VoltageLevel.NodeBreakerView nbv = voltageLevelNB.getNodeBreakerView();
assertTrue(nbv instanceof VoltageLevelAdapter.NodeBreakerViewAdapter);
nbv.setNodeCount(2);
assertEquals(2, nbv.getNodeCount());
assertEquals(2, nbv.getNodes().length);

nbv.newInternalConnection()
.setNode1(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ private Network createNodeBreaker() {
.setNominalV(400f)
.setTopologyKind(TopologyKind.NODE_BREAKER)
.add();
vl1.getNodeBreakerView().setNodeCount(5);
Substation s2 = network.newSubstation()
.setId("S2")
.setCountry(Country.FR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ private Network createNetwork(boolean retained) {
.setNominalV(400.0)
.setTopologyKind(TopologyKind.NODE_BREAKER)
.add();
vl.getNodeBreakerView().setNodeCount(2);
vl.getNodeBreakerView().newBreaker()
.setId("SW1")
.setNode1(0)
Expand All @@ -56,7 +55,6 @@ public void testNullPointer() {

VoltageLevel vl = network.getVoltageLevel("VL");
vl.getNodeBreakerView()
.setNodeCount(3)
.newInternalConnection()
.setNode1(1)
.setNode2(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void test() {
.setLowVoltageLimit(0.0)
.setTopologyKind(TopologyKind.NODE_BREAKER)
.add();
vl1.getNodeBreakerView().setNodeCount(3);
vl1.getNodeBreakerView().newBusbarSection()
.setId("C")
.setNode(0)
Expand Down Expand Up @@ -65,7 +64,6 @@ public void test() {
.setLowVoltageLimit(0.0)
.setTopologyKind(TopologyKind.NODE_BREAKER)
.add();
vl2.getNodeBreakerView().setNodeCount(5);
vl2.getNodeBreakerView().newBusbarSection()
.setId("H")
.setNode(0)
Expand Down
Loading