Пример #1
0
def envelope():
    epsilon = 1e-3
    A = [10, 1]
    T = [4, 400]

    vdp = VanderPol(epsilon, A, T)
    fun_rtol = 1e-10
    fun_atol = 1e-12

    t_tran = 0

    if t_tran > 0:
        sol = solve_ivp(vdp, [0, t_tran], [-2,1], method='BDF', \
                        jac=vdp.jac, rtol=fun_rtol, atol=fun_atol)
        y0 = sol['y'][:, -1]
    else:
        y0 = np.array([-5.84170838, 0.1623759])

    print('y0 =', y0)

    t0 = 0
    t_end = np.max(T)
    t_span = np.array([t0, t_end])

    env_rtol = 1e-1
    env_atol = 1e-2
    be_env_solver = BEEnvelope(vdp, t_span, y0, T=np.min(T), \
                               env_rtol=env_rtol, env_atol=env_atol, \
                               rtol=fun_rtol, atol=fun_atol, \
                               method='BDF', jac=vdp.jac)
    be_env_sol = be_env_solver.solve()

    trap_env_solver = TrapEnvelope(vdp, t_span, y0, T=np.min(T), \
                                   env_rtol=env_rtol, env_atol=env_atol, \
                                   rtol=fun_rtol, atol=fun_atol, \
                                   method='BDF', jac=vdp.jac)
    trap_env_sol = trap_env_solver.solve()

    sol = solve_ivp(vdp, t_span, y0, method='BDF', \
                    jac=vdp.jac, rtol=fun_rtol, atol=fun_atol)

    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
    ax1.plot(sol['t'], sol['y'][0], 'k')
    ax1.plot(be_env_sol['t'], be_env_sol['y'][0], 'ro-')
    ax1.plot(trap_env_sol['t'], trap_env_sol['y'][0], 'gs-')
    ax1.set_ylabel(r'$V_C$ (V)')
    ax2.plot(sol['t'], sol['y'][1], 'k')
    ax2.plot(be_env_sol['t'], be_env_sol['y'][1], 'ro-')
    ax2.plot(trap_env_sol['t'], trap_env_sol['y'][1], 'gs-')
    ax2.set_xlabel('Time (s)')
    ax2.set_ylabel(r'$I_L$ (A)')
    plt.show()
Пример #2
0
def envelope(imposed_paths):
    if imposed_paths:
        kwargs = {'epsilon': 0.002, 'Cac': 0.15, 'Nac': 5.85, 'd': 0.1}
        Ca0 = 0
        Na0 = kwargs['Nac']
    else:
        kwargs = {'epsilon': 0.002}
        Ca0 = 0
        Na0 = 5.85

    neuron = Neuron4(imposed_paths, **kwargs)

    atol = 1e-6
    rtol = 1e-8

    y0 = np.array([4.51773484, 0.0356291, 0.03012965, 4.94827389])
    if y0 is None:
        v0 = -80
        n0, _, _, _ = neuron.compute_ss(v0)
        #      v, n, ca, na
        y0 = [v0, n0, Ca0, Na0]
        tend = 7000
        sol = solve_ivp(neuron, [0, tend], y0, atol=atol, rtol=rtol)
        t = sol['t']
        v = sol['y'][0]
        plt.ion()
        plt.plot(t, v, 'k', lw=0.5)
        plt.show()
        idx, = np.where((t > 3000) & (t < 3500))
        jdx = np.where(v[idx] > 0)[0][0]
        y0 = sol['y'][:, idx[jdx]]

    print('  y0 =', y0)
    print('ydot =', neuron(0, y0))
    env_rtol = 1e-1
    env_atol = 1e-1
    t_span = [0, 200]

    be_env_solver = BEEnvelope(neuron, t_span, y0, T=None, T_guess=30, \
                               vars_to_use=[0,1], dT_tol=0.01, \
                               env_rtol=env_rtol, env_atol=env_atol, \
                               rtol=rtol, atol=atol)
    be_env_sol = be_env_solver.solve()

    trap_env_solver = TrapEnvelope(neuron, t_span, y0, T=None, T_guess=30, \
                                   vars_to_use=[0,1], \
                                   env_rtol=env_rtol, env_atol=env_atol, \
                                   rtol=rtol, atol=atol)
    trap_env_sol = trap_env_solver.solve()

    sol = solve_ivp(neuron, t_span, y0, rtol=rtol, atol=atol)

    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
    ax1.plot(sol['t'], sol['y'][0], 'k', lw=0.75)
    #####
    for t0, y0, T in zip(be_env_sol['t'], be_env_sol['y'].T, be_env_sol['T']):
        period = solve_ivp(neuron, [t0, t0 + T],
                           y0,
                           method='RK45',
                           rtol=1e-8,
                           atol=1e-10)
        ax1.plot(period['t'], period['y'][0], color=[1, .6, .6], lw=1)
    #####
    ax1.plot(be_env_sol['t'], be_env_sol['y'][0], 'ro-', lw=1)
    ax1.plot(trap_env_sol['t'], trap_env_sol['y'][0], 'gs-', lw=1)
    ax1.set_ylabel(r'$V$ (mV)')
    ax2.plot(sol['t'], sol['y'][2], 'k', lw=0.75)
    ax2.plot(be_env_sol['t'], be_env_sol['y'][2], 'ro-', lw=1)
    ax2.plot(trap_env_sol['t'], trap_env_sol['y'][2], 'gs-', lw=1)
    ax2.set_xlabel('Time (s)')
    ax2.set_ylabel(r'$Ca$')
    plt.show()
Пример #3
0
def variational_envelope(use_ramp,
                         N_periods=100,
                         eig_vect=None,
                         compare=False):
    if compare and eig_vect is None:
        print(
            'You must provide the initial eigenvectors if compare is set to True.'
        )
        return

    T = 20e-6
    ki = 1
    Vin = 5
    Vref = 5

    boost = Boost(0,
                  T=T,
                  ki=ki,
                  Vin=Vin,
                  Vref=Vref,
                  clock_phase=0,
                  use_compensating_ramp=use_ramp)

    fun_rtol = 1e-10
    fun_atol = 1e-12

    t_tran = 50 * T

    if t_tran > 0:
        print(
            'Vector field index at the beginning of the first integration: %d.'
            % boost.vector_field_index)
        sol = solve_ivp_switch(boost, [0,t_tran], np.array([Vin,1]), \
                               method='BDF', jac=boost.jac, \
                               rtol=fun_rtol, atol=fun_atol)
        y0 = sol['y'][:, -1]
        print('Vector field index at the end of the first integration: %d.' %
              boost.vector_field_index)
        plt.figure()
        ax = plt.subplot(2, 1, 1)
        plt.plot(sol['t'] * 1e6, sol['y'][0], 'k')
        plt.ylabel(r'$V_C$ (V)')
        plt.subplot(2, 1, 2, sharex=ax)
        plt.plot(sol['t'] * 1e6, sol['y'][1], 'r')
        plt.xlabel(r'Time ($\mu$s)')
        plt.ylabel(r'$I_L$ (A)')
        plt.show()
    else:
        y0 = np.array([8.6542, 0.82007])

    T_large = N_periods * T
    T_small = T
    boost.with_variational = True
    boost.variational_T = T_large

    t_span_var = [0, 1]
    y0_var = np.concatenate((y0, np.eye(len(y0)).flatten()))

    sol = solve_ivp_switch(boost,
                           t_span_var,
                           y0_var,
                           method='BDF',
                           rtol=fun_rtol,
                           atol=fun_atol)

    rtol = 1e-1
    atol = 1e-2
    be_var_solver = BEEnvelope(boost, [0,T_large], y0, T_guess=None, T=T_small, \
                               env_rtol=rtol, env_atol=atol, max_step=1000,
                               is_variational=True, T_var_guess=None, T_var=None, \
                               var_rtol=rtol, var_atol=atol, solver=solve_ivp_switch, \
                               rtol=fun_rtol, atol=fun_atol, method='BDF')
    trap_var_solver = TrapEnvelope(boost, [0,T_large], y0, T_guess=None, T=T_small, \
                                   env_rtol=rtol, env_atol=atol, max_step=1000,
                                   is_variational=True, T_var_guess=None, T_var=None, \
                                   var_rtol=rtol, var_atol=atol, solver=solve_ivp_switch, \
                                   rtol=fun_rtol, atol=fun_atol, method='BDF')
    print('-' * 100)
    var_sol_be = be_var_solver.solve()
    print('-' * 100)
    var_sol_trap = trap_var_solver.solve()
    print('-' * 100)

    eig, _ = np.linalg.eig(np.reshape(sol['y'][2:, -1], (2, 2)))
    print('         correct eigenvalues:', eig)
    eig, _ = np.linalg.eig(np.reshape(var_sol_be['y'][2:, -1], (2, 2)))
    print('  BE approximate eigenvalues:', eig)
    eig, _ = np.linalg.eig(np.reshape(var_sol_trap['y'][2:, -1], (2, 2)))
    print('TRAP approximate eigenvalues:', eig)

    if compare:
        data = np.loadtxt('EigFuncDaniele.txt')
        t = (data[:, 0] - T_large) / T_large

        n_steps = len(var_sol_be['M'])
        y = np.zeros((boost.n_dim**2, n_steps + 1))
        y[:, 0] = eig_vect.flatten()
        for i, mat in enumerate(var_sol_be['M']):
            y[:, i +
              1] = (mat @ np.reshape(y[:, i],
                                     (boost.n_dim, boost.n_dim))).flatten()

        fig, ax = plt.subplots(boost.n_dim, boost.n_dim, sharex=True)
        ax[0, 0].plot(t, data[:, 1], 'k.-')
        ax[0, 0].plot(var_sol_be['t'], y[0, :], 'ro')
        ax[0, 1].plot(t, data[:, 3], 'k.-')
        ax[0, 1].plot(var_sol_be['t'], y[1, :], 'ro')
        ax[1, 0].plot(t, data[:, 2], 'k.-')
        ax[1, 0].plot(var_sol_be['t'], y[2, :], 'ro')
        ax[1, 1].plot(t, data[:, 4], 'k.-')
        ax[1, 1].plot(var_sol_be['t'], y[3, :], 'ro')
        for i in range(2):
            for j in range(2):
                ax[i, j].set_xlim([0, 1])
                ax[i, j].set_ylim([-1, 1])

    labels = [r'$V_C$ (V)', r'$I_L$ (A)']
    fig, ax = plt.subplots(3, 2, sharex=True)
    for i in range(2):
        ax[0, i].plot(sol['t'], sol['y'][i], 'k', lw=1)
        ax[0, i].plot(var_sol_be['t'], var_sol_be['y'][i], 'rs-', ms=3)
        ax[0, i].plot(var_sol_trap['t'], var_sol_trap['y'][i], 'go-', ms=3)
        ax[0, i].set_ylabel(labels[i])
        ax[0, i].set_xlim([0, 1])
        for j in range(2):
            k = i * 2 + j
            ax[i + 1, j].plot(sol['t'], sol['y'][k + 2], 'k', lw=1)
            ax[i + 1, j].set_ylabel(r'$\Phi_{%d,%d}$' % (i + 1, j + 1))
            ax[i + 1, j].plot(var_sol_be['t'],
                              var_sol_be['y'][k + 2],
                              'rs',
                              ms=3)
            ax[i + 1, j].plot(var_sol_trap['t'],
                              var_sol_trap['y'][k + 2],
                              'go',
                              ms=3)
            ax[i + 1, j].set_xlim([0, 1])
        ax[2, i].set_xlabel('Normalized time')

    plt.show()
Пример #4
0
def envelope_var_R(use_ramp):
    T = 40e-6
    ki = 1
    Vin = 5
    Vref = 5
    C0 = 47e-6
    L0 = 10e-6
    R0 = 5

    fun_rtol = 1e-12
    fun_atol = 1e-14

    def R_fun(t):
        n_period = int(t / T)
        if n_period % 100 < 75:
            return R0
        return 2 * R0

    def R_fun_sin(t):
        F = 500  # [Hz]
        dR0 = R0 / 10
        return R0 - dR0 / 2 + dR0 * np.sin(2 * np.pi * F * t)

    boost = Boost(0, T=T, ki=ki, Vin=Vin, Vref=Vref, C=C0*30, L=L0*2, \
                  R=R_fun_sin, use_compensating_ramp=use_ramp)

    t_tran = 100.1 * T

    #y0 = np.array([9.3124, 1.2804])
    y0 = np.array([10.154335434351671, 1.623030961224813])

    sol = solve_ivp_switch(boost, [0,t_tran], y0, \
                           method='BDF', jac=boost.jac, \
                           rtol=fun_rtol, atol=fun_atol)
    #plt.plot(sol['t']*1e6,sol['y'][0],'k')
    #plt.plot(sol['t']*1e6,sol['y'][1],'r')
    #plt.show()

    t_span = sol['t'][-1] + np.array([0, 100 * T])
    y0 = sol['y'][:, -1]
    print('t_span =', t_span)
    print('y0 =', y0)
    print('index =', boost.vector_field_index)

    print('-' * 81)
    be_solver = BEEnvelope(boost, t_span, y0, max_step=1000, \
                           T_guess=None, T=T, \
                           env_rtol=1e-2, env_atol=1e-3, \
                           solver=solve_ivp_switch, \
                           jac=boost.jac, method='BDF', \
                           rtol=fun_rtol, atol=fun_atol)
    sol_be = be_solver.solve()
    print('-' * 81)
    trap_solver = TrapEnvelope(boost, t_span, y0, max_step=1000, \
                               T_guess=None, T=T, \
                               env_rtol=1e-3, env_atol=1e-4, \
                               solver=solve_ivp_switch, \
                               jac=boost.jac, method='BDF', \
                               rtol=fun_rtol, atol=fun_atol)
    sol_trap = trap_solver.solve()
    print('-' * 81)

    sys.stdout.write('Integrating the original system... ')
    sys.stdout.flush()
    sol = solve_ivp_switch(boost,
                           t_span,
                           y0,
                           method='BDF',
                           jac=boost.jac,
                           rtol=fun_rtol,
                           atol=fun_atol)
    sys.stdout.write('done.\n')

    labels = [r'$V_C$ (V)', r'$I_L$ (A)']
    fig, ax = plt.subplots(2, 1, sharex=True)
    for i in range(2):
        ax[i].plot(sol['t'] * 1e6, sol['y'][i], 'k', lw=1)
        ax[i].plot(sol_be['t'] * 1e6, sol_be['y'][i], 'ro-', ms=3)
        ax[i].plot(sol_trap['t'] * 1e6, sol_trap['y'][i], 'go-', ms=3)
        ax[i].set_ylabel(labels[i])
    ax[1].set_xlabel(r'Time ($\mu$s)')
    ax[1].set_xlim(t_span * 1e6)
    plt.show()
Пример #5
0
def envelope(use_ramp):
    T = 20e-6
    ki = 1
    Vin = 5
    Vref = 5

    boost = Boost(0,
                  T=T,
                  ki=ki,
                  Vin=Vin,
                  Vref=Vref,
                  clock_phase=0,
                  use_compensating_ramp=use_ramp)

    fun_rtol = 1e-10
    fun_atol = 1e-12

    y0 = np.array([Vin, 0])
    t_span = np.array([0, 500 * T])

    t_tran = 0. * T
    if t_tran > 0:
        sol = solve_ivp_switch(boost, [0,t_tran], y0, \
                               method='BDF', jac=boost.jac, \
                               rtol=fun_rtol, atol=fun_atol)
        #plt.plot(sol['t']*1e6,sol['y'][0],'k')
        #plt.plot(sol['t']*1e6,sol['y'][1],'r')
        #plt.show()
        t_span += sol['t'][-1]
        y0 = sol['y'][:, -1]

    print('t_span =', t_span)
    print('y0 =', y0)
    print('index =', boost.vector_field_index)

    print('-' * 81)
    be_solver = BEEnvelope(boost, t_span, y0, max_step=1000, \
                           T_guess=None, T=T, \
                           env_rtol=1e-2, env_atol=1e-3, \
                           solver=solve_ivp_switch, \
                           jac=boost.jac, method='BDF', \
                           rtol=fun_rtol, atol=fun_atol)
    sol_be = be_solver.solve()
    print('-' * 81)
    trap_solver = TrapEnvelope(boost, t_span, y0, max_step=1000, \
                               T_guess=None, T=T, \
                               env_rtol=1e-2, env_atol=1e-3, \
                               solver=solve_ivp_switch, \
                               jac=boost.jac, method='BDF', \
                               rtol=fun_rtol, atol=fun_atol)
    sol_trap = trap_solver.solve()
    print('-' * 81)

    sys.stdout.write('Integrating the original system... ')
    sys.stdout.flush()
    sol = solve_ivp_switch(boost,
                           t_span,
                           y0,
                           method='BDF',
                           jac=boost.jac,
                           rtol=fun_rtol,
                           atol=fun_atol)
    sys.stdout.write('done.\n')

    labels = [r'$V_C$ (V)', r'$I_L$ (A)']
    fig, ax = plt.subplots(2, 1, sharex=True)
    for i in range(2):
        ax[i].plot(sol['t'] * 1e6, sol['y'][i], 'k', lw=1)
        ax[i].plot(sol_be['t'] * 1e6, sol_be['y'][i], 'ro-', ms=3)
        ax[i].plot(sol_trap['t'] * 1e6, sol_trap['y'][i], 'go-', ms=3)
        ax[i].set_ylabel(labels[i])
    ax[1].set_xlabel(r'Time ($\mu$s)')
    ax[1].set_xlim(t_span * 1e6)
    plt.show()
Пример #6
0
def variational_envelope():
    epsilon = 1e-3
    A = [10, 1]
    T = [4, 200]
    T_large = max(T)
    T_small = min(T)
    T_small_guess = min(T) * 0.95

    vdp = VanderPol(epsilon, A, T)

    t_span_var = [0, 1]
    if A[0] == 10:
        y0 = np.array([-5.8133754, 0.13476983])
    elif A[0] == 1:
        y0 = np.array([9.32886314, 0.109778919])
    y0_var = np.concatenate((y0, np.eye(len(y0)).flatten()))

    vdp.with_variational = True
    vdp.variational_T = T_large
    sol = solve_ivp(vdp,
                    t_span_var,
                    y0_var,
                    rtol=1e-8,
                    atol=1e-10,
                    dense_output=True)

    rtol = 1e-1
    atol = 1e-2
    be_var_solver = BEEnvelope(vdp, [0,T_large], y0, T_guess=None, T=T_small, \
                               env_rtol=rtol, env_atol=atol, is_variational=True, \
                               T_var_guess=2*np.pi*0.95, var_rtol=rtol, var_atol=atol,
                               solver=solve_ivp, rtol=1e-8, atol=1e-10)
    trap_var_solver = TrapEnvelope(vdp, [0,T_large], y0, T_guess=None, T=T_small, \
                                   env_rtol=rtol, env_atol=atol, is_variational=True, \
                                   T_var_guess=2*np.pi*0.95, var_rtol=rtol, var_atol=atol,
                                   solver=solve_ivp, rtol=1e-8, atol=1e-10)
    print('-' * 100)
    var_sol_be = be_var_solver.solve()
    print('-' * 100)
    var_sol_trap = trap_var_solver.solve()
    print('-' * 100)

    eig, _ = np.linalg.eig(np.reshape(sol['y'][2:, -1], (2, 2)))
    print('         correct eigenvalues:', eig)
    eig, _ = np.linalg.eig(np.reshape(var_sol_be['y'][2:, -1], (2, 2)))
    print('  BE approximate eigenvalues:', eig)
    eig, _ = np.linalg.eig(np.reshape(var_sol_trap['y'][2:, -1], (2, 2)))
    print('TRAP approximate eigenvalues:', eig)

    light_gray = [.6, .6, .6]
    dark_gray = [.3, .3, .3]
    black = [0, 0, 0]
    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(3, 3.5))
    ax1.plot(sol['t'], sol['y'][0], color=light_gray, lw=1)
    ax1.plot(var_sol_be['t'],var_sol_be['y'][0],'o-',lw=1,\
             color=black,markerfacecolor='w',markersize=4)
    #ax1.plot(var_sol_trap['t'],var_sol_trap['y'][0],'s',lw=1,\
    #         color=light_gray,markerfacecolor='w',markersize=4)
    ax1.set_ylabel('x')
    ax1.set_xlim([0, 1])
    ax1.set_ylim([-9, 9])
    ax1.set_yticks(np.arange(-9, 10, 3))
    #ax2.plot(t_span_var,[0,0],'b')
    ax2.plot(sol['t'],
             sol['y'][2],
             color=light_gray,
             lw=1,
             label='Full solution')
    ax2.plot(var_sol_be['t'],var_sol_be['y'][2],'o',lw=1,\
             color=black,markerfacecolor='w',markersize=4)
    for i in range(0, len(var_sol_be['var']['t']) - 3, 3):
        if i == 0:
            ax2.plot(var_sol_be['var']['t'][i:i+3],var_sol_be['var']['y'][0,i:i+3],'o-',\
                     color=black,linewidth=1,markerfacecolor='w',markersize=4,\
                     label='Envelope')
        else:
            ax2.plot(var_sol_be['var']['t'][i:i+3],var_sol_be['var']['y'][0,i:i+3],'o-',\
                     color=black,linewidth=1,markerfacecolor='w',markersize=4)
    ax2.legend(loc='best')
    #ax2.plot(var_sol_trap['t'],var_sol_trap['y'][2],'s',lw=1,\
    #         color=light_gray,markerfacecolor='w',markersize=4)
    #for i in range(0,len(var_sol_trap['var']['t']),3):
    #    ax2.plot(var_sol_trap['var']['t'][i:i+3],var_sol_trap['var']['y'][0,i:i+3],'.-',\
    #             color=[1,0,1])
    ax2.set_xlabel('Normalized time')
    ax2.set_ylabel(r'$\Phi_{1,1}$')
    ax2.set_xlim([0, 1])
    ax2.set_ylim([-1.2, 1.2])
    ax2.set_yticks(np.arange(-1, 1.5, 0.5))
    plt.savefig('vanderpol_variational.pdf')
    plt.show()
Пример #7
0
def HR():
    from polimi import HindmarshRose
    b = 3
    I = 5
    hr = HindmarshRose(I, b)

    y0 = [0, 1, 0.1]
    #y0 = np.array([-0.85477615, -3.03356705,  4.73029393])
    t_tran = 100
    rtol = 1e-6
    atol = 1e-8
    sol = solve_ivp(hr, [0, t_tran],
                    y0,
                    method='BDF',
                    jac=hr.jac,
                    rtol=rtol,
                    atol=atol)
    y0 = sol['y'][:, -1]

    t_span = [0, 500]
    T_guess = 11

    be_solver = BEEnvelope(hr, t_span, y0, T_guess=T_guess, \
                           max_step=500, integer_steps=True, \
                           env_rtol=1e-3, env_atol=1e-4, \
                           solver=solve_ivp, method='BDF', jac=hr.jac, \
                           rtol=rtol, atol=atol)
    trap_solver = TrapEnvelope(hr, t_span, y0, T_guess=T_guess, \
                               max_step=500, integer_steps=True, \
                               env_rtol=1e-3, env_atol=1e-4, \
                               solver=solve_ivp, method='BDF', jac=hr.jac, \
                               rtol=rtol, atol=atol)
    trap_solver.verbose = True

    #print('-' * 81)
    #sol_be = be_solver.solve()
    print('-' * 81)
    sol_trap = trap_solver.solve()
    print('-' * 81)
    sol = solve_ivp(hr, [t_span[0], sol_trap['t'][-1]], y0, method='BDF', \
                    jac=hr.jac, rtol=rtol, atol=atol)

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))
    ax1.plot(sol['t'], sol['y'][0], 'k')
    #for t0,y0,T in zip(sol_be['t'],sol_be['y'].T,sol_be['T']):
    #    period = solve_ivp(hr, [t0,t0+T], y0, method='RK45', rtol=rtol, atol=atol)
    #    ax1.plot(period['t'], period['y'][0], color=[1,.6,.6], lw=1)
    for t0, y0, T in zip(sol_trap['t'], sol_trap['y'].T, sol_trap['T']):
        period = solve_ivp(hr, [t0, t0 + T],
                           y0,
                           method='BDF',
                           rtol=rtol,
                           atol=atol)
        ax1.plot(period['t'], period['y'][0], color=[.6, 1, .6], lw=1)
    #ax1.plot(sol_be['t'],sol_be['y'][0],'ro-')
    ax1.plot(sol_trap['t'], sol_trap['y'][0], 'go-')
    ax1.plot(sol_trap['t'], sol_trap['T'], 'mo-')
    ax1.set_xlabel('t')
    ax1.set_ylabel('y')

    #idx, = np.where(sol['t'] > 1000)
    #ax2.plot(sol['y'][0,idx],sol_trap['y'][1,idx],'k')
    #for t0,y0,T in zip(sol_trap['t'],sol_be['y'].T,sol_be['T']):
    #    if t0 < 1000:
    #        continue
    #    period = solve_ivp(hr, [t0,t0+T], y0, method='RK45', rtol=1e-8, atol=1e-10)
    #    ax2.plot(period['y'][0], period['y'][1], color=[1,.6,.6], lw=1)
    #idx, = np.where(sol_be['t'] > 1000)
    #ax2.plot(sol_trap['y'][0,idx],sol_trap['y'][1,idx],'ro-')
    #ax2.set_xlabel('x')
    #ax2.set_ylabel('y')

    plt.show()
Пример #8
0
def autonomous():
    from polimi import VanderPol

    epsilon = 1e-3
    A = [0]
    T = [1]
    vdp = VanderPol(epsilon, A, T)

    t_span = [0, 4000 * 2 * np.pi]
    t_interval = [500, 1000]
    t_interval = [9700, 10200]
    y0 = np.array([2e-3, 1e-3])
    #y0 = np.array([1,0.5])

    fun_rtol = 1e-8
    fun_atol = 1e-10
    env_rtol = 1e-3
    env_atol = 1e-6

    T_guess = 2 * np.pi * 0.9

    be_solver = BEEnvelope(vdp, t_span, y0, T_guess=T_guess, \
                           env_rtol=env_rtol, env_atol=env_atol, \
                           solver=solve_ivp, rtol=fun_rtol, \
                           atol=fun_atol, method='BDF', jac=vdp.jac)
    trap_solver = TrapEnvelope(vdp, t_span, y0, T=2*np.pi, \
                               env_rtol=env_rtol, env_atol=env_atol, \
                               solver=solve_ivp, rtol=fun_rtol, \
                               atol=fun_atol, method='BDF', jac=vdp.jac)

    try:
        data = pickle.load(open('vdp.pkl', 'rb'))
        sol = data['sol']
        sol_be = data['sol_be']
        sol_trap = data['sol_trap']
        t_span = data['t_span']
        #t_interval = data['t_interval']
    except:
        print('-' * 81)
        sol_be = be_solver.solve()
        print('-' * 81)
        sol_trap = trap_solver.solve()
        print('-' * 81)
        sol = solve_ivp(vdp, t_span, y0, method='BDF', rtol=1e-8, atol=1e-10)
        data = {'sol': sol, 'sol_be': sol_be, 'sol_trap': sol_trap, \
                't_span': t_span, 't_interval': t_interval}
        pickle.dump(data, open('vdp.pkl', 'wb'))

    black = [0, 0, 0]
    grey = [.3, .3, .3]
    light_grey = [.7, .7, .7]

    fig = plt.figure(figsize=(3.5, 5))
    ax1 = plt.axes([0.1, 0.65, 0.8, 0.275])
    ax2 = plt.axes([0.1, 0.3, 0.8, 0.275])
    ax3 = plt.axes([0.1, 0.1, 0.8, 0.125])

    ms = 3
    ax1.plot(sol['t'], sol['y'][0], color=light_grey, linewidth=0.5)
    ax1.plot(sol_be['t'], sol_be['y'][0], 'o-', color=black, \
             linewidth=1, markerfacecolor='w', markersize=ms)
    ax1.plot(sol_trap['t'], sol_trap['y'][0], 's-', color=grey, \
             linewidth=1, markerfacecolor='w', markersize=ms)
    ax1.set_xlim(t_span)
    ax1.set_ylabel('x')

    idx, = np.where((sol['t'] > t_interval[0]) & (sol['t'] < t_interval[1]))
    ax2.plot(sol['t'][idx], sol['y'][0, idx], color=light_grey, linewidth=0.5)
    m = np.min(sol['y'][0, idx])
    M = np.max(sol['y'][0, idx])
    y_lim = 2 * np.array([m, M])
    ax1.plot(t_interval[0] + np.zeros(2), y_lim, 'k--', linewidth=1)
    ax1.plot(t_interval[1] + np.zeros(2), y_lim, 'k--', linewidth=1)
    ax1.plot(t_interval, y_lim[0] + np.zeros(2), 'k--', linewidth=1)
    ax1.plot(t_interval, y_lim[1] + np.zeros(2), 'k--', linewidth=1)

    idx, = np.where((sol_be['t'] > t_interval[0])
                    & (sol_be['t'] < t_interval[1]))
    idx = np.r_[idx[0] - 1, idx, idx[-1] + 1]
    ax2.plot(sol_be['t'][idx], sol_be['y'][0,idx], 'o-', color=black, \
             linewidth=1, markerfacecolor='w', markersize=ms+1)

    idx, = np.where((sol_trap['t'] > t_interval[0])
                    & (sol_trap['t'] < t_interval[1]))
    idx = np.r_[idx[0] - 1, idx, idx[-1] + 1]
    ax2.plot(sol_trap['t'][idx], sol_trap['y'][0,idx], 's-', color=grey, \
             linewidth=1, markerfacecolor='w', markersize=ms+1)
    ax2.set_xlim(t_interval)
    ax2.set_ylim([-0.5, 0.5])
    ax2.set_yticks(np.arange(-0.4, 0.5, 0.2))
    ax2.set_ylabel('x')

    ax3.plot(sol_be['t'][1:], np.round(np.diff(sol_be['t'])/(2*np.pi)), 'o-', color=black, \
             linewidth=1, markerfacecolor='w', markersize=ms)
    ax3.plot(sol_trap['t'][1:], np.round(np.diff(sol_trap['t'])/(2*np.pi)), 's-', color=grey, \
             linewidth=1, markerfacecolor='w', markersize=ms)
    ax3.set_xlim(t_span)
    ax3.set_xlabel('Time')
    ax3.set_yticks(np.arange(0, 510, 100))
    ax3.set_ylabel('Envelope time-step')

    #plt.plot(sol['t'],sol['y'][0],'k')
    #plt.plot(sol_be['t'],sol_be['y'][0],'ro-')
    #plt.plot(sol_trap['t'],sol_trap['y'][0],'go-')
    plt.savefig('vdp_envelope.pdf')
    plt.show()