1. Python
Power systems analysis in PSS®E using
In the Name of God
Isfahan University of Technology
Department of Electrical & Computer Engineering
Power Systems Analysis Lab.
programming language
1
2. Python?
• Name of a kind of snakes
• Developed By Guido van Rossum at CWI in the early 1990s in the Netherlands
Download it from http://www.python.org
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
2
3. Why Python?
Simple
Easy to Learn
Free and Open Source
High-level language
Portable
Interpreted
Object Oriented
extensible
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
3
4. How to programming Python?
Some Python interpreters:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
4
5. Getting started with Python
1- Command line mode
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
5
6. Getting started with Python
2- Script mode
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
6
7. Getting started with Python
3-Python syntax colors
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
7
8. Getting started with Python
4- Blocks of Code
Note: Dots indicates blank spaces before the code !!
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
..two spaces means this is part of block 1
..two spaces means this is still a part of block 1
....four spaces means we just started block number 2
....four spaces means we are still in block number 2
........eight spaces means we started block number 3
....four spaces means we want this code to be grouped with block 2
........eight spaces for block 3
8
9. Getting started with Python
5- Operators
The most common operators are:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
9
10. Getting started with Python
6- Using Python as a Calculator
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
10
11. Getting started with Python
7- Strings
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
11
12. Getting started with Python
7- Strings
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
12
13. Getting started with Python
8- Variables
You can name a variable almost anything and use symbols like the _underscore !
But don’t use these words:
Note: No need to define variable Types! The first assignment to a variable creates it.
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
13
14. Getting started with Python
9- Logical operators and Boolean data types
The words “and, or,” and “not” are logical operators.
A simple condition:
A compound condition:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
14
15. Getting started with Python
10- Sequence types
Tuple
A simple immutable ordered sequence of items
Items can be of mixed types, including collection types
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
15
16. Getting started with Python
10- Sequence types
Dictionary
Mutable
key-value pair
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
16
17. Getting started with Python
10- Sequence types
List
Mutable ordered sequence of items of mixed types
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
17
18. Getting started with Python
11- Copy a variable
• For simple variables:
# b is a copy of the old value!!
• But mutable variables are always equal to each other!!
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
18
19. Getting started with Python
12- Conditional statements
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
19
20. Getting started with Python
13- Loop statements
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
20
21. Getting started with Python
14- Define a Function
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
21
22. Getting started with Python
15- Class
• A class is a User-Defined variable
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
22
23. Getting started with Python
16- Module
• Modules are imported from other scripts using the import command
• As an example:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
23
24. Getting started with Python
17- Work with files
Note: Read function didn’t skip the blank character!!
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
24
25. Getting started with Python
18- Input Data:
There are some simple ways to request data from user:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
25
26. Getting started with Python
19- Try and except:
You can try to do something in your script and make an exception for the time that
Python can’t run your code!!
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
26
27. Getting started with Python
20- Help
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
27
28. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
28
29. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
29
30. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
30
31. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
31
32. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
32
33. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
33
34. Getting started with Python
Simple examples
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
34
36. Program Automation?
Provides the mechanism to control PSS/E other than by direct interaction!
• PSS/E Automation file languages:
• Response files
• IPLAN commands
• Fortran call statements
• Python commands
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
36
37. Program Automation?
• Response files
• Often referred to as “idv” files or “Batch” commands!
• A set of instructions and inputs emulating the program manual operation
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
37
38. Program Automation?
• IPLAN commands
• A simple programming language designed for PSS/E
• Has limited capabilities compared to modern programming languages!
• Described in the IPLAN manual:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
38
39. Program Automation?
• Fortran call statements
• A well-documented language
• But we don’t use it here!!
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
39
40. Program Automation?
• Some important modules:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
40
41. Python & PSS/E
• ways of connecting:
1.The Embedded Interpreter Environment
• Allows PSS/E to execute Python files and commands.
• Run Program Automation File in PSS/E
2.The External Interpreter Environment
• Python programs access PSS/E functions without running GUI
• Faster and better Way
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
41
42. Python & PSS/E
1.The Embedded Interpreter Environment
Select the Python file type and specify the file name
Execute operations through the PSS/E GUI
click the Stop Recording button
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
42
43. Python & PSS/E
2.The External Interpreter Environment
Code that is suggested in the PSS/E manual to get an External Interpreter
Environment:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
43
44. Python & PSS/E
Functions
Most of useful functions are in psspy module. For example:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
44
45. Python & PSS/E
Functions
Most of useful functions are in psspy module. For example:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
45
46. Python & PSS/E
Functions
Most of useful functions are in psspy module. For example:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
46
47. Python & PSS/E
Functions
How can I find them?
1-The API file
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
47
48. Python & PSS/E
Functions
How can I find them?
2-The record mode
I. Click the Start Recording button
II. Select the Python file type and specify the file name
III. Execute operations through the PSS/E GUI
IV. click the Stop Recording button
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
48
49. Python & PSS/E
Functions
How can I use them?
1-Option mode
2-Array mode
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
49
psspy.fnsl(
options1=0, # disable tap stepping adjustment.
options5=0, # disable switched shunt adjustment.
)
psspy.fnsl([0,_i,_i,_i,0,_i,_i,_i])
50. Python & PSS/E
Important functions
Power Flow Operation (API-Chapter 1)
Describes the API routines corresponding to the operational functions of the PSS®E Power Flow and
Fault Analysis.
Example:
fnsl (Newton-Raphson power flow function)
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
50
51. Python & PSS/E
Important functions
Power Flow Data Changing (API-Chapter 2)
Describes the API routines used to add or modify power flow data.
Example:
branch_data (Modify the data of an existing non-transformer branch in the working case or to add
a new non-transformer branch to the working case)
If you want to change the reactance of the branch from bus 456 to bus 459 circuit 1 to 0.025 pu:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
51
ierr = psspy.branch_data(456,459,"1",[_i,_i,_i,_i,_i,_i], [_f,0.025,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f])
Equal to:
ierr = psspy.branch_data(456,459,"1", realar2 = 0.025)
52. Python & PSS/E
Important functions
Single Element Data Retrieval (API-Chapter 7)
Defines API routines that are used to retrieve data for a single element (e.g., bus,
branch, area, etc.) from the PSS®E engine and test on various program states.
Example:
busdat (Use this API to return real bus values)
If you want to retrieve the pu voltage at the bus 1:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
52
busNumber = 1
paramStr = 'PU'
ierr, Vpu = psspy.busdat(busNumber, paramStr)
53. Python & PSS/E
Important functions
Remove Elements From a Power Flow Case (API-Chapter 1)
Example:
purg(delete specified outaged equipment items from the working case (activity PURG).)
purgbrn(delete the specified non-transformer branch or two-winding transformer from the
working case (activity PURG).)
If you want to remove the line from bus 201 to bus 202 circuit "1“:
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
53
fromBus = 201
toBus = 202
ckt = '1'
ierr = psspy.purgbrn(fromBus, toBus, ckt)
54. Python & PSS/E
Silencing PSS/E Output
• PSS/E prints a voluminous amount of information to the command line when you are running a program
• silence is a function that turn off this outputs and save logs in a file
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
54
55. Python & PSS/E
Simple example
• Optimum placement of a fixed capacitor (100 MVAR) on feeders of “savnw” grid
• Scaling loads (5%) with fixed power factor for making disturbance in network and save case file as “savnw1”
• Log will save in a “psse_logfile.log” and report output is a file named “report.txt”
• Before allocating capacitor, total losses were as below:
69.04 MW & 1267.50 MVAR
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
55
56. Python & PSS/E
Simple example (Code)
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
56
57. Python & PSS/E
Simple example (Results)
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
57
0
10
20
30
40
50
60
70
80
90
MW Losses
MW
58. Python & PSS/E
Simple example (Results)
Department of Electrical and Computer Engineering
Power Systems Analysis Lab.
Amin Rabanian
58
0
200
400
600
800
1000
1200
1400
1600
MVAR Losses
MVAR