PowerDynamics.jl -- An experimentally validated open-source package for the dynamical analysis of power grids

PowerDynamics.jl is a Julia package for time-domain modeling of power grids that is specifically designed for the stability analysis of systems with high shares of renewable energies. It makes use of Julia's state-of-the-art differential equation solvers and is highly performant even for systems with a large number of components. Further, it is compatible with Julia's machine learning libraries and allows for the utilization of these methods for dynamical optimization and parameter fitting. The package comes with a number of predefined models for synchronous machines, transmission lines and inverter systems. However, the strict open-source approach and a macro-based user-interface also allows for an easy implementation of custom-built models which makes it especially interesting for the design and testing of new control strategies for distributed generation units. This paper presents how the modeling concept, implemented component models and fault scenarios have been experimentally tested against measurements in the microgrid lab of TECNALIA.


Motivation and significance
The massive integration of renewable energy sources comes with challenges for the dynamic stability and control of power grids. In conventional grids the dynamical stability is ensured by controlling a relatively small number of large conventional generators that are synchronized over the high voltage transmission grid. The inertia of the rotating generator masses stabilizes the operating state against short term fluctuations. In contrast, solar and wind power plants produce much less power and are therefore typically installed in the distribution grid, interfaced by inverters with control schemes that lock onto the grid frequency. A transition towards a larger share of renewable energy sources therefore not only implies a decentralization and an increase in the number of generating units but also significantly decreases the total amount of stabilizing inertia. To tackle this issue, the development of more decentralized control schemes and so-called grid-forming inverter controls has recently become a very active research field [1].
However, models of grid-connected inverters represent black-box models, do not allow a transparent verification of the model results and therefore do not allow a fast transfer of the latest research results. As CIGRE also emphasizes, different inverter types and manufacturer models are often available in different software and in different degrees of complexity. Therefore, harmonization in a common simulation model is impractical [2].
At the same time, the energy transition requires rapid development and continuous improvement of reliable simulation tools for dynamic network analysis [3]. For this reason, Open-Source (OS) solutions are an important contribution to the implementation of a grid-stable energy transition. While in the area of static network analysis (for load flow calculations and optimal power flow problems) there are already numerous OS tools, there is little open source available in the area of dynamic modeling of frequency and voltage stability as well as the synchronization behavior of the network in the seconds and sub-seconds range. One reason for the lack of current OS solutions is the lack of a high-performance software environment.
PowerDynamics.jl [4] is an open source software for the dynamic simulation of energy systems that aims to fill this gap [5]. It is based on the programming language Julia [6] and developed in a collaboration between the Potsdam Institute for Climate Impact Research (PIK) and the company elena international GmbH 1 . PowerDynamics has been shown to easily beat the simulation times of common commercial simulation environments, such as PowerFactory or MATLAB Simulink [7]. This opens up the possibility to run very large test cases or apply Monte Carlo sampling based methods [8,9].
PowerDynamics comes with a library of predefined component types for generators, loads, inverters, transmission lines and transformers, as well as a number of fault scenarios. Users can easily build their own test cases or read in existing test case files. We encourage users to also implement their own component types and eventually share it with others by making a pull request on GitHub 2 .
The construction of the differential equation system is based on Network-Dynamics.jl [10], a Julia package for simulating large dynamical systems on complex network structures that is also developed at PIK. The numerical integration is based on Julia's DifferentialEquations.jl package [11]. This makes PowerDynamics also compatible with Julia's scientific machine learning packages 3 and thereby opens up the possibility to apply machine learning methods to transient stability analysis.

Software Architecture
The general workflow for a dynamic simulation with PowerDynamics.jl is shown in Fig. 1. A power grid is defined by specifying the grid structure and the dynamic equations for the node and line components. Once a power grid is defined, the data can be stored in a .json-file by using write_powergrid and loaded again by using read_powergrid. The function find_operationpoint is used to find fix points of the dynamical system. Different fault scenarios can be applied to the system in operation state.

Nodes
Synchronous machines, inverters and loads are considered as node components. PowerDynamics.jl contains a library of already implemented node components. Additionally, users can define their own node components by using the node macro @DynamicNode.

Lines
Transformers and electrical lines are considered as line components. As for the node components, PowerDynamics.jl includes a standard library of different line and transformer types, including a static admittance line, StaticLine, a dynamic admittance line, RLLine, the (static) PiModelLine and a simple transfomer model based on the Pi-model.

Grid structure
The PowerGrid component is built from nodes and lines. It contains all information about the graph and is used to build the right-hand-side ODEFunction with the NetworkDynamics.jl -library. This way the model definitions and the simulation engine are decoupled.

Operationpoint
The operationpoint represents the steady-state solution of the dynamic power system. There are different methods for finding the operation point: rootfind, nlsolve and dynamic. The function find_operationpoint returns the operationpoint which is of Type State.

Fault scenarios
All simulation in PowerDynamics assume some kind of fault scenario. It can be either a change in initial conditions (different from the operation point of the system) at the beginning of a simulation. Or it is a perturbation of node or line paramers for a certain time span tspan_fault that alter the PowerGrid component. These can be e.g. a sudden change in load, PowerPerturbation, or a line failure, LineFailure. The latter faults are all derived from AbstractPerturbation, and all share the same simulate function.

Numerical solution & plotting
The simulation of a fault scenario is done with the function simulate. It converts the problem into an ODEProblem, numerically integrates it using the DifferentialEquations.jl package and returns a PowerGridSolution. This solution can then be easily plotted with Plots.jl using a predefined plotting recipe for the solution type.

Software Functionalities
PowerDynamics.jl allows for modeling symmetric 3-phase power grids in dq-coordinates. Voltage and current are both represented by complex phasors, where the real part represents the d-coordinate and the imaginary part the q-coordinate. The advantage of this complex representation is that the complex power can be calculated by s = p + jq = u · i * .
The package is designed for transient stability analysis. Dynamics on shorter timescales such as harmonics and inverter switching cannot be modeled using the phasor approach.

Sample code snippets
The following code snippet is an explanatory example of how to use the DynamicNode-Macro to define new inverters, generators or loads in PowerDynamics. It starts with the definition of parameters as arguments and the definition of the mass matrix. The boolean is true for differential equations and false for algebraic constraints. The assertion statements on the parameters is followed by a list of dynamics variables and finally the ODE-system itself.
The ODE system is expected to contain the right-hand-side for all dynamic variables stated before. The complex voltage u is always required for all dynamic nodes, however it can be replaced by an algebraic constraint by setting m_u=false. The given example of a grid-informing inverter, VSIVoltagePT1, corresponds to the one visualized in the block diagram in Fig.2.

Future Developments & Improvements
The PowerDynamics community has just started to work heavily on the modularization of PowerDynamics such that power generators, inverters and other equipment can be represented in the well-known block diagram structure. We plan to undertake this modularization with the help of Model-ingToolkit.jl 4 . In Figure 2 a block diagram is shown for the example of a grid-forming inverter. It consists of active-and reactive power droop control (as an outer control loop), a frequency integrator and active as well as a reactive power filters. The causal modeling representation from the block diagram is planned for the new major PowerDynamics release. We further plan to implement more fault scenarios, such as noise perturbations [14,15] and dynamically induced cascading failures [16]. These enhancements will make use of the state-of-the-art stochastic differential equations solvers and callback functions of DifferentialEquations [11].
We also want to extend the library of component models for lines and nodes. Here, we also expect user contributions for various inverter controls that can be shared via pull requests on GitHub. 5 More future contributions are expected from the MARiE-project (Dynamic modeling for analysis and control of intelligent energy networks) undertaken by BTU Cottbus together with elena international GmbH and funded by the German Federal Ministry of Economics and Technology. The key innovation of the project is the development of canonical base models of

Illustrative Example
Our illustrative example is the simulation of a tripping line in the IEEE 14-bus system. This system contains 5 generators, 11 loads, 17 lines and 3 transformers. We model the loads as constant power loads and the generators by a 4th order generator model [17]. The parameters are taken from [18].
The implementation of this introductory example can be found in the packages PowerDynamicsExample 6 . It is available both as a Julia script and a Jupyter Notebook. The latter can also be directly launched in the browser by using BinderHub.
The simulation of this test case is straightforward and requires only a few lines of code. PowerDynamics is a registered package. This means it can be directly installed from the Julia REPL with: For creating a new test case we would have to define an Array or OrderedDict of buses and lines and execute the function PowerGrid(buses,lines). Here, we assume that the IEEE-14bus testcase is already saved as a Json file that we can read in with read_powergrid. As the initial state for the simulation we determine the operation point with find_operationpoint. The fault scenario is the predefined function LineFailure, which removes a line from the grid. The numerical integration is done with the simulate function and the results are shown in Fig. 3.

Testlab description
An experimental validation of PowerDynamics.jl was undertaken in January 2020 at the Smart Grid Technologies Laboratory at TECNALIA within The laboratory at TECNALIA comes with a grid-forming and a gridfollowing inverter that both have self-built control schemes [19,20]. Therefore, the dynamic equations and parameters are known and can be translated into PowerDynamics code. The grid-following control consists of a low-pass filter for the voltage signal, a phase-locked loop (PLL) and a droop control for active and reactive power. The grid-forming control consists of several filters for the voltage and current signals, a droop control for frequency and voltage amplitude and a fictitious impedance.

Test Case description
For demonstrative purposes in this paper we chose a test case setup including a grid-forming, a grid-following and a load in grid connected mode (Fig. 4). The comparison between measurements and simulations for the scenario of a power set point dispatch at the voltage source inverter is shown in Fig. 5. A more detailed description and a larger variety of test cases can be found in the technical report of the Valeria project [21].

Scientific impact
Programming languages, such as Python or the commercial language MATLAB, are not up to the special requirements of modeling renewable power grids. This includes the integration of stochastic differential equations for the representation of fluctuating renewables [3] as well as the simulation of delayed differential equations to represent the influence of measurement and delay times of inverter controls [22]. Due to Julia's just-in-time compilation, PowerDynamics has also strong performance advantages compared to simulations in MATLAB or Python. It has also been shown to be much more performant than its commercial competitors MATLAB Simulink and DIgSILENT PowerFactory [7]. This does not only enable the dynamic simulation of very large test cases with a large number of nodes but also Monte Carlo sampling based methods. This includes for example efficient calculations of stability and performance measures such basin stability [8] and survivability [9] that complement standard linear stability analysis and capture also the nonlinear behavior of the system. Additionaly, PowerDynamics is also compatible with Julia's scientific machine learning packages such as DiffEqFlux.jl [23], a package for combining differential equations with neural networks. This will eventually lay the foundation for a new branch of research, a machine learning based energy system stability analysis [24]. The test grid has an interconnection to the Spanish grid (bus 1) which we model as a slack node. Measurement are taken out with two Boxes that can measure voltage and current signals for two phases. We assume the phases to be balanced and thereby calculate the third phase.
While in the context of static power flow analysis there are already numerous OS tools (which are mostly Python-based), e.g. PyPSA [25] and panda-power [26], there is little open source available in the area of dynamic modeling of frequency and voltage stability as well as the synchronization behavior of the network in the seconds and subseconds range. Only the PSAT library which has not been maintained for 10 years and therefore does not contain any current inverter models, and the new library PowerSimulation-Dynamics.jl 8 (another Julia package, continuation of LITS.jl [27]) are known. PowerSimulationDynamics, developed by NREL, uses the parsing capabilities of PowerSystems to build their industry standard power system data structures. The package is using implicit and explicit DAE representations in combination with the Sundials solvers [28]. In contrast PowerDynamics is based on the DifferentionalEquations package which enables to simulate SDEs, DDEs and explicit DAEs in mass matrix representation. This allows the modeling of inverter measurement delays and renewable power fluctuations. Additionally, since PowerDynamics uses the NetworkDynamics package for simulation model building which decouples the representation of nodes and lines, major performance optimizations with parallelization and GPU calculations are possible. This gives the possibility to model large-scale networks. Currently, we are in contact with the developers of PowerSimu-lationDynamics and collaborate on the integration of ModelingToolkit 9 and further topics.
PowerDynamics is still very young and under heavy development but the user base is steadily growing. At the moment the software is developed by and used for research at PIK, at TU Berlin, at BTU Cottbus, at TU Delft and at FZ Jülich. Further, TU Delft plans to use PowerDynamics for teaching electrical engineering courses.

Commercial impact
PowerDynamics.jl will serve as a basis or backend for web applications and other commercial products of elena international which is a spin-off of the Potsdam-Institute of Climate Impact Research. In particular, PowerDynamics together with existing Open-Source tools for static network analyses will be the backend of an innovative web application which will be offered as an analysis tool for the renewable conversion of power systems first in islanded and then also in interconnected operation. Here elena int. is already working on a prototype for the planning of micro power grids. This software can then be used for the planning of micro power grids (in the global south) and for the new concept of cellular energy systems in Europe.
As an Open-Source Software PowerDynamics.jl was also already used for creating an online simulation environment for an online course about "inertia requirements for renewable power systems" 10 . The goal of the course is to guide participants through the topic of inertia issues and possible solutions for highly renewable power grids 11 .

Conclusions
PowerDynamics is an OS Julia package for transient stability analysis which is both suitable for highly renewable power grids, especially relevant for microgrids, and large-scale networks with a large number of buses due to its performance advantages. It allows for the integration of delays and fluctuation dynamics of renewable power sources. Due to its decoupled representation of node and line components, it can be parallelized and allows for future performance optimization with GPU calculations. PowerDynamics is still a young library, however, several research projects have started that relate to PowerDynamics and push its development. Currently, it is work in progress to integrate PowerModels for the operation point search based on a load-flow analysis. Further, work started on integrating the causal modeling approach in PowerDynamics that allows the representation of inverters as block diagrams as it is coming in control theory to bridge the gap to this community and engage a wider user base into the development and spreading of PowerDynamics.

Conflict of Interest
We wish to confirm that there are no known conflicts of interest associated with this publication and there has been no significant financial support for this work that could have influenced its outcome.