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

[WIP] Wrap moveConnectable from terminal bus breaker view. #654

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,4 +762,6 @@ PYBIND11_MODULE(_pypowsybl, m) {
m.def("get_network_modification_metadata_with_element_type", &pypowsybl::getModificationMetadataWithElementType, "Get network modification metadata with element type", py::arg("network_modification_type"), py::arg("element_type"));

m.def("create_network_modification", ::createNetworkModification, "Create and apply network modification", py::arg("network"), py::arg("dataframe"), py::arg("network_modification_type"), py::arg("raise_exception"), py::arg("reporter"));

m.def("move_connectable", &pypowsybl::moveConnectable, "Move connectable", py::arg("network"), py::arg("equipment_id"), py::arg("bus_origin_id"), py::arg("bus_destination_id"));
}
4 changes: 4 additions & 0 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,4 +1311,8 @@ void createNetworkModification(pypowsybl::JavaHandle network, dataframe_array* d
pypowsybl::callJava(::createNetworkModification, network, dataframes, networkModificationType, throwException, (reporter == nullptr) ? nullptr : *reporter);
}

void moveConnectable(pypowsybl::JavaHandle network, std::string equipmentId, std::string idBusOrigin, std::string idBusDestination) {
pypowsybl::callJava(::moveConnectable, network, (char*) equipmentId.c_str(), (char*) idBusOrigin.c_str(), (char*) idBusDestination.c_str());
}

}
2 changes: 2 additions & 0 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ SeriesArray* getConnectablesOrderPositions(const JavaHandle& network, const std:

std::vector<int> getUnusedConnectableOrderPositions(pypowsybl::JavaHandle network, std::string busbarSectionId, std::string beforeOrAfter);

void moveConnectable(pypowsybl::JavaHandle network, std::string equipmentId, std::string idBusOrigin, std::string idBusDestination);

void removeAliases(pypowsybl::JavaHandle network, dataframe* dataframe);

void closePypowsybl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,4 +978,17 @@ public static DataframesMetadataPointer getModificationMetadataWithElementType(I
return res;
});
}

@CEntryPoint(name = "moveConnectable")
public static void moveConnectable(IsolateThread thread, ObjectHandle networkHandle, CCharPointer equipmentId,
CCharPointer busOrigin, CCharPointer busDestination,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
Network network = ObjectHandles.getGlobal().get(networkHandle);
String idEq = CTypeUtil.toString(equipmentId);
String idBusOrigin = CTypeUtil.toString(busOrigin);
String idBusDestination = CTypeUtil.toString(busDestination);
NetworkUtil.moveConnectable(network, idEq, idBusOrigin, idBusDestination);
});
}
}
46 changes: 46 additions & 0 deletions java/src/main/java/com/powsybl/python/network/NetworkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.dataframe.network.extensions.ConnectablePositionFeederData;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.iidm.network.extensions.SlackTerminal;
import com.powsybl.python.commons.PyPowsyblApiHeader;

import java.util.List;
Expand Down Expand Up @@ -238,4 +239,49 @@ public static Stream<ConnectablePositionFeederData> getFeeders(Network network)
return feeders.build();
}

public static void moveConnectable(Network network, String equipmentId, String busOrigin, String busDestination) {
Bus b = network.getBusBreakerView().getBus(busOrigin);
if (b == null) {
throw new PowsyblException("Cannot find bus origin " + busOrigin + " in the bus breaker view");
}
if (network.getBusBreakerView().getBus(busDestination) == null) {
throw new PowsyblException("Cannot find bus destination " + busDestination + " in the bus breaker view");
}

boolean updateRegulatingTerminal = false;
Generator gen = network.getGenerator(equipmentId);
if (gen != null && gen.getRegulatingTerminal().equals(gen.getTerminal())) {
//Equipment is a generator, move connectable does not update regulating terminal properly,
//If generator is regulating on its terminal we have to update it manually
//Fix to do properly within move connectable in powsybl-core
updateRegulatingTerminal = true;
}

boolean foundTerminal = false;
for (Terminal t : b.getConnectedTerminals()) {
Connectable<?> connectable = t.getConnectable();
if (connectable.getId().equals(equipmentId)) {
SlackTerminal slackTerminal = t.getVoltageLevel().getExtension(SlackTerminal.class);
//Terminal of the connectable was a slack terminal, mus be updated after the move...
boolean updateSlackTerminal = slackTerminal != null && slackTerminal.getTerminal().equals(t);

t.getBusBreakerView().moveConnectable(busDestination, true);

if (updateSlackTerminal) {
for (Terminal newTerminal : connectable.getTerminals()) {
if (newTerminal.getBusBreakerView().getBus().getId().equals(busDestination)) {
slackTerminal.setTerminal(newTerminal);
}
}
}
foundTerminal = true;
}
}
if (!foundTerminal) {
throw new PowsyblException("Could not find terminal for equipment " + equipmentId + " in bus " + busOrigin);
}
if (updateRegulatingTerminal) {
gen.setRegulatingTerminal(gen.getTerminal());
}
}
}
2 changes: 2 additions & 0 deletions pypowsybl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5796,6 +5796,8 @@ def create_voltage_level_topology(network: Network, df: _DataFrame = None, raise
raise_exception,
None if reporter is None else reporter._reporter_model) # pylint: disable=protected-access

def move_connectable(network: Network, equipment_id: str, bus_origin_id: str, bus_destination_id: str) -> None:
_pp.move_connectable(network._handle, equipment_id, bus_origin_id, bus_destination_id)

def transform_list_to_str(entry: _Union[str, _List[str]]) -> str:
if isinstance(entry, list):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,9 @@ def test_terminals():
n.update_terminals(element_id='LINE_S2S3', connected=False, element_side='side')
assert "No enum constant" in str(e)

def test_move_connectable():
n = pp.network.create_ieee14()
pp.network.move_connectable(network=n, equipment_id='L1-2-1', bus_origin_id='B1', bus_destination_id='B5')

if __name__ == '__main__':
unittest.main()
Loading