Skip to content

Commit

Permalink
Merge branch 'main' into save_network_bin_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
geofjamg authored Nov 12, 2023
2 parents e0f300f + 6c803de commit ef06b7c
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ 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("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
8 changes: 4 additions & 4 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,12 +1499,12 @@ JavaHandle createVoltageInitializerParams() {
return pypowsybl::callJava<JavaHandle>(::createVoltageInitializerParams);
}

JavaHandle createVoltageLimitOverride(double minVoltage, double maxVoltage) {
return pypowsybl::callJava<JavaHandle>(::createVoltageLimitOverride, minVoltage, maxVoltage);
void voltageInitializerAddSpecificLowVoltageLimits(const JavaHandle& paramsHandle, const std::string& voltageLevelId, bool isRelative, double limit) {
pypowsybl::callJava(::voltageInitializerAddSpecificLowVoltageLimits, paramsHandle, (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);
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
4 changes: 2 additions & 2 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ 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);
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,34 @@ 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) {
return doCatch(exceptionHandlerPtr,
() -> ObjectHandles.getGlobal().create(new VoltageLimitOverride(minVoltage, maxVoltage)));
@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 = "voltageInitializerAddSpecificVoltageLimits")
public static void addSpecificVoltageLimits(IsolateThread thread, CCharPointer idPtr, double minVoltage,
ObjectHandle paramsHandle, double maxVoltage,
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
@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
7 changes: 4 additions & 3 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,6 @@ 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 voltage_initializer_add_variable_shunt_compensators(
Expand All @@ -706,9 +705,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
39 changes: 37 additions & 2 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,50 @@ 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_low_voltage_limits(self, low_limits: List[Tuple[str, bool, float]]) -> None:
'''
Indicate to voltage initializer to override the network low voltages limits,
limit can be given relative to former limit or absolute.
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:
low_limits: A List with elements as (voltage level id, is limit relative, limit value)
'''
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,
limit can be given relative to previous limit or absolute.
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_specific_voltage_limits(self, limits: Dict[str, Tuple[float, float]]) -> None:
'''
Indicate to voltage initializer to override the network voltages limits.
Limits are given relative to previous limits.
Use this if voltage initializer cannot converge because of infeasibility.
Args:
limits: A dictionary keys are voltage ids, values are (lower limit, upper limit)
'''
for key in limits:
voltage_initializer_add_specific_voltage_limits(key, limits[key][0], self._handle, limits[key][1])
self.add_specific_low_voltage_limits([(key, True, limits[key][0])])
self.add_specific_high_voltage_limits([(key, True, limits[key][1])])

def add_algorithm_param(self, parameters_dict: Dict[str, str]) -> None:
'''
Expand Down
7 changes: 7 additions & 0 deletions tests/test_voltage_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ def test_parameters():
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 ef06b7c

Please sign in to comment.