def read_bin_plot_npz(filename, legendlabel, linestyle, color, y_offset=1.0): data = np.load(filename) a, e, N_broken = data['a_fin'], data['e_fin'], data['N_broken'] a_bins, N_a, a_binwidth = calcFrequency(a, N_bins, log=True) da = a_bins * (np.exp(a_binwidth) - 1.0) plt.loglog(a_bins / au, N_a / (da / au) * y_offset, label=legendlabel, linestyle=linestyle, color=color)
N_b = 1000 #Monte Carlo #Number of encounters print('M Mean =', T * encounterRate(n_p, v_rms, b_min, b_max, v_min, v_max)) N_enc_MC = np.random.poisson( T * encounterRate(n_p, v_rms, b_min, b_max, v_min, v_max)) #b values #b_MC = draw_b(b_max, N_enc_MC) #v values #v_MC = maxwell.rvs(scale=v_rms, size=N_enc_MC) v_MC = draw_vmaxwellian(v_rms, v_min, v_max, N_enc_MC) #Bin b values #b_bins_MC, N_b_MC, db_MC = calcFrequency(b_MC, N_b) #Bin v values v_bins_MC, N_v_MC, dv_MC = calcFrequency(v_MC, N_v) ''' #Binning #b bins for encounter rate dlogb = (np.log(b_max)-np.log(b_min))/N_b b = np.array([b_min*np.exp(i*dlogb) for i in range(N_b)]) db_B = b * (np.exp(dlogb) - 1.0) #print('b =', b) #print('db_B =', db_B) #db_B = (b_max - b_min)/(N_b) #b = np.array([b_min + i*db_B for i in range(N_b)]) #v bins for encounter rate dlogv = (np.log(v_max)-np.log(v_min))/N_v
plt.title('v distribution') ax1 = fig.add_subplot(111, projection='3d') ax1.scatter(v_vectors[:,0], v_vectors[:,1], v_vectors[:,2]) plt.show() fig = plt.figure() plt.title('b distribution') ax2 = fig.add_subplot(111, projection='3d') ax2.scatter(b_vectors[:,0], b_vectors[:,1], b_vectors[:,2]) plt.show() ''' N_bins = 50 plt.title('z test for v') vz_bins, N_vz, d_vz = calcFrequency(v_vectors[:, 2], N_bins) plt.plot(vz_bins, N_vz / N_vec / d_vz) plt.show() plt.title('z test for b') bz_bins, N_bz, d_bz = calcFrequency(b_vectors[:, 2], N_bins) plt.plot(bz_bins, N_bz / N_vec / d_bz) plt.plot() plt.show() plt.title('phi test for v') vphi_bins, N_vphi, d_vphi = calcFrequency(v_phis, N_bins) plt.plot(vphi_bins, N_vphi / N_vec / d_vphi) plt.show() plt.title('phi test for b')
print('Saving data') np.savez('simulation_test_{}Msol_{}e{}.npz'.format(int(M_p*mass_scale()/(2.0*10.0**30.0)), int(N_bin/10**int(np.floor(np.log10(N_bin)))), int(np.floor(np.log10(N_bin)))), a_fin=a_fin, e_fin=e_fin, N_broken=N_broken) ''' #Load data #print('Loading data') #data = np.load('binary_pop.npz') #a_fin, e_fin, N_broken = data['a_fin'], data['e_fin'], data['N_broken'] print('Number of binaries broken =', N_broken) print('Plotting') #Plot final and initial distributions #Number of bins N_bins = 50 a_bins_ini, N_a_ini, a_ini_binwidth = calcFrequency(a_ini, N_bins, log=True) a_bins_fin, N_a_fin, a_fin_binwidth = calcFrequency(a_fin, N_bins, log=True) #Actual bin widths da_ini = a_bins_ini * (np.exp(a_ini_binwidth) - 1.0) da_fin = a_bins_fin * (np.exp(a_fin_binwidth) - 1.0) e_bins_ini, N_e_ini, e_ini_binwidth = calcFrequency(e_ini, N_bins) e_bins_fin, N_e_fin, e_fin_binwidth = calcFrequency(e_fin, N_bins) #Semi-major axis plt.loglog(a_bins_ini * length_scale() / au, N_a_ini / (da_ini * length_scale() / au)) plt.loglog(a_bins_fin * length_scale() / au, N_a_fin / (da_fin * length_scale() / au)) #plt.xlim(10.0**3.0, 3.0*10.0**5.0) #plt.ylim(5.0*10.0**(-1.0), 3.0*10.0**3.0)
N_enc_F[i] = np.size(b_F) #Mean time method N_enc_T = np.zeros(N_N_enc) for i in range(N_N_enc): #time passed t = 0.0 while t <= T: rate = encounterRate(n_p, v_rms, b_min, b_max, v_min, v_max) t += np.random.exponential(1.0 / rate) N_enc_T[i] += 1 #print('N_enc_T =', N_enc_T) #Bin N_enc values #print('N_enc_B =', N_enc_B) N_enc_bins_MC, N_N_enc_MC, dN_enc_MC = calcFrequency(N_enc_MC, 100) N_enc_bins_T, N_N_enc_T, dN_enc_T = calcFrequency(N_enc_T, 100) N_enc_bins_F, N_N_enc_F, dN_enc_F = calcFrequency(N_enc_F, 100) #Plot N_enc distribution plt.plot(N_enc_bins_MC, N_N_enc_MC / N_N_enc / dN_enc_MC, label='Monte Carlo') plt.plot(N_enc_bins_T, N_N_enc_T / N_N_enc / dN_enc_T, label='Mean time') plt.plot(N_enc_bins_F, N_N_enc_F / N_N_enc / dN_enc_F, label='Filter') plt.plot([MC_mean] * np.size(N_N_enc_MC), N_N_enc_MC / N_N_enc / dN_enc_MC, label='MC mean') plt.xlabel('Number of encounters') plt.ylabel('Probability density of number of encounters') plt.legend() plt.show()