32
Introduction to Programming in Python for Power System Analysis Lukmanul Hakim

Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

  • Upload
    others

  • View
    27

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Introduction to Programming in Python for Power System Analysis

Lukmanul Hakim

Page 2: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Why Python ?

• Procedural Programming

• Object-oriented Programming

• Complex Number

• Numerical Libraries

• Support web-programming

• And many more…..

Page 3: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

How to write the code

• Python Shell Interactive Mode

• Script Mode Create a file with .py extension and running the interpreter

C:\>python your_file.py

• To make life easier, please use an IDE (Integrated Development Environment) like Eclipse, Solaris Studio, Aptana, etc….

Page 4: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Let’s start !

• Go to www.python.org Downloads

• www.numpy.org

• www.scipy.org

• www.matplotlib.org

• www.liclipse.com

• www.aptana.com

Page 5: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 6: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Create your first program in this text editor. People usually start by writing this line: Print “hello world!”

Save your first program as ex_1.py To run the program from this user interface, you just simply click on RUN and RUN MODULE or just by pressing F5 button

Create your first program in this text editor. People usually start by writing this line: Print “hello world!”

Save your first program as ex_1.py To run the program from this user interface, you just simply click on RUN and RUN MODULE or just by pressing F5 button

Page 7: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Let’s get started.

• Case 2 Make Python prints number from 1 to 10

• Create a new file New Window

• Write your code

• Save the file as ex_2.py

• Run the module

• TIPS: use your internet browser to obtain the sample code of the same case

Page 8: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 9: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Case 3

• Multiply two matrices of dimensions (3 x 3) and (3 x 1) with their elements are randomly generated

• TIPS :

– Use NUMPY to help you

– Browse the Internet for any example

– Find out how to use random number in Python

Page 10: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 11: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 12: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Case 4

2 4 1

4 2 4

5 2 6

MATRIX A =

6

2

5

MATRIX B =

SOLVE X FROM A.X = B

Page 13: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 14: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Case 5 – Solve current for this circuit

Tips: I = R-1.V

Page 15: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 16: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 17: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

A bit of Object-oriented Programming

• Power system components such as Bus, Generator, Load, Transformer, and Line can be modeled mathematically

• These physical objects can also be modeled as software objects

• Let’s apply this programming style for our electrical engineering problems

• TIPS: Each bus provides connection to any components

Page 18: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Let’s build OO Model for Case 5

• We have two batteries

• We have four nodes/buses

1. Connecting B1 and R1

2. Connecting R1, R2, and R3

3. Connecting B2 and R3

• We have three resistors

Page 19: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Case 6 – Object Model

• Bus – Has id/number of type integer – Has battery connected to it [list] – Has resistors connected to it [list]

• Battery – Has voltage [type: float] – Connected to a bus

• Resistor – Has value [type: float] – Connected to two buses

Page 20: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 21: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 22: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Results of Case 6

Page 23: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Problems…..

• Although object-oriented approach was adopted, the code was not flexible… Different problem/case/circuit results in different program

• Because we are LAZY to write different code for different problem with similar calculation method, let us create an input file and leave the program for different input data….

Page 24: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Remember: The Node Voltage Method

• Convert voltage sources in series with a resistor to an equivalent current source with the resistor in parallel

• Change resistor values to conductances.

• Select a reference node(E0)

• Assign unknown voltages (E1)(E2) ... (EN)to remaining nodes.

• Write a KCL equation for each node 1,2, ... N. The positive coefficient of the first voltage in the first equation is the sum of conductances connected to the node. The coefficient for the second voltage in the second equation is the sum of conductances connected to that node. Repeat for coefficient of third voltage, third equation, and other equations. These coefficients fall on a diagonal.

• All other coefficients for all equations are negative, representing conductances between nodes. The first equation, second coefficient is the conductance from node 1 to node 2, the third coefficient is the conductance from node 1 to node 3. Fill in negative coefficients for other equations.

• The right hand side of the equations is the current source connected to the respective nodes.

• Solve system of equations for unknown node voltages.

Page 25: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

1 2 3 4

0

Node 1: IB1 + I12 = 0

IB1 + (( V1 – V2 ) / R1 ) = 0

Node 2: I21 + I23 + I20 = 0

( V2 – V1 ) / R1 ) + ( V2 – V3 ) / R3 ) + ( V2 – V0 ) / R2 ) = 0

Node 4: IB2 + I43 = 0

IB2 + (( V4 – V3 ) / R5 ) = 0

Node 3: I32 + I34 + I30 = 0

( V3 – V2 ) / R3 ) + ( V3 – V4 ) / R5 ) + ( V3 – V0 ) / R4 ) = 0

Page 26: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

(1/R1)*V1 – (1/R1)*V2 + IB1 = 0

– (1/R1)*V1 + ((1/R1)+(1/R2)+(1/R3))* V2 – (1/R3)*V3 = 0

– (1/R2)*V2 + ((1/R3)+(1/R4)+(1/R5))* V3 – (1/R5)*V4 = 0

– (1/R5)*V3 + (1/R5)*V4 + IB2 = 0

V1 = VB1

V4 = VB2

Let’s establish a system of linear equations of this class of problem

Page 27: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Matrix form is preferable….. A.x = b

(1/R1) – (1/R1) 0 0 1 0

– (1/R1) (1/R1)+(1/R2)+(1/R3) – (1/R3) 0 0 0

0 – (1/R2) (1/R3)+(1/R4)+(1/R5) – (1/R5) 0 0

0 0 – (1/R5) (1/R5) 0 1

1 0 0 0 0 0

0 0 0 1 0 0

V1

V2

V3

V4

IB1

IB2

0

0

0

0

VB1

VB2

x = b =

A =

Page 28: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 29: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode
Page 30: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Input Data…

Page 31: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Results of Case 7

Page 32: Introduction to Programming in Python for Power System ...luq.unila.ac.id/Introduction to Programming in Python for Power Syst… · •Python Shell Interactive Mode •Script Mode

Let’s go back to Case 6 and Check!