From 754fb0dfa103d1d7091dc0541c4f9a1fb471db50 Mon Sep 17 00:00:00 2001 From: HARPER Jon Date: Wed, 2 Aug 2023 17:26:11 +0200 Subject: [PATCH] add getBusCount() to network and voltage level busbreakerview for symmetry Signed-off-by: HARPER Jon --- .../com/powsybl/iidm/network/Network.java | 11 ++++++++ .../powsybl/iidm/network/VoltageLevel.java | 12 +++++++++ .../com/powsybl/iidm/network/NetworkTest.java | 26 +++++++++++++++++++ .../iidm/network/VoltageLevelTest.java | 26 +++++++++++++++++++ .../network/impl/BusBreakerVoltageLevel.java | 5 ++++ .../iidm/network/impl/NetworkImpl.java | 5 ++++ .../network/impl/NodeBreakerVoltageLevel.java | 14 ++++++++++ .../iidm/network/tck/AbstractNetworkTest.java | 4 +++ 8 files changed, 103 insertions(+) create mode 100644 iidm/iidm-api/src/test/java/com/powsybl/iidm/network/NetworkTest.java create mode 100644 iidm/iidm-api/src/test/java/com/powsybl/iidm/network/VoltageLevelTest.java diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Network.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Network.java index 901520ed455..c074dae31c5 100644 --- a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Network.java +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Network.java @@ -6,6 +6,7 @@ */ package com.powsybl.iidm.network; +import com.google.common.primitives.Ints; import com.powsybl.commons.PowsyblException; import com.powsybl.commons.datasource.*; import com.powsybl.commons.reporter.Reporter; @@ -410,6 +411,16 @@ interface BusBreakerView { */ Stream getBusStream(); + /** + * Get the bus count. + *

+ * Depends on the working variant. + * @see VariantManager + */ + default int getBusCount() { + return Ints.checkedCast(getBusStream().count()); + } + /** * Get all switches */ diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/VoltageLevel.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/VoltageLevel.java index 9f84cb39ddf..a8c9ff67edf 100644 --- a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/VoltageLevel.java +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/VoltageLevel.java @@ -6,6 +6,7 @@ */ package com.powsybl.iidm.network; +import com.google.common.primitives.Ints; import com.powsybl.commons.PowsyblException; import com.powsybl.iidm.network.util.ShortIdDictionary; import com.powsybl.math.graph.TraverseResult; @@ -707,6 +708,17 @@ interface SwitchAdder extends IdentifiableAdder { */ Stream getBusStream(); + /** + * Get the bus count. + *

+ * Depends on the working variant if topology kind is NODE_BREAKER. + * + * @see VariantManager + */ + default int getBusCount() { + return Ints.checkedCast(getBusStream().count()); + } + /** * Get a bus. *

diff --git a/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/NetworkTest.java b/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/NetworkTest.java new file mode 100644 index 00000000000..8f5026a1f0b --- /dev/null +++ b/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/NetworkTest.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023, 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/. + */ +package com.powsybl.iidm.network; + +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class NetworkTest { + + @Test + public void testBusBreakerViewGetBusCount() { + Bus bus1 = mock(Bus.class); + Bus bus2 = mock(Bus.class); + Network.BusBreakerView bbv = mock(Network.BusBreakerView.class); + when(bbv.getBusStream()).thenReturn(Stream.of(bus1, bus2)); + when(bbv.getBusCount()).thenCallRealMethod(); + assertEquals(2, bbv.getBusCount()); + } +} diff --git a/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/VoltageLevelTest.java b/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/VoltageLevelTest.java new file mode 100644 index 00000000000..e2e06067b80 --- /dev/null +++ b/iidm/iidm-api/src/test/java/com/powsybl/iidm/network/VoltageLevelTest.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023, 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/. + */ +package com.powsybl.iidm.network; + +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class VoltageLevelTest { + + @Test + public void testBusBreakerViewGetBusCount() { + Bus bus1 = mock(Bus.class); + Bus bus2 = mock(Bus.class); + VoltageLevel.BusBreakerView bbv = mock(VoltageLevel.BusBreakerView.class); + when(bbv.getBusStream()).thenReturn(Stream.of(bus1, bus2)); + when(bbv.getBusCount()).thenCallRealMethod(); + assertEquals(2, bbv.getBusCount()); + } +} diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java index 4e4e430a55c..c61cca45c63 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/BusBreakerVoltageLevel.java @@ -598,6 +598,11 @@ public Stream getBusStream() { return graph.getVertexObjectStream().map(Function.identity()); } + @Override + public int getBusCount() { + return graph.getVertexCount(); + } + @Override public ConfiguredBus getBus(String id) { return BusBreakerVoltageLevel.this.getBus(id, false); diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java index 32495906bba..4c2ae281f38 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NetworkImpl.java @@ -66,6 +66,11 @@ public Stream getBusStream() { return getVoltageLevelStream().flatMap(vl -> vl.getBusBreakerView().getBusStream()); } + @Override + public int getBusCount() { + return getVoltageLevelStream().mapToInt(vl -> vl.getBusBreakerView().getBusCount()).sum(); + } + @Override public Iterable getSwitches() { return FluentIterable.from(getVoltageLevels()) diff --git a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java index 4c4d047997c..c30e455babc 100644 --- a/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java +++ b/iidm/iidm-impl/src/main/java/com/powsybl/iidm/network/impl/NodeBreakerVoltageLevel.java @@ -229,6 +229,10 @@ private Collection getBuses() { return id2bus.values(); } + private int getBusCount() { + return id2bus.size(); + } + private CalculatedBus getBus(int node) { return node2bus[node]; } @@ -321,6 +325,11 @@ Collection getBuses() { return busCache.getBuses(); } + int getBusCount() { + updateCache(); + return busCache.getBusCount(); + } + CalculatedBus getBus(int node) { updateCache(); return busCache.getBus(node); @@ -985,6 +994,11 @@ public Stream getBusStream() { return variants.get().calculatedBusBreakerTopology.getBuses().stream().map(Function.identity()); } + @Override + public int getBusCount() { + return variants.get().calculatedBusBreakerTopology.getBusCount(); + } + @Override public CalculatedBus getBus(String id) { return variants.get().calculatedBusBreakerTopology.getBus(id, false); diff --git a/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractNetworkTest.java b/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractNetworkTest.java index 5bd01626418..312bd782297 100644 --- a/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractNetworkTest.java +++ b/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractNetworkTest.java @@ -58,6 +58,7 @@ public void testNetwork1() { assertEquals(1, Iterables.size(network.getSubstations())); assertEquals(1, Iterables.size(network.getSubstations(Country.FR, "TSO1", REGION1))); assertEquals(1, network.getSubstationCount()); + assertEquals(2, network.getBusBreakerView().getBusCount()); Substation substation1 = network.getSubstation(SUBSTATION12); assertNotNull(substation1); @@ -156,6 +157,7 @@ public void testNetwork1() { assertEquals(300.0, rcc1.getMinQ(500), 0.0); assertEquals(2, Iterables.size(voltageLevel1.getBusBreakerView().getBuses())); + assertEquals(2, voltageLevel1.getBusBreakerView().getBusCount()); Bus busCalc1 = voltageLevel1BusbarSection1.getTerminal().getBusBreakerView().getBus(); Bus busCalc2 = voltageLevel1BusbarSection2.getTerminal().getBusBreakerView().getBus(); assertSame(busCalc1, load1.getTerminal().getBusBreakerView().getBus()); @@ -268,6 +270,7 @@ public void testNetworkWithBattery() { assertEquals(2, network.getVoltageLevelCount()); assertEquals(2, Iterables.size(network.getBatteries())); assertEquals(2, network.getBatteryCount()); + assertEquals(2, network.getBusBreakerView().getBusCount()); // Substation A Substation substation1 = network.getSubstation("P1"); @@ -284,6 +287,7 @@ public void testNetworkWithBattery() { assertEquals(400.0, voltageLevel1.getNominalV(), 0.0); assertSame(substation1, voltageLevel1.getSubstation().orElse(null)); assertSame(TopologyKind.BUS_BREAKER, voltageLevel1.getTopologyKind()); + assertEquals(1, voltageLevel1.getBusBreakerView().getBusCount()); Bus bus1 = voltageLevel1.getBusBreakerView().getBus("NGEN"); assertEquals(3, bus1.getConnectedTerminalCount());