Пример #1
0
def find_snap_time_array(path, string):
    snapno_max = int(find_max_snap(path, string))
    units = scripts.read_units(path + string)
    km = units[0]['l_cgs'] * 1.0e-5
    time_units = units[0]['t_myr']

    time_array = []
    snap_array = []
    for j in range(0, snapno_max + 1):
        if j < 10:
            snapno = '000' + str(j)
        if j < 100 and j >= 10:
            snapno = '00' + str(j)
        if j < 1000 and j >= 100:
            snapno = '0' + str(j)
        if j < 10000 and j >= 1000:
            snapno = str(j)
        if j >= 10000:
            snapno = str(j)

        try:
            f2 = gzip.open(path + string + '.snap' + snapno + '.dat.gz', 'r')
            line = f2.readline()
            line = line.split(' ')
            line = line[1]
            parsed = line.split('=')
            time = float(
                parsed[1]) * time_units  # DEFINE THE TIME OF THE SNAPFILE
            time_array.append(time)
            snap_array.append(snapno)
        except:
            continue
    return time_array, snap_array
def history_maker(id0, id1, path, snapno, time_i, savepath):
    ID_FLAG = 0
    MTflag = 1    
    id0_str = str(id0)
    f = open(savepath+'/'+id0_str+'_history.dat','w+')
    sys.stdout = f
    units=scripts.read_units(path+'/'+'initial')
    km = units[0]['l_cgs']*1.0e-5
    time_units = units[0]['t_myr']

    star_ids = [id0]
    positions = [1]
    filestring = 'initial'
    path = path
    binary = 1
    h = history_cmc.history_maker(star_ids,positions,filestring,path,binary)
    # First create the time array
    time_array = []
    id0 = id0
    for i in range(len(h[id0]['binint']['binint'])):
        time_array.append(h[id0]['binint']['binint'][i]['interaction']['type']['time']*time_units)
    print(time_array)
    print('t_onset=',time_i)
    time = h[id0]['binint']['binint']
        if snapno <= 300:
                Delta = -1
        if snapno <= 600 and snapno > 300:
                Delta = -4
        if snapno <= 1500 and snapno > 600:
                Delta = -6
        if snapno <= 10000 and snapno > 1500:
                Delta = -8
        if snapno > 10000:
                Delta = -10
Пример #3
0
def find_snap_time(path, string, snapno):
    units = scripts.read_units(path + string)
    km = units[0]['l_cgs'] * 1.0e-5
    time_units = units[0]['t_myr']
    f2 = gzip.open(path + string + '.snap' + snapno + '.dat.gz', 'r')
    line = f2.readline()
    line = line.split(' ')
    line = line[1]
    parsed = line.split('=')
    time = float(parsed[1]) * time_units  # DEFINE THE TIME OF THE SNAPFILE
    return time
Пример #4
0
def hard_soft(cluster, filestring, snapno):
    import scripts
    units = scripts.read_units(
        '/projects/b1011/kyle/cmc/branches/cmc-mpi/m22_project/rundir/' +
        cluster + '/' + filestring + '/initial')
    data2 = np.loadtxt('RV_model_sigma_' + filestring + '.dat')
    km = units[0]['l_cgs'] * 1.0e-5
    pc = units[0]['l_pc']
    time = units[0]['nbt_cgs']

    ########## make array with velocity dispersions for each bin for the particular snapshot you're interested in
    sigma = []
    s = int(snapno)
    for i in range(0, len(data2)):
        if data2[i, 0] == s:
            for j in range(2, 19, 3):
                sigma.append(data2[i, j])

#######################

    data = np.loadtxt(
        '/projects/b1011/kyle/cmc/branches/cmc-mpi/m22_project/rundir/' +
        cluster + '/' + filestring + '/initial.snap' + snapno +
        '.hrdiag_modified.dat')
    data2 = np.loadtxt(
        '/projects/b1011/kyle/cmc/branches/cmc-mpi/m22_project/rundir/' +
        cluster + '/' + filestring + '/initial.snap' + snapno + '.dat.gz')
    data[:, 0] = data[:, 0] * pc  # converts from code units to pc
    data[:, 0] = conversions.pc_to_arcsec(
        data[:, 0], 4.4)  # converts radius from pc to arcsec

    M_G_array = []
    M_MS_array = []
    m_array = []
    count_G = 0
    count_MS = 0
    count = 0
    for j in range(0, 6):
        M_G = 0
        M_MS = 0
        m = 0
        count_G = 0
        count_MS = 0
        count = 0
        if j == 0:
            r_min = 26.67
            r_max = 79.85
        if j == 1:
            r_min = 80.173
            r_max = 123.844
        if j == 2:
            r_min = 127.816
            r_max = 168.111
        if j == 3:
            r_min = 169.443
            r_max = 234.509
        if j == 4:
            r_min = 237.539
            r_max = 326.939
        if j == 5:
            r_min = 335.386
            r_max = 611.081
        for i in range(0, len(data)):
            if data[i, 0] > r_min and data[i, 0] <= r_max:
                m = m + data[i, 8]  # If any object (single or binary)
                count = count + 1  # counts all objects
                if data[i, 1] == 0:  # if a single star
                    if data[i, 2] >= 3 and data[i, 2] <= 6:  # If a giant star
                        M_G = M_G + data[i, 8]
                        count_G = count_G + 1
                    if data[i, 2] <= 1:  # If a MS star
                        M_MS = M_MS + data[i, 8]
                        count_MS = count_MS + 1
                else:  # If a binary
                    if data[i, 4] >= 3 and data[
                            i, 4] <= 6:  # If component 0 is a giant
                        M_G = M_G + data[i, 9]
                        count_G = count_G + 1
                    if data[i, 5] >= 3 and data[
                            i, 5] <= 6:  # If component 1 is a giant
                        M_G = M_G + data[i, 10]
                        count_G = count_G + 1
                    if data[i, 4] <= 1:  # If component 0 is a MS
                        M_MS = M_MS + data[i, 9]
                        count_MS = count_MS + 1
                    if data[i, 5] <= 1:  # If component 1 is a MS
                        M_MS = M_MS + data[i, 10]
                        count_MS = count_MS + 1

        M_G_array.append(M_G / count_G)
        M_MS_array.append(M_MS / count_MS)
        m_array.append(m / count)
        print M_G / count_G, M_MS / count_MS, m / count, count_G, count_MS, count

    for i in range(0, len(m_array)):
        # Convert the arrays from Msun to kg
        M_G_array[i] = M_G_array[i] * 1.99e30
        M_MS_array[i] = M_MS_array[i] * 1.99e30
        m_array[i] = m_array[i] * 1.99e30
        sigma[i] = sigma[i] * 1000.  # convert sigma to m/s
    G = 6.67e-11
    a = []
    for i in range(0, len(m_array)):
        a.append(G * M_G_array[i] * M_MS_array[i] /
                 (2.0 * m_array[i] * sigma[i]**2.) / 1.496e11)
    print a
    print sigma

    # find mean giant radius for entire cluster
    R = 0
    count = 0
    for i in range(0, len(data2)):
        if data2[i, 7] == 0:
            if data2[i, 14] >= 3 and data2[i, 14] <= 6:
                R = R + data2[i, 16]
                print data2[i, 16], data2[i, 14], data2[i, 1]
                count = count + 1
        else:
            if data2[i, 17] >= 3 and data2[i, 17] <= 6:
                R = R + data2[i, 21]
                count = count + 1
            if data2[i, 18] >= 3 and data2[i, 18] <= 6:
                R = R + data2[i, 22]
                count = count + 1
    print R / count * 1.0, count
Пример #5
0
def velocity_dispersion(path, string, snapno, ALL=1):
    import scripts
    import random
    units = scripts.read_units(path + string)
    km = units[0]['l_cgs'] * 1.0e-5
    time = units[0]['nbt_cgs']
    f = open('RV_model.dat', 'w')
    f1 = open('RV_model_sigma.dat', 'a')
    if ALL == 1:
        f4 = open(path + string + '.snap' + snapno + '.vel_dispersion.dat',
                  'w')
    if ALL == 0:
        f4 = open(
            path + string + '.snap' + snapno + '.vel_dispersion_giants.dat',
            'w')
    f2 = open('RV_obs_sigma.dat', 'w')
    f3 = open('giants.dat', 'w')
    print >> f4, '#0)r2D(pc) 1)sigma_v(1D; km/s) 2) sigma_err (km/s)'
    ####################
    data = np.genfromtxt(path + string + '.snap' + snapno + '.2Dproj.dat')
    #####################
    ###################
    Vr = []
    R = []
    bin_count = 0
    count = 0
    bin_array = []
    for i in range(0, len(data)):
        bin = 0
        if ALL == 1:
            if data[i, 1] <= 1e6:  # Looks at all stars, not a cut.
                #if data[i,3] <= 1.8 and data[i,3] >= -1.2:  # asks if in V-band mag range given by Caretta et al. 2009.
                r_xyz = [data[i, 15], data[i, 16],
                         data[i, 17]]  #x,y,z components of r in pc
                v_xyz = [data[i, 18], data[i, 19],
                         data[i, 20]]  #x,y,z components of velocity in km/s
                count = count + 1
                if data[i, 2] == 1:
                    bin_count = bin_count + 1
                    bin = 1
                bin_array.append(bin)
                r = np.sqrt(r_xyz[0]**2. + r_xyz[1]**2.)
                #R_temp = conversions.pc_to_arcsec(r,d_heliocentric)  #Converts radius in pc to arcsec
                R.append(r)
                Vr.append(
                    data[i,
                         20])  # Just use the z component as your LOS direction
        if ALL == 0:
            if data[i, 2] == 1:
                if data[i, 5] >= 2 and data[i, 5] <= 9:
                    r_xyz = [data[i, 15], data[i, 16],
                             data[i, 17]]  #x,y,z components of r in pc
                    v_xyz = [data[i, 18], data[i, 19],
                             data[i,
                                  20]]  #x,y,z components of velocity in km/s
                    count = count + 1
                    if data[i, 2] == 1:
                        bin_count = bin_count + 1
                        bin = 1
                    bin_array.append(bin)
                    bin_array.append(bin)
                    bin_array.append(bin)
                    r = np.sqrt(r_xyz[0]**2. + r_xyz[1]**2.)
                    #R_temp = conversions.pc_to_arcsec(r,d_heliocentric)  #Converts radius in pc to arcsec
                    R.append(r)
                    Vr.append(data[
                        i,
                        20])  # Just use the z component as your LOS direction
                    R.append(np.sqrt(r_xyz[1]**2. + r_xyz[2]**2.))
                    Vr.append(data[i, 18])
                    R.append(np.sqrt(r_xyz[0]**2. + r_xyz[2]**2.))
                    Vr.append(data[i, 19])
            if data[i, 6] >= 2 and data[i, 6] <= 9:
                r_xyz = [data[i, 15], data[i, 16],
                         data[i, 17]]  #x,y,z components of r in pc
                v_xyz = [data[i, 18], data[i, 19],
                         data[i, 20]]  #x,y,z components of velocity in km/s
                count = count + 1
                if data[i, 2] == 1:
                    bin_count = bin_count + 1
                    bin = 1
                bin_array.append(bin)
                bin_array.append(bin)
                bin_array.append(bin)
                r = np.sqrt(r_xyz[0]**2. + r_xyz[1]**2.)
                #R_temp = conversions.pc_to_arcsec(r,d_heliocentric)  #Converts radius in pc to arcsec
                R.append(r)
                Vr.append(
                    data[i,
                         20])  # Just use the z component as your LOS direction
                R.append(np.sqrt(r_xyz[1]**2. + r_xyz[2]**2.))
                Vr.append(data[i, 18])
                R.append(np.sqrt(r_xyz[0]**2. + r_xyz[2]**2.))
                Vr.append(data[i, 19])
            if data[i, 2] != 1:
                if data[i, 3] >= 2 and data[i, 3] <= 9:
                    r_xyz = [data[i, 15], data[i, 16],
                             data[i, 17]]  #x,y,z components of r in pc
                    v_xyz = [data[i, 18], data[i, 19],
                             data[i,
                                  20]]  #x,y,z components of velocity in km/s
                    count = count + 1
                    if data[i, 2] == 1:
                        bin_count = bin_count + 1
                        bin = 1
                    bin_array.append(bin)
                    bin_array.append(bin)
                    bin_array.append(bin)
                    r = np.sqrt(r_xyz[0]**2. + r_xyz[1]**2.)
                    #R_temp = conversions.pc_to_arcsec(r,d_heliocentric)  #Converts radius in pc to arcsec
                    R.append(r)
                    Vr.append(data[
                        i,
                        20])  # Just use the z component as your LOS direction
                    R.append(np.sqrt(r_xyz[1]**2. + r_xyz[2]**2.))
                    Vr.append(data[i, 18])
                    R.append(np.sqrt(r_xyz[0]**2. + r_xyz[2]**2.))
                    Vr.append(data[i, 19])
################################################
    mean_model = np.mean(Vr[:])  #Find global mean of all model RVs
    print 'mean=', mean_model
    array = np.zeros((len(Vr), 3))
    for i in range(0, len(Vr)):
        array[i, 0] = R[i]
        array[i, 1] = Vr[i]
        array[i, 2] = bin_array[i]
    array = array[array[:, 0].argsort(
    )]  # sorts each measurement in order of radial position
    for i in range(0, len(Vr)):
        print >> f, array[i, 0], array[i, 1], array[
            i,
            2]  #Print each radius/LOS velocity pair in order of radial position


#####################################
    sigma_array = []
    R_array = []
    R_array2 = []
    mean_array = []
    mean_array2 = []
    sigma_array2 = []
    flag = 0
    sum = 0
    print >> f1, snapno,
    ## Define each bin as having 2000 stars. Every 2000 stars, start new bin

    vel_array = [
    ]  # Makes an array with velocites of all stars within each bin
    r_array = []
    bin_count = 0
    total_count = 0
    for j in range(0, len(array)):
        if total_count <= 25:
            vel_array.append(array[j, 1])
            r_array.append(array[j, 0])
            total_count = total_count + 1
            if array[j, 2] == 1:
                bin_count = bin_count + 2
                total_count = total_count + 1
        else:
            count = 0.
            for k in range(0, len(vel_array)):
                r = np.mean(r_array)
                sum = sum + (vel_array[k] - mean_model)**2.
                count = count + 1.
            sigma = np.sqrt(sum / count)
            error = np.sqrt(sigma**2.0 / (2. * count))
            print >> f1, r, sigma, error,
            print >> f4, r, sigma, error
            #print r, 'sigma=',sigma, 'error=',error, 'count=',count, 'N_binaries=',bin_count,'N_stars=',total_count,'binary fraction=',float(bin_count)/float(total_count)
            sigma_array.append(sigma)
            R_array.append(np.mean(r_array))
            sum = 0
            flag = 0
            bin_count = 0
            total_count = 0
            vel_array = []
            r_array = []
    print >> f1, ' '
Пример #6
0
def velocity_dispersion_type(path,string, snapno,ALL=0,type=14):
        import scripts
        import random
        units=scripts.read_units(path+string)
        km = units[0]['l_cgs']*1.0e-5
        time = units[0]['nbt_cgs']
        f = open('RV_model.dat','w')
        f1 = open('RV_model_sigma.dat','a')
	if type == 14:
		type_label = 'BH'
	if type == 13:
                type_label = 'NS'
        if ALL == 1:
                f4 = open(path+string+'.snap'+snapno+'.vel_dispersion_tight.dat','w')
        if ALL == 0:
                f4 = open(path+string+'.snap'+snapno+'.vel_dispersion_'+type_label+'.dat','w')
        f2 = open('RV_obs_sigma.dat','w')
        f3 = open('giants.dat','w')
        print>>f4,'#0)r2D(pc) 1)sigma_v(1D; km/s) 2) sigma_err (km/s)'
####################
        f55 = gzip.open(path+string+'.snap'+snapno+'.dat.gz','r')
        lines55 = f55.readlines()
        #data = np.genfromtxt(path+string+'.snap'+snapno+'.dat.gz')
#####################
###################
        Vr = []
        R = []
        bin_count = 0
        count = 0
        bin_array = []
        for i in range(2,len(lines55)):
                line55 = lines55[i]
                data = line55.split(' ')
                for k in range(0,21):
                        data[k] = np.float(data[k])
                bin = 0
                if ALL == 1:
			v_r = data[3]*km/time # converts from code units to km/s
                        v_t = data[4]*km/time
                        r_km = data[2]*km  #convert r from code units to km

                        if data[7] != 1:
				for k in range(0,1):
					r,v = convert_to_3d(r_km, v_r, v_t)
					Vr.append(v[0])    # Use this if you want 1d vel dispersion
					r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
					R.append(r)
					count = count + 1

                if ALL == 0:
                        v_r = data[3]*km/time # converts from code units to km/s
                        v_t = data[4]*km/time
                        r_km = data[2]*km  #convert r from code units to km

                        if data[7] != 1:
                                if data[14] == type:
                                        for k in range(0,1):
                                        #for k in range(0,25):
                                                r,v = convert_to_3d(r_km, v_r, v_t)
                                                Vr.append(v[0])    # Use this if you want 1d vel dispersion
                                                r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
                                                R.append(r)
                                                count = count + 1
################################################
        mean_model = np.mean(Vr[:]) #Find global mean of all model RVs
        print 'mean=',mean_model, len(Vr)
        array = np.zeros((len(Vr),2))
        for i in range(0,len(Vr)):
                array[i,0] = R[i]
                array[i,1] = Vr[i]
        array = array[array[:,0].argsort()]  # sorts each measurement in order of radial position
        for i in range(0,len(Vr)):
                print>>f, array[i,0], array[i,1] #Print each radius/LOS velocity pair in order of radial position       

#####################################   
        sigma_array = []
        R_array = []
        R_array2 = []
        mean_array = []
        mean_array2 = []
        sigma_array2 = []
        flag = 0
        sum = 0
        print>>f1, snapno,
        ## Define each bin as having 2000 stars. Every 2000 stars, start new bin

        vel_array = []  # Makes an array with velocites of all stars within each bin
        r_array = []
        bin_count = 0
        total_count = 0
        for j in range(0,len(array)):
		if ALL == 0:
			count_max = 70
		if ALL == 1:
                        count_max = 1000
                if total_count <= count_max:
                        vel_array.append(array[j,1])
                        r_array.append(array[j,0])
                        total_count = total_count + 1
                else:
                        count = 0.
                        for k in range(0,len(vel_array)):
                                r = np.mean(r_array)
                                sum = sum + (vel_array[k]-mean_model)**2.
                                count = count + 1.
                        sigma = np.sqrt(sum/count)
                        error = np.sqrt(sigma**2.0/(2.*count))
                        print>>f1, r, sigma, error,
                        print>>f4, r, sigma, error
                        #print r, 'sigma=',sigma, 'error=',error, 'count=',count, 'N_binaries=',bin_count,'N_stars=',total_count,'binary fraction=',float(bin_count)/float(total_count)
                        sigma_array.append(sigma)
                        R_array.append(np.mean(r_array))
                        sum = 0
                        flag = 0
                        bin_count = 0
                        total_count = 0
                        vel_array = []
                        r_array = []
        print>>f1,' '
Пример #7
0
def velocity_dispersion(path,string,snapno,ALL=1,Bin_no=25):
	import scripts
	import random
	units=scripts.read_units(path+string)
	km = units[0]['l_cgs']*1.0e-5
	time = units[0]['nbt_cgs']
	f = open('RV_model.dat','w')
	f1 = open('RV_model_sigma.dat','a')
	if ALL == 1:
		f4 = open(path+string+'.snap'+snapno+'.vel_dispersion_tight.dat','w')
	if ALL == 0:
		f4 = open(path+string+'.snap'+snapno+'.vel_dispersion_giants_25.dat','w')
	f2 = open('RV_obs_sigma.dat','w')
	f3 = open('giants.dat','w')
	print>>f4,'#0)r2D(pc) 1)sigma_v(1D; km/s) 2) sigma_err (km/s)'
####################
	f55 = gzip.open(path+string+'.snap'+snapno+'.dat.gz','r')
	lines55 = f55.readlines()
	#data = np.genfromtxt(path+string+'.snap'+snapno+'.dat.gz')
#####################
###################
	Vr = []; Vpm_r = []; Vpm_t = []; Vpm = []
	R = []
	bin_count = 0
	count = 0
	bin_array = []
	for i in range(2,len(lines55)):
		line55 = lines55[i]
		data = line55.split(' ')
		for k in range(0,21):
			data[k] = np.float(data[k])			
		bin = 0
		if ALL == 1:
			if data[1] <= 1e6:	# Looks at all stars, not a cut.
			#if data[i,3] <= 1.8 and data[i,3] >= -1.2:  # asks if in V-band mag range given by Caretta et al. 2009.
				r_xyz = [data[15], data[16], data[17]]  #x,y,z components of r in pc
				v_xyz = [data[18], data[19], data[20]]  #x,y,z components of velocity in km/s
				count = count + 1
				if data[2] == 1:
					bin_count = bin_count + 1
					bin = 1
				bin_array.append(bin)
				r = np.sqrt(r_xyz[0]**2. + r_xyz[1]**2.)
				#R_temp = conversions.pc_to_arcsec(r,d_heliocentric)  #Converts radius in pc to arcsec
				R.append(r)
				Vr.append(data[20])		# Just use the z component as your LOS direction
		if ALL == 0:
			v_r = data[3]*km/time # converts from code units to km/s
			v_t = data[4]*km/time
			r_km = data[2]*km  #convert r from code units to km
			## EXCLUDE BINARIES HERE ######
			#if data[i,7] == 1:
			#	if data[i,17] >= 2 and data[i,17] <= 9:
			#		for k in range(0,70):
			#			r,v = convert_to_3d(r_km, v_r, v_t)
			#			Vr.append(v[0])    # Use this if you want 1d vel dispersion
			#			r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
			#			R.append(r)
			#			count = count + 1
				
			#	if data[i,18] >= 2 and data[i,18] <= 9:
                        #                for k in range(0,70):
                        #                       r,v = convert_to_3d(r_km, v_r, v_t)
                        #                        Vr.append(v[0])    # Use this if you want 1d vel dispersion
                        #                        r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
                        #                        R.append(r)
                        #                        count = count + 1			
			if data[7] != 1:
				if data[14] >= 2 and data[14] <= 9:
                    for k in range(0,1):
					#for k in range(0,25):
                        r,v = convert_to_3d(r_km, v_r, v_t)
                        Vr.append(v[0])    # Use this if you want 1d vel dispersion
                        Vpm_r.append(v[1]); Vpm_t.append(v[2])
                        Vpm.append(np.sqrt(v[1]**2 + v[2]**2))
                        r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
                        R.append(r)
                        count = count + 1
################################################
	mean_model = np.mean(Vr[:]) #Find global mean of all model RVs
	mean_model_pmr = np.mean(Vpm_r[:])
	mean_model_pmt = np.mean(Vpm_t[:])
	mean_model_pm = np.mean(Vpm[:])
	print 'mean=',mean_model
	array = np.zeros((len(Vr),5))
	for i in range(0,len(Vr)):
		array[i,0] = R[i]
		array[i,1] = Vr[i]
		array[i,2] = Vpm_r[i]
		array[i,3] = Vpm_t[i]
		array[i,4] = Vpm[i]
	array = array[array[:,0].argsort()]  # sorts each measurement in order of radial position
	for i in range(0,len(Vr)):
		print>>f, array[i,0], array[i,1], array[i,2], array[i,3], array[i,4] #Print each radius/LOS velocity pair in order of radial position	

#####################################	
	sigma_vel_array = []
	sigma_pmr_array = []
	sigma_pmt_array = []
	sigma_pm_array = []
	R_array = []
	#mean_array = []
	#flag = 0
	sum_vel = 0
	sum_pmr = 0
	sum_pmt = 0
	sum_pm = 0
	print>>f1, snapno,
        ## Define each bin as having 2000 stars. Every 2000 stars, start new bin

	vel_array = []  # Makes an array with velocites of all stars within each bin
	pmr_array = []; pmt_array = []; pm_array = []
	r_array = []
	#bin_count = 0
	total_count = 0
	for j in range(0,len(array)):   
		if total_count <= Bin_no:
		#if total_count <= 500:
			vel_array.append(array[j,1])
			pmr_array.append(array[j,2])
			pmt_array.append(array[j,3])
			pm_array.append(array[j,4])
			r_array.append(array[j,0])
			total_count = total_count + 1
		else:
			count = 0.
			for k in range(0,len(vel_array)):
				r = np.mean(r_array)
				sum_vel = sum_vel + (vel_array[k]-mean_model)**2.
				sum_pmr = sum_pmr + (pmr_array[k]-mean_model_pmr)**2.
				sum_pmt = sum_pmt + (pmt_array[k]-mean_model_pmt)**2.
				sum_pm = sum_pm + (pm_array[k]-mean_model_pm)**2.
				count = count + 1.
			sigma_vel = np.sqrt(sum_vel/count)
			error_vel = np.sqrt(sigma_vel**2.0/(2.*count))
            sigma_pmr = np.sqrt(sum_pmr/count)
			error_pmr = np.sqrt(sigma_pmr**2.0/(2.*count))
			sigma_pmt = np.sqrt(sum_pmt/count)
			error_pmt = np.sqrt(sigma_pmt**2.0/(2.*count))
			sigma_pm = np.sqrt(sum_pm/count)
			error_pm = np.sqrt(sigma_pm**2.0/(2.*count))

			print>>f1, r, sigma, error, sigma_pmr, error_pmr, sigma_pmt, error_pmt, sigma_pm, error_pm
			print>>f4, r, sigma, error, sigma_pmr, error_pmr, sigma_pmt, error_pmt, sigma_pm, error_pm
			#print r, 'sigma=',sigma, 'error=',error, 'count=',count, 'N_binaries=',bin_count,'N_stars=',total_count,'binary fraction=',float(bin_count)/float(total_count)
			sigma_vel_array.append(sigma_vel)
			sigma_pmr_array.append(sigma_pmr)
			sigma_pmt_array.append(sigma_pmt)
			sigma_pm_array.append(sigma_pm)
			R_array.append(np.mean(r_array))
			sum_vel = 0; sum_pmr = 0; sum_pmt = 0; sum_pm = 0
			#flag = 0
			#bin_count = 0
			total_count = 0
			vel_array = []; pmr_array = []; pmt_array = []; pm_array = []
			r_array = []
import gzip
import scripts
import matplotlib.pyplot as plt
import history_cmc
import blackhole
import extract_observed_prop as OBS


path = '/projects/b1095/syr904/cmc/47Tuc/rundir/47Tuc/best_fits/MOCHA47Tuc_elson_rv4_3e6_tcon/'
N=3000000
Z=0.0038
rv=4
rg=7.4

string = 'initial'
units=scripts.read_units(path+string)
km = units[0]['l_cgs']*1.0e-5
time_units = units[0]['t_myr']
m_cgs = units[0]['l_cgs']*1.0e-2
kg = units[0]['m_cgs']*1.0e-3
time_cgs = units[0]['t_cgs']
nbt = units[0]['nbt_cgs']
M_total_units = units[0]['m_msun']
pc = units[0]['l_pc']

time_array, snap_array = finder.find_snap_time_array(path,string)
#print snap_array
snapno_max_str = snap_array[-1]
snapno_max = int(snapno_max_str)
Delta = -3  #default -5000 for only making the last snapshot
    print(time_array)
    print('t_onset=',time_i)
    time = h[id0]['binint']['binint']
        if snapno <= 300:
                Delta = -1
        if snapno <= 600 and snapno > 300:
                Delta = -4
        if snapno <= 1500 and snapno > 600:
                Delta = -6
        if snapno <= 10000 and snapno > 1500:
                Delta = -8
        if snapno > 10000:
                Delta = -10

    snapno_max = int(snapno)
    units=scripts.read_units(path+'/'+'initial')
    km = units[0]['l_cgs']*1.0e-5
    time_units = units[0]['t_myr']

    flag = snapno
    m0 = []        # 0 is the companion
    m1 = []        # 1 is the BH    
    ID0 = []
    Porb = []
    e = []
    timearr = []
    for j in range(snapno,0,Delta):
        if ID_FLAG == 1:
            print('stopped!')
            break
        N_XRB = 0
Пример #10
0
def velocity_dispersion(path,string, snapno,ALL=1):
	import scripts
	import random
	units=scripts.read_units(path+string)
	km = units[0]['l_cgs']*1.0e-5
	time = units[0]['nbt_cgs']
	f = open('RV_model.dat','w')
	f1 = open('RV_model_sigma.dat','a')
	if ALL == 1:
		f4 = open(path+string+'.snap'+snapno+'.vel_dispersion_tight.dat','w')
	if ALL == 0:
		f4 = open(path+string+'.snap'+snapno+'.vel_dispersion_giants_25.dat','w')
	f2 = open('RV_obs_sigma.dat','w')
	f3 = open('giants.dat','w')
	print>>f4,'#0)r2D(pc) 1)sigma_v(1D; km/s) 2) sigma_err (km/s)'
####################
	data = np.genfromtxt(path+string+'.snap'+snapno+'.dat.gz')
#####################
###################
	Vr = []
	R = []
	bin_count = 0
	count = 0
	bin_array = []
	for i in range(0,len(data)):
		bin = 0
		if ALL == 1:
                        v_r = data[i,3]*km/time # converts from code units to km/s
                        v_t = data[i,4]*km/time
                        r_km = data[i,2]*km  #convert r from code units to km
			if data[i,7] != 1:	# Looks at singles only
                                if data[i,14] <= 9 and data[i,1] >= 0.4:
                                        for k in range(0,1):
                                        #for k in range(0,25):
                                                r,v = convert_to_3d(r_km, v_r, v_t)
                                                Vr.append(v[0])    # Use this if you want 1d vel dispersion
                                                r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
                                                R.append(r)
                                                count = count + 1
		if ALL == 0:
			v_r = data[i,3]*km/time # converts from code units to km/s
			v_t = data[i,4]*km/time
			r_km = data[i,2]*km  #convert r from code units to km
			if data[i,7] != 1:
				if data[i,14] >= 2 and data[i,14] <= 9:
                                        for k in range(0,1):
					#for k in range(0,25):
                                                r,v = convert_to_3d(r_km, v_r, v_t)
                                                Vr.append(v[0])    # Use this if you want 1d vel dispersion
                                                r = np.sqrt(r[1]**2. + r[2]**2.)/3.086e13  # convert r from km to pc
                                                R.append(r)
                                                count = count + 1
################################################
	mean_model = np.mean(Vr[:]) #Find global mean of all model RVs
	print 'mean=',mean_model
	array = np.zeros((len(Vr),2))
	for i in range(0,len(Vr)):
		array[i,0] = R[i]
		array[i,1] = Vr[i]
	array = array[array[:,0].argsort()]  # sorts each measurement in order of radial position
	for i in range(0,len(Vr)):
		print>>f, array[i,0], array[i,1] #Print each radius/LOS velocity pair in order of radial position	

#####################################	
	sigma_array = []	
	R_array = []
	R_array2 = []
	mean_array = []
	mean_array2 = []
	sigma_array2 = []
	flag = 0
	sum = 0
	print>>f1, snapno,
        ## Define each bin as having 2000 stars. Every 2000 stars, start new bin

	vel_array = []  # Makes an array with velocites of all stars within each bin
	r_array = []
	bin_count = 0
	total_count = 0
	r_min = conversions.pc_to_arcsec(array[0,0],5.0)
	r_min_bin = r_min
	for j in range(0,len(array)):
		RRR = conversions.pc_to_arcsec(array[j,0],5.0) 
		#print j, r_min_bin, RRR, RRR - r_min_bin, total_count  
		if total_count <= 2000:# or RRR - r_min_bin <= 1.58:
		#if total_count <= 500:
			vel_array.append(array[j,1])
			r_array.append(array[j,0])
			total_count = total_count + 1
		else:
		#if total_count > 100 and RRR - r_min_bin > 1.58:
			count = 0.
			r_min_bin = conversions.pc_to_arcsec(array[j,0],5.0)
			for k in range(0,len(vel_array)):
				r = np.mean(r_array)
				sum = sum + (vel_array[k]-mean_model)**2.
				count = count + 1.
			sigma = np.sqrt(sum/count)
			error = np.sqrt(sigma**2.0/(2.*count))
			print>>f1, r, sigma, error,
			print>>f4, r, sigma, error
			#print r, 'sigma=',sigma, 'error=',error, 'count=',count, 'N_binaries=',bin_count,'N_stars=',total_count,'binary fraction=',float(bin_count)/float(total_count)
			sigma_array.append(sigma)
			R_array.append(np.mean(r_array))
			sum = 0
			flag = 0
			bin_count = 0
			total_count = 0
			vel_array = []
			r_array = []
	print>>f1,' '