Skip to content

Commit

Permalink
Add target v sensitivity on current and reactive power (#662)
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
EtienneLt authored Nov 13, 2023
1 parent bdf4b7c commit 615ec32
Show file tree
Hide file tree
Showing 20 changed files with 701 additions and 452 deletions.
48 changes: 29 additions & 19 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,10 @@ PYBIND11_MODULE(_pypowsybl, m) {
m.def("set_zones", &pypowsybl::setZones, "Add zones to sensitivity analysis",
py::arg("sensitivity_analysis_context"), py::arg("zones"));

m.def("add_branch_flow_factor_matrix", &pypowsybl::addBranchFlowFactorMatrix, "Add a branch_flow factor matrix to a sensitivity analysis",
py::arg("sensitivity_analysis_context"), py::arg("matrix_id"), py::arg("branches_ids"), py::arg("variables_ids"));

m.def("add_precontingency_branch_flow_factor_matrix", &pypowsybl::addPreContingencyBranchFlowFactorMatrix, "Add a branch_flow factor matrix to a sensitivity analysis",
py::arg("sensitivity_analysis_context"), py::arg("matrix_id"), py::arg("branches_ids"), py::arg("variables_ids"));

m.def("add_postcontingency_branch_flow_factor_matrix", &pypowsybl::addPostContingencyBranchFlowFactorMatrix, "Add a branch_flow factor matrix to a sensitivity analysis",
py::arg("sensitivity_analysis_context"), py::arg("matrix_id"), py::arg("branches_ids"), py::arg("variables_ids"), py::arg("contingencies_ids"));

m.def("set_bus_voltage_factor_matrix", &pypowsybl::setBusVoltageFactorMatrix, "Add a bus_voltage factor matrix to a sensitivity analysis",
py::arg("sensitivity_analysis_context"), py::arg("bus_ids"), py::arg("target_voltage_ids"));
m.def("add_factor_matrix", &pypowsybl::addFactorMatrix, "Add a factor matrix to a sensitivity analysis",
py::arg("sensitivity_analysis_context"), py::arg("matrix_id"), py::arg("branches_ids"), py::arg("variables_ids"),
py::arg("contingencies_ids"), py::arg("contingency_context_type"), py::arg("sensitivity_function_type"),
py::arg("sensitivity_variable_type"));

m.def("run_sensitivity_analysis", &pypowsybl::runSensitivityAnalysis, "Run a sensitivity analysis", py::call_guard<py::gil_scoped_release>(),
py::arg("sensitivity_analysis_context"), py::arg("network"), py::arg("dc"), py::arg("parameters"), py::arg("provider"), py::arg("reporter"));
Expand All @@ -614,18 +607,12 @@ PYBIND11_MODULE(_pypowsybl, m) {
{ sizeof(double) * m.column_count, sizeof(double) });
});

m.def("get_branch_flows_sensitivity_matrix", &pypowsybl::getBranchFlowsSensitivityMatrix, "Get sensitivity analysis result matrix for a given contingency",
m.def("get_sensitivity_matrix", &pypowsybl::getSensitivityMatrix, "Get sensitivity analysis result matrix for a given contingency",
py::arg("sensitivity_analysis_result_context"), py::arg("matrix_id"), py::arg("contingency_id"));

m.def("get_bus_voltages_sensitivity_matrix", &pypowsybl::getBusVoltagesSensitivityMatrix, "Get sensitivity analysis result matrix for a given contingency",
py::arg("sensitivity_analysis_result_context"), py::arg("contingency_id"));

m.def("get_reference_flows", &pypowsybl::getReferenceFlows, "Get sensitivity analysis result reference flows for a given contingency",
m.def("get_reference_matrix", &pypowsybl::getReferenceMatrix, "Get sensitivity analysis result reference matrix for a given contingency",
py::arg("sensitivity_analysis_result_context"), py::arg("matrix_id"), py::arg("contingency_id"));

m.def("get_reference_voltages", &pypowsybl::getReferenceVoltages, "Get sensitivity analysis result reference voltages for a given contingency",
py::arg("sensitivity_analysis_result_context"), py::arg("contingency_id"));

py::class_<series>(m, "Series")
.def_property_readonly("name", [](const series& s) {
return s.name;
Expand Down Expand Up @@ -694,6 +681,29 @@ PYBIND11_MODULE(_pypowsybl, m) {
.value("NONE", contingency_context_type::NONE)
.value("SPECIFIC", contingency_context_type::SPECIFIC);

py::enum_<sensitivity_function_type>(m, "SensitivityFunctionType")
.value("BRANCH_ACTIVE_POWER_1", sensitivity_function_type::BRANCH_ACTIVE_POWER_1)
.value("BRANCH_CURRENT_1",sensitivity_function_type::BRANCH_CURRENT_1)
.value("BRANCH_REACTIVE_POWER_1",sensitivity_function_type::BRANCH_REACTIVE_POWER_1)
.value("BRANCH_ACTIVE_POWER_2",sensitivity_function_type::BRANCH_ACTIVE_POWER_2)
.value("BRANCH_CURRENT_2",sensitivity_function_type::BRANCH_CURRENT_2)
.value("BRANCH_REACTIVE_POWER_2",sensitivity_function_type::BRANCH_REACTIVE_POWER_2)
.value("BRANCH_ACTIVE_POWER_3",sensitivity_function_type::BRANCH_ACTIVE_POWER_3)
.value("BRANCH_CURRENT_3",sensitivity_function_type::BRANCH_CURRENT_3)
.value("BRANCH_REACTIVE_POWER_3",sensitivity_function_type::BRANCH_REACTIVE_POWER_3)
.value("BUS_VOLTAGE",sensitivity_function_type::BUS_VOLTAGE);

py::enum_<sensitivity_variable_type>(m, "SensitivityVariableType")
.value("AUTO_DETECT", sensitivity_variable_type::AUTO_DETECT)
.value("INJECTION_ACTIVE_POWER", sensitivity_variable_type::INJECTION_ACTIVE_POWER)
.value("INJECTION_REACTIVE_POWER", sensitivity_variable_type::INJECTION_REACTIVE_POWER)
.value("TRANSFORMER_PHASE", sensitivity_variable_type::TRANSFORMER_PHASE)
.value("BUS_TARGET_VOLTAGE", sensitivity_variable_type::BUS_TARGET_VOLTAGE)
.value("HVDC_LINE_ACTIVE_POWER", sensitivity_variable_type::HVDC_LINE_ACTIVE_POWER)
.value("TRANSFORMER_PHASE_1", sensitivity_variable_type::TRANSFORMER_PHASE_1)
.value("TRANSFORMER_PHASE_2", sensitivity_variable_type::TRANSFORMER_PHASE_2)
.value("TRANSFORMER_PHASE_3", sensitivity_variable_type::TRANSFORMER_PHASE_3);

m.def("get_post_contingency_results", &pypowsybl::getPostContingencyResults, "get post contingency results of a security analysis", py::arg("result"));
m.def("get_pre_contingency_result", &pypowsybl::getPreContingencyResult, "get pre contingency result of a security analysis", py::arg("result"));
m.def("get_node_breaker_view_nodes", &pypowsybl::getNodeBreakerViewNodes, "get all nodes for a voltage level", py::arg("network"), py::arg("voltage_level"));
Expand Down
25 changes: 25 additions & 0 deletions cpp/src/pypowsybl-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,31 @@ typedef enum {
SPECIFIC,
} contingency_context_type;

typedef enum {
BRANCH_ACTIVE_POWER_1=0,
BRANCH_CURRENT_1,
BRANCH_REACTIVE_POWER_1,
BRANCH_ACTIVE_POWER_2,
BRANCH_CURRENT_2,
BRANCH_REACTIVE_POWER_2,
BRANCH_ACTIVE_POWER_3,
BRANCH_CURRENT_3,
BRANCH_REACTIVE_POWER_3,
BUS_VOLTAGE,
} sensitivity_function_type;

typedef enum {
AUTO_DETECT=0,
INJECTION_ACTIVE_POWER,
INJECTION_REACTIVE_POWER,
TRANSFORMER_PHASE,
BUS_TARGET_VOLTAGE,
HVDC_LINE_ACTIVE_POWER,
TRANSFORMER_PHASE_1,
TRANSFORMER_PHASE_2,
TRANSFORMER_PHASE_3,
} sensitivity_variable_type;

typedef enum {
VOLTAGE_LEVEL_TOPOLOGY_CREATION = 0,
CREATE_COUPLING_DEVICE,
Expand Down
52 changes: 10 additions & 42 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,64 +889,32 @@ void setZones(const JavaHandle& sensitivityAnalysisContext, const std::vector<::
callJava(::setZones, sensitivityAnalysisContext, zonesPtr.get(), zones.size());
}

void addBranchFlowFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds) {
ToCharPtrPtr branchIdPtr(branchesIds);
ToCharPtrPtr variableIdPtr(variablesIds);
callJava(::addBranchFlowFactorMatrix, sensitivityAnalysisContext, branchIdPtr.get(), branchesIds.size(),
variableIdPtr.get(), variablesIds.size(), (char*) matrixId.c_str());
}

void addPreContingencyBranchFlowFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds) {
ToCharPtrPtr branchIdPtr(branchesIds);
ToCharPtrPtr variableIdPtr(variablesIds);
callJava(::addPreContingencyBranchFlowFactorMatrix, sensitivityAnalysisContext, branchIdPtr.get(), branchesIds.size(),
variableIdPtr.get(), variablesIds.size(), (char*) matrixId.c_str());
}

void addPostContingencyBranchFlowFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds, const std::vector<std::string>& contingenciesIds) {
void addFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds, const std::vector<std::string>& contingenciesIds, contingency_context_type ContingencyContextType,
sensitivity_function_type sensitivityFunctionType, sensitivity_variable_type sensitivityVariableType) {
ToCharPtrPtr branchIdPtr(branchesIds);
ToCharPtrPtr variableIdPtr(variablesIds);
ToCharPtrPtr contingenciesIdPtr(contingenciesIds);
callJava(::addPostContingencyBranchFlowFactorMatrix, sensitivityAnalysisContext, branchIdPtr.get(), branchesIds.size(),
variableIdPtr.get(), variablesIds.size(), contingenciesIdPtr.get(), contingenciesIds.size(), (char*) matrixId.c_str());
}

void setBusVoltageFactorMatrix(const JavaHandle& sensitivityAnalysisContext, const std::vector<std::string>& busIds,
const std::vector<std::string>& targetVoltageIds) {
ToCharPtrPtr busVoltageIdPtr(busIds);
ToCharPtrPtr targetVoltageIdPtr(targetVoltageIds);
callJava(::setBusVoltageFactorMatrix, sensitivityAnalysisContext, busVoltageIdPtr.get(),
busIds.size(), targetVoltageIdPtr.get(), targetVoltageIds.size());
callJava(::addFactorMatrix, sensitivityAnalysisContext, branchIdPtr.get(), branchesIds.size(),
variableIdPtr.get(), variablesIds.size(), contingenciesIdPtr.get(), contingenciesIds.size(),
(char*) matrixId.c_str(), ContingencyContextType, sensitivityFunctionType, sensitivityVariableType);
}

JavaHandle runSensitivityAnalysis(const JavaHandle& sensitivityAnalysisContext, const JavaHandle& network, bool dc, SensitivityAnalysisParameters& parameters, const std::string& provider, JavaHandle* reporter) {
auto c_parameters = parameters.to_c_struct();
return callJava<JavaHandle>(::runSensitivityAnalysis, sensitivityAnalysisContext, network, dc, c_parameters.get(), (char *) provider.data(), (reporter == nullptr) ? nullptr : *reporter);
}

matrix* getBranchFlowsSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId) {
return callJava<matrix*>(::getBranchFlowsSensitivityMatrix, sensitivityAnalysisResultContext,
matrix* getSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId) {
return callJava<matrix*>(::getSensitivityMatrix, sensitivityAnalysisResultContext,
(char*) matrixId.c_str(), (char*) contingencyId.c_str());
}

matrix* getBusVoltagesSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& contingencyId) {
return callJava<matrix*>(::getBusVoltagesSensitivityMatrix, sensitivityAnalysisResultContext,
(char*) contingencyId.c_str());
}

matrix* getReferenceFlows(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId) {
return callJava<matrix*>(::getReferenceFlows, sensitivityAnalysisResultContext,
matrix* getReferenceMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId) {
return callJava<matrix*>(::getReferenceMatrix, sensitivityAnalysisResultContext,
(char*) matrixId.c_str(), (char*) contingencyId.c_str());
}

matrix* getReferenceVoltages(const JavaHandle& sensitivityAnalysisResultContext, const std::string& contingencyId) {
return callJava<matrix*>(::getReferenceVoltages, sensitivityAnalysisResultContext,
(char*) contingencyId.c_str());
}

SeriesArray* createNetworkElementsSeriesArray(const JavaHandle& network, element_type elementType, filter_attributes_type filterAttributesType, const std::vector<std::string>& attributes, dataframe* dataframe) {
ToCharPtrPtr attributesPtr(attributes);
return new SeriesArray(callJava<array*>(::createNetworkElementsSeriesArray, network, elementType, filterAttributesType, attributesPtr.get(), attributes.size(), dataframe));
Expand Down
21 changes: 5 additions & 16 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,26 +398,15 @@ JavaHandle createSensitivityAnalysis();

void setZones(const JavaHandle& sensitivityAnalysisContext, const std::vector<::zone*>& zones);

void addBranchFlowFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds);

void addPreContingencyBranchFlowFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds);

void addPostContingencyBranchFlowFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds, const std::vector<std::string>& contingenciesIds);

void setBusVoltageFactorMatrix(const JavaHandle& sensitivityAnalysisContext, const std::vector<std::string>& busIds, const std::vector<std::string>& targetVoltageIds);
void addFactorMatrix(const JavaHandle& sensitivityAnalysisContext, std::string matrixId, const std::vector<std::string>& branchesIds,
const std::vector<std::string>& variablesIds, const std::vector<std::string>& contingenciesIds, contingency_context_type ContingencyContextType,
sensitivity_function_type sensitivityFunctionType, sensitivity_variable_type sensitivityVariableType);

JavaHandle runSensitivityAnalysis(const JavaHandle& sensitivityAnalysisContext, const JavaHandle& network, bool dc, SensitivityAnalysisParameters& parameters, const std::string& provider, JavaHandle* reporter);

matrix* getBranchFlowsSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string &contingencyId);

matrix* getBusVoltagesSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string &contingencyId);

matrix* getReferenceFlows(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId);
matrix* getSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string &contingencyId);

matrix* getReferenceVoltages(const JavaHandle& sensitivityAnalysisResultContext, const std::string& contingencyId);
matrix* getReferenceMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId);

SeriesArray* createNetworkElementsSeriesArray(const JavaHandle& network, element_type elementType, filter_attributes_type filterAttributesType, const std::vector<std::string>& attributes, dataframe* dataframe);

Expand Down
Loading

0 comments on commit 615ec32

Please sign in to comment.