A professional-grade Python tool for optimizing static generators in PowerFactory through N-1 contingency analysis
Features β’ Installation β’ Usage β’ Results β’ Architecture
This tool automates the process of finding the maximum safe power injection at each substation in a PowerFactory network without exceeding loading limits. It performs comprehensive N-1 contingency analysis, handles reactive power limits, and provides detailed optimization results with professional visualizations.
- Automated N-1 Contingency Analysis with configurable thresholds
- Reactive Power Limit Enforcement for realistic power flow calculations
- Convergence Detection and intelligent handling of numerical issues
- Real-time Progress Monitoring with detailed iteration logs
- Professional Results Export with timestamped CSV files
- Interactive Visualizations with bar charts and statistical summaries
π RESUMEN ESTADΓSTICO:
β’ Total de subestaciones: 15
β’ Potencia mΓ‘xima total: 2,450 MW
β’ Potencia promedio: 163.3 MW
β’ Potencia mΓ‘xima individual: 358 MW
β’ Potencia mΓnima individual: 89 MW
π LΓNEAS CRΓTICAS:
β’ Line 03 - 04: 3 subestaciΓ³n(es)
β’ Line 05 - 06: 2 subestaciΓ³n(es)
β’ Line 16 - 17: 2 subestaciΓ³n(es)
PowerFactory-Generator-Optimization/
βββ π src_code/ # Core business logic
β βββ __init__.py # Package exports
β βββ pf_env.py # PowerFactory environment setup
β βββ generators.py # Generator lifecycle management
β βββ contingency.py # N-1 analysis & optimization
β βββ csv_parser.py # Results parsing & processing
βββ π notebooks/ # Interactive workflow
β βββ max_contingency_job.ipynb # Main optimization notebook
β βββ Resultados.csv # Generated results
βββ π reference_code/ # Reference implementations
βββ π my_env/ # Python virtual environment
βββ π pf_project/ # PowerFactory project files
βββ requirements.txt # Python dependencies
βββ .gitignore # Git ignore rules
βββ README.md # This documentation
| Module | Purpose | Key Functions |
|---|---|---|
pf_env.py |
Environment setup | pf_enviroment(), initialize_powerfactory() |
generators.py |
Generator management | create_static_generator(), update_generator_power() |
contingency.py |
Optimization engine | optimize_generators_for_substations() |
csv_parser.py |
Data processing | parse_contingency_results() |
- PowerFactory 2021 SP2 (or compatible version)
- Python 3.9 (PowerFactory requirement)
- Valid PowerFactory License
-
Clone the repository
git clone <repository-url> cd PowerFactory-Generator-Optimization
-
Install dependencies
pip install -r requirements.txt
-
Verify PowerFactory installation
- Ensure PowerFactory is installed and licensed
- Note the Python path:
C:\Program Files\DIgSILENT\PowerFactory 2021 SP2\Python\3.9
-
Open Jupyter Notebook
jupyter notebook notebooks/max_contingency_job.ipynb
-
Configure parameters in the first cell:
# Substations to analyze substations = ["bonao3", "canabacoa", "guayubin"] # Optimization parameters initial_potencia = 1 # MW factor_potencia = 0.95 # Power factor max_cargabilidad = 110 # % loading limit threshold_inconvergence = 10 # % convergence threshold
-
Run the optimization workflow cell by cell
-
View results in the final visualization cell
# Configure PowerFactory connection
dig_path = r'C:\Program Files\DIgSILENT\PowerFactory 2021 SP2\Python\3.9'
project_name = "Your Project Name"
study_case = "1. Power Flow"
operation_scenario = "Your Scenario"# Fine-tune optimization behavior
INITIAL_POTENCIA = 1 # Starting power (MW)
FACTOR_POTENCIA = 0.95 # Power factor
MAX_CARGABILIDAD = 110 # Maximum line loading (%)
THRESHOLD_INCONVERGENCE = 10 # Convergence jump threshold (%)The tool automatically generates timestamped results files:
optimization_results_20241215_143022.csv
| Column | Description | Example |
|---|---|---|
Subestacion |
Substation name | "bonao3" |
Potencia_Maxima |
Maximum safe power (MW) | 358 |
Linea_Critica |
Limiting line | "Line 03 - 04" |
Cargabilidad_Maxima |
Maximum loading (%) | 110.03 |
- Horizontal bar charts sorted by power capacity
- Value labels showing exact MW values
- Statistical summaries with key metrics
- Critical line analysis identifying bottlenecks
- Environment Setup: Initialize PowerFactory connection
- Project Activation: Load project, study case, and scenario
- Generator Creation: Create static generator at each substation
- Iterative Optimization:
- Run power flow with reactive power limits
- Execute N-1 contingency analysis
- Check line loading constraints
- Increment power until limit reached
- Results Export: Save timestamped CSV with comprehensive data
- Reactive Power Limits:
iopt_lim = 1ensures realistic power flow - Convergence Detection: Handles numerical instabilities intelligently
- Clean Output: Removed debug messages for professional presentation
- Error Handling: Robust error handling with graceful degradation
- Memory Efficient: Processes one substation at a time
- Cleanup: Automatic generator removal after each test
- Progress Tracking: Real-time iteration monitoring
| Issue | Solution |
|---|---|
| PowerFactory Connection Failed | Verify PowerFactory is running and licensed |
| Import Errors | Check Python path and install dependencies |
| Generator Creation Failed | Verify bus names and folder permissions |
| Convergence Issues | Adjust THRESHOLD_INCONVERGENCE parameter |
Enable detailed logging by modifying print statements in source code.
from src_code import pf_enviroment, initialize_powerfactory
# Initialize PowerFactory environment
pf_enviroment(dig_path)
app = initialize_powerfactory()from src_code import create_static_generator, update_generator_power
# Create and manage generators
generator = create_static_generator(app, network_data, hoja, substation, power, pf)
update_generator_power(generator, new_power, pf)from src_code import optimize_generators_for_substations
# Run complete optimization
results = optimize_generators_for_substations(
app=app,
substations=substations,
network_data=network_data,
hoja=hoja,
initial_potencia=1,
factor_potencia=0.95,
max_cargabilidad=110,
threshold_inconvergence=10
)- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 style guidelines
- Add comprehensive docstrings
- Test with PowerFactory before submitting
- Update documentation for new features
This project is provided for educational and research purposes. Please ensure compliance with DIgSILENT PowerFactory licensing terms.
| Type | Resource |
|---|---|
| PowerFactory API | DIgSILENT Documentation |
| Python Issues | Python Documentation |
| Tool Issues | Create an issue in this repository |
-
v2.0.0 (Current)
- β
Removed unused modules (
io_utils.py,plotting.py) - β Cleaned debug output for professional presentation
- β Added automatic CSV export with timestamps
- β Enhanced visualization with statistical summaries
- β Improved error handling and convergence detection
- β Streamlined project structure
- β
Removed unused modules (
-
v1.0.0 (Legacy)
- Initial release with modular design
Built with β€οΈ for PowerFactory optimization
Professional-grade tools for power system analysis