# L filter, discontinuous 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
i = arange(npoint)
deg = 360 * (i + 0.5) / npoint
wt = radians(deg)
phi = wt[0 : npoint / 2]
minp = sin(wt)
mx0 = abs(minp)
s = sign(minp)

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

Mout = empty(nf)
Jout = empty(nf)
Pout = empty(nf)
Pin = empty(nf)
Px = empty(nf)
Jinrms = empty(nf)
S = empty(nf)
PF = empty(nf)
a = empty(nf)
g = empty(nf)
b = 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, 8))
degtick = (0, 60, 120, 180, 240, 300, 360)

for k in range(nf):
    
    mout = 1 - (k + 0.5) / nf * (1 - 2 / pi)
    Mout[k] = mout
    
    pattern = - cos(phi) - mout * phi
    
    c1 = 2 + sqrt(1 - mout**2) + mout * (arcsin(mout) - pi)
    c2 = sqrt(1 - mout**2) + mout * (arcsin(mout))
    
    gamma = arcsin(mout)
        
    w1 = c1 + pattern
    w1 = w1 * float32(phi < gamma)
    w1 = w1 * float32(w1 > 0)
    w2 = c2 + pattern
    w2 = w2 * float32(phi > gamma)
    w2 = w2 * float32(w2 > 0)
    w = w1 + w2
    
    jl = concatenate((w, w))
    state = jl > 0
    mx = mx0 * float32(state) + mout * float32(~state)
    jin = jl * s
    pin = jin * minp
    px = jl * mx
    
    Jout[k] = mean(jl)
    Pout[k] = mout * Jout[k]
    Pin[k] = mean(pin)
    Px[k] = mean(px)
    Jinrms[k] = sqrt(mean(jin**2))
    S[k] = Jinrms[k] / sqrt(2)
    PF[k] = Pin[k] / S[k]
    a[k] = 180 * mean(state)
    g[k] = 180 / pi * gamma
    b[k] = 180 * ((g[k] + a[k]) / 180 - floor((g[k] + a[k]) / 180))
    
    clf()
        
    subplot(4, 1, 1)
    plot(deg, jl)
    plot(deg, Jout[k] * ones(len(deg)), 'r')
    ylabel(r'$j_{L} \, [\text{b}], \; J_{OUT} \, [\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(4, 1, 4)
    plot(deg, jin)
    xlabel(r'$\omega t [^\circ]$')
    ylabel(r'$j_{IN}$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 0.8, 0.8)
    
    savefig('lfilterdcm{:d}.png'.format(k), bbox_inches = 'tight')


figure(1)
plot(Mout, Pin, 'g')
plot(Mout, Px, 'b')
plot(Mout, Pout, 'r')
xlabel(r'$M_{OUT}$')
ylabel(r'$P_{IN} \, [\text{g}], \; P_X \, [\text{b}], \; P_{OUT} \, [\text{r}]$')
xlim(0, 1)
ylim(0, 0.4)
savefig('Ps.png', bbox_inches = 'tight')

figure(2)
plot(Mout, a, 'r')
plot(Mout, b, 'b')
plot(Mout, g, 'g')
xlabel(r'$M_{OUT}$')
ylabel(r'$\alpha \, [\text{r}], \; \beta \, [\text{b}], \; \gamma \, [\text{g}] \quad [^\circ]$')
yticks((0, 30, 60, 90, 120, 150, 180))
xlim(2 / pi, 1)
ylim(0, 180)
savefig('angles.png', bbox_inches = 'tight')

figure(3)
plot(Mout, Pin, 'b')
plot(Mout, S, 'r')
xlabel(r'$M_{OUT}$')
ylabel(r'$P_{IN} \, [\text{b}], \; S \, [\text{r}]$')
xlim(0, 1)
ylim(0, 0.4)
savefig('PS.png', bbox_inches = 'tight')

figure(4)
plot(Mout, PF)
xlabel(r'$M_{OUT}$')
ylabel(r'$PF$')
xlim(0, 1)
ylim(0, 1)
savefig('PF.png', bbox_inches = 'tight')

figure(5)
plot(Mout, Jout)
xlabel(r'$M_{OUT}$')
ylabel(r'$J_{OUT}$')
xlim(0, 1)
ylim(0, 0.25)
savefig('Mout-Jout.png', bbox_inches = 'tight')

figure(6)
plot(Jout, Mout)
xlabel(r'$J_{OUT}$')
ylabel(r'$M_{OUT}$')
ylim(0, 1)
xlim(0, 0.25)
savefig('Jout-Mout.png', bbox_inches = 'tight')


