示例#1
0
int main(int argc, char **argv){

  
  FILE *fid;
  DIR* dir;
  char fname[300];
  long int i,j,nz,elem;
  double aver[1000],fcollv[1000],sfrv[1000],zv[1000];
  float *halo_mass;
  double zmin,zmax,dz,redshift;
  
  if(argc==1 || argc > 5) {
    printf("Generates SFRD using nonlinear halo boxes\n");
    printf("usage: get_SFR base_dir [zmin] [zmax] [dz]\n");
    printf("base_dir contains simfast21.ini\n");
    exit(1);
  }  
  get_Simfast21_params(argv[1]);
  if(global_use_Lya_xrays==0) {printf("Lya and xray use set to false - no need to calculate SFR\n");exit(0);}
  if(argc > 2) {
    zmin=atof(argv[2]);
    if (zmin < global_Zminsfr) zmin=global_Zminsfr;
    if(argc > 3) {
      zmax=atof(argv[3]);
      if(zmax>global_Zmaxsim) zmax=global_Zmaxsim;
      if(argc==5) dz=atof(argv[4]); else dz=global_Dzsim;
    }else {
      zmax=global_Zmaxsim;
      dz=global_Dzsim;
    }
    zmin=zmax-dz*ceil((zmax-zmin)/dz); /* make sure (zmax-zmin)/dz is an integer so that we get same redshifts starting from zmin or zmax...*/ 
  }else {
    zmin=global_Zminsfr;
    zmax=global_Zmaxsim;
    dz=global_Dzsim;
  }
  printf("\nCalculating SFRD between z=%f and z=%f with step %f\n",zmin,zmax,dz);
#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif
  
  /* Create directory SFR */
  sprintf(fname,"%s/SFR",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating SFR directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }  

  if(!(halo_mass=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }

  nz=0;
  printf("Calculating halo mass average...\n");
  for(redshift=zmin;redshift<(zmax+dz/10);redshift+=dz){
    zv[nz]=redshift;
    sprintf(fname, "%s/Halos/halonl_z%.3f_N%ld_L%.0f.dat",argv[1],redshift,global_N_smooth,global_L); 
    if((fid=fopen(fname,"rb"))==NULL){  
      printf("\nError opening %s\n",fname);
      exit(1);
    }
    elem=fread(halo_mass,sizeof(float),global_N3_smooth,fid);
    fclose(fid);
    aver[nz]=0.;
    for(i=0;i<global_N3_smooth;i++) aver[nz]+=halo_mass[i];
    fcollv[nz]=aver[nz]/(global_rho_m*global_L3);  /* Msun/(Mpc/h)^3 */
    aver[nz]/=global_N3_smooth;
    nz++;
  }  

  printf("Interpolation for SFRD...\n");
  gsl_interp_accel *acc = gsl_interp_accel_alloc ();
  gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, nz);
  gsl_spline_init (spline, zv, fcollv, nz);


  sprintf(fname,"%s/Output_text_files",argv[1]);
  if((dir=opendir(fname))==NULL) {
    printf("Creating Output_text_files directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }
  sprintf(fname,"%s/Output_text_files/sfrd_av_N%ld_L%.0f.dat",argv[1],global_N_smooth,global_L); 
  if((fid=fopen(fname,"a"))==NULL){
    printf("\nError opening output %s file...\n",fname); 
    exit(1);
  }  
  for(i=nz-1;i>=0;i--) {
    sfrv[i]=gsl_spline_eval_deriv(spline, zv[i], acc)*dzdt(zv[i])/3.171E-8*global_rho_b*global_fstar; /* SFRD in Msun/(Mpc/h)^3/year */
    if(sfrv[i]<0.) sfrv[i]=0.;
    fprintf(fid,"%f %.8E\n",zv[i],sfrv[i]);
  }  
  fclose(fid);
  gsl_spline_free (spline);
  gsl_interp_accel_free (acc);

  printf("Writing SFRD files...\n");
  for(i=0;i<nz;i++) {
    sprintf(fname, "%s/Halos/halonl_z%.3f_N%ld_L%.0f.dat",argv[1],zv[i],global_N_smooth,global_L); 
    if((fid=fopen(fname,"rb"))==NULL){  
      printf("\nError opening %s\n",fname);
      exit(1);
    }
    elem=fread(halo_mass,sizeof(float),global_N3_smooth,fid);
    fclose(fid);
    
    if(aver[i]>0.)
      for(j=0;j<global_N3_smooth;j++) halo_mass[j]*=sfrv[i]/aver[i];
    else for(j=0;j<global_N3_smooth;j++) halo_mass[j]=0.;
    sprintf(fname, "%s/SFR/sfrd_z%.3f_N%ld_L%.0f.dat",argv[1],zv[i],global_N_smooth,global_L); 
    if((fid=fopen(fname,"wb"))==NULL){
      printf("\nError opening output sfrd file... Chech if path is correct...\n"); 
    }
    elem=fwrite(halo_mass,sizeof(float),global_N3_smooth,fid);
    fclose(fid); 
  }

  exit(0);

}  
示例#2
0
int main(int argc, char **argv){


  FILE *fid;
  size_t elem;
  long int i,j,p;
  long int indi, indj;
  double kk;
  fftwf_complex  *map_vel_c;
  float *map;
  fftwf_complex *map_in_c; 
  fftwf_plan pc2r;
  fftwf_plan pr2c;
  char fname[256];
  DIR* dir;

 
  if(argc != 2) {
    printf("Usage: get_velocityfield work_dir\n");
    printf("work_dir - directory containing simfast21.ini \n");
    exit(1);
  }  

  get_Simfast21_params(argv[1]);
  

#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  fftwf_init_threads();
  fftwf_plan_with_nthreads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif

 if(!(map=(float *) fftwf_malloc(global_N_halo*global_N_halo*global_N_halo*sizeof(float)))) {  
    printf("Problem...\n");
    exit(1);
  }
 if(!(map_in_c = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * global_N_halo*global_N_halo*(global_N_halo/2+1)))) {  
   printf(" Out of memory...\n");
   exit(1);
 }
 if(!(map_vel_c = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * global_N_halo*global_N_halo*(global_N_halo/2+1)))) {  
   printf("Problem allocating memory for x velocity field in k-space...\n");
   exit(1);
 } 
 /* Tansformacoes de fourier para calcular as caixas vx(x) vx(y) e vx(z) */
 if(!(pc2r=fftwf_plan_dft_c2r_3d(global_N_halo, global_N_halo, global_N_halo, map_vel_c, map, FFTW_ESTIMATE))) { 
   printf("Problem...\n");
   exit(1);
 }
 /* FFT para map */
 if(!(pr2c=fftwf_plan_dft_r2c_3d(global_N_halo, global_N_halo, global_N_halo, map , map_in_c, FFTW_ESTIMATE))) { 
   printf("Problem...\n");
   exit(1);
 }
 
 sprintf(fname, "%s/delta/delta_z0_N%ld_L%d.dat", argv[1],global_N_halo,(int)(global_L));
 /*Leitura do campo de densidades no espaco real*/
 fid=fopen(fname,"rb");	/* second argument contains name of input file */
 if (fid==NULL) {
   printf("\n Density file path is not correct or the file does not exit...\n"); 
   exit (1);
 }
 elem=fread(map,sizeof(float),global_N_halo*global_N_halo*global_N_halo,fid);
 fclose(fid);
 
 
 /***********************************************************************************/
 // Conversao do mapa de densidades de real para complexo
  
 fftwf_execute(pr2c);


 /********************************************************************/
 /********************************************************************/
 /********************************************************************/
 /* Computing velocity fields */

 /* Create directory Velocity */
  sprintf(fname,"%s/Velocity",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating Velocity directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }
   
 /********************************************************************/
 printf("\nComputing v_x field...\n");fflush(0);

#ifdef _OMPTHREAD_
#pragma omp parallel for shared(global_N_halo,global_dk,map_vel_c,map_in_c,global_dx_halo) private(i,indi,j,indj,p,kk) 
#endif
 for(i=0;i<global_N_halo;i++) {         
   if(i>global_N_halo/2) {    /* Large frequencies are equivalent to smaller negative ones */
     indi=-(global_N_halo-i);
   }else indi=i;
   for(j=0;j<global_N_halo;j++) {           
     if(j>global_N_halo/2) {  
       indj=-(global_N_halo-j);
     }else indj=j;  
     for(p=0;p<=global_N_halo/2;p++) {
       kk=global_dk*sqrt(indi*indi+indj*indj+p*p);	
       if(kk>0){ 
	 //Normalizacao pois a biblioteca fftw3 não tem dx nem global_dk nos integrais
	 map_in_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=I*(global_dk)*(1/(kk*kk))*map_in_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]*global_dx_halo*global_dx_halo*global_dx_halo/global_L3;  
	 map_vel_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=indi*map_in_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p];  
       }else{
	 map_vel_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=0;
       }
     }
   }
 }
 
 box_symmetriesf(map_vel_c,global_N_halo);
 
 /* Executes FFT */
 fftwf_execute(pc2r);
 
 printf("\nWriting v_x field to file...\n");fflush(0);

 sprintf(fname, "%s/Velocity/vel_x_z0_N%ld_L%d.dat", argv[1],global_N_halo,(int)(global_L)); 
 if((fid=fopen(fname,"wb"))==NULL){  
   printf("\nThe file cannot be open\n");
   return 0;
 } 
 elem=fwrite(map,sizeof(float),global_N_halo*global_N_halo*global_N_halo,fid); 
 fclose(fid);


 /********************************************************************/
  printf("\nComputing v_y field...\n");fflush(0);

#ifdef _OMPTHREAD_
#pragma omp parallel for shared(global_N_halo,global_dk,map_vel_c,map_in_c,global_dx_halo) private(i,indi,j,indj,p,kk) 
#endif
 for(i=0;i<global_N_halo;i++) {         
   if(i>global_N_halo/2) {    /* Large frequencies are equivalent to smaller negative ones */
     indi=-(global_N_halo-i);
   }else indi=i;
   for(j=0;j<global_N_halo;j++) {           
     if(j>global_N_halo/2) {  
       indj=-(global_N_halo-j);
     }else indj=j;  
     for(p=0;p<=global_N_halo/2;p++) {
       kk=global_dk*sqrt(indi*indi+indj*indj+p*p);	
       if(kk>0){ 
	 //Normalizacao pois a biblioteca fftw3 não tem dx nem global_dk nos integrais
	 map_vel_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=indj*map_in_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p];  
       }else{
	 map_vel_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=0;
       }
     }
   }
 }
 
 box_symmetriesf(map_vel_c,global_N_halo);
 
 /* Executes FFT */
 fftwf_execute(pc2r);
   
 printf("\nWriting v_y field to file...\n");fflush(0); 
 
 sprintf(fname, "%s/Velocity/vel_y_z0_N%ld_L%d.dat", argv[1],global_N_halo,(int)(global_L)); 
 if((fid=fopen(fname,"wb"))==NULL){  
   printf("\nThe file cannot be open\n");
   return 0;
 } 
 elem=fwrite(map,sizeof(float),global_N_halo*global_N_halo*global_N_halo,fid); 
 fclose(fid);


 /********************************************************************/
 printf("\nComputing v_z field...\n");fflush(0);

#ifdef _OMPTHREAD_
#pragma omp parallel for shared(global_N_halo,global_dk,map_vel_c,map_in_c,global_dx_halo) private(i,indi,j,indj,p,kk) 
#endif
 for(i=0;i<global_N_halo;i++) {         
   if(i>global_N_halo/2) {    /* Large frequencies are equivalent to smaller negative ones */
     indi=-(global_N_halo-i);
   }else indi=i;
   for(j=0;j<global_N_halo;j++) {           
     if(j>global_N_halo/2) {  
       indj=-(global_N_halo-j);
     }else indj=j;  
     for(p=0;p<=global_N_halo/2;p++) {
       kk=global_dk*sqrt(indi*indi+indj*indj+p*p);	
       if(kk>0){ 
	 //Normalizacao pois a biblioteca fftw3 não tem dx nem global_dk nos integrais
	 map_vel_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=p*map_in_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p];  
       }else{
	 map_vel_c[i*global_N_halo*(global_N_halo/2+1)+j*(global_N_halo/2+1)+p]=0;
       }
     }
   }
 }
 
 box_symmetriesf(map_vel_c,global_N_halo);
 
 /* Executes FFT */
 fftwf_execute(pc2r);
   
 printf("\nWriting v_z field to file...\n");fflush(0);
 
 sprintf(fname, "%s/Velocity/vel_z_z0_N%ld_L%d.dat", argv[1],global_N_halo,(int)(global_L)); 
 if((fid=fopen(fname,"wb"))==NULL){  
   printf("\nThe file cannot be open\n");
   return 0;
 } 
 elem=fwrite(map,sizeof(float),global_N_halo*global_N_halo*global_N_halo,fid); 
 fclose(fid);
 
 fftwf_free(map);
 fftwf_free(map_in_c);
 fftwf_free(map_vel_c);
 fftwf_destroy_plan(pc2r);
 fftwf_destroy_plan(pr2c);

 
 exit(0);    


}
示例#3
0
int main(int argc, char * argv[]) {


  fftw_plan pc2r;
  fftw_plan pr2c;
  fftw_complex *dvdk;
  DIR* dir;
  float *temp;
  char fname[256];
  FILE *fid,*fidtxt, *fidtxt2;
  double *t21, *dvdr; 
  long int i,j,p,indi,indj;
  int nmaxcut,nmincut;
  double aver,Tcmb,kk,growth,dgdt,Hubz,TS,xtot;
  double zmin,zmax,dz,z;


  /* Check for correct number of parameters*/
  if(argc != 2) {
    printf("Usage : t21 base_dir\n");
    exit(1);
  }
  get_Simfast21_params(argv[1]);
  if(global_use_Lya_xrays==0) printf("Lya and xray use set to false - assuming TS>>TCMB in t21 calculation.\n");
  zmin=global_Zminsim;
  zmax=global_Zmaxsim;
  dz=global_Dzsim;

#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  fftw_init_threads();
  fftw_plan_with_nthreads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif
 

  /* Create directory deltaTb */
  sprintf(fname,"%s/deltaTb",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating t21 directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }

  sprintf(fname,"%s/Output_text_files/t21_av_N%ld_L%.1f.dat",argv[1],global_N_smooth,global_L/global_hubble); 
  if((fidtxt=fopen(fname,"a"))==NULL){
    printf("\nError opening output %s file...\n",fname); 
    exit(1);
  }  
  sprintf(fname,"%s/Output_text_files/TS_av_N%ld_L%.1f.dat",argv[1],global_N_smooth,global_L/global_hubble); 
  if((fidtxt2=fopen(fname,"a"))==NULL){
    printf("\nError opening output %s file...\n",fname); 
    exit(1);
  }  
  if(!(temp=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }
  if(!(t21=(double *) malloc(global_N3_smooth*sizeof(double)))) {
    printf("Problem...\n");
    exit(1);
  }
  if(!(dvdk = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*global_N_smooth*global_N_smooth*(global_N_smooth/2+1)))) {  
    printf("Problem...\n");
    exit(1);
  } 
  if(!(dvdr=(double *) fftw_malloc(global_N3_smooth*sizeof(double)))) {  
    printf("Problem...\n");
    exit(1);
  }
  if(!(pr2c=fftw_plan_dft_r2c_3d(global_N_smooth, global_N_smooth, global_N_smooth, dvdr , dvdk, FFTW_MEASURE))) { 
    printf("Problem...\n");
    exit(1);
  }
  if(!(pc2r=fftw_plan_dft_c2r_3d(global_N_smooth, global_N_smooth, global_N_smooth, dvdk, dvdr, FFTW_MEASURE))) { 
    printf("Problem...\n");
    exit(1);
  }

  /**************************************************/
  /************** redshift cycle ********************/
  /**************************************************/
  for(z=zmax;z>(zmin-dz/10);z-=dz){    
    printf("z = %f\n",z);fflush(0);
    sprintf(fname,"%s/deltaTb/deltaTb_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
    if((fid = fopen(fname,"rb"))!=NULL) {
      printf("File:%s already exists - skipping this redshift...\n",fname);
      fclose(fid);
    }else {      
      if(z>(global_Zminsfr-global_Dzsim/10) && global_use_Lya_xrays==1) {
	Tcmb=Tcmb0*(1.+z);
	sprintf(fname,"%s/x_c/xc_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
	if((fid = fopen(fname,"rb"))==NULL) {
	  printf("Error opening file:%s\n",fname);
	  exit(1);
	}
	fread(temp,sizeof(float),global_N3_smooth,fid);
	fclose(fid);
	for(i=0;i<global_N3_smooth;i++) t21[i]=(double)temp[i];
	sprintf(fname,"%s/Lya/xalpha_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
	if((fid = fopen(fname,"rb"))==NULL) {
	  printf("Error opening file:%s\n",fname);
	  exit(1);
	}
	fread(temp,sizeof(float),global_N3_smooth,fid);
	fclose(fid);
	aver=0.;
	for(i=0;i<global_N3_smooth;i++) {
	  xtot=(double)temp[i]+t21[i];
	  t21[i]=xtot/(1.+xtot);
	}
	
	sprintf(fname,"%s/xrays/TempX_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
	if((fid = fopen(fname,"rb"))==NULL) {
	  printf("Error opening file:%s\n",fname);
	  exit(1);
	}

	fread(temp,sizeof(float),global_N3_smooth,fid);
	fclose(fid);
	
	TS=0.;
	for(i=0;i<global_N3_smooth;i++) {
	
	  t21[i]=t21[i]*(1.-Tcmb/(double)temp[i]); // Temperature correction for high redshifts
	  TS+=Tcmb/(1.-t21[i]);
	}
      }else {
	for(i=0;i<global_N3_smooth;i++) t21[i]=1.0;
      }
      sprintf(fname, "%s/delta/deltanl_z%.3f_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble); 
      fid=fopen(fname,"rb");
      if (fid==NULL) {printf("Error reading deltanl file... Check path or if the file exists..."); exit (1);}
      fread(temp,sizeof(float),global_N3_smooth,fid);
      fclose(fid);
      for(i=0;i<global_N3_smooth;i++) dvdr[i]=(double)temp[i];
      /* Executes FFT */
      fftw_execute(pr2c);
      /********************************************************************/
      printf("Computing v field...\n"); 
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(dvdk,global_dk) private(i,indi,j,indj,p,kk) 
#endif
      for(i=0;i<global_N_smooth;i++) {         
	if(i>global_N_smooth/2) {    /* Large frequencies are equivalent to smaller negative ones */
	  indi=-(global_N_smooth-i);
	}else indi=i;
	for(j=0;j<global_N_smooth;j++) {           
	  if(j>global_N_smooth/2) {  
	    indj=-(global_N_smooth-j);
	  }else indj=j;  
	  for(p=0;p<=global_N_smooth/2;p++) {
	    if(i==0 && j==0 && p==0) {
	      dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=0.;
	    }else {
	      kk=global_dk*sqrt(indi*indi+indj*indj+p*p);	
	      if(global_vi==1) {
		dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=(global_dk*indi)*(global_dk*indi)/(kk*kk)*dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p];
	      }else if(global_vi==2) {
		dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=(global_dk*indj)*(global_dk*indj)/(kk*kk)*dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p];
	      }else {
		dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=(global_dk*p)*(global_dk*p)/(kk*kk)*dvdk[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p];
	      }
	    }
	  }
	}
      }
      /* Executes FFT */
      fftw_execute(pc2r);
      nmaxcut=0;
      nmincut=0;
      growth=getGrowth(z);
      dgdt=dgrowthdt(z);
      Hubz=Hz(z);
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(dvdr,global_L,global_dx_smooth,global_N3_smooth) private(i) 
#endif
      for(i=0;i<global_N3_smooth;i++) {
	dvdr[i]=-dgdt*dvdr[i]/global_L/global_L/global_L/Hubz*global_dx_smooth*global_dx_smooth*global_dx_smooth/growth; /* FT normalization + divide by H(z). Divide by growth to get deltanl back to z=0 */
      }
      for(i=0;i<global_N3_smooth;i++) {
	if(dvdr[i] > maxcut) {dvdr[i]=maxcut; nmaxcut++;}
	else if(dvdr[i] < mincut) {dvdr[i]=mincut; nmincut++;}
      }
      printf("nmaxcut: %d (%f %%), nmincut: %d (%f %%)\n\n",nmaxcut,100.*nmaxcut/global_N3_smooth,nmincut,100.*nmincut/global_N3_smooth);fflush(0);
     
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(t21,temp,dvdr,global_hubble,global_omega_b,global_omega_m,global_N3_smooth,z) private(i) 
#endif
      for(i=0;i<global_N3_smooth;i++) {
  	/* output in K */
	t21[i]=23.0/1000.*(1.+(double)temp[i])*t21[i]/(1.+1.*dvdr[i])*(0.7/global_hubble)*(global_omega_b*global_hubble*global_hubble/0.02)*sqrt((0.15/global_omega_m/global_hubble/global_hubble)*(1.+z)/10.);  /*Units in Kelvin */
      }
      
      sprintf(fname,"%s/Ionization/xHII_z%.3f_eff%.2lf_N%ld_L%.1f.dat",argv[1],z,global_eff,global_N_smooth,global_L/global_hubble);
      if((fid = fopen(fname,"rb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      fread(temp,sizeof(float),global_N3_smooth,fid);  
      fclose(fid);
      for(i=0;i<global_N3_smooth;i++) {
	t21[i]=t21[i]*(1.-(double)temp[i]);  // neutral fraction...
      }
      
      sprintf(fname,"%s/deltaTb/deltaTb_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
      if((fid = fopen(fname,"wb"))==NULL) {
	printf("Cannot open file:%s...\n",fname);
	exit(1);
      }
      for(i=0;i<global_N3_smooth;i++) temp[i]=(float)t21[i];
      fwrite(temp,sizeof(float),global_N3_smooth,fid);
      fclose(fid);
      aver=0.;
      for(i=0; i<global_N3_smooth; i++) aver+=t21[i];
      fprintf(fidtxt,"%f %.8E\n",z,aver/global_N3_smooth);
      if(z>(global_Zminsfr-global_Dzsim/10) && global_use_Lya_xrays==1) fprintf(fidtxt2,"%f %.8E\n",z,TS/global_N3_smooth);
    }
  } /* ends z cycle */
    
  fclose(fidtxt);
  exit(0);

}
示例#4
0
int main(int argc, char *argv[]) {
  
  char fname[300];
  FILE *fid;
  DIR* dir;
  size_t elem;
  long int ii,ij,ik, ii_c, ij_c, ik_c, a, b, c;   
  long int ncells_1D;
  long int i,j,p,indi,indj,ind;
  int flag_bub,iz;
  double redshift,tmp;
  double kk;
  double bfactor; /* value by which to divide bubble size R */
  double neutral,*xHI;
  float *halo_map, *top_hat_r, *density_map,*bubblef, *bubble;  
  fftwf_complex *halo_map_c, *top_hat_c, *collapsed_mass_c, *density_map_c, *total_mass_c, *bubble_c;
  fftwf_plan pr2c1,pr2c2,pr2c3,pr2c4,pc2r1,pc2r2,pc2r3;
  double zmin,zmax,dz;
  double R;
  
  
  if(argc != 2) {
    printf("Generates boxes with ionization fraction for a range of redshifts\n");
    printf("usage: get_HIIbubbles base_dir\n");
    printf("base_dir contains simfast21.ini and directory structure\n");
    exit(1);
  }  
  get_Simfast21_params(argv[1]);
  zmin=global_Zminsim;
  zmax=global_Zmaxsim;
  dz=global_Dzsim;
  bfactor=pow(10.0,log10(global_bubble_Rmax/global_dx_smooth)/global_bubble_Nbins);
  printf("Bubble radius ratio (bfactor): %f\n", bfactor); fflush(0);
 
#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  fftwf_init_threads();
  fftwf_plan_with_nthreads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);fflush(0);
#endif
  /* Create directory Ionization */
  sprintf(fname,"%s/Ionization",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating Ionization directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }    
  sprintf(fname,"%s/Output_text_files",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating Output_text_files directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }  
  
  
  
 /* Memory allocation - we could do some of the FFTs inline... */
 /*************************************************************/
  
  /* density_map mass */ 
  if(!(density_map=(float *) fftwf_malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem1...\n");
    exit(1);
  }
  if(!(density_map_c=(fftwf_complex *) fftwf_malloc(global_N_smooth*global_N_smooth*(global_N_smooth/2+1)*sizeof(fftwf_complex)))) {
    printf("Problem2...\n");
    exit(1);
  }
  if(!(pr2c1=fftwf_plan_dft_r2c_3d(global_N_smooth, global_N_smooth, global_N_smooth, density_map, density_map_c, FFTWflag))) { 
    printf("Problem3...\n");
    exit(1);
  }  
  /* halo_map mass */ 
  if(!(halo_map=(float *) fftwf_malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem4...\n");
    exit(1);
  }
  if(!(halo_map_c=(fftwf_complex *) fftwf_malloc(global_N_smooth*global_N_smooth*(global_N_smooth/2+1)*sizeof(fftwf_complex)))) {
    printf("Problem5...\n");
    exit(1);
  }
  if(!(pr2c2=fftwf_plan_dft_r2c_3d(global_N_smooth, global_N_smooth, global_N_smooth, halo_map, halo_map_c, FFTWflag))) { 
    printf("Problem6...\n");
    exit(1);
  }  
  /* total mass */
  if(!(total_mass_c=(fftwf_complex *) fftwf_malloc(global_N_smooth*global_N_smooth*(global_N_smooth/2+1)*sizeof(fftwf_complex)))) {
    printf("Problem7...\n");
    exit(1);
  }
  if(!(pc2r1=fftwf_plan_dft_c2r_3d(global_N_smooth, global_N_smooth, global_N_smooth, total_mass_c, density_map, FFTWflag))) { 
    printf("Problem8...\n");
    exit(1);
  }    
  /* collapsed mass */
  if(!(collapsed_mass_c=(fftwf_complex *) fftwf_malloc(global_N_smooth*global_N_smooth*(global_N_smooth/2+1)*sizeof(fftwf_complex)))) {
    printf("Problem9...\n");
    exit(1);
  }
  if(!(pc2r2=fftwf_plan_dft_c2r_3d(global_N_smooth, global_N_smooth, global_N_smooth, collapsed_mass_c, halo_map, FFTWflag))) { 
    printf("Problem10...\n");
    exit(1);
  }  
  /* top hat window */
  if(!(top_hat_r=(float *) fftwf_malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem11...\n");
    exit(1);
  }
  if(!(top_hat_c=(fftwf_complex *) fftwf_malloc(global_N_smooth*global_N_smooth*(global_N_smooth/2+1)*sizeof(fftwf_complex)))) {
    printf("Problem12...\n");
    exit(1);
  }
  if(!(pr2c3=fftwf_plan_dft_r2c_3d(global_N_smooth, global_N_smooth, global_N_smooth, top_hat_r, top_hat_c, FFTWflag))) { 
    printf("Problem13...\n");
    exit(1);
  } 
  /* bubble boxes */
  if(!(bubble=(float *) fftwf_malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem14...\n");
    exit(1);
  }
  if(!(bubble_c=(fftwf_complex *) fftwf_malloc(global_N_smooth*global_N_smooth*(global_N_smooth/2+1)*sizeof(fftwf_complex)))) {
    printf("Problem15...\n");
    exit(1);
  }
  if(!(bubblef=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem16...\n");
    exit(1);
  }
  if(!(pr2c4=fftwf_plan_dft_r2c_3d(global_N_smooth, global_N_smooth, global_N_smooth, bubble, bubble_c, FFTWflag))) { 
    printf("Problem17...\n");
    exit(1);
  } 
  if(!(pc2r3=fftwf_plan_dft_c2r_3d(global_N_smooth, global_N_smooth, global_N_smooth, bubble_c, bubble, FFTWflag))) { 
    printf("Problem18...\n");
    exit(1);
  }  
  if(!(xHI=(double *) malloc((int)((zmax-zmin)/dz+2)*sizeof(double)))) {
    printf("Problem19...\n");
    exit(1);
  }


  
  /****************************************************/
  /***************** Redshift cycle *******************/
  printf("Number of bubble sizes: %d\n",(int)((log(global_bubble_Rmax)-log(2.*global_dx_smooth))/log(bfactor)));
  printf("Redshift cycle...\n");fflush(0);
  iz=0;
  neutral=0.;
  for(redshift=zmin;redshift<(zmax+dz/10) && (neutral < xHlim);redshift+=dz){    
    printf("z = %f\n",redshift);fflush(0);

    sprintf(fname, "%s/delta/deltanl_z%.3f_N%ld_L%.1f.dat",argv[1],redshift,global_N_smooth,global_L/global_hubble); 
    fid=fopen(fname,"rb");
    if (fid==NULL) {printf("\nError reading deltanl file... Check path or if the file exists..."); exit (1);}
    elem=fread(density_map,sizeof(float),global_N3_smooth,fid);
    fclose(fid);
    
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(global_N3_smooth, density_map,global_rho_m, global_dx_smooth,bubblef) private(i)
#endif
    for(i=0;i<(global_N3_smooth);i++){
      density_map[i]=(1.0+density_map[i])*global_rho_m*global_dx_smooth*global_dx_smooth*global_dx_smooth; /* total mass in 1 cell */
      bubblef[i]=0.0;
    }
    sprintf(fname, "%s/Halos/masscoll_z%.3f_N%ld_L%.1f.dat",argv[1],redshift,global_N_smooth,global_L/global_hubble); 
    fid=fopen(fname,"rb");
    if (fid==NULL) {printf("\nError reading %s file... Check path or if the file exists...",fname); exit (1);}
    elem=fread(halo_map,sizeof(float),global_N3_smooth,fid);
    fclose(fid);

    /* Quick fill of single cells before going to bubble cycle */
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(global_N3_smooth,halo_map,density_map,global_eff,bubblef) private(i,tmp)
#endif
    for(i=0;i<global_N3_smooth;i++) {
      if(halo_map[i]>0.) {
	if(density_map[i]>0.) 
	  tmp=(double)halo_map[i]*global_eff/density_map[i];
	else tmp=1.0;
      }else tmp=0.;
      if(tmp>=1.0) bubblef[i]=1.0; else bubblef[i]=tmp;
    }
    /* FFT density and halos */
    fftwf_execute(pr2c1);    
    fftwf_execute(pr2c2);

    
    /************** going over the bubble sizes ****************/
    R=global_bubble_Rmax;    /* Maximum bubble size...*/
    while(R>=2*global_dx_smooth){ 
    
      printf("bubble radius R= %lf\n", R);fflush(0);    
      //      printf("Filtering halo and density boxes...\n");fflush(0);
    
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(collapsed_mass_c,halo_map_c,total_mass_c,density_map_c,global_N_smooth,global_dk,R) private(i,j,p,indi,indj,kk)
#endif
      for(i=0;i<global_N_smooth;i++) {
	if(i>global_N_smooth/2) {
	  indi=-(global_N_smooth-i);
	}else indi=i;      
	for(j=0;j<global_N_smooth;j++) {
	  if(j>global_N_smooth/2) {
	    indj=-(global_N_smooth-j);
	  }else indj=j;
	  for(p=0;p<=global_N_smooth/2;p++) {
	    kk=global_dk*sqrt(indi*indi+indj*indj+p*p);
	    total_mass_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=density_map_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]*W_filter(kk*R);
	    collapsed_mass_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=halo_map_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]*W_filter(kk*R);
	  }
	}
      }
      
      fftwf_execute(pc2r1);     
      fftwf_execute(pc2r2); 
      
      flag_bub=0;
      
      //      printf("Starting to find and fill bubbles...\n");fflush(0);

      /* signal center of bubbles */      
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(halo_map,density_map,bubble,global_N_smooth,global_eff,flag_bub) private(ii,ij,ik,ind)
#endif  
      for(ii=0;ii<global_N_smooth;ii++){
	for(ij=0;ij<global_N_smooth;ij++){
	  for(ik=0;ik<global_N_smooth;ik++){
	    ind=ii*global_N_smooth*global_N_smooth+ij*global_N_smooth+ik;
	    if(halo_map[ind]>0.) {
	      if(density_map[ind]>0.) { 
		if((double)halo_map[ind]/density_map[ind]>=1.0/global_eff) {
		  flag_bub=1;
		  bubble[ind]=1.0;  	     	    	    
		}else bubble[ind]=0;
	      }else {
		flag_bub=1;
		bubble[ind]=1.0;  	     	    	    
	      }
	    }else bubble[ind]=0;
	  }
	}
      }
    
      /* generate spherical window in real space for a given R */
      if(flag_bub>0){
	printf("Found bubble...\n");fflush(0);
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(top_hat_r,R,global_dx_smooth,global_N_smooth) private(i,j,p)
#endif  
	for(i=0;i<global_N_smooth;i++){
	  for(j=0;j<global_N_smooth;j++){
	    for(p=0;p<global_N_smooth;p++){ 
	      if(sqrt(i*i+j*j+p*p)*global_dx_smooth<=R || sqrt(i*i+(j-global_N_smooth)*(j-global_N_smooth)+p*p)*global_dx_smooth<=R  || sqrt(i*i+(j-global_N_smooth)*(j-global_N_smooth)+(p-global_N_smooth)*(p-global_N_smooth))*global_dx_smooth <=R || sqrt(i*i+(p-global_N_smooth)*(p-global_N_smooth)+j*j)*global_dx_smooth<=R || sqrt((i-global_N_smooth)*(i-global_N_smooth)+j*j+p*p)*global_dx_smooth<=R ||  sqrt((i-global_N_smooth)*(i-global_N_smooth)+(j-global_N_smooth)*(j-global_N_smooth)+p*p)*global_dx_smooth<=R ||  sqrt((i-global_N_smooth)*(i-global_N_smooth)+(j-global_N_smooth)*(j-global_N_smooth)+(p-global_N_smooth)*(p-global_N_smooth))*global_dx_smooth<=R ||  sqrt((i-global_N_smooth)*(i-global_N_smooth)+j*j+(p-global_N_smooth)*(p-global_N_smooth))*global_dx_smooth<=R ) {
		top_hat_r[i*global_N_smooth*global_N_smooth+j*global_N_smooth+p]=1.0;
	      }else top_hat_r[i*global_N_smooth*global_N_smooth+j*global_N_smooth+p]=0.0;	  
	    }
	  }
	}
	/* FFT bubble centers and window */ 
	fftwf_execute(pr2c3);
	fftwf_execute(pr2c4);
      
	/* Make convolution */
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(bubble_c,top_hat_c,global_N_smooth) private(i)
#endif
	for(i=0;i<global_N_smooth*global_N_smooth*(global_N_smooth/2+1);i++) {
	  bubble_c[i]*=top_hat_c[i];
	}
	fftwf_execute(pc2r3);     
	
	/* after dividing by global_N3_smooth, values in bubble are between 0 (neutral)and global_N3_smooth */     
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(bubble,bubblef,global_N3_smooth) private(i)
#endif
	for (i=0; i<global_N3_smooth; i++){
	  bubble[i]/=global_N3_smooth;
	  if (bubble[i]>0.2) bubblef[i]=1.0; /* neutral should be zero */
	} 
	
      } /* ends filling out bubbles in box for R */
    
      R/=bfactor;  
    } /* ends R cycle */
 
    /* just to check smallest bubbles through older method */
    printf("Going to smaller R cycle...\n"); fflush(0);
    while(R>=global_dx_smooth){
  
      printf("bubble radius R= %lf\n", R);fflush(0); 
      flag_bub=0;
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(collapsed_mass_c,halo_map_c,total_mass_c,density_map_c,global_N_smooth,global_dx_smooth,global_dk,R) private(i,j,p,indi,indj,kk)
#endif
      for(i=0;i<global_N_smooth;i++) {
	if(i>global_N_smooth/2) {
	  indi=-(global_N_smooth-i);
	}else indi=i;      
	for(j=0;j<global_N_smooth;j++) {
	  if(j>global_N_smooth/2) {
	    indj=-(global_N_smooth-j);
	  }else indj=j;
	  for(p=0;p<=global_N_smooth/2;p++) {
	    kk=global_dk*sqrt(indi*indi+indj*indj+p*p);
	    total_mass_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=density_map_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]*W_filter(kk*R);
	    collapsed_mass_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]=halo_map_c[i*global_N_smooth*(global_N_smooth/2+1)+j*(global_N_smooth/2+1)+p]*W_filter(kk*R);
	  }
	}
      }
      fftwf_execute(pc2r1); 
      fftwf_execute(pc2r2); 
   
      /* fill smaller bubbles in box */
      ncells_1D=(long int)(R/global_dx_smooth);    
      //      printf("Starting to find and fill bubbles...\n");fflush(0);
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(halo_map,density_map,global_eff,global_N_smooth,global_dx_smooth,R,ncells_1D,bubblef,flag_bub) private(ii_c,ij_c,ik_c,ii,ij,ik,a,b,c,ind)
#endif
      for(ii_c=0;ii_c<global_N_smooth;ii_c++){
	for(ij_c=0;ij_c<global_N_smooth;ij_c++){
	  for(ik_c=0;ik_c<global_N_smooth;ik_c++){
	    ind=ii_c*global_N_smooth*global_N_smooth+ij_c*global_N_smooth+ik_c;
	    if(halo_map[ind]>0.) {
	      if(!(density_map[ind]>0.) || ((double)halo_map[ind]/density_map[ind]>=1.0/global_eff)) {
		flag_bub=1;
		for(ii=-(ncells_1D+1);ii<=ncells_1D+1;ii++){
		  a=check_borders(ii_c+ii,global_N_smooth);
		  for(ij=-(ncells_1D+1);ij<=ncells_1D+1;ij++){
		    if(sqrt(ii*ii+ij*ij)*global_dx_smooth <= R){
		      b=check_borders(ij_c+ij,global_N_smooth);
		      for(ik=-(ncells_1D+1);ik<=ncells_1D+1;ik++){
			c=check_borders(ik_c+ik,global_N_smooth);
			if(sqrt(ii*ii+ij*ij+ik*ik)*global_dx_smooth <= R){
			  bubblef[a*global_N_smooth*global_N_smooth+b*global_N_smooth+c]=1.0;  	     
			}
		      }
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
      if(flag_bub>0){printf("Found bubble...\n");fflush(0);}

      R/=bfactor;
    } /* ends small bubbles R cycle */

    neutral=0.;
    for (i=0; i<global_N3_smooth; i++){
      neutral+=1.0-bubblef[i];
    }
    neutral/=global_N3_smooth;
    printf("neutral fraction=%lf\n",neutral);fflush(0);
    xHI[iz]=neutral;
    sprintf(fname, "%s/Ionization/xHII_z%.3f_eff%.2lf_N%ld_L%.1f.dat",argv[1],redshift,global_eff,global_N_smooth,global_L/global_hubble); 
    if((fid = fopen(fname,"wb"))==NULL) {
      printf("Error opening file:%s\n",fname);
      exit(1);
    }
    elem=fwrite(bubblef,sizeof(float),global_N3_smooth,fid);  
    fclose(fid);
    iz++;
  } /* ends redshift cycle */


  /* z cycle for neutral>=xHlim */
  while(redshift<(zmax+dz/10)) {
    printf("z(>%f) = %f\n",xHlim,redshift);fflush(0);
    xHI[iz]=1.0;
    sprintf(fname, "%s/Ionization/xHII_z%.3f_eff%.2lf_N%ld_L%.1f.dat",argv[1],redshift,global_eff,global_N_smooth,global_L/global_hubble); 
    if((fid = fopen(fname,"wb"))==NULL) {
      printf("Error opening file:%s\n",fname);
      exit(1);
    }
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(bubblef,global_N3_smooth) private(i)
#endif
    for(i=0;i<global_N3_smooth;i++) bubblef[i]=0.0;
    elem=fwrite(bubblef,sizeof(float),global_N3_smooth,fid);  
    fclose(fid);
    iz++;
    redshift+=dz;
  }    

  sprintf(fname, "%s/Output_text_files/zsim.txt",argv[1]);
  if((fid = fopen(fname,"a"))==NULL) {
    printf("Error opening file:%s\n",fname);
    exit(1);
  }
  for(redshift=zmax;redshift>(zmin-dz/10);redshift-=dz) fprintf(fid,"%f\n",redshift); /* first line should be highest redshift */
  fclose(fid);
  sprintf(fname, "%s/Output_text_files/x_HI_eff%.2lf_N%ld_L%.1f.dat",argv[1],global_eff,global_N_smooth,global_L/global_hubble);
  if((fid = fopen(fname,"a"))==NULL) {
    printf("Error opening file:%s\n",fname);
    exit(1);
  }
  for(i=iz-1;i>=0;i--) fprintf(fid,"%lf\n",xHI[i]); /* first line should be highest redshift */
  fclose(fid);
  
  free(xHI);
  free(bubblef);
  fftwf_free(top_hat_r);
  fftwf_free(top_hat_c);
  fftwf_free(collapsed_mass_c);
  fftwf_free(density_map);
  fftwf_free(density_map_c);
  fftwf_free(halo_map);
  fftwf_free(halo_map_c);
  fftwf_free(total_mass_c);
  fftwf_free(bubble);
  fftwf_free(bubble_c);


  exit(0);
}
示例#5
0
int main(int argc, char **argv){

  
  FILE *fid;
  long int x,y,z;
  long int nhalos;
  float *halo_map;
  Halo_t *halo_v;
  size_t elem;
  char fname[300];
  double mass;
  long int index, i;
  double Mmin, Mmax, cat_Mmin, cat_Mmax, bin_Mmin, bin_Mmax; 
  double ntot,sim_Mmax,sim_Mmin, R_lim, R, halo_mass;
  double rhalo;

  if(argc!=6) {
    printf("\nCalculates halo number density fluctuations in a box for a given mass range. Ouputs a box of NxNxN floats.\n");
    printf("usage: get_halo_deltan   work_dir   halo_catalog_file   Mmin   Mmax  output_box_file\n");
    printf("Halo catalog in Simfast21 format. Mmin and Mmax in Msun units\n");
    printf("Note: make sure that Mmin and Mmax is within the mass range of the halo finding algorithm!\n\n");
    exit(1);
  }  

  get_Simfast21_params(argv[1]);

#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif


  if(global_halo_Rmin_dx < 2) {
    printf("Warning: \"halo_Rmin_dx\" is quite small - this might have resolution effects on the standard halo finding excursion set formalism\n");
    R_lim=0.620350491*global_dx_halo;
  }else {
    R_lim=global_halo_Rmin_dx*global_dx_halo;
  }
  halo_mass=(4.0/3.0)*PI*global_rho_m*pow(R_lim,3);
  printf("Minimum mass for standard halo finding method (excursion set formalism): %E\n",halo_mass);
  if(global_use_sgrid==1){
    if(halo_mass <= global_halo_Mmin+10.) {
      printf("No need to do subgridding: resolution is enough for %E mass halos\n",global_halo_Mmin);
      global_use_sgrid=0;
      R_lim=pow(3./4/PI/global_rho_m*global_halo_Mmin,1./3);
    }
  }else {
    if(halo_mass > global_halo_Mmin) {
      printf("Warning - minimum mass for simulation is larger than halo_Mmin: you need to use subgrid.\n");
    } else R_lim=pow(3./4/PI/global_rho_m*global_halo_Mmin,1./3);
  }
  if(global_use_sgrid==1)
    R=pow(3./4/PI/global_rho_m*global_halo_Mmin,1./3);
  else
    R=R_lim;
  rhalo=exp(log(global_halo_Rmax/R)/global_Nhbins);


  
  Mmin=atof(argv[3]);
  Mmax=atof(argv[4]);
  sprintf(fname, "%s/Halos/%s",argv[1],argv[2]);
  sim_Mmax=(4.0/3.0)*PI*global_rho_m*pow(global_halo_Rmax,3);
  sim_Mmin=(4.0/3.0)*PI*global_rho_m*pow(global_halo_Rmin_dx*global_dx_halo,3);
  printf("Input: %s, output: %s, Mmin: %E Msun, Mmax: %E Msun, Sim Mmin: %E Msun, Sim Mmax: %E Msun\n",fname, argv[5], Mmin, Mmax, sim_Mmin, sim_Mmax);
  if(Mmin < sim_Mmin) printf("Warning: Mmin is smaller than minimum mass used in the standard halo finding method (no subgrid)\n");
  if(Mmax > sim_Mmax) printf("Warning: Mmax is larger than largest halo mass in the simulation.\n");

  if(!(halo_map=(float *) malloc(global_N3_halo*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }
      
    /* read halo catalog */
    if((fid=fopen(fname,"rb"))==NULL){  
      printf("Halo file: %s does not exist... Check path or run get_halos for this configuration\n",fname);
      exit(1);
    } 
    elem=fread(&nhalos,sizeof(long int),1,fid);
    printf("Reading %ld halos...\n",nhalos);fflush(0);
    if(!(halo_v=(Halo_t *) malloc(nhalos*sizeof(Halo_t)))) { 
      printf("Memory problem - halos...\n");
      exit(1);
    }
    elem=fread(halo_v,sizeof(Halo_t),nhalos,fid);  
    fclose(fid);
      
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(halo_map,global_N3_halo) private(i)
#endif
    for(i=0;i<global_N3_halo;i++)  halo_map[i]=0.0;

    printf("Searching through halo catalog...\n");fflush(0);
    ntot=0.0;
    cat_Mmin=(double)halo_v[0].Mass;
    cat_Mmax=(double)halo_v[0].Mass;
    bin_Mmin=1.0e20;
    bin_Mmax=0.0;
    for(i=0;i<nhalos;i++){
      mass=(double)halo_v[i].Mass;
      if(mass > cat_Mmax) cat_Mmax=mass;
      if(mass < cat_Mmin) cat_Mmin=mass;
      //      printf("halo mass: %E\n",mass);
      if(mass>=Mmin && mass <= Mmax) {	
	if(mass > bin_Mmax) bin_Mmax=mass;
	if(mass < bin_Mmin) bin_Mmin=mass;
	x= halo_v[i].x;     
	y= halo_v[i].y;  
	z= halo_v[i].z;    
	index=(long int)(x*global_N_halo*global_N_halo+y*global_N_halo+z);
	halo_map[index]+=1.0;
	ntot+=1.0;
      }
    }
    if((long int)ntot==0) {
      printf("No halos found in mass range. Exiting...\n");
      exit(1);
    }
    bin_Mmax=bin_Mmax*pow(rhalo,3); /* adjust mass interval due to mass resolution */
    printf("Catalogue Mmin: %E Msun, Catalogue Mmax: %E Msun\n",cat_Mmin, cat_Mmax);
    printf("Catalogue Mmin in given mass range: %E Msun, Catalogue Mmax in given mass range: %E Msun\n",bin_Mmin, bin_Mmax);
    printf("Total number of halos in mass range: %ld, average number of halos per cell: %E\n",(long int)ntot, ntot/global_N3_halo);
    printf("Number density: %E (h/Mpc)^3, dn/dm for given mass range: %E (h/Mpc)^3/Msun\n",ntot/global_L3,ntot/global_L3/(bin_Mmax-bin_Mmin)); fflush(0);

#ifdef _OMPTHREAD_
#pragma omp parallel for shared(halo_map,global_N3_halo) private(i)
#endif
    for(i=0;i<global_N3_halo;i++)  halo_map[i]=halo_map[i]/(ntot/global_N3_halo)-1.0;
    
    printf("Writing file...\n"); fflush(0);
    sprintf(fname, "%s",argv[5]); 
    if((fid=fopen(fname,"wb"))==NULL){  
      printf("\nError opening output box\n");
      return 0;
    }
    elem=fwrite(halo_map,sizeof(float),global_N3_halo,fid);
    fclose(fid);
   
 exit(0);    
 
}
示例#6
0
文件: xc.c 项目: mariogrs/Simfast21
int main(int argc, char * argv[]) {


  DIR* dir;
  double nH_bar0, nHe_bar0,nH_bar, nHe_bar,xc_HI,xc_e,aver,Tcmb;
  float *xc, *temp, *fHII, *density_map;
  char fname[256];
  FILE * fid,*fidtxt;
  double * zbox; 
  long int i;
  int nzsfr,nzbox;
  double xHII;

  /* Check for correct number of parameters*/
  if (argc != 2) {
    printf("Usage : xc base_dir\n");
    exit(1);
  }
  get_Simfast21_params(argv[1]);
  if(global_use_Lya_xrays==0) {printf("Lya and xray use set to false - no need to calculate xc\n");exit(0);}

#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif
 

  /* Create directory xc */
  sprintf(fname,"%s/x_c",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating xc directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }

  nzsfr=(int)((global_Zmaxsim-global_Zminsfr)/global_Dzsim)+1;
  zbox = (double*) malloc(sizeof(double)*nzsfr);

  sprintf(fname,"%s/Output_text_files/zsim.txt",argv[1]);
  if((fid = fopen(fname,"r"))==NULL) {
    printf("Error opening file:%s\n",fname);
    exit(1);
  }
  for (i=0;i<nzsfr;i++) 
    if(fscanf(fid,"%lf",&(zbox[nzsfr-1-i]))!=1) {    // asume first line is highest z
      printf("Error reading zsim.txt!\n");
      exit(1);
    }
  fclose(fid);    
  sprintf(fname,"%s/Output_text_files/xc_av_N%ld_L%.1f.dat",argv[1],global_N_smooth,global_L/global_hubble); 
  if((fidtxt=fopen(fname,"a"))==NULL){
    printf("\nError opening output %s file...\n",fname); 
    exit(1);
  }  
  if(!(density_map=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }
  if(!(fHII=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }
  if(!(temp=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }
  if(!(xc=(float *) malloc(global_N3_smooth*sizeof(float)))) {
    printf("Problem...\n");
    exit(1);
  }
  nH_bar0=(1.-YHe)*(global_rho_b*Msun/pow(Mpc2m*100./global_hubble,3))/mH; /* cm^-3 */
  nHe_bar0=(YHe)*(global_rho_b*Msun/pow(Mpc2m*100./global_hubble,3))/mHe;  /* cm^-3 */
  printf("nH0: %.6E  nHe0: %.6E\n",nH_bar0,nHe_bar0);

  /**************************************************/
  /************** redshift cycle ********************/
  /**************************************************/
  for(nzbox=nzsfr-1;nzbox >= 0;nzbox--) {

    /* our box is at nzbox */
    printf("ztocompute: %f\n",zbox[nzbox]);fflush(0);
    sprintf(fname,"%s/x_c/xc_z%.3f_N%ld_L%.1f.dat",argv[1],zbox[nzbox],global_N_smooth,global_L/global_hubble);
    if((fid = fopen(fname,"rb"))!=NULL) {
      printf("File:%s already exists - skipping this redshift...\n",fname);
      fclose(fid);
    }else {
      sprintf(fname,"%s/Ionization/xHII_z%.3f_N%ld_L%.1f.dat",argv[1],zbox[nzbox],global_N_smooth,global_L/global_hubble);
      if((fid = fopen(fname,"rb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      fread(fHII,sizeof(float),global_N3_smooth,fid);  
      fclose(fid);
      sprintf(fname, "%s/delta/deltanl_z%.3f_N%ld_L%.1f.dat",argv[1],zbox[nzbox],global_N_smooth,global_L/global_hubble); 
      fid=fopen(fname,"rb");
      if (fid==NULL) {printf("\nError reading deltanl file... Check path or if the file exists..."); exit (1);}
      fread(density_map,sizeof(float),global_N3_smooth,fid);
      fclose(fid);
      sprintf(fname,"%s/xrays/TempX_z%.3f_N%ld_L%.1f.dat",argv[1],zbox[nzbox],global_N_smooth,global_L/global_hubble);
      if((fid = fopen(fname,"rb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      fread(temp,sizeof(float),global_N3_smooth,fid);
      fclose(fid);
      
      nH_bar=nH_bar0*pow(1.+zbox[nzbox],3);
      nHe_bar=nHe_bar0*pow(1.+zbox[nzbox],3);         
      Tcmb=Tcmb0*(1.+zbox[nzbox]);
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(xc,global_N3_smooth,fHII,density_map,temp,Tcmb,nH_bar,nHe_bar) private(i,xc_HI,xc_e) 
#endif
      for(i=0; i<global_N3_smooth; i++) {
	xHII=(double)fHII[i];
	if(xHII < 0.99999) 
	  xc_HI=4./3*(double)(1.-xHII)*nH_bar*(1.+(double)density_map[i])*kappa_HI(temp[i])/A10*Tstar/Tcmb;
	else xc_HI=0.;
	if(xHII>0.00001) 
	  xc_e=4./3*((double)xHII*nH_bar+nHe_bar*(double)xHII)*(1.+(double)density_map[i])*kappa_e(temp[i])/A10*Tstar/Tcmb;
	else xc_e=0.;
	//    xc_e=((1-(double)fHI[i])*nH_bar+nHe_bar*(2.-2.*fHeI[i]-fHeII[i]))*(double)rho_mat[i]*kappa_e(temp)/A10*Tstar/Tcmb;
	xc[i]=(float)(xc_HI+xc_e);
      }
      sprintf(fname,"%s/x_c/xc_z%.3f_N%ld_L%.1f.dat",argv[1],zbox[nzbox],global_N_smooth,global_L/global_hubble);
      if((fid = fopen(fname,"wb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      fwrite(xc,sizeof(float),global_N3_smooth,fid);
      fclose(fid);
      aver=0.;
      for(i=0; i<global_N3_smooth; i++) aver+=xc[i];
      fprintf(fidtxt,"%f %.6E\n",zbox[nzbox],aver/global_N3_smooth);
    }
  } /* ends z cycle */
    
  fclose(fidtxt);
  exit(0);

}
示例#7
0
int main(int argc,char * argv[]) {

  float *xi, *Energycumio, *Energycumioold;
  double *Energy, deltatime,Energy2io;
  FILE *filexi, *fileEnergycum, *fileEnergycumold, *fileEnergy;
  long int i,j;
  char fname[1000];
  double zmin,zmax,dz,z,xitot;


  /* Check for correct number of parameters*/
  if(argc!=2) {
    printf("Integrates xe for X ray temperature calculation\n");
    printf("usage: integratexe base_dir\n");
    printf("base_dir contains simfast21.ini\n");
    exit(1);
  }  
  get_Simfast21_params(argv[1]);
  if(global_use_Lya_xrays==0) {printf("Lya and xray use set to false - no need to calculate xrays\n");exit(0);}
  zmin=global_Zminsfr;
  zmax=global_Zmaxsim;
  dz=global_Dzsim;

#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif
  

  xi = (float*) malloc(sizeof(float)*Buffer);
  Energycumio = (float*) malloc(sizeof(float)*Buffer);
  Energy = (double*) malloc(sizeof(double)*Buffer);  /* note the double */
  Energycumioold = (float*) malloc(sizeof(float)*Buffer);
  Energy2io=13.6*EV2J;    
  
  /**************************************************/
  /************** redshift cycle ********************/
  /**************************************************/
  for(z=zmax; z >(zmin-dz/10); z-=dz) {
    printf("z: %f\n",z);fflush(0);
    sprintf(fname,"%s/xrays/xe_heat_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
    if((fileEnergycum = fopen(fname,"rb"))!=NULL) {
      printf("File:%s already exists - skipping this redshift...\n",fname);
      fclose(fileEnergycum);
    }else {
      deltatime=fdeltatime(z,z+global_Dzsim);
      //      printf("deltatime : %E seconds\n",deltatime);    
      sprintf(fname,"%s/Ionization/xHII_z%.3f_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
      if((filexi = fopen(fname,"rb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      sprintf(fname,"%s/xrays/EpsilonXon_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
      if((fileEnergy = fopen(fname,"rb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      sprintf(fname,"%s/xrays/xe_heat_z%.3lf_N%ld_L%.1f.dat",argv[1],z,global_N_smooth,global_L/global_hubble);
      if((fileEnergycum = fopen(fname,"wb"))==NULL) {
	printf("Error opening file:%s\n",fname);
	exit(1);
      }
      if (z<(zmax-dz/10)) {
	sprintf(fname,"%s/xrays/xe_heat_z%.3lf_N%ld_L%.1f.dat",argv[1],z+global_Dzsim,global_N_smooth,global_L/global_hubble);
	if((fileEnergycumold = fopen(fname,"rb"))==NULL) {
	  printf("Error opening file:%s\n",fname);
	  exit(1);
	}
	printf("Reading previous xe file\n");
      } else printf("no previous xe file\n");
      fflush(0);
      
      memset(xi,0,sizeof(float)*Buffer);
      memset(Energy,0,sizeof(double)*Buffer);
      memset(Energycumioold,0,sizeof(float)*Buffer);
      
      printf("Looping over box for z=%f\n",z);fflush(0);
      for (i=0;i<Nbufs;i++) {   
	/* xi from bubble box */
	fread(xi,sizeof(float),Buffer,filexi);
	fread(Energy,sizeof(double),Buffer,fileEnergy);  /* epsilonX over n - in double */
	if (z<(zmax-dz/10)) 
	  fread(Energycumioold,sizeof(float),Buffer,fileEnergycumold); /* read previous xe */
	/* Assume xe=0 for larger z */
	
#ifdef _OMPTHREAD_
#pragma omp parallel for shared(Energy,Energycumio,Energycumioold,xi,deltatime,Energy2io) private(j,xitot) 
#endif
	for (j=0;j<Buffer;j++) {
	  /* Add a minimum ionization state of the gas
	     due to X-ray to prevent total non-heating
	     from X-ray.
	  */
	  xitot=xi[j]+Energycumioold[j];  /* adding previous xe to make it a better approximation */
	  if(xitot>1.0) xitot=1.0;      
	  if(xitot<0.) xitot=0.0;      
	  Energycumio[j]=(float)(Energy[j]*deltatime/Energy2io*fi(xitot)+ Energycumioold[j]);
	}
	
	fwrite(Energycumio,sizeof(float),Buffer,fileEnergycum);
      }
      if (z<(zmax-dz/10)) fclose(fileEnergycumold);
      fclose(fileEnergy);
      fclose(filexi);
      fclose(fileEnergycum);
    }
  } /* ends z cycle */    
    free(xi);
    free(Energycumio);
    free(Energy);
    free(Energycumioold);
    
    exit(0);
  }
示例#8
0
int main(int argc, char **argv){

  FILE *fid;
  DIR* dir;
  int nhalos;
  long int ln;
  int ix,iy,iz,i;
  Halo_sim *halo_in;
  Halo_t *halo_out;
  char fname[300];
  size_t elem;
  double z, dx;

  if(argc!=4) {
    printf("\nConverts halo catalogues from Mellema to Simfast21 format\n");
    printf("usage: convert_halos.x  work_dir  halo_catalog_input  z\n\n");
    exit(1);
  }  

  get_Simfast21_params(argv[1]);
  print_parms();

  z=atof(argv[3]);
  sprintf(fname, "%s/Halos/halo_z%.3f_N%ld_L%.0f.dat.catalog",argv[1],z,global_N_halo,global_L);
  fid=fopen(fname,"rb");
    if(fid!=NULL) {
      printf("File:%s already exists - exiting...\n",fname); 
      exit(1);
    }

#ifdef _OMPTHREAD_
  omp_set_num_threads(global_nthreads);
  printf("Using %d threads\n",global_nthreads);
#endif

  dx=global_dx_halo/global_hubble; /* convert to Mpc */

  /* read halo catalog */
  if((fid=fopen(argv[2],"rb"))==NULL){  
    printf("Halo file: %s does not exist...\n",argv[2]);
    exit(1);
  } 
  elem=fread(&nhalos,sizeof(int),1,fid);
  printf("Reading %d halos...\n",nhalos);fflush(0);
  if(!(halo_in=(Halo_sim *) malloc(nhalos*sizeof(Halo_sim)))) { 
    printf("Memory problem - halos...\n");
    exit(1);
  }
  elem=fread(halo_in,sizeof(Halo_sim),nhalos,fid);  
  if((int)elem!=nhalos) {printf("Problem reading halos. Exiting...\n"); exit(1);}
  fclose(fid);
  if(!(halo_out=(Halo_t *) malloc(nhalos*sizeof(Halo_t)))) { 
    printf("Memory problem - halos...\n");
    exit(1);
  }
   
  printf("Converting...\n"); fflush(0);
  //#ifdef _OMPTHREAD_
  //#pragma omp parallel for shared(halo_in,halo_out) private(i)
  //#endif
  for(i=0;i<nhalos;i++)  {
    halo_out[i].Mass=pow(10.0,halo_in[i].val[10]);  /* Mass in Msun */
    halo_out[i].Radius=halo_in[i].val[9]*(1.0+z)/1000.0*global_hubble;  /* Convert from radius in proper Kpc to comoving Mpc/h*/
    ix=(int)roundf(halo_in[i].val[0]/dx);
    if(ix < 0 || ix >= global_N_halo) {
      //#pragma omp critical
      {
	printf("Error in halo index: i, ix = %d, %d. Exiting...\n",i,ix);
	exit(1);
      }
    }
    iy=(int)roundf(halo_in[i].val[1]/dx);
    if(iy < 0 || iy >= global_N_halo) {
      //#pragma omp critical
      {
	printf("Error in halo index: i, iy = %d, %d. Exiting...\n",i, iy);
	exit(1);
      }
    }
    iz=(int)roundf(halo_in[i].val[2]/dx);
    if(iz < 0 || iz >= global_N_halo) {
      //#pragma omp critical
      {
	printf("Error in halo index: i, iz = %d, %d. Exiting...\n",i, iz);
	exit(1);
      }
    }
    /* switch indexes since we're assuming x should be the slowest moving index, not the fastest as in Garrelt... */
    halo_out[i].x=iz;
    halo_out[i].y=iy;
    halo_out[i].z=ix;
  }
  
  printf("Writing...\n"); fflush(0);
  sprintf(fname,"%s/Halos",argv[1]);
  if((dir=opendir(fname))==NULL) {  
    printf("Creating Halo directory\n");
    if(mkdir(fname,(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))!=0) {
      printf("Error creating directory!\n");
      exit(1);
    }
  }  
  sprintf(fname, "%s/Halos/halo_z%.3f_N%ld_L%.0f.dat.catalog",argv[1],z,global_N_halo,global_L);
  fid=fopen(fname,"wb");
  if(fid==NULL){printf("\nCatalog file output error. Exiting...\n"); exit (1);}
  ln=(long int)nhalos;
  elem=fwrite(&ln,sizeof(long int),1,fid);
  elem=fwrite(halo_out,sizeof(Halo_t),nhalos,fid);
  if((int)elem!=nhalos) {printf("Problem writing halos. Exiting...\n"); exit(1);}
  
  exit(0);    
  
}