/* Same as above, only now we're calculating the number of pairs * in the cross-correlation. * * NB! -- We're assuming that the satellite galaxy profiles * of both HODs are the same. */ double func1_xcorr(double m) { double N,n,fac2,rvir,f_ss=0,f_cs=0,cvir,x,rfof,ncen1,nsat1,ncen2,nsat2; m=exp(m); cvir=halo_concentration(m)*CVIR_FAC; n=dndM_interp(m); nsat1=N_sat(m); ncen1=N_cen(m); nsat2=N_sat2(m); ncen2=N_cen2(m); rvir=2*pow(3.0*m/(4*DELTA_HALO*PI*OMEGA_M*RHO_CRIT),1.0/3.0); /* Break up the contribution of pairs into * central-satellite (cs) and satellite-satellite (ss) pairs. * But for x-corr, we have c1-s2, c2-s1, s1-s2. */ f_ss=dFdx_ss(r_g2/rvir,cvir)*nsat1*nsat2; f_cs=dFdx_cs(r_g2/rvir,cvir)*(nsat1*ncen2 + nsat2*ncen1); x=n*(f_ss+f_cs)/rvir*m; return(x); }
/* THis is the integrand of the mean number integral: * dM dn/dM P(N_gals|M) N_true(M) * * NB! need to do a correction for the fact that the radius for N * isn't R200 exactly for all masses-- it's R200 for the mean mass in the bin. * */ double m2n_func2(double m) { int i; double x, logm, mu, sig, c, rvir, rs, rhos, nsat, logj; logm = m; m = exp(m); i = M2N.current_bin; logj = log(M2N.current_Ngals); //mu = exp(-0.12)*pow(M2N.current_Ngals/40.0,1.15); //mu = B_rozo + alpha_rozo*log(M2N.current_Ngals/40.0) + log(4e14*HUBBLE); sig = sig_rozo; mu = B_rozo - sig*sig/2 + alpha_rozo*(logm-log(1.0e14*HUBBLE)) + log(40.0); x = exp(-(mu - logj)*(mu - logj)/(2*sig*sig))/(RT2PI*sig)/m; // extrapolate the HOD profile out/in to the mean radius of the bin c = CVIR_FAC*halo_concentration(m); rvir = pow(3*m/(4*PI*RHO_CRIT*OMEGA_M*DELTA_HALO),THIRD); rs = rvir/c; rhos = N_sat(m)/(4*PI*rvir*rvir*rvir*HK_func(rs/rvir)); nsat = 4*PI*rhos*pow(M2N.radius[i],3.0)*HK_func(rs/M2N.radius[i]); //printf("%e %e %e %e\n",m,rvir,M2N.radius[i],nsat/N_sat(m)); return m*nsat*dndM_interp(m)*x; }
double func1cs(double m) { double N,n,fac2,rvir,f_ss,f_cs,cvir,x,rfof,ncen,nsat; m=exp(m); cvir=halo_concentration(m)*CVIR_FAC; n=dndM_interp(m); nsat=N_sat(m); ncen=N_cen(m); rvir=2*pow(3.0*m/(4*DELTA_HALO*PI*OMEGA_M*RHO_CRIT),1.0/3.0); /* Break up the contribution of pairs into * central-satellite (cs) and satellite-satellite (ss) pairs. */ f_cs=dFdx_cs(r_g2/rvir,cvir)*nsat*ncen; x=n*(f_cs)/rvir*m; // if(OUTPUT==3) // printf("%e %e %e %e %e\n",m,n,f_ss,f_cs,rvir); return(x); }
void output_hod(char fname[]) { FILE *fp; double dlogm,sig,m; int i; fp=fopen(fname,"w"); dlogm=(log(HOD.M_max)-log(HOD.M_low))/99; for(i=1;i<=100;++i) { m=exp((i-1)*dlogm)*HOD.M_low; sig = N_sat(m)*N_sat(m) + N_cen(m)*(1-N_cen(m)); fprintf(fp,"%e %e %e %e %e %e\n", m,N_cen(m),N_sat(m),N_avg(m),sig,sig/(N_avg(m)*N_avg(m))); } fclose(fp); }
double func2_m2n(double m) { double ncen,nsat,x; m = exp(m); ncen = N_cen(m); nsat = N_sat(m); x = poisson_prob(g31_number-1,nsat); if(isnan(x))x = 0; return ncen*x*m*dndM_interp(m); }
/* This is the <(N_sat-1)(N_sat)> moment, which is * for the number of pairs of satellite galaxies. * For a Poisson distribution, <N(N-1)> = N^2 */ double moment_ss(double m) { double n,x; n=N_sat(m); return(n*n); // HAMANA et al if(n>1) return n*n; if(n>0.25) return n*n*log10(4*n)/log10(4); return 0; // sub-poisson model from millennium run // doesn't seem to effect -20.5 stats much x = 0.6 + 0.4*exp(-0.1/pow(n,1.5)); return(n*n*x*x); }
/* This is the function passed to qromo in the above routine. * It is the number density of * galaxy pairs in halos of mass m at separation r_g2. * See Equation (11) from Berlind & Weinberg. */ double func1(double m) { double N,n,fac2,rvir,f_ss,f_cs,cvir,x,rfof,ncen,nsat,ss; m=exp(m); cvir=halo_concentration(m)*CVIR_FAC; n=dndM_interp(m); nsat=N_sat(m); ncen=N_cen(m); rvir=2*pow(3.0*m/(4*DELTA_HALO*PI*OMEGA_M*RHO_CRIT),1.0/3.0); /* Break up the contribution of pairs into * central-satellite (cs) and satellite-satellite (ss) pairs. */ f_ss=dFdx_ss(r_g2/rvir,cvir)*moment_ss(m)*0.5; f_cs=dFdx_cs(r_g2/rvir,cvir)*nsat*ncen; x=n*(f_ss+f_cs)/rvir*m; // only send the cs term //x = n*f_cs/rvir*m; // only send the ss term //x = n*f_ss/rvir*m; // HAMANA /* ss = moment_ss(m); if(ss>1) return x; else return n*dFdx_cs(r_g2/rvir,cvir)*moment_ss(m)*0.5/rvir*m; */ // if(OUTPUT==3) // printf("%e %e %e %e %e\n",m,n,f_ss,f_cs,rvir); return(x); }
void populate_simulation_hod() { FILE *fp,*fpa[9],*fp2,*fpb[9],*fpc[9],*fps[9],*fpt; int i,j,k,n,imass,n1,j_start=0,i1,galcnt[1000],halocnt[1000],imag; double mass,xg[3],vg[3],nsat,nc[10],ncen,mlo,mag,err1,err2,r,fac,sigv; char aa[1000]; float x1,xh[3],vh[3],vgf[3]; long IDUM3 = -445; float **galarr; int *galid,id1=0,id2=0,j1; float dx,dy,dz,dr,drh,rv1,rv2,rmin,rmax; float **haloarr; int ngal,nsati[9],ALL_FILES=0,TRACK_GALAXIES=0,WARREN_MASS_CORRECTION=0,haloid; float *xt,*yt,*zt,*vxt,*vyt,*vzt; int SO_FILE = 1, JEANS_DISPERSION = 0; // if(RESOLUTION > 1.6) // WARREN_MASS_CORRECTION = 1; TRACK_GALAXIES=1; galarr = matrix(1,10000000,0,5); haloarr = matrix(1,10000000,0,6); galid = ivector(1,10000000); fp=openfile(Files.HaloFile); sprintf(aa,"%s.mock",Task.root_filename); fp2 = fopen(aa,"w"); sprintf(aa,"%s.mock_halo",Task.root_filename); fpt = fopen(aa,"w"); for(i=0;i<1000;++i) halocnt[i]=0; for(i=0;i<1000;++i) galcnt[i]=0; set_HOD_params(); mlo = HOD.M_low; printf("MLO %e %e %f\n",mlo,HOD.M_min,HOD.sigma_logM); printf("BOX_SIZE %f\n",BOX_SIZE); fflush(stdout); haloid = 0; while(!feof(fp)) { if(SO_FILE) { fscanf(fp,"%d %lf %f %f %f %f %f %f %f %f", &i,&mass,&x1,&x1,&xh[0],&xh[1],&xh[2],&vh[0],&vh[1],&vh[2]); fgets(aa,1000,fp); /* fac=sqrt(4.499E-48)*pow(4*DELTA_HALO*PI*OMEGA_M*RHO_CRIT/3,1.0/6.0)*3.09E19; sigv=fac*pow(mass,1.0/3.0)/sqrt(2.0); printf("%e %e %f\n",mass,sigv,VBIAS); if(i==2) one_halo(1,1); */ //haloid = i; } else { fscanf(fp,"%d %d %e %e %e %e %e %e %e", &i,&imass,&xh[0],&xh[1],&xh[2],&x1,&vh[0],&vh[1],&vh[2]); mass=imass*RHO_CRIT*OMEGA_M*pow(RESOLUTION,3.0); } haloid++; //in case it's not iterated in the file if(mass>HOD.M_max)continue; if(WARREN_MASS_CORRECTION) mass = imass*(1-pow(imass,-0.6))*RHO_CRIT*OMEGA_M*pow(RESOLUTION,3.0); if(mass<mlo)continue; for(i=0;i<3;++i) { if(xh[i]<0)xh[i]+=BOX_SIZE; if(xh[i]>BOX_SIZE)xh[i]-=BOX_SIZE; } i1 = (int)(log10(mass)/0.1); halocnt[i1]++; ncen=N_cen(mass); if(drand48()>ncen)goto SATELLITES; if(VBIAS_C>0) { NFW_central_velocity(mass,vg,mag); for(i=0;i<3;++i) vh[i]+=vg[i]; } fprintf(fp2,"%e %e %e %e %e %e\n",xh[0],xh[1],xh[2],vh[0],vh[1],vh[2]); fprintf(fpt,"%d\n",haloid); // THis mocks up the ~22 columns of the MR mocks // fprintf(fpt,"%d %d %d %e %e %e 0.0 0.0 0.0 %e %e %e 0.0 0.0 0.0 0.0 0.0 0.0 %e 0.0 0.0\n", // 0,0,0,xh[0],xh[1],xh[2],vh[0],vh[1],vh[2],log10(mass)); if(VBIAS_C>0) { for(i=0;i<3;++i) vh[i]-=vg[i]; } galcnt[i1]++; if(TRACK_GALAXIES) { id2++; galid[id2] = haloid; galarr[id2][0] = xh[0]; galarr[id2][1] = xh[1]; galarr[id2][2] = xh[2]; galarr[id2][3] = vh[0]; galarr[id2][4] = vh[1]; galarr[id2][5] = vh[2]; } if(TRACK_GALAXIES) { id1++; haloarr[id1][0] = xh[0]; haloarr[id1][1] = xh[1]; haloarr[id1][2] = xh[2]; haloarr[id1][3] = vh[0]; haloarr[id1][4] = vh[1]; haloarr[id1][5] = vh[2]; haloarr[id1][6] = mass; } SATELLITES: nsat = N_sat(mass); if(nsat>250) n1 = gasdev(&IDUM3)*sqrt(nsat) + nsat; else n1 = poisson_deviate(nsat); /* if(nsat>1) fprintf(stdout,"BUH %d %f %e %d %e %f\n",haloid,nsat,mass,n1,HOD.M1,pow(mass/HOD.M1,HOD.alpha)); */ for(i=1;i<=n1;++i) { r = NFW_position(mass,xg); if(JEANS_DISPERSION) jeans_dispersion(mass,r,vg); else NFW_velocity(mass,vg,mag); for(k=0;k<3;++k) { xg[k]+=xh[k]; if(xg[k]<0)xg[k]+=BOX_SIZE; if(xg[k]>BOX_SIZE)xg[k]-=BOX_SIZE; vg[k]+=vh[k]; /* vg[k] = vgf[k]; */ /* vg[k] = gasdev(&IDUM3)*500; vg[k] = non_gaussian_velocity(); */ } fprintf(fp2,"%e %e %e %e %e %e\n",xg[0],xg[1],xg[2],vg[0],vg[1],vg[2]); fprintf(fpt,"%d\n",haloid); // fprintf(fpt,"%d %d %d %e %e %e 0.0 0.0 0.0 %e %e %e 0.0 0.0 0.0 0.0 0.0 0.0 %e 0.0 0.0\n",1,1,1,xg[0],xg[1],xg[2],vg[0],vg[1],vg[2],log10(mass)); if(TRACK_GALAXIES) { id2++; galid[id2] = haloid; galarr[id2][0] = xg[0]; galarr[id2][1] = xg[1]; galarr[id2][2] = xg[2]; galarr[id2][3] = vg[0]; galarr[id2][4] = vg[1]; galarr[id2][5] = vg[2]; } /* Bin up the galaxies by halo mass to check the HOD */ galcnt[i1]++; } if(feof(fp))break; } fclose(fp2); fclose(fpt); /* output the binned HOD */ sprintf(aa,"%s.binned_HOD",Task.root_filename); fp2=fopen(aa,"w"); for(i=0;i<1000;++i) if(galcnt[i]>0) fprintf(fp2,"%d %f %f %d %d\n", i,(i+0.5)*0.1,(float)galcnt[i]/halocnt[i],galcnt[i],halocnt[i]); fclose(fp2); /* Calculate the two-halo term */ if(TRACK_GALAXIES) { fprintf(stderr,"Calling nbody_2halo...\n"); calc_nbody_two_halo(galarr,galid,id2); } return ; /* Track the close pairs - for Non-Tinkers, don't worry about this. */ rmin = 1.5; rmax = 2.5; for(i=1;i<=id2;++i) for(j=i+1;j<=id2;++j) { if(galid[i]==galid[j])continue; dx = galarr[i][0] - galarr[j][0]; if(dx>BOX_SIZE/2)dx = BOX_SIZE-dx; if(dx>rmax)continue; dy = galarr[i][1] - galarr[j][1]; if(dy>BOX_SIZE/2)dy = BOX_SIZE-dy; if(dy>rmax)continue; dz = galarr[i][2] - galarr[j][2]; if(dz>BOX_SIZE/2)dz = BOX_SIZE-dz; if(dz>rmax)continue; dr = sqrt(dx*dx+dy*dy+dz*dz); if(dr<rmin || dr>rmax)continue; i1 = galid[i]; j1 = galid[j]; dx = haloarr[i1][0] - haloarr[j1][0]; if(dx>BOX_SIZE/2)dx = BOX_SIZE-dx; dy = haloarr[i1][1] - haloarr[j1][1]; if(dy>BOX_SIZE/2)dy = BOX_SIZE-dy; dz = haloarr[i1][2] - haloarr[j1][2]; if(dz>BOX_SIZE/2)dz = BOX_SIZE-dz; drh = sqrt(dx*dx+dy*dy+dz*dz); rv1 = pow(3*haloarr[i1][6]/(4*DELTA_HALO*PI*RHO_CRIT*OMEGA_M),1.0/3.0); rv2 = pow(3*haloarr[j1][6]/(4*DELTA_HALO*PI*RHO_CRIT*OMEGA_M),1.0/3.0); if(dr < rv1+rv2) { dx = galarr[i][3] - galarr[j][3]; dy = galarr[i][4] - galarr[j][4]; dz = galarr[i][5] - galarr[j][5]; printf("%e %e %e %e %e %e %e\n",dr,rv1+rv2,haloarr[i1][6],haloarr[j1][6],dx,dy,dz); fflush(stdout); } } exit(0); }
void fit_color_samples() { int n,niter,i,j; double *a,**pp,*yy,FTOL=1.0E-3,chi2min,s1,dlogm,m; FILE *fp; char aa[1000]; mcmc_color_minimization(); fprintf(stderr,"\n\nCHI2 MINIMIZATION OF W_P(R_P) COLOR DATA..........\n"); fprintf(stderr, "--------------------------------------------\n\n"); HOD.blue_fraction = 0.5857; /* <- Millenium fraction (sloan);; SDSS fraction -> 0.565; */ HOD.blue_fraction = 0.6555; /* <- Millenium fraction (B-V>0.8) */ HOD.blue_fraction = 0.565; /* SDSS -19,-20 */ HOD.blue_fraction = 0.492; /* SDSS -20,-21 */ HOD.blue_fraction = 0.379; /* SDSS -21 */ wp_color.ON = 1; if(POWELL) FTOL=1.0E-3; else FTOL=1.0E-5; for(n=0,i=1;i<=7;++i) { n+=HOD.free[i]; if(!OUTPUT)continue; printf("wp_min> free[%i] = %d\n",i,HOD.free[i]); } /* The parameters that govern the blue fraction aren't * listed in the HOD.free array, so add them in. * NB: There are four parameters for these two functions, * one of which is fit by the number densities. */ n+=3; if(OUTPUT)printf("wp_min> Number of free parameters: %d\n",n); wp_color_input(); wp.ncf=n; a=dvector(1,n); if(POWELL) pp=dmatrix(1,n,1,n); else pp=dmatrix(1,n+1,1,n); yy=dvector(1,n+1); initial_color_values(a,pp,yy); if(POWELL) { if(OUTPUT)printf("wp_min> starting powell.\n"); powell(a,pp,n,FTOL,&niter,&chi2min,chi2_wp_color); chi2min = chi2_wp_color(a); } else { if(OUTPUT)printf("wp_min> starting amoeba.\n"); amoeba(pp,yy,n,FTOL,chi2_wp_color,&niter); for(i=1;i<=n;++i)a[i]=pp[1][i]; chi2min = chi2_wp_color(a); } s1=qromo(func_galaxy_bias,log(HOD.M_low),log(HOD.M_max),midpnt); GALAXY_BIAS=s1/GALAXY_DENSITY; printf("POWELL %e %e %e ",chi2min,HOD.M_min,HOD.fblue0_cen); if(HOD.pdfc==7) printf("%e ",HOD.M_cen_max); for(i=1;i<=n;++i)printf("%e ",a[i]); printf(" %f\n",GALAXY_BIAS); /* Output the fit and the HOD curve. */ sprintf(aa,"%s.fit",Task.root_filename); fp=fopen(aa,"w"); fprintf(fp,"%e %e %e ",chi2min,HOD.M_min,HOD.fblue0_cen); if(HOD.pdfc==7) fprintf(fp,"%e ",HOD.M_cen_max); for(i=1;i<=n;++i)fprintf(fp,"%e ",a[i]); fprintf(fp," %f\n",GALAXY_BIAS); fclose(fp); sprintf(aa,"%s.HOD",Task.root_filename); fp=fopen(aa,"w"); dlogm=(log(HOD.M_max)-log(HOD.M_low))/99; for(i=1;i<=100;++i) { m=exp((i-1)*dlogm)*HOD.M_low; fprintf(fp,"%e %e %e %e\n",m,N_cen(m),N_sat(m),N_avg(m)); } fclose(fp); }
void output_drg_lf(int iout) { FILE *fp; char fname[100]; int i,j,k,nlogm=400,imag,nmag,jmin; double dlogm,halocnt[401],magcnt[200],m,msub,n1,mlow,dmag,magmin,nsub; double *hmass, *mag, *yy; dmag = 0.2; magmin = -18.0; nmag = 5/dmag; for(i=0;i<200;++i) magcnt[i] = 0; hmass = dvector(1,nlogm); mag = dvector(1,nlogm); yy = dvector(1,nlogm); mlow = HOD.M_low/5.0; for(i=1;i<=nlogm;++i) halocnt[i] = 0; // go through the mass function to find TOTAL number of halos dlogm = log(HOD.M_max/mlow)/nlogm; for(i=1;i<=nlogm;++i) { m = exp(dlogm*(i-0.5))*mlow; halocnt[i] += dndM_interp(m)*m*dlogm; for(j=1;j<=nlogm;++j) { msub = exp(dlogm*(j-0.5))*mlow; if(msub>m/2)continue; // no subhalo greater than half total mass // charlie's fit (with extra factor of log(10)?) halocnt[j] += 0.02*log(10)*pow(msub/m,-0.9)*exp(-msub/(2*m))*dlogm*dndM_interp(m)*m*dlogm; // gao et al 2004-- gives ~4% at M=1e12 z=0. //halocnt[i] += 6.3e-4*pow(msub,-1.9)*m*msub*dlogm*dndM_interp(m)*m*dlogm; } } // make it cumulative // and associate a magnitude at every mass. mag[nlogm] = mag_at_fixed_ngal(halocnt[nlogm]); hmass[nlogm] = exp(dlogm*(nlogm-0.5)*mlow); for(i=nlogm-1;i>=1;--i) { halocnt[i] += halocnt[i+1]; mag[i] = mag_at_fixed_ngal(halocnt[i]); hmass[i] = exp(dlogm*(i-0.5))*mlow; //printf("CNT %e %e %e\n",hmass[i],halocnt[i],lf_number_density(-27.9,-2.0)); } // prepare for spline interpolation spline(hmass,mag,nlogm,1.0E+30,1.0E+30,yy); // now go through HOD and create the DRG luminosity function for(i=1;i<=nlogm-1;++i) { m = hmass[i]; if(m<HOD.M_low)continue; imag = (magmin-mag[i])/dmag+1; //printf("%e\n",magcnt[i]); magcnt[imag] += N_cen(m)*dndM_interp(m)*dlogm*m; //printf("%e %f %d %e %e %e %e %e\n",m,mag[i],imag,magcnt[imag],N_cen(m),m,dlogm,dndM_interp(m)); //continue; // find the minimum subhalo mass nsub = 0; for(j=nlogm;j>=1;--j) { msub = hmass[j]; if(msub>m/2)continue; nsub += 0.02*log(10)*pow(msub/m,-0.9)*exp(msub/(2*m))*dlogm; if(nsub>N_sat(m)/HOD.freds) break; } if(j==nlogm)continue; jmin = j; if(jmin>=0) { printf("ERR %d %e %e %e %e %f\n",j,m,msub,nsub,N_sat(m)/HOD.freds,mag[jmin]); } // go through the satellites for(j=jmin;j<=nlogm;++j) { msub = hmass[j]; if(msub>m/2)break; imag = (magmin-mag[j])/dmag+1; magcnt[imag] += HOD.freds*0.02*log(10)*pow(msub/m,-0.9)*exp(msub/(2*m))*dlogm*dndM_interp(m)*m*dlogm; } } // print out the results sprintf(fname,"drg_lf.%d",iout); fp = fopen(fname,"w"); n1 = 0; for(i=0;i<200;++i) { if(-(i-0.5)*dmag+magmin >-21.6)continue; magcnt[i]/=dmag; if(magcnt[i]>0) fprintf(fp,"%f %e\n",-(i-0.5)*dmag + magmin,magcnt[i]); n1 += magcnt[i]*dmag; } printf("TOTAL ngal = %e\n",n1); fclose(fp); }
/* This function is to be passed to qromo to integrate the number density * of satellite galaxies. */ double func_satellite_density(double m) { m=exp(m); return(N_sat(m)*dndM_interp(m)*m); }
/* This function is to be passed to qromo to integrate the number density * of satellite galaxies. */ double func_satfrac(double m) { m=exp(m); return(N_sat(m)*dndM_interp(m)*m); }
double func_satmass(double m) { return N_sat(m) - 1; }
/* If what is needed is the total number of galaxies in a halo. */ double N_avg(double m) { return(N_cen(m)+N_sat(m)); }
double generate_satellite_galaxy_tab(double mass, double mstarlow, int reset) { static double **mgal, **ngal, **zz, prev_mass = -1, *mhalo, *totsat, *zzh, dlogmh, masslimit; static int n=0, nm, *nn; int i, j, k, iter=0; double mlo, mhi, dm, ngaltot, pmax, p, x, m, mhlo, mhhi, p1, w; if(reset) { if(!n) { n = 100; nm = 100; mgal = dmatrix(1,nm,1,n); ngal = dmatrix(1,nm,1,n); zz = dmatrix(1,nm,1,n); mhalo = dvector(1,nm); totsat = dvector(1,nm); zzh = dvector(1,nm); nn = ivector(1,nm); } mlo = mstarlow; mhi = 12.0; dm = (mhi-mlo)/(n-1); mhlo = 1.0E+11; mhhi = 1.0E+16; dlogmh = log(mhhi/mhlo)/(nm-1); masslimit = mhlo; for(j=1;j<=nm;++j) { ngaltot = 0; pmax = 0; mhalo[j] = exp(dlogmh*(j-1))*mhlo; nn[j] = n; for(i=1;i<=n;++i) { mgal[j][i] = (i-1)*dm + mlo; set_up_hod_for_shmr(pow(10.0,mgal[j][i]-dm/2),pow(10.0,mgal[j][i]+dm/2),wpl.a); p = N_sat(mhalo[j]); if(i==1 && p<=0)masslimit = mhalo[j]; //if(BLUE_FLAG==0) //printf("BUH %d %d %e %e %e\n",j,i,mhalo[j],mgal[j][i],p); if(p<1.0E-10) { nn[j] = i-1; break; } ngal[j][i] = p; if(ngal[j][i]>pmax)pmax=ngal[j][i]; } set_up_hod_for_shmr(pow(10.0,mstarlow),pow(10.0,12.0),wpl.a); totsat[j] = log(N_sat(mhalo[j])); //if(BLUE_FLAG==0) //printf("BUHTOT %e\n",N_sat(mhalo[j])); mhalo[j] = log(mhalo[j]); //printf("> %e %e\n",mhalo[j]/log(10),totsat[j]/log(10)); for(i=1;i<=nn[j];++i) ngal[j][i] /= pmax; for(i=1;i<=nn[j];++i) ngal[j][i] = log(ngal[j][i]); spline(mgal[j],ngal[j],nn[j],1.0E+30,1.0E+30,zz[j]); } spline(mhalo, totsat,nm,1.0E+30,1.0+30,zzh); return; } //if mass<0 just return total number of satellites if(mass<0) { if(-mass<masslimit)return 0; splint(mhalo, totsat, zzh, nm,log(-mass),&p); return exp(p); } // find the mass bins the bracket the input mass mass = log(mass); for(j=2;j<=nm;++j) if(mhalo[j]>mass)break; if(j>nm)j=nm; w = (mass-mhalo[j-1])/dlogmh; // randomly grab while(1) { iter++; m = drand48()*(12.0-mstarlow)+mstarlow; splint(mgal[j],ngal[j],zz[j],nn[j],m,&p); p = exp(p); if(m>mgal[j][nn[j]])p = 0; splint(mgal[j-1],ngal[j-1],zz[j-1],nn[j-1],m,&p1); p1 = exp(p1); if(m>mgal[j-1][nn[j-1]])p1 = 0; p = p*w + p1*(1-w); if(iter>10000){ printf("BOO %e %e %e %e %e\n",exp(mass),m,p,w,p1); m = 9.0; break; } if(drand48()<p)break; } return m; }
double generate_satellite_galaxy(double mass, double mstarlow) { static double *mgal, *ngal, *zz, prev_mass = -1; static int n=0; int i, j, k, iter=0; double mlo, mhi, dm, ngaltot, pmax, p, x, m; if(mass != prev_mass) { if(mass<0)mass = -mass; prev_mass = mass; if(!n) { n = 20; mgal = dvector(1,n); ngal = dvector(1,n); zz = dvector(1,n); } mlo = mstarlow; mhi = 12.0; dm = (mhi-mlo)/(n-1); ngaltot = 0; pmax = 0; for(i=1;i<=n;++i) { mgal[i] = (i-1)*dm + mlo; set_up_hod_for_shmr(pow(10.0,mgal[i]-dm/2),pow(10.0,mgal[i]+dm/2),wpl.a); ngal[i] = N_sat(mass); //printf("%e %f %f\n",mass,mgal[i],ngal[i]); if(ngal[i]>pmax)pmax=ngal[i]; } set_up_hod_for_shmr(pow(10.0,mstarlow),pow(10.0,12.0),wpl.a); ngaltot = N_sat(mass); for(i=1;i<=n;++i) ngal[i] /= pmax; spline(mgal,ngal,n,1.0E+30,1.0E+30,zz); // if reset call, send total number of satellites return ngaltot; } // test while(iter<1000) { iter++; m = drand48()*(12.0-mstarlow)+mstarlow; splint(mgal,ngal,zz,n,m,&p); printf("TEST2 %f %e\n",m,p); } exit(0); // randomly grab while(1) { m = drand48()*(12.0-mstarlow)+mstarlow; splint(mgal,ngal,zz,n,m,&p); if(drand48()<p)break; } return m; }