def eval_mean_error_functions(act,ang,n_vec,toy_aa,timeseries,withplot=False): """ Calculates sqrt(mean(E)) and sqrt(mean(F)) """ Err = np.zeros(6) NT = len(timeseries) size = len(ang[6:])/3 UA = ua(toy_aa.T[3:].T,np.ones(3)) fig,axis=None,None if(withplot): fig,axis=plt.subplots(3,2) plt.subplots_adjust(wspace=0.3) for K in range(3): ErrJ = np.array([(i[K]-act[K]-2.*np.sum(n_vec.T[K]*act[3:]*np.cos(np.dot(n_vec,i[3:]))))**2 for i in toy_aa]) Err[K] = np.sum(ErrJ) ErrT = np.array(((ang[K]+timeseries*ang[K+3]-UA.T[K]-2.*np.array([np.sum(ang[6+K*size:6+(K+1)*size]*np.sin(np.sum(n_vec*i,axis=1))) for i in toy_aa.T[3:].T])))**2) Err[K+3] = np.sum(ErrT) if(withplot): axis[K][0].plot(ErrJ,'.') axis[K][0].set_ylabel(r'$E$'+str(K+1)) axis[K][1].plot(ErrT,'.') axis[K][1].set_ylabel(r'$F$'+str(K+1)) if(withplot): for i in range(3): axis[i][0].set_xlabel(r'$t$') axis[i][1].set_xlabel(r'$t$') plt.show() EJ = np.sqrt(Err[:3]/NT) ET = np.sqrt(Err[3:]/NT) return np.array([EJ,ET])
def plot_Sn_timesamples(PSP): """ Plots Fig. 5 from Sanders & Binney (2014) """ TT = pot.stackel_triax() f, a = plt.subplots(2, 1, figsize=[3.32, 3.6]) plt.subplots_adjust(hspace=0.0, top=0.8) LowestPeriod = 2.0 * np.pi / 38.86564386 Times = np.array([2.0, 4.0, 8.0, 12.0]) Sr = np.arange(2, 14, 2) # Loop over length of integration window for i, P, C in zip(Times, [".", "s", "D", "^"], ["k", "r", "b", "g"]): diffact = np.zeros((len(Sr), 3)) difffreq = np.zeros((len(Sr), 3)) MAXGAPS = np.array([]) # Loop over N_max for k, j in enumerate(Sr): NT = choose_NT(j) timeseries = np.linspace(0.0, i * LowestPeriod, NT) results = odeint(pot.orbit_derivs2, PSP, timeseries, args=(TT,), rtol=1e-13, atol=1e-13) act, ang, n_vec, toy_aa, pars = find_actions(results, timeseries, N_matrix=j, ifprint=False, use_box=True) # Check all modes checks, maxgap = ced(n_vec, ua(toy_aa.T[3:].T, np.ones(3))) if len(maxgap) > 0: maxgap = np.max(maxgap) else: maxgap = 0 diffact[k] = act[:3] / TT.action(results[0]) print i, j, print_max_average(n_vec, toy_aa.T[3:].T, act[3:]), str(ang[3:6] - TT.freq(results[0])).replace( "[", "" ).replace("]", ""), str(np.abs(act[:3] - TT.action(results[0]))).replace("[", "").replace("]", ""), len( checks ), maxgap MAXGAPS = np.append(MAXGAPS, maxgap) difffreq[k] = ang[3:6] / TT.freq(results[0]) size = 15 if P == ".": size = 30 LW = np.array(map(lambda i: 0.5 + i * 0.5, MAXGAPS)) a[0].scatter( Sr, np.log10(np.abs(diffact.T[2] - 1)), marker=P, s=size, color=C, facecolors="none", lw=LW, label=r"$T =\,$" + str(i) + r"$\,T_F$", ) a[1].scatter(Sr, np.log10(np.abs(difffreq.T[2] - 1)), marker=P, s=size, color=C, facecolors="none", lw=LW) a[1].get_yticklabels()[-1].set_visible(False) a[0].set_xticklabels([]) a[0].set_xlim(1, 13) a[0].set_ylabel(r"$\log_{10}|J_3^\prime/J_{3, \rm true}-1|$") leg = a[0].legend(loc="upper center", bbox_to_anchor=(0.5, 1.4), ncol=2, scatterpoints=1) leg.draw_frame(False) a[1].set_xlim(1, 13) a[1].set_xlabel(r"$N_{\rm max}$") a[1].set_ylabel(r"$\log_{10}|\Omega_3^\prime/\Omega_{3,\rm true}-1|$") plt.savefig("Sn_T_box.pdf", bbox_inches="tight")
def eval_mean_error_functions(act, ang, n_vec, toy_aa, timeseries, withplot=False): """ Calculates sqrt(mean(E)) and sqrt(mean(F)) """ Err = np.zeros(6) NT = len(timeseries) size = len(ang[6:]) / 3 UA = ua(toy_aa.T[3:].T, np.ones(3)) fig, axis = None, None if (withplot): fig, axis = plt.subplots(3, 2) plt.subplots_adjust(wspace=0.3) for K in range(3): ErrJ = np.array([ (i[K] - act[K] - 2. * np.sum(n_vec.T[K] * act[3:] * np.cos(np.dot(n_vec, i[3:]))))**2 for i in toy_aa ]) Err[K] = np.sum(ErrJ) ErrT = np.array( ((ang[K] + timeseries * ang[K + 3] - UA.T[K] - 2. * np.array([ np.sum(ang[6 + K * size:6 + (K + 1) * size] * np.sin(np.sum(n_vec * i, axis=1))) for i in toy_aa.T[3:].T ])))**2) Err[K + 3] = np.sum(ErrT) if (withplot): axis[K][0].plot(ErrJ, '.') axis[K][0].set_ylabel(r'$E$' + str(K + 1)) axis[K][1].plot(ErrT, '.') axis[K][1].set_ylabel(r'$F$' + str(K + 1)) if (withplot): for i in range(3): axis[i][0].set_xlabel(r'$t$') axis[i][1].set_xlabel(r'$t$') plt.show() EJ = np.sqrt(Err[:3] / NT) ET = np.sqrt(Err[3:] / NT) return np.array([EJ, ET])
(act,ang,n_vec,toy_aa,para),loop = find_actions(results, timeseries, N_matrix = 6, ifloop=True,ifprint = False) size = len(ang[6:])/3 AA = [np.array([np.sum(ang[6+i*size:6+(i+1)*size]*np.sin(np.sum(n_vec*K,axis=1))) for K in toy_aa.T[3:].T]) for i in range(3)] # a[0,2].plot((toy_aa.T[3]+2.*AA[0]) % (2.*np.pi) / np.pi,(toy_aa.T[4]+2.*AA[1]) % (2.*np.pi)/ np.pi,'.',markersize=3) # a[1,2].plot((toy_aa.T[3]+2.*AA[0]) % (2.*np.pi) / np.pi,(toy_aa.T[5]+2.*AA[2]) % (2.*np.pi) / np.pi,'.',markersize=3) # plt.savefig('LM_test.pdf',bbox_inches='tight') # exit(0) toyangles = toy_aa.T[3:6].T/np.pi MaxRoll = 0.5*Roll from solver import check_each_direction as ced from solver import unroll_angles as ua for i in np.arange(0,MaxRoll,Roll/100.): (act,ang,n_vec,toy_aa,para),loop = find_actions(results[i:i+Roll], timeseries[:Roll], N_matrix = 6, ifloop=True,ifprint = False) checks,maxgap = ced(n_vec,ua(toy_aa.T[3:].T,np.ones(3))) print(checks) print len(results[i:i+Roll]),act[0],act[1],act[2],ang[3],ang[4],ang[5],ang[0] % (2.*np.pi),ang[1] % (2.*np.pi),ang[2] % (2.*np.pi),' '.join(map(str, loop)) if(len(angles)==0): angles = ang[:3]%(2.*np.pi)/np.pi freqs = ang[3:6] actions = act[:3] times = timeseries[i] else: angles = np.vstack((angles,ang[:3]%(2.*np.pi)/np.pi)) freqs = np.vstack((freqs,ang[3:6])) actions = np.vstack((actions,act[:3])) times = np.vstack((times,timeseries[i])) freqs = freqs/Conv print(np.mean(actions.T[0]),np.mean(actions.T[1]),np.mean(actions.T[2]))
# a[1,2].plot((toy_aa.T[3]+2.*AA[0]) % (2.*np.pi) / np.pi,(toy_aa.T[5]+2.*AA[2]) % (2.*np.pi) / np.pi,'.',markersize=3) # plt.savefig('LM_test.pdf',bbox_inches='tight') # exit(0) toyangles = toy_aa.T[3:6].T / np.pi MaxRoll = 0.5 * Roll from solver import check_each_direction as ced from solver import unroll_angles as ua for i in np.arange(0, MaxRoll, Roll / 100.): (act, ang, n_vec, toy_aa, para), loop = find_actions(results[i:i + Roll], timeseries[:Roll], N_matrix=6, ifloop=True, ifprint=False) checks, maxgap = ced(n_vec, ua(toy_aa.T[3:].T, np.ones(3))) print(checks) print len( results[i:i + Roll] ), act[0], act[1], act[2], ang[3], ang[4], ang[5], ang[0] % ( 2. * np.pi), ang[1] % (2. * np.pi), ang[2] % (2. * np.pi), ' '.join( map(str, loop)) if (len(angles) == 0): angles = ang[:3] % (2. * np.pi) / np.pi freqs = ang[3:6] actions = act[:3] times = timeseries[i] else: angles = np.vstack((angles, ang[:3] % (2. * np.pi) / np.pi)) freqs = np.vstack((freqs, ang[3:6])) actions = np.vstack((actions, act[:3]))
def plot_Sn_timesamples(PSP): """ Plots Fig. 5 from Sanders & Binney (2014) """ TT = pot.stackel_triax() f, a = plt.subplots(2, 1, figsize=[3.32, 3.6]) plt.subplots_adjust(hspace=0., top=0.8) LowestPeriod = 2. * np.pi / 38.86564386 Times = np.array([2., 4., 8., 12.]) Sr = np.arange(2, 14, 2) # Loop over length of integration window for i, P, C in zip(Times, ['.', 's', 'D', '^'], ['k', 'r', 'b', 'g']): diffact = np.zeros((len(Sr), 3)) difffreq = np.zeros((len(Sr), 3)) MAXGAPS = np.array([]) # Loop over N_max for k, j in enumerate(Sr): NT = choose_NT(j) timeseries = np.linspace(0., i * LowestPeriod, NT) results = odeint(pot.orbit_derivs2, PSP, timeseries, args=(TT, ), rtol=1e-13, atol=1e-13) act, ang, n_vec, toy_aa, pars = find_actions(results, timeseries, N_matrix=j, ifprint=False, use_box=True) # Check all modes checks, maxgap = ced(n_vec, ua(toy_aa.T[3:].T, np.ones(3))) if len(maxgap) > 0: maxgap = np.max(maxgap) else: maxgap = 0 diffact[k] = act[:3] / TT.action(results[0]) print i, j, print_max_average( n_vec, toy_aa.T[3:].T, act[3:]), str(ang[3:6] - TT.freq(results[0])).replace( '[', '').replace(']', ''), str( np.abs(act[:3] - TT.action(results[0]))).replace( '[', '').replace(']', ''), len(checks), maxgap MAXGAPS = np.append(MAXGAPS, maxgap) difffreq[k] = ang[3:6] / TT.freq(results[0]) size = 15 if (P == '.'): size = 30 LW = np.array(map(lambda i: 0.5 + i * 0.5, MAXGAPS)) a[0].scatter(Sr, np.log10(np.abs(diffact.T[2] - 1)), marker=P, s=size, color=C, facecolors="none", lw=LW, label=r'$T =\,$' + str(i) + r'$\,T_F$') a[1].scatter(Sr, np.log10(np.abs(difffreq.T[2] - 1)), marker=P, s=size, color=C, facecolors="none", lw=LW) a[1].get_yticklabels()[-1].set_visible(False) a[0].set_xticklabels([]) a[0].set_xlim(1, 13) a[0].set_ylabel(r"$\log_{10}|J_3^\prime/J_{3, \rm true}-1|$") leg = a[0].legend(loc='upper center', bbox_to_anchor=(0.5, 1.4), ncol=2, scatterpoints=1) leg.draw_frame(False) a[1].set_xlim(1, 13) a[1].set_xlabel(r'$N_{\rm max}$') a[1].set_ylabel(r"$\log_{10}|\Omega_3^\prime/\Omega_{3,\rm true}-1|$") plt.savefig('Sn_T_box.pdf', bbox_inches='tight')