# half-wave rectifier and 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
i = arange(npoint)
deg = 360 * (i - 0.5) / npoint
wt = radians(deg)
minp = sin(wt)

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, 6))
degtick = (0, 60, 120, 180, 240, 300, 360)

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

Mout = empty(nf)
Jout = empty(nf)
alpha = empty(nf)
Pout = empty(nf)
Pin = empty(nf)
Jinrms = empty(nf)
S = empty(nf)
PF = empty(nf)

for k in range(nf):
    
    mout = (k + 0.5) / nf
    Mout[k] = mout
    
    o = sqrt(1 - mout**2) + mout * arcsin(mout)
    
    jl = o - cos(wt) - mout * wt
    d = logical_and(jl > 0,  logical_not(logical_and(minp < mout, wt < pi / 2)))
    jl = jl * float16(d)
    
    pin = minp * jl
    pout = mout * jl
    ml = (minp - mout) * float16(jl > 0)
    pl = ml * jl
    
    Jout[k] = mean(jl)
    alpha[k] = 360 * mean(float16(d))
    Pout[k] = mean(pout)
    Pin[k] = mean(pin)
    Jinrms[k] = sqrt(mean(jl**2))
    S[k] = Jinrms[k] / sqrt(2)
    PF[k] = Pin[k] / S[k]
    
    clf()
    
    subplot(3, 1, 1)
    plot(deg, jl, 'b')
    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, 2)

    subplot(3, 1, 2)
    plot(deg, minp, 'b')
    plot(deg, mout * ones(len(deg)), 'r')
    ylabel(r'$m_{IN} \, [\text{b}], \; M_{OUT} \, [\text{r}]$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 1.5, 1.5)

    subplot(3, 1, 3)
    plot(deg, ml, 'b')
    ylabel(r'$m_{L}$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 1.5, 1.5)
    xlabel(r'$\omega t [^\circ]$')
    
    savefig('lfilterhalfwave{:d}.png'.format(k), bbox_inches = 'tight')
    


# power diagrams
figure(1)
plot(Mout, Pin, 'b')
plot(Mout, Pout, 'r')
xlabel(r'$M_{OUT}$')
ylabel(r'$P_{IN} \, [b], \; P_{OUT} \, [r]$')
xlim(0, 1)
ylim(0, 0.14)
savefig('Powers.png', bbox_inches = 'tight')

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

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

# conduction angle
figure(4)
plot(Mout, alpha)
xlabel(r'$M_{OUT}$')
ylabel(r'$\alpha \, [^\circ]$')
xlim(0, 1)
ylim(0, 360)
yticks(degtick)
savefig('alpha.png', bbox_inches = 'tight')

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

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

