Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt committed Nov 7, 2023
1 parent 24b775b commit 9df3ad7
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 132 deletions.
9 changes: 0 additions & 9 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,19 +586,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_branch_factor_matrix", &pypowsybl::addBranchFactorMatrix, "Add a branch 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"));

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"));

Expand Down
25 changes: 0 additions & 25 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,31 +871,6 @@ 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) {
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 addBranchFactorMatrix(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) {
ToCharPtrPtr branchIdPtr(branchesIds);
Expand Down
9 changes: 0 additions & 9 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,9 @@ 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 addBranchFactorMatrix(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);

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);

JavaHandle runSensitivityAnalysis(const JavaHandle& sensitivityAnalysisContext, const JavaHandle& network, bool dc, SensitivityAnalysisParameters& parameters, const std::string& provider, JavaHandle* reporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,53 +118,6 @@ public static void addBranchFactorMatrix(IsolateThread thread, ObjectHandle sens
});
}

@CEntryPoint(name = "addBranchFlowFactorMatrix")
public static void addBranchFlowFactorMatrix(IsolateThread thread, ObjectHandle sensitivityAnalysisContextHandle,
CCharPointerPointer branchIdPtrPtr, int branchIdCount,
CCharPointerPointer variableIdPtrPtr, int variableIdCount,
CCharPointer matrixIdPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
SensitivityAnalysisContext analysisContext = ObjectHandles.getGlobal().get(sensitivityAnalysisContextHandle);
List<String> branchesIds = toStringList(branchIdPtrPtr, branchIdCount);
List<String> variablesIds = toStringList(variableIdPtrPtr, variableIdCount);
String matrixId = CTypeUtil.toString(matrixIdPtr);
analysisContext.addBranchFlowFactorMatrix(matrixId, branchesIds, variablesIds);
});
}

@CEntryPoint(name = "addPreContingencyBranchFlowFactorMatrix")
public static void addPreContingencyBranchFlowFactorMatrix(IsolateThread thread, ObjectHandle sensitivityAnalysisContextHandle,
CCharPointerPointer branchIdPtrPtr, int branchIdCount,
CCharPointerPointer variableIdPtrPtr, int variableIdCount,
CCharPointer matrixIdPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
SensitivityAnalysisContext analysisContext = ObjectHandles.getGlobal().get(sensitivityAnalysisContextHandle);
List<String> branchesIds = toStringList(branchIdPtrPtr, branchIdCount);
List<String> variablesIds = toStringList(variableIdPtrPtr, variableIdCount);
String matrixId = CTypeUtil.toString(matrixIdPtr);
analysisContext.addPreContingencyBranchFlowFactorMatrix(matrixId, branchesIds, variablesIds);
});
}

@CEntryPoint(name = "addPostContingencyBranchFlowFactorMatrix")
public static void addPostContingencyBranchFlowFactorMatrix(IsolateThread thread, ObjectHandle sensitivityAnalysisContextHandle,
CCharPointerPointer branchIdPtrPtr, int branchIdCount,
CCharPointerPointer variableIdPtrPtr, int variableIdCount,
CCharPointerPointer contingenciesIdPtrPtr, int contingenciesIdCount,
CCharPointer matrixIdPtr,
ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
SensitivityAnalysisContext analysisContext = ObjectHandles.getGlobal().get(sensitivityAnalysisContextHandle);
List<String> branchesIds = toStringList(branchIdPtrPtr, branchIdCount);
List<String> variablesIds = toStringList(variableIdPtrPtr, variableIdCount);
List<String> contingencies = toStringList(contingenciesIdPtrPtr, contingenciesIdCount);
String matrixId = CTypeUtil.toString(matrixIdPtr);
analysisContext.addPostContingencyBranchFlowFactorMatrix(matrixId, branchesIds, variablesIds, contingencies);
});
}

@CEntryPoint(name = "setBusVoltageFactorMatrix")
public static void setBusVoltageFactorMatrix(IsolateThread thread, ObjectHandle sensitivityAnalysisContextHandle,
CCharPointerPointer busVoltageIdPtrPtr, int branchIdCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,9 @@ int getColumnCount() {

private MatrixInfo busVoltageFactorsMatrix;

void addBranchFlowFactorMatrix(String matrixId, ContingencyContextType contingencyContextType, List<String> branchesIds,
List<String> variablesIds, List<String> contingencyIds) {
if (branchFlowFactorsMatrix.containsKey(matrixId)) {
throw new PowsyblException("Matrix '" + matrixId + "' already exists.");
}
MatrixInfo info = new MatrixInfo(contingencyContextType, SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, branchesIds, variablesIds, contingencyIds);
branchFlowFactorsMatrix.put(matrixId, info);
}

void addBranchFlowFactorMatrix(String matrixId, List<String> branchesIds, List<String> variablesIds) {
addBranchFlowFactorMatrix(matrixId, ContingencyContextType.ALL, branchesIds, variablesIds, Collections.emptyList());
}

void addPreContingencyBranchFlowFactorMatrix(String matrixId, List<String> branchesIds, List<String> variablesIds) {
addBranchFlowFactorMatrix(matrixId, ContingencyContextType.NONE, branchesIds, variablesIds, Collections.emptyList());
}

void addPostContingencyBranchFlowFactorMatrix(String matrixId, List<String> branchesIds, List<String> variablesIds, List<String> contingencies) {
addBranchFlowFactorMatrix(matrixId, ContingencyContextType.SPECIFIC, branchesIds, variablesIds, contingencies);
}

void addBranchFactorMatrix(String matrixId, List<String> branchesIds, List<String> variablesIds,
List<String> contingencies, ContingencyContextType contingencyContextType,
SensitivityFunctionType sensitivityFunctionType) {
List<String> contingencies, ContingencyContextType contingencyContextType,
SensitivityFunctionType sensitivityFunctionType) {
if (branchFlowFactorsMatrix.containsKey(matrixId)) {
throw new PowsyblException("Matrix '" + matrixId + "' already exists.");
}
Expand Down
3 changes: 0 additions & 3 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -614,9 +614,6 @@ def get_validation_level(network: JavaHandle) -> ValidationLevel: ...
def get_variant_ids(network: JavaHandle) -> List[str]: ...
def get_version_table() -> str: ...
def get_working_variant_id(network: JavaHandle) -> str: ...
def add_branch_flow_factor_matrix(sensitivity_analysis_context: JavaHandle, matrix_id: str, branches_ids: List[str], variables_ids: List[str]) -> None: ...
def add_precontingency_branch_flow_factor_matrix(sensitivity_analysis_context: JavaHandle, matrix_id: str, branches_ids: List[str], variables_ids: List[str]) -> None: ...
def add_postcontingency_branch_flow_factor_matrix(sensitivity_analysis_context: JavaHandle, matrix_id: str, branches_ids: List[str], variables_ids: List[str], contingencies_ids: List[str]) -> None: ...
def add_branch_factor_matrix(sensitivity_analysis_context: JavaHandle, matrix_id: str, branches_ids: List[str], variables_ids: List[str], contingencies_ids: List[str], contingency_context_type: ContingencyContextType, sensitivity_function_type: SensitivityFunctionType) -> None: ...
def is_config_read() -> bool: ...
def get_default_loadflow_provider() -> str: ...
Expand Down
22 changes: 6 additions & 16 deletions pypowsybl/sensitivity/impl/sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ def add_branch_flow_factor_matrix(self, branches_ids: List[str], variables_ids:
variables_ids: variables which may impact branch flows,to which we should compute sensitivities
matrix_id: The matrix unique identifier, to be used to retrieve the sensibility value
"""
(flatten_variables_ids, branch_data_frame_index) = self._process_variable_ids(variables_ids)
_pypowsybl.add_branch_flow_factor_matrix(self._handle, matrix_id, branches_ids, flatten_variables_ids)
self.branches_ids[matrix_id] = branches_ids
self.branch_data_frame_index[matrix_id] = branch_data_frame_index
self.add_branch_factor_matrix(branches_ids, variables_ids, [], ContingencyContextType.ALL,
SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, matrix_id)

def add_precontingency_branch_flow_factor_matrix(self, branches_ids: List[str], variables_ids: List[str],
matrix_id: str = 'default') -> None:
Expand All @@ -107,12 +105,8 @@ def add_precontingency_branch_flow_factor_matrix(self, branches_ids: List[str],
variables_ids: variables which may impact branch flows,to which we should compute sensitivities
matrix_id: The matrix unique identifier, to be used to retrieve the sensibility value
"""
(flatten_variables_ids, branch_data_frame_index) = self._process_variable_ids(variables_ids)

_pypowsybl.add_precontingency_branch_flow_factor_matrix(self._handle, matrix_id, branches_ids,
flatten_variables_ids)
self.branches_ids[matrix_id] = branches_ids
self.branch_data_frame_index[matrix_id] = branch_data_frame_index
self.add_branch_factor_matrix(branches_ids, variables_ids, [], ContingencyContextType.NONE,
SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, matrix_id)

def add_postcontingency_branch_flow_factor_matrix(self, branches_ids: List[str], variables_ids: List[str],
contingencies_ids: List[str],
Expand All @@ -131,12 +125,8 @@ def add_postcontingency_branch_flow_factor_matrix(self, branches_ids: List[str],
contingencies_ids: List of the IDs of the contingencies to simulate
matrix_id: The matrix unique identifier, to be used to retrieve the sensibility value
"""
(flatten_variables_ids, branch_data_frame_index) = self._process_variable_ids(variables_ids)

_pypowsybl.add_postcontingency_branch_flow_factor_matrix(self._handle, matrix_id, branches_ids,
flatten_variables_ids, contingencies_ids)
self.branches_ids[matrix_id] = branches_ids
self.branch_data_frame_index[matrix_id] = branch_data_frame_index
self.add_branch_factor_matrix(branches_ids, variables_ids, contingencies_ids, ContingencyContextType.SPECIFIC,
SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, matrix_id)

def add_branch_factor_matrix(self, branches_ids: List[str], variables_ids: List[str], contingencies_ids: List[str],
contingency_context_type: ContingencyContextType,
Expand Down

0 comments on commit 9df3ad7

Please sign in to comment.