[bug]Transformers Lost During From_ppc() Conversion - To_ppc() Incorrectly Sets Transformer Ratio To 1.0 Instead Of Turns Ratio
Introduction
This article addresses a critical bug encountered in pandapower, a widely-used open-source tool for power system modeling and analysis. The bug specifically involves the loss of transformers during the conversion process between the pandapower network format and the power flow calculation (PPC) format. This issue arises due to an incorrect calculation of the transformer ratio in the to_ppc()
function when dealing with transformers having a zero-degree phase shift. The incorrect transformer ratio leads to the from_ppc()
function failing to recognize these transformers, effectively causing them to disappear from the network model. This problem can have significant implications for power system simulations and analyses, especially those involving networks with transformers operating at zero phase shift. Understanding the root cause and impact of this bug is crucial for users of pandapower to ensure the accuracy and reliability of their power system models.
Bug Report Checklist
Before diving into the specifics of the bug, it's important to acknowledge the systematic approach taken to identify and report the issue. The following steps were meticulously followed to ensure a comprehensive and reproducible bug report:
- [x] Searched the issues page for similar reports
- [x] Read the relevant sections of the documentation
- [x] Browsed the tutorials and tests for useful code snippets and examples of use
- [x] Reproduced the issue after updating with
pip install --upgrade pandapower
(orgit pull
) - [x] Tried basic troubleshooting (if a bug/error) like restarting the interpreter and checking the pythonpath
This thorough process underscores the commitment to responsible bug reporting, ensuring that the issue is well-documented and can be effectively addressed by the pandapower development team. By following these steps, the bug reporter has laid a solid foundation for understanding and resolving the problem. This systematic approach is vital for maintaining the integrity and reliability of pandapower as a power system analysis tool.
Reproducible Example
To clearly illustrate the bug, a reproducible example was created using pandapower and its built-in network models. This example demonstrates how transformers with a zero-degree phase shift are lost during the conversion between the pandapower network format and the power flow calculation (PPC) format. The code snippet below provides a step-by-step demonstration of the issue:
import pandapower as pp
import pandapower.networks as nw

print("Original built-in network (works):")
net_working = nw.simple_four_bus_system()
print(f"Shift degree: net_working.trafo.iloc[0].shift_degree}°") # 150.0°
ppc_net = pp.converter.to_ppc(net_working, check_connectivity=True, mode='pf', init='flat')
net_converted = pp.converter.from_ppc(ppc_net, f_hz=50)
print(f"Transformers after conversion") # 1 (works)
print("\nSame network with zero shift (breaks):")
net_broken = nw.simple_four_bus_system()
net_broken.trafo.loc[0, 'shift_degree'] = 0.0 # Change to zero shift
ppc_net = pp.converter.to_ppc(net_broken, check_connectivity=True, mode='pf', init='flat')
net_converted = pp.converter.from_ppc(ppc_net, f_hz=50)
print(f"Transformers after conversion: {len(net_converted.trafo)}") # 0 (BUG!)
for i, branch in enumerate(ppc_net['branch']):
ratio, angle = branch[8], branch[9]
print(f"Branch i}, angle=angle")
Detailed Explanation
The code first initializes a built-in network from pandapower, the simple_four_bus_system
. This network inherently includes a transformer with a non-zero phase shift. The code proceeds to convert this network to the PPC format using pp.converter.to_ppc()
and then back to the pandapower format using pp.converter.from_ppc()
. As expected, the transformer is correctly preserved in this round trip.
Next, the code modifies the same network, specifically setting the shift_degree
of the transformer to 0.0. This is where the bug manifests. Upon converting this modified network to PPC format and back, the transformer is lost. The output clearly shows that the number of transformers in the converted network (net_converted.trafo
) is 0, indicating the transformer has disappeared.
To further diagnose the issue, the code iterates through the branches in the PPC network (ppc_net['branch']
). It prints the ratio and angle for each branch, revealing that all branches have a ratio of 1.0 and an angle of 0.0. This is the crux of the problem: the to_ppc()
function incorrectly sets the transformer ratio to 1.0 when the phase shift is zero, preventing from_ppc()
from recognizing it as a transformer.
This example meticulously demonstrates the bug, providing a clear and concise way for others to reproduce and understand the issue. The use of a built-in network and minimal code modifications ensures that the example is easily accessible and applicable to a wide range of pandapower users.
Issue Description and Traceback
The core of the bug lies in the handling of transformers with a zero-degree phase shift during the conversion process. Specifically, transformers with shift_degree=0
are lost during the to_ppc() → from_ppc()
round-trip conversion. This occurs because the to_ppc()
converter incorrectly sets the transformer turns ratio to 1.0 instead of the actual voltage ratio. Consequently, the from_ppc()
function fails to identify these elements as transformers since it relies on both the ratio and angle to correctly classify network components.
Root Cause Analysis
to_ppc()
Bug: The primary issue is within theto_ppc()
function. Transformers should have a ratio equal to the high-voltage side voltage (vn_hv_kv
) divided by the low-voltage side voltage (vn_lv_kv
). For instance, a 10kV/0.4kV transformer should have a ratio of 10.0/0.4 = 25.0. However,to_ppc()
incorrectly sets this ratio to 1.0 when the phase shift is zero.from_ppc()
Logic: Thefrom_ppc()
function uses a specific logic to identify transformers. It only recognizes elements as transformers if(ratio ≠ 0 AND ratio ≠ 1.0) OR angle ≠ 0
. This condition is designed to distinguish transformers from other network components. However, when the ratio is incorrectly set to 1.0 and the angle is 0.0 (due to zero phase shift), the function fails to identify the element as a transformer.- Result: Zero-Shift Transformers Are Lost: The combination of the incorrect ratio calculation in
to_ppc()
and the identification logic infrom_ppc()
results in zero-shift transformers being lost during the conversion process. This means that any power system model relying on accurate representation of such transformers will produce incorrect results.
Future Warning
During the conversion process, a FutureWarning
is also raised, indicating a potential issue with data type compatibility in pandas. This warning suggests that setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. The specific line of code triggering this warning is:
branch_lookup.loc[is_trafo, "element"] = idx_trafo
This warning, while not directly causing the transformer loss, highlights a potential area for future improvement in pandapower's data handling.
Impact Assessment
The loss of transformers during conversion can have significant consequences for power system analysis. Transformers are critical components for voltage regulation and power flow control. If they are not correctly represented in the model, simulations may yield inaccurate results, leading to flawed decision-making in grid planning and operation. Specifically, networks with transformers operating at zero phase shift are at high risk of producing incorrect simulation results due to this bug. Therefore, addressing this issue is crucial to maintain the reliability and accuracy of pandapower as a power system analysis tool.
Expected Behavior
The expected behavior is that transformers should be accurately represented during the conversion process between pandapower's network format and the PPC format. Specifically, a 10kV/0.4kV transformer should have a ratio of 25.0 in the PPC format, reflecting the actual voltage transformation ratio. This accurate representation is crucial for the integrity of power system simulations and analyses. When the transformer ratio is correctly calculated, the from_ppc()
function can accurately identify and restore the transformer in the converted network, ensuring that no network components are lost during the round-trip conversion.
Discrepancy Between Expected and Actual Behavior
The actual behavior deviates significantly from this expectation. Instead of the correct ratio of 25.0, all branches in the PPC format are shown with a ratio of 1.0 when the transformer has a zero-degree phase shift. This incorrect ratio, coupled with the zero-degree angle, makes the zero-shift transformers unidentifiable by the from_ppc()
function. As a result, these transformers are effectively lost during the conversion process, leading to an inaccurate representation of the power system network. This discrepancy highlights a critical flaw in the to_ppc()
function's handling of transformers with zero phase shift, emphasizing the need for a corrective solution to ensure accurate power system modeling.
Installed Versions
To provide a comprehensive context for the bug report, the installed versions of key software packages are listed below. These version details are crucial for reproducing the bug and ensuring that any proposed solutions are compatible with the user's environment. The following versions were used during the bug identification and reporting process:
Package Versions:
- Python version: 3.12.9
- pandas version: 2.2.3
- networkx version: 3.4.2
- scipy version: 1.13.1
- numpy version: 1.26.0
- packaging version: 24.2
- tqdm version: 4.67.1
- deepdiff version: 8.2.0
Operating System:
- OS: macOS 14.6.1 (Darwin 23.6.0)
The specified versions include Python 3.12.9, pandas 2.2.3, networkx 3.4.2, scipy 1.13.1, and numpy 1.26.0, among others. These packages are essential dependencies for pandapower, and their versions can influence the behavior of the software. Additionally, the operating system information (macOS 14.6.1) is provided, as platform-specific issues can sometimes arise. By documenting these details, the bug report ensures that developers have the necessary information to replicate the issue and develop targeted solutions. This level of detail is vital for efficient bug resolution and maintaining the reliability of pandapower across different environments.
Conclusion
In conclusion, the identified bug in pandapower, where transformers with zero-degree phase shift are lost during the to_ppc() → from_ppc()
conversion, poses a significant challenge to accurate power system modeling. The root cause lies in the incorrect calculation of the transformer ratio within the to_ppc()
function, leading to the from_ppc()
function failing to recognize these transformers. This issue can result in flawed simulation outcomes, particularly in networks relying on the correct representation of zero-shift transformers. Addressing this bug is crucial for maintaining the integrity and reliability of pandapower as a power system analysis tool.
The detailed bug report, including the reproducible example and version information, provides a solid foundation for developers to investigate and implement a solution. By rectifying the ratio calculation in to_ppc()
and ensuring that from_ppc()
can correctly identify transformers with a zero-degree phase shift, pandapower can continue to serve as a dependable resource for power system engineers and researchers. The systematic approach to identifying and reporting this bug underscores the importance of thorough testing and community collaboration in maintaining the quality of open-source software. Future efforts should focus on implementing comprehensive test cases that specifically address transformers with various phase shifts to prevent similar issues from arising.