# Using Python for the Simulation of a Closed-Loop PI Controller for a Buck Converter
## 2 Simulation of the Open-Loop Operation
### Open-Loop Simulation  
As previously mentioned, the simulation technique will be implemented in Python. Thus, it is important to briefly introduce the main tools that will be used in this context. Python is a high-level, general-purpose programming language that can be used to develop numerical computation applications, such as simulation of physical systems. For this purpose, it is essential to use some Python libraries. The most important ones, in this context, are NumPy and Matplotlib. The Numerical Python package (Numpy) is used to manipulate arrays and matrices, and the Matplotlib package is used for plotting. To simplify the implementation of the simulation technique, the code must be subdivided into different modules, which together form a package. The first module must contain two functions that compute i, i, v and v values during the conduction state (Figure 4) and during the non-conducting state (Figure 5). Figure 3: Buck converter circuit during the non-conduction state. Thus, after discretizing Equation (1), it is possible to construct a function that describes the operation of the converter during the conduction state (Figure 4). This function must receive as input arguments the initial time, t\({}_{0}\), the sampling period, PA, the characteristics of the converter (V\({}_{\text{in}}\), R\({}_{\text{S}}\), L, ESR, C and R\({}_{\text{Load}}\)) and the initial conditions (the inductor current, i\({}_{\text{L0}}\), and capacitor voltage, v\({}_{\text{CD}}\), at the initial time). Following this, the same procedure must be applied for the non-conduction state, in order to obtain the non-conducting state function (Figure 5). The previous function (Figure 5) takes practically the same input arguments as the function in Figure 4. The difference between the two functions is related to the input arguments, as can be seen by comparing Equations (1) and (2). After creating the module which reproduces the conduction and non-conducting states, it is necessary to create a new one: the module that will be responsible for generating the output signal of the Pulse Width Modulator (PWM). This module will be composed of a single function that receives as input arguments the converter switching period (P), the duty cycle (DC), the final simulation time (tf) and the total number of iterations (TAM). In turn, the function returns the time vector, as well as a square waveform with period P and duty cycle DC. Finally, it is necessary to create a new module that contains the main function. The main function needs to import the previous modules (Figures 4-6), as well as the Numpy and Matplotlib packages, so that it can simulate the buck converter. Figure 4: Function that models the behavior of the converter during the on state: (**a**) Algorithm and (**b**) Python code. Figure 5: Function that models the behavior of the converter during the off state: (**a**) Algorithm and (**b**) Python code. Figure 7 shows the main function flowchart. CI represents the current iteration and t, i\({}_{\text{L}}\), v\({}_{\text{C}}\), i\({}_{\text{C}}\) and v\({}_{\text{O}}\) represent the vectors referring to the following quantities: time, inductor current, output voltage component due exclusively to C, capacitor current and output voltage, respectively. Figure 6: Function that generates the output PWM signal: (**a**) Algorithm and (**b**) Python code. Figure 7: Main function flowchart. The conduction stage, non-conduction stage and PWM generator functions are represented in Figures 4-6, respectively. Equation (9) is commonly used to choose the capacitor capacitance. Following this, formulas that allow for the computation of the inductor inductance will be presented. The critical inductance value takes into account the bound condition between the CCM and the DCM. The above condition occurs when the average inductor current is equal to half the ripple of the inductor current. Thus, using Equation (4), it is possible to calculate the critical value of \(\mathrm{L}\) (\(\mathrm{L}_{\mathrm{critical}}\)) as:  
\[