Skip to content

Commit

Permalink
add getBusCount() to network and voltage level busbreakerview for sym…
Browse files Browse the repository at this point in the history
…metry

Signed-off-by: HARPER Jon <jon.harper87@gmail.com>
  • Loading branch information
jonenst committed Aug 6, 2023
1 parent c04c06f commit 754fb0d
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
11 changes: 11 additions & 0 deletions iidm/iidm-api/src/main/java/com/powsybl/iidm/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -410,6 +411,16 @@ interface BusBreakerView {
*/
Stream<Bus> getBusStream();

/**
* Get the bus count.
* <p>
* Depends on the working variant.
* @see VariantManager
*/
default int getBusCount() {
return Ints.checkedCast(getBusStream().count());
}

/**
* Get all switches
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -707,6 +708,17 @@ interface SwitchAdder extends IdentifiableAdder<Switch, SwitchAdder> {
*/
Stream<Bus> getBusStream();

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

/**
* Get a bus.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ public Stream<Bus> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public Stream<Bus> getBusStream() {
return getVoltageLevelStream().flatMap(vl -> vl.getBusBreakerView().getBusStream());
}

@Override
public int getBusCount() {
return getVoltageLevelStream().mapToInt(vl -> vl.getBusBreakerView().getBusCount()).sum();
}

@Override
public Iterable<Switch> getSwitches() {
return FluentIterable.from(getVoltageLevels())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ private Collection<CalculatedBus> getBuses() {
return id2bus.values();
}

private int getBusCount() {
return id2bus.size();
}

private CalculatedBus getBus(int node) {
return node2bus[node];
}
Expand Down Expand Up @@ -321,6 +325,11 @@ Collection<CalculatedBus> getBuses() {
return busCache.getBuses();
}

int getBusCount() {
updateCache();
return busCache.getBusCount();
}

CalculatedBus getBus(int node) {
updateCache();
return busCache.getBus(node);
Expand Down Expand Up @@ -985,6 +994,11 @@ public Stream<Bus> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand All @@ -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());
Expand Down

0 comments on commit 754fb0d

Please sign in to comment.