double xi_linear_interp(double r) { static int flag=0,prev_cosmo=0; static double *x,*y,*y2; int n=100,i; double a,rlo=0.1,rhi=150,dlogr,klo; double xi_linear_int(); if(!flag || RESET_COSMOLOGY!=prev_cosmo) { if(!flag) { x=dvector(1,n); y=dvector(1,n); y2=dvector(1,n); } flag=1; dlogr = (log(rhi)-log(rlo))/(n-1); klo = 0; if(BOX_SIZE>0)klo = 1/BOX_SIZE; for(i=1;i<=n;++i) { r_g4 = x[i] = exp((i-1)*dlogr)*rlo; y[i] = qromo(xi_linear_int,klo,1.0/r_g4,midpnt)+ qromo(xi_linear_int,1.0/r_g4,1.0E+3,midpnt); } check_for_smoothness(x,y,n,0); spline(x,y,n,2.0E+30,2.0E+30,y2); prev_cosmo=RESET_COSMOLOGY; } splint(x,y,y2,n,r,&a); return(a); }
void mass2number() { int i,j,k,n; double s1,s2,mbar; char fname[1000]; FILE *fp; sprintf(fname,"%s.M2N",Task.root_filename); fp = fopen(fname,"w"); for(i=2;i<=10;++i) { g31_number = i; s1 = qromo(func1_m2n,log(HOD.M_low),log(HOD.M_max),midpnt); s2 = qromo(func2_m2n,log(HOD.M_low),log(HOD.M_max),midpnt); mbar = s1/s2; fprintf(fp,"%d %e %e\n",i,mbar,mbar/i); } for(i=20;i<=150;i+=10) { g31_number = i; s1 = qromo(func1_m2n,log(HOD.M_low),log(HOD.M_max),midpnt); s2 = qromo(func2_m2n,log(HOD.M_low),log(HOD.M_max),midpnt); mbar = s1/s2; fprintf(fp,"%d %e %e\n",i,mbar,mbar/i); } fclose(fp); }
double func_findmshift2(double x) { double m; HOD.mass_shift = x; printf("C %f %e %e\n",x,qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt),NGAL_DRG); return qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt) - NGAL_DRG; }
double func_findmshift(double x) { double m; HOD.mass_shift = x; m = qromo(func_drg1,log(HOD.M_low),log(HOD.M_max),midpnt)/ qromo(func_drg1a,log(HOD.M_low),log(HOD.M_max),midpnt)/MALL; //printf("B %f %f\n",m,MSHIFT); return m - MSHIFT; }
/* Same as above, but no instead of using the linear * theory power spectrum, use the non-linear P(k) */ double nonlinear_sigmac(double rad) { double xk1,s1=1,s2=0,sigma0; rad1=rad; xk1 = 1./(2.*PI*mabs(rad)); s1=qromo(func_sigmac_nl,0.0,xk1,midpnt); s2=qromo(func_sigmac_nl,xk1,1.e20,midinf); sigma0=sqrt((s1+s2)); return sigma0; }
/* Here we calculate the one-halo real space term * logarithmically spaced in r. The minimum value of r = 0.01 Mpc/h. The maximum * value of r is set to be approximately twice the virial radius of M_max. * * Only halos with virial radii greater than 1/2 the separation * contribute to the 1-halo term. * Terminate integrations when r>2*R_vir(M_max). */ void calc_real_space_one_halo(double *r, double *xi, int n) { static int ncnt=0; double fac,s1,rhi=1,rlo=-2,dr,mlo,x1,x2; int i,j; FILE *fp; char fname[100]; ncnt++; rlo=log(0.01); rhi=log(1.9*pow(3*HOD.M_max/(4*PI*DELTA_HALO*RHO_CRIT*OMEGA_M),1.0/3.0)); dr=(rhi-rlo)/(n-1); if(OUTPUT>1) printf("calc_one_halo> starting...\n"); if(!XCORR) GALAXY_DENSITY2 = GALAXY_DENSITY; if(OUTPUT>2) { sprintf(fname,"%s.1halo",Task.root_filename); fp = fopen(fname,"w"); } for(i=1;i<=n;++i) { r_g2=r[i]=exp((i-1)*dr + rlo); fac=1.0/(2*PI*r_g2*r_g2*GALAXY_DENSITY*GALAXY_DENSITY2); mlo = 4./3.*PI*RHO_CRIT*DELTA_HALO*OMEGA_M*pow(r[i]*.5,3.0); if(mlo<HOD.M_low) mlo = HOD.M_low; if(XCORR) s1=fac*qromo(func1_xcorr,log(mlo),log(HOD.M_max),midpnt)*0.5; else s1=fac*qromo(func1,log(mlo),log(HOD.M_max),midpnt); xi[i]=s1; if(OUTPUT>1) printf("calc_one_halo> %f %e %e\n",r[i],s1,fac); continue; x1 = fac*qromo(func1satsat,log(mlo),log(HOD.M_max),midpnt); x2=0; if(r_g2<exp(rhi)/2) x2 = fac*qromo(func1cs,log(mlo),log(HOD.M_max),midpnt); if(OUTPUT>1)printf("MOO%d %f %e %e\n",ncnt,r_g2,x1,x2); if(OUTPUT>2)fprintf(fp,"%f %e %e\n",r_g2,x1,x2); } if(OUTPUT>2)fclose(fp); }
double sigmac(double rad) { double xk1,s1=1,s2=0,sigma0; rad1=rad; xk1 = 1./(2.*PI*mabs(rad)); s1=qromo(func_sigmac,0.0,xk1,midpnt); s2=qromo(func_sigmac,xk1,1.e20,midinf); sigma0=sqrt((s1+s2)*(4*PI)/pow(2*PI,3.0)); return sigma0; }
/* Calculates and tabulates the non-linear matter correlation function. * Since this is only really needed for the scale-dependence of the bias, * which is basically 1 at scales r>~8 Mpc/h, I won't calculate this much * past that value. */ double xi_interp(double r) { static int flag=0,prev_cosmo=0; static double *x,*y,*y2; int n=30,i,j; double a,xi_int(),rhi=95,rlo=0.1,dlogr,klo,s1,s2,tolerance=1.0e-6; if(!flag || RESET_COSMOLOGY!=prev_cosmo) { // discrete_fourier_transform(); if(!flag) { x=dvector(1,n); y=dvector(1,n); y2=dvector(1,n); } flag=1; dlogr = (log(rhi)-log(rlo))/(n-1); for(i=1;i<=n;++i) { klo = 0; if(BOX_SIZE>0)klo = TWOPI/BOX_SIZE; r_g4 = x[i] = exp((i-1)*dlogr)*rlo; j=1; s1 = qromo(xi_int,klo,j/r_g4,midpnt); s2 = s1; klo = j/r_g4; while(mabs(s1)>tolerance*mabs(s2)) { j+=16; s1 = qromo(xi_int,klo,j/r_g4,midpnt); s2 += s1; klo = j/r_g4; } y[i]=s2; //fprintf(stderr,"%f %f\n",r_g4,s2); } check_for_smoothness(x,y,n,0); spline(x,y,n,2.0E+30,2.0E+30,y2); prev_cosmo=RESET_COSMOLOGY; } splint(x,y,y2,n,r,&a); return(a); }
/* This function is sent to zbrent to determine what M1 is based * on the number density. Both M_min and M_low have already been specified. */ double fixfunc2(double m) { double n; HOD.M1=m=pow(10.0,m); n=qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt); return(GALAXY_DENSITY-n); }
double func_find_mone2(double m) { double fsat; HOD.M1=exp(m); set_HOD_params(); fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; return fsat-fsat_g1; }
//*************************************************TERM 3************************************************* dcomplex Zetafunc::term3full(const double q2){ double resultre=0.,resultim=0.; #pragma omp parallel { double tmp,r,theta,phi,wdprod,ghatwnorm; dcomplex result(0.,0.),tmpcomp1,tmpcomp2,tmpcomp3; threevec<double> wvec,ghatw,wpar,wperp; Zetafuncint integrand2; #pragma omp parallel for reduction(+:resultre) reduction(+:resultim) for(int z=-MAXRUN; z<=MAXRUN; z++){ for(int y=-MAXRUN; y<=MAXRUN; y++){ for(int x=-MAXRUN; x<=MAXRUN; x++){ if( (x==0) && (y==0) && (z==0) ) continue; //exclude zero! wvec(static_cast<double>(x),static_cast<double>(y),static_cast<double>(z)); //compute scalar product and orthogonal projection: if(!is_zeroboost){ orthogonal_projection(wvec,boost,wpar,wperp); ghatw=gamma*wpar+wperp; } else{ ghatw=wvec; } ghatwnorm=ghatw.norm(); //solve the integral: integrand2.set(q2,ghatwnorm,l); Midpnt<TFunctor> int2(integrand2,0.,lambda); tmp=qromo(int2); if(l!=0) tmp*=::std::pow(ghatwnorm,static_cast<double>(l)); //compute the complex parts: ghatw.get_spherical_coordinates(r,theta,phi); tmpcomp1=spherical_harmonicy(l,m,theta,phi); //exip(-ipi w*d)-term if(!is_zeroboost){ wdprod=wvec*boost; tmpcomp2(cos(pimath*wdprod),-sin(pimath*wdprod)); } else tmpcomp2(1.,0.); tmpcomp3=tmpcomp1*tmpcomp2*tmp; //result+=tmpcomp3; resultre+=tmpcomp3.re(); resultim+=tmpcomp3.im(); } } } } dcomplex result(resultre,resultim); result*=gamma*::std::pow(pimath,static_cast<double>(1.5+l)); return result; }
/* Calculate the chi2 for the M2N data. * ---------------------------------------------------------------------------- * * For each Ngals, calculate the mean halo mass and the mean number of galaxies. * * M_bar(N_gals) = \int dM dn/dM P(N_gals|Mass) Mass * divided by \int dM dn/dM P(N_gals|Mass) * * N_bar(N_gals) = \int dM dn/dM P(N_gals|Mass) N_true(Mass) * divided by \int dM dn/dM P(N_gals|Mass) * * Where P(N_true|Mass) is the Poisson distribution for satellites + nearest int for centrals. * * Where P(N_gals|N_true) is something we have yet to determine. * * For bins in Ngals that are wider than one, do a weighted sum between all the * values of Ngals. * * How many systems do you find at a fixed Ngals? * * n(N_gals) = \int dM dn/dM P(N_gals|Mass) * */ double chi2_m2n() { int i,j,k,n; double mbar, nbar, nsys, m2n, chi2, x1, vol; static int iter=0; chi2 = 0; printf("%f %f %f\n",alpha_rozo, B_rozo, sig_rozo); // calculate the number densities of the clusters for the current cosmology /* vol = comoving_volume(0.1,0.3); for(i=1;i<=M2N.counts_nbins;++i) M2N.ndens_N200[i] = M2N.counts_N200[i]/vol; */ for(i=1;i<=M2N.ndata;++i) { mbar = nbar = nsys = x1 = 0; M2N.current_bin = i; for(j=M2N.Ngals_lo[i];j<=M2N.Ngals_hi[i];++j) { M2N.current_Ngals = j; mbar += qromo(m2n_func1,log(HOD.M_min),log(HOD.M_max),midpnt); nbar += qromo(m2n_func2,log(HOD.M_min),log(HOD.M_max),midpnt); nsys += qromo(m2n_func3,log(HOD.M_min),log(HOD.M_max),midpnt); x1 += qromo(m2n_func1a,log(HOD.M_min),log(HOD.M_max),midpnt); } m2n = mbar/nbar; M2N.model_m2n[i] = m2n; M2N.model_mass[i] = mbar/nsys; x1 /= nsys; chi2 += (M2N.m2n[i] - m2n)*(M2N.m2n[i] - m2n)/(M2N.err[i]*M2N.err[i]); if(OUTPUT) printf("CHIM2N%d %d %e %e %e %e %e %e %e %e\n",iter,i,M2N.m2n[i],M2N.err[i],m2n,M2N.mass[i],M2N.model_mass[i],chi2,x1,exp(-0.12)*pow(M2N.Ngals_lo[i]/40.,1.15)*4e14*HUBBLE); //exit(0); } iter++; return chi2; }
/* Calculate and output the galaxy-galaxy clustering signal * in the user-defined stellar mass bins. */ void shmr_clustering() { int i,j; double mlo, mhi, x1, x2, x3, x4, r, dr; FILE *fp; char fname[1000]; if(COLOR)return shmr_color_clustering(); wpl.reset_inversion = 1; for(i=1;i<=wpl.nsamples;++i) { sprintf(fname,"%s.clustering_bin%d",Task.root_filename,i); fp = fopen(fname,"w"); // make a header: fprintf(fp,"# %e %e\n",wpl.mstar_wplo[0][i],wpl.mstar_wphi[0][i]); fflush(fp); mlo = pow(10.0,wpl.mstar_wplo[0][i]); mhi = pow(10.0,wpl.mstar_wphi[0][i]); set_up_hod_for_shmr(mlo,mhi,wpl.a); GALAXY_DENSITY = qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt); GALAXY_BIAS = qromo(func_galaxy_bias,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; fprintf(stdout,"%e %e\n",GALAXY_DENSITY,GALAXY_BIAS); RESET_FLAG_1H = RESET_FLAG_2H = 1; RESET_KAISER++; dr=(log(70.0)-log(0.01))/49.0; for(j=0;j<50;++j) { r = exp(j*dr+log(0.01)); x1 = one_halo_real_space(r); x2 = two_halo_real_space(r); x3 = projected_xi(r); x4 = projected_xi_1halo(r); fprintf(fp,"%f %e %e %e %e %e\n",r,x1,x2,x1+x2,x3,x4); fflush(fp); } fclose(fp); } }
void zspace_minimization(char *fname) { int n,niter,i,j; double *a,**pp,*yy,FTOL=1.0E-3,chi2min,qromo(),midpnt(),s1; fprintf(stderr,"\n\nCHI2 MINIMIZATION OF W_P(R_P)+xi(s,p) DATA..........\n"); fprintf(stderr, "-----------------------------------------------------\n\n"); OUTPUT=0; wp_input(); Work.imodel=2; /* Find the number of free parameters in the minimization * for the real-space correlation function. */ for(n=0,i=1;i<=100;++i) { n+=HOD.free[i]; if(OUTPUT) printf("zspace_min> free[%i] = %d\n",i,HOD.free[i]); } 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_zspace_values(a,pp,yy); if(POWELL) { powell(a,pp,n,FTOL,&niter,&chi2min,func_chi2); chi2min = func_chi2(a); } else amoeba(pp,yy,n,FTOL,func_chi2,&niter); s1=qromo(func_galaxy_bias,log(HOD.M_low),log(HOD.M_max),midpnt); GALAXY_BIAS=s1/GALAXY_DENSITY; if(!ThisTask) { printf("ZSPACEMIN %e %e ",chi2min,HOD.M_min); for(i=1;i<=n;++i)printf("%e ",a[i]); printf(" %f\n",GALAXY_BIAS); } output_parameter_file(fname); }
double Zetafunc::term3spherical(const double q2){ double result=0.,integral; threevec<double> wvec; Zetafuncint integrand2; //double wrapper since lower integration boundary is a singularity: integrand2.set(q2,0.,0,is_improved); Midpnt<TFunctor> int2(integrand2,0.,lambda); integral=qromo(int2); //multiply integral with stuff: result=pimath*integral/2.; return result; }
/* This is the equation for zbrent to solve. What value * of M_min gives the correct galaxy density? * For HODs that have sharp central cutoffs, use M_min as the lower * limit of the integration. For soft cutoffs, first find M_low. */ double fixfunc1(double m) { double n,mlo; HOD.M_min=m=pow(10.0,m); HOD.M_low=0; if(SOFT_CENTRAL_CUTOFF) mlo = (zbrent(func_mlow,log(HOD.M_min)-5*HOD.sigma_logM*2.3,log(HOD.M_min),1.0E-5)); else mlo = log(HOD.M_min); if(exp(mlo)<1.0E7)mlo=log(1.0E+7); n=qromo(func_galaxy_density,mlo,log(HOD.M_max),midpnt); // fprintf(stderr,"MMIN %e %e %e %e %e %e\n",m,exp(mlo),n,GALAXY_DENSITY,N_sat(2*m),N_cen(m)); return(GALAXY_DENSITY-n); }
double lf_number_density(double mag1, double mag2) { return qromo(lf_all,mag1,mag2,midpnt); }
void drg_model() { int k,i,n=40,j,i1,i2,ngal; double dlogr,r,mass,xk,pnorm,psp,rm,sig,t0,t1,pnorm1,mp,mlo,mhi,dr, slo,shi,dsdM,rlo,rhi,dlogm,f1,f2,fac,cvir,rvir,rs,r1,r2,mtot=0,m, chi2lf,chi2w,x0,mlow; FILE *fp,*fp2; float x1,x2,x3,x4; char aa[1000],fname[1000],**argv; float *xx,*yy,*zz,*vx,*vy,*vz; float *wdata,*rdata,*edata,*lfdata,*lferr,*lfmag,*wmodel; int magcnt[100]; int nwdata, nlf, j1, ngrid = 10, ingal; double tmin, tmax, dtheta, theta, delta_mag, maglo; /* FITTING RIK'S DATA */ rlo = log(1.0); rhi = log(1000); dtheta = (rhi-rlo)/(19); for(i=0;i<20;++i) { theta = exp(rlo+i*dtheta); printf("ALL %e %e\n",theta,wtheta(theta)); fflush(stdout); } sprintf(Task.root_filename,"all"); tasks(ARGC,ARGV); RESET_FLAG_1H++; RESET_FLAG_2H++; GALAXY_DENSITY = 4.9e-4; NGAL_DRG = 4.9e-4; HOD.pdfc = 10; // set up centrals for RED HOD.freds = 0.3; // for RED HOD.fredc = 1; HOD.mass_shift = zbrent(func_findmshift2,0.0,2.434682,1.0E-4); //spaced in fcen0 fprintf(stdout,"fsat = %.2f fcen = %.2f mu = %.2f ngal= %e %e\n",HOD.freds,HOD.fredc, HOD.mass_shift,GALAXY_DENSITY,qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt)); for(i=0;i<20;++i) { theta = exp(rlo+i*dtheta); printf("RED %e %e\n",theta,wtheta(theta)); fflush(stdout); } sprintf(Task.root_filename,"red"); tasks(ARGC,ARGV); RESET_FLAG_1H++; RESET_FLAG_2H++; GALAXY_DENSITY = 1.9e-3; HOD.pdfc = 11; // set up centrals for BLUE HOD.freds = 1 - HOD.freds; // for BLUE printf("ngal = %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt)); for(i=0;i<20;++i) { theta = exp(rlo+i*dtheta); printf("BLUE %e %e\n",theta,wtheta(theta)); fflush(stdout); } sprintf(Task.root_filename,"blue"); tasks(ARGC,ARGV); exit(0); BOX_SIZE = 160.; fp = openfile("wtheta_corrected.data"); nwdata = filesize(fp); wdata = vector(1,nwdata); rdata = vector(1,nwdata); edata = vector(1,nwdata); wmodel = vector(1,nwdata); for(i=1;i<=nwdata;++i) fscanf(fp,"%f %f %f",&rdata[i],&wdata[i],&edata[i]); fclose(fp); fp = openfile("LF_DRG.data"); nlf = filesize(fp); lfmag = vector(1,nlf); lfdata = vector(1,nlf); lferr = vector(1,nlf); for(i=1;i<=nlf;++i) fscanf(fp,"%f %f %f",&lfmag[i],&lfdata[i],&lferr[i]); fclose(fp); delta_mag = 0.45; maglo = -23.86-delta_mag/2; MSTAR = mstar(); tmin = log(1.0); tmax = log(1000); dtheta = (tmax-tmin)/(19); //HOD.M_min *= 1.5; //HOD.M_low *= 1.5; // get mean mass of centrals (ALL) MALL = qromo(func_drg1,log(HOD.M_low),log(HOD.M_max),midpnt)/ qromo(func_drg1a,log(HOD.M_low),log(HOD.M_max),midpnt); //new model HOD.mass_shift = 0.5; HOD.pdfc = 10; //HOD.shift_alpha = 1; mlow = 1; mhi = 2; dlogm = log(mhi/mlow)/ngrid; //analytic_sham(); /********************************** */ // let's do the prediction for the blue galaxies. // best-fit model (alpha=1) fit10 is 12 28 // best-fit model (alpha=1) fit9 is 30 19 j1 = 12; j = 19; // set up HOD as DRGs // for stepping in mshift ERROR_FLAG = 0; MSHIFT = exp((j1-0.5)*dlogm)*mlow; HOD.fredc = 1; HOD.mass_shift = zbrent(func_findmshift,0.0,10.0,1.0E-4); HOD.freds = (j-0.5)/ngrid; HOD.fredc = zbrent(func_findfredc,0.0,1.0,1.0E-4); // for stepping in fsat0 HOD.fredc = (j1-0.5)/ngrid; HOD.freds = (j-0.5)/ngrid; HOD.mass_shift = zbrent(func_findmshift2,0.0,1.434682,1.0E-4); //spaced in fcen0 // 9panel c2 5.9 //CHI 993 0.978682 0.660204 0.181224 2.119014e-01 8.793237e+00 1.237535 6.664499e-04 HOD.mass_shift = 0.181; HOD.fredc = 0.66; HOD.freds = 0.3;//0.979; HOD.freds = 1 - HOD.freds; // for NON-DRGs HOD.pdfc = 11; // set up centrals as 1-f(DRG) //HOD.pdfc = 7; // square-well blues at low-mass end //HOD.M_min = 7e11; //HOD.M_cen_max = 1.5e12; //HOD.M_low = 7e11; GALAXY_DENSITY = qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt); fprintf(stdout,"fsat = %.2f fcen = %.2f ngal= %e\n",HOD.freds,HOD.fredc, GALAXY_DENSITY); RESET_FLAG_1H++; RESET_FLAG_2H++; for(i=0;i<20;++i) { theta = exp(tmin+i*dtheta); fprintf(stdout,"WTH %e %e\n",theta,wtheta(theta)); } sprintf(Task.root_filename,"BX"); //tasks(2,argv); exit(0); /* ****************************************/ if(ARGC>3) ingal = atoi(ARGV[3]); else ingal = 1; NGAL_DRG = 6.5e-4;//*(1+(ingal-5)/2.5*0.13); // do an outer loop of the mass shifts for(j1=1;j1<=ngrid;++j1) { // go in steps of mean mass shift from 1 to 2. MSHIFT = exp((j1-0.5)*dlogm)*mlow; HOD.fredc = 1; HOD.mass_shift = zbrent(func_findmshift,0.0,10.0,1.0E-4); //HOD.mass_shift = 1.116703; //figure 3 // doing equally spaced in fcen0 //HOD.fredc = (j1-0.5)/ngrid; for(j=1;j<=ngrid;++j) { ERROR_FLAG = 0; HOD.freds = (j-0.5)/ngrid; HOD.fredc = zbrent(func_findfredc,0.0,1.0,1.0E-4); if(ERROR_FLAG) HOD.fredc = 1.0; ERROR_FLAG = 0; /* HOD.mass_shift = zbrent(func_findmshift2,0.0,1.434682,1.0E-4); //spaced in fcen0 if(ERROR_FLAG) { chi2lf = chi2w = 1.0e6; printf("CHI %d %d %f %f %f %e %e %f\n",j1,j,HOD.freds,HOD.fredc,HOD.mass_shift,chi2lf,chi2w,MSHIFT); fflush(stdout); continue; } */ //best fit model from chains (wth+n only) //1.648589e-01 7.732068e-01 9.796977e-01 MSHIFT = pow(10.0,0.164); HOD.freds = 0.7732; HOD.fredc = 0.977; HOD.mass_shift = zbrent(func_findmshift,0.0,10.0,1.0E-4); // third column 7.10 //CHI 7 10 0.950000 1.000000 0.661607 9.897945e+00 1.109673e+01 1.569168 6.500000e-04 5.905374e-04 HOD.mass_shift = 0.6616; HOD.fredc = 1.00; HOD.freds = 0.95; j1 = 7; j = 10; // 9panel c2 5.9 //CHI 993 0.978682 0.660204 0.181224 2.119014e-01 8.793237e+00 1.237535 6.664499e-04 j1 = 5; j = 9; HOD.mass_shift = 0.181; HOD.fredc = 0.66; HOD.freds = 0.979; //for 9panel c1 1.1 j1 = 1; j = 1; HOD.mass_shift = 0; HOD.fredc = NGAL_DRG/GALAXY_DENSITY; HOD.freds = NGAL_DRG/GALAXY_DENSITY; //best-fit model without LF HOD.mass_shift = 0.48; HOD.fredc = 0.99; HOD.freds = 0.69; //best-fit model with LF (bottom row of 6panel) //8.234815e-02 9.011035e-01 6.467542e-01 j1 = 7; j = 10; HOD.mass_shift = 1.505979e-01 ; HOD.fredc = 6.467542e-01 ; HOD.freds = 9.011035e-01 ; GALAXY_DENSITY = qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt); fprintf(stdout,"fsat = %.1f fcen = %f ngal= %e\n",HOD.freds,HOD.fredc, GALAXY_DENSITY); RESET_FLAG_1H++; RESET_FLAG_2H++; /* for(i=0;i<20;++i) { theta = exp(tmin+i*dtheta); fprintf(stdout,"WTH %e %e\n",theta,wtheta(theta)); } exit(0); */ //populate_simulation(); //exit(0); // get qudari model points //fp = openfile("q8m.dat"); // calculate the chi^2 for the wtheta values. chi2w = 0; for(i=1;i<=nwdata;++i) { x0 = wtheta(rdata[i]); wmodel[i] = x0; //q8 model //fscanf(fp,"%f %f",&x1,&wmodel[i]); //x0 = wmodel[i]; printf("XX %e %e %e %e\n",rdata[i],wdata[i],x0,edata[i]); chi2w += (wdata[i]-x0)*(wdata[i]-x0)/(edata[i]*edata[i]); } //fclose(fp); fmuh(chi2w); if(USE_COVAR) chi2w = chi2wtheta_covar(wdata,wmodel); fmuh(chi2w); //exit(0); sprintf(fname,"wth_mshift_%d.%d",j1,j); fp = fopen(fname,"w"); for(i=0;i<20;++i) { theta = exp(tmin+i*dtheta); fprintf(fp,"%e %e\n",theta,wtheta(theta)); } fclose(fp); //continue; // do the real-space clustering and HOD sprintf(Task.root_filename,"mshift_%d.%d",j1,j); tasks(2,argv); // now make the luminosity function for this model //output_drg_lf(j); sprintf(fname,"sham ../../SHAM/halosub_0.284 hod_mshift_%d.%d %f %f %f %f > gal_mshift_%d.%d",j1,j,HOD.freds,HOD.fredc,HOD.mass_shift,GALAXY_DENSITY,j1,j); //sprintf(fname,"sham ../SHAM_120/halosub_0.3323.dat hod_mshift_%.1f.%d %f %f %f > gal_mshift_%.1f.%d",HOD.mass_shift,j,HOD.freds,HOD.fredc,HOD.mass_shift,HOD.mass_shift,j); fprintf(stderr,"[%s]\n",fname); system(fname); // calculate the clustering of this drg sample sprintf(fname,"covar3 0.1 15 12 160 0 160 1 gal_mshift_%d.%d a 0 1 auto > xi.mshift_%d.%d",j1,j,j1,j); //fprintf(stderr,"[%s]\n",fname); //system(fname); // calculate the luminosity function sprintf(fname,"gal_mshift_%d.%d",j1,j); fp = openfile(fname); n = filesize(fp); for(i=1;i<=nlf;++i) magcnt[i] = 0; for(i=1;i<=n;++i) { for(k=1;k<=10;++k) fscanf(fp,"%f",&x1);// printf("%e\n",x1); } k = (x1-maglo)/delta_mag + 1; if(k>=1 && k<=nlf)magcnt[k]+=1; //printf("%d %d %f %f %f\n",i,k,x1,maglo,delta_mag); fscanf(fp,"%f",&x1); } fclose(fp); // calculate the chi^2 for the luminosity function chi2lf = 0; for(i=1;i<=nlf;++i) { if(i==nlf) x0 = log10(magcnt[i]/pow(BOX_SIZE/0.7,3.0)/delta_mag); else x0 = log10(magcnt[i]/pow(BOX_SIZE/0.7,3.0)/delta_mag); printf("LF %d %d %f %f %f\n",j1,j,x0,lfdata[i],lferr[i]); chi2lf += (x0 - lfdata[i])*(x0 - lfdata[i])/(lferr[i]*lferr[i]); } printf("CHI %d %d %f %f %f %e %e %f %e %e\n",j1,j,HOD.freds,HOD.fredc,HOD.mass_shift,chi2lf,chi2w,MSHIFT,NGAL_DRG,GALAXY_DENSITY); fflush(stdout); exit(0); } } exit(0); }
double func_findfredc(double f) { HOD.fredc = f; printf("%e %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt),NGAL_DRG); return qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt) - NGAL_DRG; }
dcomplex Zetafunc::term3noboost(const double q2){ dcomplex result(0.,0.),sphfact,tmpcomp; double tmp,r,theta,phi; //zero term is zero: result=dcomplex(0.,0.); //line terms //x>0, y=z=0 and y>0, x=z=0 and z>0, x=y=0: //note that z->-z is equivalent to theta->pi-theta, similar to this: x->-x yields phi->pi-phi and y->-y is phi->2pi-phi sphfact= spherical_harmonicy(l,m,pimath/2. , 0.); //x>0 sphfact+= spherical_harmonicy(l,m,pimath/2. , pimath); //x<0 sphfact+= spherical_harmonicy(l,m,pimath/2. , pimath/2.); //y>0 sphfact+= spherical_harmonicy(l,m,pimath/2. , 3.*pimath/2.); //y<0 sphfact+= spherical_harmonicy(l,m,0. , 0.); //z>0 sphfact+= spherical_harmonicy(l,m,pimath , 0.); //z<0 double resultre=0., resultim=0.; #pragma omp parallel { Zetafuncint integrand2; #pragma omp parallel for reduction(+:resultre) private(tmp) for(int x=1; x<=MAXRUN; x++){ integrand2.set(q2,x,l); Midpnt<TFunctor> int2(integrand2,0.,lambda); tmp=qromo(int2); if(l!=0) tmp*=::std::pow(x,static_cast<double>(l)); resultre+=tmp; } } result+=resultre*sphfact; //plane-terms resultre=0.; resultim=0.; double* integrals=new double[MAXRUN*MAXRUN]; #pragma omp parallel { Zetafuncint integrand2; //do integrals first: #pragma omp parallel for shared(integrals) private(r,theta,phi,tmp) for(int y=1; y<=MAXRUN; y++){ for(int x=y; x<=MAXRUN; x++){ threevec<int>(x,y,0).get_spherical_coordinates(r,theta,phi); //integral integrand2.set(q2,r,l); Midpnt<TFunctor> int2(integrand2,0.,lambda); tmp=qromo(int2); if(l!=0) tmp*=::std::pow(r,static_cast<double>(l)); //store results: it is symmetrc in x and y: integrals[x-1+MAXRUN*(y-1)]=tmp; integrals[y-1+MAXRUN*(x-1)]=tmp; } } //angular parts //x,y!>0, z=0: //the four ylm account for the possibilities +/+, +/-, -/+, -/-: #pragma omp parallel for reduction(+:resultre) reduction(+:resultim) shared(integrals) private(r,theta,phi,tmp,tmpcomp) for(int y=1; y<=MAXRUN; y++){ for(int x=1; x<=MAXRUN; x++){ threevec<int>(x,y,0).get_spherical_coordinates(r,theta,phi); //z=0: sphfact= spherical_harmonicy(l,m,theta , phi); //x,y>0 sphfact+= spherical_harmonicy(l,m,theta , pimath-phi); //x<0,y>0 sphfact+= spherical_harmonicy(l,m,theta , pimath+phi); //x,y<0 sphfact+= spherical_harmonicy(l,m,theta , 2.*pimath-phi); //x>0,y<0 //result tmpcomp=integrals[y-1+MAXRUN*(x-1)]*sphfact; resultre+=tmpcomp.re(); resultre+=tmpcomp.im(); } } //x,z>0, y=0 and y,z>0, x=0: #pragma omp parallel for reduction(+:resultre) reduction(+:resultim) shared(integrals) private(r,theta,phi,tmp,tmpcomp) for(int z=1; z<=MAXRUN; z++){ for(int x=1; x<=MAXRUN; x++){ threevec<int>(x,0,z).get_spherical_coordinates(r,theta,phi); //y=0: sphfact= spherical_harmonicy(l,m,theta , 0); //x,z>0 sphfact+= spherical_harmonicy(l,m,pimath-theta , 0); //x>0,z<0 sphfact+= spherical_harmonicy(l,m,theta , pimath); //x<0,z>0 sphfact+= spherical_harmonicy(l,m,pimath-theta , pimath); //x,z<0 //x=0: sphfact+= spherical_harmonicy(l,m,theta , pimath/2.); //y,z>0 sphfact+= spherical_harmonicy(l,m,pimath-theta , pimath/2.); //y>0,z<0 sphfact+= spherical_harmonicy(l,m,theta , 3.*pimath/2.); //y<0,z>0 sphfact+= spherical_harmonicy(l,m,pimath-theta , 3.*pimath/2.); //y,z<0 //result: tmpcomp=integrals[z-1+MAXRUN*(x-1)]*sphfact; resultre+=tmpcomp.re(); resultre+=tmpcomp.im(); } } } delete [] integrals; result+=dcomplex(resultre,resultim); //cubic terms: resultre=0.; resultim=0.; integrals=new double[MAXRUN*MAXRUN*MAXRUN]; #pragma omp parallel { //do integrals first Zetafuncint integrand2; #pragma omp parallel for shared(integrals) private(r,theta,phi,tmp) for(int z=1; z<=MAXRUN; z++){ for(int y=z; y<=MAXRUN; y++){ for(int x=y; x<=MAXRUN; x++){ threevec<int>(x,y,z).get_spherical_coordinates(r,theta,phi); //integral integrand2.set(q2,r,l); Midpnt<TFunctor> int2(integrand2,0.,lambda); tmp=qromo(int2); if(l!=0) tmp*=::std::pow(r,static_cast<double>(l)); //store results: it is symmetrc in x and y: integrals[x-1+MAXRUN*((y-1)+MAXRUN*(z-1))]=tmp; integrals[z-1+MAXRUN*((x-1)+MAXRUN*(y-1))]=tmp; integrals[y-1+MAXRUN*((z-1)+MAXRUN*(x-1))]=tmp; } } } #pragma omp parallel for reduction(+:resultre) reduction(+:resultim) shared(integrals) private(r,theta,phi,tmpcomp) for(int z=1; z<=MAXRUN; z++){ for(int y=1; y<=MAXRUN; y++){ for(int x=1; x<=MAXRUN; x++){ threevec<int>(x,y,z).get_spherical_coordinates(r,theta,phi); //compute sphfact: account for all possible orientations sphfact=dcomplex(0.,0.); for(unsigned int thetaa=0; thetaa<2; thetaa++){ sphfact+=spherical_harmonicy(l,m,static_cast<double>(thetaa)*pimath-theta,phi); //x,y>0 sphfact+=spherical_harmonicy(l,m,static_cast<double>(thetaa)*pimath-theta,pimath-phi); //x<0,y>0 sphfact+=spherical_harmonicy(l,m,static_cast<double>(thetaa)*pimath-theta,pimath+phi); //x,y<0 sphfact+=spherical_harmonicy(l,m,static_cast<double>(thetaa)*pimath-theta,2.*pimath-phi); //x>0,y<0 } //compute the result: tmpcomp=integrals[x-1+MAXRUN*((y-1)+MAXRUN*(z-1))]*sphfact; //integral * ylm factor resultre+=tmpcomp.re(); resultim+=tmpcomp.im(); } } } } delete [] integrals; result+=dcomplex(resultre,resultim); //multiply result by factor: result*=::std::pow(pimath,static_cast<double>(1.5+l)); return result; }
/* Calculates the average mass of a halo in the HOD * (weighted by central galaxies). */ double number_weighted_central_mass() { double funcxx2(); return(qromo(funcxx2,log(HOD.M_low),log(HOD.M_max),midpnt)/ qromo(func_central_density,log(HOD.M_low),log(HOD.M_max),midpnt)); }
/****************************************************************** * * HOD.free[] also controls which variables will be held constant/vary * during MCMC minimization. Since this routine will also so z-space * minimization if requested, indices>6 are cosmological. * * i variable * --- -------- * [1] -> M_min * [2] -> M1 * [3] -> alpha * [4] -> M_cut * [5] -> sigmaM * [6] -> CVIR_FAC * [7] -> MaxCen (or M_cen_max) * [8] -> M_sat_break * [9] -> alpha1 * * [10]-> OMEGA_M * [11]-> SIGMA_8 * [12]-> VBIAS * [13]-> VBIAS_C * [14]-> GAMMA * [15]-> SPECTRAL_INDX * [16]-> HUBBLE PARAMETER [used for P(k)] * * [0] -> The galaxy_density will be considered data with errors on it, * and therefore no variable will be fixed by the galaxy density. * */ void m2n_mcmc() { double stepfac=1; double error=1,tolerance=0,**cov1,**tmp,*a,*avg1,chi2,chi2prev, **evect,*eval,*aprev,*atemp,**tmp1,*opar,x1,fsat,**chain,*start_dev,*eval_prev; int n,i,j,k,nrot,niter=0,count=0,imax_chain=100000,NSTEP=50,NSTEP_MAX=10000,convergence=0; long IDUM=-555; int *pcheck,pcnt,ptot=20,firstflag=1,*iweight,total_weight; double t0,tprev,temp,chi2a,chi2b; double delta_halo_rhoc = 200; int icvir; //test_pdf(); // initialize use of SYSTEMATIC ERROR USE_ERRORS = 1; M2N.IDUM = -5555; // fix the value of OEMGA_M fot T(k) M2N.fix_omegam = 0; M2N.constant_omegam = 0.27; opar=dvector(1,100); MCMC=Task.MCMC; pcheck=calloc(ptot,sizeof(int)); /* Since we're at constant halo overdensity wrt RHO_CRIT, * set the overdensity of the halo */ H2OFZ = 0.27*pow(1.25,3.0)+0.73; DELTA_HALO = delta_halo_rhoc/OMEGA_M*(OMEGA_M*1.25*1.25*1.25 + (1-OMEGA_M))/(1.25*1.25*1.25); /* read in the wp data for a single luminosity threshold sample. */ wp_input(); /* read in the M2N data. */ m2n_input(); //input_maxbcg_counts(); Work.imodel=2; Work.chi2=1; srand48(32498793); /* Find the number of free parameters in the minimization * for the real-space correlation function. */ for(n=0,i=1;i<100;++i) { n+=HOD.free[i]; /* if(i>N_HOD_PARAMS && HOD.free[i])MCMC=3;*/ if(OUTPUT) printf("mcmc_min> free[%i] = %d\n",i,HOD.free[i]); } if(USE_ERRORS) n+=3; if(USE_VARIABLE_ROZO) n+=3; wp.ncf=n; /* Find out which free parameter is for CVIR_FAC */ j=0; if(HOD.free[6]) for(i=0;i<6;++i) if(HOD.free[i])j++; icvir=j+1; if(HOD.free[0]) { wp.ngal = GALAXY_DENSITY; wp.ngal_err = 0.1*wp.ngal; FIX_PARAM = 0; } if(OUTPUT) printf("mcmc_min> %d free parameters\n",n); a=dvector(1,n); start_dev=dvector(1,n); aprev=dvector(1,n); atemp=dvector(1,n); cov1=dmatrix(1,n,1,n); avg1=dvector(1,n); tmp=dmatrix(1,n,1,n); tmp1=dmatrix(1,n,1,1); evect=dmatrix(1,n,1,n); eval=dvector(1,n); eval_prev=dvector(1,n); chain=dmatrix(1,imax_chain,1,n); iweight = ivector(1,imax_chain); for(i=1;i<=imax_chain;++i) iweight[i] = 0; IDUM=IDUM_MCMC; chi2prev=m2n_initialize(a,cov1,avg1,start_dev); niter++; for(i=1;i<=n;++i) { aprev[i] = a[i]; chain[1][i] = a[i]; } pcnt=0; pcheck[pcnt]=1; stepfac=1; while(niter<NSTEP) { pcnt++; if(pcnt==ptot) { for(j=i=0;i<ptot;++i)j+=pcheck[i]; stepfac = stepfac*pow(0.9,5-j); if(!ThisTask)printf("STEPFAC %f %d %d\n",stepfac,j,count); pcnt=0; } stepfac=0.7; for(i=1;i<=n;++i) a[i] = (1+gasdev(&IDUM)*start_dev[i]*stepfac)*aprev[i]; if(MCMC>1) { RESET_COSMOLOGY++; j=0; for(i=1;i<=N_HOD_PARAMS;++i)if(HOD.free[i])j++; i=N_HOD_PARAMS; if(HOD.free[++i])OMEGA_M = a[++j]; if(HOD.free[++i])SIGMA_8 = a[++j]; if(HOD.free[++i])VBIAS = a[++j]; if(HOD.free[++i])VBIAS_C = a[++j]; if(HOD.free[++i])GAMMA = a[++j]; if(HOD.free[++i])SPECTRAL_INDX = a[++j]; if(HOD.free[++i])HUBBLE = a[++j]; } if(VBIAS_C<0)continue; if(USE_ERRORS) { M2N.mf_amp = a[++j]; M2N.bias_amp = a[++j]; M2N.scalebias_amp = a[++j]; } if(USE_VARIABLE_ROZO) { alpha_rozo = a[++j]; B_rozo = a[++j]; sig_rozo = a[++j]; } /* Hard-wire CVIR variation */ if(HOD.free[6]) CVIR_FAC = a[icvir]; /* Since we're at constant halo overdensity wrt RHO_CRIT, * set the overdensity of the halo */ DELTA_HALO = delta_halo_rhoc/OMEGA_M*(OMEGA_M*1.25*1.25*1.25 + (1-OMEGA_M))/(1.25*1.25*1.25); // Check to see if any of our parameters are out of range. if(parameter_out_of_range(a)){ muh(1); continue; } /* Draw random value of cvir from prior. */ /* if(CVIR_FAC<0.3 || CVIR_FAC>1.2)continue; */ /* CVIR_FAC = 0.9*drand48()+0.3; */ /* GAMMA = gasdev(&IDUM)*0.02 + 0.15; */ chi2=m2n_chi2_wp_wrapper(a); // reset cosmology for z=0.25 SIGMA_8 = SIGMA_8*growthfactor(0.25); RESET_COSMOLOGY++; if(MCMC>1 && chi2<1.0E7)chi2+= chi2a = chi2_m2n(); if(MCMC>1 && chi2<1.0E7)chi2+= chi2b = chi2_number_profiles(); if(!ThisTask){ printf("TRY %d ",++count); for(i=1;i<=n;++i) printf("%.4e ",a[i]); printf("%e\n",chi2);fflush(stdout); } pcheck[pcnt]=1; if(!(chi2<chi2prev || drand48() <= exp(-(chi2-chi2prev)/2))) { /* This for loop puts the prev element in the chain is * the current trial point is rejected. */ /* For the initialization, don't use this: we need * separate elements for estimating the covariance matrix. */ /* for(i=1;i<=n;++i) a[i] = aprev[i]; chi2 = chi2prev; */ if(USE_IWEIGHT) iweight[niter+1]++; pcheck[pcnt]=0; continue; } niter++; iweight[niter]++; for(i=1;i<=n;++i) chain[niter][i]=a[i]; for(i=1;i<=n;++i) avg1[i] += a[i]; for(i=1;i<=n;++i) aprev[i] = a[i]; for(i=1;i<=n;++i) for(j=1;j<=n;++j) cov1[i][j] += a[i]*a[j]; chi2prev=chi2; if(!ThisTask){ printf("ACCEPT %d %d ",niter,count); for(i=1;i<=n;++i) printf("%e ",a[i]); printf("%e %e %e %e\n",chi2,chi2a,chi2b,chi2-chi2a-chi2b);fflush(stdout); printf("HSTATS %d %e %e %e %e\n",niter,HOD.M_min,number_weighted_halo_mass(), number_weighted_central_mass(), qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; printf("FSAT %d %e %e %e %e\n",niter,fsat,HOD.M_min,HOD.sigma_logM,qromo(func_galaxy_bias,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); } } stepfac=1.6/sqrt(n); pcnt=-1; t0 = second(); NSTEP = niter; while(niter<imax_chain) { pcnt++; if(pcnt==ptot) { for(j=i=0;i<ptot;++i)j+=pcheck[i]; stepfac=1.6/sqrt(n); if(!ThisTask)printf("STEPFAC %f %d %d\n",stepfac,j,count); pcnt=0; } stepfac=1.6/sqrt(n)*1.2; if(convergence)goto SKIP_MATRIX; for(j=1;j<=n;++j) { avg1[j]=0; for(k=1;k<=n;++k) cov1[j][k]=0; } total_weight = 0; for(i=1;i<=niter;++i) { for(j=1;j<=n;++j) { avg1[j]+=chain[i][j]*iweight[i]; for(k=1;k<=n;++k) cov1[j][k]+=chain[i][j]*chain[i][k]*iweight[i]; } total_weight+=iweight[i]; } for(i=1;i<=n;++i) for(j=1;j<=n;++j) tmp[i][j] = cov1[i][j]/total_weight - avg1[i]*avg1[j]/(total_weight*total_weight); jacobi(tmp,n,eval,evect,&nrot); gaussj(evect,n,tmp1,1); SKIP_MATRIX: for(i=1;i<=n;++i) atemp[i] = gasdev(&IDUM)*sqrt(eval[i])*stepfac; for(i=1;i<=n;++i) for(a[i]=0,j=1;j<=n;++j) a[i] += atemp[j]*evect[j][i]; for(i=1;i<=n;++i) a[i] += aprev[i]; /* We seem to be having a problem with this. * So, broadcast the model params from the root processor. */ #ifdef PARALLEL MPI_Bcast(&a[1],n,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD); #endif if(MCMC>1) { RESET_COSMOLOGY++; j=0; for(i=1;i<=N_HOD_PARAMS;++i)if(HOD.free[i])j++; i=N_HOD_PARAMS; if(HOD.free[++i])OMEGA_M = a[++j]; if(HOD.free[++i])SIGMA_8 = a[++j]; if(HOD.free[++i])VBIAS = a[++j]; if(HOD.free[++i])VBIAS_C = a[++j]; if(HOD.free[++i])GAMMA = a[++j]; if(HOD.free[++i])SPECTRAL_INDX = a[++j]; if(HOD.free[++i])HUBBLE = a[++j]; } if(VBIAS_C<0)continue; if(USE_ERRORS) { M2N.mf_amp = a[++j]; M2N.bias_amp = a[++j]; M2N.scalebias_amp = a[++j]; } if(USE_VARIABLE_ROZO) { alpha_rozo = a[++j]; B_rozo = a[++j]; sig_rozo = a[++j]; } /* Hard-wire CVIR variation */ if(HOD.free[6]) CVIR_FAC = a[icvir]; /* Since we're at constant halo overdensity wrt RHO_CRIT, * set the overdensity of the halo */ DELTA_HALO = delta_halo_rhoc/OMEGA_M*(OMEGA_M*1.25*1.25*1.25 + (1-OMEGA_M))/(1.25*1.25*1.25); // Check to see if any of our parameters are out of range. if(parameter_out_of_range(a))continue; /* Draw random value of cvir from prior. */ /* CVIR_FAC = a[n]; */ /* if(CVIR_FAC<0.3 || CVIR_FAC>1.2)continue; */ /* CVIR_FAC = 0.7*drand48()+0.3; */ /* GAMMA = gasdev(&IDUM)*0.02 + 0.15; */ // printf("GAMMA %d %f %f\n",count+1,GAMMA,CVIR_FAC); chi2=m2n_chi2_wp_wrapper(a); // reset cosmology for z=0.25 SIGMA_8 = SIGMA_8*growthfactor(0.25); RESET_COSMOLOGY++; if(MCMC>1 && chi2<1.0E7)chi2 += chi2a = chi2_m2n(); if(MCMC>1 && chi2<1.0E7)chi2 += chi2b = chi2_number_profiles(); tprev = t0; t0 = second(); ++count; if(!ThisTask) { printf("TRY %d ",count); for(i=1;i<=n;++i) printf("%.4e ",a[i]); if(RESTART==2) { printf("%e %e %.2f\n",chi2,chi2/(1+exp(-count/100.0)), timediff(tprev,t0));fflush(stdout); } else { printf("%e %.2f\n",chi2, timediff(tprev,t0));fflush(stdout); } } if(0) { printf("CPU%02d %d ",ThisTask,count); for(i=1;i<=n;++i) printf("%.4e ",a[i]); if(RESTART==2) { printf("%e %e %.2f\n",chi2,chi2/(1+exp(-count/100.0)), timediff(tprev,t0));fflush(stdout); } else { printf("%e %.2f\n",chi2, timediff(tprev,t0));fflush(stdout); } } pcheck[pcnt]=0; if(!(chi2<chi2prev || drand48() <= exp(-(chi2-chi2prev)/2))) { /* for(i=1;i<=n;++i) a[i] = aprev[i]; chi2 = chi2prev; */ if(USE_IWEIGHT) iweight[niter+1]++; continue; } pcheck[pcnt]=1; // if(NSTEP<NSTEP_MAX)NSTEP++; niter++; if(!convergence)NSTEP = niter; iweight[niter]++; if(niter%NSTEP_MAX==0 && !convergence && niter>NSTEP_MAX) { convergence = 1; for(i=1;i<=n;++i) { x1=fabs(eval[i]-eval_prev[i])/eval_prev[i]; if(x1>0.01)convergence = 0; printf("CONVERGENCE CHECK %d %d %e %e %e\n",niter/NSTEP_MAX,i,x1,eval[i],eval_prev[i]); } for(i=1;i<=n;++i) eval_prev[i] = eval[i]; convergence = 0; if(convergence) printf("CONVERGENCE ACCOMPLISHED %d %d \n",niter,count); } if(niter==NSTEP_MAX) { for(i=1;i<=n;++i) eval_prev[i] = eval[i]; } for(i=1;i<=n;++i) chain[niter][i]=a[i]; for(i=1;i<=n;++i) avg1[i] += a[i]; for(i=1;i<=n;++i) aprev[i] = a[i]; for(i=1;i<=n;++i) for(j=1;j<=n;++j) cov1[i][j] += a[i]*a[j]; chi2prev=chi2; if(!ThisTask) { printf("ACCEPT %d %d ",niter,count); for(i=1;i<=n;++i) printf("%e ",a[i]); printf("%e %e %e %e\n",chi2,chi2a,chi2b,chi2-chi2a-chi2b);fflush(stdout); if(MCMC==1) { printf("HSTATS %d %e %e %e %e\n",niter,HOD.M_min,number_weighted_halo_mass(), number_weighted_central_mass(), qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; printf("FSAT %d %e %e %e %e\n",niter,fsat,HOD.M_min,HOD.sigma_logM,qromo(func_galaxy_bias,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); } } } }
double comoving_volume(double zlo, double zhi) { return 7500./41266.*pow(qromo(efunc1,zlo,zhi,midpnt)*c_on_H0,3.0)*4./3.*PI; }
int main(int argc, char **argv) { double s1, delta_vir, omega_m, x; int i, j; FILE *fp; #ifdef PARALLEL printf("STARTING>>>\n"); fflush(stdout); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask); MPI_Comm_size(MPI_COMM_WORLD, &NTask); printf("TASK %d reporting for duty.\n",ThisTask); fflush(stdout); #endif ARGC = argc; ARGV = argv; OUTPUT=0; HOD.fredc = HOD.freds = 1.0; for(i=1; i<=99; ++i) HOD.free[i]=0; wp.esys=0; Work.chi2=0; Work.imodel=1; USE_ERRORS = 0; ITRANS=4; HUBBLE=0.7; BEST_FIT = 0; HOD.M_sat_break = 1.0e14; HOD.alpha1 = 1.0; if(argc==1) endrun("./HOD.x hod.bat_file > output"); read_parameter_file(argv[1]); if(REDSHIFT>0) { SIGMA_8 = SIGMA_8*growthfactor(REDSHIFT); HUBBLEZ = sqrt(OMEGA_M*pow(1+REDSHIFT,3.0)+1-OMEGA_M); OMEGA_Z = OMEGA_M*pow(1+REDSHIFT,3.0)/(OMEGA_M*pow(1+REDSHIFT,3.0)+(1-OMEGA_M)); fprintf(stdout,"SIGMA_8(Z=%.3f)= %.4f\n",REDSHIFT,SIGMA_8); fprintf(stdout,"H(Z=%.3f)/H0= %.4f\n",REDSHIFT,HUBBLEZ); HOD.M_min = 0; RESET_COSMOLOGY++; set_HOD_params(); } /* Output the virial overdensity for reference. */ if(OUTPUT) { omega_m=OMEGA_M*pow(1+REDSHIFT,3.0)/(OMEGA_M*pow(1+REDSHIFT,3.0)+(1-OMEGA_M)); x=omega_m-1; delta_vir=(18*PI*PI+82*x-39*x*x)/(1+x); printf("DELTA_VIR(Omega_m,z) = %f\n",delta_vir); } /* Do some initialization if we're doing SHMR */ if(SHMR_FLAG) { if(SATELLITE_PARAMETERIZATION)SHMR_PARAMS = 14; if(VARIABLE_ALPHA)SHMR_PARAMS += 2; if(VARIABLE_EXCLUSION)wpl.a[SHMR_PARAMS+1] = EXCLUSION_RADIUS; wpl.ncf = SHMR_PARAMS + VARIABLE_EXCLUSION; HOD.pdfs = 100; HOD.pdfc = 101; wpx.calculate_two_halo = 1; input_stellar_mass_bins(); // if we have input from the prompt, take that if(argc>2 && atoi(argv[2])!=999) { fp = openfile(argv[2]); fscanf(fp,"%d %d",&i,&j); for(i=1; i<=wpl.ncf; ++i) fscanf(fp,"%lf",&wpl.a[i]); fclose(fp); } } for(i=1; i<=wpl.ncf; ++i) printf("wpl.a[%d]= %e\n",i,wpl.a[i]); /* LENSING TESTING FOR ALEXIE */ if(argc>2) IDUM_MCMC=atoi(argv[2]); SIGMA_8Z0 = 0.8; if(argc>2) if(atoi(argv[2])==999) test(argc,argv); /* If there's no cross-correlation function, * set the second number density equal to the first */ if(!XCORR) GALAXY_DENSITY2 = GALAXY_DENSITY; /* Initialize the non-linear power spectrum. */ nonlinear_sigmac(8.0); sigmac_interp(1.0E13); sigmac_radius_interp(1.0); /* Skip the HOD stuff if we're SHMR-ing it: */ if(SHMR_FLAG) { if(argc>2 && atoi(argv[2])==999)test(argc, argv); if(argc>3 && atoi(argv[3])==999)test(argc, argv); goto TASKS; } /* Get the galaxy bias factor */ s1=qromo(func_galaxy_bias,log(HOD.M_low),log(HOD.M_max),midpnt); GALAXY_BIAS=s1/GALAXY_DENSITY; if(OUTPUT) fprintf(stdout,"Galaxy Bias bg= %f\n",GALAXY_BIAS); fflush(stdout); /* Get the galaxy satellite fraction */ s1=qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt)/ GALAXY_DENSITY; if(OUTPUT) fprintf(stdout,"fsat %e\n",s1); fflush(stdout); /* Mean halo mass. */ if(OUTPUT) fprintf(stdout,"M_eff %e\n",number_weighted_halo_mass()); fflush(stdout); /* Set up BETA for wp integration. */ BETA = pow(OMEGA_M,0.6)/GALAXY_BIAS; if(OUTPUT) printf("BETA = %f\n",BETA); TASKS: tasks(argc,argv); }
void mcmc_color_minimization() { double stepfac=1; double error=1,tolerance=0,**cov1,**tmp,*a,*avg1,chi2,chi2prev, **evect,*eval,*aprev,*atemp,**tmp1,*opar,x1,fsat,**chain,*start_dev,*eval_prev; int n,i,j,k,nrot,niter=0,count=0,imax_chain=100000,NSTEP=50,NSTEP_MAX=10000,convergence=0; long IDUM=-555; int *pcheck,pcnt,ptot=20,firstflag=1,*iweight,total_weight; double t0,tprev,temp,chi2a,chi2b; opar=dvector(1,100); MCMC=Task.MCMC; pcheck=calloc(ptot,sizeof(int)); Work.imodel=2; Work.chi2=1; OUTPUT=1; fprintf(stderr,"\n\nMCMC 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; wp_color_input(); srand48(32498793); /* Find the number of free parameters in the minimization * for the real-space correlation function. */ for(n=0,i=1;i<12;++i) { n+=HOD.free[i]; /* if(i>N_HOD_PARAMS && HOD.free[i])MCMC=3;*/ if(OUTPUT) printf("mcmc_min> free[%i] = %d\n",i,HOD.free[i]); } // add 3 parameters for the color stuff n+=3; wp.ncf=n; if(HOD.free[0]) { wp.ngal = GALAXY_DENSITY; wp.ngal_err = 0.1*wp.ngal; FIX_PARAM = 0; } if(OUTPUT) printf("mcmc_min> %d free parameters\n",n); a=dvector(1,n); start_dev=dvector(1,n); aprev=dvector(1,n); atemp=dvector(1,n); cov1=dmatrix(1,n,1,n); avg1=dvector(1,n); tmp=dmatrix(1,n,1,n); tmp1=dmatrix(1,n,1,1); evect=dmatrix(1,n,1,n); eval=dvector(1,n); eval_prev=dvector(1,n); chain=dmatrix(1,imax_chain,1,n); iweight = ivector(1,imax_chain); for(i=1;i<=imax_chain;++i) iweight[i] = 0; IDUM=IDUM_MCMC; //chi2prev=mcmc_initialize(a,cov1,avg1,start_dev); chi2prev=mcmc_color_initialize(a,cov1,avg1,start_dev); //initial_color_values(a,pp,yy); niter++; for(i=1;i<=n;++i) { aprev[i] = a[i]; chain[1][i] = a[i]; } pcnt=0; pcheck[pcnt]=1; stepfac=1; while(niter<NSTEP) { pcnt++; if(pcnt==ptot) { for(j=i=0;i<ptot;++i)j+=pcheck[i]; stepfac = stepfac*pow(0.9,5-j); if(!ThisTask)printf("STEPFAC %f %d %d\n",stepfac,j,count); pcnt=0; } /* stepfac=0.7; */ for(i=1;i<=n;++i) a[i] = (1+gasdev(&IDUM)*start_dev[i]*stepfac)*aprev[i]; if(MCMC>1) { RESET_COSMOLOGY++; j=0; for(i=1;i<=N_HOD_PARAMS;++i)if(HOD.free[i])j++; i=N_HOD_PARAMS; if(HOD.free[++i])OMEGA_M = a[++j]; if(HOD.free[++i])SIGMA_8 = a[++j]; if(HOD.free[++i])VBIAS = a[++j]; if(HOD.free[++i])VBIAS_C = a[++j]; if(HOD.free[++i])GAMMA = a[++j]; if(HOD.free[++i])SPECTRAL_INDX = a[++j]; /* if(HOD.free[++i])SIGV = a[++j]; */ } if(VBIAS_C<0)continue; /* Hard-wire CVIR variation */ if(HOD.free[6]) CVIR_FAC = a[3]; chi2=chi2_wp_color_wrapper(a); if(!ThisTask){ printf("TRY %d ",++count); for(i=1;i<=n;++i) printf("%.4e ",a[i]); printf("%e\n",chi2);fflush(stdout); } pcheck[pcnt]=1; if(!(chi2<chi2prev || drand48() <= exp(-(chi2-chi2prev)/2))) { /* This for loop puts the prev element in the chain is * the current trial point is rejected. */ /* For the initialization, don't use this: we need * separate elements for estimating the covariance matrix. */ /* for(i=1;i<=n;++i) a[i] = aprev[i]; chi2 = chi2prev; */ pcheck[pcnt]=0; continue; } niter++; iweight[niter]++; for(i=1;i<=n;++i) chain[niter][i]=a[i]; for(i=1;i<=n;++i) avg1[i] += a[i]; for(i=1;i<=n;++i) aprev[i] = a[i]; for(i=1;i<=n;++i) for(j=1;j<=n;++j) cov1[i][j] += a[i]*a[j]; chi2prev=chi2; if(!ThisTask){ printf("ACCEPT %d %d ",niter,count); for(i=1;i<=n;++i) printf("%e ",a[i]); printf("%e\n",chi2);fflush(stdout); printf("HSTATS %d %e %e %e %e\n",niter,HOD.M_min,number_weighted_halo_mass(), number_weighted_central_mass(), qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); printf("FSAT %d %e %e %e %e\n",niter,HOD.M_min,wp.fsat_red,wp.fsat_blue,wp.fsat_all); } } RESTART_POINT: stepfac=1.6/sqrt(n); pcnt=-1; t0 = second(); NSTEP = niter; while(niter<imax_chain) { pcnt++; if(pcnt==ptot) { for(j=i=0;i<ptot;++i)j+=pcheck[i]; //stepfac=1.6/sqrt(n); //stepfac=0.6/sqrt(n); // stepfac = stepfac*pow(0.9,6-j); stepfac = 0.25; stepfac = 0.5; stepfac=1.6/sqrt(n); if(!ThisTask)printf("STEPFAC %f %d %d\n",stepfac,j,count); pcnt=0; } stepfac=1.6/sqrt(n); //stepfac = 0; if(convergence)goto SKIP_MATRIX; // if(niter>NSTEP_MAX && niter%NSTEP_MAX!=0)goto SKIP_MATRIX; for(j=1;j<=n;++j) { avg1[j]=0; for(k=1;k<=n;++k) cov1[j][k]=0; } total_weight = 0; for(i=1;i<=niter;++i) { for(j=1;j<=n;++j) { avg1[j]+=chain[i][j]*iweight[i]; for(k=1;k<=n;++k) cov1[j][k]+=chain[i][j]*chain[i][k]*iweight[i]; } total_weight+=iweight[i]; } for(i=1;i<=n;++i) for(j=1;j<=n;++j) tmp[i][j] = cov1[i][j]/total_weight - avg1[i]*avg1[j]/(total_weight*total_weight); jacobi(tmp,n,eval,evect,&nrot); gaussj(evect,n,tmp1,1); SKIP_MATRIX: if(RESTART==4)convergence = 1; if(RESTART && count==0)stepfac=0; for(i=1;i<=n;++i) atemp[i] = gasdev(&IDUM)*sqrt(eval[i])*stepfac; for(i=1;i<=n;++i) for(a[i]=0,j=1;j<=n;++j) a[i] += atemp[j]*evect[j][i]; for(i=1;i<=n;++i) a[i] += aprev[i]; /* We seem to be having a problem with this. * So, broadcast the model params from the root processor. */ #ifdef PARALLEL MPI_Bcast(&a[1],n,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD); #endif // CHeck that the chi2 for the last point in the restart chain // is recovered. /* if(RESTART && !count) for(i=1;i<=n;++i) a[i] = aprev[i]; */ if(RESTART==5) { j = count+1; if(j>4000)exit(0); for(i=1;i<=n;++i) a[i] = chain[j][i]; } /* if(!ThisTask) for(i=1;i<=n;++i) { printf("COV %d %d %e ",count,i,sqrt(eval[i])); for(j=1;j<=n;++j) printf("%e ",evect[j][i]); printf("\n"); } */ /* Using only variances */ //for(i=1;i<=n;++i) // a[i] = aprev[i] + gasdev(&IDUM)*sqrt(tmp[i][i])*stepfac; if(MCMC>1) { RESET_COSMOLOGY++; j=0; for(i=1;i<=N_HOD_PARAMS;++i)if(HOD.free[i])j++; i=N_HOD_PARAMS; if(HOD.free[++i])OMEGA_M = a[++j]; if(HOD.free[++i])SIGMA_8 = a[++j]; if(HOD.free[++i])VBIAS = a[++j]; if(HOD.free[++i])VBIAS_C = a[++j]; if(HOD.free[++i])GAMMA = a[++j]; if(HOD.free[++i])SPECTRAL_INDX = a[++j]; /* if(HOD.free[++i])SIGV = a[++j]; */ } if(VBIAS_C<0)continue; /* Hard-wire CVIR variation */ if(HOD.free[6]) CVIR_FAC = a[3]; chi2=chi2_wp_color_wrapper(a); tprev = t0; t0 = second(); ++count; if(!ThisTask) { printf("TRY %d ",count); for(i=1;i<=n;++i) printf("%.4e ",a[i]); if(RESTART==2) { printf("%e %e %.2f\n",chi2,chi2/(1+exp(-count/100.0)), timediff(tprev,t0));fflush(stdout); } else { printf("%e %.2f\n",chi2, timediff(tprev,t0));fflush(stdout); } } if(0) { printf("CPU%02d %d ",ThisTask,count); for(i=1;i<=n;++i) printf("%.4e ",a[i]); if(RESTART==2) { printf("%e %e %.2f\n",chi2,chi2/(1+exp(-count/100.0)), timediff(tprev,t0));fflush(stdout); } else { printf("%e %.2f\n",chi2, timediff(tprev,t0));fflush(stdout); } } pcheck[pcnt]=0; if(!(chi2<chi2prev || drand48() <= exp(-(chi2-chi2prev)/2))) { /* for(i=1;i<=n;++i) a[i] = aprev[i]; chi2 = chi2prev; */ continue; } pcheck[pcnt]=1; // if(NSTEP<NSTEP_MAX)NSTEP++; niter++; if(!convergence)NSTEP = niter; iweight[niter]++; if(niter%NSTEP_MAX==0 && !convergence && niter>NSTEP_MAX) { convergence = 1; for(i=1;i<=n;++i) { x1=fabs(eval[i]-eval_prev[i])/eval_prev[i]; if(x1>0.01)convergence = 0; printf("CONVERGENCE CHECK %d %d %e %e %e\n",niter/NSTEP_MAX,i,x1,eval[i],eval_prev[i]); } for(i=1;i<=n;++i) eval_prev[i] = eval[i]; convergence = 0; if(convergence) printf("CONVERGENCE ACCOMPLISHED %d %d \n",niter,count); } if(niter==NSTEP_MAX) { for(i=1;i<=n;++i) eval_prev[i] = eval[i]; } for(i=1;i<=n;++i) chain[niter][i]=a[i]; for(i=1;i<=n;++i) avg1[i] += a[i]; for(i=1;i<=n;++i) aprev[i] = a[i]; for(i=1;i<=n;++i) for(j=1;j<=n;++j) cov1[i][j] += a[i]*a[j]; chi2prev=chi2; if(!ThisTask) { printf("ACCEPT %d %d ",niter,count); for(i=1;i<=n;++i) printf("%e ",a[i]); printf("%e\n",chi2);fflush(stdout); printf("FSAT %d %e %e %e %e\n",niter,HOD.M_min,wp.fsat_red,wp.fsat_blue,wp.fsat_all); if(MCMC==1) { printf("HSTATS %d %e %e %e %e\n",niter,HOD.M_min,number_weighted_halo_mass(), number_weighted_central_mass(), qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); fsat = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; } } } }
double chi2_wp_color(double *a) { double chi2,nsat_blue,ncen_blue,dlogm,m,func_blue_fraction(),temp1,temp2,fsat_blue,fsat_red,fsat_all; int i,j; static int iter=0; COVAR = 0; HOD.color = 0; GALAXY_DENSITY2 = GALAXY_DENSITY = wp_color.ngal_full; wp.np = wp_color.n_full; for(i=wp.ncf-2;i<=wp.ncf;++i) if(a[i]<0)return(1.0e+7); for(i=1;i<=wp.np;++i) { wp.r[i] = wp_color.r_full[i]; wp.x[i] = wp_color.x_full[i]; wp.e[i] = wp_color.e_full[i]; for(j=1;j<=wp.np;++j) wp.covar[i][j] = wp_color.covar_full[i][j]; } chi2 = chi2_wp(a); HOD.M_low0 = set_low_mass(); fsat_all = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; /* dlogm=(log(HOD.M_max)-log(HOD.M_low))/99; for(i=1;i<=100;++i) { m=exp((i-1)*dlogm)*HOD.M_low; printf("HODFULL %e %f %f %f\n",m,N_cen(m)+N_sat(m),N_cen(m),N_sat(m)); } printf("GALDEN FULL %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt)); sprintf(Task.root_filename,"gal_full"); populate_simulation(); */ if(!iter) for(i=1;i<=wp.np;++i) for(j=1;j<=wp.np;++j) wp_color.covar_full[i][j] = wp.covar[i][j]; HOD.fblue0_sat = a[wp.ncf - 2]; HOD.sigma_fblue_sat = a[wp.ncf - 1]; HOD.sigma_fblue_cen = a[wp.ncf - 0]; HOD.fblue0_cen = 1.0; HOD.color = 1; HOD.fblue0_cen = pow(10.0,zbrent(func_blue_fraction,-5.0,1.0,1.0E-5)); if(OUTPUT) fprintf(stdout,"old fblue0_cen= %e %e\n",HOD.fblue0_cen,HOD.M_min); if(DENSITY_DEPENDENCE) { HOD.color = 2; temp1 = HOD.M_min; temp2 = HOD.M_low; populate_simulation(); HOD.M_min = temp1; HOD.M_low = temp2; HOD.fblue0_cen = pow(10.0,zbrent(dd_func_red_fraction,-5.0,1.0,1.0E-5)); HOD.color = 1; if(OUTPUT) fprintf(stdout,"new fblue0_cen= %e %e\n",HOD.fblue0_cen, (1. - HOD.M_min_fac*(1.0 - HOD.fblue0_cen))); // populate_simulation(); //exit(0); } else HOD.fblue0_cen = pow(10.0,zbrent(func_blue_fraction,-5.0,1.0,1.0E-5)); if(ERROR_FLAG) { ERROR_FLAG=0; return(1.0e7); } /* This is only if you have square functions for the central occupation */ /* nsat_blue = qromo(func_satellite_density,log(HOD.M_low),log(HOD.M_max),midpnt); ncen_blue = qromo(func_central_density,log(HOD.M_low),log(HOD.M_cen_max),midpnt); HOD.fblue0_cen = (wp_color.ngal_blue - nsat_blue)/ncen_blue; */ GALAXY_DENSITY2 = GALAXY_DENSITY = wp_color.ngal_blue; wp.np = wp_color.n_blue; for(i=1;i<=wp.np;++i) { wp.r[i] = wp_color.r_blue[i]; wp.x[i] = wp_color.x_blue[i]; wp.e[i] = wp_color.e_blue[i]; for(j=1;j<=wp.np;++j) wp.covar[i][j] = wp_color.covar_blue[i][j]; } chi2 += chi2_wp(a); fsat_blue = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; /* dlogm=(log(HOD.M_max)-log(HOD.M_low))/99; for(i=1;i<=100;++i) { m=exp((i-1)*dlogm)*HOD.M_low; printf("HODBLUE %e %f %f %f\n",m,N_cen(m)+N_sat(m),N_cen(m),N_sat(m)); } printf("GALDEN BLUE %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt)); sprintf(Task.root_filename,"gal_blue"); populate_simulation(); */ if(!iter) for(i=1;i<=wp.np;++i) for(j=1;j<=wp.np;++j) wp_color.covar_blue[i][j] = wp.covar[i][j]; HOD.color = 2; GALAXY_DENSITY2 = GALAXY_DENSITY = wp_color.ngal_red; wp.np = wp_color.n_red; for(i=1;i<=wp.np;++i) { wp.r[i] = wp_color.r_red[i]; wp.x[i] = wp_color.x_red[i]; wp.e[i] = wp_color.e_red[i]; for(j=1;j<=wp.np;++j) wp.covar[i][j] = wp_color.covar_red[i][j]; } chi2 += chi2_wp(a); fsat_red = qromo(func_satfrac,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY; /* dlogm=(log(HOD.M_max)-log(HOD.M_low))/99; for(i=1;i<=100;++i) { m=exp((i-1)*dlogm)*HOD.M_low; printf("HODRED %e %f %f %f\n",m,N_cen(m)+N_sat(m),N_cen(m),N_sat(m)); } printf("GALDEN RED %e\n",qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt)); sprintf(Task.root_filename,"gal_red"); populate_simulation(); exit(0); */ if(!iter) for(i=1;i<=wp.np;++i) for(j=1;j<=wp.np;++j) wp_color.covar_red[i][j] = wp.covar[i][j]; iter++; wp.fsat_all = fsat_all; wp.fsat_red = fsat_red; wp.fsat_blue = fsat_blue; printf("COLOR_PARAMS %d %e %e %e %e %e %e %e %e\n",iter,chi2,HOD.M_min,HOD.M1,HOD.alpha,HOD.fblue0_cen,HOD.fblue0_sat,HOD.sigma_fblue_cen,HOD.sigma_fblue_sat); printf("COLOR_ITER %d %e %e %f %f %f\n",iter,chi2,HOD.fblue0_cen,fsat_all,fsat_blue,fsat_red); fflush(stdout); return(chi2); }
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); }
/* This is a function to set the HOD parameters until * I get some input code or batch file set up. * */ void set_HOD_params() { int i,j=1; double m,error=1.0,tol=1.0E-4,prev,s1,mlo; /* If the mass function at M_max is undefined, reset M_max */ //LOCAL_DENSITY = -0.2; if(LOCAL_DENSITY!=0) { HOD.M_max = 1.0E16; while(dndM_interp(HOD.M_max)<=0) { HOD.M_max*=0.9; } fprintf(stderr,"NEW M_max= %e\n",HOD.M_max); } if(HOD.pdfc == 2 || HOD.pdfc == 3 || HOD.pdfc == 6 || HOD.pdfc == 8 || HOD.pdfc == 9 || HOD.pdfc == 12 || HOD.pdfc == 13) SOFT_CENTRAL_CUTOFF=1; /* Error trap both the galaxy density and M_min both left unspecified. */ if(HOD.M_min<=0 && GALAXY_DENSITY<=0 && HOD.free[0]==0) endrun("ERROR: Must specify either M_min or GALAXY_DENSITY"); /* If the user has specified M_min and M1, calculate the galaxy density. */ if(HOD.M_min>0 && HOD.M1>0) { HOD.M_low = -1; if (wp.ngal==0) wp.ngal = GALAXY_DENSITY; if(SOFT_CENTRAL_CUTOFF) HOD.M_low0 = HOD.M_low = exp(zbrent(func_mlow,log(HOD.M_min*1.0E-6),log(HOD.M_min*1.1),1.0E-5)); else HOD.M_low0 = HOD.M_low = HOD.M_min; if(HOD.M_low > HOD.M1 ) { while(N_cen(HOD.M_low) > 0.01) HOD.M_low /= 2.; } //if(HOD.M_low < HOD.M_min/100.) HOD.M_low = HOD.M_min/100.; GALAXY_DENSITY=qromo(func_galaxy_density,log(HOD.M_low),log(HOD.M_max),midpnt); if(OUTPUT) { fprintf(stdout,"M_low= %e\n",HOD.M_low); fprintf(stdout,"ng= %e\n",GALAXY_DENSITY); } } /* If M_min<=0 then use the specified galaxy density to calculate M_min */ if(HOD.M_min<=0) { if(HOD.pdfc==7 && HOD.pdfc==8) HOD.M_min=pow(10.0,zbrent(fixfunc1,8.0,log10(HOD.M_cen_max*0.99),1.0E-5)); else HOD.M_min=pow(10.0,zbrent(fixfunc1,7.0,14.8,1.0E-5)); if(OUTPUT) fprintf(stderr,"SOFT_CENTRAL_CUTOFF Delta-n %d %e\n",SOFT_CENTRAL_CUTOFF, fixfunc1(log10(HOD.M_min))); HOD.M_low = -1; if(SOFT_CENTRAL_CUTOFF) { //HOD.M_low = exp(zbrent(func_mlow,log(HOD.M_min)-5*HOD.sigma_logM*2.3, // log(HOD.M_min),1.0E-5)); HOD.M_low = HOD.M_min/2.; while(N_cen(HOD.M_low) > 0.01) HOD.M_low /= 2.; } else HOD.M_low = HOD.M_min; if(HOD.M_low<1.0E7)HOD.M_low=1.0E+7; if(OUTPUT) { fprintf(stdout,"M_min %e [ng= %e]\n",HOD.M_min,GALAXY_DENSITY); fprintf(stdout,"M_low= %e\n",HOD.M_low); } } /* If M1<=0 then use the specified galaxy density to calculate M1 */ if(HOD.M1<=0) { HOD.M_low = -1; if(SOFT_CENTRAL_CUTOFF) HOD.M_low = exp(zbrent(func_mlow,log(HOD.M_min)-5*HOD.sigma_logM*2.3, log(HOD.M_min*1.1),1.0E-5)); else HOD.M_low = HOD.M_min; if(HOD.M_low<1.0E7)HOD.M_low=1.0E+7; HOD.M1=pow(10.0,zbrent(fixfunc2,log10(HOD.M_low),15.8,1.0E-5)); if(OUTPUT) { fprintf(stdout,"M1 %e [ng= %e]\n",HOD.M1,GALAXY_DENSITY); fprintf(stdout,"M_min = %e M_low= %e\n",HOD.M_min,HOD.M_low); } } /* Set the number of halo mass bins we've got. */ NUM_POW2MASS_BINS=log(HOD.M_max/HOD.M_low)/LN_2+1; if(HOD.pdfc==6) HOD.M_hi = set_high_central_mass(); HOD.M_low0 = set_low_mass(); return; }
/* This pair of functions is meant to find the halo mass at which 1 halo * should exist in a volume of size BOX_SIZE^3 */ double func_max_mass(double mlo) { return qromo(func_halo_density,mlo,log(1.0E17),midpnt)*BOX_SIZE*BOX_SIZE*BOX_SIZE - 1; }
/* Calculates the average mass of a halo in the HOD. */ double number_weighted_halo_mass() { double funcxx1(); return(qromo(funcxx1,log(HOD.M_low),log(HOD.M_max),midpnt)/GALAXY_DENSITY); }