# L filter, continuous conduction mode
# all voltages normalized by Vm
# all currents normalized by Vm/wL
# Vin=Vm sin(wt)

from pylab import *

# initialization
npoint = 720 # reduce if the simulation is too slow
Joutmax = 0.5
i = arange(npoint)
deg = 360 * (i + 0.5) / npoint
wt = radians(deg)
phi = wt[0 : npoint/2]
minp = sin(wt)
mx = abs(minp)
s = sign(minp)

nf = 100 # number of frames, reduce if the simulation is too slow

pattern = 1 - cos(phi) - 2 / pi * phi;
app = - 2.0 / (3 * pi) * sin(2 * wt);

Jout = empty(nf)
Mout = empty(nf)
Pin = empty(nf)
Pinapp = empty(nf)
Px = empty(nf)
Pxapp = empty(nf)
Jinrms = empty(nf)
Jinrmsapp = empty(nf)
S = empty(nf)
Sapp = empty(nf)
PF = empty(nf)
PFapp = empty(nf)

close('all')
rc('text', usetex = True)
rc('font', family = 'serif')
rc('font', size = 12)
rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']

figure(0, figsize = (4, 4))#(4, 8))
degtick = (0, 60, 120, 180, 240, 300, 360)

for k in range(nf):
       
    Jout[k] = 0.21 + k * (Joutmax - 0.21) / nf
    mout = 2 / pi
    Mout[k] = mout
    
    w = Jout[k] + pattern
    jl = concatenate((w, w))
    jin = jl * s
    pin = jin * minp
    px = jl * mx
    jlapp = Jout[k] + app
    jinapp = jlapp * s
    pinapp = jinapp * minp
    pxapp = jlapp * mx
    
    Pin[k] = mean(pin)
    Pinapp[k] = mean(pinapp)
    Px[k] = mean(px)
    Pxapp[k] = mean(pxapp)
    Jinrms[k] = sqrt(mean(jin**2))
    Jinrmsapp[k] = sqrt(mean(jinapp**2))
    S[k] = Jinrms[k] / sqrt(2)
    Sapp[k] = Jinrmsapp[k] / sqrt(2)
    PF[k] = Pin[k] / S[k]
    PFapp[k] = Pinapp[k] / Sapp[k]

    clf()
    
    subplot(2, 1, 1)#(4, 1, 1)
    plot(deg, jl, 'b')
    plot(deg, jlapp, 'r')
    ylabel(r'$j_{L} \, [\text{b}], \; j_{L\,app} \, [\text{r}]$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 0.8, 0.8)

    #subplot(4, 1, 2)
    #plot(deg, mx)
    #plot(deg, mout * ones(len(deg)), 'r')
    #ylabel(r'$m_{X} \, [\text{b}], \; M_{OUT} \, [\text{r}]$')
    #xlim(0, 360)
    #xticks(degtick)
    #ylim(- 1.2, 1.2)

    #subplot(4, 1, 3)
    #plot(deg, minp, 'b')
    #plot(deg, - minp, '--b')
    #plot(deg, mout * ones(len(deg)), 'r')
    #ylabel(r'$m_{IN}, \; - m_{IN}, \; M_{OUT} \, [\text{r}]$')
    #xlim(0, 360)
    #xticks(degtick)
    #ylim(- 1.2, 1.2)
    
    subplot(2, 1, 2)#(4, 1, 4)
    plot(deg, jin, 'b')
    plot(deg, jinapp, 'r')
    xlabel(r'$\omega t [^\circ]$')
    ylabel(r'$j_{IN} \, [\text{b}], \; j_{IN\,app} \, [\text{r}]$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 0.8, 0.8)
    
    savefig('lfilterccm{:d}.png'.format(k), bbox_inches = 'tight')

# input power
figure(1)
plot(Jout, Pin, 'b')
plot(Jout, Pinapp, 'r')
xlabel(r'$J_{OUT}$')
ylabel(r'$P_{IN} \, [\text{b}], \; P_{IN\,app} \, [\text{r}]$')
xlim(0, Joutmax)
ylim(0, 0.4)
savefig('Pin.png', bbox_inches = 'tight')

# Px
figure(2)
plot(Jout, Px, 'b')
plot(Jout, Pxapp, 'r')
xlabel(r'$J_{OUT}$')
ylabel(r'$P_X$')
xlim(0, Joutmax)
ylim(0, 0.4)
savefig('Px.png', bbox_inches = 'tight')

# apparent power
figure(3)
plot(Jout, S, 'b')
plot(Jout, Sapp, 'r')
xlabel(r'$J_{OUT}$')
ylabel(r'$S$')
xlim(0, Joutmax)
ylim(0, 0.4)
savefig('S.png', bbox_inches = 'tight')

# power factor
figure(4)
plot(Jout, PF, 'b')
plot(Jout, PFapp, 'r')
xlabel(r'$J_{OUT}$')
ylabel(r'$PF$')
xlim(0, Joutmax)
ylim(0, 1)
savefig('PF.png', bbox_inches = 'tight')

