from pylab import *

scale = 0.75

M = 0.4
D0 = M

Jout = linspace(0, 0.75, 76)

close('all')

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

W = 10.5
H = 15.0
ll = 1.0 / W
rl = 6.0 / W
w = 4 / W
h = 3 / H
l1 = 11.5 / H
l2 = 8.0 / H
l3 = 4.5 / H
l4 = 1.0 / H

count = - 1

for j in Jout:

    count += 1

    DJ = D0 * (1 - D0)

    if j > DJ:

        linecolor = 'b'

        J0 = j - DJ
        J1 = j + DJ
    
        tau = [0, 0, D0, D0, 1, 1]
        jl = [J0, J0, J1, J1, J0, J0]
        ml = [- M, 1 - M, 1 - M, - M, - M, 1 - M]
        js = [0, J0, J1, 0, 0, J0]
        ms = [1, 0, 0, 1, 1, 0]
        jd = [J0, 0, 0, J1, J0, 0]
        md = [0, 1, 1, 0, 0, 1]
        jc = [- DJ, - DJ, DJ, DJ, - DJ, - DJ]
        mc = [M, M, M, M, M, M]
        
    else:
    
        linecolor = 'm'
        
        d = D0
        
        m = d**2 / (d**2 + j)
        
        dt = d / m

        J1 = 2 * (1 - m) * d
    
        tau = [0, 0, d, d, dt, dt, 1, 1]
        jl = [0, 0, J1, J1, 0, 0, 0, 0]
        ml = [0, 1 - m, 1 - m, - m, - m, 0, 0, 1 - m]
        js = [0, 0, J1, 0, 0, 0, 0, 0]
        ms = [1, 0, 0, 1, 1, 1 - m, 1 - m, 0]
        jd = [0, 0, 0, J1, 0, 0, 0, 0]
        md = [0, 1, 1, 0, 0, m, m, 1]
        jc = array([0, 0, J1, J1, 0, 0, 0, 0]) - j
        mc = [m, m, m, m, m, m, m, m]
   

    close('all')

    f = figure(1, figsize = scale * array([W, H]))

    figtext(ll, (H - 0.25) / H, r'$J_{OUT} = ' + r'{:4.2f}'.format(j) + r'$')

    f.add_axes([ll, l1, w, h])
    plot(tau, ml, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 1.2, 1.2)
    ylabel(r'$m_L$')

    f.add_axes([ll, l2, w, h])
    plot(tau, jl, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 0.2, 1.2)
    ylabel(r'$j_L$')

    f.add_axes([ll, l3, w, h])
    plot(tau, mc, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 1.2, 1.2)
    ylabel(r'$m_{OUT}$')

    f.add_axes([ll, l4, w, h])
    plot(tau, jc, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 0.4, 1.0)
    xlabel(r'$\tau$')
    ylabel(r'$j_C$')

    f.add_axes([rl, l1, w, h])
    plot(tau, ms, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 1.2, 1.2)
    ylabel(r'$m_S$')

    f.add_axes([rl, l2, w, h])
    plot(tau, js, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 0.2, 1.2)
    ylabel(r'$j_S$')

    f.add_axes([rl, l3, w, h])
    plot(tau, md, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 1.2, 1.2)
    ylabel(r'$m_D$')

    f.add_axes([rl, l4, w, h])
    plot(tau, jd, linecolor, linewidth = 2)
    xlim(- 0.01, 1.01)
    ylim(- 0.2, 1.2)
    xlabel(r'$\tau$')
    ylabel(r'$j_D$')

    savefig('buck-openloop-{:02d}.png'.format(count))

