How to automate PSCAD studies with Python

PSCAD studies are automated with its Python Automation Library, mhi.pscad: a script opens a case, changes components in it, builds and runs it in the time domain, reads the waveforms it recorded and works out what the study asks for. Each guide below covers one kind of study and opens the same study in the Workflow Builder, which writes the whole Python file for you from blocks.

Guides, one study each

The pattern every automation follows

PSCAD has one calculation, the EMT run, so every study is the same loop around it. This one times a fault on a Timed Fault Logic block over four durations; the guides fill each step in.

import mhi.pscad

pscad = mhi.pscad.connect()                    # 1. connect (launch() starts a PSCAD)
project = pscad.project("my_case")             # 2. the case, already in the workspace
fault = project.find_all("master:tfaultn")[0]  # 3. the component the study changes

for duration in (0.08, 0.12, 0.16, 0.20):      # 4. the loop
    fault.parameters(DF="%g [s]" % duration)   # 5. change the model
    project.run()                              # 6. build if needed, then run in the time domain
    # 7. read the channels from the .out files, check them, record a row
  1. Connect: mhi.pscad.connect() to the PSCAD that is open, launch() to start one.
  2. Open the case: a .pscx in the workspace, with the libraries its components come from.
  3. Find the components the study changes, by definition and by Name (component reference).
  4. Loop over what the study varies: a fault duration, a parameter, a layer, a substitution set.
  5. Change the model by writing parameters. A fault or a switching is a component already drawn in the case: there is no event list.
  6. Build and run: the case compiles into Fortran, then EMTDC runs it.
  7. Read, check, record: the output channels come back as waveforms; a peak, a nadir, a THD or a damping ratio is worked out from them and written into a table. Then put every parameter back.

Write it, or have it written

  • Write the Python yourself with the Automation Library: the guides show the calls per study, and the component reference lists every parameter of 383 Master Library definitions, read out of PSCAD 5.0.0, with units and the symbols a Choice takes.
  • Draw it in the Workflow Builder: wire blocks for the study, the loops and the checks, and download one Python file. It connects or starts PSCAD, 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.

What is worth automating first

  • Many runs of one case: every fault duration, every parameter value, every network arrangement held in a layer.
  • Searches: a critical clearing time takes about eight runs by bisection instead of thirty by hand.
  • Long start-ups: a snapshot taken once a case has settled saves simulating the same seconds again for every branch.
  • Reading the waveforms: THD, RMS, sequence components and damping out of every run, written into one table in the same layout every time.

Questions

Can PSCAD be automated with Python?

Yes. The PSCAD Automation Library, pip install mhi-pscad, drives PSCAD from any Python 3.7 or newer: open a workspace and a case, read and write component parameters, build, run and read the result files.

Does scripting need a separate licence?

No separate scripting licence: the automation server is part of PSCAD. PSCAD still needs a licence certificate before a run will start.

Can a script run several cases at once?

Yes, through a simulation set: PSCAD runs its cases in volleys, several at a time. A script that only loops runs one case after the other.

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 library.