Skip to content

Commit

Permalink
LoadFlowComponentResult: added status_text and reference_bus_id
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Jeandemange <damien.jeandemange@artelys.com>
  • Loading branch information
jeandemanged committed Feb 11, 2024
1 parent 5b647dd commit fce83c3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ print(results)
```

```bash
[LoadFlowComponentResult(component_num=0, status=CONVERGED, iteration_count=3, slack_bus_id='VL4_0', slack_bus_active_power_mismatch=-0.006081)]
[LoadFlowComponentResult(connected_component_num=0, synchronous_component_num=0, status=CONVERGED, status_text=CONVERGED, iteration_count=3, reference_bus_id='VL4_0', slack_bus_id='VL4_0', slack_bus_active_power_mismatch=-0.006081, distributed_active_power=-600.0)]
```

We can now get buses data (like any other network elements) as a [Pandas](https://pandas.pydata.org/) dataframe:
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,18 @@ PYBIND11_MODULE(_pypowsybl, m) {
.def_property_readonly("status", [](const loadflow_component_result& r) {
return static_cast<pypowsybl::LoadFlowComponentStatus>(r.status);
})
.def_property_readonly("status_text", [](const loadflow_component_result& r) {
return r.status_text;
})
.def_property_readonly("iteration_count", [](const loadflow_component_result& r) {
return r.iteration_count;
})
.def_property_readonly("slack_bus_id", [](const loadflow_component_result& r) {
return r.slack_bus_id;
})
.def_property_readonly("reference_bus_id", [](const loadflow_component_result& r) {
return r.reference_bus_id;
})
.def_property_readonly("slack_bus_active_power_mismatch", [](const loadflow_component_result& r) {
return r.slack_bus_active_power_mismatch;
})
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/pypowsybl-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ typedef struct loadflow_component_result_struct {
int connected_component_num;
int synchronous_component_num;
int status;
char* status_text;
int iteration_count;
char* reference_bus_id;
char* slack_bus_id;
double slack_bus_active_power_mismatch;
double distributed_active_power;
Expand Down
2 changes: 1 addition & 1 deletion docs/user_guide/loadflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ included in the computation:
.. doctest::

>>> results
[ComponentResult(connected_component_num=0, synchronous_component_num=0, status=CONVERGED, iteration_count=3, slack_bus_id='VLHV1_0', slack_bus_active_power_mismatch=-606.5596837558763, distributed_active_power=0.0)]
[ComponentResult(connected_component_num=0, synchronous_component_num=0, status=CONVERGED, status_text=CONVERGED, iteration_count=3, reference_bus_id='VLHV1_0', slack_bus_id='VLHV1_0', slack_bus_active_power_mismatch=-606.5596837558763, distributed_active_power=0.0)]

Component results provides general information about the loadflow: was it successful ? how many iterations did
it need ? what's the remaining active power imbalance ? For example, let's have a look at the imbalance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,24 @@ public interface LoadFlowComponentResultPointer extends PointerBase {
@CField("status")
void setStatus(int status);

@CField("status_text")
CCharPointer getStatusText();

@CField("status_text")
void setStatusText(CCharPointer statusText);

@CField("iteration_count")
int getIterationCount();

@CField("iteration_count")
void setIterationCount(int iterationCount);

@CField("reference_bus_id")
CCharPointer getReferenceBusId();

@CField("reference_bus_id")
void setReferenceBusId(CCharPointer referenceBusId);

@CField("slack_bus_id")
CCharPointer getSlackBusId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public static void freeLoadFlowComponentResultPointer(IsolateThread thread, PyPo
PyPowsyblApiHeader.ExceptionHandlerPointer exceptionHandlerPtr) {
doCatch(exceptionHandlerPtr, () -> {
for (int i = 0; i < componentResultArrayPtr.getLength(); i++) {
UnmanagedMemory.free(componentResultArrayPtr.getPtr().addressOf(i).getSlackBusId());
PyPowsyblApiHeader.LoadFlowComponentResultPointer loadFlowComponentResultPointer = componentResultArrayPtr.getPtr().addressOf(i);
UnmanagedMemory.free(loadFlowComponentResultPointer.getStatusText());
UnmanagedMemory.free(loadFlowComponentResultPointer.getReferenceBusId());
UnmanagedMemory.free(loadFlowComponentResultPointer.getSlackBusId());
}
freeArrayPointer(componentResultArrayPtr);
});
Expand Down Expand Up @@ -126,7 +129,9 @@ private static PyPowsyblApiHeader.ArrayPointer<PyPowsyblApiHeader.LoadFlowCompon
ptr.setConnectedComponentNum(componentResult.getConnectedComponentNum());
ptr.setSynchronousComponentNum(componentResult.getSynchronousComponentNum());
ptr.setStatus(componentResult.getStatus().ordinal());
ptr.setStatusText(CTypeUtil.toCharPtr(componentResult.getStatusText()));
ptr.setIterationCount(componentResult.getIterationCount());
ptr.setReferenceBusId(CTypeUtil.toCharPtr(componentResult.getReferenceBusId()));
ptr.setSlackBusId(CTypeUtil.toCharPtr(componentResult.getSlackBusId()));
ptr.setSlackBusActivePowerMismatch(componentResult.getSlackBusActivePowerMismatch());
ptr.setDistributedActivePower(componentResult.getDistributedActivePower());
Expand Down
4 changes: 4 additions & 0 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,14 @@ class LoadFlowComponentResult:
@property
def distributed_active_power(self) -> float: ...
@property
def reference_bus_id(self) -> str: ...
@property
def slack_bus_id(self) -> str: ...
@property
def status(self) -> LoadFlowComponentStatus: ...
@property
def status_text(self) -> str: ...
@property
def synchronous_component_num(self) -> int: ...

class LoadFlowComponentResultArray:
Expand Down
12 changes: 12 additions & 0 deletions pypowsybl/loadflow/impl/component_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def status(self) -> ComponentStatus:
"""Status of the loadflow for this component."""
return self._res.status

@property
def status_text(self) -> str:
"""Status text of the loadflow for this component."""
return self._res.status_text

@property
def connected_component_num(self) -> int:
"""Number of the connected component."""
Expand All @@ -41,6 +46,11 @@ def iteration_count(self) -> int:
"""The number of iterations performed by the loadflow."""
return self._res.iteration_count

@property
def reference_bus_id(self) -> str:
"""ID of the (angle) reference bus used for this component."""
return self._res.reference_bus_id

@property
def slack_bus_id(self) -> str:
"""ID of the slack bus used for this component."""
Expand All @@ -61,7 +71,9 @@ def __repr__(self) -> str:
f"connected_component_num={self.connected_component_num!r}" \
f", synchronous_component_num={self.synchronous_component_num!r}" \
f", status={self.status.name}" \
f", status_text={self.status_text}" \
f", iteration_count={self.iteration_count!r}" \
f", reference_bus_id={self.reference_bus_id!r}" \
f", slack_bus_id={self.slack_bus_id!r}" \
f", slack_bus_active_power_mismatch={self.slack_bus_active_power_mismatch!r}" \
f", distributed_active_power={self.distributed_active_power!r}" \
Expand Down
2 changes: 2 additions & 0 deletions tests/test_loadflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ def test_run_lf():
results = lf.run_ac(n)
assert 1 == len(results)
assert lf.ComponentStatus.CONVERGED == results[0].status
assert 'CONVERGED' == results[0].status_text
assert 0 == results[0].connected_component_num
assert 0 == results[0].synchronous_component_num
assert 'VL1_0' == results[0].reference_bus_id
assert 'VL1_0' == results[0].slack_bus_id
assert abs(results[0].slack_bus_active_power_mismatch) < 0.01
assert 3 == results[0].iteration_count
Expand Down

0 comments on commit fce83c3

Please sign in to comment.