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

Import pypowsybl nominal voltage filter into powsybl-diagram #566

Merged
merged 17 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -35,6 +35,8 @@ private Set<VoltageLevel> getVoltageLevels() {
return voltageLevels;
}

private static final String UNKNOWN_VOLTAGE_LEVEL = "Unknown voltage level id '";
flo-dup marked this conversation as resolved.
Show resolved Hide resolved

@Override
public boolean test(VoltageLevel voltageLevel) {
return voltageLevels.contains(voltageLevel);
Expand All @@ -47,7 +49,7 @@ public static VoltageLevelFilter createVoltageLevelDepthFilter(Network network,
Set<VoltageLevel> voltageLevels = new HashSet<>();
VoltageLevel vl = network.getVoltageLevel(voltageLevelId);
if (vl == null) {
throw new PowsyblException("Unknown voltage level id '" + voltageLevelId + "'");
throw new PowsyblException(UNKNOWN_VOLTAGE_LEVEL + voltageLevelId + "'");
}

Set<VoltageLevel> startingSet = new HashSet<>();
Expand All @@ -63,7 +65,7 @@ public static VoltageLevelFilter createVoltageLevelsDepthFilter(Network network,
for (String voltageLevelId : voltageLevelIds) {
VoltageLevel vl = network.getVoltageLevel(voltageLevelId);
if (vl == null) {
throw new PowsyblException("Unknown voltage level id '" + voltageLevelId + "'");
throw new PowsyblException(UNKNOWN_VOLTAGE_LEVEL + voltageLevelId + "'");
}
startingSet.add(vl);
}
Expand Down Expand Up @@ -99,6 +101,64 @@ private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth,
traverseVoltageLevels(nextDepthVoltageLevels, depth - 1, visitedVoltageLevels);
}

private static void traverseVoltageLevelsWithVoltageFilter(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels,
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
double highNominalVoltageBound, double lowNominalVoltageBound) {
if (depth >= 0) {
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
Set<VoltageLevel> nextDepthVoltageLevels = new HashSet<>();
for (VoltageLevel vl : voltageLevelsDepth) {
if (!visitedVoltageLevels.contains(vl)) {
if (highNominalVoltageBound > 0 && lowNominalVoltageBound > 0) {
if (vl.getNominalV() >= lowNominalVoltageBound
&& vl.getNominalV() <= highNominalVoltageBound) {
traverseVoltageLevel(visitedVoltageLevels, nextDepthVoltageLevels, vl);
}
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
} else if (highNominalVoltageBound > 0) {
if (vl.getNominalV() <= highNominalVoltageBound) {
traverseVoltageLevel(visitedVoltageLevels, nextDepthVoltageLevels, vl);
}
} else if (lowNominalVoltageBound > 0) {
if (vl.getNominalV() >= lowNominalVoltageBound) {
traverseVoltageLevel(visitedVoltageLevels, nextDepthVoltageLevels, vl);
}
} else {
traverseVoltageLevel(visitedVoltageLevels, nextDepthVoltageLevels, vl);
}
}
}
traverseVoltageLevelsWithVoltageFilter(nextDepthVoltageLevels, depth - 1, visitedVoltageLevels, highNominalVoltageBound, lowNominalVoltageBound);
}
}

private static void traverseVoltageLevel(Set<VoltageLevel> visitedVoltageLevels, Set<VoltageLevel> nextDepthVoltageLevels, VoltageLevel vl) {
visitedVoltageLevels.add(vl);
vl.visitEquipments(new VlVisitor(nextDepthVoltageLevels, visitedVoltageLevels));
}

public static VoltageLevelFilter createNominalVoltageFilter(Network network, List<String> voltageLevelIds,
double highNominalVoltageBound,
double lowNominalVoltageBound, int depth) {
Objects.requireNonNull(network);
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
Objects.requireNonNull(voltageLevelIds);
Set<VoltageLevel> startingSet = new HashSet<>();

for (String voltageLevelId : voltageLevelIds) {
VoltageLevel vl = network.getVoltageLevel(voltageLevelId);
if (vl == null) {
throw new PowsyblException(UNKNOWN_VOLTAGE_LEVEL + voltageLevelId + "'");
}
if (lowNominalVoltageBound > 0 && vl.getNominalV() < lowNominalVoltageBound ||
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
highNominalVoltageBound > 0 && vl.getNominalV() > highNominalVoltageBound) {
throw new PowsyblException("vl '" + voltageLevelId +
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
"' has his nominal voltage out of the indicated thresholds");
}
startingSet.add(vl);
}

Set<VoltageLevel> voltageLevels = new HashSet<>();
VoltageLevelFilter.traverseVoltageLevelsWithVoltageFilter(startingSet, depth, voltageLevels, highNominalVoltageBound, lowNominalVoltageBound);
return new VoltageLevelFilter(voltageLevels);
}

private static class VlVisitor extends DefaultTopologyVisitor {
private final Set<VoltageLevel> nextDepthVoltageLevels;
private final Set<VoltageLevel> visitedVoltageLevels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.commons.PowsyblException;
import com.powsybl.diagram.test.Networks;
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.*;
import com.powsybl.nad.build.iidm.VoltageLevelFilter;
Expand All @@ -26,7 +28,7 @@
import java.nio.file.Path;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Thomas Adam {@literal <tadam at silicom.fr>}
Expand Down Expand Up @@ -75,4 +77,52 @@ void testGetVisibleVoltageLevels() {
ids = NetworkAreaDiagram.getDisplayedVoltageLevels(network, List.of("VLHV1"), 2);
assertEquals("VLGEN, VLHV1, VLHV2, VLLOAD", String.join(", ", ids));
}

@Test
void testVoltageFilteredDiagramTwoBounds() {
Network network = IeeeCdfNetworkFactory.create14();
Path svgFileVoltageFilter = fileSystem.getPath("nad-test-voltage-filter.svg");
NetworkAreaDiagram.draw(network, svgFileVoltageFilter, new NadParameters(), VoltageLevelFilter.createNominalVoltageFilter(network, List.of("VL4"), 180, 120, 2));
assertEquals(toString("/IEEE_14_bus_voltage_filter1.svg"), getContentFile(svgFileVoltageFilter));
}

@Test
void testVoltageFilteredDiagramLowBound() {
Network network = IeeeCdfNetworkFactory.create14();
Path svgFileVoltageFilter = fileSystem.getPath("nad-test-voltage-filter.svg");
NetworkAreaDiagram.draw(network, svgFileVoltageFilter, new NadParameters(), VoltageLevelFilter.createNominalVoltageFilter(network, List.of("VL4"), -1, 120, 2));
assertEquals(toString("/IEEE_14_bus_voltage_filter1.svg"), getContentFile(svgFileVoltageFilter));
}

@Test
void testVoltageFilteredDiagramHighBound() {
Network network = IeeeCdfNetworkFactory.create14();
Path svgFileVoltageFilter = fileSystem.getPath("nad-test-voltage-filter.svg");
NetworkAreaDiagram.draw(network, svgFileVoltageFilter, new NadParameters(), VoltageLevelFilter.createNominalVoltageFilter(network, List.of("VL4"), 180, -1, 2));
assertEquals(toString("/IEEE_14_bus_voltage_filter2.svg"), getContentFile(svgFileVoltageFilter));
}

@Test
void testVoltageFilteredDiagramNoBound() {
Network network = IeeeCdfNetworkFactory.create14();
Path svgFileVoltageFilter = fileSystem.getPath("nad-test-voltage-filter.svg");
NetworkAreaDiagram.draw(network, svgFileVoltageFilter, new NadParameters(), VoltageLevelFilter.createNominalVoltageFilter(network, List.of("VL4"), -1, -1, 2));
assertEquals(toString("/IEEE_14_bus_voltage_filter2.svg"), getContentFile(svgFileVoltageFilter));
}

@Test
void testVoltageFilteredDiagramOutOfBound() {
Network network = IeeeCdfNetworkFactory.create14();
List<String> voltageLevelList = List.of("VL4");
PowsyblException e = assertThrows(PowsyblException.class, () -> VoltageLevelFilter.createNominalVoltageFilter(network, voltageLevelList, 90, -1, 2));
assertTrue(e.getMessage().contains("vl 'VL4' has his nominal voltage out of the indicated thresholds"));
}

@Test
void testVoltageFilteredDiagramUnexistingVoltageLevel() {
Network network = IeeeCdfNetworkFactory.create14();
List<String> voltageLevelList = List.of("VL456");
PowsyblException e = assertThrows(PowsyblException.class, () -> VoltageLevelFilter.createNominalVoltageFilter(network, voltageLevelList, 90, -1, 2));
assertTrue(e.getMessage().contains("Unknown voltage level id 'VL456'"));
}
}
Loading
Loading