Python For Power Systems Computation
Python For Power Systems Computation
AbstractThis paper presents a power system analysis tool, comparable to proprietary solutions. Specific contributions of
called D OME, entirely based on Python scripting language as the paper are:
well as on public domain efficient C and Fortran libraries. The
objects of the paper are twofold. First, the paper discusses the 1) To show that the Python language is an adequate tool
features that makes the Python language an adequate tool for for power system analysis studies.
research, massive numerical simulations and education. Then the 2) To describe the architecture of the developed Python-
paper describes the architecture of the developed software tool based software tool, namely D OME.
and provides a variety of examples to show the advanced features 3) To provide a comparison through power system simula-
and the performance of the developed tool.
tions of the performance of some open-source scientific
Index TermsPython language, scripting, power flow analysis, libraries.
eigenvalue analysis, time domain integration.
The paper is organized as follows. Section II provides the
rationale for using Python as the main language for a power
I. I NTRODUCTION system analysis tool. Section III provides a description of the
The author is supported by the Ministry of Science and Innovation of Spain, ability of producing publication quality 2D figures in
MICINN Projects ENE2009-07685 and ENE2012-31326. Python is as powerful as in Matlab.
2
TABLE I
C OMPARISON OF THE PERFORMANCE OF SPARSE MATRIX FACTORIZATION analysis problem. Finally, LUSOL, which is a legacy Fortran
LIBRARIES FOR THE SOLUTION OF THE POWER FLOW PROBLEM library used, for example, in GAMS [21], is the slowest of all
Library Total CPU 1st fact. Next fact. the considered libraries.
time [s] time [s] time [s] The comparison given in Table I cannot be fully fair as
KLU 0.0933 0.0044 0.0026 each library stores sparse matrices in a slightly different way.
CXSPARSE 0.0936 0.0043 0.0027 The representation used in D OME is a C object that stores
UMFPACK 0.1750 0.0126 0.0095 the matrix in the compact compressed column storage (CCS)
SUPERLU 0.1927 0.0247 0.0082 format. This is the well-known Harwell-Boeing sparse matrix
LUSOL 0.3112 0.0360 0.0195 representation. In particular, D OME uses the CCS implemen-
tation provided by the CVXOPT package [22]. This C object
is fully compatible only with KLU and UMFPACK libraries,
els are included in the proposed case study. Multipro- whereas extra memory allocation is required to link the other
cessing is also exploited to run these simulations. libraries, so that their performance can be affected.
3) Ability of solving the eigenvalue analysis of delayed
B. Time Domain Integration of SDAEs
differential equations (DDAEs). This analysis is particu-
larly demanding as the size of the problem can be huge This subsection illustrates the ability of D OME to simulate
even for small systems. Also in this case, a variety of SDAE systems. D OME includes implicit A-stable time integra-
mathematical libraries are compared. tion schemes for stiff DAE systems. These are the backward
All simulations are solved on a server equipped with two Euler, the trapezoidal method and the backward differentiation
processors Intel Xeon Six Core 2.66 GHz, an Intel SSD of formula of order 2. Moreover, D OME includes the possibility
256 GB, and 64 GB of RAM, and mounting a Linux 64 bits to model and numerically integrate stochastic processes, such
operating system. as the Wiener and the Orstein-Uhlenbeck processes, which
have been proposed in the literature [23], [24]. Continuous
SDAE wind models based on the Weibull distribution are
A. Comparison of Sparse Matrix Factorization Libraries also included [25]. Stochastic processes are integrated using
This subsection compares the performance of a variety of Maryuama-Euler and Milstein schemes [26].
sparse matrix factorization libraries through power system As previously discussed, D OME allows including new de-
analysis. Only open-source libraries are considered. These are: vices with the minimal effort. Then, Python classes and
CXSPARSE, UMFPACK, KLU, LUSOL, and SUPERLU. The metaprogramming allow easily implementing stochastic mod-
former three libraries are part of the SuiteSparse package [17], els. These features allows defining a stochastic version of any
whereas SUPERLU and LUSOL are available at [18] and [19], device by simply merging together the original device class
respectively. A 1254-bus 1944-line network that models that and the class implementing the stochastic process. Moreover,
UCTE 2002 Winter Off-peak is used as benchmark system. a special meta-device that adds stochastic processes to an
Details on this system can be found in [20]. arbitrary state or algebraic variable allows to modify the
Table I shows the results obtained for the considered li- behavior of any implemented device.
braries which are compared in terms of the CPU time required Figure 3 shows 1000 trajectories (strong solutions of the
to solve the power flow analysis for the UCTE 2002 Winter SDAE) as well as the mean trajectory (weak solution of the
Off-peak. Eight iterations are required to solve the power flow SDAE) for the IEEE 14-bus system that includes stochastic
problem by means of a standard Newton-Raphson technique. processes in the bus voltage phasors, machine rotor speeds as
This relatively high number of iterations is due to the fact that well as load power consumption. All standard dynamic data
the voltages of some buses are higher than 1.1 pu, which leads of this system can be found in [14]. The simulations consist
PQ loads to switch to constant impedance models. in applying the outage of line 2-4 at t = 1 s. All devices are
Table I indicates the total time required to solve the power perturbed through Orstein-Uhlenbeck processes, which allows
flow analysis, which includes the Jacobian matrix factorization bounding the standard deviation thanks to the mean-reverting
as well as the time to build the Jacobian matrix itself and by feature of this process [23]. The diffusion term of all stochastic
the overall power flow algorithm; the CPU time to factorize processes is considered constant and is assumed to be 1%
for the first time the Jacobian matrix, which includes both the of the mean value. These processes account for harmonics,
symbolic and the numeric factorization steps; and the time for vibrations, and load randomness.
solving the factorization the second and following iterations, Integrating SDAEs requires is a demanding task. In general,
which implies only the numeric factorization step. one has to solve a few thousands of time domain integrations
The two most efficient libraries are KLU and CXSPARSE. with different generations of the normal distributions used to
KLU is known to be particularly suited for factorizing sparse define the stochastic processes. These high number of solutions
matrices that describe electrical circuits. As a matter of fact, is required to properly define the statistical properties of the
KLU is the library used in the OpenDSS project [6]. On the resulting trajectories (e.g., standard deviation, autocorrelation,
other hand, the efficiency of CXSPARSE was not expected, etc.). However, since each simulation is fully decoupled from
as its main purpose is didactic. Other libraries, such as UMF- all others, parallelization can be easily exploited. D OME in-
PACK (used in Matlab) and SUPERLU (which is a fork of cludes some basic mechanism to parallelize simulations by dis-
KLU) are not particularly efficient for solving the power flow tributing multiple tasks over available processors. As a matter
4
Fig. 3. Active power injected at bus 1 for the IEEE 14-bus system. Gray Fig. 4. Full spectrum for the IEEE 14-bus system with 5 ms time delay in
lines are the 1000 computed trajectories; the black line is the mean value. the AVR measured voltage signals.
of fact, multiprocessing is particularly easy in Python (thanks it can be easily noted, even for small N the computational
to the built-in multiprocessing module), especially if burden of the solution of the eigenvalue problem dramatically
processes do not share memory. As a result, solving the 1000 increases. Fortunately, only a reduced set of eigenvalue is
simulations with a fixed time step of 0.05 s and a standard needed for the purpose of stability analysis, i.e., those whose
dishonest Newton-Raphson solver takes about 8.5 seconds on real part is close to the imaginary axis. Hence, efficient
the 24 processors (12 processors are virtual) available on the algorithms able to compute a reduced number of eigenvalues
server used in this case study. with a given property are to be used.
The case study considered in this subsection is again the
IEEE 14-bus system as described in [14]. The dynamic order
C. Eigenvalue Analysis of DDAEs
of this system is n = 49. Let assume that the terminal
This subsection describes the small-signal stability analysis voltage signal of the 5 AVRs of synchronous machines have
of a power system modeled as a delayed DAE. As it is a measurement delay of = 5 ms. One can debate whether
well-known, the number of eigenvalues of a DDAEs with considering the same delay for all AVRs is reasonable or not.
constant delays is infinite, as it is the solution of the following However, how to set-up the technique proposed in [29] to
characteristic equation: include multiple time delays is currently an open question and
X
is out of the scope of this paper.
() = I n A0 Ai ei (1) Figure 4 shows the full spectrum in the S-domain obtained
i=1 with N = 40. Hence, the total number of eigenvalues are
where A0 is the standard state matrix, I n is the identity matrix 1960. The eigenvalues have been computed using the routine
of order n, Ai are the characteristic matrix for each constant for generalized matrix provided by the well-known LAPACK
delay i [27]. library. The total simulation time is 28.3 s. Further increasing
The technique implemented in D OME attempts to compute a N provides more details on the spectrum but do not alter the
reduced number of eigenvalues of (1) using the approximation values of eigenvalues close to the imaginary axis, which are
proposed in [28][30]. These methods are based on a dis- the only of interest for the stability analysis.
cretization of the partial differential equation (PDE) represen- While the S-domain is widely used in power system anal-
tation of the DDAE. The implementation of such discretization ysis, there are alternative domain that can be used to solve
is surprisingly simple while results proved to be accurate. the eigenvalue analysis. In particular, D OME implements the
The idea is to transform the original DDAE problem into an Z-domain bilinear transformation, as follows:
equivalent PDE system of infinite dimensions. Then, instead
AZ = (AS + I)(AS I nx )1 (2)
of computing the roots of retarded functional differential
equations, one has to solve a finite, though possibly large, where AS is the original state matrix, I the identity matrix of
matrix eigenvalue problem of the discretized PDE system. the same size as AS , and is a weighting factor that, based
Without entering into mathematical details (the interested on heuristic considerations, can be set to = 8 [32].
reader can find an exhaustive discussion in [31]), let simply say Computing AZ is more expensive than AS but using
that the resulting problem to be solved is a standard eigenvalue AZ can be useful to better visualize stiff systems (as the
analysis of a highly sparse square matrix whose size is N n, eigenvalues falls within or close to the unitary circle) and
where n is the number of state variable of the system and for fastening the determination of the maximum amplitude
N is the number of nodes of the Chebyshev grid used to eigenvalue (e.g., by means of the Arnoldi iteration), especially
approximate the PDE representation of the DDAE [29]. As in case of unstable equilibrium points with only one eigenvalue
5
TABLE II
C OMPARISON OF THE PERFORMANCE OF LIBRARIES FOR EIGENVALUE [7] C. A. Canizares, F. L. Alvarado, and S. Zhang, UWPFLOW
ANALYSIS OF LARGE SPARSE MATRICES . Program, 2006, university of Waterloo, available at
http://www.power.uwaterloo.ca.
Library Method CPU time [s] [8] PYPOWER, available at www.pypower.org.
ARPACK Arnoldi Iteration 2.15 [9] minpower, available at minpowertoolkit.com.
SLEPC Arnoldi Iteration 2.16 [10] F. Milano and L. Vanfretti, State of the Art and Future of OSS for Power
Systems, in IEEE PES Gen. Meeting, Calgary, Canada, Jul. 2009.
SLEPC Krylov-Shur method 1.50 [11] B. Pierce, Types and Programming Languages. Cambridge, MA: MIT
SLEPC Lanczos method 1.48 Press, 2002.
[12] P. S. Pacheco, An Introduction to Parallel Programming. Burlington,
MA: Elsevier, 2011.
[13] B. OSullivan, J. Goerzen, and D. Stewart, Real World Haskell. Se-
bastopol, CA: OReilly, 2008.
outside the unit circle. In fact, the eigenvalues in the Z- [14] F. Milano, Power System Modelling and Scripting. London: Springer,
plane are stable if their magnitude is lower than 1, and 2010.
unstable if greater than 1. Bifurcation points are on the unitary [15] Dome Project, available at www3.uclm.es/profesorado/federico.milano.
[16] L. Vanfretti and F. Milano, Facilitating Constructive Alignment in
circumference. Power Systems Engineering Education using Free and Open Source
Since the critical eigenvalues in the Z-domain have magni- Software, IEEE Trans. on Education, vol. 55, no. 3, pp. 309318, 2012.
tude close to 1, it is possible to take advantage of algorithms [17] SuiteSparse version 4.0.2, available at
www.cise.ufl.edu/research/sparse/SuiteSparse.
that compute a reduced number of eigenvalues with a given [18] SuperLU version 4.3, available at crd-legacy.lbl.gov/ xiaoye/SuperLU.
property. D OME provides C extensions to some efficient open- [19] LUSOL, available at www.stanford.edu/group/SOL/software/lusol.html.
source libraries such as ARPACK [33] and SLEPc [34]. The [20] Q. Zhou and J. W. Bialek, Approximate Model of European Intercon-
nected System as a Benchmark System to Study Effects of Cross-Border
latter library is based on PETSc, which is a C++ based suite Trades, IEEE Trans. on Power Systems, vol. 20, no. 2, pp. 782787,
of data structures and routines for the scalable solution of May 2005.
scientific applications [35]. [21] General Algebraic Modeling System, available at www.gams.com.
[22] CVXOPT Python Software for Convex Optimization, available at
Table II shows a comparison of different algorithms and abel.ee.ucla.edu/cvxopt.
libraries to compute the 50 eigenvalues with largest magnitude [23] M. Perninge, V. Knazkins, M. Amelin, and L. Soder, Risk Estimation
in the Z-domain for the IEEE 14-bus system with time delays of Critical Time to Voltage Instability Induced by Saddle-Node Bifur-
cation, IEEE Trans. on Power Systems, vol. 25, no. 3, pp. 16001610,
and N = 40. In particular, the Arnoldi iteration, the Krylov- Aug. 2011.
Shur method and the Lanczos method are compared. As it can [24] Z. Y. Dong, J. H. Zhao, and D. J. Hill, Numerical Simulation for
be observed, the Arnoldi iteration of ARPACK and SLEPc Stochastic Transient Stability Assessment, IEEE Trans. on Power
Systems, vol. 27, no. 4, pp. 17411749, Nov. 2012.
implementations shows similar performance. On the other [25] R. Zarate-Minano, M. Anghel, and F. Milano, Continuous Wind Speed
hand, the Krylov-Shur and the Lanczos methods provide best Models based on Stochastic Differential Equations, accepted for pub-
results. lication on Applied Energy, 2012.
[26] E. Kloeden, E. Platen, and H. Schurz, Numerical Solution of SDE
Through Computer Experiments. New York, NY, third edition: Springer,
V. C ONCLUSIONS 2003.
[27] W. Michiels and S. Niculescu, Stability and Stabilization of Time-Delay
This paper shows that Python is a modern, mature, complete Systems. Philadelphia: SIAM, 2007.
and versatile scripting language that is fully prepared for [28] A. Bellen and M. Zennaro, Numerical Methods for Delay Differential
scientific research and education on power system analysis. Equations. Oxford: Oxford Science Publications, 2003.
[29] D. Breda, Solution Operator Approximations for Characteristic Roots
The case studies discussed in the paper demonstrate that prop- of Delay Differential Equations, Applied Numerical Mathematics,
erly linking Python to efficient general purpose mathematical vol. 56, pp. 305317, 2006.
libraries allows obtaining excellent performance. [30] D. Breda, S. Maset, and R. Vermiglio, Pseudospectral Approximation
of Eigenvalues of Derivative Operators with Non-local Boundary Con-
Future work will focus on further developing D OME in ditions, Applied Numerical Mathematics, vol. 56, pp. 318331, 2006.
various directions, such as smart grid modeling, parallel com- [31] F. Milano and M. Anghel, Impact of Time Delays on Power System
puting including GPUs and heterogeneous architectures, as Stability, IEEE Trans. on Circuits and Systems - I: Regular Papers,
vol. 59, no. 4, pp. 889900, 2012.
well as testing novel mathematical tools for power system [32] M. Ilic and J. Zaborszky, Dynamic and Control of Large Electric Power
analysis. Systems. New York: Wiley-Interscience Publication, 2000.
[33] ARPACK Arnoldi Package, available at
www.caam.rice.edu/software/ARPACK.
R EFERENCES [34] SLEPc Scalable Library for Eigenvalue Problem Computations,
[1] H. P. Langtangen, Python Scripting for Computational Science. Hei- available at www.grycap.upv.es/slepc.
delberg: Springer-Verlag, 2002, third edition. [35] PETSc Portable, Extensible Toolkit for Scientific Computation,
[2] J. H. Chow and K. W. Cheung, A Toolbox for Power System Dynamics available at www.mcs.anl.gov/petsc.
and Control Engineering Education and Research, IEEE Trans. on
Power Systems, vol. 7, no. 4, pp. 15591564, Nov. 1992.
[3] F. Milano, An Open Source Power System Analysis Toolbox, IEEE Federico Milano (S09) received from the Uni-
Trans. on Power Systems, vol. 20, no. 3, pp. 11991206, Aug. 2005. versity of Genoa, Italy, the Electrical Engineering
[4] R. D. Zimmerman, C. E. Murillo-Sanchez, and R. J. Thomas, MAT- degree and the Ph.D. degree in 1999 and 2003, re-
POWER: Steady-State Operations, Planning, and Analysis Tools for spectively. From 2001 to 2002 he was with the Uni-
Power Systems Research and Education, IEEE Trans. on Power Sys- versity of Waterloo, Canada, as a Visiting Scholar.
tems, vol. 26, no. 1, pp. 12 19, Feb. 2011. From 2003 to 2013, he was with the University of
[5] M. Zhou and S. Zhou, Internet, Open-source and Power System Castilla-La Mancha, Spain. He joined the University
Simulation, in IEEE PES Gen. Meeting, Montreal, Quebec, Jun. 2007. College Dublin, Ireland, in 2013, where is currently
[6] R. C. Dugan and T. E. McDermott, An Open Source Platform for Associate Professor. His research interests include
Collaborating on Smart Grid Research, in IEEE PES Gen. Meeting, power system modeling, stability and control.
july 2011, pp. 1 7.