Exemplo n.º 1
0
int main(int argc, char *argv[]){
  char        filename_root_out[256];

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

  // Fetch user inputs
  strcpy(filename_root_out,argv[1]);

  // Read the run parameters
  int i_read_start;
  int i_read_stop;
  int i_read_step;
  int n_search;
  int flag_fix_bridges;
  int flag_compute_fragmented;
  int flag_compute_ghosts;
  read_trees_run_parameters(filename_root_out,
                            &i_read_start,
                            &i_read_stop,
                            &i_read_step,
                            &n_search,
                            &flag_fix_bridges,
                            &flag_compute_fragmented,
                            &flag_compute_ghosts);

  // Default to scanning the whole range of snapshots when building forests
  int n_search_forests=i_read_stop;

  // Generate mapping
  compute_forests(filename_root_out,n_search_forests);

  SID_exit(ERROR_NONE);
}
Exemplo n.º 2
0
int main(int argc, char *argv[]){

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

  // Fetch user inputs
  char filename_SSimPL_dir[MAX_FILENAME_LENGTH];
  char filename_halo_version_root[MAX_FILENAME_LENGTH];
  char filename_trees_name[MAX_FILENAME_LENGTH];
  char filename_output_root[MAX_FILENAME_LENGTH];
  strcpy(filename_SSimPL_dir,       argv[1]);
  strcpy(filename_halo_version_root,argv[2]);
  strcpy(filename_trees_name,       argv[3]);
  strcpy(filename_output_root,      argv[4]);

  // Set the halo and tree filename roots
  char filename_trees_root[MAX_FILENAME_LENGTH];
  char filename_halos_root[MAX_FILENAME_LENGTH];
  sprintf(filename_trees_root,"%s/trees/%s",filename_SSimPL_dir,filename_trees_name);
  sprintf(filename_halos_root,"%s/halos/%s",filename_SSimPL_dir,filename_halo_version_root);

  SID_log("Generating treenode markers & analysis of merger trees...",SID_LOG_OPEN|SID_LOG_TIMER);

  // Perform analysis
  tree_info *trees;
  read_trees(filename_SSimPL_dir,
             filename_halo_version_root,
             filename_trees_name,
             TREE_MODE_DEFAULT,
             &trees);

  // Read catalogs
  read_trees_catalogs(trees,
                      filename_SSimPL_dir,
                      filename_halo_version_root,
                      READ_TREES_CATALOGS_BOTH);

  // Loop over the two halo types
  for(int i_type=0;i_type<2;i_type++){
     // Initialize markers (just one-at-a-time to save RAM)
     int mode;
     if(i_type==0)
        mode=PRECOMPUTE_TREENODE_MARKER_GROUPS;
     else
        mode=PRECOMPUTE_TREENODE_MARKER_SUBGROUPS;

     // Compute markers
     precompute_treenode_markers(trees,mode);
     // Write markers
     write_treenode_markers(trees,filename_output_root,mode);
     // Free markers
     free_precompute_treenode_markers(trees,mode);
  }

  // Clean-up
  free_trees(&trees);

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
Exemplo n.º 3
0
int main(int argc, char *argv[]){
  plist_info plist;
  int        snapshot;
  char       filename_in[256];
  char       filename_out[256];

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

  /**********************/
  /* Parse command line */
  /**********************/
  if(argc!=2){
    fprintf(stderr,"\n syntax: %s gadget_file\n",argv[0]);
    fprintf(stderr," ------\n\n");
    return(ERROR_SYNTAX);
  }
  else{
    strcpy(filename_in, argv[1]);
    snapshot=atoi(argv[2]);
    strcpy(filename_out,argv[3]);
    strcat(filename_out,".csv");
  }

  SID_log("Converting GADGET file to .csv...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in,filename_out);

  /****************************************/
  /* Read GADGET file into data structure */
  /****************************************/
  init_plist(&plist,NULL,GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY);
  SID_log("Reading GADGET file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in);
  read_gadget_binary(filename_in,snapshot,&plist,READ_GADGET_DEFAULT);
  SID_log("Done.",SID_LOG_CLOSE);

  /********************/
  /* Write ascii file */
  /********************/
  SID_log("Writing .csv file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_out);
  write_gadget_csv(filename_out,&plist);
  SID_log("Done.",SID_LOG_CLOSE);

  /************/
  /* Clean-up */
  /************/
  free_plist(&plist);

  SID_log("Done.",SID_LOG_CLOSE);

  return(ERROR_NONE);
}
Exemplo n.º 4
0
int main(int argc, char *argv[]){
  int     n_search;
  int     i_halo;
  char    filename_SSimPL_root[MAX_FILENAME_LENGTH];
  char    filename_in[MAX_FILENAME_LENGTH];
  char    group_text_prefix[4];
  int     n_files;
  int     k_read;
  int     max_n_groups;
  int     l_read;
  int     n_groups;
  int     j_read;
  int     mode;
  int     n_groups_i;
  int     n_groups_j;
  int     j_halo;
  int     match;
  int     i_read;
  int     i_read_start;
  int     i_read_stop;
  SID_fp  fp_in;

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

  // Fetch user inputs
  strcpy(filename_SSimPL_root,argv[1]);
  if(!strcmp(argv[2],"groups") || !strcmp(argv[2],"group"))
     mode=MATCH_GROUPS;
  else if(!strcmp(argv[2],"subgroups") || !strcmp(argv[2],"subgroup"))
     mode=MATCH_SUBGROUPS;
  else{
     SID_log("Invalid mode selection {%s}.  Should be 'group' or 'subgroup'.",SID_LOG_COMMENT,argv[2]);
     SID_exit(ERROR_SYNTAX);
  }
  i_read=atoi(argv[3]);
  j_read=atoi(argv[4]);
  SID_log("Searching match information for halo #%d in file #%d of {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,i_halo,i_read,filename_SSimPL_root);

  // Convert filename_root to filename
  switch(mode){
     case MATCH_SUBGROUPS:
     sprintf(group_text_prefix,"sub");
     break;
     case MATCH_GROUPS:
     sprintf(group_text_prefix,"");
     break;
  }

  // Set the standard SSiMPL match file path
  char filename_root_in[MAX_FILENAME_LENGTH];
  sprintf(filename_root_in,"%s/trees/matches/",filename_SSimPL_root);

  // Set the output file
  char filename_base[MAX_FILENAME_LENGTH];
  char filename_out[MAX_FILENAME_LENGTH];
  sprintf(filename_base,filename_SSimPL_root);
  if(!strcmp(&(filename_base[strlen(filename_base)-1]),"/"))
     strcpy(&(filename_base[strlen(filename_base)-1]),"\0");
  strip_path(filename_base);
  sprintf(filename_out,"%s_%d_%d_2way_matches.txt",filename_base,i_read,j_read);

  // Read header information
  SID_log("Reading header information...",SID_LOG_OPEN);
  sprintf(filename_in,"%s/%sgroup_matches_header.dat",filename_root_in,group_text_prefix);
  SID_fopen(filename_in,"r",&fp_in);
  SID_fread(&i_read_start,sizeof(int),1,&fp_in);SID_log("snap start  =%d",SID_LOG_COMMENT,i_read_start);
  SID_fread(&i_read_stop, sizeof(int),1,&fp_in);SID_log("snap stop   =%d",SID_LOG_COMMENT,i_read_stop);
  SID_fread(&n_search,    sizeof(int),1,&fp_in);SID_log("search range=%d",SID_LOG_COMMENT,n_search);
  SID_fread(&n_files,     sizeof(int),1,&fp_in);SID_log("# of files  =%d",SID_LOG_COMMENT,n_files);
  for(k_read=0,max_n_groups=0;k_read<n_files;k_read++){
     SID_fread(&l_read,  sizeof(int),1,&fp_in);
     SID_fread(&n_groups,sizeof(int),1,&fp_in);
     SID_fseek(&fp_in,   sizeof(int),n_groups,SID_SEEK_CUR);
     if(mode==MATCH_GROUPS)
        SID_fseek(&fp_in,   sizeof(int),n_groups,SID_SEEK_CUR);
     max_n_groups=MAX(max_n_groups,n_groups);
  }
  SID_log("Max # groups=%d",SID_LOG_COMMENT,max_n_groups);
  SID_fclose(&fp_in);
  SID_log("Done.",SID_LOG_CLOSE);

  // Initialize some arrays
  int    *n_particles_i       =(int    *)SID_malloc(sizeof(int)   *max_n_groups);
  int    *n_particles_j       =(int    *)SID_malloc(sizeof(int)   *max_n_groups);
  int    *match_forward_ids   =(int    *)SID_malloc(sizeof(int)   *max_n_groups);
  size_t *match_forward_index =(size_t *)SID_malloc(sizeof(size_t)*max_n_groups);
  float  *match_forward_score =(float  *)SID_malloc(sizeof(float) *max_n_groups);
  char   *match_forward_2way  =(char   *)SID_malloc(sizeof(char)  *max_n_groups);
  int    *match_backward_ids  =(int    *)SID_malloc(sizeof(int)   *max_n_groups);
  size_t *match_backward_index=(size_t *)SID_malloc(sizeof(size_t)*max_n_groups);
  float  *match_backward_score=(float  *)SID_malloc(sizeof(float) *max_n_groups);
  char   *match_backward_2way =(char   *)SID_malloc(sizeof(char)  *max_n_groups);

  // Loop over all matching combinations
  SID_log("Reading forward matches...",SID_LOG_OPEN|SID_LOG_TIMER);
  SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);
  read_matches(filename_root_in,
               i_read,
               j_read,
               max_n_groups,
               mode,
               &n_groups_i,
               &n_groups_j,
               n_particles_i,
               n_particles_j,
               NULL,
               NULL,
               match_forward_ids,
               match_forward_score,
               match_forward_index,
               match_forward_2way,
               FALSE);
  SID_log("Done.",SID_LOG_CLOSE);
  SID_log("Processing backwards matches...",SID_LOG_OPEN|SID_LOG_TIMER);
  read_matches(filename_root_in,
               j_read,
               i_read,
               max_n_groups,
               mode,
               &n_groups_j,
               &n_groups_i,
               n_particles_j,
               n_particles_i,
               NULL,
               NULL,
               match_backward_ids,
               match_backward_score,
               match_backward_index,
               match_backward_2way,
               FALSE);
  SID_log("Done.",SID_LOG_CLOSE);

  // Open output file
  FILE *fp_out;
  fp_out=fopen(filename_out,"w");
  int i_column=1;
  fprintf(fp_out,"# Column (%02d): Halo index for snapshot %d\n",          i_column++,i_read);
  fprintf(fp_out,"#        (%02d): Halo index for snapshot %d\n",          i_column++,j_read);
  fprintf(fp_out,"#        (%02d): No. particles in snapshot %d\n",        i_column++,i_read);
  fprintf(fp_out,"#        (%02d): No. particles in snapshot %d\n",        i_column++,j_read);
  fprintf(fp_out,"#        (%02d): Forward  match score\n",                i_column++);
  fprintf(fp_out,"#        (%02d): Forward  match score/min match score\n",i_column++);
  fprintf(fp_out,"#        (%02d): Backward match score\n",                i_column++);
  fprintf(fp_out,"#        (%02d): Backward match score/min match score\n",i_column++);
  for(int i_halo=0;i_halo<n_groups_i;i_halo++){
     int j_halo=match_forward_ids[i_halo];
     if(match_forward_2way[i_halo]){
        if(j_halo<0 || j_halo>n_groups_j)
           SID_trap_error("There's an invalid match id (ie. %d<0 || %d>%d)  attached to a 2-way match!",ERROR_LOGIC,j_halo,j_halo,n_groups_j);
        fprintf(fp_out,"%7d %7d %6d %6d %10.3le %10.3le %10.3le %10.3le\n",
                i_halo,
                j_halo,
                n_particles_i[i_halo],
                n_particles_j[j_halo],
                match_forward_score[i_halo],
                match_forward_score[i_halo]/minimum_match_score((double)n_particles_i[i_halo]),
                match_backward_score[j_halo],
                match_backward_score[j_halo]/minimum_match_score((double)n_particles_j[j_halo]));
     }                
  }
  fclose(fp_out);

  // Clean-up
  SID_free(SID_FARG n_particles_i);
  SID_free(SID_FARG n_particles_j);
  SID_free(SID_FARG match_forward_ids);
  SID_free(SID_FARG match_forward_index);
  SID_free(SID_FARG match_forward_score);
  SID_free(SID_FARG match_forward_2way);
  SID_free(SID_FARG match_backward_ids);
  SID_free(SID_FARG match_backward_index);
  SID_free(SID_FARG match_backward_score);
  SID_free(SID_FARG match_backward_2way);
  
  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
Exemplo n.º 5
0
int main(int argc, char *argv[]){

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

  // Fetch user inputs
  char filename_SSimPL_dir[MAX_FILENAME_LENGTH];
  char filename_halo_version_root[MAX_FILENAME_LENGTH];
  char filename_trees_root[MAX_FILENAME_LENGTH];
  char filename_trees_name[MAX_FILENAME_LENGTH];
  char filename_halos_root[MAX_FILENAME_LENGTH];
  char filename_out_root[MAX_FILENAME_LENGTH];
  strcpy(filename_SSimPL_dir,       argv[1]);
  strcpy(filename_halo_version_root,argv[2]);
  strcpy(filename_trees_name,       argv[3]);
  double z_lo         =(double)atof(argv[4]);
  double z_hi         =(double)atof(argv[5]);
  double M_cut_min    =(double)atof(argv[6]);
  strcpy(filename_out_root,         argv[7]);

  // Set some filenames
  sprintf(filename_trees_root,"%s/trees/%s",filename_SSimPL_dir,filename_trees_name);
  sprintf(filename_halos_root,"%s/halos/%s",filename_SSimPL_dir,filename_halo_version_root);

  SID_log("Creating a catalog matched across redshifts z_lo~%lf and z_hi~%lf...",SID_LOG_OPEN|SID_LOG_TIMER,z_lo,z_hi);

  // Perform analysis
  tree_info *trees;
  read_trees(filename_SSimPL_dir,
             filename_halo_version_root,
             filename_trees_name,
             TREE_MODE_DEFAULT|TREE_READ_EXTENDED_POINTERS,
             &trees);

  // Read ancillary data
  read_trees_catalogs(trees,READ_TREES_CATALOGS_GROUPS|READ_TREES_CATALOGS_SUBGROUPS);

  // Determine which snapshots best match the given redshifts
  int i_z_lo=find_treesnap_z(trees,z_lo);
  int i_z_hi=find_treesnap_z(trees,z_hi);

  // Generate two catalogs
  SID_log("Compiling catalogs...",SID_LOG_OPEN|SID_LOG_TIMER);
  for(int i_cat=0;i_cat<2;i_cat++){
     char filename_out[MAX_FILENAME_LENGTH];
     switch(i_cat){
        case 0:
           SID_log("Processing group catalog...",SID_LOG_OPEN);
           sprintf(filename_out,"%s_groups.txt",filename_out_root);
           break;
        case 1:
           SID_log("Processing subgroup catalog...",SID_LOG_OPEN);
           sprintf(filename_out,"%s_subgroups.txt",filename_out_root);
           break;
     }

     // Open file and write header
     FILE *fp_out  =fopen(filename_out,"w");
     int   i_column=1;
     fprintf(fp_out,"# Catalog matched from z_hi=%lf to z_lo=%lf\n",trees->z_list[i_z_hi],trees->z_list[i_z_lo]);
     fprintf(fp_out,"#   SSiMPL_dir  ={%s}\n",filename_SSimPL_dir);
     fprintf(fp_out,"#   halo_version={%s}\n",filename_halo_version_root);
     fprintf(fp_out,"#   tree_version={%s}\n",filename_trees_name);
     fprintf(fp_out,"#\n");
     if(i_cat==0){
        fprintf(fp_out,"# Column (%02d): FoF      index   (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): FoF      index   (z_lo)\n",i_column++);
     }
     else{
        fprintf(fp_out,"# Column (%02d): subgroup index   (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): subgroup index   (z_lo)\n",i_column++);
        fprintf(fp_out,"#        (%02d): FoF      index   (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): FoF      index   (z_lo)\n",i_column++);
     }
     fprintf(fp_out,"#        (%02d): n_particles      (z_hi)\n",i_column++);
     if(i_cat==0){
        fprintf(fp_out,"#        (%02d): n_subgroups      (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): n_subgroups      (z_lo)\n",i_column++);
        fprintf(fp_out,"#        (%02d): sig_v_1D  [km/s] (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): sig_v_1D  [km/s] (z_lo)\n",i_column++);
        fprintf(fp_out,"#        (%02d): sig_v_1Dp [km/s] (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): sig_v_1Dp [km/s] (z_lo)\n",i_column++);
     }
     else{
        fprintf(fp_out,"#        (%02d): M_vir_sub      (z_hi)\n",i_column++);
        fprintf(fp_out,"#        (%02d): M_vir_sub      (z_lo)\n",i_column++);
     }
     fprintf(fp_out,"#        (%02d): M_vir_FoF        (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): M_vir_FoF        (z_lo)\n",i_column++);
     fprintf(fp_out,"#        (%02d): x                (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): y                (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): z                (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): x                (z_lo)\n",i_column++);
     fprintf(fp_out,"#        (%02d): y                (z_lo)\n",i_column++);
     fprintf(fp_out,"#        (%02d): z                (z_lo)\n",i_column++);
     fprintf(fp_out,"#        (%02d): v_x              (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): v_y              (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): v_z              (z_hi)\n",i_column++);
     fprintf(fp_out,"#        (%02d): v_x              (z_lo)\n",i_column++);
     fprintf(fp_out,"#        (%02d): v_y              (z_lo)\n",i_column++);
     fprintf(fp_out,"#        (%02d): v_z              (z_lo)\n",i_column++);

     // Write catalog
     tree_node_info        *current;
     halo_properties_info **group_properties   =(halo_properties_info **)ADaPS_fetch(trees->data,"properties_groups");
     halo_properties_info **subgroup_properties=(halo_properties_info **)ADaPS_fetch(trees->data,"properties_subgroups");
     if(i_cat==0)
        current=trees->first_neighbour_groups[i_z_hi];
     else
        current=trees->first_neighbour_subgroups[i_z_hi];
     while(current!=NULL){
        tree_node_info       *current_subgroup;
        tree_node_info       *current_group;
        halo_properties_info *current_properties;
        halo_properties_info *current_group_properties;
        halo_properties_info *current_subgroup_properties;
        if(i_cat==0){
           current_subgroup           =NULL;
           current_group              =current;
           current_group_properties   =&(group_properties[current_group->snap_tree][current_group->neighbour_index]);
           current_subgroup_properties=&(subgroup_properties[current->snap_tree][current->neighbour_index]);
           current_properties         =current_group_properties;
        }
        else{
           current_subgroup           =current;
           current_group              =current->parent_top;
           current_group_properties   =&(group_properties[current_group->snap_tree][current_group->neighbour_index]);
           current_subgroup_properties=&(subgroup_properties[current->snap_tree][current->neighbour_index]);
           current_properties         =current_subgroup_properties;
        }
        if(current_properties->M_vir>=M_cut_min){
           tree_node_info *matched;
           int             flag_exact=find_treenode_snap_equals_given(trees,current,i_z_lo,&matched,TREE_PROGENITOR_ORDER_N_PARTICLES_PEAK);
           if(matched!=NULL && flag_exact){
              int n_sub_lo;
              int n_sub_hi;
              tree_node_info *matched_group;
              tree_node_info *matched_subgroup;
              halo_properties_info *matched_group_properties;
              halo_properties_info *matched_subgroup_properties;
              double current_group_sigma_v=0.;
              double matched_group_sigma_v=0.;
              if(i_cat==0){
                 double v_mean;
                 matched_subgroup           =NULL;
                 matched_group              =matched;
                 matched_subgroup_properties=NULL;
                 matched_group_properties   =&(group_properties[matched_group->snap_tree][matched_group->neighbour_index]);
                 tree_node_info *current_j;

                 // Compute 1D velocity dispersion for current group                
                 current_j=current_group->substructure_first;
                 v_mean   =0.;
                 n_sub_hi =0;
                 while(current_j!=NULL){
                    double M_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].M_vir;
                    if(M_i>M_cut_min){
                       float  v_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].velocity_COM[0];
                       v_mean+=v_i;
                       n_sub_hi++;
                    }
                    current_j=current_j->substructure_next;
                 }
                 current_j=current_group->substructure_first;
                 current_group_sigma_v=0.;
                 if(n_sub_hi>1){
                    v_mean/=(double)n_sub_hi;
                    while(current_j!=NULL){
                       double M_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].M_vir;
                       if(M_i>M_cut_min){
                          float v_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].velocity_COM[0];
                          current_group_sigma_v+=pow(v_i-v_mean,2.);
                       }
                       current_j=current_j->substructure_next;
                    }
                    current_group_sigma_v=sqrt(current_group_sigma_v/(double)(n_sub_hi-1));
                 }

                 // Compute 1D velocity dispersion for matched group                
                 current_j=matched_group->substructure_first;
                 v_mean   =0.;
                 n_sub_lo =0;
                 while(current_j!=NULL){
                    double M_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].M_vir;
                    if(M_i>M_cut_min){
                       float v_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].velocity_COM[0];
                       v_mean+=v_i;
                       n_sub_lo++;
                    }
                    current_j=current_j->substructure_next;
                 }
                 current_j=matched_group->substructure_first;
                 matched_group_sigma_v=0.;
                 if(n_sub_lo>1){
                    v_mean/=(double)n_sub_lo;
                    while(current_j!=NULL){
                       double M_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].M_vir;
                       if(M_i>M_cut_min){
                          float v_i=subgroup_properties[current_j->snap_tree][current_j->neighbour_index].velocity_COM[0];
                          matched_group_sigma_v+=pow(v_i-v_mean,2.);
                       }
                       current_j=current_j->substructure_next;
                    }
                    matched_group_sigma_v=sqrt(matched_group_sigma_v/(double)(n_sub_lo-1));
                 }

              }
              else{
                 matched_subgroup           =matched;
                 matched_group              =matched_subgroup->parent_top;
                 matched_subgroup_properties=&(subgroup_properties[matched_subgroup->snap_tree][matched_subgroup->neighbour_index]);
                 matched_group_properties   =&(group_properties[matched_group->snap_tree][matched_group->neighbour_index]);
              }
              if(i_cat==0)
                 fprintf(fp_out,"%7d %7d %6d %5d %5d %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le\n",
                    current_group->neighbour_index,
                    matched_group->neighbour_index,
                    current_group_properties->n_particles,
                    n_sub_hi,
                    n_sub_lo,
                    current_group_sigma_v,
                    matched_group_sigma_v,
                    current_group_properties->sigma_v,
                    matched_group_properties->sigma_v,
                    current_group_properties->M_vir,
                    matched_group_properties->M_vir,
                    current_group_properties->position_MBP[0],
                    current_group_properties->position_MBP[1],
                    current_group_properties->position_MBP[2],
                    matched_group_properties->position_MBP[0],
                    matched_group_properties->position_MBP[1],
                    matched_group_properties->position_MBP[2],
                    current_group_properties->velocity_COM[0],
                    current_group_properties->velocity_COM[1],
                    current_group_properties->velocity_COM[2],
                    matched_group_properties->velocity_COM[0],
                    matched_group_properties->velocity_COM[1],
                    matched_group_properties->velocity_COM[2]);
              else
                 fprintf(fp_out,"%7d %7d %7d %7d %6d %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le %10.3le\n",
                    current_subgroup->neighbour_index,
                    matched_subgroup->neighbour_index,
                    current_group->neighbour_index,
                    matched_group->neighbour_index,
                    current_subgroup_properties->n_particles,
                    current_subgroup_properties->M_vir,
                    matched_subgroup_properties->M_vir,
                    current_group_properties->M_vir,
                    matched_group_properties->M_vir,
                    current_subgroup_properties->position_MBP[0],
                    current_subgroup_properties->position_MBP[1],
                    current_subgroup_properties->position_MBP[2],
                    matched_subgroup_properties->position_MBP[0],
                    matched_subgroup_properties->position_MBP[1],
                    matched_subgroup_properties->position_MBP[2],
                    current_subgroup_properties->velocity_COM[0],
                    current_subgroup_properties->velocity_COM[1],
                    current_subgroup_properties->velocity_COM[2],
                    matched_subgroup_properties->velocity_COM[0],
                    matched_subgroup_properties->velocity_COM[1],
                    matched_subgroup_properties->velocity_COM[2]);
           }
        }
        current=current->next_neighbour;
     }
     fclose(fp_out);
     SID_log("Done.",SID_LOG_CLOSE);
  }
  SID_log("Done.",SID_LOG_CLOSE);

  // Clean-up
  free_trees(&trees);

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
Exemplo n.º 6
0
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);
}
Exemplo n.º 7
0
int main(int argc, char *argv[]){
  char    filename_root[256];
  char    filename_properties[256];
  char    filename_indices[256];
  char    filename_out[256];
  char    prefix_text[5];
  FILE   *fp_properties=NULL;
  FILE   *fp_profiles  =NULL;
  FILE   *fp_out       =NULL;
  int     i_file;
  int     n_files;
  int     n_groups_all;
  int     i_group;
  int     i_group_selected;
  int     i_profile;
  int     flag_process_group;
  int     snap_number;
  int     n_groups_properties;
  int     n_groups_profiles;  
  halo_properties_info properties;
  halo_profile_info    profile;
  float  lambda,v_c;
  float  offset_COM;
  float  r_min,r_max;

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

  strcpy(filename_root, argv[1]);
  snap_number     =atoi(argv[2]);
  i_group_selected=atoi(argv[3]);

  if(i_group_selected<0){
    flag_process_group=TRUE;
    i_group_selected*=-1;
    sprintf(prefix_text,"");
  }
  else{
    flag_process_group=FALSE;
    sprintf(prefix_text,"sub");
  }

  if(SID.I_am_Master){
    sprintf(filename_indices,"%s_%03d.catalog_%sgroups_indices",filename_root,snap_number,prefix_text);

    FILE *fp_in;
    if((fp_in=fopen(filename_indices,"r"))==NULL)
       SID_trap_error("Could not open file {%s}.",ERROR_IO_OPEN,filename_indices);
    int i_file,n_files,n_groups,n_groups_all;
    fread(&i_file,      sizeof(int),1,fp_in);
    fread(&n_files,     sizeof(int),1,fp_in);
    fread(&n_groups,    sizeof(int),1,fp_in);
    fread(&n_groups_all,sizeof(int),1,fp_in);
    int n_particles_i;
    for(i_group=0;i_group<i_group_selected;i_group++){
       fread(&n_particles_i,sizeof(int),1,fp_in);
       fseeko(fp_in,(off_t)(sizeof(size_t)*n_particles_i),SEEK_CUR);
    }
    fread(&n_particles_i,sizeof(int),1,fp_in);
    fprintf(stderr,"n_particles=%d\n",n_particles_i);
    int i_particle;
    for(i_particle=0;i_particle<n_particles_i;i_particle++){
       size_t index_i;
       fread(&index_i,sizeof(size_t),1,fp_in);
       fprintf(stderr,"%4d %lld\n",i_particle,index_i);
    }
   
    fclose(fp_in);

  }  

  SID_exit(ERROR_NONE);
}
Exemplo n.º 8
0
int main(int argc, char *argv[]){
   double  redshift;
   char    filename_in[MAX_FILENAME_LENGTH];
   char    filename_out[MAX_FILENAME_LENGTH];
   char    filename_cosmology[MAX_FILENAME_LENGTH];
   double  box_size;
   double  lM_min,dlM;
   char   *line=NULL;
   size_t  line_length=0;
   int     M_column;
   int     n_bins;
   int     flag_log;
 
   // Initialization -- MPI etc.
   SID_init(&argc,&argv,NULL,NULL);
   if(argc!=11)
     SID_trap_error("Incorrect syntax.",ERROR_SYNTAX);

   // Parse arguments
   strcpy(filename_in, argv[1]);
   strcpy(filename_out,argv[2]);
   redshift=(double)atof(argv[3]);
   strcpy(filename_cosmology,argv[4]);
   box_size=(double)atof(argv[5]);
   M_column=(int)   atoi(argv[6]);
   flag_log=(int)   atoi(argv[7]);
   lM_min  =(double)atof(argv[8]);
   dlM     =(double)atof(argv[9]);
   n_bins  =(int)   atoi(argv[10]);

   SID_log("Producing a mass function for ascii file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in);
 
   // Initialize cosmology
   cosmo_info *cosmo;
   read_gbpCosmo_file(&cosmo,filename_cosmology);
   double h_Hubble=((double *)ADaPS_fetch(cosmo,"h_Hubble"))[0];

   // Open file
   FILE *fp_in;
   if((fp_in=fopen(filename_in,"r"))==NULL)
      SID_trap_error("Could not open {%s} for reading.",ERROR_IO_OPEN,filename_in);

   // Allocate memory for the data. Read it and sort it in ascending order 
   SID_log("Reading data...",SID_LOG_OPEN|SID_LOG_TIMER);
   int     n_data_in=count_lines_data(fp_in);
   SID_log("(%d items)...",SID_LOG_CONTINUE,n_data_in);
   double *data  =(double *)malloc(sizeof(double)*n_data_in);
   int n_data=0;
   for(int i=0;i<n_data_in;i++){
     double data_in;
     grab_next_line_data(fp_in,&line,&line_length);
     grab_double(line,M_column,&data_in);
     if(!flag_log)
        data_in=take_log10(data_in);
     if(data_in>=lM_min)
        data[n_data++]=data_in;
   }
   SID_log("(%d will be used)...",SID_LOG_CONTINUE,n_data);
   fclose(fp_in);
   SID_free(SID_FARG line);
   SID_log("Done.",SID_LOG_CLOSE);

   // Perform sort
   SID_log("Sorting data...",SID_LOG_OPEN|SID_LOG_TIMER);
   merge_sort(data,n_data,NULL,SID_DOUBLE,SORT_INPLACE_ONLY,SORT_COMPUTE_INPLACE);
   SID_log("Done.",SID_LOG_CLOSE);

   // Compile histogram
   SID_log("Computing mass function...",SID_LOG_OPEN|SID_LOG_TIMER);
   double *bin       =(double *)SID_malloc(sizeof(double)*(n_bins+1));
   double *bin_median=(double *)SID_malloc(sizeof(double)*n_bins);
   int    *hist      =(int    *)SID_calloc(sizeof(int)   *n_bins);
   double  lM_bin_min=lM_min;
   double  lM_bin_max=lM_min;
   int     i_data_lo=-1;
   int     i_data_hi=-1;
   int     i_bin =0;
   int     i_data=0;
   for(i_bin=0;i_bin<n_bins;i_bin++){
     lM_bin_min=lM_bin_max;
     lM_bin_max=lM_min+((double)(i_bin+1))*dlM;
     bin[i_bin]=lM_bin_min;
     i_data_lo=i_data;
     i_data_hi=i_data;
     while(data[i_data]<lM_bin_max && i_data<n_data){
        hist[i_bin]++;
        i_data_hi=i_data;
        i_data++;
        if(i_data>=n_data) break;
     }
     int i_data_mid=(i_data_lo+i_data_hi)/2;
     if(hist[i_bin]>0){
       if(hist[i_bin]%2)
         bin_median[i_bin]=data[i_data_mid];
       else
         bin_median[i_bin]=0.5*(data[i_data_mid]+data[i_data_mid+1]);
     }
     else
        bin_median[i_bin]=0.5*(lM_bin_max+lM_bin_min);
   }
   bin[i_bin]=lM_bin_max;
   SID_log("Done.",SID_LOG_CLOSE);

   // Write mass function
   FILE *fp_out;
   if((fp_out=fopen(filename_out,"w"))==NULL){
     fprintf(stderr,"Error opening output file {%s}.\n",filename_out);
     SID_free(SID_FARG data);
     return(1);
   }
   SID_log("Writing results to {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_out);
   double box_volume=box_size*box_size*box_size;
   fprintf(fp_out,"# Mass function for column %d in {%s}\n",M_column,filename_in);
   fprintf(fp_out,"# Column (01): M_lo     [source units]\n");
   fprintf(fp_out,"#        (02): M_median [source units]\n");
   fprintf(fp_out,"#        (03): M_hi     [source units]\n");
   fprintf(fp_out,"#        (04): No. in bin\n");
   fprintf(fp_out,"#        (05): MFn (per unit volume, per dlogM)\n");
   fprintf(fp_out,"#        (06): +/- MFn\n");
   fprintf(fp_out,"#        (07): Sheth & Tormen MFn\n");
   fprintf(fp_out,"#        (08): Watson MFn\n");
   fprintf(fp_out,"#        (09): No. w/ M>M_lo\n");
   fprintf(fp_out,"#        (10): Cumulative MFn (per unit volume)\n");
   fprintf(fp_out,"#        (11): +/- Cumulative MFn\n");
   fprintf(fp_out,"#        (12): Sheth & Tormen Cumulative MFn\n");
   fprintf(fp_out,"#        (13): Watson Cumulative MFn\n");
   double M_sol_inv_h=M_SOL/h_Hubble;
   double Mpc_inv_h  =M_PER_MPC/h_Hubble;
   for(int i=0;i<n_bins;i++){
     double dn_dlogM_theory_1=mass_function(take_alog10(bin_median[i])*M_sol_inv_h,
                                            redshift,
                                            &cosmo,
                                            MF_ST)*pow(Mpc_inv_h,3.0);
     double n_theory_1=mass_function_cumulative(take_alog10(bin[i])*M_sol_inv_h,
                                                redshift,
                                                &cosmo,
                                                MF_ST)*pow(Mpc_inv_h,3.0);
     double dn_dlogM_theory_2=mass_function(take_alog10(bin_median[i])*M_sol_inv_h,
                                            redshift,
                                            &cosmo,
                                            MF_WATSON)*pow(Mpc_inv_h,3.0);
     double n_theory_2=mass_function_cumulative(take_alog10(bin[i])*M_sol_inv_h,
                                                redshift,
                                                &cosmo,
                                                MF_WATSON)*pow(Mpc_inv_h,3.0);
     // Compute cumulative histogram
     int cumulative_hist=0;
     for(int j_bin=i;j_bin<n_bins;j_bin++)
        cumulative_hist+=hist[j_bin];
     fprintf(fp_out,"%11.4le %11.4le %11.4le %6d %11.4le %11.4le %10.4le %10.4le %6d %10.4le %10.4le %10.4le %10.4le\n",
             bin[i],
             bin_median[i],
             bin[i+1],
             hist[i],
             (double)(hist[i])/(box_volume*dlM),
             sqrt((double)(hist[i]))/(box_volume*dlM),
             dn_dlogM_theory_1,dn_dlogM_theory_2,
             cumulative_hist,
             (double)(cumulative_hist)/box_volume,
             sqrt((double)(cumulative_hist))/box_volume,
             n_theory_1,n_theory_2);
   }
   fclose(fp_out);
   SID_log("Done.",SID_LOG_CLOSE);

   // Free allocated memory
   SID_free(SID_FARG data);
   SID_free(SID_FARG bin);
   SID_free(SID_FARG bin_median);
   SID_free(SID_FARG hist);
  
   SID_log("Done.",SID_LOG_CLOSE);
   SID_exit(ERROR_NONE);
}
Exemplo n.º 9
0
int main(int argc, char *argv[]){

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

  SID_log("Constructing match catalog...",SID_LOG_OPEN|SID_LOG_TIMER);

  // Parse arguments
  char filename_catalog_root[MAX_FILENAME_LENGTH];
  char filename_matches_root[MAX_FILENAME_LENGTH];
  char filename_out[MAX_FILENAME_LENGTH];
  char prefix_text[32];
  int  flag_matches_type;
  int  i_read;
  int  j_read;
  int  catalog_read_mode;
  int  matches_read_mode;
  strcpy(filename_catalog_root,argv[1]);
  strcpy(filename_matches_root,argv[2]);
  flag_matches_type      =atoi(argv[3]);
  strcpy(prefix_text,          argv[4]);
  i_read                 =atoi(argv[5]);
  j_read                 =atoi(argv[6]);
  strcpy(filename_out,         argv[7]);
  if(!strcpy(prefix_text,"subgroup") ||
     !strcpy(prefix_text,"subgroups") ||
     !strcpy(prefix_text,"sub")){
     sprintf(prefix_text,"sub");
     catalog_read_mode=READ_CATALOG_SUBGROUPS|READ_CATALOG_PROPERTIES;
     matches_read_mode=MATCH_SUBGROUPS;
  }
  else if(!strcpy(prefix_text,"group") ||
          !strcpy(prefix_text,"groups")){
     sprintf(prefix_text,"");
     catalog_read_mode=READ_CATALOG_GROUPS|READ_CATALOG_PROPERTIES;
     matches_read_mode=MATCH_GROUPS;
  }
  else
     SID_trap_error("Invalid catalog type (%s).",ERROR_SYNTAX,prefix_text);

  // Set filenames 
  char filename_cat1[MAX_FILENAME_LENGTH];
  char filename_cat2[MAX_FILENAME_LENGTH];
  sprintf(filename_cat1,"%s_%03d.catalog_%sgroups_properties",filename_catalog_root,prefix_text,i_read);
  sprintf(filename_cat2,"%s_%03d.catalog_%sgroups_properties",filename_catalog_root,prefix_text,j_read);

  // Contents of the halo properties structure
  //struct halo_properties_info{
  //  long long id_MBP;                    // ID of most bound particle in structure
  //  int       n_particles;               // Number of particles in the structure
  //  float     position_COM[3];           // Centre-of-mass position      [Mpc/h]
  //  float     position_MBP[3];           // Most bound particle position [Mpc/h]
  //  float     velocity_COM[3];           // Centre-of-mass velocity      [km/s]
  //  float     velocity_MBP[3];           // Most bound particle velocity [km/s]
  //  double    M_vir;                     // Bryan & Norman (ApJ 495, 80, 1998) virial mass [M_sol/h]
  //  float     R_vir;                     // Virial radius [Mpc/h]
  //  float     R_halo;                    // Distance of last halo particle from MBP [Mpc/h]
  //  float     R_max;                     // Radius of maximum circular velocity     [Mpc/h]
  //  float     V_max;                     // Maximum circular velocity               [km/s]
  //  float     sigma_v;                   // Total 3D velocity dispersion            [km/s]
  //  float     spin[3];                   // Specific angular momentum vector        [Mpc/h*km/s]
  //  float     q_triaxial;                // Triaxial shape parameter q=b/a
  //  float     s_triaxial;                // Triaxial shape parameter s=c/a
  //  float     shape_eigen_vectors[3][3]; // Normalized triaxial shape eigenvectors
  //};

  // Read matches
  int    *n_subgroups  =NULL;
  int    *n_groups     =NULL;
  int    *n_particles_i=NULL;
  int    *n_particles_j=NULL;
  int    *match_ids    =NULL;
  float  *match_score  =NULL;
  size_t *match_index  =NULL;
  int     n_halos_i;
  int     n_halos_j;
  int     n_files;
  int     n_subgroups_max;
  int     n_groups_max;
  int     n_halos_max;
  read_matches_header(filename_matches_root,
                      0,
                      MAX(i_read,j_read),
                      1,
                      &n_files,
                      &n_subgroups,
                      &n_groups,
                      &n_subgroups_max,
                      &n_groups_max,
                      &n_halos_max);
  read_matches(filename_matches_root,
               i_read,
               j_read,
               n_halos_max,
               matches_read_mode,
               &n_halos_i,
               &n_halos_j,
               n_particles_i,
               n_particles_j,
               NULL,
               NULL,
               match_ids,
               match_score,
               match_index,
               NULL,
               FALSE);

  // Create a storage array mapping the indices of the second catalog
  //    to those of the halos they are matched to in the first catalog
  int  i_halo;
  int  j_halo;
  int *storage_index;
  storage_index=(int *)SID_malloc(sizeof(int)*n_halos_j);
  for(j_halo=0;j_halo<n_halos_j;j_halo++)
     storage_index[j_halo]=-1;
  for(i_halo=0,j_halo=0;j_halo<n_halos_j && i_halo<n_halos_i;j_halo++){
     while(match_ids[match_index[i_halo]]<j_halo && i_halo<(n_halos_i-1)) i_halo++;
     if(match_ids[match_index[i_halo]]<j_halo)                            i_halo++;
     if(match_ids[match_index[i_halo]]==j_halo)
        storage_index[j_halo]=match_index[i_halo];
  }

  // Open catalog files
  fp_catalog_info fp_properties_i;
  fp_catalog_info fp_properties_j;
  fopen_catalog(filename_catalog_root,
                i_read,
                catalog_read_mode,
                &fp_properties_i);
  fopen_catalog(filename_catalog_root,
                j_read,
                catalog_read_mode,
                &fp_properties_j);

  // Read catalogs
  halo_properties_info *properties_i;
  halo_properties_info *properties_j;
  properties_i    =(halo_properties_info *)SID_malloc(sizeof(halo_properties_info)*n_halos_i);
  properties_j    =(halo_properties_info *)SID_malloc(sizeof(halo_properties_info)*n_halos_i);
  for(i_halo=0;i_halo<n_halos_i;i_halo++)
     fread_catalog_file(&fp_properties_i,NULL,NULL,&(properties_i[i_halo]),NULL,i_halo);
  for(j_halo=0;j_halo<n_halos_j;j_halo++){
     if(storage_index[j_halo]>=0)
        fread_catalog_file(&fp_properties_j,NULL,NULL,&(properties_j[storage_index[j_halo]]),NULL,j_halo);
  }
  fclose_catalog(&fp_properties_i);
  fclose_catalog(&fp_properties_j);

  // Write results
  int   i_column=0;
  FILE *fp_out;
  fp_out=fopen(filename_out,"w");
  fprintf(fp_out,"# Catalog for matches {root %s} and catalog {root %s}; snap No. %d to %d.\n",
                 filename_matches_root,
                 filename_catalog_root,
                 i_read,j_read);
  fprintf(fp_out,"# Columns:(%02d) id (catalog No. 1)\n",          i_column++);
  fprintf(fp_out,"#         (%02d) id (catalog No. 2)\n",          i_column++);
  fprintf(fp_out,"#         (%02d)  M (catalog No. 1) [M_sol/h]\n",i_column++);
  fprintf(fp_out,"#         (%02d)  M (catalog No. 2) [M_sol/h]\n",i_column++);
  fprintf(fp_out,"#         (%02d)  x (catalog No. 1) [Mpc/h]\n",  i_column++);
  fprintf(fp_out,"#         (%02d)  y (catalog No. 1) [Mpc/h]\n",  i_column++);
  fprintf(fp_out,"#         (%02d)  z (catalog No. 1) [Mpc/h]\n",  i_column++);
  fprintf(fp_out,"#         (%02d)  x (catalog No. 2) [Mpc/h]\n",  i_column++);
  fprintf(fp_out,"#         (%02d)  y (catalog No. 2) [Mpc/h]\n",  i_column++);
  fprintf(fp_out,"#         (%02d)  z (catalog No. 2) [Mpc/h]\n",  i_column++);
  for(i_halo=0;i_halo<n_halos_i;i_halo++){
     fprintf(fp_out,"%10d %10d %10.4le %10.4le %10.4f %10.4f %10.4f %10.4f %10.4f %10.4f\n",
                    i_halo,
                    match_ids[i_halo],
                    properties_i[i_halo].M_vir,
                    properties_j[i_halo].M_vir,
                    properties_i[i_halo].position_COM[0],
                    properties_i[i_halo].position_COM[1],
                    properties_i[i_halo].position_COM[2],
                    properties_j[i_halo].position_COM[0],
                    properties_j[i_halo].position_COM[1],
                    properties_j[i_halo].position_COM[2]);
  }
  fclose(fp_out);

  // Clean-up
  SID_free(SID_FARG n_subgroups);
  SID_free(SID_FARG n_groups);
  SID_free(SID_FARG n_particles_i);
  SID_free(SID_FARG n_particles_j);
  SID_free(SID_FARG properties_i);
  SID_free(SID_FARG properties_j);
  SID_free(SID_FARG match_ids);
  SID_free(SID_FARG match_score);
  SID_free(SID_FARG match_index);
  SID_free(SID_FARG storage_index);

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
Exemplo n.º 10
0
int main(int argc, char *argv[]){
  plist_info plist;
  char       filename_in_root[256];
  char       filename_out_root[256];
  char       filename_in[256];
  char       filename_out[256];
  int        subsample_factor;
  int        i_snap,i_file,j_file;
  int        n_files;
  unsigned int n_local[N_GADGET_TYPE];
  int          i_type;
  size_t     i_particle,j_particle;
  FILE      *fp;
  FILE      *fp_out;
  int        record_length_in;
  int        record_length_out;
  GBPREAL       position[3];
  GBPREAL       velocity[3];
  int        ID_int;
  RNG_info   RNG;
  int        seed_init=10463743;
  int        seed;
  GBPREAL       RNG_max;
  long long n_all_keep[N_GADGET_TYPE];
  long long n_all_init[N_GADGET_TYPE];
  unsigned int n_all_high_word[N_GADGET_TYPE];
  unsigned int n_all[N_GADGET_TYPE];
  double    mass_array[N_GADGET_TYPE];
  int       n_keep_total;
  int       n_init_total;
  int       record_length;
  int       flag_long_ids;
  int       id_in_int;
  long long id_in_long;
  int       n_groups;

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

  // Parse command line
  if(argc!=4){
    fprintf(stderr,"\n syntax: %s filename_in_root snapshot_number subsample_factor\n",argv[0]);
    fprintf(stderr," ------\n\n");
    return(ERROR_SYNTAX);
  }
  else{
    strcpy(filename_in_root, argv[1]);
    i_snap          =atoi(argv[2]);
    subsample_factor=atoi(argv[3]);
  }
  sprintf(filename_out_root,"%s_subsample",filename_in_root);

  RNG_max=1./(GBPREAL)(subsample_factor);

  SID_log("Subsampling GADGET file by a factor of %d...",SID_LOG_OPEN,subsample_factor);

  // Grab info from first header
  // Read the header and determine the input file-format
  gadget_read_info   fp_gadget;
  int                flag_filefound=init_gadget_read(filename_in_root,i_snap,&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;
  if(!flag_filefound)
     SID_trap_error("File not found.",ERROR_LOGIC);
  if(flag_filefound)
    n_files =header.n_files;
  else
    n_files=0;
  if(n_files>0){
     init_seed_from_clock(&seed_init);
     SID_log("Seed=%d",SID_LOG_COMMENT,seed_init);
     
     SID_log("Counting particles in %d files...",SID_LOG_OPEN|SID_LOG_TIMER,n_files);
     for(i_type=0;i_type<N_GADGET_TYPE;i_type++)
       n_all_keep[i_type]=0;
     for(i_file=SID.My_rank,j_file=0;j_file<n_files;i_file+=SID.n_proc,j_file+=SID.n_proc){
       if(n_files>1)
          SID_log("Processing file(s) %d->%d...",SID_LOG_OPEN,j_file,MIN(j_file+SID.n_proc-1,n_files-1));
       if(i_file<n_files){
         set_gadget_filename(&fp_gadget,i_file,filename_in);
         fp=fopen(filename_in,"r");
         fread_verify(&record_length_in,sizeof(int),1,fp);
         fread_verify(&header,sizeof(gadget_header_info),1,fp);
         fread_verify(&record_length_out,sizeof(int),1,fp);
         seed=seed_init+1387*i_file;
         init_RNG(&seed,&RNG,RNG_DEFAULT);
         for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
           for(j_particle=0;j_particle<header.n_file[i_type];j_particle++){
             if(random_number(&RNG)<=RNG_max)
               n_all_keep[i_type]++;
           }
         }
         free_RNG(&RNG);
         fclose(fp);
       }
       if(n_files>1)
          SID_log("Done.",SID_LOG_CLOSE);
     }
     SID_Allreduce(SID_IN_PLACE,n_all_keep,N_GADGET_TYPE,SID_LONG_LONG,SID_SUM,SID.COMM_WORLD);
     SID_log("Done.",SID_LOG_CLOSE);
     SID_log("Header properties will be...",SID_LOG_OPEN);
     for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
       n_all_high_word[i_type]=(unsigned int)(((long long)n_all_keep[i_type])>>32);
       n_all[i_type]          =(unsigned int)(((long long)n_all_keep[i_type])-(((long long)(n_all_high_word[i_type]))<<32));
       n_all_init[i_type]     =((long long)(header.n_all_lo_word[i_type]))+(((long long)(header.n_all_hi_word[i_type]))<<32);
       if(n_all_keep[i_type]>0)
         mass_array[i_type]=header.mass_array[i_type]*((double)n_all_init[i_type]/(double)n_all_keep[i_type]);
       else
         mass_array[i_type]=0.;
     }
     for(i_type=0;i_type<N_GADGET_TYPE;i_type++)
       SID_log("mass_array[%d]=%le (was %le)", SID_LOG_COMMENT,i_type,mass_array[i_type],header.mass_array[i_type]);
     for(i_type=0;i_type<N_GADGET_TYPE;i_type++)
       SID_log("n_all[%d]     =%d (was %d)",SID_LOG_COMMENT,i_type,n_all[i_type],header.n_all_lo_word[i_type]);
     for(i_type=0;i_type<N_GADGET_TYPE;i_type++)
       SID_log("high_word[%d] =%d (was %d)",SID_LOG_COMMENT,i_type,n_all_high_word[i_type],header.n_all_hi_word[i_type]);
     SID_log("Done.",SID_LOG_CLOSE);
     SID_log("Writing subsampled snapshot...",SID_LOG_OPEN|SID_LOG_TIMER);
     for(i_file=SID.My_rank,j_file=0;j_file<n_files;i_file+=SID.n_proc,j_file+=SID.n_proc){
       if(n_files>1)
          SID_log("Processing file(s) %d->%d...",SID_LOG_OPEN,j_file,MIN(j_file+SID.n_proc-1,n_files-1));
       if(i_file<n_files){
         set_gadget_filename(&fp_gadget,i_file,filename_in);
         change_gadget_filename(filename_in_root,"snapshot_subsample",i_snap,i_file,flag_multifile,flag_file_type,filename_out);
         if(i_file==0){
            FILE *fp_test;
            char  filename_out_directory[MAX_FILENAME_LENGTH];
            strcpy(filename_out_directory,filename_out);
            strip_file_root(filename_out_directory);
            if((fp_test=fopen(filename_out_directory,"r"))==NULL)
               mkdir(filename_out_directory,02755);
            else
               fclose(fp_test);
         }
         fp    =fopen(filename_in, "r");
         fp_out=fopen(filename_out,"w");

         // Header
         fread_verify(&record_length_in,sizeof(int),1,fp);
         fread_verify(&header,sizeof(gadget_header_info),1,fp);
         fread_verify(&record_length_out,sizeof(int),1,fp);
         for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
           n_local[i_type]               =header.n_file[i_type];
           header.n_file[i_type]         =0;
           header.mass_array[i_type]     =mass_array[i_type];
           header.n_all_lo_word[i_type]  =n_all[i_type];
           header.n_all_hi_word[i_type]  =n_all_high_word[i_type];
         }
         seed=seed_init+1387*i_file;
         init_RNG(&seed,&RNG,RNG_DEFAULT);
         for(i_type=0,n_keep_total=0,n_init_total=0;i_type<N_GADGET_TYPE;i_type++){
           for(j_particle=0;j_particle<n_local[i_type];j_particle++,i_particle++){
             if(random_number(&RNG)<=RNG_max){
               header.n_file[i_type]++;
               n_keep_total++;
             }
             n_init_total++;
           }
         }
         free_RNG(&RNG);
         record_length=record_length_in;
         fwrite(&record_length,sizeof(int),1,fp_out);
         fwrite(&header,sizeof(gadget_header_info),1,fp_out);
         fwrite(&record_length,sizeof(int),1,fp_out);

         // Positions
         record_length=3*sizeof(GBPREAL)*n_keep_total;
         seed=seed_init+1387*i_file;
         init_RNG(&seed,&RNG,RNG_DEFAULT);
         fread_verify(&record_length_in,sizeof(int),1,fp);
         fwrite(&record_length,sizeof(int),1,fp_out);
         for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
           for(j_particle=0;j_particle<n_local[i_type];j_particle++){
             fread_verify(position,sizeof(GBPREAL),3,fp);
             if(random_number(&RNG)<=RNG_max)
               fwrite(position,sizeof(GBPREAL),3,fp_out);
           }
         }
         fread_verify(&record_length_out,sizeof(int),1,fp);
         fwrite(&record_length,sizeof(int),1,fp_out);
         free_RNG(&RNG);

         // Velocities
         record_length=3*sizeof(GBPREAL)*n_keep_total;
         seed=seed_init+1387*i_file;
         init_RNG(&seed,&RNG,RNG_DEFAULT);
         fread_verify(&record_length_in,sizeof(int),1,fp);
         fwrite(&record_length,sizeof(int),1,fp_out);
         for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
           for(j_particle=0;j_particle<n_local[i_type];j_particle++){
             fread_verify(velocity,sizeof(GBPREAL),3,fp);
             if(random_number(&RNG)<=RNG_max)
               fwrite(velocity,sizeof(GBPREAL),3,fp_out);
           }
         }
         fread_verify(&record_length_out,sizeof(int),1,fp);
         fwrite(&record_length,sizeof(int),1,fp_out);
         free_RNG(&RNG);

         // IDs
         seed=seed_init+1387*i_file;
         init_RNG(&seed,&RNG,RNG_DEFAULT);
         fread_verify(&record_length_in,sizeof(int),1,fp);
         if(record_length_in/sizeof(int)==n_init_total){
           flag_long_ids=FALSE;
           record_length=n_keep_total*sizeof(int);
         }
         else{
           flag_long_ids=TRUE;
           record_length=n_keep_total*sizeof(long long);
         }
         fwrite(&record_length,sizeof(int),1,fp_out);
         if(flag_long_ids){
           for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
             for(j_particle=0;j_particle<n_local[i_type];j_particle++){
               fread_verify(&id_in_long,sizeof(long long),1,fp);
               if(random_number(&RNG)<=RNG_max)
                 fwrite(&id_in_long,sizeof(long long),1,fp_out);
             }
           }
         }
         else{
           for(i_type=0;i_type<N_GADGET_TYPE;i_type++){
             for(j_particle=0;j_particle<n_local[i_type];j_particle++){
               fread_verify(&id_in_int,sizeof(int),1,fp);
               if(random_number(&RNG)<=RNG_max)
                 fwrite(&id_in_int,sizeof(int),1,fp_out);
             }
           }
         }
         fread_verify(&record_length_out,sizeof(int),1,fp);
         fwrite(&record_length,sizeof(int),1,fp_out);
         free_RNG(&RNG);

         fclose(fp);
         fclose(fp_out);
       }
       if(n_files>1)
          SID_log("Done.",SID_LOG_CLOSE);
     }
     SID_log("Done.",SID_LOG_CLOSE);
  }
Exemplo n.º 11
0
int main(int argc, char *argv[]){
  int     n_search;
  int     i_halo;
  char    filename_SSimPL_root[MAX_FILENAME_LENGTH];
  char    filename_in[MAX_FILENAME_LENGTH];
  char    group_text_prefix[4];
  int     n_files;
  int     k_read;
  int     max_n_groups;
  int     l_read;
  int     n_groups;
  int     j_read;
  int     mode;
  int     n_groups_i;
  int     n_groups_j;
  int     j_halo;
  int     match;
  int     i_read;
  int     i_read_start;
  int     i_read_stop;
  SID_fp  fp_in;

  SID_init(&argc,&argv,NULL,NULL);
  SID_log("Writing match score table...",SID_LOG_OPEN);

  FILE *fp=stdout;
  fprintf(fp,"# Table of match score values.\n");
  fprintf(fp,"# Match Rank Index=\n",MATCH_SCORE_RANK_INDEX);
  fprintf(fp,"# Column (01): n_particles\n");
  fprintf(fp,"#        (02): max score       (n_particles)\n");
  fprintf(fp,"#        (03): min match score (n_particles)\n");
  fprintf(fp,"#        (04): max score       (0.90*n_particles)\n");
  fprintf(fp,"#        (05): max score       (0.80*n_particles)\n");
  fprintf(fp,"#        (06): max score       (0.70*n_particles)\n");
  fprintf(fp,"#        (07): max score       (0.60*n_particles)\n");
  fprintf(fp,"#        (08): max score       (0.50*n_particles)\n");
  fprintf(fp,"#        (09): max score       (0.40*n_particles)\n");
  fprintf(fp,"#        (10): max score       (0.30*n_particles)\n");
  fprintf(fp,"#        (11): max score       (0.20*n_particles)\n");
  fprintf(fp,"#        (12): max score       (0.10*n_particles)\n");
  fprintf(fp,"#        (13): max score       (0.50*n_particles)\n");
  fprintf(fp,"#        (14): max score       (0.01*n_particles)\n");
  for(double n_particles=1.;n_particles<1e9;n_particles*=1.2){
     fprintf(fp,"%le %le %le %le %le %le %le %le %le %le %le %le %le %le\n",
             n_particles,
             maximum_match_score(n_particles),
             minimum_match_score(n_particles),
             maximum_match_score(0.9*n_particles),
             maximum_match_score(0.8*n_particles),
             maximum_match_score(0.7*n_particles),
             maximum_match_score(0.6*n_particles),
             maximum_match_score(0.5*n_particles),
             maximum_match_score(0.4*n_particles),
             maximum_match_score(0.3*n_particles),
             maximum_match_score(0.2*n_particles),
             maximum_match_score(0.1*n_particles),
             maximum_match_score(0.05*n_particles),
             maximum_match_score(0.01*n_particles));
  }
  if(fp!=stdout && fp!=stderr)
     fclose(fp);

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
Exemplo n.º 12
0
int main(int argc, char *argv[]){
  plist_info  plist;
  char        filename_groups_root[256];
  char        filename_snapshot_root[256];
  char        filename_snapshot[256];
  char        filename_number[256];
  char        filename_output_properties[256];
  char        filename_output_properties_dir[256];
  char        filename_output_profiles[256];
  char        filename_output_profiles_dir[256];
  char        filename_output_properties_temp[256];
  char        filename_output_profiles_temp[256];
  char        filename_input_properties[256];
  char        filename_input_properties_dir[256];
  char        filename_input_profiles[256];
  char        filename_input_profiles_dir[256];
  char        filename_input_properties_temp[256];
  char        filename_input_profiles_temp[256];
  char        group_text_prefix[4];
  int         n_groups_process;
  int         n_groups;
  int         n_groups_all;
  int         j_file;
  int         j_file_in;
  int         i_group;
  int         i_file_lo;
  int         i_file_hi;
  int         i_file;
  int         i_file_skip;
  int         i_particle;
  int         j_particle;
  int         i_process;
  int         n_particles;
  int         n_particles_max;
  GBPREAL       *x_array;
  GBPREAL       *y_array;
  GBPREAL       *z_array;
  GBPREAL       *vx_array;
  GBPREAL       *vy_array;
  GBPREAL       *vz_array;
  int        *n_particles_groups_process;
  int        *n_particles_groups;
  int        *n_particles_subgroups;
  int        *group_offset;
  size_t      n_particles_in_groups;
  size_t     *ids_snapshot;
  size_t     *ids_groups;
  size_t     *ids_sort_index;
  size_t     *ids_snapshot_sort_index;
  size_t     *ids_groups_sort_index;
  size_t      n_particles_snapshot;
  int         i_value;
  double      h_Hubble;
  double      Omega_M;
  double      Omega_b;
  double      Omega_Lambda;
  double      f_gas;
  double      Omega_k;
  double      sigma_8;
  double      n_spec;
  double      redshift;
  double      box_size;
  double      particle_mass;
  
  int         r_val;
  struct      stat file_stats;
  size_t      n_bytes;
  size_t      n_bytes_buffer;
  void       *buffer;
  
  FILE       *fp_properties;
  FILE       *fp_profiles;
  FILE       *fp_properties_temp;
  FILE       *fp_profiles_temp;
  cosmo_info *cosmo;
  halo_properties_info  properties;
  halo_profile_info     profile;
  int                   n_files,n_files_i;
  int                   n_files_temp=1;
  int                   flag_process_profiles;

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

  // Fetch user inputs
  strcpy(filename_groups_root,argv[1]);
  i_file_lo  =atoi(argv[2]);
  i_file_hi  =atoi(argv[3]);
  i_file_skip=atoi(argv[4]);

  flag_process_profiles=FALSE;

  SID_log("Processing group/subgroup statistics for files #%d->#%d...",SID_LOG_OPEN|SID_LOG_TIMER,i_file_lo,i_file_hi);

  for(i_file=i_file_lo;i_file<=i_file_hi;i_file+=i_file_skip){
    sprintf(filename_number,"%03d", i_file);
    SID_log("Processing file #%d...",SID_LOG_OPEN|SID_LOG_TIMER,i_file);

    for(i_process=0;i_process<2;i_process++){
      switch(i_process){
      case 0:
        sprintf(group_text_prefix,"");
        break;
      case 1:
        sprintf(group_text_prefix,"sub");
        break;
      }
      SID_log("Processing %sgroup files...",SID_LOG_OPEN|SID_LOG_TIMER,group_text_prefix);
      char filename_base[MAX_FILENAME_LENGTH];
      sprintf(filename_base,"%s_%03d",filename_groups_root,i_file);
      strip_path(filename_base);
      sprintf(filename_input_properties,     "%s_%s.catalog_%sgroups_properties",    filename_groups_root,filename_number,group_text_prefix);
      sprintf(filename_output_properties,    "%s_cat_%s.catalog_%sgroups_properties",filename_groups_root,filename_number,group_text_prefix);
      sprintf(filename_input_properties_dir, "%s",filename_input_properties);
      sprintf(filename_output_properties_dir,"%s",filename_output_properties);
      if(flag_process_profiles){
        sprintf(filename_input_profiles,     "%s_%s.catalog_%sgroups_profiles",    filename_groups_root,filename_number,group_text_prefix);
        sprintf(filename_output_profiles,    "%s_cat_%s.catalog_%sgroups_profiles",filename_groups_root,filename_number,group_text_prefix);
        sprintf(filename_input_profiles_dir, "%s",filename_input_profiles);
        sprintf(filename_output_profiles_dir,"%s",filename_output_profiles);
      }

      // Get the number of files involved
      j_file=0;
      sprintf(filename_input_properties_temp,"%s/%s.catalog_%sgroups_properties.%d",filename_input_properties_dir,filename_base,group_text_prefix,j_file);
      if((fp_properties_temp=fopen(filename_input_properties_temp,"r"))==NULL)
         SID_trap_error("Can not open {%s}",ERROR_IO_OPEN,filename_output_properties_temp);
      fread(&j_file,      sizeof(int),1,fp_properties_temp);
      fread(&n_files,     sizeof(int),1,fp_properties_temp);
      fclose(fp_properties_temp);

      // Concatinate the temporary files together
      fp_properties=fopen(filename_output_properties,"w");
      if(flag_process_profiles)
        fp_profiles  =fopen(filename_output_profiles,  "w");
      for(j_file=0;j_file<n_files;j_file++){

        SID_log("Processing file No. %d of %d...",SID_LOG_OPEN,j_file+1,n_files);
        // Set filenames
        sprintf(filename_input_properties_temp,"%s/%s.catalog_%sgroups_properties.%d",filename_input_properties_dir,filename_base,group_text_prefix,j_file);
        sprintf(filename_input_profiles_temp,  "%s/%s.catalog_%sgroups_profiles.%d",  filename_input_properties_dir,filename_base,group_text_prefix,j_file);
        // Cat properties
        if((fp_properties_temp=fopen(filename_input_properties_temp,"r"))==NULL)
           SID_trap_error("Can't open file {%s}",ERROR_IO_OPEN,filename_output_properties_temp);
        fread(&j_file_in,   sizeof(int),1,fp_properties_temp);
        fread(&n_files_i,   sizeof(int),1,fp_properties_temp);
        fread(&n_groups,    sizeof(int),1,fp_properties_temp);
        fread(&n_groups_all,sizeof(int),1,fp_properties_temp);
        SID_log("Processing properties (%d of %d %sgroups)...",SID_LOG_OPEN,n_groups,n_groups_all,group_text_prefix);
        if(j_file==0){
          fwrite(&j_file,      sizeof(int),1,fp_properties);
          fwrite(&n_files_temp,sizeof(int),1,fp_properties);
          fwrite(&n_groups_all,sizeof(int),1,fp_properties);
          fwrite(&n_groups_all,sizeof(int),1,fp_properties);
        }
        for(i_group=0;i_group<n_groups;i_group++){
          fread( &properties,sizeof(halo_properties_info),1,fp_properties_temp);
          fwrite(&properties,sizeof(halo_properties_info),1,fp_properties);                  
        }
        fclose(fp_properties_temp);
        SID_log("Done.",SID_LOG_CLOSE);

        // Cat profiles
        if(flag_process_profiles){
          fp_profiles_temp=fopen(filename_output_profiles_temp,"r");
          fread(&j_file_in,   sizeof(int),1,fp_profiles_temp);
          fread(&n_files_i,   sizeof(int),1,fp_profiles_temp);
          fread(&n_groups,    sizeof(int),1,fp_profiles_temp);
          fread(&n_groups_all,sizeof(int),1,fp_profiles_temp);
          SID_log("Processing profiles (%d of %d %sgroups)...",SID_LOG_OPEN,n_groups,n_groups_all,group_text_prefix);
          if(j_file==0){
            fwrite(&j_file,      sizeof(int),1,fp_profiles);
            fwrite(&n_files_temp,sizeof(int),1,fp_profiles);
            fwrite(&n_groups_all,sizeof(int),1,fp_profiles);
            fwrite(&n_groups_all,sizeof(int),1,fp_profiles);
          }
          for(i_group=0;i_group<n_groups;i_group++){
            fread( &(profile.n_bins),sizeof(int),1,fp_profiles_temp);
            fwrite(&(profile.n_bins),sizeof(int),1,fp_profiles);
            if(profile.n_bins>0){
              fread( profile.bins,sizeof(halo_profile_bin_info),profile.n_bins,fp_profiles_temp);
              fwrite(profile.bins,sizeof(halo_profile_bin_info),profile.n_bins,fp_profiles);
            }
          }
          fclose(fp_profiles_temp);
          SID_log("Done.",SID_LOG_CLOSE);
        }
        SID_log("Done. (file %3d of %3d)",SID_LOG_CLOSE,j_file,n_files);
      }
      fclose(fp_properties);
      if(flag_process_profiles)
        fclose(fp_profiles);
      SID_log("Done.",SID_LOG_CLOSE);
    }
    SID_log("Done.",SID_LOG_CLOSE);
  }

  SID_log("Done.",SID_LOG_CLOSE);
  SID_exit(ERROR_NONE);
}
Exemplo n.º 13
0
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);
}
Exemplo n.º 14
0
int main(int argc, char *argv[]){
  char        filename_tree_in[256];
  int         select_tree;
  int         n_trees;
  int         n_halos_total;
  int        *n_halos;
  int         i_tree;
  FILE       *fp;
  halo_properties_SAGE_info  *halos;
  halo_properties_SAGE_info   halo;
  int        *snap_num;
  size_t     *snap_num_index;
  int         i_snap,i_halo,j_halo,k_halo;
  int         n_halos_snap;
  int        *group_halo_first;
  int         group_halo_last;
  size_t     *group_halo_first_index;
  int        *snap_index;
  int descendant_min,descendant_max;
  int progenitor_first_min,progenitor_first_max;
  int progenitor_next_min,progenitor_next_max;
  int group_halo_first_min,group_halo_first_max;
  int group_halo_next_min,group_halo_next_max;
  int snap_num_min,snap_num_max;
  int halo_index_min,halo_index_max;
  int n_gal=0;
  int max_snap=0;

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

  // Fetch user inputs
  strcpy(filename_tree_in,argv[1]);
  select_tree=atoi(argv[2]);

  SID_log("Displaying tree %d from {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,select_tree,filename_tree_in);
  fp=fopen(filename_tree_in,"r");
  fread_verify(&n_trees,      sizeof(int),1,fp);
  fread_verify(&n_halos_total,sizeof(int),1,fp);
  SID_log("(%d trees and %d halos)...",SID_LOG_CONTINUE,n_trees,n_halos_total);
  n_halos=(int *)SID_malloc(sizeof(int)*n_trees);
  fread_verify(n_halos,sizeof(int),n_trees,fp);
  for(i_tree=0;i_tree<select_tree;i_tree++){
    for(i_halo=0;i_halo<n_halos[i_tree];i_halo++){
      fread_verify(&halo,sizeof(halo_properties_SAGE_info),1,fp);
      max_snap=MAX(max_snap,halo.snap_num);
    }
  }
  halos               =(halo_properties_SAGE_info *)SID_malloc(sizeof(halo_properties_SAGE_info)*n_halos[i_tree]);
  snap_num            =(int       *)SID_malloc(sizeof(int)*n_halos[i_tree]);
  snap_index          =(int       *)SID_malloc(sizeof(int)*n_halos[i_tree]);
  group_halo_first    =(int       *)SID_malloc(sizeof(int)*n_halos[i_tree]);
  fread_verify(halos,sizeof(halo_properties_SAGE_info),n_halos[i_tree],fp);
  descendant_min      =10000;
  descendant_max      =    0;
  progenitor_first_min=10000;
  progenitor_first_max=    0;
  progenitor_next_min =10000;
  progenitor_next_max =    0;
  group_halo_first_min=10000;
  group_halo_first_max=    0;
  group_halo_next_min =10000;
  group_halo_next_max =    0;
  snap_num_min        =10000;
  snap_num_max        =    0;
  halo_index_min      =10000;
  halo_index_max      =    0; 
  for(i_halo=0;i_halo<n_halos[i_tree];i_halo++){
    snap_num[i_halo] =halos[i_halo].snap_num;

    if(halos[i_halo].descendant>=0)
      descendant_min      =MIN(descendant_min,halos[i_halo].descendant);
    if(halos[i_halo].progenitor_first>=0)
      progenitor_first_min=MIN(progenitor_first_min,halos[i_halo].progenitor_first);
    if(halos[i_halo].progenitor_next>=0)
      progenitor_next_min =MIN(progenitor_next_min,halos[i_halo].progenitor_next);
    if(halos[i_halo].group_halo_first>=0)
      group_halo_first_min=MIN(group_halo_first_min,halos[i_halo].group_halo_first);
    if(halos[i_halo].group_halo_next>=0)
      group_halo_next_min =MIN(group_halo_next_min,halos[i_halo].group_halo_next);
    if(halo.snap_num>=0)
      snap_num_min        =MIN(snap_num_min,halos[i_halo].snap_num);
    if(halos[i_halo].halo_index>=0)
      halo_index_min      =MIN(halo_index_min,halos[i_halo].halo_index);

    descendant_max      =MAX(descendant_max,halos[i_halo].descendant);
    progenitor_first_max=MAX(progenitor_first_max,halos[i_halo].progenitor_first);
    progenitor_next_max =MAX(progenitor_next_max,halos[i_halo].progenitor_next);
    group_halo_first_max=MAX(group_halo_first_max,halos[i_halo].group_halo_first);
    group_halo_next_max =MAX(group_halo_next_max,halos[i_halo].group_halo_next);
    snap_num_max        =MAX(snap_num_max,halos[i_halo].snap_num);
    halo_index_max      =MAX(halo_index_max,halos[i_halo].halo_index);
    max_snap=MAX(max_snap,halos[i_halo].snap_num);
  }

  i_tree++;
  for(;i_tree<n_trees;i_tree++){
    for(i_halo=0;i_halo<n_halos[i_tree];i_halo++){
      fread_verify(&halo,sizeof(halo_properties_SAGE_info),1,fp);
      max_snap=MAX(max_snap,halo.snap_num);
    }
  }

  rewind(fp); 
  fread_verify(&n_trees,      sizeof(int),1,fp);
  fread_verify(&n_halos_total,sizeof(int),1,fp); 
  for(i_tree=0,n_gal=0;i_tree<n_trees;i_tree++){
    for(i_halo=0;i_halo<n_halos[i_tree];i_halo++){
      fread_verify(&halo,sizeof(halo_properties_SAGE_info),1,fp);
      if(halo.snap_num==max_snap) n_gal++;
    }
  }

  SID_log("n_trees          =%d",    SID_LOG_COMMENT,n_trees);
  SID_log("n_halos[snap=%3d]=%d",    SID_LOG_COMMENT,n_gal);
  SID_log("n_halos_total    =%d",    SID_LOG_COMMENT,n_halos_total);
  SID_log("Descendants      =%d->%d",SID_LOG_COMMENT,descendant_min,      descendant_max);
  SID_log("Progenitor_first =%d->%d",SID_LOG_COMMENT,progenitor_first_min,progenitor_first_max);
  SID_log("Progenitor_next  =%d->%d",SID_LOG_COMMENT,progenitor_next_min, progenitor_next_max);
  SID_log("Group_halo_first =%d->%d",SID_LOG_COMMENT,group_halo_first_min,group_halo_first_max);
  SID_log("Group_halo_next  =%d->%d",SID_LOG_COMMENT,group_halo_next_min, group_halo_next_max);
  SID_log("Snap_num         =%d->%d",SID_LOG_COMMENT,snap_num_min,        snap_num_max);
  SID_log("Halo_index       =%d->%d",SID_LOG_COMMENT,halo_index_min,      halo_index_max);

  merge_sort((void *)snap_num,(size_t)n_halos[i_tree],&snap_num_index,SID_INT,SORT_COMPUTE_INDEX,FALSE);
  for(i_snap=snap_num_max,i_halo=n_halos[i_tree]-1;i_snap>=snap_num_min && i_halo>=0;i_snap--){
    n_halos_snap=0;
    while(snap_num[snap_num_index[i_halo]]==i_snap && i_halo>0){
      n_halos_snap++;
      i_halo--;
    }
    if(snap_num[snap_num_index[i_halo]]==i_snap){
      n_halos_snap++;
      i_halo--;
    }
    for(j_halo=0;j_halo<n_halos_snap;j_halo++){
      group_halo_first[j_halo]=halos[snap_num_index[i_halo+j_halo+1]].group_halo_first;
      snap_index[j_halo]      =snap_num_index[i_halo+j_halo+1];
    }
    merge_sort((void *)group_halo_first,(size_t)n_halos_snap,&group_halo_first_index,SID_INT,SORT_COMPUTE_INDEX,FALSE);
    group_halo_last=-99;
    if(n_halos_snap>0)
      printf("Snap #%3d: ",i_snap);
    for(j_halo=0;j_halo<n_halos_snap;j_halo++){
      k_halo=snap_index[group_halo_first_index[j_halo]];
      if(group_halo_last!=halos[k_halo].group_halo_first){
        if(j_halo!=0)
          printf(") ");        
        printf("(");        
      }
      else
        printf(" ");        
      // Generate output
/*
      printf("I:%5d->%5d/S:%5d/PF:%5d/PN:%5d/G:%5d->%5d",
             k_halo,
             halos[k_halo].descendant,
             halos[k_halo].n_particles,
             halos[k_halo].progenitor_first,
             halos[k_halo].progenitor_next,
             halos[k_halo].group_halo_first,
             halos[k_halo].group_halo_next);      
*/
/*
           printf("I%05d.D%05d.S%05d.F%05d.N%05d.f%05d.n%05d",
                  k_halo,
                  halos[k_halo].descendant,
                  halos[k_halo].n_particles,
                  halos[k_halo].progenitor_first,
                  halos[k_halo].progenitor_next,
                  halos[k_halo].group_halo_first,
                  halos[k_halo].group_halo_next);      
*/
/*
           printf("%05d",
                  halos[k_halo].n_particles);
*/
/*
           printf("%05d/%05d/%05d",
                  k_halo,halos[k_halo].descendant,
                  halos[k_halo].n_particles);
*/
           printf("%05d/%05d/%05d/%05d",
                  k_halo,
                  halos[k_halo].descendant,
                  halos[k_halo].group_halo_first,
                  halos[k_halo].group_halo_next);
      group_halo_last=halos[k_halo].group_halo_first;
    }
    if(n_halos_snap>0)
      printf(")\n");
    SID_free((void **)&group_halo_first_index);
  }
  SID_free((void **)&snap_num_index);

  SID_free((void **)&snap_num);
  SID_free((void **)&snap_index);
  SID_free((void **)&group_halo_first);
  SID_free((void **)&n_halos);
  SID_free((void **)&halos);
  fclose(fp);
  SID_log("Done.",SID_LOG_CLOSE);

  SID_exit(0);
}
Exemplo n.º 15
0
int main(int argc, char *argv[]){
  char        filename_tree_in[256];
  int         n_trees;
  int         n_halos_total;
  int        *n_halos;
  int         i_tree;
  FILE       *fp;
  halo_properties_SAGE_info  *halos;
  halo_properties_SAGE_info   halo;
  int        *snap_num;
  size_t     *snap_num_index;
  int         i_snap,i_halo,j_halo,k_halo;
  int         n_halos_snap;
  int        *group_halo_first;
  int         group_halo_last;
  size_t     *group_halo_first_index;
  int        *snap_index;
  int descendant_min,descendant_max;
  int progenitor_first_min,progenitor_first_max;
  int progenitor_next_min,progenitor_next_max;
  int group_halo_first_min,group_halo_first_max;
  int group_halo_next_min,group_halo_next_max;
  int snap_num_min,snap_num_max;
  int halo_index_min,halo_index_max;
  int n_gal=0;
  int max_snap=0;
  int n_halos_max;
  int n_subtrees;
  int halo_search;
  int flag_search;

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

  // Fetch user inputs
  strcpy(filename_tree_in,argv[1]);
  halo_search=atoi(argv[2]);

  SID_log("Finding halo #%d's tree in {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,halo_search,filename_tree_in);
  fp=fopen(filename_tree_in,"r");
  fread_verify(&n_trees,      sizeof(int),1,fp);
  fread_verify(&n_halos_total,sizeof(int),1,fp);
  SID_log("%d trees and %d halos",SID_LOG_COMMENT,n_trees,n_halos_total);
  n_halos=(int *)SID_malloc(sizeof(int)*n_trees);
  fread_verify(n_halos,sizeof(int),n_trees,fp);
  calc_max(n_halos,&n_halos_max,n_trees,SID_INT,CALC_MODE_DEFAULT);
  halos      =(halo_properties_SAGE_info *)SID_malloc(sizeof(halo_properties_SAGE_info)*n_halos_max);
  for(i_tree=0,flag_search=TRUE;i_tree<n_trees && flag_search;i_tree++){
    fread_verify(halos,sizeof(halo_properties_SAGE_info),n_halos[i_tree],fp);
    for(i_halo=0,n_subtrees=0;i_halo<n_halos[i_tree];i_halo++){
      if(halos[i_halo].halo_id==halo_search){
        flag_search=FALSE;
        SID_log("Found it in tree #%d",SID_LOG_COMMENT,i_tree);
      }
    }
  }
  if(flag_search)
    SID_log("COULD NOT FIND HALO #%d IN THIS FILE!",SID_LOG_COMMENT,halo_search);

  // Clean-up
  fclose(fp);
  SID_free((void **)&halos);
  SID_log("Done.",SID_LOG_CLOSE);

  SID_exit(0);
}
Exemplo n.º 16
0
int main(int argc, char *argv[]){
  char    filename_properties[256];
  char    filename_profiles[256];
  char    filename_out_root[256];
  char    filename_out[256];
  char    filename_SSimPL[MAX_FILENAME_LENGTH];
  char    filename_halo_type[MAX_FILENAME_LENGTH];
  int     snap_number;
  int     snap_number_start;
  int     snap_number_stop;
  int     snap_number_step;

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

  strcpy(filename_SSimPL,   argv[1]);
  strcpy(filename_halo_type,argv[2]);
  snap_number_start   =atoi(argv[3]);
  snap_number_stop    =atoi(argv[4]);
  snap_number_step    =atoi(argv[5]);
  strcpy(filename_out_root, argv[6]);

  int flag_use_profiles=FALSE;

  if(SID.I_am_Master){
    SID_log("Processing catalogs for snaps %d->%d...",SID_LOG_OPEN|SID_LOG_TIMER,snap_number_start,snap_number_stop);
    for(snap_number=snap_number_start;snap_number<=snap_number_stop;snap_number++){

         // Open halos
         char filename_halos[256];
         sprintf(filename_halos,"%s/halos/%s_%03d.catalog_groups",filename_SSimPL,filename_halo_type,snap_number);
         FILE *fp_halos=NULL;
         if((fp_halos=fopen(filename_halos,"r"))==NULL)
            SID_trap_error("Could not open halo file {%s} for reading.",ERROR_IO_OPEN,filename_halos);
         int n_groups_halos,group_offset_byte_size;
         fread_verify(&n_groups_halos,        sizeof(int),1,fp_halos);
         fread_verify(&group_offset_byte_size,sizeof(int),1,fp_halos);

         // Skip group sizes and offsets
         fseeko(fp_halos,(off_t)(n_groups_halos*(sizeof(int)+group_offset_byte_size)),SEEK_CUR);

         // Open catalogs
         char filename_cat_root[256];
         sprintf(filename_cat_root,"%s/catalogs/%s",filename_SSimPL,filename_halo_type);
         fp_catalog_info fp_catalog_groups;
         fp_catalog_info fp_catalog_subgroups;
         fopen_catalog(filename_cat_root,
                       snap_number,
                       READ_CATALOG_GROUPS|READ_CATALOG_PROPERTIES|READ_CATALOG_PROPERTIES,
                       &fp_catalog_groups);
         fopen_catalog(filename_cat_root,
                       snap_number,
                       READ_CATALOG_SUBGROUPS|READ_CATALOG_PROPERTIES|READ_CATALOG_PROPERTIES,
                       &fp_catalog_subgroups);

         // Open SO files if they're available
         fp_multifile_info fp_SO;
         int flag_use_SO=fopen_multifile("%s/catalogs/%s_%03d.catalog_groups_SO",sizeof(float),&fp_SO,filename_SSimPL,filename_halo_type,snap_number);
         if(flag_use_SO)
            SID_log("SO files present.",SID_LOG_COMMENT);

         // Sanity check
         if(n_groups_halos!=fp_catalog_groups.n_halos_total)
            SID_trap_error("Group counts in halo and catalog files don't match (ie. %d!=%d).",ERROR_LOGIC,n_groups_halos,fp_catalog_groups.n_halos_total);

         // Process halos
         SID_log("Processing snapshot #%03d...",SID_LOG_OPEN,snap_number);
         SID_log("(%d groups, %d subgroups)...",SID_LOG_CONTINUE,fp_catalog_groups.n_halos_total,fp_catalog_subgroups.n_halos_total);

         // Initialzie halo trend data structure
         halo_trend_info  halo_trend_data;
         char             filename_run[MAX_FILENAME_LENGTH];
         sprintf(filename_run,"%s/run/run.txt",filename_SSimPL);
         parameter_list_info *parameter_list=NULL;
         init_parameter_list(&parameter_list);
         add_parameter_to_list(parameter_list,"box_size",SID_DOUBLE,   PARAMETER_MODE_DEFAULT);
         add_parameter_to_list(parameter_list,"N_dark",  SID_SIZE_T,   PARAMETER_MODE_DEFAULT);
         add_parameter_to_list(parameter_list,"m_dark",  SID_DOUBLE,   PARAMETER_MODE_DEFAULT);
         read_gbpParam_file(filename_run,parameter_list);
         fetch_parameter_data(parameter_list,"box_size",&(halo_trend_data.box_size)); 
         fetch_parameter_data(parameter_list,"m_dark",  &(halo_trend_data.m_p)); 
         free_parameter_list(&parameter_list);
         char             filename_snaps[MAX_FILENAME_LENGTH];
         sprintf(filename_snaps,"%s/run/a_list.txt",filename_SSimPL);
         FILE *fp_snaps=fopen(filename_snaps,"r");
         size_t line_length=0;
         char  *line=NULL;
         halo_trend_data.n_snaps=count_lines_data(fp_snaps);
         halo_trend_data.z_list =(double *)SID_malloc(sizeof(double)*halo_trend_data.n_snaps);
         for (int i_snap=0;i_snap<halo_trend_data.n_snaps;i_snap++){
            double a_i;
            grab_next_line_data(fp_snaps,&line,&line_length);
            grab_double(line,1,&a_i);
            halo_trend_data.z_list[i_snap]=z_of_a(a_i);
         }
         SID_free(SID_FARG line);
         fclose(fp_snaps);

         // Initialize halo data structure
         halo_info        halo_data;
         halo_data.flag_use_profiles  = flag_use_profiles;
         halo_data.flag_use_SO        = flag_use_SO;
         halo_data.snapshot           = snap_number;
         halo_data.properties_group   =(halo_properties_info *)SID_malloc(sizeof(halo_properties_info));
         halo_data.properties_subgroup=(halo_properties_info *)SID_malloc(sizeof(halo_properties_info));
         halo_data.profiles_group     =(halo_profile_info    *)SID_malloc(sizeof(halo_profile_info));
         halo_data.profiles_subgroup  =(halo_profile_info    *)SID_malloc(sizeof(halo_profile_info));

         // Initialize trends
         trend_info *trend_M_FoF=NULL;
         init_trend(&trend_M_FoF,"SSFctn",&halo_trend_data,init_halo_trend_property_logM_FoF,free_halo_trend_property_logM_FoF,calc_halo_trend_property_index_logM_FoF);
         init_halo_trend_coordinate(&halo_trend_data,trend_M_FoF,"SSFctn");

         // Read halos and construct histograms
         for(int i_group=0,i_subgroup=0;i_group<fp_catalog_groups.n_halos_total;i_group++){
            int n_subgroups_group;
            // Read group catalog
            fread_catalog_file(&fp_catalog_groups,NULL,NULL,halo_data.properties_group,halo_data.profiles_group,i_group);
            // Read number of subgroups
            fread_verify(&n_subgroups_group,sizeof(int),1,fp_halos);
            // Read SO masses (if available)
            if(flag_use_SO)
               fread_multifile(&fp_SO,halo_data.SO_data_group,i_group);
            // Loop over subgroups
            halo_data.n_sub         =n_subgroups_group;
            halo_data.np_sub        =0;
            halo_data.np_sub_largest=0;
            for(int j_subgroup=0;j_subgroup<n_subgroups_group;i_subgroup++,j_subgroup++){
               // Read subgroup properties
               fread_catalog_file(&fp_catalog_subgroups,NULL,NULL,halo_data.properties_subgroup,halo_data.profiles_subgroup,i_subgroup);
               int np_i=halo_data.properties_subgroup->n_particles;
               halo_data.np_sub+=np_i;
               if(np_i>halo_data.np_sub_largest)
                  halo_data.np_sub_largest=np_i;
               // Add halo to subgroup trends
            }
            // Add halo to group trends
            add_item_to_trend(trend_M_FoF,GBP_ADD_ITEM_TO_TREND_DEFAULT,&halo_data);
         }

         // Write results
         char filename_out[MAX_FILENAME_LENGTH];
         sprintf(filename_out,"%s_%03d",filename_out_root,snap_number);
         write_trend_ascii(trend_M_FoF,filename_out);
         free_trend(&trend_M_FoF);

         // Clean-up
         SID_free(SID_FARG halo_trend_data.z_list);
         SID_free(SID_FARG halo_data.properties_group);
         SID_free(SID_FARG halo_data.properties_subgroup);
         SID_free(SID_FARG halo_data.profiles_group);
         SID_free(SID_FARG halo_data.profiles_subgroup);
         fclose(fp_halos);
         fclose_catalog(&fp_catalog_groups);
         fclose_catalog(&fp_catalog_subgroups);
         fclose_multifile(&fp_SO);
         SID_log("Done.",SID_LOG_CLOSE);
     }
     SID_log("Done.",SID_LOG_CLOSE);
  }  

  SID_exit(ERROR_NONE);
}
Exemplo n.º 17
0
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);
}