PWM
In the circuit given below, the switches are operated using the pulse width modulation technique. The modulation voltage is $m(t)$, and the triangular carrier voltage is $v_c(t)$. The switch S1 is turned on when $m(t) > v_c(t)$, and the switch S2 is turned on otherwise. The parameter values are $R=10\,\Omega$, $L=1\,$mH, and $V_{dc}=200\,$V. If the modulation voltage is kept constant at $m(t)=2$, what is the average current through the inductor?In [1]:
from IPython.display import Image
Image(filename =r'pwm_1_fig_1.png', width=450)
Out[1]:
In [2]:
# run this cell to view the circuit file.
%pycat pwm_1_orig.in
We now replace the strings such as \$Vdc, \$L, with the values of our choice by running the python script given below. It takes an existing circuit file pwm_1_orig.in and produces a new circuit file pwm_1.in, after replacing \$Vdc, \$L, etc. with values of our choice.
In [3]:
import gseim_calc as calc
s_Vdc = "200"
s_L = "1e-3"
s_R = "10"
s_f_carrier = "1e3"
s_dt_min = "1e-6"
s_dt_nrml = "10e-6"
s_m = "2"
t_end = 5.0*(1/float(s_f_carrier))
s_t_end = ("%11.4E"%t_end).strip()
l = [
('$Vdc', s_Vdc),
('$L', s_L),
('$R', s_R),
('$f_carrier', s_f_carrier),
('$dt_min', s_dt_min),
('$dt_nrml', s_dt_nrml),
('$t_end', s_t_end),
('$m', s_m)
]
calc.replace_strings_1("pwm_1_orig.in", "pwm_1.in", l)
print('pwm_1.in is ready for execution')
pwm_1.in is ready for execution
Execute the following cell to run GSEIM on pwm_1.in.
In [4]:
import os
import dos_unix
# uncomment for windows:
#dos_unix.d2u("pwm_1.in")
os.system('run_gseim pwm_1.in')
get_lib_elements: filename gseim_aux/xbe.aux get_lib_elements: filename gseim_aux/ebe.aux Circuit: filename = pwm_1.in Circuit: n_xbeu_vr = 4 Circuit: n_ebeu_nd = 4 main: i_solve = 0 main: calling solve_trns Transient simulation starts... i=0 i=1000 solve_trns_exc completed. GSEIM: Program completed.
Out[4]:
0
The circuit file (pwm_1.in) is created in the same directory as that used for launching Jupyter notebook. The last step (i.e., running GSEIM on pwm_1.in) creates a data file called pwm_1.datin the same directory. We can now use the python code below to compute/plot the various quantities of interest.
In [5]:
import numpy as np
import matplotlib.pyplot as plt
import gseim_calc as calc
from setsize import set_size
slv = calc.slv("pwm_1.in")
i_slv = 0
i_out = 0
filename = slv.l_filename_all[i_slv][i_out]
print('filename:', filename)
u = np.loadtxt(filename)
t = u[:, 0]
col_v_out = slv.get_index(i_slv,i_out,"v_out")
col_IL = slv.get_index(i_slv,i_out,"IL" )
col_ISrc = slv.get_index(i_slv,i_out,"ISrc" )
col_P_R = slv.get_index(i_slv,i_out,"P_R" )
col_g1 = slv.get_index(i_slv,i_out,"g1" )
col_g2 = slv.get_index(i_slv,i_out,"g2" )
col_s = slv.get_index(i_slv,i_out,"s" )
col_t = slv.get_index(i_slv,i_out,"t" )
t_end = t[-1]
l_IL = calc.avg_rms_2(t, u[:,col_IL], 0.0, t_end, 1.0e-6)
l_v_out = calc.avg_rms_2(t, u[:,col_v_out], 0.0, t_end, 1.0e-6)
t_IL = np.array(l_IL[0])
t_v_out = np.array(l_v_out[0])
print('average IL:', "%11.4E"%l_IL[1][0])
print('average load voltage:', "%11.4E"%l_v_out[1][0])
color1='green'
color2='crimson'
color3='goldenrod'
color4='blue'
color5='cornflowerblue'
fig, ax = plt.subplots(3, sharex=False)
plt.subplots_adjust(wspace=0, hspace=0.0)
set_size(5.5, 4, ax[0])
for i in range(3):
ax[i].set_xlim(left=0, right=t_end*1e3)
ax[i].grid(color='#CCCCCC', linestyle='solid', linewidth=0.5)
for i in range(2):
ax[i].tick_params(labelbottom=False)
ax[0].plot(t*1e3, u[:,col_t ], color=color5, linewidth=1.0, label="$v_{carrier}$")
ax[0].plot(t*1e3, u[:,col_s ], color=color4, linewidth=1.0, label="$m$")
ax[1].plot(t*1e3, u[:,col_v_out], color=color1, linewidth=1.0, label="$V_{out}$")
ax[2].plot(t*1e3, u[:,col_IL ], color=color2, linewidth=1.0, label="$i_L$")
ax[1].plot(t_v_out*1e3, l_v_out[1], color=color1, linewidth=1.0, label="$V_{out}^{avg}$", linestyle='--', dashes=(5,3))
ax[2].plot(t_IL*1e3 , l_IL[1] , color=color2, linewidth=1.0, label="$i_L^{avg}$" , linestyle='--', dashes=(5,3))
ax[2].set_xlabel('time (msec)', fontsize=14)
for i in range(3):
ax[i].legend(loc = 'lower right',frameon = True, fontsize = 10, title = None,
markerfirst = True, markerscale = 1.0, labelspacing = 0.5, columnspacing = 2.0,
prop = {'size' : 12},)
#plt.tight_layout()
plt.show()
filename: pwm_1.dat average IL: 8.0125E+00 average load voltage: 8.0019E+01
This notebook was contributed by Prof. Nakul Narayanan K, Govt. Engineering College, Thrissur. He may be contacted at nakul@gectcr.ac.in.
In [ ]: