Skip to content

Commit

Permalink
Merge branch 'main' into misc-minor-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
geofjamg authored Dec 19, 2024
2 parents 984a78f + c154d6b commit aa3074c
Show file tree
Hide file tree
Showing 43 changed files with 1,571 additions and 173 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Requirements:
To build from sources and install PyPowSyBl package:

```bash
git clone --recursive https://github.com/powsybl/pypowsybl.git
git clone https://github.com/powsybl/pypowsybl.git
export JAVA_HOME=<path to GraalVM>
pip install --upgrade setuptools pip
pip install -r requirements.txt
Expand Down
45 changes: 39 additions & 6 deletions cpp/powsybl-cpp/powsybl-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1337,8 +1337,8 @@ JavaHandle createEventMapping() {
return PowsyblCaller::get()->callJava<JavaHandle>(::createEventMapping);
}

JavaHandle runDynamicModel(JavaHandle dynamicModelContext, JavaHandle network, JavaHandle dynamicMapping, JavaHandle eventMapping, JavaHandle timeSeriesMapping, int start, int stop, JavaHandle reportNode) {
return PowsyblCaller::get()->callJava<JavaHandle>(::runDynamicModel, dynamicModelContext, network, dynamicMapping, eventMapping, timeSeriesMapping, start, stop, reportNode);
JavaHandle runDynamicModel(JavaHandle dynamicModelContext, JavaHandle network, JavaHandle dynamicMapping, JavaHandle eventMapping, JavaHandle timeSeriesMapping, int start, int stop, JavaHandle *reportNode) {
return PowsyblCaller::get()->callJava<JavaHandle>(::runDynamicModel, dynamicModelContext, network, dynamicMapping, eventMapping, timeSeriesMapping, start, stop, (reportNode == nullptr) ? nullptr : *reportNode);
}

void addDynamicMappings(JavaHandle dynamicMappingHandle, DynamicMappingType mappingType, dataframe_array* dataframes) {
Expand All @@ -1349,12 +1349,17 @@ void addEventMappings(JavaHandle eventMappingHandle, EventMappingType mappingTyp
PowsyblCaller::get()->callJava<>(::addEventMappings, eventMappingHandle, mappingType, mappingDf);
}

void addCurve(JavaHandle curveMappingHandle, std::string dynamicId, std::string variable) {
PowsyblCaller::get()->callJava<>(::addCurve, curveMappingHandle, (char*) dynamicId.c_str(), (char*) variable.c_str());
void addOutputVariables(JavaHandle outputVariablesHandle, std::string dynamicId, std::vector<std::string>& variables, bool isDynamic, OutputVariableType variableType) {
ToCharPtrPtr variablesPtr(variables);
PowsyblCaller::get()->callJava<>(::addOutputVariables, outputVariablesHandle, (char*) dynamicId.c_str(), variablesPtr.get(), variables.size(), isDynamic, variableType);
}

std::string getDynamicSimulationResultsStatus(JavaHandle dynamicSimulationResultsHandle) {
return PowsyblCaller::get()->callJava<std::string>(::getDynamicSimulationResultsStatus, dynamicSimulationResultsHandle);
DynamicSimulationStatus getDynamicSimulationResultsStatus(JavaHandle resultsHandle) {
return PowsyblCaller::get()->callJava<DynamicSimulationStatus>(::getDynamicSimulationResultsStatus, resultsHandle);
}

std::string getDynamicSimulationResultsStatusText(JavaHandle resultsHandle) {
return PowsyblCaller::get()->callJava<std::string>(::getDynamicSimulationResultsStatusText, resultsHandle);
}

SeriesArray* getDynamicCurve(JavaHandle resultHandle, std::string curveName) {
Expand All @@ -1366,6 +1371,14 @@ std::vector<std::string> getAllDynamicCurvesIds(JavaHandle resultHandle) {
return vector.get();
}

SeriesArray* getFinalStateValues(JavaHandle resultHandle) {
return new SeriesArray(PowsyblCaller::get()->callJava<array*>(::getFinalStateValues, resultHandle));
}

SeriesArray* getTimeline(JavaHandle resultHandle) {
return new SeriesArray(PowsyblCaller::get()->callJava<array*>(::getTimeline, resultHandle));
}

std::vector<std::string> getSupportedModels(DynamicMappingType mappingType) {
ToStringVector vector(PowsyblCaller::get()->callJava<array*>(::getSupportedModels, mappingType));
return vector.get();
Expand Down Expand Up @@ -1647,4 +1660,24 @@ JavaHandle runVoltageInitializer(bool debug, const JavaHandle& networkHandle, co
return pypowsybl::PowsyblCaller::get()->callJava<JavaHandle>(::runVoltageInitializer, debug, networkHandle, paramsHandle);
}

JavaHandle createRao() {
return pypowsybl::PowsyblCaller::get()->callJava<JavaHandle>(::createRao);
}

RaoComputationStatus getRaoResultStatus(const JavaHandle& raoResult) {
return pypowsybl::PowsyblCaller::get()->callJava<RaoComputationStatus>(::getRaoResultStatus, raoResult);
}

JavaHandle getCrac(const JavaHandle& raoContext) {
return pypowsybl::PowsyblCaller::get()->callJava<JavaHandle>(::getCrac, raoContext);
}

JavaHandle getRaoResult(const JavaHandle& raoContext) {
return pypowsybl::PowsyblCaller::get()->callJava<JavaHandle>(::getRaoResult, raoContext);
}

JavaHandle createDefaultRaoParameters() {
return pypowsybl::PowsyblCaller::get()->callJava<JavaHandle>(::createDefaultRaoParameters);
}

}
18 changes: 14 additions & 4 deletions cpp/powsybl-cpp/powsybl-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,10 @@ JavaHandle createDynamicModelMapping();
JavaHandle createTimeseriesMapping();
JavaHandle createEventMapping();

JavaHandle runDynamicModel(JavaHandle dynamicModelContext, JavaHandle network, JavaHandle dynamicMapping, JavaHandle eventMapping, JavaHandle timeSeriesMapping, int start, int stop, JavaHandle reportNode);
JavaHandle runDynamicModel(JavaHandle dynamicModelContext, JavaHandle network, JavaHandle dynamicMapping, JavaHandle eventMapping, JavaHandle timeSeriesMapping, int start, int stop, JavaHandle* reportNode);

// timeseries/curves mapping
void addCurve(JavaHandle curveMappingHandle, std::string dynamicId, std::string variable);
// timeseries mapping
void addOutputVariables(JavaHandle outputVariablesHandle, std::string dynamicId, std::vector<std::string>& variables, bool isDynamic, OutputVariableType variableType);

// events mapping
void addEventMappings(JavaHandle eventMappingHandle, EventMappingType mappingType, dataframe* mappingDf);
Expand All @@ -781,9 +781,12 @@ std::vector<std::vector<SeriesMetadata>> getDynamicMappingsMetaData(DynamicMappi
std::vector<std::string> getSupportedModels(DynamicMappingType mappingType);

// results
std::string getDynamicSimulationResultsStatus(JavaHandle dynamicSimulationResultsHandle);
DynamicSimulationStatus getDynamicSimulationResultsStatus(JavaHandle resultsHandle);
std::string getDynamicSimulationResultsStatusText(JavaHandle resultsHandle);
SeriesArray* getDynamicCurve(JavaHandle resultHandle, std::string curveName);
std::vector<std::string> getAllDynamicCurvesIds(JavaHandle resultHandle);
SeriesArray* getFinalStateValues(JavaHandle resultHandle);
SeriesArray* getTimeline(JavaHandle resultHandle);

//=======END OF dynamic modeling for dynawo package==========

Expand Down Expand Up @@ -845,5 +848,12 @@ SeriesArray* getFeederResults(const JavaHandle& shortCircuitAnalysisResult, bool
SeriesArray* getShortCircuitLimitViolations(const JavaHandle& shortCircuitAnalysisResult);
SeriesArray* getShortCircuitBusResults(const JavaHandle& shortCircuitAnalysisResult, bool withFortescueResult);

// OpenRao
JavaHandle createRao();
JavaHandle getCrac(const JavaHandle& raoContext);
JavaHandle getRaoResult(const JavaHandle& raoContext);
RaoComputationStatus getRaoResultStatus(const JavaHandle& raoResult);
JavaHandle createDefaultRaoParameters();

}
#endif //PYPOWSYBL_H
3 changes: 1 addition & 2 deletions cpp/pypowsybl-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ include_directories(${POWSYBL_CPP_SOURCE_DIR} ${PYPOWSYBL_JAVA_SOURCE_DIR} ${PYP
link_directories(${PYPOWSYBL_JAVA_LIBRARY_DIR})
pybind11_add_module(_pypowsybl pylogging.cpp bindings.cpp)

add_dependencies(_pypowsybl native-image math-native)
add_dependencies(math-native native-image)
add_dependencies(_pypowsybl native-image)

# Fix related to issue describred here https://github.com/actions/runner-images/issues/10004#issuecomment-2156109231
# Should fix incompatibility between MSVC runtime 14.40.XXX and previous version
Expand Down
85 changes: 82 additions & 3 deletions cpp/pypowsybl-cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ pypowsybl::JavaHandle loadNetworkFromBinaryBuffersPython(std::vector<py::buffer>

py::bytes saveNetworkToBinaryBufferPython(const pypowsybl::JavaHandle& network, const std::string& format, const std::map<std::string, std::string>& parameters, pypowsybl::JavaHandle* reportNode);

void setCracSource(const pypowsybl::JavaHandle& networkHandle, const pypowsybl::JavaHandle& raoHandle, const py::buffer& crac);
void setGlskSource(const pypowsybl::JavaHandle& networkHandle, const pypowsybl::JavaHandle& raoHandle, const py::buffer& glsk);
void runRaoWithParameters(const pypowsybl::JavaHandle& networkHandle, const pypowsybl::JavaHandle& raoHandle, const pypowsybl::JavaHandle& parametersHandle);
pypowsybl::JavaHandle loadRaoParametersFromBuffer(const py::buffer& parameters);

py::bytes saveRaoParametersToBinaryBuffer(const pypowsybl::JavaHandle& rao_parameters);
py::bytes saveRaoResultsToBinaryBuffer(const pypowsybl::JavaHandle& raoContext, const pypowsybl::JavaHandle& crac);

template<typename T>
void bindArray(py::module_& m, const std::string& className) {
py::class_<T>(m, className.c_str())
Expand Down Expand Up @@ -180,6 +188,14 @@ void dynamicSimulationBindings(py::module_& m) {
.value("NODE_FAULT", EventMappingType::NODE_FAULT)
.value("ACTIVE_POWER_VARIATION", EventMappingType::ACTIVE_POWER_VARIATION);

py::enum_<OutputVariableType>(m, "OutputVariableType")
.value("CURVE", OutputVariableType::CURVE)
.value("FINAL_STATE", OutputVariableType::FINAL_STATE);

py::enum_<DynamicSimulationStatus>(m, "DynamicSimulationStatus")
.value("SUCCESS", DynamicSimulationStatus::DYNAMIC_SIMULATION_SUCCESS)
.value("FAILURE", DynamicSimulationStatus::DYNAMIC_SIMULATION_FAILURE);

//entrypoints for constructors
m.def("create_dynamic_simulation_context", &pypowsybl::createDynamicSimulationContext);
m.def("create_dynamic_model_mapping", &pypowsybl::createDynamicModelMapping);
Expand All @@ -195,17 +211,20 @@ void dynamicSimulationBindings(py::module_& m) {
m.def("get_dynamic_mappings_meta_data", &pypowsybl::getDynamicMappingsMetaData, py::arg("mapping_type"));
m.def("get_supported_models", &pypowsybl::getSupportedModels, py::arg("mapping_type"));

// timeseries/curves mapping
m.def("add_curve", &pypowsybl::addCurve, py::arg("curve_mapping_handle"), py::arg("dynamic_id"), py::arg("variable"));
// timeseries mapping
m.def("add_output_variables", &pypowsybl::addOutputVariables, py::arg("output_variables_handle"), py::arg("dynamic_id"), py::arg("variables"), py::arg("is_dynamic"), py::arg("output_variable_type"));

// events mapping
m.def("add_all_event_mappings", &pypowsybl::addEventMappings, py::arg("event_mapping_handle"), py::arg("mapping_type"), py::arg("mapping_df"));
m.def("get_event_mappings_meta_data", &pypowsybl::getEventMappingsMetaData, py::arg("mapping_type"));

// Simulation results
m.def("get_dynamic_simulation_results_status", &pypowsybl::getDynamicSimulationResultsStatus, py::arg("result_handle"));
m.def("get_dynamic_simulation_results_status_text", &pypowsybl::getDynamicSimulationResultsStatusText, py::arg("result_handle"));
m.def("get_dynamic_curve", &pypowsybl::getDynamicCurve, py::arg("report_handle"), py::arg("curve_name"));
m.def("get_all_dynamic_curves_ids", &pypowsybl::getAllDynamicCurvesIds, py::arg("report_handle"));
m.def("get_final_state_values", &pypowsybl::getFinalStateValues, py::arg("result_handle"));
m.def("get_timeline", &pypowsybl::getTimeline, py::arg("result_handle"));
}

void voltageInitializerBinding(py::module_& m) {
Expand Down Expand Up @@ -711,6 +730,10 @@ PYBIND11_MODULE(_pypowsybl, m) {
.value("ANY_VIOLATION_CONDITION", condition_type::ANY_VIOLATION_CONDITION)
.value("AT_LEAST_ONE_VIOLATION_CONDITION", condition_type::AT_LEAST_ONE_VIOLATION_CONDITION);

py::enum_<RaoComputationStatus>(m, "RaoComputationStatus")
.value("DEFAULT", RaoComputationStatus::DEFAULT)
.value("FAILURE", RaoComputationStatus::FAILURE);

py::class_<network_metadata, std::shared_ptr<network_metadata>>(m, "NetworkMetadata")
.def_property_readonly("id", [](const network_metadata& att) {
return att.id;
Expand Down Expand Up @@ -1091,6 +1114,21 @@ PYBIND11_MODULE(_pypowsybl, m) {
m.def("get_short_circuit_limit_violations", &pypowsybl::getShortCircuitLimitViolations, "gets the limit violations of a short-circuit analysis", py::arg("result"));
m.def("get_short_circuit_bus_results", &pypowsybl::getShortCircuitBusResults, "gets the bus results of a short-circuit analysis", py::arg("result"), py::arg("with_fortescue_result"));

m.def("create_rao", &pypowsybl::createRao, "Create rao context");
m.def("run_rao", ::runRaoWithParameters, py::call_guard<py::gil_scoped_release>(), "Run a rao from buffered inputs",
py::arg("network"), py::arg("rao_context"), py::arg("parameters"));
m.def("set_crac_source", ::setCracSource, py::call_guard<py::gil_scoped_release>(), "Set crac source",
py::arg("network"), py::arg("rao_context"), py::arg("crac_source"));
m.def("set_glsk_source", ::setGlskSource, py::call_guard<py::gil_scoped_release>(), "Set glsk source",
py::arg("network"), py::arg("rao_context"), py::arg("glsk_source"));
m.def("get_crac", &pypowsybl::getCrac, "Get crac associated to the rao context", py::arg("rao_context"));
m.def("get_rao_result", &pypowsybl::getRaoResult, "Get rao result associated to the rao context", py::arg("rao_context"));
m.def("create_default_rao_parameters", &pypowsybl::createDefaultRaoParameters, "Create a default rao parameter");
m.def("load_rao_parameters", ::loadRaoParametersFromBuffer, "Load rao parameters from a buffer", py::arg("parameters_buffer"));
m.def("serialize_rao_parameters", ::saveRaoParametersToBinaryBuffer, "Serialize rao parameters to a buffer", py::arg("rao_parameters"));
m.def("serialize_rao_results_to_buffer", ::saveRaoResultsToBinaryBuffer, "Run a rao", py::arg("rao_result"), py::arg("crac"));
m.def("get_rao_result_status", &pypowsybl::getRaoResultStatus, "Get the status of a rao result", py::arg("rao_result"));

}

void setLogLevelFromPythonLogger(pypowsybl::GraalVmGuard* guard, exception_handler* exc) {
Expand Down Expand Up @@ -1146,6 +1184,47 @@ py::bytes saveNetworkToBinaryBufferPython(const pypowsybl::JavaHandle& network,
parameterValuesPtr.get(), parameterValues.size(), reportNode == nullptr ? nullptr : *reportNode);
py::gil_scoped_acquire acquire;
py::bytes bytes((char*) byteArray->ptr, byteArray->length);
pypowsybl::PowsyblCaller::get()->callJava<>(::freeNetworkBinaryBuffer, byteArray);
pypowsybl::PowsyblCaller::get()->callJava<>(::freeBinaryBuffer, byteArray);
return bytes;
}

void setCracSource(const pypowsybl::JavaHandle& networkHandle, const pypowsybl::JavaHandle& raoHandle, const py::buffer& crac) {
py::buffer_info cracInfo = crac.request();
pypowsybl::PowsyblCaller::get()->callJava<>(::setCracBufferedSource,
networkHandle, raoHandle, static_cast<char*>(cracInfo.ptr), cracInfo.size);
}

void setGlskSource(const pypowsybl::JavaHandle& networkHandle, const pypowsybl::JavaHandle& raoHandle, const py::buffer& glsk) {
py::buffer_info glskInfo = glsk.request();
pypowsybl::PowsyblCaller::get()->callJava<>(::setGlskBufferedSource,
networkHandle, raoHandle,
static_cast<char*>(glskInfo.ptr), glskInfo.size);
}

void runRaoWithParameters(const pypowsybl::JavaHandle& networkHandle, const pypowsybl::JavaHandle& raoHandle, const pypowsybl::JavaHandle& parametersHandle) {
pypowsybl::PowsyblCaller::get()->callJava<>(::runRao,
networkHandle, raoHandle, parametersHandle);
}

py::bytes saveRaoResultsToBinaryBuffer(const pypowsybl::JavaHandle& raoResult, const pypowsybl::JavaHandle& crac) {
array* byteArray = pypowsybl::PowsyblCaller::get()->callJava<array*>(::serializeRaoResultsToBuffer, raoResult, crac);
py::gil_scoped_acquire acquire;
py::bytes bytes((char*) byteArray->ptr, byteArray->length);
pypowsybl::PowsyblCaller::get()->callJava<>(::freeBinaryBuffer, byteArray);
return bytes;
}

pypowsybl::JavaHandle loadRaoParametersFromBuffer(const py::buffer& parameters) {
py::buffer_info parametersInfo = parameters.request();
return pypowsybl::PowsyblCaller::get()->callJava<pypowsybl::JavaHandle>(::loadRaoParameters,
static_cast<char*>(parametersInfo.ptr), parametersInfo.size);
}

py::bytes saveRaoParametersToBinaryBuffer(const pypowsybl::JavaHandle& rao_parameters) {
array* byteArray = pypowsybl::PowsyblCaller::get()->callJava<array*>(::serializeRaoParameters, rao_parameters);
py::gil_scoped_acquire acquire;
py::bytes bytes((char*) byteArray->ptr, byteArray->length);
pypowsybl::PowsyblCaller::get()->callJava<>(::freeBinaryBuffer, byteArray);
return bytes;
}

Loading

0 comments on commit aa3074c

Please sign in to comment.