How to automate PowerFactory studies with Python

PowerFactory studies are automated with its Python API: a script activates a study case, runs the calculation commands (load flow, short circuit, RMS or EMT simulation) in a loop over study cases, outages or values, reads the results and saves tables and graphs. Each guide below solves one study with a script tested on PowerFactory's example projects, and opens the same study in the Workflow Builder, which writes the Python for you from blocks.

Guides, one study each

The pattern every automation follows

Whatever the study, a PowerFactory script does the same seven things. This one is a minimal N-1 loop; the guides fill each step in for their study.

import sys

sys.path.append(r"C:\Program Files\DIgSILENT\PowerFactory 2024\Python\3.12")
import powerfactory

PROJECT = "Nine-bus System"

app = powerfactory.GetApplicationExt()                # 1. connect (GetApplication() inside PowerFactory)
app.ActivateProject(PROJECT)                          # 2. the project, with its active study case
ldf = app.GetFromStudyCase("ComLdf")                  # 3. the command of the study
lines = app.GetCalcRelevantObjects("*.ElmLne")

rows = []
for line in lines:                                    # 4. loop over what the study varies
    line.SetAttribute("outserv", 1)
    try:
        if ldf.Execute() == 0:                        # 5. run, and check it worked
            worst = max(o.GetAttribute("c:loading") for o in lines if o.outserv == 0)
            rows.append((line.loc_name, round(worst, 1)))   # 6. read and keep the results
    finally:
        line.SetAttribute("outserv", 0)               # 7. leave the project as it was

print(rows)
  1. Connect: GetApplicationExt() from your own Python, GetApplication() inside PowerFactory.
  2. Activate the project and study case: they decide the grids, the scenario and the variations.
  3. Get the command from the study case: ComLdf, ComShc, ComInc and ComSim.
  4. Loop over what the study varies: study cases, outages, a dispatch, a fault location, a clearing time.
  5. Run and check: Execute() returns 0 when it worked; anything else is a result to record, not a crash.
  6. Read the results with GetAttribute, by the attribute's name (attribute reference).
  7. Put everything back in finally, because every change is saved in the project.

Write it, or have it written

  • Write the Python yourself with the API: the guides give a working script per study and the attribute reference gives every attribute name of 73 PowerFactory classes, with units.
  • Draw it in the Workflow Builder: wire blocks for the study, the loops and the checks, and download one Python file. It finds the PowerFactory module, connects inside or in engine mode, keeps a log, saves tables and graphs, and puts back every value it changed. Each guide has an "Open in the Builder" button that loads its study as a Quick Start; how to use the Builder shows the rest.

What is worth automating first

  • The same study on many cases: every study case, every operation scenario, every year of a plan.
  • Screening: N-1 outages, fault levels at every busbar, hosting capacity at every busbar.
  • Searches: the largest dispatch that passes, a critical clearing time, a hosting capacity: a few runs with bisection instead of hundreds by hand.
  • Reporting: the same tables and graphs, in the same layout, every time the model changes.

Questions

Can PowerFactory be automated with Python?

Yes. PowerFactory installs a Python module, powerfactory, that reaches every object and command of a project: scripts run inside PowerFactory as Python commands, or from your own Python with PowerFactory in the background.

Does scripting need a special licence?

The licence has to include scripting. Without it, starting PowerFactory from Python fails with ExitError 3010.

Python or DPL?

DPL is PowerFactory's own scripting language and runs only inside it. Python reaches the same objects and adds the Python world: pandas for tables, matplotlib for graphs, SciPy for optimisation.

Do I need to know Python?

Not for the studies the Workflow Builder covers: it writes the script from blocks. Reading the generated file is a good way to learn the API.