Beispiel #1
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);
}
Beispiel #2
0
int main(int argc, char *argv[]) {
    plist_info            plist;
    char                  filename_root[256];
    char                  filename_log[256];
    char                  filename_number[256];
    char                  filename_in_halos[256];
    char                  filename_out_groups[256];
    char                  filename_out_groups_A[256];
    char                  filename_out_groups_B[256];
    char                  filename_out_groups_C[256];
    char                  filename_out_subgroups[256];
    char                  filename_out_subgroups_A[256];
    char                  filename_out_subgroups_B[256];
    char                  filename_out_hierarchy[256];
    char                  filename_out_hierarchy_A[256];
    char                  filename_out_hierarchy_B[256];
    char                  filename_out_particles[256];
    char                  i_match_txt[5];
    int                   n_groups_AHF;
    int                   n_groups;
    int                   n_subgroups;
    int                   n_subgroups_matched;
    int                   n_subgroups_group;
    size_t                n_particles;
    size_t                n_particles_in_groups;
    size_t                n_particles_in_subgroups;
    size_t                n_particles_AHF_not_used;
    int                   n_particles_temp;
    int *                 n_p_1 = NULL;
    int                   flag_continue;
    int                   flag_long_ids;
    int                   i_match;
    int                   match_id_next;
    int *                 match_id         = NULL;
    int *                 match_id_initial = NULL;
    FILE *                fp               = NULL;
    FILE *                fp_in_halos      = NULL;
    FILE *                fp_out           = NULL;
    int                   n_match;
    int *                 id_2                   = NULL;
    size_t *              particle_ids_AHF       = NULL;
    size_t *              particle_ids_AHF_index = NULL;
    size_t                id_largest;
    int                   id_byte_size;
    size_t *              group_particles = NULL;
    int                   group_id;
    int                   subgroup_id;
    int                   i_group;
    int                   j_group;
    int                   k_group;
    size_t                n_particles_AHF;
    int *                 subgroup_size   = NULL;
    int *                 hierarchy_level = NULL;
    int *                 hierarchy_match = NULL;
    int                   subgroup_size_max;
    int *                 subgroup_size_list       = NULL;
    int *                 subgroup_index_list      = NULL;
    size_t *              subgroup_size_list_index = NULL;
    int *                 group_offsets            = NULL;
    size_t                group_index;
    int *                 group_size        = NULL;
    int *                 group_size_AHF    = NULL;
    int *                 group_offsets_AHF = NULL;
    int                   max_subgroup_size;
    int                   i_subgroup;
    int                   j_subgroup;
    int                   n_subgroups_group_max;
    size_t *              group_size_index = NULL;
    size_t *              match_id_index   = NULL;
    size_t                subgroup_index;
    int                   group_offset;
    int                   subgroup_offset;
    int                   group_count;
    size_t *              group_particles_index = NULL;
    size_t *              subgroup_particles    = NULL;
    int *                 particle_group        = NULL;
    size_t *              particle_group_index  = NULL;
    size_t                i_particle;
    size_t                j_particle;
    size_t                k_particle;
    int                   i_file;
    int                   i_file_start;
    int                   i_file_stop;
    size_t *              match_index = NULL;
    int                   flag_match_subgroups;
    FILE *                fp_log             = NULL;
    FILE *                fp_in              = NULL;
    FILE *                fp_out_particles   = NULL;
    FILE *                fp_out_groups      = NULL;
    FILE *                fp_out_groups_A    = NULL;
    FILE *                fp_out_groups_B    = NULL;
    FILE *                fp_out_groups_C    = NULL;
    FILE *                fp_out_subgroups_A = NULL;
    FILE *                fp_out_subgroups_B = NULL;
    FILE *                fp_out_hierarchy_A = NULL;
    FILE *                fp_out_hierarchy_B = NULL;
    FILE *                fp_test            = NULL;
    int                   substructure_level;
    int                   substructure_level_max;
    halo_properties_info *properties      = NULL;
    void *                particle_buffer = NULL;
    int                   flag_found;

    SID_Init(&argc, &argv, NULL);

    strcpy(filename_root, argv[1]);
    i_file_start = atoi(argv[2]);
    i_file_stop  = atoi(argv[3]);

    SID_log("Converting files #%d->#%d from AHF to subfind format...", SID_LOG_OPEN | SID_LOG_TIMER, i_file_start, i_file_stop);

    sprintf(filename_log, "%s_%dto%d.convert_AHF_log", filename_root, i_file_start, i_file_stop);

    // Loop over all files
    for(i_file = i_file_start; i_file <= i_file_stop; i_file++) {
        SID_log("Processing file #%d...", SID_LOG_OPEN | SID_LOG_TIMER, i_file);

        // Read catalogs
        if(i_file < 10)
            sprintf(filename_number, "00%1d", i_file);
        else if(i_file < 100)
            sprintf(filename_number, "0%2d", i_file);
        else
            sprintf(filename_number, "%3d", i_file);

        // Read AHF group file
        init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);
        read_groups_AHF(filename_root, i_file, READ_GROUPS_ALL, &plist, filename_number);
        n_groups_AHF = ((int *)ADaPS_fetch(plist.data, "n_groups_%s", filename_number))[0];

        n_groups                 = 0;
        n_subgroups              = 0;
        n_subgroups_matched      = 0;
        n_subgroups_group        = 0;
        n_particles              = 0;
        n_particles_in_groups    = 0;
        n_particles_in_subgroups = 0;
        n_particles_AHF_not_used = 0;
        n_subgroups_group_max    = 0;

        if(n_groups_AHF > 0) {
            n_particles_AHF   = (size_t)((size_t *)ADaPS_fetch(plist.data, "n_particles_%s", filename_number))[0];
            group_size_AHF    = (int *)ADaPS_fetch(plist.data, "n_particles_group_%s", filename_number);
            group_offsets_AHF = (int *)ADaPS_fetch(plist.data, "particle_offset_group_%s", filename_number);
            particle_ids_AHF  = (size_t *)ADaPS_fetch(plist.data, "particle_ids_%s", filename_number);

            // Find largest id so we know what size to write the ids with
            for(i_particle = 0, id_largest = 0; i_particle < n_particles_AHF; i_particle++)
                id_largest = GBP_MAX(id_largest, particle_ids_AHF[i_particle]);
            if(id_largest > INT_MAX) {
                flag_long_ids = GBP_TRUE;
                id_byte_size  = sizeof(size_t);
            } else {
                flag_long_ids = GBP_FALSE;
                id_byte_size  = sizeof(int);
            }

            // Match AHF groups against themselves to find substructure
            match_halos(&plist, NULL, i_file, NULL, 0, &plist, NULL, i_file, NULL, 0, "substructure", MATCH_SUBSTRUCTURE, MATCH_SCORE_RANK_INDEX);
            match_id_initial = (int *)ADaPS_fetch(plist.data, "match_substructure");
            hierarchy_match  = match_id_initial; // Fore readability

            // Assign sub-...-sub-structures to parent (ie. top-level) halos
            SID_log("Assigning substructures to groups...", SID_LOG_OPEN);
            group_size      = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            subgroup_size   = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            hierarchy_level = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            particle_group  = (int *)SID_malloc(sizeof(int) * n_particles_AHF);
            for(i_group = 0, i_particle = 0; i_group < n_groups_AHF; i_group++) {
                group_size[i_group]    = 0;
                subgroup_size[i_group] = 0;
                for(j_particle = 0; j_particle < group_size_AHF[i_group]; i_particle++, j_particle++)
                    particle_group[i_particle] = i_group;
            }
            match_id = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            for(i_group = 0, substructure_level_max = 0; i_group < n_groups_AHF; i_group++) {
                substructure_level = 0;
                match_id_next      = match_id_initial[i_group];
                match_id[i_group]  = match_id_next;
                while(match_id_next >= 0) {
                    substructure_level++;
                    match_id[i_group] = match_id_next; // Tie subgroups to their top-level group
                    match_id_next     = match_id_initial[match_id_next];
                }
                if(match_id[i_group] < 0)
                    match_id[i_group] = i_group; // Unmatched halos should be matched to themselves
                hierarchy_level[i_group] = substructure_level;
                substructure_level_max   = GBP_MAX(substructure_level, substructure_level_max);
            }
            // needed? ADaPS_store(&(plist.data),(void *)(match_id),"match_substructure",ADaPS_DEFAULT);
            SID_log("Done.", SID_LOG_CLOSE);

            // Make sure the deepest substructures are given particle ownership
            SID_log("Assigning particles to subgroups...", SID_LOG_OPEN);
            merge_sort(
                (void *)particle_ids_AHF, (size_t)n_particles_AHF, &particle_ids_AHF_index, SID_SIZE_T, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
            for(i_particle = 0, n_particles_AHF_not_used = 0; i_particle < n_particles_AHF; i_particle += k_particle) {
                // Count the number of times this particle id is used
                j_particle = i_particle;
                while(particle_ids_AHF[particle_ids_AHF_index[j_particle]] == particle_ids_AHF[particle_ids_AHF_index[i_particle]] &&
                      j_particle < (n_particles_AHF - 2))
                    j_particle++;
                if(particle_ids_AHF[particle_ids_AHF_index[j_particle]] == particle_ids_AHF[particle_ids_AHF_index[i_particle]])
                    j_particle++;
                k_particle = j_particle - i_particle;
                // Find the deepest substructure using this particle id...
                i_group = particle_group[particle_ids_AHF_index[i_particle]];
                for(j_particle = 1; j_particle < k_particle; j_particle++) {
                    j_group = particle_group[particle_ids_AHF_index[i_particle + j_particle]];
                    if(group_size_AHF[j_group] < group_size_AHF[i_group])
                        i_group = j_group;
                }
                // ... and set particle's group to a dummy value if this particle instance is not from the deepest group
                for(j_particle = 0, flag_found = GBP_FALSE; j_particle < k_particle; j_particle++) {
                    if(particle_group[particle_ids_AHF_index[i_particle + j_particle]] != i_group || flag_found) {
                        particle_group[particle_ids_AHF_index[i_particle + j_particle]] = -1;
                        n_particles_AHF_not_used++;
                    } else
                        flag_found = GBP_TRUE;
                }
            }
            SID_free((void **)&particle_ids_AHF_index);
            SID_log("Done.", SID_LOG_CLOSE);

            // Generate subgroup_size array
            for(i_group = 0; i_group < n_groups_AHF; i_group++)
                subgroup_size[i_group] = 0;
            for(i_particle = 0; i_particle < n_particles_AHF; i_particle++) {
                i_group = particle_group[i_particle];
                if(i_group >= 0)
                    subgroup_size[i_group]++;
            }

            // Get rid of groups that are too small
            for(i_particle = 0; i_particle < n_particles_AHF; i_particle++) {
                i_group = particle_group[i_particle];
                if(i_group >= 0) {
                    if(subgroup_size[i_group] < 20) {
                        n_particles_AHF_not_used++;
                        particle_group[i_particle] = -1;
                    }
                }
            }

            // Regenerate subgroup_size array
            for(i_group = 0; i_group < n_groups_AHF; i_group++)
                subgroup_size[i_group] = 0;
            for(i_particle = 0; i_particle < n_particles_AHF; i_particle++) {
                i_group = particle_group[i_particle];
                if(i_group >= 0)
                    subgroup_size[i_group]++;
            }

            // Find the largest subgroup's size
            for(i_group = 0, n_subgroups = 0, subgroup_size_max = 0; i_group < n_groups_AHF; i_group++)
                subgroup_size_max = GBP_MAX(subgroup_size[i_group], subgroup_size_max);

            // Generate group_size array
            for(i_group = 0; i_group < n_groups_AHF; i_group++)
                group_size[match_id[i_group]] += subgroup_size[i_group]; // update group size

            // Sort groups in order of size
            merge_sort((void *)group_size, (size_t)n_groups_AHF, &group_size_index, SID_INT, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
            merge_sort((void *)match_id, (size_t)n_groups_AHF, &match_id_index, SID_INT, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);

            // Count groups, subgroups, etc.
            SID_log("Counting groups & subgroups...", SID_LOG_OPEN);
            for(i_group = 0, n_groups = 0, n_subgroups = 0; i_group < n_groups_AHF; i_group++) {
                group_index = group_size_index[n_groups_AHF - i_group - 1];

                // Find start of subgroup list for this group
                j_group = find_index_int(match_id, group_index, n_groups_AHF, match_id_index);
                while(group_index > match_id[match_id_index[j_group]] && j_group < (n_groups_AHF - 2))
                    j_group++;
                if(group_index > match_id[match_id_index[j_group]])
                    j_group++;

                // Count subgroups
                n_subgroups_group = 0;
                while(match_id[match_id_index[j_group]] == group_index && j_group < (n_groups_AHF - 2)) {
                    if(subgroup_size[match_id_index[j_group]] > 0)
                        n_subgroups_group++;
                    j_group++;
                }
                if(match_id[match_id_index[j_group]] == group_index) {
                    if(subgroup_size[match_id_index[j_group]] > 0)
                        n_subgroups_group++;
                    j_group++;
                }
                n_subgroups += n_subgroups_group;

                // Largest number of subgroups
                n_subgroups_group_max = GBP_MAX(n_subgroups_group_max, n_subgroups_group);

                // Count groups
                if(n_subgroups_group > 0)
                    n_groups++;
            }
            SID_log("Done.", SID_LOG_CLOSE);
        }

        // Find largest subgroup and count the number of particles in groups
        for(i_group = 0, max_subgroup_size = 0, n_particles_in_groups = 0; i_group < n_groups_AHF; i_group++) {
            max_subgroup_size = GBP_MAX(max_subgroup_size, subgroup_size[i_group]);
            if(subgroup_size[i_group] > 0)
                n_particles_in_groups += (size_t)subgroup_size[i_group];
        }

        // Write some statistics
        SID_log("Substructure statistics:", SID_LOG_OPEN);
        SID_log("Number of groups                 =%d", SID_LOG_COMMENT, n_groups);
        SID_log("Number of subgroups              =%d", SID_LOG_COMMENT, n_subgroups);
        SID_log("Max number of subgroups per group=%d", SID_LOG_COMMENT, n_subgroups_group_max);
        SID_log("Largest subgroup                 =%d particles", SID_LOG_COMMENT, subgroup_size_max);
        SID_log("Depth of substructure heirarchy  =%d levels", SID_LOG_COMMENT, substructure_level_max);
        SID_log("Number of AHF particles used     =%lld", SID_LOG_COMMENT, n_particles_in_groups);
        SID_log("Number of AHF particles NOT used =%lld", SID_LOG_COMMENT, n_particles_AHF_not_used);
        SID_log("", SID_LOG_CLOSE | SID_LOG_NOPRINT);

        // Open files
        SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE, 0);
        SID_log("Writing %d groups, %d subgroups and %lld particles to files...",
                SID_LOG_OPEN | SID_LOG_TIMER,
                n_groups,
                n_subgroups,
                n_particles_in_groups);
        sprintf(filename_out_groups, "%s_%s.catalog_groups", filename_root, filename_number);
        sprintf(filename_out_groups_A, "%s_%s.catalog_groups_A", filename_root, filename_number);
        sprintf(filename_out_groups_B, "%s_%s.catalog_groups_B", filename_root, filename_number);
        sprintf(filename_out_groups_C, "%s_%s.catalog_groups_C", filename_root, filename_number);
        sprintf(filename_out_subgroups, "%s_%s.catalog_subgroups", filename_root, filename_number);
        sprintf(filename_out_subgroups_A, "%s_%s.catalog_subgroups_A", filename_root, filename_number);
        sprintf(filename_out_subgroups_B, "%s_%s.catalog_subgroups_B", filename_root, filename_number);
        sprintf(filename_out_hierarchy, "%s_%s.catalog_hierarchy", filename_root, filename_number);
        sprintf(filename_out_hierarchy_A, "%s_%s.catalog_hierarchy_A", filename_root, filename_number);
        sprintf(filename_out_hierarchy_B, "%s_%s.catalog_hierarchy_B", filename_root, filename_number);
        sprintf(filename_out_particles, "%s_%s.catalog_particles", filename_root, filename_number);
        fp_out_groups_A    = fopen(filename_out_groups_A, "w");
        fp_out_groups_B    = fopen(filename_out_groups_B, "w");
        fp_out_groups_C    = fopen(filename_out_groups_C, "w");
        fp_out_subgroups_A = fopen(filename_out_subgroups_A, "w");
        fp_out_subgroups_B = fopen(filename_out_subgroups_B, "w");
        fp_out_hierarchy_A = fopen(filename_out_hierarchy_A, "w");
        fp_out_hierarchy_B = fopen(filename_out_hierarchy_B, "w");
        fp_out_particles   = fopen(filename_out_particles, "w");

        // Write headers
        fwrite(&n_groups, sizeof(int), 1, fp_out_groups_A);
        fwrite(&n_subgroups, sizeof(int), 1, fp_out_subgroups_A);
        fwrite(&n_subgroups, sizeof(int), 1, fp_out_hierarchy_A);
        fwrite(&id_byte_size, sizeof(int), 1, fp_out_particles);
        switch(flag_long_ids) {
            case GBP_TRUE:
                fwrite(&n_particles_in_groups, sizeof(size_t), 1, fp_out_particles);
                break;
            default:
                n_particles_temp = (int)n_particles_in_groups;
                fwrite(&n_particles_temp, sizeof(int), 1, fp_out_particles);
                break;
        }

        // Write files; group and subgroup files in parts (to be concatinated together later)
        subgroup_size_list  = (int *)SID_malloc(sizeof(int) * n_subgroups_group_max);
        subgroup_index_list = (int *)SID_malloc(sizeof(int) * n_subgroups_group_max);
        particle_buffer     = (void *)SID_malloc(id_byte_size * subgroup_size_max);
        subgroup_offset     = 0;
        group_offset        = 0;

        for(i_group = n_groups_AHF - 1; i_group >= n_groups_AHF - n_groups; i_group--) {
            group_index = group_size_index[i_group];

            // Find start of subgroup list for this group
            i_subgroup = find_index_int(match_id, group_index, n_groups_AHF, match_id_index);
            while(group_index > match_id[match_id_index[i_subgroup]] && i_subgroup < (n_groups_AHF - 2))
                i_subgroup++;
            if(group_index > match_id[match_id_index[i_subgroup]])
                i_subgroup++;

            // Create a list of subgroups for this group and sort it by size
            n_subgroups_group = 0;
            subgroup_index    = match_id_index[i_subgroup];
            while(match_id[subgroup_index] == group_index && i_subgroup < (n_groups_AHF - 2)) {
                if(subgroup_size[subgroup_index] > 0) {
                    subgroup_size_list[n_subgroups_group]  = subgroup_size[subgroup_index];
                    subgroup_index_list[n_subgroups_group] = (int)subgroup_index;
                    n_subgroups_group++;
                }
                i_subgroup++;
                subgroup_index = match_id_index[i_subgroup];
            }
            if(match_id[subgroup_index] == group_index) {
                if(subgroup_size[subgroup_index] > 0) {
                    subgroup_size_list[n_subgroups_group]  = subgroup_size[subgroup_index];
                    subgroup_index_list[n_subgroups_group] = (int)subgroup_index;
                    n_subgroups_group++;
                }
                i_subgroup++;
                subgroup_index = match_id_index[i_subgroup];
            }
            merge_sort((void *)subgroup_size_list,
                       (size_t)n_subgroups_group,
                       &subgroup_size_list_index,
                       SID_INT,
                       SORT_COMPUTE_INDEX,
                       SORT_COMPUTE_NOT_INPLACE);

            // Perform writes for subgroups and particle lists
            for(i_subgroup = 0, i_particle = 0; i_subgroup < n_subgroups_group; i_subgroup++) {
                j_subgroup = subgroup_index_list[subgroup_size_list_index[n_subgroups_group - i_subgroup - 1]];
                // ... subgroups ...
                fwrite(&(subgroup_size[j_subgroup]), sizeof(int), 1, fp_out_subgroups_A);
                fwrite(&(subgroup_offset), sizeof(int), 1, fp_out_subgroups_B);
                fwrite(&(hierarchy_match[j_subgroup]), sizeof(int), 1, fp_out_hierarchy_A);
                fwrite(&(hierarchy_level[j_subgroup]), sizeof(int), 1, fp_out_hierarchy_B);
                subgroup_offset += subgroup_size[j_subgroup];
                // ... and particles
                for(j_particle = group_offsets_AHF[j_subgroup], k_particle = 0, i_particle = 0; k_particle < group_size_AHF[j_subgroup];
                    j_particle++, k_particle++) {
                    if(particle_group[j_particle] == j_subgroup) {
                        switch(flag_long_ids) {
                            case GBP_TRUE:
                                ((size_t *)particle_buffer)[i_particle++] = (size_t)(particle_ids_AHF[j_particle]);
                                break;
                            default:
                                ((int *)particle_buffer)[i_particle++] = (int)(particle_ids_AHF[j_particle]);
                                break;
                        }
                    }
                }
                if(i_particle == subgroup_size[j_subgroup])
                    fwrite(particle_buffer, id_byte_size, i_particle, fp_out_particles);
                else
                    SID_exit_error("Subgroup size mismatch!", SID_ERROR_LOGIC);
            }

            SID_free((void **)&subgroup_size_list_index);

            // Perform writes for groups
            fwrite(&(group_size[group_index]), sizeof(int), 1, fp_out_groups_A);
            fwrite(&group_offset, sizeof(int), 1, fp_out_groups_B);
            fwrite(&n_subgroups_group, sizeof(int), 1, fp_out_groups_C);
            group_offset += group_size[group_index];
        }
        SID_free((void **)&subgroup_size_list);
        SID_free((void **)&subgroup_index_list);
        SID_free((void **)&particle_buffer);

        fclose(fp_out_groups_A);
        fclose(fp_out_groups_B);
        fclose(fp_out_groups_C);
        fclose(fp_out_subgroups_A);
        fclose(fp_out_subgroups_B);
        fclose(fp_out_hierarchy_A);
        fclose(fp_out_hierarchy_B);
        fclose(fp_out_particles);

        // Concatinate group and subgroup temp files into final files
        SID_cat_files(filename_out_groups, SID_CAT_CLEAN, 3, filename_out_groups_A, filename_out_groups_B, filename_out_groups_C);

        SID_cat_files(filename_out_subgroups, SID_CAT_CLEAN, 2, filename_out_subgroups_A, filename_out_subgroups_B);

        SID_cat_files(filename_out_hierarchy, SID_CAT_CLEAN, 2, filename_out_hierarchy_A, filename_out_hierarchy_B);

        // Clean-up
        SID_free((void **)&subgroup_size);
        SID_free((void **)&hierarchy_level);
        SID_free((void **)&group_size);
        SID_free((void **)&group_size_index);
        SID_free((void **)&match_id_index);
        free_plist(&plist);
        SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);
        SID_log("Done.", SID_LOG_CLOSE);

        // Write log file
        SID_log("Writing to log file...", SID_LOG_OPEN);
        // Write a header for the log file
        if(i_file == i_file_start) {
            fp_log = fopen(filename_log, "w");
            fprintf(fp_log, "# (1):  filenumber\n");
            fprintf(fp_log, "# (2):  n_groups_AHF\n");
            fprintf(fp_log, "# (3):  n_particles_AHF\n");
            fprintf(fp_log, "# (4):  n_groups\n");
            fprintf(fp_log, "# (5):  n_subgroups\n");
            fprintf(fp_log, "# (6):  max number of subgroups per group\n");
            fprintf(fp_log, "# (7):  largest subgroup\n");
            fprintf(fp_log, "# (8):  depth of substructure heirarchy\n");
            fprintf(fp_log, "# (9):  number of AHF particles used\n");
            fprintf(fp_log, "# (10): number of AHF particles NOT used\n");
        } else
            fp_log = fopen(filename_log, "a");
        fprintf(fp_log,
                "%4d %9d %12zd %9d %9d %9d %9d %9d %12zd %12zd\n",
                i_file,
                n_groups_AHF,
                n_particles_AHF,
                n_groups,
                n_subgroups,
                n_subgroups_group_max,
                subgroup_size_max,
                substructure_level_max,
                n_particles_in_groups,
                n_particles_AHF_not_used);
        fclose(fp_log);

        SID_log("Done.", SID_LOG_CLOSE);

        SID_log("Done.", SID_LOG_CLOSE);
    }

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
void init_gbpCosmo2gbpCosmo(cosmo_info      **cosmo_source,
                            cosmo_info      **cosmo_target,
                            double            z_min,
                            double            M_min,
                            double            M_max,
                            gbpCosmo2gbpCosmo_info *gbpCosmo2gbpCosmo){
   SID_log("Initializing cosmology scaling...",SID_LOG_OPEN|SID_LOG_TIMER);
   SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE,-1);

   // Store some infor in the gbpCosmo2gbpCosmo_info structure
   gbpCosmo2gbpCosmo->M_min       =M_min;
   gbpCosmo2gbpCosmo->M_max       =M_max;
   gbpCosmo2gbpCosmo->z_min       =z_min;
   gbpCosmo2gbpCosmo->cosmo_source=(*cosmo_source);
   gbpCosmo2gbpCosmo->cosmo_target=(*cosmo_target);

   // Perform minimization
   //const gsl_multimin_fminimizer_type *T=gsl_multimin_fminimizer_nmsimplex2;
   const gsl_multimin_fminimizer_type *T=gsl_multimin_fminimizer_nmsimplex;
   gsl_multimin_fminimizer            *s = NULL;
   gsl_vector *ss, *x;
   gsl_multimin_function minex_func;
 
   // Starting point 
   x = gsl_vector_alloc (2);
   gsl_vector_set (x, 0, 1.);    // inv_s
   gsl_vector_set (x, 1, z_min); // z_scaled
 
   // Set initial step sizes to 1 
   ss = gsl_vector_alloc (2);
   gsl_vector_set_all (ss, 1.0);

   // Set parameters
   init_gbpCosmo2gbpCosmo_integrand_params params;
   params.cosmo_source=cosmo_source;
   params.cosmo_target=cosmo_target;
   params.z_source    =z_min;
   params.R_1         =R_of_M(M_min,*cosmo_source);
   params.R_2         =R_of_M(M_max,*cosmo_source);
   params.inv_s       =gsl_vector_get(x,0);
   params.z_target    =gsl_vector_get(x,1);
   params.n_int       =100;
   params.wspace      =gsl_integration_workspace_alloc(params.n_int);

   // Initialize method
   minex_func.n      = 2;
   minex_func.f      = init_gbpCosmo2gbpCosmo_minimize_function;
   minex_func.params = (void *)(&params);
   s                 = gsl_multimin_fminimizer_alloc (T, 2);
   gsl_multimin_fminimizer_set(s,&minex_func,x,ss);

   // Perform minimization 
   double size;
   int    status;
   size_t iter    =  0;
   size_t iter_max=200;
   do{
       iter++;
       status=gsl_multimin_fminimizer_iterate(s);
       if(status) 
          SID_trap_error("Error encountered during minimisation in init_gbpCosmo2gbpCosmo() (status=%d).",ERROR_LOGIC,status);
       size   = gsl_multimin_fminimizer_size(s);
       status = gsl_multimin_test_size(size,1e-2);
   } while(status==GSL_CONTINUE && iter<=iter_max);
   if(status!=GSL_SUCCESS)
      SID_trap_error("Failed to converge during minimisation in init_gbpCosmo2gbpCosmo() (status=%d,iter=%d).",ERROR_LOGIC,status,iter);

   // Finalize results   
   double Omega_M_source =    ((double *)ADaPS_fetch(*cosmo_source,"Omega_M") )[0];
   double H_Hubble_source=1e2*((double *)ADaPS_fetch(*cosmo_source,"h_Hubble"))[0];
   double Omega_M_target =    ((double *)ADaPS_fetch(*cosmo_target,"Omega_M") )[0];
   double H_Hubble_target=1e2*((double *)ADaPS_fetch(*cosmo_target,"h_Hubble"))[0];
   gbpCosmo2gbpCosmo->s_L         =1./gsl_vector_get(s->x,0);
   gbpCosmo2gbpCosmo->s_M         =(Omega_M_target*H_Hubble_target)/(Omega_M_source*H_Hubble_source)*pow((gbpCosmo2gbpCosmo->s_L),3.);
   gbpCosmo2gbpCosmo->z_min_scaled=gsl_vector_get(s->x,1);;

   // Calculate growth factors needed for
   //    determining redshift mappings
   gbpCosmo2gbpCosmo->D_prime_z_min=linear_growth_factor(z_min,                    *cosmo_target);
   gbpCosmo2gbpCosmo->D_z_scaled   =linear_growth_factor(gbpCosmo2gbpCosmo->z_min_scaled,*cosmo_source);
   gbpCosmo2gbpCosmo->D_ratio      =gbpCosmo2gbpCosmo->D_prime_z_min/gbpCosmo2gbpCosmo->D_z_scaled;

   // Clean-up
   gsl_vector_free(x);
   gsl_vector_free(ss);
   gsl_multimin_fminimizer_free(s);
   gsl_integration_workspace_free(params.wspace);
   SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);
   SID_log("Done.",SID_LOG_CLOSE);
}
Beispiel #4
0
int main(int argc, char *argv[]) {
    int     n_search;
    int     n_files;
    int     k_read;
    int     max_n_groups;
    int     l_read;
    int *   n_particles_i;
    int *   n_particles_j;
    int     n_groups_i;
    int     n_groups_j;
    int *   match_ids;
    float * match_score;
    size_t *match_index;
    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);

    // Fetch user inputs
    if(argc != 12)
        SID_exit_error("Invalid Syntax.", SID_ERROR_SYNTAX);
    char filename_SSimPL_root[SID_MAX_FILENAME_LENGTH];
    char filename_halos_root[SID_MAX_FILENAME_LENGTH];
    char filename_trees_root[SID_MAX_FILENAME_LENGTH];
    char filename_out_root[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_SSimPL_root, argv[1]);
    strcpy(filename_halos_root, argv[2]);
    strcpy(filename_trees_root, argv[3]);
    double x_cen    = (double)atof(argv[4]);
    double y_cen    = (double)atof(argv[5]);
    double z_cen    = (double)atof(argv[6]);
    double radius   = (double)atof(argv[7]);
    double z_min_in = (double)atof(argv[8]);
    double z_max_in = (double)atof(argv[9]);
    double M_min    = (double)atof(argv[10]);
    strcpy(filename_out_root, argv[11]);
    double radius2 = radius * radius;

    SID_log("Query trees for sphere (x,y,z,r)=(%.2lf,%.2lf,%.2lf,%.2lf) between z=%.2lf and z=%.2lf...",
            SID_LOG_OPEN,
            x_cen,
            y_cen,
            z_cen,
            radius,
            z_min_in,
            z_max_in);

    char filename_catalog_root[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_catalog_root, "%s/catalogs/%s", filename_SSimPL_root, filename_halos_root);

    // Read tree header information
    tree_info *trees;
    char       filename_file_root[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_file_root, "%s/trees/%s", filename_SSimPL_root, filename_trees_root);
    SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE, 0);
    init_trees_read(filename_SSimPL_root, filename_halos_root, filename_trees_root, TREE_READ_HEADER_ONLY, &trees);
    SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);

    // Turn given redshift range into snapshot range
    int i_snap_min_z = find_treesnap_z(trees, z_max_in);
    int i_snap_max_z = find_treesnap_z(trees, z_min_in);
    SID_log("z=%.2lf -> snapshot=%d", SID_LOG_COMMENT, z_min_in, trees->snap_list[i_snap_max_z]);
    SID_log("z=%.2lf -> snapshot=%d", SID_LOG_COMMENT, z_max_in, trees->snap_list[i_snap_min_z]);

    // Perform query
    int  n_groups_list    = 0;
    int  n_subgroups_list = 0;
    int *group_list       = NULL;
    int *subgroup_list    = NULL;
    for(int i_pass = 0; i_pass < 3; i_pass++) {
        if(i_pass == 0)
            SID_log("Counting halos to be queried...", SID_LOG_OPEN | SID_LOG_TIMER);
        else if(i_pass == 1)
            SID_log("Identifying halos to be queried...", SID_LOG_OPEN | SID_LOG_TIMER);
        else if(i_pass == 2)
            SID_log("Performing query...", SID_LOG_OPEN | SID_LOG_TIMER);
        // Write headers
        if(i_pass == 2) {
            for(int i_type = 0; i_type < 2; i_type++) {
                int  n_list    = 0;
                int *halo_list = NULL;
                if(i_type == 0) {
                    n_list    = n_groups_list;
                    halo_list = group_list;
                } else {
                    n_list    = n_subgroups_list;
                    halo_list = subgroup_list;
                }
                for(int i_list = 0; i_list < n_list; i_list++) {
                    int  i_column = 1;
                    char filename_out[SID_MAX_FILENAME_LENGTH];
                    if(i_type == 0)
                        sprintf(filename_out, "%s_group_%09d.txt", filename_out_root, halo_list[i_list]);
                    else
                        sprintf(filename_out, "%s_subgroup_%09d.txt", filename_out_root, halo_list[i_list]);
                    FILE *fp_out = fopen(filename_out, "w");
                    fprintf(fp_out, "# Column (%02d): Halo expansion factor\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo redshift\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo ID\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo log10(M_vir [M_sol/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo n_particles\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo n_particles_peak\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo x [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo y [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo z [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo radius [Mpc/h])\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo tree ID\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant file offset\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Descendant ID\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge forematch snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge forematch index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge backmatch snapshot\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Bridge backmatch index\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo type\n", i_column++);
                    fprintf(fp_out, "#        (%02d): Halo type string\n", i_column++);
                    if(i_type == 1) {
                        fprintf(fp_out, "#        (%02d): Group index\n", i_column++);
                        fprintf(fp_out, "#        (%02d): Group ID\n", i_column++);
                        fprintf(fp_out, "#        (%02d): Subgroup index\n", i_column++);
                        fprintf(fp_out, "#        (%02d): FoF Centre dx [Mpc/h])\n", i_column++);
                        fprintf(fp_out, "#        (%02d): FoF Centre dy [Mpc/h])\n", i_column++);
                        fprintf(fp_out, "#        (%02d): FoF Centre dz [Mpc/h])\n", i_column++);
                    }
                    fclose(fp_out);
                }
            }
        }

        // Set the snapshot range we need to scan
        int i_snap_start;
        int i_snap_stop;
        if(i_pass < 2) {
            i_snap_start = i_snap_min_z;
            i_snap_stop  = i_snap_max_z;
        } else {
            i_snap_start = 0;
            i_snap_stop  = trees->n_snaps - 1;
        }

        // Loop over the range of snapshots
        int i_list = 0;
        for(int i_snap = i_snap_start; i_snap <= i_snap_stop; i_snap++) {
            // Get the snapshot
            int snapshot = trees->snap_list[i_snap];
            if(i_pass == 2)
                SID_log("Processing snapshot %03d...", SID_LOG_OPEN, snapshot);

            // Open properties for this snapshot
            fp_catalog_info      fp_properties_groups;
            fp_catalog_info      fp_properties_subgroups;
            halo_properties_info properties_groups;
            halo_properties_info properties_subgroups;
            fopen_catalog(filename_catalog_root, snapshot, READ_CATALOG_GROUPS | READ_CATALOG_PROPERTIES, &fp_properties_groups);
            fopen_catalog(filename_catalog_root, snapshot, READ_CATALOG_SUBGROUPS | READ_CATALOG_PROPERTIES, &fp_properties_subgroups);

            // Open horizontal trees for this snapshot
            SID_fp fp_in_trees;
            SID_fp fp_in_bridge_forematch;
            SID_fp fp_in_bridge_backmatch;
            char   filename_in[SID_MAX_FILENAME_LENGTH];
            sprintf(filename_in, "%s/trees/%s/horizontal/trees/horizontal_trees_%03d.dat", filename_SSimPL_root, filename_trees_root, snapshot);
            SID_fopen(filename_in, "r", &fp_in_trees);
            sprintf(filename_in,
                    "%s/trees/%s/horizontal/trees/horizontal_trees_forematch_pointers_%03d.dat",
                    filename_SSimPL_root,
                    filename_trees_root,
                    snapshot);
            SID_fopen(filename_in, "r", &fp_in_bridge_forematch);
            sprintf(filename_in,
                    "%s/trees/%s/horizontal/trees/horizontal_trees_backmatch_pointers_%03d.dat",
                    filename_SSimPL_root,
                    filename_trees_root,
                    snapshot);
            SID_fopen(filename_in, "r", &fp_in_bridge_backmatch);

            // Read trees header
            int n_step_in;
            int n_search_in;
            int n_groups;
            int n_subgroups;
            int n_groups_max_in;
            int n_subgroups_max_in;
            int n_trees_subgroup_in;
            int n_trees_group_in;
            SID_fread_all(&n_step_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_search_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_groups, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_subgroups, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_groups_max_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_subgroups_max_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_trees_subgroup_in, sizeof(int), 1, &fp_in_trees);
            SID_fread_all(&n_trees_group_in, sizeof(int), 1, &fp_in_trees);
            SID_fskip(sizeof(int), 8, &fp_in_bridge_forematch);
            SID_fskip(sizeof(int), 8, &fp_in_bridge_backmatch);

            // Scan through the trees
            int i_subgroup = 0;
            for(int i_group = 0; i_group < n_groups; i_group++) {
                // Read group
                int   group_id;
                int   group_type;
                int   group_descendant_id;
                int   group_tree_id;
                int   group_file_offset;
                int   group_index;
                int   group_n_particles_peak;
                int   n_subgroups_group;
                int   group_forematch_id;
                int   group_forematch_first_file;
                int   group_forematch_first_index;
                float group_forematch_first_score;
                int   group_forematch_default_file;
                int   group_forematch_default_index;
                float group_forematch_default_score;
                int   group_forematch_best_file;
                int   group_forematch_best_index;
                float group_forematch_best_score;
                float group_forematch_score_prog;
                int   group_backmatch_id;
                int   group_backmatch_file;
                int   group_backmatch_index;
                float group_backmatch_score;
                float group_backmatch_score_prog;
                SID_fread_all(&group_id, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_type, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_descendant_id, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_tree_id, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_file_offset, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_index, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_n_particles_peak, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&n_subgroups_group, sizeof(int), 1, &fp_in_trees);
                SID_fread_all(&group_forematch_id, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_first_file, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_first_index, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_first_score, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_default_file, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_default_index, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_default_score, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_best_file, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_best_index, sizeof(int), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_best_score, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_forematch_score_prog, sizeof(float), 1, &fp_in_bridge_forematch);
                SID_fread_all(&group_backmatch_id, sizeof(int), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_file, sizeof(int), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_index, sizeof(int), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_score, sizeof(float), 1, &fp_in_bridge_backmatch);
                SID_fread_all(&group_backmatch_score_prog, sizeof(float), 1, &fp_in_bridge_backmatch);
                SID_fskip(sizeof(int), 1, &fp_in_bridge_forematch); // skip subhalo count
                SID_fskip(sizeof(int), 1, &fp_in_bridge_backmatch); // skip subhalo count
                fread_catalog_file(&fp_properties_groups, NULL, NULL, &properties_groups, NULL, i_group);

                // Process group
                process_local(trees,
                              i_pass,
                              filename_out_root,
                              radius2,
                              M_min,
                              i_snap,
                              i_group,
                              0,
                              i_group,
                              group_id,
                              group_file_offset,
                              group_type,
                              group_n_particles_peak,
                              group_tree_id,
                              group_index,
                              group_descendant_id,
                              group_forematch_first_index,
                              group_forematch_first_file,
                              group_backmatch_index,
                              group_backmatch_file,
                              group_id,
                              &properties_groups,
                              NULL,
                              &n_groups_list,
                              group_list,
                              x_cen,
                              y_cen,
                              z_cen);

                for(int j_subgroup = 0; j_subgroup < n_subgroups_group; i_subgroup++, j_subgroup++) {
                    // Read subgroup
                    int   subgroup_id;
                    int   subgroup_type;
                    int   subgroup_descendant_id;
                    int   subgroup_tree_id;
                    int   subgroup_file_offset;
                    int   subgroup_index;
                    int   subgroup_n_particles_peak;
                    int   subgroup_forematch_id;
                    int   subgroup_forematch_first_file;
                    int   subgroup_forematch_first_index;
                    float subgroup_forematch_first_score;
                    int   subgroup_forematch_default_file;
                    int   subgroup_forematch_default_index;
                    float subgroup_forematch_default_score;
                    int   subgroup_forematch_best_file;
                    int   subgroup_forematch_best_index;
                    float subgroup_forematch_best_score;
                    float subgroup_forematch_score_prog;
                    int   subgroup_backmatch_id;
                    int   subgroup_backmatch_file;
                    int   subgroup_backmatch_index;
                    float subgroup_backmatch_score;
                    float subgroup_backmatch_score_prog;
                    SID_fread_all(&subgroup_id, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_type, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_descendant_id, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_tree_id, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_file_offset, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_index, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_n_particles_peak, sizeof(int), 1, &fp_in_trees);
                    SID_fread_all(&subgroup_forematch_id, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_first_file, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_first_index, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_first_score, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_default_file, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_default_index, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_default_score, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_best_file, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_best_index, sizeof(int), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_best_score, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_forematch_score_prog, sizeof(float), 1, &fp_in_bridge_forematch);
                    SID_fread_all(&subgroup_backmatch_id, sizeof(int), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_file, sizeof(int), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_index, sizeof(int), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_score, sizeof(float), 1, &fp_in_bridge_backmatch);
                    SID_fread_all(&subgroup_backmatch_score_prog, sizeof(float), 1, &fp_in_bridge_backmatch);
                    fread_catalog_file(&fp_properties_subgroups, NULL, NULL, &properties_subgroups, NULL, i_subgroup);

                    // Process subgroup
                    process_local(trees,
                                  i_pass,
                                  filename_out_root,
                                  radius2,
                                  M_min,
                                  i_snap,
                                  i_subgroup,
                                  j_subgroup,
                                  i_group,
                                  subgroup_id,
                                  subgroup_file_offset,
                                  subgroup_type,
                                  subgroup_n_particles_peak,
                                  subgroup_tree_id,
                                  subgroup_index,
                                  subgroup_descendant_id,
                                  subgroup_forematch_first_index,
                                  subgroup_forematch_first_file,
                                  subgroup_backmatch_index,
                                  subgroup_backmatch_file,
                                  group_id,
                                  &properties_subgroups,
                                  &properties_groups,
                                  &n_subgroups_list,
                                  subgroup_list,
                                  x_cen,
                                  y_cen,
                                  z_cen);
                }
            }
            fclose_catalog(&fp_properties_groups);
            fclose_catalog(&fp_properties_subgroups);
            SID_fclose(&fp_in_trees);
            SID_fclose(&fp_in_bridge_forematch);
            SID_fclose(&fp_in_bridge_backmatch);
            if(i_pass == 2)
                SID_log("Done.", SID_LOG_CLOSE);
        } // i_snap
        if(i_pass == 0) {
            group_list       = (int *)SID_malloc(sizeof(int) * n_groups_list);
            subgroup_list    = (int *)SID_malloc(sizeof(int) * n_subgroups_list);
            n_groups_list    = 0;
            n_subgroups_list = 0;
        } else if(i_pass == 1) {
            SID_log("(%d groups and %d subgroups found)...", SID_LOG_CONTINUE, n_groups_list, n_subgroups_list);
            // Write list files
            for(int i_type = 0; i_type < 2; i_type++) {
                char filename_out[SID_MAX_FILENAME_LENGTH];
                int  n_list    = 0;
                int *halo_list = NULL;
                if(i_type == 0) {
                    sprintf(filename_out, "%s_group_list.txt", filename_out_root);
                    n_list    = n_groups_list;
                    halo_list = group_list;
                } else {
                    sprintf(filename_out, "%s_subgroup_list.txt", filename_out_root);
                    n_list    = n_subgroups_list;
                    halo_list = subgroup_list;
                }
                FILE *fp_out = fopen(filename_out, "w");
                fprintf(fp_out, "# Halo IDs in list of tree tracks with base {%s}\n", filename_out_root);
                for(int i_list = 0; i_list < n_list; i_list++)
                    fprintf(fp_out, "%d\n", halo_list[i_list]);
                fclose(fp_out);
            }
        }
        SID_log("Done.", SID_LOG_CLOSE);
    }

    // Clean-up
    SID_free(SID_FARG group_list);
    SID_free(SID_FARG subgroup_list);
    free_trees(&trees);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
Beispiel #5
0
int main(int argc, char *argv[]) {
    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    char   filename_halos_root[256];
    char   filename_catalog_root[256];
    char   filename_PHKs_root[256];
    double box_size;
    double dx;
    int    i_file_lo_in;
    int    i_file_hi_in;
    int    i_file_skip;
    strcpy(filename_halos_root, argv[1]);
    strcpy(filename_catalog_root, argv[2]);
    strcpy(filename_PHKs_root, argv[3]);
    box_size     = atof(argv[4]);
    dx           = atof(argv[5]);
    i_file_lo_in = atoi(argv[6]);
    i_file_hi_in = atoi(argv[7]);
    i_file_skip  = atoi(argv[8]);

    int i_file_lo;
    int i_file_hi;
    if(i_file_lo_in < i_file_hi_in) {
        i_file_lo = i_file_lo_in;
        i_file_hi = i_file_hi_in;
    } else {
        i_file_lo = i_file_hi_in;
        i_file_hi = i_file_lo_in;
    }

    SID_log("Generating group PH keys for files #%d->#%d...", SID_LOG_OPEN | SID_LOG_TIMER, i_file_lo, i_file_hi);
    for(int i_file = i_file_lo; i_file <= i_file_hi; i_file += i_file_skip) {
        SID_log("Processing file #%03d...", SID_LOG_OPEN | SID_LOG_TIMER, i_file);
        SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE, 0);

        // Read group info from the halo catalogs
        plist_info plist;
        int *      PHK_group       = NULL;
        size_t *   PHK_group_index = NULL;
        char *     filename_number = (char *)SID_malloc(sizeof(char) * 10);
        init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);
        sprintf(filename_number, "%03d", i_file);
        ADaPS_store(&(plist.data), (void *)filename_number, "read_catalog", ADaPS_DEFAULT);
        read_groups(filename_halos_root, i_file, READ_GROUPS_ALL | READ_GROUPS_MBP_IDS_ONLY, &plist, filename_number);
        int n_groups_all = ((int *)ADaPS_fetch(plist.data, "n_groups_all_%s", filename_number))[0];
        int n_groups     = ((int *)ADaPS_fetch(plist.data, "n_groups_%s", filename_number))[0];

        // If there's any groups to analyze ...
        int *  n_particles_groups     = NULL;
        size_t n_particles_cumulative = 0;
        int    n_bits                 = 0; // Default value if there are no groups
        if(n_groups > 0) {
            // Fetch the halo sizes
            n_particles_groups = (int *)ADaPS_fetch(plist.data, "n_particles_group_%s", filename_number);

            // Read MBP data from halo catalogs
            SID_log("Reading most-bound-particle positions...", SID_LOG_OPEN);
            halo_properties_info group_properties;
            fp_catalog_info      fp_group_properties;
            double *             x_array = (double *)SID_malloc(sizeof(double) * n_groups);
            double *             y_array = (double *)SID_malloc(sizeof(double) * n_groups);
            double *             z_array = (double *)SID_malloc(sizeof(double) * n_groups);
            fopen_catalog(filename_catalog_root, i_file, READ_CATALOG_GROUPS | READ_CATALOG_PROPERTIES, &fp_group_properties);
            if(fp_group_properties.n_halos_total != n_groups)
                SID_exit_error("Halo counts in group files and catalogs don't match (ie. %d!=%d)", SID_ERROR_LOGIC,
                               fp_group_properties.n_halos_total, n_groups);
            for(int i_group = 0; i_group < n_groups; i_group++) {
                fread_catalog_file(&fp_group_properties, NULL, NULL, &group_properties, NULL, i_group);
                x_array[i_group] = group_properties.position_MBP[0];
                y_array[i_group] = group_properties.position_MBP[1];
                z_array[i_group] = group_properties.position_MBP[2];
                // Enforce periodic BCs
                if(x_array[i_group] < 0.)
                    x_array[i_group] += box_size;
                if(x_array[i_group] >= box_size)
                    x_array[i_group] -= box_size;
                if(y_array[i_group] < 0.)
                    y_array[i_group] += box_size;
                if(y_array[i_group] >= box_size)
                    y_array[i_group] -= box_size;
                if(z_array[i_group] < 0.)
                    z_array[i_group] += box_size;
                if(z_array[i_group] >= box_size)
                    z_array[i_group] -= box_size;
            }
            fclose_catalog(&fp_group_properties);
            SID_log("Done.", SID_LOG_CLOSE);

            // Determine the number of bits to use for the PHKs
            for(n_bits = N_BITS_MIN; (box_size / pow(2., (double)(n_bits + 1))) > dx && n_bits <= 20;)
                n_bits++;

            // Compute PHKs
            SID_log("Computing PHKs (using %d bits per dimension)...", SID_LOG_OPEN, n_bits);
            PHK_group = (int *)SID_malloc(sizeof(int) * n_groups);
            for(int i_group = 0; i_group < n_groups; i_group++) {
                // Compute the key for this group
                PHK_group[i_group] = compute_PHK_from_Cartesian(
                    n_bits, 3, (double)x_array[i_group] / box_size, (double)y_array[i_group] / box_size, (double)z_array[i_group] / box_size);
            }
            SID_free(SID_FARG x_array);
            SID_free(SID_FARG y_array);
            SID_free(SID_FARG z_array);
            SID_log("Done.", SID_LOG_CLOSE);

            // Sort PHKs
            SID_log("Sorting PHKs...", SID_LOG_OPEN);
            merge_sort((void *)PHK_group, n_groups, &PHK_group_index, SID_INT, SORT_COMPUTE_INDEX, GBP_FALSE);
            SID_log("Done.", SID_LOG_CLOSE);

            // Count the number of particles
            for(int i_group = 0; i_group < n_groups; i_group++)
                n_particles_cumulative += n_particles_groups[PHK_group_index[i_group]];
        }

        // Write results
        SID_log("Writing results for %d groups...", SID_LOG_OPEN, n_groups);
        char filename_output_properties[256];
        sprintf(filename_output_properties, "%s_%s.catalog_PHKs", filename_PHKs_root, filename_number);
        FILE *fp_PHKs = fopen(filename_output_properties, "w");
        fwrite(&n_groups, sizeof(int), 1, fp_PHKs);
        fwrite(&n_bits, sizeof(int), 1, fp_PHKs);
        fwrite(&n_particles_cumulative, sizeof(size_t), 1, fp_PHKs);
        n_particles_cumulative = 0;
        for(int i_group = 0; i_group < n_groups; i_group++) {
            int index_temp = (int)PHK_group_index[i_group];
            n_particles_cumulative += n_particles_groups[index_temp];
            fwrite(&(PHK_group[index_temp]), sizeof(int), 1, fp_PHKs);
            fwrite(&index_temp, sizeof(int), 1, fp_PHKs);
            fwrite(&n_particles_cumulative, sizeof(size_t), 1, fp_PHKs);
        }
        fclose(fp_PHKs);
        SID_log("Done.", SID_LOG_CLOSE);

        // Clean-up
        free_plist(&plist);
        if(n_groups > 0) {
            SID_free(SID_FARG PHK_group);
            SID_free(SID_FARG PHK_group_index);
        }

        SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);
        SID_log("Done.", SID_LOG_CLOSE);
    }

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}