Modelling and Initialization of a Virtual Synchronous Machine for Power System Fundamental Frequency Simulations

The share of renewable energy sources interfaced to the bulk power system through power electronic devices is continuously increasing. As a result, the power systems of the future face challenges of stability and reduced inertia and, to maintain the stability of the low-inertia systems, various concepts of grid-forming converter control schemes have been introduced, which enable converters to behave similarly to traditional synchronous machines. As the research in this area is gaining on importance, the sheer amount of papers with models for different kinds of simulations and the complexity of control algorithms may be daunting for newcomers in this field to understand them and to start modeling such devices. Moreover, different control schemes are often erroneously used interchangeably which adds to the confusion. This papers aims to give a clear understanding of the above aspects by explaining details of modeling and initialization of grid-forming converter control schemes for power system fundamental frequency dynamic simulations (root-mean-square or RMS simulations) by using an example of a virtual synchronous machine (VSM). Starting from comparison of traditional systems and systems with a high share of converters and continuing with a functional overview of virtual inertia based converters in modern power systems, a step-by-step initialization of the static and dynamic model is given, focusing on RMS simulations. Finally, selected simulation examples are shown which illustrate main characteristics of virtual synchronous machines and their comparison to real synchronous machines. The presented dynamic model is made freely available in DIgSILENT PowerFactory format.


I. INTRODUCTION
In December 2019, the European Commission presented the European Green Deal, its flagship plan that aims to make Europe climate neutral by 2050 [1]. To satisfy the Paris Agreement of being CO 2 neutral by 2050, there is an increasing amount of renewable energy sources (RES) being integrated into power systems worldwide [2]. The most represented RES are photovoltaic (PV) panels and wind turbines [3] since they are among the most environmentally friendly and abundant [4] sources. Additionally, the level of RES integrated in the distribution network systems have been continuously growing in the last decade making power systems decentralized [5]. In 2019, a record amount of renewable energy was achieved, accounting for over 40% of the growth in primary energy and 10% in power generation [6]: wind energy provided the largest contribution to renewables' growth (160 TWh) followed closely by solar (140 TWh). To decarbonise the power system by 2050, RES must grow even stronger [6]. Traditionally, power system operation is based on producing electrical energy from thermal and hydro power plants based on synchronous rotating machines (SM). Synchronous turbine-generators (SGs) store kinetic energy and provide rotational inertia on which the synchronized operation of the power system is based [7], [8]. In the event of a power imbalance (e.g. loss of generators, loss of loads, tie line faults, system splits, etc.) inertia helps to reduce the rate of frequency change [9], [10] allowing more time for the automatic control mechanisms and the system operator to take actions [8]. Furthermore, SGs provide voltage control by their reactive power capability. Therefore, SGs and their control mechanisms were the key in maintaining stability of the traditional power systems.
On the other hand, RES behave differently than conventional, centralized generation facilities. Most new RES are connected to the grid via power electronic converters and their power production is more complex to predict. Converter-interfaced units do not provide rotational inertia to the grid (e.g. PV units), even if physical inertia exists on the device-side of the converter (e.g. wind turbines) [7]. In case these units do not participate in provision of systems ancillary services, as is the case in most countries, displacing conventional generators with converter-connected resources can result in reduced systems capability to maintain system angle, frequency and voltage stability [9], [11]. Such systems are often referred to as low-or zero-inertia systems. In lowinertia networks the dynamics are generally faster and new interactions of dynamic phenomena arise between converterbased units and synchronous units [12] which are still being researched. Nevertheless, the technical capability to operate a low-inertia system exists which means that, if adequately implemented, the converter-based power system could be superior to the traditional one both in terms of controllability as well as in the response time to deviations and disturbances.
The challenge of operating a low-inertia system is not a challenge of tomorrow, but already of today. Although lowinertia systems are characteristic for island systems such as UK or Nordic countries, the reduction of inertia is happening in large, interconnected systems such as the continental Europe as well. Therefore, system operators of smaller, island systems as well as ENTSO-E have recognized the need for utilizing the advantages of converter based power plants into the ancillary service markets, such as emulated inertia, fast frequency response, voltage control, etc. [13]- [18].
From the perspective of control in AC grids, power converters can be classified as grid-feeding (i.e. grid-following), grid-supporting and grid-forming [19], and they act as interfaces between RES and the grid [20]. Nevertheless, from the electric circuitry perspective, the difference lies in whether they are modeled as a current source (grid-following) or as a voltage source (grid-forming). In both cases, grid-supporting means that they provide ancillary services by modifying their setpoints based on local measurements [21], although some research suggests the distinction is even more subtle and depends on the implemented control algorithm [22]. Today's converter-interfaced resources that are connected to the bulk power system are typically grid-following in order to precisely regulate the active and reactive power exchanged with the network. On the downside, they must be synchronized with the grid voltage of the point of common coupling (usually via a phase-locked loop), cannot operate in island mode (at least one synchronous generator or a grid-forming converter is necessary) and have trouble operating in weak grids. To tackle this issue, the concept of Virtual Synchronous Machine (VSM) has been introduced in the literature. It is designed to mimic the basic dynamic behavior of a classical synchronous turbine-generator, which is providing inertia, damping, primary frequency control, voltage control and capability to operate off-grid.
There are different implementations of the VSM in the literature and they are often mistakenly perceived as identical. High-fidelity models are usually found in the literature that include either semiconductor switching phenomena or averaged models in the electromagnetic transient (EMT) time domain. What the existing literature is missing is a comprehensive overview of VSM implementation and simulation in commercial power system simulation software, which often has limited or less flexible dynamic simulation environment compared to e.g. Simulink. This paper aims to demystify and clarify different VSM implementations and provide a comprehensive step-by-step guide on how to implement it in commercial power system simulation software starting from steady-state model (load flow), through dynamic framework with defined inputs and outputs to the initialization of the dynamic model, and finally RMS simulations which are still the key in performing dynamic simulations of bulk power systems. However, RMS models lose accuracy with higher share of RES and reduction of synchronous generation since the detailed control systems can't be adequately represented in RMS simulation tools [23]. The ten VSG solutions with focus on the active power control and their tuning procedure are described, and then experimentally tested in [24]. However, the referred paper does not present how to implement models in commercial power system simulation software, which was the motivation for this paper.
The paper is envisioned as a tutorial to assist the earlycareer researchers, students, practical engineers in the industry as well as others and contributes by: 1) Providing a review and technical explanation of the different grid-forming converter control schemes that emulate the behavior of a synchronous machine, with a goal to demystify their subtle differences and to show how to implement a detailed dynamic model with a straightforward initialization procedure. The later is given as a step-by-step guidance emphasizing the key aspects and pointing out the usual pitfalls.
2) Enabling open source/access model presented in the paper as a backbone for future research and development of the above mentioned target groups.The repository link of model is given by [25]. The prerequisite background of the reader, required to understand and follow this paper, is the basic understanding of power system analysis (load flow), power system dynamic operation and modeling of systems of differential equations.
The rest of the paper is organized as follows: Section II gives an overview of the different implementations of virtual inertia (VI)-based converters. In Section III a static model, system layout and values of used parameters are explained to execute load flow analysis. The dynamic models are presented in Section IV. Initialization of the model is given in Section V. Finally, selected simulation examples are provided in Section VI.

II. IMPLEMENTATIONS OF THE VIRTUAL SYNCHRONOUS MACHINE
Virtual inertia (VI)-based converters solve the problems introduced by converter-connected sources. In literature, VIbased converters can be found under different names, such as: virtual synchronous machine (VSM), virtual synchronous generator (VSG), synchronverter, etc. Each concept arose from the structure and mathematical model of the SM [26]. SM includes synchronous generators and synchronous motors. All the control methods present an idea of operating a converter like a synchronous machine, however there are minor differences between each of them [27]. The equivalence between a conventional synchronous machine and a voltagesourced converter (VSC) emulating a synchronous machine is illustrated in Fig. 1. An analogy exists between the DClink voltage and the grid frequency, as well as capacitance and the moment of inertia. The DC link capacitor serves as a storage capacity for transient active power changes, similar to the rotating inertia of SMs. Therefore, the DC-link voltage changes when there is an imbalance between the input power from the device-side and the AC power delivered to the network. The DC-link capacitors can provide the short-term power used for inertia emulation assuming there is enough energy in it, but in all cases the DC link balancing power must come from the device side (wind turbine, battery, solar PV, supercapacitor, . . . ) in order to keep the DC link voltage inside operational limits necessary for making the converter operate properly.  Figure 1: Equivalence between a conventional synchronous machine and a converter emulating a synchronous machine A general mathematical model of all VI-based converters is based on emulating the swing equation (1) of a SM [20]: where P gen is the generated power, P load is the power demand with losses, J is the total machine inertia, and ω r is the rotor speed. The above equation shows that the relation between the SM rotor speed (system frequency) is proportional to the imbalance between generation and load. The VI-based converters allow RES to participate in frequency regulation by controlling their output power during frequency disturbances. The ability to choose and change the parameters during operation (e.g. inertia, field inductance, mutual inductance and friction coefficient) shows a great advantage over actual SGs. Unlike real SMs, these parameters are not defined by the actual construction of the machine; rather, they can theoretically be completely arbitrary since they are just a part of the control software. However, the parameters need to satisfy design constraints to ensure converter stability and satisfactory dynamic performance. Poorly chosen virtual machine parameters may cause instability, for example: large values of virtual inertia or droop gain. A careful tuning of controller parameters is necessary to ensure stable operation of the converters individually and together in the system. Three most common topologies of the VI-based converters are shown in Fig. 2, in which the gray blocks and black lines are common for all implementations. All VI-based converters consist of a conventional voltage source converter, filter inductors, capacitor and the respective control system. Their differences can be noticed in the control system.

A. VSM
The VSM is a grid-forming control scheme which emulates the static and dynamic performance of a SG. General structure of the VSM is shown in Fig. 2 by gray and blue elements. Its virtual inertia topology is based on the swing equation with frequency droop control (2), written in per-unit (p.u.) in this case.
where ω V SM and ω * V SM are the speed of the virtual rotor and speed reference in p.u., respectively; T a is the virtual mechanical time constant equivalent to 2H in a traditional SM in seconds; p * is the external power reference in p.u.; p is the measured output electrical power in p.u.; k ω is the droop gain of the virtual governor, k d is the damping constant andω g is the measured grid frequency in per-unit. In a real SM damping torque depends on the operating point of the machine. A model with constant k d will not mimic the SM behavior throughout the entire operating range [28]. Referring to Fig.  2, the output of the reactive power control is the voltage amplitude reference while the outputs of the active power control block, which includes the swing equation, are the frequency and phase angle references. These references serve as inputs for the cascaded voltage and current control [29]. An application of the cascade loop ensures controllability and flexibility for protection strategies, because limitations of the voltages and currents of the converter are included [28].
The phase-locked loop (PLL) estimates the actual grid frequency by measuring the grid voltage and is used only for the implementation of the damping effect in (2  control rely on it as in conventional, grid-following VSC [29]. A virtual impedance is included in the VSM to facilitate power-frequency droop control in resistive grids [30]. Since it is based on the mathematical model of a SG, the VSM is modeled in a rotating direct-quadrature (dq) reference frame (in this case, in the synchronous reference frame) [29]. The first model was introduced in 2007 in [31] and named VIrtual Synchronous MAchine (VISMA). Unlike the VSM described above, VISMA is considered as a current source and cannot operate in island mode. In [32] a three-phase model of VISMA is presented. Compared to a dq model, a three-phase model of VISMA is more adequate under asymmetrical load conditions or during rapid disturbances in the grid [32]. There is also the Institute of Electrical Power Engineering (IEPE's) topology that represents the VISMA operating as a voltage source which offers better performance in islanded mode, however in the grid-connected mode it exhibits some issues during grid synchronization due to transient currents [33].
Another approach is the virtual synchronous machine with zero inertia constant H (VSM0H) [34], [35]. VSM0H has no PLL and no inner current loop. Active and reactive power control loops generate the voltage amplitude and frequency based on reactive power -voltage and active power -frequency droops, as shown by (3) and (4).
where D f , v * and D v are frequency droop, external voltage set-point and voltage droop, respectively. The active and reactive power measurements are filtered using boxcar filters which remove higher harmonics during unbalanced or distorted grid conditions while ensuring steady values of frequency and voltage amplitude. However, a phasor measurement unit (PMU)-class frequency measurement is needed for the filtering. Because there is no "inertia", this control strategy does not reduce the rate-of-change-offrequency (RoCoF) right after the event, although the nadir is well-managed. Significant frequency deviations can cause large changes in output power.
Finally, an advanced version of VSM is the Self-Tuning VSM (ST-VSM) which varies the damping coefficient and virtual inertia constant during operation using online optimization algorithms [36].
It should be emphasized that the power conversion is usually achieved indirectly by back-to-back converter scheme, due to decoupling between the grid and the RES. The backto-back converter configuration is divided into a machineside converter (MSC) and a grid-side converter (GSC) with separated control systems of both parts [37]. GSC have to control active and reactive power flows, while MSC have to manage the DC voltage needs, as it is presented in [38]. Therefore, the previously presented control structures are usually used as a control system of the GSC. Alternatively, the GSC can control the DC voltage and the MSC controls the active power. According to the article [39], there are two approaches to the VSM classified by the control objectives. The previous presented VSM structures are PQ-VSM, since they control the active and reactive power outputs with the precondition that the DC-link voltage is constant or is well controlled. Another approach is DCVQ-VSM that controls the reactive power output and the DC-link voltage. The main purpose of the DCVQ-VSM is to utilize the dynamics of the DC-link capacitors to provide similar inertial response as rotor of a SG. Instead of the active power controller of the PQ-VSM, the DC-link voltage controller is used to achieve the inertia effect and the grid synchronization, e.g. [39].

B. VSG
Generally speaking, VSG can be considered to fall under Swing-Equation Based Inertial Response (SEBIR) meth-ods which add the swing equation to dq-axis controllers with current injection (DQCI) [34]. General structure of the VSG/SEBIR converter is shown in Fig. 2 by gray and red elements.
After a power imbalance in the system, SEBIR will detect the RoCoF and add the signal ∆P proportional to RoCoF active power reference. The virtual inertia results using SE-BIR were not promising [35], because the response time of SEBIR is slower than inertial response of conventional SMs [40]. SEBIR converters require a PLL for synchronization, regardless of the way the grid frequency is estimated for inertial response. This type of control is also well-known under virtual or synthetic inertia control.
The swing equation for inertia emulation of the VSM (2) can be reformulated to extract the power reference expression (5). It is assumed that the electrical power p is controlled by converter. The VSM reference frequency is replaced by the grid reference frequency ω * g and the VSM rotor frequency ω V SM is replaced by the measured grid frequencyω g [28].
k ω is the steady state droop gain and k i the inertia constant [28]. Depending on the sign of the dωg dt , the VSG will absorb or inject power.
With this approach, the internal model of the machine inertia is not established and an external voltage and physical inertia must exist [28]. Consequently, the VSG can not operate in islanded mode. The output of the reactive power control is the reactive power reference q * * , and the output of the active power control is the active power reference p * * [26], [28], as shown in Fig. 2. Current references are calculated based on p * * and q * * , and the measured grid voltage at the point of common couplingṽ. The term VSG has been coined around 2008 [41] based on some earlier research in this area, but it does not imply the capability of standalone operation, unlike a real SG.

C. SYNCHRONVERTER
General structure of the synchronverter machine is shown in Fig. 2 by gray and green elements. Synchronverter's topology is based on a synchronous generator model. The synchronverter can be illustrated like a SG with a small capacitor bank connected in parallel to the stator [26], [42]. In dynamic equations of SG, the mechanical power exchanged with the prime mover is replaced with the power exchanged with the DC bus. A synchronverter has all advantages and disadvantages (e.g. oscillations around the synchronous frequency) of a SG. The synchronverter can be operated like a synchronous motor. The concept of the synchronverter is similar to the VSM, but with simpler control logic. The initial model of the synchronverter required a PLL for grid synchronization, but once synchronized, the PLL is not needed anymore. Unlike the VSM, the voltage amplitude reference and phase angle are directly used to drive the gate signals [28], as shown in Fig. 2. Therefore, this implementation does not have the cas-caded voltage-current control loops. Consequently, the constraints of the converter cannot be included in this structure and the protection is harder to implement [28]. An advanced model is self-synchronizing without the PLL requirement. It is substituted with an active power synchronization loop with virtual impedance.

D. SUMMARY
Although in literature (e.g. [43]) these three control strategies (VSM, VSG/SEBIR and synchronverter) are often perceived as identical, subtle differences have been shown and explained. Even though historically the VSG implies gridfollowing current source behavior, we argue it is much more intuitive to equalize the terms VSM and VSG to imply gridforming voltage source behavior since all synchronous generators can operate without the presence of an external grid. Thus, the differentiation between VSMs and grid-following units with virtual/synthetic inertia enabled (SEBIR) would be clearer and more intuitive. VSM taxonomy is given in Table  1.
There are others approaches of VI-based converters, such as: inducverters that behave as induction generators; a VIbased static synchronous compensator which mimics synchronous condenser and virtual oscillator controller (VOC) that enables synchronization of RES without dedicated communication [33]. Their implementations are out of the scope of this paper.

III. STEADY-STATE REPRESENTATION
In this paper, the modeling and simulations are done in DIgSILENT PowerFactory, however the presented procedure is equivalent for any other commercial power system simulation software. The shown VSM model is used to control a static self-commutated switching power converter supplied from an electric energy source.
The first step is to create a 1-port network element which represents a converter-based source and is interfaced to the power system model via a VSC (e.g. a static generator element in PowerFactory, a disperse generation element in NEPLAN, etc.). The static generator is connected to the grid through an three-phase LC filter to attenuate higher harmonics due to switching. However, this functionality will not be visible in RMS simulations since it assumes only the fundamental frequency component and no higher harmonics. Nevertheless, LC filter will impact reactive power flow. The presented VSM model can be implemented without the LC filter for RMS simulations, but this is beyond the scope of this paper.
To enable the grid-forming behavior, the static generator must necessarily be considered as a voltage source, not current source by setting the appropriate parameter in the network element. For load-flow analysis, the static generator can operate in PQ (operating with defined active and reactive power injections), PV (operating with defined active power injection and controlling the terminal voltage magnitude) or as a swing bus if this element is a reference machine in the VOLUME X, 2021 • a greater discharge of ESS than VSM with fixed parameters • sufficient processing power should be provided • high computational burden power system model (defining the voltage magnitude and angle of the respective bus). For a simple single-machine infinite bus system shown in Fig. 3 (single-phase equivalent circuit) the static generator is set as the reference machine in islanded mode, while in grid-connected mode the external grid is set as the reference. This is important so that the reference frame transformations are done correctly; note that the reference machine defines the global (grid) synchronous rotating reference frame.
In Fig. 3, the components that physically exist are shown in black. The external grid is represented by a voltage source with the corresponding internal impedance which can also include coupling impedance of lines and transformers. The dashed teal part shows the equivalent virtual circuit of the VSM and it has no physical meaning. Analogous to the equivalent circuit of a SG, the controlled current is obtained by summing the voltage vector measured at the point of common coupling v o and the voltage drop across the virtual impedance. Hence, v V SM =| V V SM | δθ V SM would be the equivalent of the internal electromotive force (EMF) of a SG. The relation between voltage vectors is shown in Fig. 4. The modeling and control of the electrical system is implemented in the synchronous rotating reference frame. The global reference frame is aligned with the reference machine voltage vector and it rotates with the speed of the reference machine, in this case with the external grid v g . All other voltage vectors are phase displaced relative to the phase of the reference machine. Therefore, the measured values have to be transformed to the VSM-oriented dq frame to be used in the VSM control strategy. For example, the voltage v o is measured at the point of the common coupling and it leads the grid voltage vector by the phase angle δθ o . To transform it to the VSM-oriented reference frame, expression (6) is used: Since the main idea of the VSM converters is to emulate behaviour of the SM, their implementations can be based more or less explicitly on a mathematical model of a SM. The models of SG can be classified from the 9 th order to the 2 nd order, depending on the desired degree of complexity and accuracy in reproducing the SM dynamics. One is free to model the swing equation and voltage amplitude set by reactive power controller (2 nd order [29]), or reduced SM model combined with mechanical dynamics (5 th or 4 th order model [46]), or full order SM model (7 th order [31]), or even an accurate model of an SG based on the type of the turbine (9 th -or 8 th -order, with hydraulic or steam turbine, respectively). For pedagogical reasons, the VSM implementation presented in this paper is based on the 2 nd order SG model, which assumes a constant flux, while neglecting the transient and sub-transient dynamics. Fig. 5 shows the VSM model from [29] implemented in this paper with all the necessary blocks and signals to implement it in commercial power system simulation software. Each component can be described by equations or block diagrams. A general control strategy of VSM consists of: voltage and reactive power control, virtual inertia and active power control, virtual impedance, voltage control, active damping, current control and phase-locked loop (optionally for implementation of the damping term; not needed for the synchronization or control).

A. REFERENCE FRAME TRANSFORMATIONS
The measurement devices in the simulation packages (e.g. PowerFactory) usually output the real and imaginary components of the measured values in the global SRF aligned with the reference machine. The variables in the global reference frame are labeled with "r" (real) and "i" (imaginary) indices. The variables used as input signals for the VSM model must be transformed to the local VSM-oriented reference frame (labeled with "d" and "q" indices) by (7). This is what the ri/dq transformation block does.
The d-component and q-component are given by (8) and (9): The currents are transformed analogously using the same rotation by e −jδθ V SM . The voltage v o is measured at the point of the common coupling, i.e. at the capacitor (Fig. 3). The direction of the currents i o and i CV is shown in Fig. 3. Note that special attention must be paid to the connection of the measuring elements so that the signs of the measured values are correct and consistent throughout the model. Otherwise, the model may become numerically unstable. This is especially relevant for measuring various current signals from the grid model since the measuring element may return either positive or negative values of the same current depending on the location of measurement connection and the orientation of the measuring element. For example, the dynamic model may want to generate positive inner current references for power flowing out of the machine, while the measuring element may return negative currents for the same direction of power, thus the control system may become unstable and not numerically converge.
Likewise, control signals for the static generator have to be transformed from the VSM frame back to the global frame using the inverse transformation (rotation by e jδθ V SM ). This is what the dq/ri transformation block does (explained in more detail in section IV-H).

B. VIRTUAL INERTIA AND ACTIVE POWER CONTROL
The implementation of this block is based on virtual swingequation (10) that introduces virtual inertia and damping effect of the SG.
External grid cf vo p, q p * is external power reference and p is the output electric power measured after the LC filter (11). The damping effect is achieved by the damping constant k d and comparing the VSM angular speed ω V SM to the estimated grid frequency ω P LL . The power-frequency droop is defined by the droop gain k ω and difference between the VSM angular speed and its reference value ω * V SM .
The solution of the equation (10) gives the VSM angular speed ω V SM . To determine the VSM phase displacement, it is necessary to define the speed deviation δω V SM (12): where ω g is the per-unit speed signal of the reference machine which defines the global reference frame speed. Note that this signal does not exist in reality and it is only needed to properly execute the RMS simulation and define the relative position of the VSM to the reference machine. In reality, this signal would have to be estimated by a PLL, PMU, etc. The VSM phase displacement δθ V SM is defined by (13): where ω b = 2πf b is the rated (base) angular grid frequency, equal to 100π for a 50 Hz system. This phase angle is used to create the VSM-oriented reference frame.
Output ω V SM is connected to the f * input of the static generator model. This connection is only active if the static generator element is set as the reference machine in the system or a swing node, thus f * := ω V SM . This is necessary because it defines the speed of the global reference frame as well as the grid frequency if the converter operates in island mode.

C. PHASE LOCKED LOOP
The PLL is used to estimate the actual grid frequency. The input signal is the voltage v o in the grid reference frame (14). Unlike the article [29] where both d-component and q-component of the voltage are estimated, this implementation transforms the positive-sequence voltage v o in the grid reference frame to the PLL reference frame by zeroing out the d-component of the voltage in the steady-state. The qcomponent is then: The first order low-pass filter is then used on the q-voltage component, where ω LP is the cut-off frequency (15). The filtered signalv P LL q,o is the input signal for the PI regulator estimating the grid frequency deviation δω P LL (16).
The angular speed displacement is used to define the phase displacement (17): The actual grid frequency estimated by PLL is given by expression (18):

D. REACTIVE POWER CONTROL
The reactive power controller provides the voltage amplitude reference v * * . The measured reactive power q is first filtered (19) and then used to calculate the voltage reference (20).
ω f is cut-off frequency of the low-pass filter,q is filtered value of the reactive power, v * is the external voltage amplitude reference, q * is the reactive power reference and k q is the reactive power droop gain. The reactive power can be expressed in scalar form in the VSM reference frame as: With the reactive power controller, the voltage droop control of the traditional SG is achieved. However, note that the VSM control structure directly controls the magnitude of the internal virtual EMF of the converter, while controlling some remote terminal in the grid is not possible in this implementation.

E. VIRTUAL IMPEDANCE
The voltage amplitude reference is passed through a virtual impedance before it is used as an input signal to the cascaded voltage and current control. Virtual impedance has equivalent impact to the VSM phase angle as the synchronous impedance of the SG. The output signals of this block are the dq reference voltage components given by expressions (22) and (23): where r v and l v are virtual resistance and inductance shown in teal color in Fig. 3. The virtual impedance determines the sensitivity of the VSM model to the changes in grid conditions. Lower magnitude of virtual impedance increases the model sensitivity. Virtual impedance is necessary in power networks in which the resistances are on the same order of magnitude as inductances (e.g. MV/LV networks) in order to achieve decoupled control of active and reactive power.

F. VOLTAGE CONTROL
The voltage controller provides the current references for the inner current controller. The d-axis and q-axis voltages are compared to their references and then passed through PI controllers producing the current references i * CV . The dq components of the current references are given by expressions (24) and (26): where k pv and k iv are the PI controller gains, c f is per unit value of the filter capacitance and k f f i is the feed-forward gain that can be set to either 0 or 1 enabling or disabling the feed-forward of measured currents flowing into the grid. 1 and 2 are integrator states of the PI voltage controllers.

G. ACTIVE DAMPING
The block of the active damping is used to attenuate oscillations of the filter capacitor voltage v o using a high-pass filter (28). The resulting filtered values are given by expressions (29): where φ represents the state of the filter with cut-off frequency ω AD . These equations are used for both d-component and q-component. The VSM model can operate without active damping, but in this case the transients will be more pronounced.

H. CURRENT CONTROL
The current controller provides the voltage reference for the static generator. Firstly, the measured converter currents and the current references are compared. Then, their difference is passed through a PI controller generating the voltage reference for the static generator. The d-component and qcomponent of the output signals are given by expressions (30) and (32): where k pc and k ic are the PI controller gain, l f is perunit value of the filter inductance and k f f v is the feedforward gain. γ 1 and γ 2 are integrator states of the PI voltage controllers. The resulting voltage references are then divided by the measured DC-link voltage to generate the modulation signals used to control the static generator. Modulation signals are generally in the range [0, 1] to avoid overmodulation issues. In this case the DC link voltage v DC is considered stiff and equal 1 p.u. because the source connected to the DC side of the converter and the respective DC voltage dynamics are beyond the scope of this paper. The DC dynamics exists in an actual system, but it is usually implemented in the DC-DC converter or another VSC operated in the back-toback scheme. However, this paper only introduces the PQ controlled grid-side VSC. The input signals of the converter (static generator) are defined by (34): Before the signals are sent to the converter, they must be transformed back to the grid (global) reference frame by VOLUME X, 2021 rotating the dq space vector by e jδθ V SM , described by (35) and (36): Notice that the voltage and current controllers are presented with the cross-coupling between the d-component and q-component to improve tracking. Cross-coupling is not needed and it has no effect in the steady-state, however transient behavior is improved with cross-coupling.
For simplicity reasons, integrator windup issues are not considered in the described model as they are beyond the scope of this paper. Nevertheless, all the PI controllers can be replaced with PI controllers with anti-windup limits.

I. CURRENT LIMITATION
Although the RES are conceived to export all available active power, they should have the reactive power capability to set the grid voltage at the desired value. By replacing the conventional power generators with RES, the voltage regulation has to be carried out by positioning the terminal voltage at a predefined level. In order to maintain power quality and system reliability, the RES should possess the low-voltage ride-through (LVRT) capability. LVRT capability implies the injection of balanced/unbalanced active/reactive currents to the grid during voltage sags. With this, the current limitation is especially necessary during grid faults, due to higher possibility of converter saturation and synchronization instability [47].
One solution is to oversize the converter to handle larger currents, but it causes higher converter costs. On the other hand, virtual admittance can be decreased to limit the current. Since the virtual admittance needed to achieve a given maximum current changes for different fault conditions, it can be decreased until the admissible current is achieved. However, the maximum allowable current does not depend on fault conditions and control [48].
Therefore, a direct current limiter on the reference values is usually implemented. The output currents are limited, and the reference values set to the outer power controllers are adjusted based on the grid code requirements [48]. During normal operation mode, the converter usually operates at unity power factor, while injected reactive power to the grid is zero, i.e. the active power has precedence. During a symmetrical fault where the converter injects balanced currents, the converter current can be limited as follows (37): assuming that q component is reactive, d component is active, and I max is the maximum current limitation [49]. The current limitation should be implemented inside the VSM control, i.e. between the voltage and current controllers, as shown in Fig. 6. However, during low-voltage conditions where the reactive power is prioritized, the active component of the converter current is set to fulfill (37) [49]. If V min is between Figure 6: Current limitation control of voltage source converters 0.9 and 1.1 p.u. the converter operates normally. If V min is between 0.5 and 0.9 p.u. the converter injects both active and reactive powers simultaneously. If V min is smaller than 0.5 p.u. the converter injects only reactive current to the grid. The minimum voltage amplitude (V min ) is determined dynamically by the controller, which is utilized for the detection of the voltage sag and operation mode [47], [50]. The reactive power can also take the precedence during normal operation.
In the proposed model, the current limitation and priority control modes are not considered for simplicity reasons.

V. INITIALIZATION OF THE DYNAMIC MODEL
The model initialization starts with the grid elements and load flow calculation that provide the static generator modulation signals u * r and u * i . The initialization of the dynamic model is usually performed output-to-input. The output signals of the network elements are usually known from the steady-state load flow calculation and they are used to initialize the inputs by backtracking through the model. Then, all states variables and all unknown input or output signals must be initialized. The general rule is to set all derivatives to zero ( dx dt = 0 or s = 0, i.e. the steady-state is observed) and solve the system of (non)linear equations.
However, with a model this complex there are two issues with the general procedure described above. Firstly, commercial power system simulation software usually has less flexible and inferior interface for dynamic modeling and initialization compared to e.g. MATLAB/Simulink. Therefore, instead of solving a complete set of equations at once, which sometimes the software will not allow or will fail at, it is better to initialize the model in a modular manner i.e. by doing it for each block separately which is also easier to debug. Secondly, such a model cannot be sequentially initialized output-to-input since there are not enough data known starting from the output of the dynamic model (which are modulation signal inputs to the static generator element). Some data from the dynamic model input is also known and the whole model must be simultaneously initialized input-tooutput and output-to-input while solving the conflicts when the two sides meet. Therefore, this paper presents a modular, straightforward initialization procedure.
The initialization flowchart is summarized in Fig. 7. The process is described in more details in the following subsections. The integration step size of the RMS simulation should be set to 1 ms with 10 ms maximum step, otherwise oscillations due to numerical stability issues will arise.

A. INITIALIZATION FROM INPUT TO OUTPUT
PLL input signals are known and the outputs need to be initialized. In the steady-state the angular speed deviation is zero and the actual grid frequency corresponds to the rated value (38). The phase displacement is initialized using positivesequence voltage components calculated by load flow. It is equal to the phase angle of the measured voltage (39) relative to the reference machine.
In steady-state the VSM angular speed deviation is zero and the VSM frequency is equal to ω * g . To initialize the VSM phase displacement, voltage at the point of common coupling v o and the current flowing to the external grid i o must be measured. According to Fig. 3 the current i o causes the voltage drop across the virtual impedance. Consequently, the VSM internal voltage vector is defined by (40): Note that the v o , i o are left in the global reference frame since the internal load angle of the VSM is defined as relative to the reference machine. The steady-state value of the VSM phase displacement is defined by real and imaginary components of the VSM voltage (41): The external power reference p * is equal to the measured power p in steady-state.
Observing the equations (28) and (29), it can be noticed that the filtered values will be equal to the voltage v o . Therefore, the output signals of the active damping in the steady-state are zero (42) meaning that there are no voltage oscillations. However, input to the active damping block is v o , which must be transformed to the VSM-oriented reference frame during initialization. v * AD = 0 (42)

B. INITIALIZATION FROM OUTPUT TO INPUT
The previously described initialization involved the known input signals and unknown outputs that needed to be initialized. The initialization of the voltage and current controllers is performed output-to-input. The converter control signals are known from load flow calculation and used backwards through the current/voltage controllers, virtual impedance and reactive power controller, initializing all of them. The converter signals u * r and u * i are measured in the grid reference frame. Hence, they are first transformed to the VSM-oriented reference frame by (43) and (44).  and i CV also have to be transformed to the VSM-oriented reference frame after δθ V SM was initialized by (41).
Then, the converter voltage references are given by (45) and (46): In steady-state the current references, that are input signals to this block, are equal to the measured currents i CV . Therefore, there is no control error before the PI controllers and the state variables are initialized based on the known outputs (47) and (48): (48) Similarly, the voltage controller is initialized. The steadystate values of the voltage references are equal to the measured voltage v o . The state variables of the voltage PI controllers are given by (49) and (50): Since the voltage reference for the voltage controller v * o is equal to the measured voltage v o , the voltage amplitude reference v * * can be expressed by (51): Note that in steady-state v * * should be equal to the v V SM (40), that is, in the VSM reference frame, the q-component is zero and only the d-component persists and is analogous to the internal EMF of a synchronous generator. The input external voltage reference of the reactive power controller in steady-state can be calculated by equation (52). But since the derivations are zero, the filtered valueq correspond to the measured reactive power q. And since in steadystate, the measured reactive power is equal to the reactive power setpoint, the external voltage setpoint is equal to the desired value of the internal virtual EMF

VI. DYNAMIC SIMULATION EXAMPLES
This section analyses the behavior of the described VSM model in both grid-connected and islanded operation. For simulation purposes, a load is connected in parallel to an external grid. Simulation examples show the response of the VSM to a step increase of active power of a load connected to the external grid bus. The step change happens at t = 4 s. The external grid is modeled as a voltage source behind transient reactance (classical model of a synchronous generator, with no AVR and turbine-governor). The control configuration described in sections III and IV is valid for both operation modes without any modification.

A. OPERATING IN GRID-CONNECTED MODE
When an increase in load power occurs, the static generator increases its output power p o to maintain power balance, as shown in Fig. 8(a). The VSM virtual rotor speed follows the grid frequency, and at the instance of the step change in load power, it releases energy from its virtual inertia to oppose the change in frequency followed by the primary frequency response to equalize the generation and consumption, and stabilize the frequency (Fig. 8(b)). Conversely, the external grid power temporarily decreases and returns to the same value since it exhibits a behavior of an unregulated SG. Analogous to a SG, with an increase in power demand, the virtual rotor frequency decreases. As soon as the power balance is re-established, ω V SM stabilizes and remains at a quasisteady-state value, but lower from the frequency setpoint. The VSM phase displacement will also increase as shown in Fig.  8(c).

B. OPERATING IN ISLANDED MODE
In islanded mode, the static generator is set up as a reference machine while feeding the local load. The response of the virtual rotor frequency ω V SM in islanded mode is similar to the response in grid-connected mode. However, ω V SM experiences more oscillations due to the lack of physical inertia in the system, as shown in Fig. 9(b). It also requires more time to stabilize at a new value. The static generator will respond to an increase in load power in islanded mode with an instantaneous increase in output power p o ( Fig. 9(a)), provided there is enough stored energy in the buffer of the converter-interfaced source.

C. SENSITIVITY OF THE VSM MODEL TO PARAMETER CHANGES
In these simulations the VSM also works standalone, powering the local load at the end of the line. A step increase in load power is introduced at t = 4 s. The mechanical constant T a is equivalent to 2H in a traditional SM, playing a vital role in maintaining the frequency stability of the power system. The higher the value of the mechanical constant the smaller the frequency nadir and RoCoF, but the settling time is longer (time to reach steadystate value within certain accuracy). However, lower value of T a may cause unwanted oscillations in the system. According to Fig. 10(a), it can be deduced that T a has no impact on the steady-state value, just like an actual machine. The virtual inertia has the same effect as the real inertia, ie. the system is more stable when it increases, provided that the converterinterfaced source has an adequate energy buffer.
With droop gain k ω , the power-frequency droop is achieved defining the steady-state frequency deviation after a generation-load disturbance. As shown in Fig. 10(b), the lower the value of k ω , the bigger the frequency nadir and steady-state deviation, while the settling time is longer. However, too large of a droop gain may destabilize the system. The next parameter impacting the VSM behavior is the damping coefficient k d . In the presented VSM implementa- tion, the damping effect is achieved by comparing the VSM rotor frequency to the speed of the reference machine / grid frequency which tends to drive them towards equilibrium. However, in islanded mode as well as in grid-connected mode in which the external grid is modeled as a rotating mass without primary frequency controller, this implementation, ironically, tends to destabilize the system with increasing k d due to the PLL lag. As shown in Fig. 10(b), lower k d value results in lower maximum deviation and settling time, while it has no impact on the quiescent value.
Alternatively, the damping term can be implemented by comparing ω V SM to the nominal frequency and this would drive the VSM speed towards nominal speed. The damping term tries to emulate the stabilizing effect of damper windings and/or rotor shaft friction and/or frequency sensitivity of load, although the latter is usually included in the load model.

D. REACTIVE POWER CONTROL PERFORMANCE OF VSM
In order to show the reactive controller performance, response of the VSM control system in front of a three-phase voltage sag is also simulated. As shown in Fig. 11a, the ter-VOLUME X, 2021 minal voltages drop. Analogous to a SG, the static generator remains connected to the grid during the fault and responds with an instantaneous increase in output reactive power, as shown in Fig 11c. The control system is able to adjust the voltage reference during voltage sag, as shown in Fig. 11b.

VII. CONCLUSION
Most common virtual inertia converter control schemes: virtual synchronous machine (VSM), virtual synchronous generator (VSG) and synchronverter are often incorrectly used synonymously in the literature and may be confusing to newcomers in the field. In reality, they present three different control designs and this paper gives an overview of them and provides a clear distinction. Then, the paper shows a stepby-step tutorial procedure on how to implement a detailed dynamic VSM model in commercial power system simulation software for fundamental frequency (RMS) simulations and explains the technical and mathematical background of it. The steady-state representation of the VSM model, used to control a power converter supplied from an electric energy source, is presented. The algorithm for straightforward and modular initialization of all the variables is given which avoids solving a complex set of equations simultaneously. Finally, the paper provides a selected dynamic simulation examples to explain and show the impact of different VSM parameters on its dynamic response as well as to verify the ability of the VSM model to operate in grid-connected and islanded modes. Finally, as a contribution of the tutorial the presented models are made open access available to the research community for further upgrades and customized implementation [25].

APPENDIX. INCLUDING DC DYNAMICS AND DEVICE DYNAMICS
The VSM control offers a promising way to design advanced control systems for RES. However, it is in an evolving state, and practical applications still need to be improved. Since previous sections present only the GSC dynamics, the DC dynamics and device dynamics are reported in this section. The applications of the VSM control strategy in PV system and battery system are described. These models are also made open access [25] to allow students and young researches to assess the performance of the VSM under different (deloaded) operating conditions, with stochastic input power or different SOC values of BESS, or to modify the model with alternative control schemes.

A. DYNAMIC MODEL OF ROOFTOP PV SYSTEM
The control system applied to PV system is based on the DCVQ-VSM configuration, i.e. instead of active power controller, the DC-link voltage controller is used to provide inertial response. The principle of VSM implementation in PV system is shown in Fig. 12.
Firstly, the PV array model is introduced. Its model is available in DIgSILENT PowerFactory, and described by equations (53) -(56) [51]: where N s is a number of modules in series that form a string, N p is the number of strings in parallel that form an array; Secondly, the DC link is necessary to model, due to decoupling DC power and AC power. It also acts as an energy buffer to ride-through power disturbances. As given by (57), DC-link voltage is function of the power injected into capacitor from the PV p P V and the converter power output taken from the capacitor p.  Figure 12: Principle of VSM implementation in PV system: user-defined dynamic model elements are in gray and network elements are in green Furthermore, the DC link voltage is input to the DClink voltage controller which keeps its value constant. The implementation of the DC-link voltage controller is described by (58), enabling the inertial response of the PV converter and grid synchronization [39]: where ω V SM is the VSM angular frequency; ω g is the frequency setting value obtained by (59); U DC is the DC-link voltage; U ref DC is the reference value of the DC-link voltage; K T is the DC-link voltage tracking coefficient, K D is the damping coefficient; K J is the inertia emulation coefficient; ω 0 is the nominal frequency value; m is the weighting coefficient. The DC-link voltage deviation under grid frequency deviation is determined by the weighting coefficient m. When its value is closer to 1, the DC-link deviations are reduced and exceeding the allowable range is prevented. The VSM phase displacement is defined by (60): where ω b is the base angular grid frequency, and δω V SM is the VSM frequency deviations calculated by (61).
f ref is the global reference frame speed.
The reactive power control, virtual impedance, voltage control, active damping, current control, and reference frame transformation are implemented as described in Section IV.

B. DYNAMIC MODEL OF HOUSEHOLD BATTERY SYSTEM
The principle of VSM implementation in battery system is shown in Fig. 13. The battery model is based on available model in PowerFactory, and described by equations (62) -(65): U cell = U max ·SOC +U min ·(1−SOC)−I cell ·R icell (64) where C r is the amount of serial connected cells, C p is the amount of parallel connected cells; C c is the rated cell capacity in Ah; U max and U min are the maximum and minimum allowed voltages of a battery cell; R icell is the internal resistance per cell [52]. Actually, U min is the voltage of a discharged cell, and U max is the voltage of a fully loaded cell [52]. The integrator in (63) counts the current input and output of the battery. The input signal is DC current I in kA. This block outputs the State of Charge (SOC), battery terminal voltage V term , the current of a single cell I cell , and the voltage of a single cell U cell . DC side provides the DC voltage in V and current in kA by (66) and (67), respectively. The input signals are the measured active power P in MW and the outer DC voltage of the battery V term .
The VSM control of converter used to interface battery system with the grid is created as described in sections III VOLUME X, 2021 -V and shown in Fig. 5. The amount of inertia support that can be provided by the battery system in a VSM needs to be regulated based on the SOC of the battery. Accordingly, the charge controller is implemented between the voltage and current controllers, in order to calculate if the battery has to be charged or not. The input values are SOC and the dcomponent (active component) of the reference current. The charge control checks whether the current and SOC values are within certain limits. It prevents a discharging of the battery if the SOC falls below a certain value (minSOC), and prevents a charging of the battery if the SOC is above a certain value (maxSOC). It should be emphasized that current PI controllers include anti-windup limits.  Figure 13: Principle of VSM implementation in battery system: user-defined dynamic model elements are in gray and network elements are in green In figures 12 and 13 can be noticed that converters are interfaced directly with the grid, i.e. without LC filter. Therefore, the voltage and current controllers of VSM control system are implemented without cross-coupling.

C. SIMULATION EXAMPLES WITH INCLUDED DC DYNAMICS
The modeled battery system has the charging and discharging power of 5 kW and the capacity of 15 kWh. The simulation examples in Fig. 14 show the battery system transition to islanded operation. Before the islanding is triggered, the battery is charged with 3,5 kW. Because of the surplus of the electrical power, the frequency decreases. The virtual inertia releases the energy to maintain the frequency stability. Therefore, the VSM frequency decreases as shown in Fig.  14a. After the reestablishment of the power balance , it is also stabilized at the new value. During islanded operation, the battery system is set as a reference machine. The battery changes its operational mode to the discharge mode in order to supply loads, as shown in Fig. 14c. Since the voltage decreases (14b), the converter increases its output reactive power (14d).
The modeled rooftop PV system has the nominal power of 2,5 kW and it operates in maximum power point tracking.
The simulation examples in Fig. 15 show the PV system response to an increase in load power, while the PV system is connected to the grid. When an increase in load power occurs, the frequency and voltage decreases. The PV system temporarily and slightly changes it output power during power imbalance and returns to the same operational point, as shown in Figures 15c and 15d. The PV response to frequency deviations is reduced in this case because m = 1 (59) so the DC-link voltage deviation under grid frequency deviation is reduced.