# L filter
# 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)
mx0 = abs(minp)
s = sign(minp)

nf2 = 100 # half-number of frames, reduce if the simulation is too slow

nf = 2 * nf2
Mout = empty(nf)
Jout = empty(nf)
Pout = empty(nf)
Pin = empty(nf)
Px = empty(nf)
Jinrms = empty(nf)
S = empty(nf)
PF = 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(nf2):
    
    mout = 1 - (k + 0.5) / nf2 * (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))
    
    beta = arcsin(mout)
        
    w1 = c1 + pattern
    w1 = w1 * float32(phi < beta)
    w1 = w1 * float32(w1 > 0)
    w2 = c2 + pattern
    w2 = w2 * float32(phi > beta)
    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 * jin))
    S[k] = Jinrms[k] / sqrt(2)
    PF[k] = Pin[k] / S[k]
        
    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('lfilter{:d}.png'.format(k), bbox_inches = 'tight')

print 'Joutcrit = {:.4f}'.format(mean(jl))

pattern = 1 - cos(phi) - 2 / pi * phi
beta = 180 / pi * arcsin(2 / pi)
gamma = 180 / pi * arcsin(2 / pi)
alpha = 180

for k in range(nf2, nf):
       
    Jout[k] = 0.21 + (k - nf2) * (Joutmax - 0.21) / nf2
    mout = 2 / pi
    Mout[k] = mout
    
    w = Jout[k] + pattern
    jl = concatenate((w, w))
    mx = mx0
    jin = jl * s
    pin = jin * minp
    px = jl * mx
    
    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]
    
    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('lfilter{:d}.png'.format(k), bbox_inches = 'tight')
    
# output plane
figure(1)
plot(Jout, Mout)
xlabel(r'$J_{OUT}$')
ylabel(r'$M_{OUT}$')
xlim(0, Joutmax)
ylim(0, 1)
savefig('Mout.png', bbox_inches = 'tight')

# power diagrams
figure(2)
plot(Jout, Pin, 'g', Jout, Px, 'b', Jout, Pout, 'r')
xlabel(r'$J_{OUT}$')
ylabel(r'$P_{IN} \, [\text{g}], \; P_X \, [\text{b}], \; P_{OUT} \, [\text{r}]$')
xlim(0, Joutmax)
ylim(0, 0.4)
savefig('Powers.png', bbox_inches = 'tight')

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

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

