Skip to content

Commit

Permalink
Merge branch 'main' into change-to-compatibility-mode-for-graalvm
Browse files Browse the repository at this point in the history
Signed-off-by: EtienneLt <[email protected]>
  • Loading branch information
EtienneLt authored Oct 24, 2023
2 parents 17a1c53 + 00c8101 commit 2ccf833
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 5 deletions.
3 changes: 3 additions & 0 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ PYBIND11_MODULE(_pypowsybl, m) {
m.def("get_network_area_diagram_svg", &pypowsybl::getNetworkAreaDiagramSvg, "Get network area diagram SVG as a string",
py::arg("network"), py::arg("voltage_level_ids"), py::arg("depth"), py::arg("high_nominal_voltage_bound"), py::arg("low_nominal_voltage_bound"), py::arg("edge_name_displayed"));

m.def("get_network_area_diagram_displayed_voltage_levels", &pypowsybl::getNetworkAreaDiagramDisplayedVoltageLevels, "Get network area diagram displayed voltage level",
py::arg("network"), py::arg("voltage_level_ids"), py::arg("depth"));

m.def("create_security_analysis", &pypowsybl::createSecurityAnalysis, "Create a security analysis");

m.def("add_contingency", &pypowsybl::addContingency, "Add a contingency to a security analysis or sensitivity analysis",
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,13 @@ std::string getNetworkAreaDiagramSvg(const JavaHandle& network, const std::vecto
return toString(callJava<char*>(::getNetworkAreaDiagramSvg, network, voltageLevelIdPtr.get(), voltageLevelIds.size(), depth, highNominalVoltageBound, lowNominalVoltageBound, edgeNameDisplayed));
}

std::vector<std::string> getNetworkAreaDiagramDisplayedVoltageLevels(const JavaHandle& network, const std::vector<std::string>& voltageLevelIds, int depth) {
ToCharPtrPtr voltageLevelIdPtr(voltageLevelIds);
auto displayedVoltageLevelIdsArrayPtr = callJava<array*>(::getNetworkAreaDiagramDisplayedVoltageLevels, network, voltageLevelIdPtr.get(), voltageLevelIds.size(), depth);
ToStringVector displayedVoltageLevelIds(displayedVoltageLevelIdsArrayPtr);
return displayedVoltageLevelIds.get();
}

JavaHandle createSecurityAnalysis() {
return callJava<JavaHandle>(::createSecurityAnalysis);
}
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ void writeNetworkAreaDiagramSvg(const JavaHandle& network, const std::string& sv

std::string getNetworkAreaDiagramSvg(const JavaHandle& network, const std::vector<std::string>& voltageLevelIds, int depth, double highNominalVoltageBound, double lowNominalVoltageBound, bool edgeNameDisplayed);

std::vector<std::string> getNetworkAreaDiagramDisplayedVoltageLevels(const JavaHandle& network, const std::vector<std::string>& voltageLevelIds, int depth);

JavaHandle createSecurityAnalysis();

void addContingency(const JavaHandle& analysisContext, const std::string& contingencyId, const std::vector<std::string>& elementsIds);
Expand Down
1 change: 1 addition & 0 deletions docs/reference/network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Miscellaneous network functions
Network.write_single_line_diagram_svg
Network.get_network_area_diagram
Network.write_network_area_diagram_svg
Network.get_network_area_diagram_displayed_voltage_levels
Network.disconnect
Network.connect
Network.open_switch
Expand Down
7 changes: 7 additions & 0 deletions docs/user_guide/network_visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ Note that similarly to single-line diagrams, a loadflow can be run before writin
>>> network = pp.network.create_ieee9()
>>> result = pp.loadflow.run_ac(network)
>>> network.write_network_area_diagram_svg('ieee9.svg')
In order to get a list of the displayed voltage levels from an input voltage level (or an input list of voltage levels) and a depth:

.. code-block:: python
>>> network = pp.network.create_ieee300()
>>> list_vl = network.get_network_area_diagram_displayed_voltage_levels('VL1', 1)
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ static VoltageLevelFilter getNominalVoltageFilter(Network network, List<String>
return new VoltageLevelFilter(voltageLevels);
}

public static List<String> getDisplayedVoltageLevels(Network network, List<String> voltageLevelIds, int depth) {
return NetworkAreaDiagram.getDisplayedVoltageLevels(network, voltageLevelIds, depth);
}

private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels,
double highNominalVoltageBound, double lowNominalVoltageBound) {
if (depth >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,16 @@ public static CCharPointer getNetworkAreaDiagramSvg(IsolateThread thread, Object
});
}

@CEntryPoint(name = "getNetworkAreaDiagramDisplayedVoltageLevels")
public static PyPowsyblApiHeader.ArrayPointer<CCharPointerPointer> getNetworkAreaDiagramDisplayedVoltageLevels(IsolateThread thread, ObjectHandle networkHandle, CCharPointerPointer voltageLevelIdsPointer,
int voltageLevelIdCount, int depth, ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr, () -> {
Network network = ObjectHandles.getGlobal().get(networkHandle);
List<String> voltageLevelIds = toStringList(voltageLevelIdsPointer, voltageLevelIdCount);
return createCharPtrArray(NetworkAreaDiagramUtil.getDisplayedVoltageLevels(network, voltageLevelIds, depth));
});
}

@CEntryPoint(name = "getValidationLevel")
public static ValidationLevelType getValidationLevel(IsolateThread thread, ObjectHandle networkHandle, ExceptionHandlerPointer exceptionHandlerPtr) {
exceptionHandlerPtr.setMessage(WordFactory.nullPointer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import com.powsybl.commons.test.TestUtil;
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.*;
import com.powsybl.nad.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Objects;
import java.util.*;

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

Expand All @@ -31,4 +32,14 @@ void test() throws IOException {
assertEquals(TestUtil.normalizeLineSeparator(new String(ByteStreams.toByteArray(Objects.requireNonNull(NetworkAreaDiagramUtil.class.getResourceAsStream("/nad.svg"))), StandardCharsets.UTF_8)),
TestUtil.normalizeLineSeparator(svg));
}

@Test
void testGetVisibleVoltageLevels() {
Network network = EurostagTutorialExample1Factory.createWithTieLine();
List<String> ids = NetworkAreaDiagram.getDisplayedVoltageLevels(network, List.of("VLHV1"), 1);
assertEquals("VLGEN, VLHV1, VLHV2", String.join(", ", ids));

ids = NetworkAreaDiagram.getDisplayedVoltageLevels(network, List.of("VLHV1"), 2);
assertEquals("VLGEN, VLHV1, VLHV2, VLLOAD", String.join(", ", ids));
}
}
1 change: 1 addition & 0 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ def get_security_analysis_provider_parameters_names(provider: str) -> List[str]:
def get_sensitivity_analysis_provider_parameters_names(provider: str) -> List[str]: ...
def get_limit_violations(result: JavaHandle) -> SeriesArray: ...
def get_network_area_diagram_svg(network: JavaHandle, voltage_level_ids: Union[str, List[str]], depth: int, high_nominal_voltage_bound: float, low_nominal_voltage_bound: float, edge_name_displayed: bool) -> str: ...
def get_network_area_diagram_displayed_voltage_levels(network: JavaHandle, voltage_level_ids: Union[str, List[str]], depth: int) -> List[str]: ...
def get_network_elements_ids(network: JavaHandle, element_type: ElementType, nominal_voltages: List[float], countries: List[str], main_connected_component: bool, main_synchronous_component: bool, not_connected_to_same_bus_at_both_sides: bool) -> List[str]: ...
def get_network_export_formats() -> List[str]: ...
def get_network_import_formats() -> List[str]: ...
Expand Down
17 changes: 17 additions & 0 deletions pypowsybl/network/impl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ def get_network_area_diagram(self, voltage_level_ids: Union[str, List[str]] = No
high_nominal_voltage_bound, low_nominal_voltage_bound,
edge_name_displayed))

def get_network_area_diagram_displayed_voltage_levels(self, voltage_level_ids: Union[str, List[str]],
depth: int = 0) -> List[str]:
"""
Gathers the name of the displayed voltage levels of a network-area diagram in a list, according to
the input voltage level(s) and the depth of the diagram.
Args:
voltage_level_ids: the voltage level ID(s), center(s) of the diagram
depth: the diagram depth around the voltage level
Returns:
a list of the displayed voltage levels
"""
if isinstance(voltage_level_ids, str):
voltage_level_ids = [voltage_level_ids]
return _pp.get_network_area_diagram_displayed_voltage_levels(self._handle, voltage_level_ids, depth)

def get_elements_ids(self, element_type: ElementType, nominal_voltages: Set[float] = None,
countries: Set[str] = None,
main_connected_component: bool = True, main_synchronous_component: bool = True,
Expand Down
13 changes: 10 additions & 3 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,12 @@ def test_sld_nad():
n.write_network_area_diagram_svg(test_svg, high_nominal_voltage_bound=50, depth=10)


def test_nad_displayed_voltage_levels():
n = pp.network.create_ieee14()
list_vl = n.get_network_area_diagram_displayed_voltage_levels('VL1', 1)
assert ['VL1', 'VL2', 'VL5'] == list_vl


def test_current_limits():
network = pp.network.create_eurostag_tutorial_example1_network()
assert 9 == len(network.get_current_limits())
Expand Down Expand Up @@ -1245,22 +1251,23 @@ def test_network_merge():
assert 10 == len(merge.get_voltage_levels())
sub_networks = merge.get_sub_networks()
expected_sub_networks = pd.DataFrame(index=pd.Series(name='id',
data=['urn:uuid:d400c631-75a0-4c30-8aed-832b0d282e73', 'urn:uuid:77b55f87-fc1e-4046-9599-6c6b4f991a86']))
data=['urn:uuid:d400c631-75a0-4c30-8aed-832b0d282e73',
'urn:uuid:77b55f87-fc1e-4046-9599-6c6b4f991a86']))
pd.testing.assert_frame_equal(expected_sub_networks, sub_networks, check_dtype=False)
be_from_merge = merge.get_sub_network('urn:uuid:d400c631-75a0-4c30-8aed-832b0d282e73')
assert 6 == len(be_from_merge.get_voltage_levels())
nl_from_merge = merge.get_sub_network('urn:uuid:77b55f87-fc1e-4046-9599-6c6b4f991a86')
assert 4 == len(nl_from_merge.get_voltage_levels())
be_from_merge.detach()
assert 6 == len(be_from_merge.get_voltage_levels())
assert 4 == len(merge.get_voltage_levels()) # only remain NL in the merge
assert 4 == len(merge.get_voltage_levels()) # only remain NL in the merge
sub_networks = merge.get_sub_networks()
expected_sub_networks = pd.DataFrame(index=pd.Series(name='id',
data=['urn:uuid:77b55f87-fc1e-4046-9599-6c6b4f991a86']))
pd.testing.assert_frame_equal(expected_sub_networks, sub_networks, check_dtype=False)
nl_from_merge.detach()
assert 4 == len(nl_from_merge.get_voltage_levels())
assert 0 == len(merge.get_voltage_levels()) # merge is empty
assert 0 == len(merge.get_voltage_levels()) # merge is empty


def test_linear_shunt_compensator_sections():
Expand Down

0 comments on commit 2ccf833

Please sign in to comment.