def find_tstar(phi0, discretization=200): """ finds tstar and t_opt (at discretization) for a given phi0""" # define a periodic spline for the start_time = pos_start_root + phi0 * 23.7 / (2 * np.pi) prc_pos = stats.threshold(-pmodel.pPRC_interp(times + start_time)[:, 15], threshmin=0) prc_neg = stats.threshold(-pmodel.pPRC_interp(times + start_time)[:, 15], threshmax=0) prc = -pmodel.pPRC_interp(times + start_time)[:, 15] prc_pos_spl = ut.PeriodicSpline(times, prc_pos, period=pmodel.T) prc_neg_spl = ut.PeriodicSpline(times, prc_neg, period=pmodel.T) def dphitot_dt(phis, t): # integrates everything [phi_osc_pos, phi_osc_neg, phi_shift_pos, phi_shift_neg] = phis dphi_osc_pos_dt = (2*np.pi)/pmodel.T +\ umax*prc_pos_spl(phi_osc_pos*pmodel.T/(2*np.pi)) dphi_osc_neg_dt = (2*np.pi)/pmodel.T +\ umax*prc_neg_spl(phi_osc_neg*pmodel.T/(2*np.pi)) dphi_shft_pos_dt = umax * prc_pos_spl(phi_osc_pos * pmodel.T / (2 * np.pi)) dphi_shft_neg_dt = umax * prc_neg_spl(phi_osc_neg * pmodel.T / (2 * np.pi)) return (dphi_osc_pos_dt, dphi_osc_neg_dt, dphi_shft_pos_dt, dphi_shft_neg_dt) int_times = np.linspace(0, 3 * pmodel.T, 10001) delta_phis_total = integrate.odeint(dphitot_dt, [0, 0, 0, 0], int_times, hmax=0.001) # get the delta phis delta_phi_fs = np.linspace(0, 2 * np.pi, discretization) cross_loc = np.min( np.where(delta_phis_total[:, 2] - delta_phis_total[:, 3] >= 2 * np.pi)) t_star = int_times[cross_loc] phi_star = delta_phis_total[cross_loc, 3] % (2 * np.pi) t_opts = [] for phif in delta_phi_fs: if phif > phi_star: time_to_reach = np.min(int_times[np.where( delta_phis_total[:, 3] + 2 * np.pi <= phif)]) else: time_to_reach = np.min( int_times[np.where(delta_phis_total[:, 2] >= phif)]) t_opts.append(time_to_reach) return delta_phi_fs, np.asarray(t_opts), t_star, phi_star
time_to_sync = -2 return prediction_window, shift_val*24./pmodel.T, time_to_sync*24./pmodel.T # generate inputs for the resync time function inputs = [] idx = 0 for prediction_window in range(1,16,2): for shift_val in range(-12,13,4): inputs.append([idx, prediction_window, shift_val]) idx+=1 #run the MPC to generate a heatmap if sim==True: timer = uts.laptimer() num_cpus = 20 with futures.ProcessPoolExecutor(max_workers=num_cpus) as executor: result = executor.map(mpc_problem_time, inputs, chunksize=1) results1 = [] errors1 = [] for idx,res in enumerate(result): try: results1.append(res) except: errors1.append(inputs[idx]) # if any inputs give errors results1 = np.asarray(results1) np.save('Data/multistep_window_timetosync.npy', results1)
threshmin=0) prc_neg = stats.threshold(-pmodel.pPRC_interp(times + start_time)[:, 15], threshmax=0) prc = -pmodel.pPRC_interp(times + start_time)[:, 15] prc_spl = pmodel.pPRC_interp.splines[15] # # plots to see what the portions look like #plo.PlotOptions() #plt.figure() #plt.plot(times, prc, color='k', label='PRC') #plt.plot(times, prc_pos, color='h', ls='--', label='PRC+') #plt.plot(times, prc_neg, color='fl', ls='--', label='PRC-') #plt.legend() #plt.tight_layout(**plo.layout_pad) prc_pos_spl = ut.PeriodicSpline(times, prc_pos, period=pmodel.T) prc_neg_spl = ut.PeriodicSpline(times, prc_neg, period=pmodel.T) def dphitot_dt(phis, t): [phi_osc_pos, phi_osc_neg, phi_shift_pos, phi_shift_neg] = phis dphi_osc_pos_dt = (2 * np.pi) / pmodel.T + umax * prc_pos_spl( phi_osc_pos * pmodel.T / (2 * np.pi)) dphi_osc_neg_dt = (2 * np.pi) / pmodel.T + umax * prc_neg_spl( phi_osc_neg * pmodel.T / (2 * np.pi)) dphi_shft_pos_dt = umax * prc_pos_spl(phi_osc_pos * pmodel.T / (2 * np.pi)) dphi_shft_neg_dt = umax * prc_neg_spl(phi_osc_neg * pmodel.T / (2 * np.pi)) return dphi_osc_pos_dt, dphi_osc_neg_dt, dphi_shft_pos_dt, dphi_shft_neg_dt int_times = np.linspace(0, 3 * pmodel.T, 10001)
pmodel = lco.Oscillator(model(), param, y0in) pmodel.calc_y0() pmodel.corestationary() pmodel.limit_cycle() pmodel.find_prc() # start at negative prc region times = np.linspace(0, pmodel.T, 10001) roots = pmodel.pPRC_interp.splines[15].root_offset() neg_start_root = roots[0] # for when region becomes positive pos_start_root = roots[1] # for when region becomes negative umax = 0.06 # define a periodic spline for the prc start_time = pos_start_root prc_pos = ut.threshold(-pmodel.pPRC_interp(times + start_time)[:, 15], threshmin=0) prc_neg = ut.threshold(-pmodel.pPRC_interp(times + start_time)[:, 15], threshmax=0) prc = -pmodel.pPRC_interp(times + start_time)[:, 15] prc_spl = pmodel.pPRC_interp.splines[15] # the optimal cases - capture only + or - prc_pos_spl = ut.PeriodicSpline(times, prc_pos, period=pmodel.T) prc_neg_spl = ut.PeriodicSpline(times, prc_neg, period=pmodel.T) # define integration for one step def step_integrate(phi0, u_val, step): """ function that integrates one step forward. returns final phase, total shift value """ def dphidt(phi, t):