def run():
    H0 = Constants.H0
    WM = Constants.WM
    WV = Constants.WV

    smbh_cluster = get_clusters.get_pickled_file('smbh_cluster.pkl')
    smbh_by_id = analyze_clusters.get_smbh_by_id(smbh_cluster)

    galaxies_cluster_no_bad_z = get_clusters.get_pickled_file('galaxies_cluster_no_bad_z.pkl')
    galaxies_by_id = analyze_clusters.get_galaxies_by_id(galaxies_cluster_no_bad_z)

    galaxies_masses, final_masses = analyze_clusters.get_galaxies_masses(galaxies_by_id)

    galaxy_num = '80'
    if galaxy_num in galaxies_by_id.keys():
        gm_popt = get_galaxy_mass_coefficients(galaxies_masses, galaxy_num)
        print 'Galaxy Mass Coefficients for Galaxy #%s:' % (galaxy_num)
        print gm_popt    

#        bh_popt, central_bh_mass_fitted = merger_tree_plots.curve_fit_central_bh_masses(galaxy_num, central_bh_mass)
#        print 'Central Black Hole Mass Coefficients for Galaxy #%s:' % (galaxy_num)
#        print sm_popt

        sm_popt = get_star_mass_coefficients(galaxies_by_id, galaxy_num)
        print 'Stellar Mass Coefficients for Galaxy #%s:' % (galaxy_num)
        print sm_popt

    else:
        sys.exit('No galaxy with such a number!')
def run():
    galaxies_cluster_no_bad_z = get_clusters.get_pickled_file('galaxies_cluster_no_bad_z.pkl')
    galaxies_by_id = analyze_clusters.get_galaxies_by_id(galaxies_cluster_no_bad_z)
    smbh_cluster = get_clusters.get_pickled_file('smbh_cluster.pkl')
    for galaxy_num in ['1', '51', '65']:
        #time orig. in Gyr; use Myr to not have to convert it in hermite code
        time = [time_slice[1]*1.e3 for time_slice in galaxies_by_id[galaxy_num]] 
        stellar_mass = analyze_clusters.get_galaxy_stellar_mass(galaxies_by_id, galaxy_num)
        popt = get_func_coeffs(time, stellar_mass)
        print 'Galaxy #%s:' % (galaxy_num)
        print popt
        stellar_mass_fitted = [10.**(poly_func(t, *tuple(popt))) for t in time]
        plot_stellar_mass(time, galaxy_num, stellar_mass, stellar_mass_fitted)
Esempio n. 3
0
def run():
    H0 = Constants.H0
    WM = Constants.WM
    WV = Constants.WV

#    smbh_cluster = get_clusters.process_smbh_cluster(H0, WM, WV)
    smbh_cluster = get_clusters.get_pickled_file('smbh_cluster.pkl')

#    galaxies_cluster = get_clusters.process_galaxies_cluster(H0, WM, WV)
    galaxies_cluster_no_bad_z = get_clusters.get_pickled_file('galaxies_cluster_no_bad_z.pkl')

#    galaxy_1 = [line for line in galaxies_cluster if line[2]==1]
#    black_holes = [time_slice[-1] for time_slice in galaxy_1 if time_slice[0]<=0.0][0]
#    print black_holes

    galaxies_by_id = analyze_clusters.get_galaxies_by_id(galaxies_cluster_no_bad_z)
    galaxies_masses, final_masses = analyze_clusters.get_galaxies_masses(galaxies_by_id)
    print galaxies_masses['65']

    analyze_clusters.plot_galaxies_all_masses(galaxies_masses)
    '''
def run():
    mpl.rcParams.update({'font.size': 15})
    plt.rc('text', usetex=True)
    plt.rc('font', family='Computer Modern Sans serif')
    H0 = Constants.H0
    WM = Constants.WM
    WV = Constants.WV

#    smbh_cluster = get_clusters.process_smbh_cluster(H0, WM, WV)
    smbh_cluster = get_clusters.get_pickled_file('smbh_cluster.pkl')
    smbh_by_id = analyze_clusters.get_smbh_by_id(smbh_cluster)

#    galaxies_cluster = get_clusters.process_galaxies_cluster(H0, WM, WV)
    galaxies_cluster_no_bad_z = get_clusters.get_pickled_file('galaxies_cluster_no_bad_z.pkl')
    galaxies_by_id = analyze_clusters.get_galaxies_by_id(galaxies_cluster_no_bad_z)
    galaxies_masses, final_masses = analyze_clusters.get_galaxies_masses(galaxies_by_id)


    for galaxy_num in ['1', '65', '149', '187', '217']:
#    galaxy_num = '187'
        smbh_mass = np.array(analyze_clusters.get_cbh_accreted_plus_seed_mass(smbh_by_id, galaxy_num))
        popt, smbh_mass_fitted = curve_fit_central_bh_masses(galaxy_num, smbh_mass)
        smbh_mass_fitted = np.array(smbh_mass_fitted)
        
        galaxy_mass_v_time = zip(galaxies_masses[galaxy_num][1], galaxies_masses[galaxy_num][2])
        galaxy_mass_v_time = np.array(map(list, galaxy_mass_v_time))
        popt_galaxy_mass = analyze_clusters.curve_fit_any_galaxy_mass(galaxies_masses, galaxy_num)
        ydata_est = [analyze_clusters.poly_func(x, *tuple(popt_galaxy_mass)) for x in galaxies_masses[galaxy_num][1]]
        galaxies_mass_fitted = [galaxies_masses[galaxy_num][1], [10**y for y in ydata_est]]
        galaxies_mass_fitted = np.array(galaxies_mass_fitted)

        stellar_mass = analyze_clusters.get_galaxy_stellar_mass(galaxies_by_id, galaxy_num)
        stellar_mass_v_time = zip(galaxies_masses[galaxy_num][1], stellar_mass)
        stellar_mass_v_time = np.array(map(list, stellar_mass_v_time))
#        print galaxies_by_id[galaxy_num]
#        print galaxy_mass_v_time, '\n'
#        print stellar_mass_v_time, '\n'
#        print smbh_mass

        plt.figure()
        plt.semilogy(galaxy_mass_v_time[:,0], galaxy_mass_v_time[:,1], 'b', lw=3.5, alpha=0.5, label='Galaxy Mass')
        plt.semilogy(galaxies_mass_fitted[0,:], galaxies_mass_fitted[1,:], 'b--', lw=3.5, alpha=0.5)
#        plt.ylabel('Mass [M$_\odot$]')
        plt.ylabel(r'$M_{BH} [M_\odot]$')

        plt.semilogy(stellar_mass_v_time[:,0], stellar_mass_v_time[:,1], 'g', lw=2., alpha=0.5, label='Stellar Mass')
        popt_star_mass = galaxies_star_mass.get_func_coeffs(stellar_mass_v_time[:,0]*1.e3, stellar_mass_v_time[:,1])
        stellar_mass_fitted = zip(stellar_mass_v_time[:,0]*1.e3, 
                                  [10.**(galaxies_star_mass.poly_func(t, *tuple(popt_star_mass))) for t in 
                                                                                         stellar_mass_v_time[:,0]*1.e3])
        stellar_mass_fitted = np.array(stellar_mass_fitted)
        plt.semilogy(stellar_mass_fitted[:,0]/1.e3, stellar_mass_fitted[:,1], 'g--', lw=2., alpha=0.5)

        print(galaxy_num, smbh_mass[:,1])
        plt.semilogy(smbh_mass[:,0], smbh_mass[:,1], 'r', lw=0.5, alpha=1., label='Central BH Mass')
        plt.semilogy(smbh_mass_fitted[0,:], smbh_mass_fitted[1,:], 'r--', lw=1., alpha=0.5)

        plt.xticks(np.arange(0., math.ceil(max(galaxy_mass_v_time[:,0]))+1., 1.0))
        plt.xlabel('Time [Gyr]')
        plt.xticks(np.arange(0, 16, 2))
#        plt.xlim([0, 14])
        plt.ylim([10.**4, 10.**15])
        plt.tight_layout()
        plt.legend(loc='lower right')
        plt.savefig(os.path.join(plots_folder, 'Masses_plot_galaxy_%s.png' % (galaxy_num)))
#        plt.show()
        plt.close()

#    print galaxies_by_id[galaxy_num]

#    galaxy_1 = [line for line in galaxies_cluster if line[2]==1]
#    black_holes = [time_slice[-1] for time_slice in galaxy_1 if time_slice[0]<=0.0][0]
#    print black_holes


#    analyze_clusters.plot_galaxies_all_masses(galaxies_masses)
    '''
    top_masses = 3
    galaxies_max_mass = analyze_clusters.get_most_massive_galaxies(galaxies_masses, final_masses, top_masses)
#    print galaxies_max_mass

    F1 = open(os.path.join(get_clusters.cluster_folder, ''.join([str(top_masses), '_max_mass_galaxies.pkl'])), 'wb')
    pickle.dump(galaxies_max_mass, F1)
    F1.close()
#    analyze_clusters.plot_max_galaxies_masses(galaxies_max_mass)

    popt_galaxies_masses, galaxies_max_mass_fitted = analyze_clusters.curve_fit_most_massive_galaxies(galaxies_max_mass)
    print popt_galaxies_masses

    F = open(os.path.join(get_clusters.cluster_folder,
                          ''.join([str(top_masses), '_max_mass_galaxies_fitted.pkl'])), 'wb')
    pickle.dump(galaxies_max_mass_fitted, F)
    F.close()
####################################################
    f = open(os.path.join(os.path.expanduser('~'), 'Dropbox', 'Columbia', 'Ostriker', 'Cannibalism', 'hermite',
                      'galaxy_mass.txt'), 'rb')
    time_mass = f.readlines()
    time = []
    mass = []
    for line in time_mass:
        time.append(float(line.split()[0]))
        mass.append(float(line.split()[1]))
    time = np.array(time)
    garbage = []
    mass = np.array(mass)
    galaxies_max_mass_fitted['65'] = [time, garbage, mass]

    analyze_clusters.plot_max_masses_galaxies_orig_and_fitted(galaxies_max_mass, galaxies_max_mass_fitted, top_masses)
    '''
    fit_and_plot_central_bh_masses(smbh_cluster, galaxies_cluster_no_bad_z)
def run():
    mpl.rcParams.update({'font.size': 15})
    plt.rc('text', usetex=True)
    plt.rc('font', family='Computer Modern Sans serif')
    smbh_cluster = get_clusters.get_pickled_file('smbh_cluster.pkl')
    galaxies_cluster_no_bad_z = get_clusters.get_pickled_file('galaxies_cluster_no_bad_z.pkl')

    galaxies_by_id = analyze_clusters.get_galaxies_by_id(galaxies_cluster_no_bad_z)
#    print(galaxies_by_id['217'])
    galaxies_masses, final_masses = analyze_clusters.get_galaxies_masses(galaxies_by_id)
#    write_galaxies_final_masses(galaxies_masses)

#    galaxy_num = '65'
    merged_bhs_from_simulations = {'1': [[7.63838E+06,2.35248E+06,1.23739E+09,2.14763E+08],
                                         [3146.E+06, 12560.E+06, 2974.E+06, 71209.E+06]],
                                   '65': [[8.08983E+08,7.20130E+07,2.96940E+08,7.06418E+07],
                                          [10000.E+06, 61228.E+06, 19018.E+06, 63883.E+06]],
                                   '187': [[1.21178E+07, 2.02427E+07], [72505.E+06, 50722.E+06]],
                                   '217': [[1.34011E+07, 1.11187E+06], [3248.E+06, 20166.E+06]]}
    galaxy_names = {'1': 'A', '65': 'B', '187': 'D', '217': 'E'}
    for galaxy_num, name in galaxy_names.iteritems():
        t_s, masses_fitted, galaxy_mass_coeffs = get_galaxy_mass_data(galaxies_masses, galaxy_num)

        central_bh_mass, central_bh_mass_fitted, central_bh_mass_coeffs, bh_masses = \
                                                get_central_bh_mass_growth(galaxies_cluster_no_bad_z, smbh_cluster,
                                                                                                             galaxy_num)
        time, stellar_mass, stellar_mass_coefficients, stellar_mass_fitted = get_stellar_mass_growth(galaxies_by_id,
                                                                                                             galaxy_num)
        output_coeffs_for_achain_h(galaxy_num, central_bh_mass_coeffs, galaxy_mass_coeffs, stellar_mass_coefficients)

#        plot_all_data(t_s, masses_fitted, galaxies_masses, galaxy_num, central_bh_mass, central_bh_mass_fitted, time,
#                                                                                     stellar_mass, stellar_mass_fitted)
        ts_at_ctr = read_orbiting_bhs(galaxy_num, stellar_mass_coefficients)
        ts_at_ctr = np.array(ts_at_ctr)
#        print ts_at_ctr
        plt.figure()
        plt.hist(np.log10(ts_at_ctr*1.e6), bins=np.arange(9., 18.+.25, .25), alpha=0.5, color='blue',
                                                                                                   label='Orbiting BHs')
        plt.hist(np.log10(merged_bhs_from_simulations[galaxy_num][1]), bins=np.arange(9., 18.+.25, .25), alpha=0.5,
                                                                                       color='red', label='Merged BHs')
        plt.axvline(np.log10(hubble_time), color='g', linestyle='dashed', linewidth=2, label='Hubble Time')
        plt.xlabel(r'$\log_{10}\left(t_{fric}\right) [Yrs]$')
        plt.ylim([0,23])
        plt.yticks(np.arange(0, 23, 2))
        plt.ylabel('Count of black holes')
#        plt.title(''.join(['Histogram of Orbiting Black Holes Time to Reach Center for Galaxy ',
#                           name]))
        plt.legend(loc='upper left')
        plt.savefig(os.path.join(plots_folder, ''.join(['t_at_center_histogram_gal_', galaxy_num, '.png'])))
        plt.close()
        
        plt.figure()
        bh_masses = np.array(bh_masses)
        print(galaxy_num, np.log10(bh_masses))
        plt.hist(np.log10(bh_masses), bins=np.arange(2., 10.+.25, .25), alpha=0.5, color='blue', label='Orbiting BHs')
        plt.hist(np.log10(merged_bhs_from_simulations[galaxy_num][0]), bins=np.arange(2., 10.+.25, .25), alpha=0.5,
                                                                                       color='red', label='Merged BHs')
#        plt.xlabel('BH Mass (M$_\odot$), log')
        plt.xlabel(r'$\log_{10}\left(M_{BH}\right) [M_\odot]$')
        plt.ylim([0,22])
        plt.yticks(np.arange(0, 23, 2))
        plt.ylabel('Count of black holes')
        plt.legend(loc='upper right')
#        plt.title(''.join(['Histogram of Masses of Orbiting Black Holes for Galaxy ',
#                           name]))
        plt.savefig(os.path.join(plots_folder, ''.join(['orbiting_bh_mass_histogram_gal_', galaxy_num, '.png'])))
        plt.close()