Skip to content

Commit

Permalink
Update voltage initializer add_specific_voltage_limits to be usable w…
Browse files Browse the repository at this point in the history
…ith new java API for addSpecificVoltageLimits

Signed-off-by: Pauline Jean-Marie <[email protected]>
  • Loading branch information
Pauline Jean-Marie committed Oct 31, 2023
1 parent bfd6114 commit 1e2f559
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 31 deletions.
6 changes: 4 additions & 2 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ void voltageInitializerBinding(py::module_& m) {
.value("SPECIFIC_VOLTAGE_PROFILE", VoltageInitializerObjective::SPECIFIC_VOLTAGE_PROFILE);

m.def("create_voltage_initializer_params", &pypowsybl::createVoltageInitializerParams);
m.def("create_voltage_limit_override", &pypowsybl::createVoltageLimitOverride, py::arg("min_voltage"), py::arg("max_voltage"));
m.def("create_low_voltage_limit_override", &pypowsybl::createLowVoltageLimitOverride, py::arg("voltage_level_id"), py::arg("is_relative"), py::arg("limit"));
m.def("create_high_voltage_limit_override", &pypowsybl::createHighVoltageLimitOverride, py::arg("voltage_level_id"), py::arg("is_relative"), py::arg("limit"));

m.def("voltage_initializer_add_variable_shunt_compensators", &pypowsybl::voltageInitializerAddVariableShuntCompensators, py::arg("params_handle"), py::arg("id_ptr"));
m.def("voltage_initializer_add_constant_q_generators", &pypowsybl::voltageInitializerAddConstantQGenerators, py::arg("params_handle"), py::arg("id_ptr"));
m.def("voltage_initializer_add_variable_two_windings_transformers", &pypowsybl::voltageInitializerAddVariableTwoWindingsTransformers, py::arg("params_handle"), py::arg("id_ptr"));
m.def("voltage_initializer_add_specific_voltage_limits", &pypowsybl::voltageInitializerAddSpecificVoltageLimits, py::arg("id_ptr"), py::arg("min_voltage"), py::arg("params_handle"), py::arg("max_voltage"));
m.def("voltage_initializer_add_specific_low_voltage_limits", &pypowsybl::voltageInitializerAddSpecificLowVoltageLimits, py::arg("params_handle"), py::arg("voltage_level_id"), py::arg("is_relative"), py::arg("limit"));
m.def("voltage_initializer_add_specific_high_voltage_limits", &pypowsybl::voltageInitializerAddSpecificHighVoltageLimits, py::arg("params_handle"), py::arg("voltage_level_id"), py::arg("is_relative"), py::arg("limit"));

m.def("voltage_initializer_add_algorithm_param", &pypowsybl::voltageInitializerAddAlgorithmParam, py::arg("params_handle"), py::arg("key_ptr"), py::arg("value_ptr"));
m.def("voltage_initializer_set_objective", &pypowsybl::voltageInitializerSetObjective, py::arg("params_handle"), py::arg("c_objective"));
Expand Down
16 changes: 12 additions & 4 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,12 +1481,20 @@ JavaHandle createVoltageInitializerParams() {
return pypowsybl::callJava<JavaHandle>(::createVoltageInitializerParams);
}

JavaHandle createVoltageLimitOverride(double minVoltage, double maxVoltage) {
return pypowsybl::callJava<JavaHandle>(::createVoltageLimitOverride, minVoltage, maxVoltage);
JavaHandle createLowVoltageLimitOverride(const std::string& voltageLevelId, bool isRelative, double limit) {
return pypowsybl::callJava<JavaHandle>(::createLowVoltageLimitOverride, (char*) voltageLevelId.c_str(), isRelative, limit);
}

void voltageInitializerAddSpecificVoltageLimits(const std::string& idPtr, double minVoltage, const JavaHandle& paramsHandle, double maxVoltage) {
pypowsybl::callJava(::voltageInitializerAddSpecificVoltageLimits, (char*) idPtr.c_str(), minVoltage, paramsHandle, maxVoltage);
JavaHandle createHighVoltageLimitOverride(const std::string& voltageLevelId, bool isRelative, double limit) {
return pypowsybl::callJava<JavaHandle>(::createHighVoltageLimitOverride, (char*) voltageLevelId.c_str(), isRelative, limit);
}

void voltageInitializerAddSpecificLowVoltageLimits(const JavaHandle& paramsHandle, const std::string& voltageLevelId, bool isRelative, double limit) {
pypowsybl::callJava(::voltageInitializerAddSpecificLowVoltageLimits, paramsHandle, (char*) voltageLevelId.c_str(), isRelative, limit);
}

void voltageInitializerAddSpecificHighVoltageLimits(const JavaHandle& paramsHandle, const std::string& voltageLevelId, bool isRelative, double limit) {
pypowsybl::callJava(::voltageInitializerAddSpecificHighVoltageLimits, paramsHandle, (char*) voltageLevelId.c_str(), isRelative, limit);
}

void voltageInitializerAddVariableShuntCompensators(const JavaHandle& paramsHandle, const std::string& idPtr) {
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,10 @@ std::vector<std::string> getAllDynamicCurvesIds(JavaHandle resultHandle);
//=======Voltage initializer mapping========

JavaHandle createVoltageInitializerParams();
JavaHandle createVoltageLimitOverride(double minVoltage, double maxVoltage);
void voltageInitializerAddSpecificVoltageLimits(const std::string& idPtr, double minVoltage, const JavaHandle& paramsHandle, double maxVoltage);
JavaHandle createLowVoltageLimitOverride(const std::string& voltageLevelId, bool isRelative, double limit);
JavaHandle createHighVoltageLimitOverride(const std::string& voltageLevelId, bool isRelative, double limit);
void voltageInitializerAddSpecificLowVoltageLimits(const JavaHandle& paramsHandle, const std::string& voltageLevelId, bool isRelative, double limit);
void voltageInitializerAddSpecificHighVoltageLimits(const JavaHandle& paramsHandle, const std::string& voltageLevelId, bool isRelative, double limit);
void voltageInitializerAddVariableShuntCompensators(const JavaHandle& paramsHandle, const std::string& idPtr);
void voltageInitializerAddConstantQGenerators(const JavaHandle& paramsHandle, const std::string& idPtr);
void voltageInitializerAddVariableTwoWindingsTransformers(const JavaHandle& paramsHandle, const std::string& idPtr);
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/voltage_initializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ VoltageInitializerParameters : How to parameterize the tool
VoltageInitializerParameters.add_variable_shunt_compensators
VoltageInitializerParameters.add_constant_q_generators
VoltageInitializerParameters.add_variable_two_windings_transformers
VoltageInitializerParameters.add_specific_voltage_limits
VoltageInitializerParameters.add_specific_low_voltage_limits
VoltageInitializerParameters.add_specific_high_voltage_limits
VoltageInitializerParameters.add_algorithm_param
VoltageInitializerParameters.set_objective
VoltageInitializerParameters.set_objective_distance
Expand Down
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<mapdb.version>3.0.8</mapdb.version>
<maven-dependency-plugin.version>3.6.0</maven-dependency-plugin.version>
<powsybl-dependencies.version>2023.3.1</powsybl-dependencies.version>
<powsybl-open-reac.version>0.2.0</powsybl-open-reac.version>
<powsybl-open-reac.version>0.3.0</powsybl-open-reac.version>
<zstdjni.version>1.5.5-3</zstdjni.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static com.powsybl.python.commons.Util.doCatch;

import java.util.List;
import java.util.Map;

import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.ObjectHandle;
Expand Down Expand Up @@ -54,21 +53,60 @@ public static ObjectHandle createVoltageInitializerParams(IsolateThread thread,
return doCatch(exceptionHandlerPtr, () -> ObjectHandles.getGlobal().create(new OpenReacParameters()));
}

@CEntryPoint(name = "createVoltageLimitOverride")
public static ObjectHandle createVoltageLimitOverride(IsolateThread thread, double minVoltage, double maxVoltage,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
@CEntryPoint(name = "createLowVoltageLimitOverride")
public static ObjectHandle createLowVoltageLimitOverride(IsolateThread thread,
CCharPointer idPtr, boolean isRelative, double limit,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr,
() -> ObjectHandles.getGlobal().create(new VoltageLimitOverride(minVoltage, maxVoltage)));
() -> ObjectHandles.getGlobal().create(new VoltageLimitOverride(
CTypeUtil.toString(idPtr),
VoltageLimitOverride.VoltageLimitType.LOW_VOLTAGE_LIMIT,
isRelative,
limit))
);
}

@CEntryPoint(name = "voltageInitializerAddSpecificVoltageLimits")
public static void addSpecificVoltageLimits(IsolateThread thread, CCharPointer idPtr, double minVoltage,
ObjectHandle paramsHandle, double maxVoltage,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
@CEntryPoint(name = "createHighVoltageLimitOverride")
public static ObjectHandle createHighVoltageLimitOverride(IsolateThread thread,
CCharPointer idPtr, boolean isRelative, double limit,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr,
() -> ObjectHandles.getGlobal().create(new VoltageLimitOverride(
CTypeUtil.toString(idPtr),
VoltageLimitOverride.VoltageLimitType.HIGH_VOLTAGE_LIMIT,
isRelative,
limit))
);
}

@CEntryPoint(name = "voltageInitializerAddSpecificLowVoltageLimits")
public static void addSpecificLowVoltageLimits(IsolateThread thread, ObjectHandle paramsHandle,
CCharPointer idPtr, boolean isRelative, double limit,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
OpenReacParameters params = ObjectHandles.getGlobal().get(paramsHandle);
String voltageLevelId = CTypeUtil.toString(idPtr);
doCatch(exceptionHandlerPtr, () -> params
.addSpecificVoltageLimits(List.of(
new VoltageLimitOverride(voltageLevelId,
VoltageLimitOverride.VoltageLimitType.LOW_VOLTAGE_LIMIT,
isRelative,
limit)
)));
}

@CEntryPoint(name = "voltageInitializerAddSpecificHighVoltageLimits")
public static void addSpecificHighVoltageLimits(IsolateThread thread, ObjectHandle paramsHandle,
CCharPointer idPtr, boolean isRelative, double limit,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
OpenReacParameters params = ObjectHandles.getGlobal().get(paramsHandle);
String voltageId = CTypeUtil.toString(idPtr);
String voltageLevelId = CTypeUtil.toString(idPtr);
doCatch(exceptionHandlerPtr, () -> params
.addSpecificVoltageLimits(Map.of(voltageId, new VoltageLimitOverride(minVoltage, maxVoltage))));
.addSpecificVoltageLimits(List.of(
new VoltageLimitOverride(voltageLevelId,
VoltageLimitOverride.VoltageLimitType.HIGH_VOLTAGE_LIMIT,
isRelative,
limit)
)));
}

@CEntryPoint(name = "voltageInitializerAddVariableShuntCompensators")
Expand Down
9 changes: 6 additions & 3 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ def get_shortcircuit_provider_names() -> List[str]: ...
def get_shortcircuit_provider_parameters_names(provider: str) -> List[str]: ...

def create_voltage_initializer_params() -> JavaHandle: ...
def create_voltage_limit_override(min_voltage: float, max_voltage: float) -> JavaHandle: ...
def create_low_voltage_limit_override(voltage_level_id: str, is_relative: bool, limit: float) -> JavaHandle: ...
def create_high_voltage_limit_override(voltage_level_id: str, is_relative: bool, limit: float) -> JavaHandle: ...


def voltage_initializer_add_variable_shunt_compensators(
Expand All @@ -706,9 +707,11 @@ def voltage_initializer_add_variable_two_windings_transformers(
params_handle: JavaHandle, id_ptr: str) -> None: ...


def voltage_initializer_add_specific_voltage_limits(
id_ptr: str, min_voltage: float, params_handle: JavaHandle, max_voltage: float) -> None: ...
def voltage_initializer_add_specific_low_voltage_limits(
params_handle: JavaHandle, voltage_level_id: str, is_relative: bool, limit: float) -> None: ...

def voltage_initializer_add_specific_high_voltage_limits(
params_handle: JavaHandle, voltage_level_id: str, is_relative: bool, limit: float) -> None: ...

def voltage_initializer_add_algorithm_param(
params_handle: JavaHandle, key_ptr: str, value_ptr: str) -> None: ...
Expand Down
32 changes: 26 additions & 6 deletions pypowsybl/voltage_initializer/impl/voltage_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
voltage_initializer_add_variable_shunt_compensators,
voltage_initializer_add_constant_q_generators,
voltage_initializer_add_variable_two_windings_transformers,
voltage_initializer_add_specific_voltage_limits,
voltage_initializer_add_specific_low_voltage_limits,
voltage_initializer_add_specific_high_voltage_limits,
voltage_initializer_add_algorithm_param,
VoltageInitializerObjective,
voltage_initializer_set_objective,
Expand Down Expand Up @@ -63,16 +64,35 @@ def add_variable_two_windings_transformers(self, transformer_id_list: List[str])
for id in transformer_id_list:
voltage_initializer_add_variable_two_windings_transformers(self._handle, id)

def add_specific_voltage_limits(self, limits: Dict[str, Tuple[float, float]]) -> None:
def add_specific_low_voltage_limits(self, low_limits: List[Tuple[str, bool, float]]) -> None:
'''
Indicate to voltage initializer to override the network voltages limits.
Indicate to voltage initializer to override the network low voltages limits.
High limits can be given for the same voltage level ids using
:func:`~VoltageInitializerParameters.add_specific_high_voltage_limits`
but it is not necessary to give a high limit as long as each voltage level has its limits
defined and consistent after overrides (low limit < high limit, low limit > 0...)
Use this if voltage initializer cannot converge because of infeasibility.
Args:
limits: A dictionary keys are voltage ids, values are (lower limit, upper limit)
low_limits: A List with elements as (voltage level id, is limit relative, limit value)
'''
for key in limits:
voltage_initializer_add_specific_voltage_limits(key, limits[key][0], self._handle, limits[key][1])
for voltage_level_id, is_relative, limit in low_limits:
voltage_initializer_add_specific_low_voltage_limits(self._handle, voltage_level_id, is_relative, limit)

def add_specific_high_voltage_limits(self, high_limits: List[Tuple[str, bool, float]]) -> None:
'''
Indicate to voltage initializer to override the network high voltages limits.
Low limits can be given for the same voltage level ids using
:func:`~VoltageInitializerParameters.add_specific_low_voltage_limits`
but it is not necessary to give a low limit as long as each voltage level has its limits
defined and consistent after overrides (low limit < high limit, low limit > 0...)
Use this if voltage initializer cannot converge because of infeasibility.
Args:
high_limits: A List with elements as (voltage level id, is limit relative, limit value)
'''
for voltage_level_id, is_relative, limit in high_limits:
voltage_initializer_add_specific_high_voltage_limits(self._handle, voltage_level_id, is_relative, limit)

def add_algorithm_param(self, parameters_dict: Dict[str, str]) -> None:
'''
Expand Down
7 changes: 6 additions & 1 deletion tests/test_voltage_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def test_parameters():
params.add_variable_two_windings_transformers(["twt1", "twt2"])

params.add_algorithm_param({"foo": "bar", "bar": "bar2"})
params.add_specific_voltage_limits({"vl_id": (0.5, 1.2)})
params.add_specific_low_voltage_limits([("vl_id", True, 0.5)])
params.add_specific_high_voltage_limits([("vl_id", True, 1.2)])
params.add_specific_low_voltage_limits([("vl_id_2", False, 380)])
params.add_specific_high_voltage_limits([("vl_id_3", False, 420)])
params.add_specific_low_voltage_limits([("vl_id_4", False, 380)])
params.add_specific_high_voltage_limits([("vl_id_4", True, 2.3)])

params.set_objective(va.VoltageInitializerObjective.SPECIFIC_VOLTAGE_PROFILE)
params.set_objective_distance(1.3)
Expand Down

0 comments on commit 1e2f559

Please sign in to comment.