⚡ PowerFactory Scripter
Samples & Guides

Samples & Guides

Worked examples showing how to configure the tool for common power systems studies.

01

Capacitor Bank Sizing: Voltage Regulation

↓ JSON
1 Objective

A system with a high concentration of inductive motors is causing the bus voltage to sag below acceptable limits. The goal is to find the minimum reactive power (MVAr) of a shunt capacitor that restores the bus voltage within the standard operating range of 0.9–1.1 p.u.

2 Assumptions
  • Monitored bus element: MainBus.ElmTerm
  • Capacitor element: Cap.ElmShnt
  • Reactive power sweep: 0 – 10 MVAr in 0.5 MVAr steps
  • Voltage standard: 0.9 – 1.1 p.u.
3 What to Input
Initialisation
Input Variables
Name Object Attribute Lower Upper Step
Output Variables
Scalar
Running the Code
4 Click ⚡ Generate Code in the tool.
5 Click Copy (or download as .py).
6
Paste the code into your Python environment and run it.
Make sure PowerFactory is installed and the API path is configured correctly.
7 Expected Output

The script writes a CSV file to your output directory, e.g. results.csv:

Q_cap,Converge?,bus_voltage
0.0,True,0.873
0.5,True,0.891
1.0,True,0.908
1.5,True,0.925
2.0,True,0.943
2.5,True,0.961
3.0,True,1.024
3.5,True,1.041
...

Open the CSV in Excel or Python (pandas) to plot voltage vs. capacitor size and identify the minimum capacitor rating that satisfies your voltage standard.

02

Harmonic Filter Sizing: THD Minimisation

↓ JSON
1 Objective

The system Total Harmonic Distortion (THD) exceeds the permissible limit. The goal is to find the optimal capacitance, inductance, and resistance of a passive harmonic filter that minimises THD, subject to a maximum resistance constraint.

2 Assumptions
  • Filter element: Filter.ElmFilter
  • Parameters to optimise: capacitance (capa), inductance (rrea), resistance (rres)
  • Target: minimise THD. Verify result manually against your standard (e.g. < 3%)
  • Constraint: resistance ≤ 0.05 Ω
3 What to Input
Initialisation
Input Variables
Name Object Attribute Lower Upper Step
Output Variables
Scalar
Optimisation Settings
Constraints
Variable Operator Value
Running the Code
4 Click ⚡ Generate Code in the tool.
5 Click Copy (or download as .py).
6
Paste the code into your Python environment and run it.
Bayesian optimisation may take a few minutes. Progress is printed to the console if Use progress bar is enabled.
7 Expected Output

The script prints the best parameters found by the optimiser:

Optimisation complete.
Best parameters:
  C_f (capa) = 4.72e-04
  L_f (rrea) = 1.23e-02
  R_f (rres) = 0.038
Best objective (thd) = 2.61

Verify the THD value against your standard (e.g. < 3%) manually. The script does not enforce the limit; it only minimises THD within the parameter bounds and constraints you provide. You may modify the code to do this automatically.

03

Synchronous Condenser Sizing: Fault Ride-Through & Inertial Response

↓ JSON
1 Objective

A grid-connected synchronous condenser must be sized to satisfy two dynamic criteria simultaneously: (1) Fault Ride-Through (FRT), where the machine must stay in synchronism and provide continuous reactive current support throughout a major fault without pole-slipping; and (2) Inertial Response, where the rotating mass must be large enough to limit the frequency nadir to an acceptable level following the sudden loss of a generator. The script sweeps the number of parallel condenser units and records the worst-case dynamic response for each size, automatically identifying the minimum installation that meets both criteria.

2 Assumptions
  • Synchronous condenser element: SynCon.ElmSym
  • Monitored bus: MainBus.ElmTerm
  • 3-phase fault event pre-configured in the PowerFactory study case dynamic event list (applied at t = 1 s, cleared at t = 1.15 s)
  • Simulation window: 0–30 s (fault application, clearing, and full post-fault recovery)
  • FRT pass criterion: bus_voltage_nadir ≥ 0.85 p.u. and out_of_step = 0
  • Inertia pass criterion: rotor_speed_min ≥ 0.97 p.u. (≥ 48.5 Hz on a 50 Hz base)
  • Combined pass/fail evaluated automatically by the sizing_ok custom calculation output
3 What to Input
Initialisation
Input Variables
Name Object Attribute Lower Upper Step
Output Variables
Timeseries
Output graph (Time vs variable, saved as .png in {output_dir}/graph)
Timeseries
Timeseries
Output graph (Time vs variable, saved as .png in {output_dir}/graph)
Timeseries
Custom
Custom Function
def sizing_ok(bus_voltage_nadir, rotor_speed_min, out_of_step):
    return bus_voltage_nadir >= 0.85 and rotor_speed_min >= 0.97 and out_of_step == 0

Arguments must match names of other defined input or output variables. Returns True if all sizing criteria pass, False otherwise.

Running the Code
4 Click ⚡ Generate Code in the tool.
5 Click Copy (or download as .py).
6
Paste the code into your Python environment and run it.
Ensure the 3-phase fault event is defined in the PowerFactory study case before running. Each RMS simulation covers 30 s, so expect the full sweep (10 runs) to take several minutes.
7 Expected Output

The script writes a summary CSV to your output directory, e.g. results.csv:

ngnum,Converge?,bus_voltage_nadir,synccon_q_peak,rotor_speed_min,out_of_step,sizing_ok
1,True,0.71,47.3,0.961,1,False
2,True,0.74,94.1,0.966,1,False
3,True,0.76,140.8,0.970,0,False
4,True,0.79,187.2,0.973,0,True
5,True,0.82,233.6,0.976,0,True
...
10,True,0.91,466.1,0.984,0,True

The minimum passing size is the first row where sizing_ok = True; here, that is 4 parallel units. Time-domain waveform plots for bus_voltage_nadir and rotor_speed_min are saved as .png files in {output_dir}/graph/ for each scenario.

04

N-1 Line Contingency: Wildcard Approach

↓ JSON
1 Objective

Assess the steady-state impact of losing any single line in the network. The Contingency problem type resolves all matching lines at runtime via a wildcard query, with no manual enumeration or spreadsheet required. The script runs a base case followed by one load flow per line, recording bus voltages and line loadings alongside convergence status for every scenario.

2 Assumptions
  • N-1 contingency mode will be used
  • Only lines above 66 kV will be considered
  • Bus voltage and line loading of all buses and lines will be monitored
3 Configuration

Select Contingency as the Problem Type. The Input Variables section is hidden because the Contingency Config section replaces it.

Initialisation
Contingency Config
# Element Query Filter Attribute Op Filter Value
1
4 Output Variables
Output Variables
Scalar
Scalar
Running the Code
5 Click ⚡ Generate Code in the tool.
6 Click Copy (or download as .py).
7
Paste the code into your Python environment and run it.
The script runs one load flow per line plus the base case. No Excel file or code changes needed regardless of network size.
8 Expected Output

The script writes a summary CSV to your output directory, e.g. results.csv. The first two columns identify the contingency and its convergence status; subsequent columns are the output values, each prefixed with the variable name and the element's loc_name (e.g. bus_voltage_Bus_HV1, loading_Line_A):

Contingent Element 1,Converge?,bus_voltage_Bus_HV1,bus_voltage_Bus_HV2,bus_voltage_Bus_HV3,loading_Line_A,loading_Line_B,loading_Line_C,loading_Line_D
Base Case,True,1.023,0.998,1.005,42.1,38.5,65.3,71.2
Line_A,True,0.887,1.012,0.995,65.3,88.7,45.2,80.5
Line_B,True,1.012,0.889,1.003,88.7,55.2,40.1,75.3
Line_C,False,,,,,,,,
Line_D,True,0.991,1.003,0.998,48.6,71.9,82.3,95.1

Converge? = False for Line_C indicates the load flow did not converge with that line tripped. The row is still recorded but output columns are left empty.

Frequently Asked Questions

How do I find the API Path?

The API Path points to the PowerFactory Python API folder. It is located inside a Python subfolder of your PowerFactory installation, with a sub-folder for each available Python version. For example:

C:\Program Files\DIgSILENT\PowerFactory 2024\Python\3.10\

Choose the sub-folder that matches the Python version you are using to run your script.

What does each Study Type do?
Study Type PF Command Use to Analyse ...
Steady State Load Flow (ldf) Steady state voltages, currents, loading, etc
Dynamic RMS RMS simulation (rms) Fault response, generator dynamics, inverter dynamics, etc
Dynamic EMT EMT simulation (emt) Switching transients, power electronics, detailed waveforms, etc
Harmonic Harmonic load flow (hdf) Total harmonic distortion (THD), individual harmonics, etc
Python File vs. Notebook: which should I use?

Python File (.py): Best for production use, scheduled runs, or when you want to import the script as a module. Run from the terminal or your IDE.

Notebook (.ipynb): Best for exploratory work, step-by-step debugging, or when you want inline plots and tables alongside the code. Open in Jupyter.