Skip to content

Commit

Permalink
add target v sensitivity on current and reactive power
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <etienne.lesot@rte-france.com>
  • Loading branch information
EtienneLt committed Oct 13, 2023
1 parent fb22913 commit c42b4da
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 54 deletions.
4 changes: 4 additions & 0 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ PYBIND11_MODULE(_pypowsybl, m) {
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"));

Expand Down
12 changes: 12 additions & 0 deletions cpp/src/pypowsybl-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ 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,
} sensitivity_function_type;

typedef enum {
VOLTAGE_LEVEL_TOPOLOGY_CREATION = 0,
CREATE_COUPLING_DEVICE,
Expand Down
10 changes: 10 additions & 0 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,16 @@ void addPostContingencyBranchFlowFactorMatrix(const JavaHandle& sensitivityAnaly
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);
ToCharPtrPtr variableIdPtr(variablesIds);
ToCharPtrPtr contingenciesIdPtr(contingenciesIds);
callJava(::addBranchFactorMatrix, sensitivityAnalysisContext, branchIdPtr.get(), branchesIds.size(),
variableIdPtr.get(), variablesIds.size(), contingenciesIdPtr.get(), contingenciesIds.size(),
(char*) matrixId.c_str(), ContingencyContextType, sensitivityFunctionType);
}

void setBusVoltageFactorMatrix(const JavaHandle& sensitivityAnalysisContext, const std::vector<std::string>& busIds,
const std::vector<std::string>& targetVoltageIds) {
ToCharPtrPtr busVoltageIdPtr(busIds);
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ void setZones(const JavaHandle& sensitivityAnalysisContext, const std::vector<::
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,25 @@ public enum RemoveModificationType {
public static native RemoveModificationType fromCValue(int value);
}

@CEnum("sensitivity_function_type")
public enum SensitivityFunctionType {
BRANCH_ACTIVE_POWER_1,
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;

@CEnumValue
public native int getCValue();

@CEnumLookup
public static native SensitivityFunctionType fromCValue(int value);
}

@CStruct("matrix")
public interface MatrixPointer extends PointerBase {

Expand Down
42 changes: 26 additions & 16 deletions java/src/main/java/com/powsybl/python/commons/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.powsybl.python.commons.PyPowsyblApiHeader.VoltageInitializerObjective;
import com.powsybl.python.commons.PyPowsyblApiHeader.VoltageInitializerStatus;
import com.powsybl.python.dataframe.CDataframeHandler;
import com.powsybl.sensitivity.SensitivityFunctionType;
import org.graalvm.nativeimage.UnmanagedMemory;
import org.graalvm.nativeimage.c.struct.SizeOf;
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
Expand Down Expand Up @@ -99,7 +100,7 @@ public static long doCatch(PyPowsyblApiHeader.ExceptionHandlerPointer exceptionH
}

public static <T extends Enum<?>> T doCatch(PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr,
Supplier<T> supplier) {
Supplier<T> supplier) {
exceptionHandlerPtr.setMessage(WordFactory.nullPointer());
try {
return supplier.get();
Expand Down Expand Up @@ -314,17 +315,26 @@ public static DataframeElementType convert(PyPowsyblApiHeader.ElementType type)
}
}

public static SensitivityFunctionType convert(PyPowsyblApiHeader.SensitivityFunctionType type) {
return switch (type) {
case BRANCH_ACTIVE_POWER_1 -> SensitivityFunctionType.BRANCH_ACTIVE_POWER_1;
case BRANCH_CURRENT_1 -> SensitivityFunctionType.BRANCH_CURRENT_1;
case BRANCH_REACTIVE_POWER_1 -> SensitivityFunctionType.BRANCH_REACTIVE_POWER_1;
case BRANCH_ACTIVE_POWER_2 -> SensitivityFunctionType.BRANCH_ACTIVE_POWER_2;
case BRANCH_CURRENT_2 -> SensitivityFunctionType.BRANCH_CURRENT_2;
case BRANCH_REACTIVE_POWER_2 -> SensitivityFunctionType.BRANCH_REACTIVE_POWER_2;
case BRANCH_ACTIVE_POWER_3 -> SensitivityFunctionType.BRANCH_ACTIVE_POWER_3;
case BRANCH_CURRENT_3 -> SensitivityFunctionType.BRANCH_CURRENT_3;
case BRANCH_REACTIVE_POWER_3 -> SensitivityFunctionType.BRANCH_REACTIVE_POWER_3;
};
}

public static ContingencyContextType convert(PyPowsyblApiHeader.RawContingencyContextType type) {
switch (type) {
case ALL:
return ContingencyContextType.ALL;
case NONE:
return ContingencyContextType.NONE;
case SPECIFIC:
return ContingencyContextType.SPECIFIC;
default:
throw new PowsyblException("Unknown contingency context type : " + type);
}
return switch (type) {
case ALL -> ContingencyContextType.ALL;
case NONE -> ContingencyContextType.NONE;
case SPECIFIC -> ContingencyContextType.SPECIFIC;
};
}

public static PyPowsyblApiHeader.ValidationLevelType convert(ValidationLevel level) {
Expand Down Expand Up @@ -409,11 +419,11 @@ public static byte[] binaryBufferToBytes(ByteBuffer buffer) {
}
}

private static final byte[] ZIP_SIGNATURE = new byte[] {0x50, 0x4B, 0x03, 0x04};
private static final byte[] GZIP_SIGNATURE = new byte[] {0x1F, (byte) 0x8B};
private static final byte[] XZ_SIGNATURE = new byte[] {(byte) 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00};
private static final byte[] BZIP2_SIGNATURE = new byte[] {0x42, 0x5A, 0x68};
private static final byte[] ZSTD_SIGNATURE = new byte[] {0x28, (byte) 0xB5, 0x2F, (byte) 0xFD};
private static final byte[] ZIP_SIGNATURE = new byte[]{0x50, 0x4B, 0x03, 0x04};
private static final byte[] GZIP_SIGNATURE = new byte[]{0x1F, (byte) 0x8B};
private static final byte[] XZ_SIGNATURE = new byte[]{(byte) 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00};
private static final byte[] BZIP2_SIGNATURE = new byte[]{0x42, 0x5A, 0x68};
private static final byte[] ZSTD_SIGNATURE = new byte[]{0x28, (byte) 0xB5, 0x2F, (byte) 0xFD};

private static boolean compareSignature(ByteBuffer buffer, byte[] signature) {
byte[] header = new byte[signature.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ public static void setZones(IsolateThread thread, ObjectHandle sensitivityAnalys
});
}

@CEntryPoint(name = "addBranchFactorMatrix")
public static void addBranchFactorMatrix(IsolateThread thread, ObjectHandle sensitivityAnalysisContextHandle,
CCharPointerPointer branchIdPtrPtr, int branchIdCount,
CCharPointerPointer variableIdPtrPtr, int variableIdCount,
CCharPointerPointer contingenciesIdPtrPtr, int contingenciesIdCount,
CCharPointer matrixIdPtr,
PyPowsyblApiHeader.RawContingencyContextType contingencyContextType,
PyPowsyblApiHeader.SensitivityFunctionType sensitivityFunctionType,
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);
List<String> contingencies = toStringList(contingenciesIdPtrPtr, contingenciesIdCount);
analysisContext.addBranchFactorMatrix(matrixId, branchesIds, variablesIds, contingencies, Util.convert(contingencyContextType), Util.convert(sensitivityFunctionType));
});
}

@CEntryPoint(name = "addBranchFlowFactorMatrix")
public static void addBranchFlowFactorMatrix(IsolateThread thread, ObjectHandle sensitivityAnalysisContextHandle,
CCharPointerPointer branchIdPtrPtr, int branchIdCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ void addPostContingencyBranchFlowFactorMatrix(String matrixId, List<String> bran
addBranchFlowFactorMatrix(matrixId, ContingencyContextType.SPECIFIC, branchesIds, variablesIds, contingencies);
}

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

public void setVariableSets(List<SensitivityVariableSet> variableSets) {
this.variableSets = Objects.requireNonNull(variableSets);
}
Expand Down Expand Up @@ -201,63 +211,74 @@ SensitivityAnalysisResultContext run(Network network, SensitivityAnalysisParamet
}
}

if (matrix.getFunctionType() == SensitivityFunctionType.BRANCH_ACTIVE_POWER_1) {
for (int row = 0; row < rows.size(); row++) {
String variableId = rows.get(row);
Injection<?> injection = getInjection(network, variableId);
for (int column = 0; column < columns.size(); column++) {
String branchId = columns.get(column);
Branch<?> branch = network.getBranch(branchId);
if (branch == null) {
throw new PowsyblException("Branch '" + branchId + "' not found");
}
if (injection != null) {
for (ContingencyContext cCtx : contingencyContexts) {
handler.onFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, branchId,
SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId,
false, cCtx);
switch (matrix.getFunctionType()) {
case BRANCH_ACTIVE_POWER_1 -> {
for (String variableId : rows) {
Injection<?> injection = getInjection(network, variableId);
for (String branchId : columns) {
Branch<?> branch = network.getBranch(branchId);
if (branch == null) {
throw new PowsyblException("Branch '" + branchId + "' not found");
}
} else {
TwoWindingsTransformer twt = network.getTwoWindingsTransformer(variableId);
if (twt != null) {
if (twt.getPhaseTapChanger() == null) {
throw new PowsyblException("Transformer '" + variableId + "' is not a phase shifter");
}
if (injection != null) {
for (ContingencyContext cCtx : contingencyContexts) {
handler.onFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, branchId,
SensitivityVariableType.TRANSFORMER_PHASE, variableId,
SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId,
false, cCtx);
}
} else {
HvdcLine hvdcLine = network.getHvdcLine(variableId);
if (hvdcLine != null) {
TwoWindingsTransformer twt = network.getTwoWindingsTransformer(variableId);
if (twt != null) {
if (twt.getPhaseTapChanger() == null) {
throw new PowsyblException("Transformer '" + variableId + "' is not a phase shifter");
}
for (ContingencyContext cCtx : contingencyContexts) {
handler.onFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, branchId,
SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, variableId,
SensitivityVariableType.TRANSFORMER_PHASE, variableId,
false, cCtx);
}
} else {
if (variableSetsById.containsKey(variableId)) {
HvdcLine hvdcLine = network.getHvdcLine(variableId);
if (hvdcLine != null) {
for (ContingencyContext cCtx : contingencyContexts) {
handler.onFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, branchId,
SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId,
true, cCtx);
SensitivityVariableType.HVDC_LINE_ACTIVE_POWER, variableId,
false, cCtx);
}
} else {
throw new PowsyblException("Variable '" + variableId + "' not found");
if (variableSetsById.containsKey(variableId)) {
for (ContingencyContext cCtx : contingencyContexts) {
handler.onFactor(SensitivityFunctionType.BRANCH_ACTIVE_POWER_1, branchId,
SensitivityVariableType.INJECTION_ACTIVE_POWER, variableId,
true, cCtx);
}
} else {
throw new PowsyblException("Variable '" + variableId + "' not found");
}
}
}
}
}
}
}
} else if (matrix.getFunctionType() == SensitivityFunctionType.BUS_VOLTAGE) {
for (int row = 0; row < rows.size(); row++) {
String targetVoltageId = rows.get(row);
for (int column = 0; column < columns.size(); column++) {
String busVoltageId = columns.get(column);
handler.onFactor(SensitivityFunctionType.BUS_VOLTAGE, busVoltageId,
SensitivityVariableType.BUS_TARGET_VOLTAGE, targetVoltageId, false, ContingencyContext.all());
case BUS_VOLTAGE -> {
for (String targetVoltageId : rows) {
for (String busVoltageId : columns) {
handler.onFactor(SensitivityFunctionType.BUS_VOLTAGE, busVoltageId,
SensitivityVariableType.BUS_TARGET_VOLTAGE, targetVoltageId, false, ContingencyContext.all());
}
}
}
case BRANCH_REACTIVE_POWER_1, BRANCH_REACTIVE_POWER_2, BRANCH_REACTIVE_POWER_3, BRANCH_CURRENT_1, BRANCH_CURRENT_2, BRANCH_CURRENT_3 -> {
for (String targetVoltageId : rows) {
for (String branchId : columns) {
Branch<?> branch = network.getBranch(branchId);
if (branch == null) {
throw new PowsyblException("Branch '" + branchId + "' not found");
}
handler.onFactor(matrix.getFunctionType(), branchId,
SensitivityVariableType.BUS_TARGET_VOLTAGE, targetVoltageId, false, ContingencyContext.all());
}
}
}
}
Expand Down
Loading

0 comments on commit c42b4da

Please sign in to comment.