コード例 #1
0
ファイル: plot_cosmo.c プロジェクト: gbpoole/gbpCode
int main(int argc, char *argv[]) {
    SID_Init(&argc, &argv, NULL);

    // Parse arguments and initialize
    double z;
    if(argc < 2 || argc > 3) {
        fprintf(stderr, "\n Syntax: %s z [gbpCosmo_file.txt]\n", argv[0]);
        fprintf(stderr, " ------\n\n");
        return (SID_ERROR_SYNTAX);
    } else
        z = (double)atof(argv[1]);
    SID_log("Computing cosmology information for z=%.2lf...", SID_LOG_OPEN, z);

    // Initialize cosmology
    ADaPS *cosmo = NULL;
    if(argc == 2)
        init_cosmo_default(&cosmo);
    else if(argc == 3)
        read_gbpCosmo_file(&cosmo, argv[2]);

    // Output results
    double h_Hubble = ((double *)ADaPS_fetch(cosmo, "h_Hubble"))[0];
    SID_log("R_NL(z)      = %10.3lf Mpc", SID_LOG_COMMENT, R_NL_z(z, &cosmo) / M_PER_MPC);
    SID_log("rho_crit     = %13.6le Msol/(Mpc^3)", SID_LOG_COMMENT, rho_crit_z(z, cosmo) * (M_PER_MPC / M_SOL) * M_PER_MPC * M_PER_MPC);
    SID_log("D_angular    = %10.3lf Mpc", SID_LOG_COMMENT, D_angular(z, cosmo) / M_PER_MPC);
    SID_log("D_luminosity = %10.3lf Mpc", SID_LOG_COMMENT, D_luminosity(z, cosmo) / M_PER_MPC);
    SID_log("D_comoving   = %10.3lf Mpc", SID_LOG_COMMENT, D_comove(z, cosmo) / M_PER_MPC);
    SID_log("D_horizon    = %10.3lf Mpc", SID_LOG_COMMENT, C_VACUUM * deltat_a(&cosmo, 0., a_of_z(z)) / M_PER_MPC);
    SID_log("D_V          = %10.3lf Mpc",
            SID_LOG_COMMENT,
            pow((1 + z) * D_angular(z, cosmo) * (1 + z) * D_angular(z, cosmo) * z * C_VACUUM / H_convert(H_z(z, cosmo)), ONE_THIRD) / M_PER_MPC);
    SID_log("H(z)         = %10.3lf km/s/Mpc", SID_LOG_COMMENT, H_z(z, cosmo));
    SID_log("t_age(z)     = %10.3le years", SID_LOG_COMMENT, t_age_z(z, &cosmo) / S_PER_YEAR);
    SID_log("t_Hubble(z)  = %10.3le years", SID_LOG_COMMENT, t_Hubble_z(z, cosmo) / S_PER_YEAR);
    SID_log("t_dyn(z)     = %10.3le years", SID_LOG_COMMENT, t_dyn_z(z, cosmo) / S_PER_YEAR);
    SID_log("n_dyn(<z)    = %10.3le", SID_LOG_COMMENT, n_dyn_ztoz(0., z, cosmo));

    SID_log("Done.", SID_LOG_CLOSE);

    // Clean-up
    free_cosmo(&cosmo);
    SID_Finalize();
}
コード例 #2
0
ファイル: free_pspec.c プロジェクト: manodeep/gbpCode
void free_pspec(pspec_info *pspec){
  SID_log("Freeing power spectrum...",SID_LOG_OPEN);
  free_cosmo(&(pspec->cosmo));
  free_field(&(pspec->FFT));
  SID_free(SID_FARG (pspec->k_1D));
  SID_free(SID_FARG (pspec->n_modes_1D));
  SID_free(SID_FARG (pspec->n_modes_2D));
  int i_run;
  for(i_run=0;i_run<4;i_run++){
      SID_free(SID_FARG (pspec->P_k_1D[i_run]));
      SID_free(SID_FARG (pspec->dP_k_1D[i_run]));
      SID_free(SID_FARG (pspec->P_k_2D[i_run]));
      SID_free(SID_FARG (pspec->dP_k_2D[i_run]));
  }
  SID_free(SID_FARG pspec->P_k_1D);
  SID_free(SID_FARG pspec->dP_k_1D);
  SID_free(SID_FARG pspec->P_k_2D);
  SID_free(SID_FARG pspec->dP_k_2D);
  SID_log("Done.",SID_LOG_CLOSE);
}
コード例 #3
0
ファイル: free_trees.c プロジェクト: manodeep/gbpCode
void free_trees(tree_info **trees){

  SID_log("Freeing trees...",SID_LOG_OPEN);

  // Free reference trees
  if((*trees)->trees_reference!=NULL)
     free_trees(&((*trees)->trees_reference));

  // Free look-up arrays
  free_trees_lookup((*trees));

  // Free ADaPS structure
  ADaPS_free(SID_FARG (*trees)->data);

  // Free cosmology
  free_cosmo(&((*trees)->cosmo));

  // Free nodes
  int i_snap;
  for(i_snap=0;i_snap<(*trees)->n_snaps;i_snap++){
     tree_node_info *current;
     tree_node_info *next;
     if((*trees)->first_neighbour_groups!=NULL){
        next=(*trees)->first_neighbour_groups[i_snap];
        while(next!=NULL){
           current=next;
           next   =current->next_neighbour;
           SID_free(SID_FARG current);
        }
     }
     if((*trees)->first_neighbour_subgroups!=NULL){
        next=(*trees)->first_neighbour_subgroups[i_snap];
        while(next!=NULL){
           current=next;
           next   =current->next_neighbour;
           SID_free(SID_FARG current);
        }
     }
  }

  // Free match scores
  for(int i_type=0;i_type<2;i_type++){
     float **match_scores;
     if(i_type==0)
        match_scores=(*trees)->group_match_scores;
     else
        match_scores=(*trees)->subgroup_match_scores;
     if(match_scores!=NULL){
        for(i_snap=0;i_snap<(*trees)->n_snaps;i_snap++)
           SID_free(SID_FARG match_scores[i_snap]);
        SID_free(SID_FARG match_scores);
     }
  }

  // Free the various other arrays
  SID_free(SID_FARG (*trees)->snap_list);
  SID_free(SID_FARG (*trees)->a_list);
  SID_free(SID_FARG (*trees)->z_list);
  SID_free(SID_FARG (*trees)->t_list);
  SID_free(SID_FARG (*trees)->n_groups_catalog);
  SID_free(SID_FARG (*trees)->n_subgroups_catalog);
  SID_free(SID_FARG (*trees)->n_groups_snap_local);
  SID_free(SID_FARG (*trees)->n_subgroups_snap_local);
  SID_free(SID_FARG (*trees)->n_groups_forest_local);
  SID_free(SID_FARG (*trees)->n_subgroups_forest_local);
  SID_free(SID_FARG (*trees)->first_neighbour_groups);
  SID_free(SID_FARG (*trees)->first_neighbour_subgroups);
  SID_free(SID_FARG (*trees)->last_neighbour_groups);
  SID_free(SID_FARG (*trees)->last_neighbour_subgroups);
  SID_free(SID_FARG (*trees)->first_in_forest_groups);
  SID_free(SID_FARG (*trees)->first_in_forest_subgroups);
  SID_free(SID_FARG (*trees)->last_in_forest_groups);
  SID_free(SID_FARG (*trees)->last_in_forest_subgroups);
  SID_free(SID_FARG (*trees)->tree2forest_mapping_group);
  SID_free(SID_FARG (*trees)->tree2forest_mapping_subgroup);

  // Free the main structure
  SID_free(SID_FARG (*trees));

  SID_log("Done.",SID_LOG_CLOSE);
}
コード例 #4
0
ファイル: make_mass_function.c プロジェクト: webbjj/gbpCode
int main(int argc, char *argv[]){

  SID_init(&argc,&argv,NULL,NULL);

  char   filename_cosmology[MAX_FILENAME_LENGTH];
  char   paramterization[MAX_FILENAME_LENGTH];
  double log_M_min    =atof(argv[1]);
  double log_M_max    =atof(argv[2]);
  int    n_M_bins     =atoi(argv[3]);
  double redshift     =atof(argv[4]);
  strcpy(filename_cosmology,argv[5]);
  strcpy(paramterization,   argv[6]);

  SID_log("Constructing mass function between log(M)=%5.3lf->%5.3lf at z=%5.3lf...",SID_LOG_OPEN,log_M_min,log_M_max,redshift);

  // Initialize cosmology
  cosmo_info *cosmo=NULL;
  read_gbpCosmo_file(&cosmo,filename_cosmology);

  // Decide which parameterization we are going to use
  int  select_flag;
  char mfn_text[32];
  if(!strcmp(paramterization,"JENKINS")){
     sprintf(mfn_text,"Jenkins");
     select_flag=MF_JENKINS;
  }
  else if(!strcmp(paramterization,"PS")){
     sprintf(mfn_text,"Press & Schechter");
     select_flag=MF_PS;
  }
  else if(!strcmp(paramterization,"ST")){
     sprintf(mfn_text,"Sheth & Tormen");
     select_flag=MF_ST;
  }
  else
     SID_trap_error("Invalid parameterization selected {%s}.  Should be {JENKINS,PS or ST}.",ERROR_SYNTAX,paramterization);

  // Create output filename
  char  filename_out[MAX_FILENAME_LENGTH];
  char  redshift_text[64];
  char *cosmology_name=(char *)ADaPS_fetch(cosmo,"name");
  float_to_text(redshift,3,redshift_text);
  sprintf(filename_out,"mass_function_z%s_%s_%s.txt",redshift_text,cosmology_name,paramterization);

  // Open file and write header
  FILE *fp_out=NULL;
  fp_out=fopen(filename_out,"w");
  int i_column=1;
  fprintf(fp_out,"# Mass function (%s) for %s cosmology at z=%lf\n",mfn_text,filename_cosmology,redshift);
  fprintf(fp_out,"# \n");
  fprintf(fp_out,"# Column (%02d): log M [h^-1 M_sol]\n",                              i_column++);
  fprintf(fp_out,"#        (%02d): Mass function [(h^{-1} Mpc]^{-3} per dlogM]\n",     i_column++);
  fprintf(fp_out,"#        (%02d): Cumulative Mass function(>M) [(h^{-1} Mpc]^{-3}]\n",i_column++);

  // Create the mass function
  SID_log("Writing results to {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_out);
  pcounter_info pcounter;
  SID_init_pcounter(&pcounter,n_M_bins,10);
  double h_Hubble   =((double *)ADaPS_fetch(cosmo,"h_Hubble"))[0];
  double mass_factor=M_SOL/h_Hubble;
  double MFct_factor=pow(M_PER_MPC,3.0);
  for(int i_bin=0;i_bin<n_M_bins;i_bin++){
     double log_M;
     if(i_bin==0)
        log_M=log_M_min;
     else if(i_bin==(n_M_bins-1))
        log_M=log_M_max;
     else
        log_M=log_M_min+(((double)(i_bin))/((double)(n_M_bins-1)))*(log_M_max-log_M_min);
     fprintf(fp_out,"%le %le %le\n",log_M,
                                    MFct_factor*mass_function           (mass_factor*take_alog10(log_M),redshift,&cosmo,select_flag),
                                    MFct_factor*mass_function_cumulative(mass_factor*take_alog10(log_M),redshift,&cosmo,select_flag));
     SID_check_pcounter(&pcounter,i_bin);
  }
  fclose(fp_out);
  SID_log("Done.",SID_LOG_CLOSE);

  // Clean-up
  free_cosmo(&cosmo);

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
コード例 #5
0
ファイル: plot_clustering.c プロジェクト: webbjj/gbpCode
int main(int argc, char *argv[]){

  SID_init(&argc,&argv,NULL,NULL);

  // Parse arguments and initialize
  double z;
  if(argc<2 || argc>3){
    fprintf(stderr,"\n Syntax: %s z [gbpCosmo_file.txt]\n",argv[0]);
    fprintf(stderr," ------\n\n");
    return(ERROR_SYNTAX);
  }
  else
    z=(double)atof(argv[1]);

  SID_log("Computing clustering information for z=%.2lf...",SID_LOG_OPEN,z);

  // Initialize cosmology
  cosmo_info *cosmo=NULL;
  if(argc==2)
     init_cosmo_default(&cosmo);
  else if(argc==3)
     read_gbpCosmo_file(&cosmo,argv[2]);

  // Initialize
  int     mode     =PSPEC_LINEAR_TF;
  int     component=PSPEC_ALL_MATTER;
  init_sigma_M(&cosmo,z,mode,component);
  int     n_k     =((int    *)ADaPS_fetch(cosmo,"n_k"))[0];
  double *lk_P    = (double *)ADaPS_fetch(cosmo,"lk_P");
  double  h_Hubble=((double *)ADaPS_fetch(cosmo,"h_Hubble"))[0];
  SID_log("Done.",SID_LOG_CLOSE);

  // Generate file
  SID_log("Writing table to stdout...",SID_LOG_OPEN);
  double delta_c    =1.686;
  double m_per_mpc_h=M_PER_MPC/h_Hubble;
  printf("# Column (01): k [h Mpc^-1]\n");
  printf("#        (02): R [h^-1 Mpc] \n");
  printf("#        (03): M [h^-1 M_sol]\n");
  printf("#        (04): V_max [km/s] \n");
  printf("#        (05): P_k [(h^-1 Mpc)^3]\n");
  printf("#        (06): sigma\n");
  printf("#        (07): nu (peak height)\n");
  printf("#        (08): b_BPR\n");
  printf("#        (09): b_TRK\n");
  printf("#        (10): z-space boost Kaiser '87 (applied to b_TRK)\n");
  printf("#        (11): b_TRK total (w/ Kaiser boost)\n");
  printf("#        (12): b_halo_Poole \n");
  printf("#        (13): z-space boost Poole \n");
  printf("#        (14): b_total_Poole \n");
  printf("#        (15): b_halo_Poole        (substructure)\n");
  printf("#        (16): z-space boost Poole (substructure)\n");
  printf("#        (17): b_total_Poole       (substructure)\n");
  for(int i_k=0;i_k<n_k;i_k++){
     double k_P  =take_alog10(lk_P[i_k]);
     double R_P  =R_of_k(k_P);
     double M_R  =M_of_k(k_P,z,cosmo);
     double P_k  =power_spectrum(k_P,z,&cosmo,mode,component);
     double sigma=sqrt(power_spectrum_variance(k_P,z,&cosmo,mode,component));
     double V_max=V_max_NFW(M_R,z,NFW_MODE_DEFAULT,&cosmo);
     double nu   =delta_c/sigma;
     double bias =1.;
     if(M_R<1e16*M_SOL){
        printf("%10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le %10.5le\n",
               k_P*m_per_mpc_h,
               R_P/m_per_mpc_h,
               M_R/(M_SOL/h_Hubble),
               V_max*1e-3,
               P_k/pow(m_per_mpc_h,3.),
               sigma,
               nu,
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_BPR),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_TRK),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_TRK|BIAS_MODEL_KAISER_BOOST),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_TRK|BIAS_MODEL_KAISER),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_HALO),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_ZSPACE),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_TOTAL),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_SUBSTRUCTURE|BIAS_MODEL_POOLE_HALO),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_SUBSTRUCTURE|BIAS_MODEL_POOLE_ZSPACE),
               bias_model(M_R,delta_c,z,&cosmo,BIAS_MODEL_POOLE_SUBSTRUCTURE|BIAS_MODEL_POOLE_TOTAL));
     }
  }
  SID_log("Done.",SID_LOG_CLOSE);

  // Clean-up
  free_cosmo(&cosmo);

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
コード例 #6
0
int main(int argc, char *argv[]) {
    SID_Init(&argc, &argv, NULL);

    // Parse arguments
    char filename_cosmo[SID_MAX_FILENAME_LENGTH];
    char filename_list_in[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_cosmo, argv[1]);
    strcpy(filename_list_in, argv[2]);

    // Read input list
    char *  line        = NULL;
    size_t  line_length = 0;
    FILE *  fp_in       = fopen(filename_list_in, "r");
    int     n_list      = count_lines_data(fp_in);
    double *a_list      = (double *)SID_malloc(sizeof(double) * n_list);
    for(int i_list = 0; i_list < n_list; i_list++) {
        grab_next_line_data(fp_in, &line, &line_length);
        grab_double(line, 1, &(a_list[i_list]));
    }
    SID_free(SID_FARG line);

    double a_lo_table = a_list[0];
    double a_hi_table = a_list[n_list - 1];
    for(int i_list = 0; i_list < n_list; i_list++) {
        a_lo_table = GBP_MIN(a_lo_table, a_list[i_list]);
        a_hi_table = GBP_MAX(a_hi_table, a_list[i_list]);
    }
    a_lo_table = GBP_MIN(a_lo_table, 1e-4);

    SID_log("Creating extended snapshot list...", SID_LOG_OPEN);

    // Initialize cosmology
    cosmo_info *cosmo = NULL;
    read_gbpCosmo_file(&cosmo, filename_cosmo);

    // Creeate interpolation tables
    int     n_table     = 500;
    double *a_table     = (double *)SID_malloc(n_table * sizeof(double));
    double *t_table     = (double *)SID_malloc(n_table * sizeof(double));
    double *n_dyn_table = (double *)SID_malloc(n_table * sizeof(double));
    double  da_table    = (a_hi_table - a_lo_table) / (double)(n_table - 1);
    for(int i_table = 0; i_table < n_table; i_table++) {
        // Expansion factor
        if(i_table == 0)
            a_table[i_table] = a_lo_table;
        else if(i_table == (n_table - 1))
            a_table[i_table] = a_hi_table;
        else
            a_table[i_table] = a_table[0] + da_table * (double)i_table;
        // Cosmic time
        t_table[i_table] = t_age_a(a_table[i_table], &cosmo);
        // Number of dynamical times
        if(i_table == 0)
            n_dyn_table[i_table] = 0.;
        else
            n_dyn_table[i_table] = delta_n_dyn(a_table[0], a_table[i_table], &cosmo);
    }
    interp_info *n_of_a_interp = NULL;
    init_interpolate(a_table, n_dyn_table, n_table, gsl_interp_akima, &n_of_a_interp);

    // Generate desired table
    double a_last   = 0.;
    double z_last   = 0.;
    double t_last   = 0.;
    double n_last   = 0.;
    double a_i      = 0.;
    double z_i      = 1.e22;
    double t_i      = 0.;
    double n_i      = 0.;
    double da       = 0.;
    double dz       = 0.;
    double dt       = 0.;
    double dn       = 0.;
    int    i_column = 1;
    printf("# Table of times, etc. generated from expansion factors in {%s}\n", filename_list_in);
    printf("# Column (%02d): snapshot number\n", i_column++);
    printf("#        (%02d): a:=expansion factor\n", i_column++);
    printf("#        (%02d): da\n", i_column++);
    printf("#        (%02d): z:=redshift\n", i_column++);
    printf("#        (%02d): dz\n", i_column++);
    printf("#        (%02d): t:=cosmic time  [years]\n", i_column++);
    printf("#        (%02d): dt\n", i_column++);
    printf("#        (%02d): n_dyn:=No. of dynamical times\n", i_column++);
    printf("#        (%02d): dn_dyn\n", i_column++);
    for(int i_list = 0; i_list < n_list; i_list++) {
        a_last = a_i;
        z_last = z_i;
        t_last = t_i;
        n_last = n_i;
        a_i    = a_list[i_list];
        z_i    = z_of_a(a_i);
        t_i    = t_age_a(a_i, &cosmo) / S_PER_YEAR;
        n_i    = interpolate(n_of_a_interp, a_i);
        da     = a_i - a_last;
        dz     = fabs(z_i - z_last);
        dt     = t_i - t_last;
        dn     = n_i - n_last;
        printf("%03d %le %le %le %le %le %le %le %le\n", i_list, a_i, da, z_i, dz, t_i, dt, n_i, dn);
    }

    // Clean-up
    SID_free(SID_FARG a_list);
    SID_free(SID_FARG a_table);
    SID_free(SID_FARG t_table);
    SID_free(SID_FARG n_dyn_table);
    free_interpolate(SID_FARG n_of_a_interp, NULL);
    free_cosmo(&cosmo);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
コード例 #7
0
ファイル: make_gadget_grid.c プロジェクト: manodeep/gbpCode
int main(int argc, char *argv[]){
  int     n_species;
  int     n_load;
  int     n_used;
  int     flag_used[N_GADGET_TYPE];
  char    species_name[256];
  double  h_Hubble;
  double  n_spec;
  double  redshift;
  int     i_species;
  char    n_string[64];
  int             n[3];
  double          L[3];
  FILE           *fp_1D;
  FILE           *fp_2D;
  cosmo_info     *cosmo;
  field_info     *field[N_GADGET_TYPE];
  field_info     *field_norm[N_GADGET_TYPE];
  plist_info      plist_header;
  plist_info      plist;
  FILE           *fp;
  int     i_temp;
  int     n_temp;
  double *k_temp;
  double *kmin_temp;
  double *kmax_temp;
  double *P_temp;
  size_t *n_mode_temp;
  double *sigma_P_temp;
  double *shot_noise_temp;
  double *dP_temp;
  int     snapshot_number;
  int     i_compute;
  int     distribution_scheme;
  double  k_min_1D;
  double  k_max_1D;
  double  k_min_2D;
  double  k_max_2D;
  int     n_k_1D;
  int     n_k_2D;
  double *k_1D;
  double *P_k_1D;
  double *dP_k_1D;
  int    *n_modes_1D;
  double *P_k_2D;
  double *dP_k_2D;
  int    *n_modes_2D;
  int     n_groups=1;
  double  dk_1D;
  double  dk_2D;
  char   *grid_identifier;

  // Initialization -- MPI etc.
  SID_init(&argc,&argv,NULL,NULL);

  // Parse arguments
  int grid_size;
  char filename_in_root[MAX_FILENAME_LENGTH];
  char filename_out_root[MAX_FILENAME_LENGTH];
  strcpy(filename_in_root,  argv[1]);
  snapshot_number=(int)atoi(argv[2]);
  strcpy(filename_out_root, argv[3]);
  grid_size      =(int)atoi(argv[4]);
  if(!strcmp(argv[5],"ngp") || !strcmp(argv[5],"NGP"))
     distribution_scheme=MAP2GRID_DIST_NGP;
  else if(!strcmp(argv[5],"cic") || !strcmp(argv[5],"CIC"))
     distribution_scheme=MAP2GRID_DIST_CIC;
  else if(!strcmp(argv[5],"tsc") || !strcmp(argv[5],"TSC"))
     distribution_scheme=MAP2GRID_DIST_TSC;
  else if(!strcmp(argv[5],"d12") || !strcmp(argv[5],"D12"))
     distribution_scheme=MAP2GRID_DIST_DWT12;
  else if(!strcmp(argv[5],"d20") || !strcmp(argv[5],"D20"))
     distribution_scheme=MAP2GRID_DIST_DWT20;
  else
     SID_trap_error("Invalid distribution scheme {%s} specified.",ERROR_SYNTAX,argv[5]);

  SID_log("Smoothing Gadget file {%s;snapshot=#%d} to a %dx%dx%d grid with %s kernel...",SID_LOG_OPEN|SID_LOG_TIMER,
          filename_in_root,snapshot_number,grid_size,grid_size,grid_size,argv[5]);

  // Initialization -- fetch header info
  SID_log("Reading Gadget header...",SID_LOG_OPEN);
  gadget_read_info   fp_gadget;
  int                flag_filefound=init_gadget_read(filename_in_root,snapshot_number,&fp_gadget);
  int                flag_multifile=fp_gadget.flag_multifile;
  int                flag_file_type=fp_gadget.flag_file_type;
  gadget_header_info header        =fp_gadget.header;
  double             box_size      =(double)(header.box_size);
  size_t            *n_all         =(size_t *)SID_calloc(sizeof(size_t)*N_GADGET_TYPE);
  size_t             n_total;
  if(flag_filefound){
     if(SID.I_am_Master){
        FILE *fp_in;
        char  filename[MAX_FILENAME_LENGTH];
        int   block_length_open;
        int   block_length_close;
        set_gadget_filename(&fp_gadget,0,filename);
        fp_in=fopen(filename,"r");
        fread_verify(&block_length_open, sizeof(int),1,fp_in);
        fread_verify(&header,            sizeof(gadget_header_info),1,fp_in);
        fread_verify(&block_length_close,sizeof(int),1,fp_in);
        fclose(fp_in);
        if(block_length_open!=block_length_close)
           SID_trap_error("Block lengths don't match (ie. %d!=%d).",ERROR_LOGIC,block_length_open,block_length_close);
     }
     SID_Bcast(&header,sizeof(gadget_header_info),MASTER_RANK,SID.COMM_WORLD);
     redshift=header.redshift;
     h_Hubble=header.h_Hubble;
     box_size=header.box_size;
     if(SID.n_proc>1)
        n_load=1;
     else
        n_load=header.n_files;
     for(i_species=0,n_total=0,n_used=0;i_species<N_GADGET_TYPE;i_species++){
        n_all[i_species]=(size_t)header.n_all_lo_word[i_species]+((size_t)header.n_all_hi_word[i_species])<<32;
        n_total+=n_all[i_species];
        if(n_all[i_species]>0){
           n_used++;
           flag_used[i_species]=TRUE;
        }
        else
           flag_used[i_species]=FALSE;
     }

     // Initialize cosmology
     double box_size        =((double *)ADaPS_fetch(plist.data,"box_size"))[0];
     double h_Hubble        =((double *)ADaPS_fetch(plist.data,"h_Hubble"))[0];
     double redshift        =((double *)ADaPS_fetch(plist.data,"redshift"))[0];
     double expansion_factor=((double *)ADaPS_fetch(plist.data,"expansion_factor"))[0];
     double Omega_M         =((double *)ADaPS_fetch(plist.data,"Omega_M"))[0];
     double Omega_Lambda    =((double *)ADaPS_fetch(plist.data,"Omega_Lambda"))[0];
     double Omega_k         =1.-Omega_Lambda-Omega_M;
     double Omega_b=0.; // not needed, so doesn't matter
     double f_gas  =Omega_b/Omega_M;
     double sigma_8=0.; // not needed, so doesn't matter
     double n_spec =0.; // not needed, so doesn't matter
     char   cosmo_name[16];
     sprintf(cosmo_name,"Gadget file's");
     init_cosmo(&cosmo,
                cosmo_name,
                Omega_Lambda,
                Omega_M,
                Omega_k,
                Omega_b,
                f_gas,
                h_Hubble,
                sigma_8,
                n_spec);
  }
  SID_log("Done.",SID_LOG_CLOSE);

  grid_identifier=(char *)SID_calloc(GRID_IDENTIFIER_SIZE*sizeof(char));

  // Only process if there are >0 particles present
  if(n_used>0){

     // Loop over ithe real-space and 3 redshift-space frames
     int i_write;
     int i_run;
     int n_run;
     int n_grids_total; 
     n_grids_total=4; // For now, hard-wire real-space density and velocity grids only
     n_run=1;         // For now, hard-wire real-space calculation only
     for(i_run=0,i_write=0;i_run<n_run;i_run++){

        // Read catalog
        int  n_grid;
        char i_run_identifier[8];
        switch(i_run){
        case 0:
           SID_log("Processing real-space ...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"r");
           n_grid=4;
           break;
        case 1:
           SID_log("Processing v_x redshift space...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"x");
           n_grid=1;
           break;
        case 2:
           SID_log("Processing v_y redshift space...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"y");
           n_grid=1;
           break;
        case 3:
           SID_log("Processing v_z redsift space...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"z");
           n_grid=1;
           break;
        }

        // For each i_run case, loop over the fields we want to produce
        int i_grid;
        for(i_grid=0;i_grid<n_grid;i_grid++){

           char i_grid_identifier[8];
           switch(i_grid){
           case 0:
              SID_log("Processing density grid ...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"rho");
              break;
           case 1:
              SID_log("Processing v_x velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"v_x");
              break;
           case 2:
              SID_log("Processing v_y velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"v_y");
              break;
           case 3:
              SID_log("Processing v_z velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"v_z");
              break;
           }

           // Initialize the field that will hold the grid
           int        n[]={grid_size,grid_size,grid_size};
           double     L[]={box_size, box_size, box_size};
           int        i_init;
           for(i_species=0;i_species<N_GADGET_TYPE;i_species++){
              if(flag_used[i_species]){
                 field[i_species]     =(field_info *)SID_malloc(sizeof(field_info));
                 field_norm[i_species]=(field_info *)SID_malloc(sizeof(field_info));
                 init_field(3,n,L,field[i_species]);
                 init_field(3,n,L,field_norm[i_species]);
                 i_init=i_species;
              }
              else{
                 field[i_species]     =NULL;
                 field_norm[i_species]=NULL;
              }
           }

           // Loop over all the files that this rank will read
           int i_load;
           for(i_load=0;i_load<n_load;i_load++){
              if(n_load>1)
                 SID_log("Processing file No. %d of %d...",SID_LOG_OPEN|SID_LOG_TIMER,i_load+1,n_load);

              // Initialization -- read gadget file
              GBPREAL mass_array[N_GADGET_TYPE];
              init_plist(&plist,&((field[i_init])->slab),GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY);
              char filename_root[MAX_FILENAME_LENGTH];
              read_gadget_binary_local(filename_in_root,
                                       snapshot_number,
                                       i_run,
                                       i_load,
                                       n_load,
                                       mass_array,
                                       &(field[i_init]->slab),
                                       cosmo,
                                       &plist);

              // Generate power spectra
              for(i_species=0;i_species<plist.n_species;i_species++){

                 // Determine how many particles of species i_species there are
                 if(n_all[i_species]>0){
                    // Fetch the needed information
                    size_t   n_particles;
                    size_t   n_particles_local;
                    int      flag_alloc_m;
                    GBPREAL *x_particles_local;
                    GBPREAL *y_particles_local;
                    GBPREAL *z_particles_local;
                    GBPREAL *vx_particles_local;
                    GBPREAL *vy_particles_local;
                    GBPREAL *vz_particles_local;
                    GBPREAL *m_particles_local;
                    GBPREAL *v_particles_local;
                    GBPREAL *w_particles_local;
                    n_particles      =((size_t  *)ADaPS_fetch(plist.data,"n_all_%s",plist.species[i_species]))[0];
                    n_particles_local=((size_t  *)ADaPS_fetch(plist.data,"n_%s",    plist.species[i_species]))[0];
                    x_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"x_%s",    plist.species[i_species]);
                    y_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"y_%s",    plist.species[i_species]);
                    z_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"z_%s",    plist.species[i_species]);
                    vx_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vx_%s",   plist.species[i_species]);
                    vy_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vy_%s",   plist.species[i_species]);
                    vz_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vz_%s",   plist.species[i_species]);
                    if(ADaPS_exist(plist.data,"M_%s",plist.species[i_species])){
                       flag_alloc_m=FALSE;
                       m_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"M_%s",plist.species[i_species]);
                    }
                    else{
                       flag_alloc_m=TRUE;
                       m_particles_local=(GBPREAL *)SID_malloc(n_particles_local*sizeof(GBPREAL));
                       int i_particle;
                       for(i_particle=0;i_particle<n_particles_local;i_particle++)
                          m_particles_local[i_particle]=mass_array[i_species];
                    }

                    // Decide the map_to_grid() mode
                    int mode;
                    if(n_load==1)
                       mode=MAP2GRID_MODE_DEFAULT;
                    else if(i_load==0 || n_load==1)
                       mode=MAP2GRID_MODE_DEFAULT|MAP2GRID_MODE_NONORM;
                    else if(i_load==(n_load-1))
                       mode=MAP2GRID_MODE_NOCLEAN;
                    else
                       mode=MAP2GRID_MODE_NOCLEAN|MAP2GRID_MODE_NONORM;

                    // Set the array that will weight the grid
                    field_info *field_i;
                    field_info *field_norm_i;
                    double factor;
                    switch(i_grid){
                    case 0:
                       v_particles_local=m_particles_local;
                       w_particles_local=NULL;
                       field_i          =field[i_species];
                       field_norm_i     =NULL;
                       mode|=MAP2GRID_MODE_APPLYFACTOR;
                       factor=pow((double)grid_size/box_size,3.);
                       break;
                    case 1:
                       v_particles_local=vx_particles_local;
                       w_particles_local=m_particles_local;
                       field_i          =field[i_species];
                       field_norm_i     =field_norm[i_species];
                       factor=1.;
                       break;
                    case 2:
                       v_particles_local=vy_particles_local;
                       w_particles_local=m_particles_local;
                       field_i          =field[i_species];
                       field_norm_i     =field_norm[i_species];
                       factor=1.;
                       break;
                    case 3:
                       v_particles_local=vz_particles_local;
                       w_particles_local=m_particles_local;
                       field_i          =field[i_species];
                       field_norm_i     =field_norm[i_species];
                       factor=1.;
                       break;
                    }

                    // Generate grid
                    map_to_grid(n_particles_local,
                                x_particles_local,
                                y_particles_local,
                                z_particles_local,
                                v_particles_local,
                                w_particles_local,
                                cosmo,
                                redshift,
                                distribution_scheme,
                                factor,
                                field_i,
                                field_norm_i,
                                mode);
                    if(flag_alloc_m)
                       SID_free(SID_FARG m_particles_local);
                 }
              }

              // Clean-up
              free_plist(&plist);
              if(n_load>1)
                 SID_log("Done.",SID_LOG_CLOSE);
           } // loop over i_load
           
           // Write results to disk
           char filename_out_species[MAX_FILENAME_LENGTH];
           init_plist(&plist,NULL,GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY);
           for(i_species=0;i_species<plist.n_species;i_species++){
              if(flag_used[i_species]){
                 sprintf(grid_identifier,"%s_%s_%s",i_grid_identifier,i_run_identifier,plist.species[i_species]);
                 sprintf(filename_out_species,"%s_%s",filename_out_root,plist.species[i_species]);
                 write_grid(field[i_species],
                            filename_out_species,
                            i_write,
                            n_grids_total,
                            distribution_scheme,
                            grid_identifier,
                            header.box_size);
                 free_field(field[i_species]);
                 free_field(field_norm[i_species]);
                 SID_free(SID_FARG field[i_species]);
                 SID_free(SID_FARG field_norm[i_species]);
                 i_write++;
              }
           }

           // Clean-up
           free_plist(&plist);
           SID_log("Done.",SID_LOG_CLOSE);

        } // loop over i_grid

        SID_log("Done.",SID_LOG_CLOSE);
     } // loop over i_run
  } // if n_used>0 

  // Clean-up
  free_cosmo(&cosmo);
  SID_free(SID_FARG grid_identifier);
  SID_free(SID_FARG n_all);

  SID_log("Done.",SID_LOG_CLOSE);

  SID_exit(ERROR_NONE);
}