Skip to content

Commit

Permalink
c/cpp part does not work, to be fixed
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt committed Jun 25, 2024
1 parent 7af2b13 commit 09de88c
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 10 deletions.
8 changes: 8 additions & 0 deletions cpp/powsybl-cpp/powsybl-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ typedef enum {
THREE,
} ThreeSide;

typedef struct voltage_range_struct {
double minimum_nominal_voltage;
double maximum_nominal_voltage;
double voltage;
double range_coefficient;
} voltage_range;

typedef struct shortcircuit_analysis_parameters_struct {
unsigned char with_voltage_result;
unsigned char with_feeder_result;
Expand All @@ -383,6 +390,7 @@ typedef struct shortcircuit_analysis_parameters_struct {
unsigned char with_fortescue_result;
double min_voltage_drop_proportional_threshold;
int initial_voltage_profile_mode;
array voltage_ranges;
char** provider_parameters_keys;
int provider_parameters_keys_count;
char** provider_parameters_values;
Expand Down
50 changes: 46 additions & 4 deletions cpp/powsybl-cpp/powsybl-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ Array<limit_violation>::~Array() {
// already freed by contingency_result
}

template<>
Array<voltage_range>::~Array() {
// already freed by short circuit parameters
}

template<>
Array<series>::~Array() {
PowsyblCaller::get()->callJava<>(::freeSeriesArray, delegate_);
Expand Down Expand Up @@ -221,6 +226,11 @@ void copyCharPtrPtrToVector(char** src, int count, std::vector<std::string>& des
std::copy(src, src + count, std::back_inserter(dest));
}

void copyVoltageRangePtrPtrToVector(voltage_range** src, int count, std::vector<voltage_range>& dest) {
dest.clear();
std::copy(src, src + count, std::back_inserter(dest));
}

void deleteLoadFlowParameters(loadflow_parameters* ptr) {
pypowsybl::deleteCharPtrPtr(ptr->countries_to_balance, ptr->countries_to_balance_count);
pypowsybl::deleteCharPtrPtr(ptr->provider_parameters_keys, ptr->provider_parameters_keys_count);
Expand Down Expand Up @@ -1344,16 +1354,48 @@ void deleteShortCircuitAnalysisParameters(shortcircuit_analysis_parameters* ptr)
pypowsybl::deleteCharPtrPtr(ptr->provider_parameters_values, ptr->provider_parameters_values_count);
}

ShortCircuitAnalysisParameters::ShortCircuitAnalysisParameters(shortcircuit_analysis_parameters* src)
{
::voltage_range* createVoltageRange(double minimum_nominal_voltage, double maximum_nominal_voltage, double voltage, double range_coefficient) {
auto vr = pypowsybl::PowsyblCaller::get()->callJava<voltage_range*>(::createVoltageRange);
vr->minimum_nominal_voltage = minimum_nominal_voltage;
vr->maximum_nominal_voltage = maximum_nominal_voltage;
vr->voltage = voltage;
vr->range_coefficient = range_coefficient;
return vr;
}

void deleteVoltageRange(::voltage_range* vr) {
pypowsybl::PowsyblCaller::get()->callJava(::deleteVoltageRange, vr);
}

class VoltageRangesPtr {
public:
VoltageRangesPtr(const std::vector<voltage_range*>& vector)
: vector_(vector) {
}

~VoltageRangesPtr() {
for (auto vr : vector_) {
deleteVoltageRange(vr);
}
}

::voltage_range** get() const {
return (::voltage_range**) &vector_[0];
}

private:
const std::vector<::voltage_range*>& vector_;
};

ShortCircuitAnalysisParameters::ShortCircuitAnalysisParameters(shortcircuit_analysis_parameters* src) {
with_feeder_result = (bool) src->with_feeder_result;
with_limit_violations = (bool) src->with_limit_violations;
study_type = static_cast<ShortCircuitStudyType>(src->study_type);
with_fortescue_result = (bool) src->with_fortescue_result;
with_voltage_result = (bool) src->with_voltage_result;
min_voltage_drop_proportional_threshold = (double) src->min_voltage_drop_proportional_threshold;
initial_voltage_profile_mode = static_cast<InitialVoltageProfileMode>(src->initial_voltage_profile_mode);

// smthg to copy voltage range to vector
copyCharPtrPtrToVector(src->provider_parameters_keys, src->provider_parameters_keys_count, provider_parameters_keys);
copyCharPtrPtrToVector(src->provider_parameters_values, src->provider_parameters_values_count, provider_parameters_values);
}
Expand All @@ -1367,7 +1409,7 @@ std::shared_ptr<shortcircuit_analysis_parameters> ShortCircuitAnalysisParameters
res->with_fortescue_result = (bool) with_fortescue_result;
res->min_voltage_drop_proportional_threshold = min_voltage_drop_proportional_threshold;
res->initial_voltage_profile_mode = initial_voltage_profile_mode;

// smthg to copy voltage range to array
res->provider_parameters_keys = pypowsybl::copyVectorStringToCharPtrPtr(provider_parameters_keys);
res->provider_parameters_keys_count = provider_parameters_keys.size();
res->provider_parameters_values = pypowsybl::copyVectorStringToCharPtrPtr(provider_parameters_values);
Expand Down
3 changes: 2 additions & 1 deletion cpp/powsybl-cpp/powsybl-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ class ShortCircuitAnalysisParameters {
double min_voltage_drop_proportional_threshold;
InitialVoltageProfileMode initial_voltage_profile_mode;

std::vector<voltage_range> voltage_ranges;
std::vector<std::string> provider_parameters_keys;
std::vector<std::string> provider_parameters_values;
};
Expand Down Expand Up @@ -812,6 +813,6 @@ SeriesArray* getFaultResults(const JavaHandle& shortCircuitAnalysisResult, bool
SeriesArray* getFeederResults(const JavaHandle& shortCircuitAnalysisResult, bool withFortescueResult);
SeriesArray* getShortCircuitLimitViolations(const JavaHandle& shortCircuitAnalysisResult);
SeriesArray* getShortCircuitBusResults(const JavaHandle& shortCircuitAnalysisResult, bool withFortescueResult);

::voltage_range* createVoltageRange(const double minimum_nominal_voltage, const double maximum_nominal_voltage, const double voltage, const double range_coefficient);
}
#endif //PYPOWSYBL_H
6 changes: 6 additions & 0 deletions cpp/pypowsybl-cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,11 @@ PYBIND11_MODULE(_pypowsybl, m) {
.value("STEADY_STATE", pypowsybl::ShortCircuitStudyType::STEADY_STATE,
"The last stage of the short circuit, once all transient effects are gone.");

py::class_<::voltage_range>(m, "VoltageRange")
.def(py::init([](const double minimum_nominal_voltage, const double maximum_nominal_voltage, const double voltage, const double range_coefficient) {
return pypowsybl::createVoltageRange(minimum_nominal_voltage, maximum_nominal_voltage, voltage, range_coefficient);
}), py::arg("minimum_nominal_voltage"), py::arg("maximum_nominal_voltage"), py::arg("voltage"), py::arg("range_coefficient"));

py::class_<pypowsybl::ShortCircuitAnalysisParameters>(m, "ShortCircuitAnalysisParameters")
.def(py::init(&pypowsybl::createShortCircuitAnalysisParameters))
.def_readwrite("with_voltage_result", &pypowsybl::ShortCircuitAnalysisParameters::with_voltage_result)
Expand All @@ -1007,6 +1012,7 @@ PYBIND11_MODULE(_pypowsybl, m) {
.def_readwrite("with_fortescue_result", &pypowsybl::ShortCircuitAnalysisParameters::with_fortescue_result)
.def_readwrite("min_voltage_drop_proportional_threshold", &pypowsybl::ShortCircuitAnalysisParameters::min_voltage_drop_proportional_threshold)
.def_readwrite("initial_voltage_profile_mode", &pypowsybl::ShortCircuitAnalysisParameters::initial_voltage_profile_mode)
.def_readwrite("voltage_ranges", &pypowsybl::ShortCircuitAnalysisParameters::voltage_ranges)
.def_readwrite("provider_parameters_keys", &pypowsybl::ShortCircuitAnalysisParameters::provider_parameters_keys)
.def_readwrite("provider_parameters_values", &pypowsybl::ShortCircuitAnalysisParameters::provider_parameters_values);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public static CCharPointer getVersionTable(IsolateThread thread, ExceptionHandle
return doCatch(exceptionHandlerPtr, () -> CTypeUtil.toCharPtr(Version.getTableString()));
}

@CEntryPoint(name = "freeVoltageRangeArray")
public static void freeVoltageRangeArray(IsolateThread thread, ArrayPointer<VoltageRangePointer> arrayPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> freeVoltageRangeArrayContent(arrayPtr));
}

@CEntryPoint(name = "freeStringArray")
public static void freeStringArray(IsolateThread thread, ArrayPointer<CCharPointerPointer> arrayPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
Expand Down Expand Up @@ -99,6 +105,12 @@ private static void freeArrayContent(ArrayPointer<CCharPointerPointer> array) {
}
}

private static void freeVoltageRangeArrayContent(ArrayPointer<VoltageRangePointer> array) {
for (int i = 0; i < array.getLength(); i++) {
UnmanagedMemory.free(array.getPtr().read(i));
}
}

@CEntryPoint(name = "destroyObjectHandle")
public static void destroyObjectHandle(IsolateThread thread, ObjectHandle objectHandle, ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> ObjectHandles.getGlobal().destroy(objectHandle));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ public static ObjectHandle createShortCircuitAnalysis(IsolateThread thread, PyPo
return doCatch(exceptionHandlerPtr, () -> ObjectHandles.getGlobal().create(new ShortCircuitAnalysisContext()));
}

@CEntryPoint(name = "createVoltageRange")
public static PyPowsyblApiHeader.VoltageRangePointer createVoltageRange(IsolateThread thread, PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
return doCatch(exceptionHandlerPtr, () -> UnmanagedMemory.<PyPowsyblApiHeader.VoltageRangePointer>calloc(SizeOf.get(PyPowsyblApiHeader.VoltageRangePointer.class)));
}

@CEntryPoint(name = "deleteVoltageRange")
public static void deleteVoltageRange(IsolateThread thread, PyPowsyblApiHeader.VoltageRangePointer voltageRangePointer, PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> UnmanagedMemory.free(voltageRangePointer));
}

private static ShortCircuitAnalysisProvider getProvider(String name) {
String actualName = name.isEmpty() ? PyPowsyblConfiguration.getDefaultShortCircuitAnalysisProvider() : name;
return ShortCircuitAnalysisProvider.findAll().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static ShortCircuitAnalysisProvider getShortCircuitAnalysisProvider(Strin
}

public static ShortCircuitParameters createShortCircuitAnalysisParameters() {
return PyPowsyblConfiguration.isReadConfig() ? ShortCircuitParameters.load() : new ShortCircuitParameters();
return PyPowsyblConfiguration.isReadConfig() ? ShortCircuitParameters.load() : new ShortCircuitParameters().setVoltageRanges(new ArrayList<>());
}

public static ShortCircuitParameters createShortCircuitAnalysisParameters(PyPowsyblApiHeader.ShortCircuitAnalysisParametersPointer shortCircuitAnalysisParametersPointer,
Expand Down Expand Up @@ -68,7 +68,7 @@ private static ShortCircuitParameters createShortCircuitAnalysisParameters(PyPow

private static List<VoltageRange> createVoltageRangeList(PyPowsyblApiHeader.ArrayPointer<PyPowsyblApiHeader.VoltageRangePointer> voltageRangePointerArrayPointer) {
List<VoltageRange> voltageRanges = new ArrayList<>();
for (int i=0; i<= voltageRangePointerArrayPointer.getLength(); i++) {
for (int i = 0; i <= voltageRangePointerArrayPointer.getLength(); i++) {
PyPowsyblApiHeader.VoltageRangePointer voltageRangePointer = voltageRangePointerArrayPointer.getPtr().addressOf(i);
voltageRanges.add(new VoltageRange(voltageRangePointer.getMinimumNominalVoltage(),
voltageRangePointer.getMaximumNominalVoltage(),
Expand Down
18 changes: 17 additions & 1 deletion pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -689,16 +689,32 @@ class InitialVoltageProfileMode:
CONFIGURED: ClassVar[InitialVoltageProfileMode] = ...
PREVIOUS_VALUE: ClassVar[InitialVoltageProfileMode] = ...

class VoltageRange:
def __init__(self, minimum_nominal_voltage: float, maximum_nominal_voltage: float, voltage: float, range_coefficient: float) -> None: ...

@property
def minimum_nominal_voltage(self) -> float: ...

@property
def maximum_nominal_voltage(self) -> float: ...

@property
def voltage(self) -> float: ...

@property
def range_coefficient(self) -> float: ...

class ShortCircuitAnalysisParameters:
with_voltage_result: bool
with_feeder_result: bool
with_limit_violations: bool
study_type: ShortCircuitStudyType
with_fortescue_result: bool
min_voltage_drop_proportional_threshold: float
initial_voltage_profile_mode: InitialVoltageProfileMode
voltage_ranges: List[VoltageRange]
provider_parameters_keys: List[str]
provider_parameters_values: List[str]
initial_voltage_profile_mode: InitialVoltageProfileMode
def __init__(self) -> None: ...

def add_contingency(analysis_context: JavaHandle, contingency_id: str, elements_ids: List[str]) -> None: ...
Expand Down
1 change: 1 addition & 0 deletions pypowsybl/shortcircuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
get_default_provider,
get_provider_names,
get_provider_parameters_names)
from .impl.voltage_range import VoltageRange
13 changes: 11 additions & 2 deletions pypowsybl/shortcircuit/impl/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
#
from typing import Dict
from typing import Dict, List

from pypowsybl import _pypowsybl
from pypowsybl._pypowsybl import ShortCircuitStudyType, InitialVoltageProfileMode
from .voltage_range import VoltageRange

ShortCircuitStudyType.__module__ = __name__
ShortCircuitStudyType.__name__ = 'ShortCircuitStudyType'
Expand Down Expand Up @@ -37,6 +38,7 @@ class Parameters: # pylint: disable=too-few-public-methods
min_voltage_drop_proportional_threshold: specifies a threshold for filtering the voltage results.
Only nodes where the voltage drop due to the short circuit is greater than this property are retained.
study_type: specifies the type of short-circuit study. It can be SUB_TRANSIENT, TRANSIENT or STEADY_STATE.
voltage_ranges:
initial_voltage_profile_mode: specify how the computation is initialized. It can be NOMINAL, CONFIGURED or PREVIOUS_VALUE
"""

Expand All @@ -48,7 +50,8 @@ def __init__(self,
study_type: ShortCircuitStudyType = None,
provider_parameters: Dict[str, str] = None,
with_fortescue_result: bool = None,
initial_voltage_profile_mode: InitialVoltageProfileMode = None):
initial_voltage_profile_mode: InitialVoltageProfileMode = None,
voltage_ranges: List[VoltageRange] = None):
self._init_with_default_values()
if with_feeder_result is not None:
self.with_feeder_result = with_feeder_result
Expand All @@ -66,6 +69,8 @@ def __init__(self,
self.with_fortescue_result = with_fortescue_result
if initial_voltage_profile_mode is not None:
self.initial_voltage_profile_mode = initial_voltage_profile_mode
if voltage_ranges is not None:
self.voltage_ranges = voltage_ranges

def _init_from_c(self, c_parameters: _pypowsybl.ShortCircuitAnalysisParameters) -> None:
self.with_feeder_result = c_parameters.with_feeder_result
Expand All @@ -77,6 +82,7 @@ def _init_from_c(self, c_parameters: _pypowsybl.ShortCircuitAnalysisParameters)
zip(c_parameters.provider_parameters_keys, c_parameters.provider_parameters_values))
self.with_fortescue_result = c_parameters.with_fortescue_result
self.initial_voltage_profile_mode = c_parameters.initial_voltage_profile_mode
self.voltage_ranges = [VoltageRange(vr.minimum_nominal_voltage, vr.maximum_nominal_voltage, vr.voltage, vr.range_coefficient) for vr in c_parameters.voltage_ranges]

def _init_with_default_values(self) -> None:
self._init_from_c(_pypowsybl.ShortCircuitAnalysisParameters())
Expand All @@ -87,6 +93,7 @@ def _init_with_default_values(self) -> None:
self.study_type = ShortCircuitStudyType.TRANSIENT
self.with_fortescue_result = False
self.initial_voltage_profile_mode = InitialVoltageProfileMode.NOMINAL
self.voltage_ranges = []

def _to_c_parameters(self) -> _pypowsybl.ShortCircuitAnalysisParameters:
c_parameters = _pypowsybl.ShortCircuitAnalysisParameters()
Expand All @@ -97,6 +104,7 @@ def _to_c_parameters(self) -> _pypowsybl.ShortCircuitAnalysisParameters:
c_parameters.with_fortescue_result = self.with_fortescue_result
c_parameters.min_voltage_drop_proportional_threshold = self.min_voltage_drop_proportional_threshold
c_parameters.initial_voltage_profile_mode = self.initial_voltage_profile_mode
c_parameters.voltage_ranges = [_pypowsybl.VoltageRange(vr.minimum_nominal_voltage, vr.maximum_nominal_voltage, vr.voltage, vr.range_coefficient) for vr in self.voltage_ranges]
c_parameters.provider_parameters_keys = []
c_parameters.provider_parameters_values = []
return c_parameters
Expand All @@ -110,4 +118,5 @@ def __repr__(self) -> str:
f", study_type={self.study_type!r}" \
f", with_fortescue_result={self.with_fortescue_result!r}" \
f", initial_voltage_profile_mode={self.initial_voltage_profile_mode!r}" \
f", voltage_ranges={self.voltage_ranges!r}" \
f")"
1 change: 1 addition & 0 deletions tests/test_shortcircuit_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
import pypowsybl as pp
import pypowsybl.network as pn
from pypowsybl.shortcircuit import VoltageRange
import pytest
import pathlib
import pandas as pd
Expand Down

0 comments on commit 09de88c

Please sign in to comment.