0% found this document useful (0 votes)
56 views6 pages

DCOPF Tutorial Using Pyomo and Pandapower

This tutorial provides a comprehensive guide on formulating and solving a DC Optimal Power Flow (DCOPF) problem using Pyomo, a Python-based modeling language. It covers the mathematical formulation of DCOPF, including variables, parameters, objective functions, and constraints, while also demonstrating how to utilize the pandapower library for power system data extraction. By the end of the tutorial, readers will be equipped to implement DCOPF models and analyze the results effectively.

Uploaded by

Didik Hariadi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

DCOPF Tutorial Using Pyomo and Pandapower

This tutorial provides a comprehensive guide on formulating and solving a DC Optimal Power Flow (DCOPF) problem using Pyomo, a Python-based modeling language. It covers the mathematical formulation of DCOPF, including variables, parameters, objective functions, and constraints, while also demonstrating how to utilize the pandapower library for power system data extraction. By the end of the tutorial, readers will be equipped to implement DCOPF models and analyze the results effectively.

Uploaded by

Didik Hariadi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial on DCOPF Formulation in Pyomo

Muhammad Al Firdausi
Dr. Syed Mujahid
January 15, 2025

1 Introduction
In modern power systems, the efficient and reliable distribution of electricity is paramount. Optimal power flow
(OPF) is a crucial tool for optimizing the operation of these complex networks. OPF problems aim to determine the
optimal operating conditions of a power system while satisfying various constraints related to physical constraints
and operational constraints.
This tutorial focuses on a linearized version of OPF known as DCOPF [1]. DCOPF makes several simplifying
assumptions, such as neglecting reactive power. These assumptions significantly reduce the computational complexity
of the problem, making it a valuable tool for initial analysis and planning.
This tutorial will guide you through the formulation of a DCOPF problem using Pyomo [2], a powerful Python-
based modeling language for mathematical optimization. This tutorial assumes you have a basic understanding of
mathematical modeling in Pyomo1 concepts but may not have prior experience with power system analysis or OPF.
We will also demonstrate how to leverage the pandapower library [3] to obtain the necessary power system data for
your DCOPF model.
By the end of this tutorial, you will be able to:

• Understand the concepts of DCOPF and its importance in power system operation.
• Formulate and solve a DCOPF problem using Pyomo.
• Utilize pandapower to obtain and integrate power system data into your Pyomo model.

Let’s begin our exploration of DCOPF and its implementation in Pyomo.

2 Mathematical Formulation of DCOPF


A typical mathematical optimization model consists of the following components:

• Variables: These are the unknown quantities that we aim to determine in the optimization problem. In
DCOPF, variables typically represent the real power generation at each generator bus.
• Parameters: These are known or called data, fixed values that represent the characteristics of the power
system. Examples include bus demands, thermal limit for each transmission line, and generator limits.
• Objective Function: This is the function that we want to minimize or maximize. In DCOPF, common
objective functions include minimizing the total generation cost or minimizing power losses within the system.
• Constraints: These are the limitations or restrictions that must be satisfied by the solution. In DCOPF,
constraints include nodal power balance, power generation limits, and transmission network limits.
In this tutorial, the DCOPF problem aims to find the optimal operating point of a power system by adjusting
generator outputs while satisfying physical and operational constraints. Key assumptions in DCOPF formulation:
1 [Link] [Link]

1
• Linearized Power Flow: DCOPF relies on a linearized power flow model, which simplifies the complex non-
linear relationships in AC power systems. The use of this linearization is consistent with industry practice [4]
• Neglect of Reactive Power: Reactive power is assumed to have negligible impact on real power flow.

• Constant Voltage Magnitudes: Voltage magnitudes at all buses are assumed to be constant.
Mathematical notations used in the DCOPF model are described in Table 1. MW (megawatt) and MVA (megavolt
ampere) are units of the real power and thermal limit, respectively.

Table 1: Nomenclature

Symbol Description
ng Set of generators.
nl Set of bus loads.
nt Set of transmission lines.
2
Ci (Pi ) Quadratic cost function of generator i: c2i PGi + c1i PGi + c0i .
Pi Real power output of generator i (MW).
Fimax Thermal limit of transmission line i (MVA).
Pil Demand in bus load i (MW).
P T DF nt × ng matrix of power transmission distribution factors [5].
min/max
Pi Minimum/maximum power generation limit of generator i (MW).

The DCOPF model is as follows:


ng
X
minimize Ci (Pi ) (1)
i=1
subject to:
ng nl
X X
Pi − Pil = 0 (2)
i=1 i=1
− Fimax + P T DFi · Pil ≤ P T DFi · Pi ≤ Fimax + P T DFi · Pil ∀ i = 1, . . . , ng (3)
Pimin ≤ Pi ≤ Pimax ∀ i = 1, . . . , ng (4)

3 Installing Pandapower
The official pandapower website recommends Anaconda distribution for installing Python distribution that al-
ready includes a lot of modules for scientific computing that are needed for pandapower2 . Since these packages
depend on C-libraries, they cannot be easily installed through pip on Windows systems. Therefore, we recommend
to use conda to install pandapower3 :

conda i n s t a l l conda−f o r g e : : pandapower

4 DCOPF in Pyomo with Pandapower


This section demonstrates how to implement the DCOPF model in Pyomo using data available from the pan-
dapower library. Pandapower is a powerful Python library for power system analysis, providing tools for creating,
modifying, and analyzing power system networks. We will leverage pandapower to:
2 [Link]
3 [Link]

2
• Create a sample power system: Construct a basic power system network with buses, generators, and
transmission lines from publicly available data.
• Extract relevant data: Retrieve necessary data from the pandapower network, such as bus demands, power
transmission distribution factors, and generator limits.
• Formulate the Pyomo model: Integrate the extracted data into the Pyomo model, defining variables,
parameters, objective function, and constraints as described in the previous section.
• Solve the DCOPF problem: Utilize a suitable solver (e.g., CPLEX, Gurobi) through Pyomo to find the
optimal solution for the DCOPF problem.
• Analyze the results: Interpret the solution obtained from the solver, such as optimal generator outputs and
power flows on transmission lines.
The following code demonstrates the implementation of the DCOPF model in Pyomo. We begin by importing
necessary libraries, including Pyomo and the required solvers. Then, we define the sets, parameters, and variables
of the model. Subsequently, we formulate the objective function and constraints within the Pyomo model. Finally,
we create an instance of the model, specify the solver, and solve the optimization problem. The results, including
optimal generator outputs and total cost, can then be extracted and analyzed.

4.1 Code Listings


The following listings contain code for DCOPF implementation on Pyomo using pandapower. The first paragraph
right after every listing explains what the respective code is about.

Listing 1: Import necessary libriaries


1 import pandapower a s pp
2 import pandas a s pd
3 import numpy a s np
4 import pandapower . c o n v e r t e r a s pc
5 from pandapower . pypower . makePTDF import makePTDF
6 import pyomo . e n v i r o n a s pyo
Code in Listing 1 line 4 imports the converter module from the pandapower library. The module provides some
very useful converters which enable an exchange of network data with other Power System analysis tools4 . line 5
imports the available function to build the PTDF matrix.

Listing 2: Data extraction


1 n e t = pp . networks . case6ww ( )
2 P max = np . h s t a c k ( [ n e t . e x t g r i d . max p mw . v a l u e s , n e t . gen . max p mw . v a l u e s ] )
3 P min = np . h s t a c k ( [ n e t . e x t g r i d . min p mw . v a l u e s , n e t . gen . min p mw . v a l u e s ] )
4 P d = np . a r r a y ( [ 0 f o r i i n r a n g e ( l e n ( n e t . bus ) ) ] )
5 f o r i , bus i n enumerate ( n e t . l o a d . bus ) :
6 P d [ bus ] = n e t . l o a d . p mw [ i ]
7 ngen = l e n ( n e t . gen )
8 D = n e t . l o a d . p mw . v a l u e s
9 c o s t c o e f f = n e t . p o l y c o s t . l o c [ : , ’ c p 0 e u r ’ : ’ cp2 eur per mw2 ’ ] . v a l u e s
10 ppc = pc . t o p p c ( net , i n i t =’ f l a t ’ )
11 baseMVA = ppc [ ’ baseMVA ’ ]
12 bus = ppc [ ’ bus ’ ]
13 branch = ppc [ ’ branch ’ ]
14 PTDF = makePTDF(baseMVA , bus , branch )
15 F max = np . a r r a y ( [ rateA f o r i i n r a n g e ( l e n ( branch ) ) \
16 f o r j , rateA i n enumerate ( branch [ i ] ) i f j == 5 ] )
4 [Link]

3
Code in Listing 2 line 1 loads the 6-bus case example network introduced by Wood and Wollenberg [5]. In
pandapower, all Power System Test Cases were converted from PYPOWER5 or MATPOWER [6] case files. lines 2
and 3 extract the maximum and minimum generation level, respectively. Note that there is slightly different way to
represent generators between pandapower and MATPOWER. In the original MATPOWER case files, all generators
are listed in table mpc.gen6 whether in pandapower, there is a separation between external grid ([Link] grid) and
the network generators ([Link]). Therefore, we used [Link] to combine both things from pandapower. The for
loop in line 5 is used to create an array of loads of the bus. This technique is used since [Link] in pandapower
only contains load bus instead of all buses in the system (opposed to [Link] in the MATPOWER). Line 9 extracts
related polynomial cost for the network. ppc in line 10 uses to ppc function to convert a pandapower net to a pypower
case file. In line 15, the thermal limit for each transmission line is extracted. Since the unit of Fimax is in the MVA,
we can not use the pre-calculated thermal limit available in [Link] i ka which in Kilo ampere. rateA in the
MATPOWER manual is in MVA and can be used to specify branch flow limits.

Listing 3: Pyomo model


1 model = pyo . ConcreteModel ( )
2 # sets
3 ngen = l e n ( n e t . gen ) + l e n ( n e t . e x t g r i d )
4 model . ng = pyo . RangeSet ( 0 , ngen −1)
5 # variables
6 model . P = pyo . Var ( model . ng )
7 # o b j e c t i v e (1 / 15)
8 model . o b j = pyo . O b j e c t i v e ( expr =
9 c o s t c o e f f [ : , 0 ] . sum ( )
10 + sum ( c o s t c o e f f [ : , 1 ] [ i ] ∗ model . P [ i ] f o r i i n model . ng )
11 + sum ( c o s t c o e f f [ : , 2 ] [ i ] ∗ ( model . P [ i ] ) ∗ ∗ 2 f o r i i n model . ng ) ,
12 s e n s e = pyo . minimize
13 )
14 # constraint (2)
15 model . c2 = pyo . C o n s t r a i n t ( expr =
16 sum ( model . P [ i ] f o r i i n model . ng ) − D. sum ( ) == 0
17 )
18 # constraint (3)
19 model . c 3 l b = pyo . C o n s t r a i n t L i s t ( )
20 model . c3 ub = pyo . C o n s t r a i n t L i s t ( )
21 f o r i in range ( len ( net . l i n e ) ) :
22 model . c 3 l b . add ( expr=
23 (−F max + (PTDF@P d ) ) [ i ] <= PTDF [ : , : ngen ] [ i ] @model . P
24 )
25 model . c3 ub . add ( expr=
26 ( F max + (PTDF@P d ) ) [ i ] >= PTDF [ : , : ngen ] [ i ] @model . P
27 )
28 # constraint (4)
29 model . c 4 l b = pyo . C o n s t r a i n t L i s t ( )
30 model . c4 ub = pyo . C o n s t r a i n t L i s t ( )
31 f o r i i n model . P :
32 model . c 4 l b . add ( expr=
33 P min [ i ] <= model . P [ i ]
34 )
35 model . c4 ub . add ( expr=
36 P max [ i ] >= model . P [ i ]
37 )

5 [Link]
6 [Link]

4
In Listing 3, row 1 defines a ConcreteModel from the Pyomo library. A ConcreteModel is a type of model in Pyomo
where the data is known at the time of model creation. Next, row 5 defines ng set in [Link] which represents the
range of generators in the power system model. The number of generators is calculated as the sum of the lengths
of [Link] (conventional generators) and [Link] grid (external grids). [Link] is defined as a Pyomo RangeSet
starting from 0 and ending at the total number of generators (ngen) minus 1 (by default, Pyomo set starts from 1).
Then, line 6 defines the variable Pi as model.P. It is defined as a Pyomo Var indexed by the set [Link], meaning
there is a power output variable for each generator in the set. After that, the code defines the objective function of
the optimization problem. The objective function (1) is to minimize (line 12) the total cost of generation, which is
calculated as the sum of three components: a constant cost (line 9), a linear cost proportional to the power output
(line 10), and a quadratic cost proportional to the square of the power output (line 11). The cost coeff variable
has been defined in Listing 2 line 9. Finally, the code defines three sets of constraints: model.c2 (2 is coded in line
15), model.c3 lb & model.c3 ub (3), and model.c4 lb & model.c4 ub (4). model.c2 ensures that the total power
generation matches the total demand ([Link]() in line 16). model.c3 lb and model.c3 ub are constraint lists that
impose lower and upper bounds on the power flow on each transmission line in the system. These constraints use the
PTDF matrix to calculate the impact of each generator’s output on the power flow. model.c4 lb and model.c4 lb
are constraint lists that enforce the minimum (Pimin ) and maximum (Pimax ) power output limits for each generator.

4.2 Solving The Model


After formulating the DCOPF model in Pyomo, the next step is to solve the optimization problem. This involves
using a suitable solver to find the values of the decision variables (e.g., Pi ) that minimize the objective function while
satisfying all the constraints. Pyomo provides an interface to various solvers, such as CPLEX, Gurobi, and MOSEK.
You can specify the solver to be used and then call the SolverFactory to solve the optimization problem. The solver
will return the optimal solution, including the values of the decision variables and the objective function value. The
following code uses Gurobi to solve the model.

Listing 4: Import necessary libriaries


1 s o l v e r = pyo . S o l v e r F a c t o r y ( ’ g u r o b i ’ )
2 r e s = s o l v e r . s o l v e ( model )
3 p r i n t ( f ’ C t o t a l = ’ , pyo . v a l u e ( model . o b j ) )
4 f o r i i n model . P :
5 p r i n t ( f ’ P{ i }= ’ , pyo . v a l u e ( model . P [ i ] ) )
and the output is:

C t o t a l= 3 0 4 6 . 4 1 2 5 1 1 6 8 7 1 4 7 3
P0= 5 0 . 0 0 0 0 0 0 1 0 1 3 2 1 4 6
P1= 8 8 . 0 7 3 6 1 9 5 8 4 6 2 3 7 6
P2= 7 1 . 9 2 6 3 8 0 3 1 4 0 5 4 7 8

The output tells us that the generation level for external grid (P0) is 50 MW, for generator 1 (P1) and 2 (P2) are
88 MW and 72 MW, respectively, and the total cost to generate the power is 3046.

5 Conclusion
This tutorial has provided a brief introduction to the formulation and solution of DCOPF problems using Pyomo
and pandapower. We began by exploring the concepts of DCOPF, including its underlying assumptions, mathemat-
ical formulation, and key components such as variables, parameters, objective functions, and constraints.
Furthermore, we demonstrated how to leverage the power of Pyomo to model and solve the DCOPF problem.
By integrating pandapower, we effectively obtained power system data case and seamlessly integrated it into the
Pyomo model. This practical approach provides a valuable framework for analyzing and optimizing the operation of
real-world power systems.

5
By mastering the concepts and techniques presented in this tutorial, you can gain valuable insights into the
challenges and opportunities associated with optimizing power system operations. Finally, the code in jupyter
notebook format can be downloaded from this link: [Link] Pyomo

References
[1] V. H. Hinojosa and F. Gonzalez-Longatt, “Preventive security-constrained dcopf formulation using power trans-
mission distribution factors and line outage distribution factors,” Energies, vol. 11, no. 6, p. 1497, 2018.
[2] W. E. Hart, J.-P. Watson, and D. L. Woodruff, “Pyomo: modeling and solving mathematical programs in python,”
Mathematical Programming Computation, vol. 3, pp. 219–260, 2011.
[3] L. Thurner, A. Scheidler, F. Schäfer, J.-H. Menke, J. Dollichon, F. Meier, S. Meinecke, and M. Braun, “pan-
dapower—an open-source python tool for convenient modeling, analysis, and optimization of electric power
systems,” IEEE Transactions on Power Systems, vol. 33, no. 6, pp. 6510–6521, 2018.
[4] R. A. Jabr, “Adjustable robust opf with renewable energy sources,” IEEE Transactions on Power Systems,
vol. 28, no. 4, pp. 4742–4751, 2013.
[5] A. J. Wood, B. F. Wollenberg, and G. B. Sheblé, Power generation, operation, and control. John Wiley & Sons,
2013.
[6] R. D. Zimmerman, C. E. Murillo-Sánchez, and R. J. Thomas, “Matpower: Steady-state operations, planning,
and analysis tools for power systems research and education,” IEEE Transactions on power systems, vol. 26,
no. 1, pp. 12–19, 2010.

You might also like