def read_rho_rh(modelpath): mstar_conv = dyn.conv('mstar', modelpath + 'initial.conv.sh') l_conv = dyn.conv('l', modelpath + 'initial.conv.sh') #datarho = np.genfromtxt(modelpath+'initial.rho_lagrad.dat') #rho_rh0 = datarho[:,20][0]*mstar_conv/l_conv**3 filerho = modelpath + 'initial.rho_lagrad.dat' with open(filerho, 'r') as frho: for i, line in enumerate(frho): if i == 2: # 3th line data = line.split() rho_rh0 = float(data[20]) * mstar_conv / l_conv**3 break return rho_rh0
def estimate_Nmsp_Lgamma_atpresent(): sample_init = np.genfromtxt( '/projects/b1095/syr904/projects/GCE/catalog/cluster_sample_initial.dat', dtype=str) N_star = sample_init[:, 0] rv = sample_init[:, 1] rg = sample_init[:, 2] z = sample_init[:, 3] rg_init = sample_init[:, 5].astype(float) num_msp = [] L_gamma = [] Mass = [] for ii in range(len(N_star)): if rg_init[ii] <= 3.: modelpath = '/projects/b1091/CMC_Grid_March2019/rundir/rv' + rv[ ii] + '/rg' + rg[ii] + '/z' + z[ii] + '/' + N_star[ii] + '/' m_conv = dyn.conv('m', modelpath + 'initial.conv.sh') nsfile = modelpath + 'initial.ns.dat' with open(nsfile, 'r') as fns: for line in fns: pass ns_last_line = line datans = ns_last_line.split() num_msp.append(int(datans[6])) dynfile = modelpath + 'initial.dyn.dat' with open(dynfile, 'r') as fdyn: for line in fdyn: pass dyn_last_line = line datadyn = dyn_last_line.split() Mass.append(float(datadyn[4]) * m_conv) lgammafile = '/projects/b1095/syr904/projects/GCE/catalog/Lgamma_alltime_maingrid.dat' pathfile = '/projects/b1095/syr904/projects/GCE/catalog/path_allfinished_newruns_maingrid.dat' datalg = np.genfromtxt(lgammafile) modellg = datalg[:, 0] tlg = datalg[:, 1] lmsp = datalg[:, 2] sourcedir = np.genfromtxt(pathfile, dtype=str) paths = list(sourcedir[:, 0]) model_no = paths.index(modelpath) print(model_no, modelpath) lg_last = lmsp[modellg == model_no] if not len(lg_last): continue lg_last = lg_last[-1] print(lg_last, model_no) L_gamma.append(lg_last) print(sum(num_msp), sum(L_gamma), sum(Mass))
def find_rh_density(modelpath, thesnap): filestr = modelpath + 'initial' snap = modelpath + thesnap dynfile = np.genfromtxt(filestr + '.dyn.dat') tdyn = dynfile[:, 0] r_h = dynfile[:, 20] m = [] radius = [] t_conv = dyn.conv('t', filestr + '.conv.sh') l_conv = dyn.conv('l', filestr + '.conv.sh') print(l_conv) tsnap = dyn.get_time(snap) for xx in range(len(tdyn)): if round(tdyn[xx], 6) == tsnap: rh_snap = r_h[xx] print(rh_snap) with gzip.open(snap, 'r') as fsnap: next(fsnap) next(fsnap) for line in fsnap: data = line.split() m.append(float(data[1])) radius.append(float(data[2]) * l_conv) if float(data[2]) > rh_snap: break #vol = (2*twopi/3)*(rh_snap*l_conv)**3 vol = (2 * twopi / 3) * (radius[-1])**3 averho_rh = sum(m) / vol return averho_rh, m, radius
modelfiles = np.genfromtxt( '/projects/b1091/CMC_Grid_March2019/rundir/path.dat', dtype=str, delimiter='') sourcedir = modelfiles[:, 0] status = [] for i in range(len(sourcedir)): filestr1 = sourcedir[i] + 'initial' dynfile1 = filestr1 + '.dyn.dat' filestr2 = sourcedir[i] + 'initial2' dynfile2 = filestr2 + '.dyn.dat' if os.path.isfile(dynfile2) and os.path.getsize(dynfile2) > 0: t_conv = dyn.conv('t', filestr1 + '.conv.sh') with open(dynfile2, 'r') as fdyn: next(fdyn) next(fdyn) for line in fdyn: datadyn = line.split() totaln = float(datadyn[3]) break with open(dynfile2, 'r') as fdyn: for line in fdyn: pass lastdyn = line.split() time = float(lastdyn[0]) * t_conv
def find_BH_XX_atsnap(filepath, lowlim, highlim, snapno, savename): property_init, property_finl, property_des = ntc.find_tc_properties_final( filepath) ID0 = property_init['id0'] ID1 = property_init['id1'] T = property_init['time'] Types = property_init['type'] filestr = filepath + 'initial' #snaps=dyn.get_snapshots(filestr) #lastsnap=snaps[-1] snap = filepath + 'initial.snap0' + str(snapno) + '.dat.gz' t_conv = dyn.conv('t', filestr + '.conv.sh') l_conv = dyn.conv('l', filestr + '.conv.sh') time = dyn.get_time(snap) * t_conv #os.system('rm '+savepath) fmsb = open(filepath + savename + str(snapno) + '.dat', 'w+') fmsb.write( '#1.ID0 2.ID1 3.M0 4.M1 5.K0 6.K1 7.a(AU) 8.ecc 9.radrol0 10.radrol1 11.B(G) 12.P(sec) 13.tcflag 14.SN 15.TC_time(Myr) 16.r(pc)\n' ) with gzip.open(snap, 'r') as flast: next(flast) next(flast) for line in flast: datalast = line.split() if int(datalast[7]) == 1: if int(datalast[17]) == 14 and lowlim <= int( datalast[18]) <= highlim: ID0ms = int(datalast[10]) ID1ms = int(datalast[11]) tcflag = 4 tctime = -100 for ii in range(len(ID0)): if (ID0ms == ID0[ii] and ID1ms == ID1[ii]) or ( ID1ms == ID0[ii] and ID0ms == ID1[ii]): if Types[ii] == 'SS_COLL_Giant': tcflag = 81 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 91 tctime = T[ii] * t_conv break elif ID0ms == ID0[ii] or ID0ms == ID1[ii]: if Types[ii] == 'SS_COLL_Giant': tcflag = 82 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 92 tctime = T[ii] * t_conv break fmsb.write( '%d %d %f %f %d %d %f %f %f %f %e %f %d %d %f %f\n' % (int(datalast[10]), int(datalast[11]), float(datalast[8]), float( datalast[9]), int(datalast[17]), int( datalast[18]), float(datalast[12]), float(datalast[13]), float(datalast[43]), float(datalast[44]), float(datalast[47]), float(twopi * yearsc / float(datalast[45])), tcflag, int(datalast[49]), tctime, float(datalast[2]) * l_conv)) if lowlim <= int(datalast[17]) <= highlim and int( datalast[18]) == 14: ID0ms = int(datalast[11]) ID1ms = int(datalast[10]) tcflag = 4 tctime = -100 for ii in range(len(ID0)): if (ID0ms == ID0[ii] and ID1ms == ID1[ii]) or ( ID1ms == ID0[ii] and ID0ms == ID1[ii]): if Types[ii] == 'SS_COLL_Giant': tcflag = 81 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 91 tctime = T[ii] * t_conv break elif ID0ms == ID0[ii] or ID0ms == ID1[ii]: if Types[ii] == 'SS_COLL_Giant': tcflag = 82 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 92 tctime = T[ii] * t_conv break fmsb.write( '%d %d %f %f %d %d %f %f %f %f %e %f %d %d %f %f\n' % (int(datalast[11]), int(datalast[10]), float(datalast[9]), float( datalast[8]), int(datalast[18]), int( datalast[17]), float(datalast[12]), float(datalast[13]), float(datalast[44]), float(datalast[43]), float(datalast[48]), float(twopi * yearsc / float(datalast[46])), tcflag, int(datalast[50]), tctime, float(datalast[2]) * l_conv)) fmsb.close()
def find_WD_XX_atsnap_hdf5(filepath, lowlim, highlim, snapno, savename): property_init, property_finl, property_des = nhf.find_tc_properties( filepath) ID0 = property_init['id0'] ID1 = property_init['id1'] T = property_init['time'] Types = property_init['type'] all_keys = np.genfromtxt(filepath + 'snap_keys.txt', dtype='str') all_snapno = all_keys[:, 0] all_snaptime = all_keys[:, 1] thekey = '/' + str(snapno) + '(t=' + all_snaptime[snapno] + ')' filestr = filepath + 'initial' snap = cmct.Snapshot( fname=filepath + 'initial.snapshots.h5', snapshot_name=thekey, conv=filepath + 'initial.conv.sh', dist=4.52, # distance to cluster in kpc z=0.0038) t_conv = dyn.conv('t', filestr + '.conv.sh') l_conv = dyn.conv('l', filestr + '.conv.sh') #os.system('rm '+savepath) fmsb = open(filepath + savename + str(snapno) + '.dat', 'w+') fmsb.write( '#1.ID0 2.ID1 3.M0 4.M1 5.K0 6.K1 7.a(AU) 8.ecc 9.radrol0 10.radrol1 11.B(G) 12.P(sec) 13.tcflag 14.SN 15.TC_time(Myr) 16.R(pc)\n' ) binflag = snap.data['binflag'] k0 = snap.data['bin_startype0'] k1 = snap.data['bin_startype1'] id0 = snap.data['id0'] id1 = snap.data['id1'] m0 = snap.data['m0_MSUN'] m1 = snap.data['m1_MSUN'] sma = snap.data['a_AU'] ecc = snap.data['e'] rad0 = snap.data['radrol0'] rad1 = snap.data['radrol1'] B0 = snap.data['B0'] B1 = snap.data['B1'] ospin0 = snap.data['ospin0'] ospin1 = snap.data['ospin1'] SN0 = snap.data['formation0'] SN1 = snap.data['formation1'] Rpc = snap.data['r'] * l_conv for xx in range(len(binflag)): if binflag[xx] == 1: if 10 <= k0[xx] <= 12 and lowlim <= k1[xx] <= highlim: ID0ms = id0[xx] ID1ms = id1[xx] tcflag = 4 tctime = -100 for ii in range(len(ID0)): if (ID0ms == ID0[ii] and ID1ms == ID1[ii]) or (ID1ms == ID0[ii] and ID0ms == ID1[ii]): if Types[ii] == 'SS_COLL_Giant': tcflag = 81 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 91 tctime = T[ii] * t_conv break elif ID0ms == ID0[ii] or ID0ms == ID1[ii]: if Types[ii] == 'SS_COLL_Giant': tcflag = 82 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 92 tctime = T[ii] * t_conv break fmsb.write( '%d %d %f %f %d %d %f %f %f %f %e %f %d %d %f %f\n' % (id0[xx], id1[xx], m0[xx], m1[xx], k0[xx], k1[xx], sma[xx], ecc[xx], rad0[xx], rad1[xx], B0[xx], twopi * yearsc / ospin0[xx], tcflag, SN0[xx], tctime, Rpc[xx])) if lowlim <= k0[xx] <= highlim and 10 <= k1[xx] <= 12: ID0ms = id1[xx] ID1ms = id0[xx] tcflag = 4 tctime = -100 for ii in range(len(ID0)): if (ID0ms == ID0[ii] and ID1ms == ID1[ii]) or (ID1ms == ID0[ii] and ID0ms == ID1[ii]): if Types[ii] == 'SS_COLL_Giant': tcflag = 81 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 91 tctime = T[ii] * t_conv break elif ID0ms == ID0[ii] or ID0ms == ID1[ii]: if Types[ii] == 'SS_COLL_Giant': tcflag = 82 tctime = T[ii] * t_conv break if Types[ii] == 'SS_COLL_TC_P': tcflag = 92 tctime = T[ii] * t_conv break fmsb.write( '%d %d %f %f %d %d %f %f %f %f %e %f %d %d %f %f\n' % (id1[xx], id0[xx], m1[xx], m0[xx], k1[xx], k0[xx], sma[xx], ecc[xx], rad1[xx], rad0[xx], B1[xx], twopi * yearsc / ospin1[xx], tcflag, SN1[xx], tctime, Rpc[xx])) fmsb.close()
def cluster_inspiral(r_initial, cluster_path, rho_initial, cluster_status): #datadyn = np.genfromtxt(cluster_path+'initial.dyn.dat') #datarho = np.genfromtxt(cluster_path+'initial.rho_lagrad.dat') #m_gc = np.array(datadyn[:,4])*m_conv #rho_halfm = np.array(datarho[:,20])*(mstar_conv/l_conv**3) #t_rho = np.array(datarho[:,0])*t_conv #for kk in range(len(t_rho)-1): # delta_t = (t_rho[kk+1]-t_rho[kk])/1000. # rg_new = df(delta_t, rg_old, m_gc[kk]) # rho_ga_new = galaxy_rho_r(rg_new, circ_vel) # if rho_halfm[kk+1] <= rho_ga_new: # print('cluster disrupted', t_rho[kk+1], rg_new, rho_halfm[kk+1]) # rg_disrupt = rg_new; t_disrupt = t_rho[kk+1]; rho_disrupt = rho_halfm[kk+1] # break # rg_old = rg_new t_conv = dyn.conv('t', cluster_path + 'initial.conv.sh') l_conv = dyn.conv('l', cluster_path + 'initial.conv.sh') m_conv = dyn.conv('m', cluster_path + 'initial.conv.sh') mstar_conv = dyn.conv('mstar', cluster_path + 'initial.conv.sh') rg_disrupt = -100 t_disrupt = -100 rho_disrupt = -100 filerho = cluster_path + 'initial.rho_lagrad.dat' filedyn = cluster_path + 'initial.dyn.dat' with open(filerho, 'r') as fr: next(fr) next(fr) for i_rho, l_rho in enumerate(fr): pass nline_rho = i_rho + 1 #print(nline_rho) line_cut = int(0.7 * nline_rho) with open(filedyn, 'r') as fd: next(fd) next(fd) for l_dyn in fd: data = l_dyn.split() m_old = float(data[4]) break print(m_old) ##Initialize the spiral in rg_old = r_initial t_old = 0. m_old = m_old * m_conv rho_halfm_old = rho_initial ##Starting spiral in check = 0 with open(filerho, 'r') as frho, open(filedyn, 'r') as fdyn: next(frho) next(frho) next(fdyn) next(fdyn) for i, lines in enumerate(zip(frho, fdyn)): if i == 0: continue if i <= line_cut and i % 10 != 0: continue datarho = lines[0].split() datadyn = lines[1].split() t_curr = float(datarho[0]) rho_halfm_curr = float(datarho[20]) * (mstar_conv / l_conv**3) m_curr = float(datadyn[4]) * m_conv delta_t = (t_curr - t_old) * t_conv / 1000. rg_new = df(delta_t, rg_old, m_old) if rg_new <= 0: check = 1 print('cluster reached center', t_curr * t_conv, rg_new, rho_halfm_curr) rg_disrupt = rg_old t_disrupt = t_old * t_conv rho_disrupt = rho_halfm_old type_disrupt = 1 break rho_ga_new = galaxy_rho_r(rg_new, circ_vel) if (rho_halfm_curr <= rho_ga_new): check = 1 print('cluster disrupted', t_curr * t_conv, rg_new, rho_halfm_curr) rg_disrupt = rg_new t_disrupt = t_curr * t_conv rho_disrupt = rho_halfm_curr type_disrupt = 2 break t_old = t_curr m_old = m_curr rg_old = rg_new rho_halfm_old = rho_halfm_curr #print(i) #print(rho_halfm_curr, rho_ga_new, m_curr, rg_new) if check == 0: rg_disrupt = rg_new t_disrupt = t_curr * t_conv rho_disrupt = rho_halfm_curr type_disrupt = 3 print(rg_disrupt, t_disrupt, rho_disrupt) return rg_disrupt, t_disrupt, rho_disrupt, type_disrupt
def read_property_attime(modelpath, time): ##time in Myr Num_msp = -100 Mass = -100 Lum_gamma = -100 Num_msp_old = -100 Mass_old = -100 Lum_gamma_old = -100 t_conv = dyn.conv('t', modelpath + 'initial.conv.sh') m_conv = dyn.conv('m', modelpath + 'initial.conv.sh') ##Extract number of MSP with open(modelpath + 'initial.ns.dat', 'r') as fns: next(fns) for i, line in enumerate(fns): datans = line.split() t_ns = float(datans[0]) * t_conv if i == 0: n_msp_old = int(datans[6]) if round(t_ns, 6) >= round(time, 6): print(t_ns, time) Num_msp = int(datans[6]) Num_msp_old = n_msp_old break n_msp_old = int(datans[6]) if Num_msp == -100: Num_msp = int(datans[6]) Num_msp_old = int(datans[6]) ##Extract mass of cluster datadyn = np.genfromtxt(modelpath + 'initial.dyn.dat') with open(modelpath + 'initial.dyn.dat', 'r') as fdyn: next(fdyn) next(fdyn) for i, line in enumerate(fdyn): datadyn = line.split() t_dyn = float(datadyn[0]) * t_conv if i == 0: m_dyn_old = float(datadyn[4]) * m_conv if round(t_dyn, 6) >= round(time, 6): print(t_dyn, time) Mass = float(datadyn[4]) * m_conv Mass_old = m_dyn_old break m_dyn_old = float(datadyn[4]) * m_conv if Mass == -100: Mass = float(datadyn[4]) * m_conv Mass_old = float(datadyn[4]) * m_conv ##Extract Lgamma if Num_msp != 0 or Num_msp_old != 0: sourcedir = np.genfromtxt( '/projects/b1095/syr904/projects/GCE/catalog/path_allfinished_newruns_maingrid.dat', dtype=str) paths = sourcedir[:, 0] model_no = list(paths).index(modelpath) datagama = np.genfromtxt( '/projects/b1095/syr904/projects/GCE/catalog/Lgamma_alltime_maingrid.dat' ) model_gama = datagama[:, 0] t_gama = datagama[:, 1] lgama = datagama[:, 2] t_model = t_gama[model_gama == model_no] lum_model = lgama[model_gama == model_no] for xx in range(len(t_model)): if round(t_model[xx], 6) >= round(time, 6): Lum_gamma = lum_model[xx] Lum_gamma_old = lum_model[xx - 1] #print(Num_msp, Mass, Lum_gamma, Num_msp_old, Mass_old, Lum_gamma_old) return Num_msp, Mass, Lum_gamma, Num_msp_old, Mass_old, Lum_gamma_old
def find_Lgammaray_alltime(pathlist, start, end, eta_gamma=0.1): Cscale = 9.6 * 10**33 ##in erg/s sourcedir = np.genfromtxt(pathlist, dtype=str) paths = sourcedir[:, 0] status = sourcedir[:, 1] #fgamma=open('/projects/b1095/syr904/N1e7_rg8_f5_z0.002/Lgamma_alltime_N1e7.dat', 'a+') #fgamma.write('#1.Model 2.T(Myr) 3.Lmsp\n') for ii in range(start, end): print(paths[ii]) filestr = paths[ii] + 'initial' t_conv = dyn.conv('t', filestr + '.conv.sh') s = paths[ii].split('/') n_star = s[-2] z = s[-3][1:] rg = s[-4][2:] rv = s[-5][2:] Lmsp = [] time = [] with open(filestr + '.morepulsars.dat', 'r') as fpsr: next(fpsr) for line in fpsr: data = line.split() if int(data[2]) != 1: Pspin = float(data[9]) ##in sec B = float(data[7]) if Pspin <= 0.03: Lmsp.append(Cscale * (eta_gamma / 0.2) * (B / 10**8.5)**2 * (3. / (Pspin * 1000.))**4) time.append(float(data[1]) * t_conv) else: if int(data[11]) == 13: Pspin = float(data[9]) ##in sec B = float(data[7]) if Pspin <= 0.03: Lmsp.append(Cscale * (eta_gamma / 0.2) * (B / 10**8.5)**2 * (3. / (Pspin * 1000.))**4) time.append(float(data[1]) * t_conv) if int(data[12]) == 13: Pspin = float(data[10]) ##in sec B = float(data[8]) if Pspin <= 0.03: Lmsp.append(Cscale * (eta_gamma / 0.2) * (B / 10**8.5)**2 * (3. / (Pspin * 1000.))**4) time.append(float(data[1]) * t_conv) #print(time) #sums = {} #for key, value in zip(time,Lmsp): # try: # sums[key] += value # except KeyError: # sums[key] = value #print(sums, sums.keys()) #allkey = sums.keys() #for key in sums: # thetime = float(key) # #print(thetime) # theL = sums[key] # fgamma.write('%d %f %e\n'%(ii, thetime, theL)) fgamma = open( '/projects/b1095/syr904/projects/GCE/catalog/data_lgamma/model_rv' + rv + '_rg' + rg + '_z' + z + '_' + n_star + '.txt', 'a+') fgamma.write('#1.Model 2.T(Myr) 3.Lmsp\n') allkey = list(Counter(time).keys()) #print(allkey) for x in range(len(allkey)): theL = 0 for y in range(len(time)): if time[y] == allkey[x]: theL += Lmsp[y] fgamma.write('%d %f %e\n' % (ii, allkey[x], theL)) print(ii) fgamma.close()