Пример #1
0
void init_trees_data(tree_info    *trees,
                     void       ***rval,
                     size_t        data_size,
                     int           mode,
                     const char   *name,
                     ...){
  va_list  vargs;
  va_start(vargs,name);

  // Create the new item (and apply some defaults)
  ADaPS                           *new_item;
  store_tree_data_free_parms_info *params;
  params                        =(store_tree_data_free_parms_info *)SID_malloc(sizeof(store_tree_data_free_parms_info));
  params->n_snaps               =trees->n_snaps;
  new_item                      =(ADaPS *)SID_malloc(sizeof(ADaPS));
  new_item->mode                =ADaPS_CUSTOM;
  new_item->free_function       =free_trees_data;
  new_item->free_function_params=params;

  // Determine the size of the allocation
  new_item->data_size=trees->n_snaps*sizeof(void *);

  // Allocate arrays
  new_item->data=SID_malloc(sizeof(void *)*trees->n_snaps);
  void **data=(void **)new_item->data;
  for(int i_snap=0;i_snap<trees->n_snaps;i_snap++){
     size_t n_items;
     size_t alloc_size;
     if(mode==INIT_TREE_DATA_GROUPS)
        n_items=trees->n_groups_snap_local[i_snap];
     else if(mode==INIT_TREE_DATA_SUBGROUPS)
        n_items=trees->n_subgroups_snap_local[i_snap];
     else
        SID_trap_error("Invalid init_tree_data() mode (%d).",ERROR_LOGIC,mode);
     alloc_size          =data_size*n_items;
     data[i_snap]        =SID_malloc(alloc_size); // Returns NULL if alloc_size==0
     new_item->data_size+=alloc_size;
  }

  // Give the new item its name
  vsprintf(new_item->name,name,vargs);

  // Remove any previous entries with this name
  ADaPS_remove(&(trees->data),new_item->name);

  // Place new item at the start of the list
  new_item->next=trees->data;
  trees->data   =new_item;

  (*rval)=data;

  va_end(vargs);
}
Пример #2
0
void swap_endian_grids(const char *filename_in,const char *filename_out,int mode){

  SID_log("Swapping endian of grids...",SID_LOG_OPEN);

  // Sanity check
  if(check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_FROM_NATIVE) && check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_FROM_NATIVE))
     SID_trap_error("Invalid mode flag (%d) in swap_endian_grids().",ERROR_LOGIC,mode);

  // Open input and output files
  FILE *fp_in =NULL;
  FILE *fp_out=NULL;
  if((fp_in=fopen(filename_in,"r"))==NULL)
     SID_log("not present.",SID_LOG_CLOSE,filename_in);
  else{
     if((fp_out=fopen(filename_out,"w"))==NULL)
        SID_trap_error("Could not open {%s} for writing.",ERROR_IO_OPEN,filename_out);

     // Read the needed header information and rewind
     int    n[3];
     double L[3];
     int    n_grids;
     int    scheme;
     fread_verify(&(n[0]), sizeof(int),   1,fp_in);
     fread_verify(&(n[1]), sizeof(int),   1,fp_in);
     fread_verify(&(n[2]), sizeof(int),   1,fp_in);
     fread_verify(&(L[0]), sizeof(double),1,fp_in);
     fread_verify(&(L[1]), sizeof(double),1,fp_in);
     fread_verify(&(L[2]), sizeof(double),1,fp_in);
     fread_verify(&n_grids,sizeof(int),   1,fp_in);
     fread_verify(&scheme, sizeof(int),   1,fp_in);
     if(check_mode_for_flag(mode,SWAP_SSIMPL_ENDIAN_TO_NATIVE)){
        swap_endian((char *)(n),       3,sizeof(int));
        swap_endian((char *)(&n_grids),1,sizeof(int));
     }
     int grid_size=n[0]*n[1]*n[2];
     rewind(fp_in);

     // Create a read buffer
     char *buffer=(char *)SID_malloc(sizeof(char)*grid_size*sizeof(fftw_real));

     // Process the file
     rewrite_swap_endian(fp_in,fp_out,3,sizeof(int),   buffer);
     rewrite_swap_endian(fp_in,fp_out,3,sizeof(double),buffer);
     rewrite_swap_endian(fp_in,fp_out,2,sizeof(int),   buffer);
     for(int i_grid=0;i_grid<n_grids;i_grid++){
        rewrite_swap_endian(fp_in,fp_out,GRID_IDENTIFIER_SIZE,sizeof(char),     buffer);
        rewrite_swap_endian(fp_in,fp_out,grid_size,           sizeof(fftw_real),buffer);
     }

     // Free the read buffer
     SID_free(SID_FARG buffer);

     // Close files
     fclose(fp_in);
     fclose(fp_out);
  
     SID_log("Done.",SID_LOG_CLOSE);
  }
}
Пример #3
0
void swap_endian_halos_subgroups_local(const char *filename_in_root,
                                       const char *filename_out_root,
                                       const char *filename_halo_type,
                                       int         snap_number,
                                       int         mode) {
    SID_log("Swapping endian of subgroup file...", SID_LOG_OPEN);

    // Sanity check
    if(SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_FROM_NATIVE) &&
            SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_FROM_NATIVE))
        SID_exit_error("Invalid mode flag (%d) in swap_endian_halos_subgroups_local().", SID_ERROR_LOGIC, mode);

    // Set filenames
    char filename_in[SID_MAX_FILENAME_LENGTH];
    char filename_out[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_in, "%s/%s_%03d.catalog_subgroups", filename_in_root, filename_halo_type, snap_number);
    sprintf(filename_out, "%s/%s_%03d.catalog_subgroups", filename_out_root, filename_halo_type, snap_number);

    // Open input and output files
    FILE *fp_in  = NULL;
    FILE *fp_out = NULL;
    if((fp_in = fopen(filename_in, "r")) == NULL)
        SID_exit_error("Could not open {%s} for reading.", SID_ERROR_IO_OPEN, filename_in);
    if((fp_out = fopen(filename_out, "w")) == NULL)
        SID_exit_error("Could not open {%s} for writing.", SID_ERROR_IO_OPEN, filename_out);

    // Read the needed header information and rewind
    int n_subgroups;
    int offset_size_bytes;
    SID_fread_verify(&n_subgroups, sizeof(int), 1, fp_in);
    SID_fread_verify(&offset_size_bytes, sizeof(int), 1, fp_in);
    if(SID_CHECK_BITFIELD_SWITCH(mode, SWAP_SSIMPL_ENDIAN_TO_NATIVE)) {
        swap_endian((char *)(&n_subgroups), 1, sizeof(int));
        swap_endian((char *)(&offset_size_bytes), 1, sizeof(int));
    }
    rewind(fp_in);

    // Create a read buffer
    char *buffer = (char *)SID_malloc(sizeof(char) * offset_size_bytes);

    // Process the file
    rewrite_swap_endian(fp_in, fp_out, 2, sizeof(int), buffer);
    for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++)
        rewrite_swap_endian(fp_in, fp_out, 1, sizeof(int), buffer);
    for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++)
        rewrite_swap_endian(fp_in, fp_out, 1, offset_size_bytes, buffer);
    for(int i_subgroup = 0; i_subgroup < n_subgroups; i_subgroup++)
        rewrite_swap_endian(fp_in, fp_out, 1, sizeof(int), buffer);

    // Free the read buffer
    SID_free(SID_FARG buffer);

    // Close files
    fclose(fp_in);
    fclose(fp_out);

    SID_log("Done.", SID_LOG_CLOSE);
}
Пример #4
0
double bias_model_BPR_integral(cosmo_info **cosmo, double z) {
    double       z_max = 10000.;
    interp_info *interp;
    if(!ADaPS_exist(*cosmo, "bias_model_BPR_Iz_interp")) {
        int     n_int;
        int     i_int;
        double  dz;
        double  Omega_M, Omega_k, Omega_Lambda;
        double  z_temp;
        double *x_int;
        double *y_int;
        double  log_z;
        double  dlog_z;
        n_int        = 250;
        Omega_M      = ((double *)ADaPS_fetch(*cosmo, "Omega_M"))[0];
        Omega_k      = ((double *)ADaPS_fetch(*cosmo, "Omega_k"))[0];
        Omega_Lambda = ((double *)ADaPS_fetch(*cosmo, "Omega_Lambda"))[0];
        x_int        = (double *)SID_malloc(sizeof(double) * n_int);
        y_int        = (double *)SID_malloc(sizeof(double) * n_int);
        i_int        = 0;
        x_int[i_int] = 0.;
        y_int[i_int] = pow((1. + x_int[i_int]) / E_z(Omega_M, Omega_k, Omega_Lambda, x_int[i_int]), 3.);
        i_int++;
        x_int[i_int] = take_log10(z_max) / (double)(n_int - 1);
        y_int[i_int] = pow((1. + x_int[i_int]) / E_z(Omega_M, Omega_k, Omega_Lambda, x_int[i_int]), 3.);
        log_z        = take_log10(x_int[i_int]);
        dlog_z       = (take_log10(z_max) - log_z) / (double)(n_int - 2);
        for(i_int++, log_z += dlog_z; i_int < (n_int - 1); i_int++, log_z += dlog_z) {
            x_int[i_int] = take_alog10(log_z);
            y_int[i_int] = pow((1. + x_int[i_int]) / E_z(Omega_M, Omega_k, Omega_Lambda, x_int[i_int]), 3.);
        }
        x_int[i_int] = z_max;
        y_int[i_int] = pow((1. + x_int[i_int]) / E_z(Omega_M, Omega_k, Omega_Lambda, x_int[i_int]), 3.);
        init_interpolate(x_int, y_int, (size_t)n_int, gsl_interp_cspline, &interp);
        SID_free(SID_FARG x_int);
        SID_free(SID_FARG y_int);
        ADaPS_store_interp(cosmo, (void *)(interp), "bias_model_BPR_Iz_interp");

    } else
        interp = (interp_info *)ADaPS_fetch(*cosmo, "bias_model_BPR_Iz_interp");
    return (interpolate_integral(interp, z, z_max));
}
void init_tree_property_tau(trend_property_info *property, void *trees_in, int i_hist, int *mode, gbp_va_list *vargs) {
    (*mode)              = GBP_HISTOGRAM_IRREGULAR_XLO_DEFINED;
    tree_info *trees     = (tree_info *)trees_in;
    double *   tau_array = (double *)SID_malloc(sizeof(double) * trees->n_snaps);
    int        n_bins;
    int        i_0;
    if(property->is_ordinate) {
        n_bins = trees->n_snaps;
        i_0    = trees->n_snaps - 1;
    } else {
        n_bins = GBP_MIN(i_hist + 1, trees->n_snaps);
        i_0    = i_hist;
    }
    SID_va_start(vargs);
    SID_add_va_arg(vargs, sizeof(double), &tau_array);
    SID_add_va_arg(vargs, sizeof(int), &n_bins);
    for(int i_tau = 0; i_tau < n_bins; i_tau++)
        tau_array[i_tau] = (trees->t_list[i_0] - trees->t_list[i_0 - i_tau]) / t_dyn_z(trees->z_list[i_0], trees->cosmo);
}
Пример #6
0
void set_MCMC_covariance(MCMC_info *MCMC, double *V) {
    int i_P, j_P;
    if(V == NULL)
        SID_log("Initializing the covariance matrix...", SID_LOG_OPEN);
    else
        SID_log("Updating the covariance matrix...", SID_LOG_OPEN);
    if(MCMC->V == NULL)
        MCMC->V = (double *)SID_malloc(sizeof(double) * MCMC->n_P * MCMC->n_P);
    if(MCMC->m == NULL)
        MCMC->m = gsl_matrix_calloc(MCMC->n_P, MCMC->n_P);
    if(MCMC->b == NULL)
        MCMC->b = gsl_vector_calloc(MCMC->n_P);
    if(V == NULL) {
        for(i_P = 0; i_P < MCMC->n_P; i_P++) {
            for(j_P = 0; j_P < MCMC->n_P; j_P++) {
                if(i_P == j_P) {
                    // Matrix decomposition fails if we initialize an element to zero here.
                    //   Check for this and start with an different value if true.
                    if(MCMC->P_init[i_P] == 0.)
                        MCMC->V[i_P * MCMC->n_P + j_P] = 1e-3 * MCMC->P_limit_max[i_P];
                    else
                        MCMC->V[i_P * MCMC->n_P + j_P] = pow(MCMC->P_init[i_P], 2.);
                } else
                    MCMC->V[i_P * MCMC->n_P + j_P] = 0.;
            }
        }
    } else
        memcpy(MCMC->V, V, (size_t)(MCMC->n_P * MCMC->n_P) * sizeof(double));
    for(i_P = 0; i_P < MCMC->n_P; i_P++)
        for(j_P = 0; j_P < MCMC->n_P; j_P++)
            gsl_matrix_set(MCMC->m, i_P, j_P, MCMC->V[i_P * MCMC->n_P + j_P]);
    gsl_linalg_cholesky_decomp(MCMC->m);

    // Set the upper-diagonal to zero to get L where covariance=L*L^T
    for(i_P = 0; i_P < MCMC->n_P; i_P++)
        for(j_P = i_P + 1; j_P < MCMC->n_P; j_P++)
            gsl_matrix_set(MCMC->m, i_P, j_P, 0.);
    SID_log("Done.", SID_LOG_CLOSE);
}
Пример #7
0
void init_image(int width, int height, const char *colourmapselect, image_info **image) {
    int i_pixel;

    // Allocate image
    (*image) = (image_info *)SID_malloc(sizeof(image_info));

    // Set image specs
    (*image)->width    = width;
    (*image)->height   = height;
    (*image)->n_pixels = width * height;

    // Init gdlib stuff
    (*image)->gd_ptr = gdImageCreateTrueColor((*image)->width, (*image)->height);

    // Allocate image buffers
    (*image)->values = (double *)malloc(sizeof(double) * (*image)->n_pixels);
    for(i_pixel = 0; i_pixel < (*image)->n_pixels; i_pixel++)
        (*image)->values[i_pixel] = 0.;

    // Set colourtable
    strcpy((*image)->colourmapselect, colourmapselect);
    create_colour_table(colourmapselect, &((*image)->n_colours), &((*image)->colour_table));
}
Пример #8
0
void read_mark_file(plist_info *plist,
                    const char *mark_name,
                    const char *filename_in,
                    int         mode){
  int      i_species;
  size_t   i_particle;
  size_t   j_particle;
  size_t   k_particle;
  int      i_rank;
  size_t   i_mark;
  size_t   n_particles_local;
  size_t  *mark_list_buffer;
  int     *mark_list;
  size_t  *ids_local;
  size_t  *mark_list_local;
  size_t   n_mark_total;
  size_t   n_mark_total_check;
  size_t   n_mark_type_local[N_GADGET_TYPE];
  size_t   n_mark_local;
  size_t   n_particle_local;
  SID_fp   fp_mark_file;
  size_t   i_start_local[N_GADGET_TYPE];
  size_t   n_mark_bcast;
  size_t  *ids_local_index;
  size_t   n_buffer;
  int      flag_allocate;
  int      flag_read_mode;
  int      flag_mark_mode;
  int      flag_op_mode;
  markfile_header_info header={N_GADGET_TYPE};
  
  SID_log("Reading mark file...",SID_LOG_OPEN);
  
  // Interpret run mode
  if(check_mode_for_flag(mode,MARK_READ_ALL))
    flag_read_mode=MARK_READ_ALL;
  else
    flag_read_mode=MARK_DEFAULT;
  if(check_mode_for_flag(mode,MARK_LIST_ONLY))
    flag_mark_mode=MARK_LIST_ONLY;
  else
    flag_mark_mode=MARK_DEFAULT;
  if(check_mode_for_flag(mode,MARK_INIT) || check_mode_for_flag(mode,MARK_OR))
    flag_op_mode=MARK_DEFAULT;
  else
    flag_op_mode=MARK_AND;
  
  // Open mark list and read header
  SID_fopen_chunked(filename_in,
                    "r",
                    &fp_mark_file,
                    &header);
  if(header.n_type!=N_GADGET_TYPE)
    SID_trap_error("Inconsistant number of species in mark file (ie. %d!=%d)!",ERROR_LOGIC,header.n_type,N_GADGET_TYPE);

  // List numbers of particles in the log output
  size_t n_particles_all;
  int    n_non_zero;
  for(i_species=0,n_particles_all=0,n_non_zero=0;i_species<header.n_type;i_species++){
    if(header.n_mark_species[i_species]>0){
      n_particles_all+=header.n_mark_species[i_species];
      n_non_zero++;
    }
  }
  SID_log("%lld",SID_LOG_CONTINUE,n_particles_all);
  if(n_non_zero>0)
    SID_log(" (",SID_LOG_CONTINUE,n_particles_all);
  for(i_species=0;i_species<N_GADGET_TYPE;i_species++){
    if(header.n_mark_species[i_species]>0){
      if(i_species==n_non_zero-1){
        if(n_non_zero>1)
          SID_log("and %lld %s",SID_LOG_CONTINUE,header.n_mark_species[i_species],plist->species[i_species]);
        else
          SID_log("%lld %s",SID_LOG_CONTINUE,header.n_mark_species[i_species],plist->species[i_species]);
      }
      else{
        if(n_non_zero>1)
          SID_log("%lld %s, ",SID_LOG_CONTINUE,header.n_mark_species[i_species],plist->species[i_species]);
        else
          SID_log("%lld %s",SID_LOG_CONTINUE,header.n_mark_species[i_species],plist->species[i_species]);
      }
    }
  }
  if(n_non_zero>0)
    SID_log(") particles...",SID_LOG_CONTINUE);
  else
    SID_log(" particles...",SID_LOG_CONTINUE);


  // Set list sizes and prep offsets for reading
  for(i_species=0,n_mark_local=0,n_mark_total_check=0;i_species<header.n_type;i_species++){
    if(header.n_mark_species[i_species]>0){
      ADaPS_store(&(plist->data),(void *)(&(header.n_mark_species[i_species])),"n_%s_%s",ADaPS_SCALAR_SIZE_T,mark_name,plist->species[i_species]);
      switch(flag_read_mode){
      case MARK_READ_ALL:
        n_mark_type_local[i_species]=header.n_mark_species[i_species];
        i_start_local[i_species]    =0;
        break;
      default:
        n_mark_type_local[i_species]=header.n_mark_species[i_species]/SID.n_proc;
        i_start_local[i_species]    =(SID.My_rank)*n_mark_type_local[i_species];
        if(SID.I_am_last_rank)
          n_mark_type_local[i_species]=header.n_mark_species[i_species]-i_start_local[i_species];
        break;
      }
      ADaPS_store(&(plist->data),(void *)(&(n_mark_type_local[i_species])),"n_local_%s_%s",ADaPS_SCALAR_SIZE_T,mark_name,plist->species[i_species]);
      n_mark_local      +=n_mark_type_local[i_species];
      n_mark_total_check+=header.n_mark_species[i_species];
    }
  }

  // Sanity check
  SID_Allreduce(&n_mark_local,&n_mark_total,1,SID_SIZE_T,SID_SUM,SID.COMM_WORLD);
  if(n_mark_total!=n_mark_total_check)
    SID_trap_error("Particle numbers don't add-up right in read_mark_file!",ERROR_LOGIC);

  // Read file and create/store mark arrays
  switch(flag_mark_mode){
  case MARK_LIST_ONLY:
    for(i_species=0;i_species<header.n_type;i_species++){
      if(header.n_mark_species[i_species]>0){
        // Allocate array
        if(n_mark_type_local[i_species]>0)
          mark_list_local=(size_t *)SID_malloc(sizeof(size_t)*n_mark_type_local[i_species]);
        else
          mark_list_local=NULL;

        // Perform read
        SID_fread_chunked(mark_list_local,
                          n_mark_type_local[i_species],
                          i_start_local[i_species],
                          &fp_mark_file);

        // Sort marked particles
        if(n_mark_type_local[i_species]>0){
          merge_sort(mark_list_local,n_mark_type_local[i_species],NULL,SID_SIZE_T,SORT_INPLACE_ONLY,SORT_COMPUTE_INPLACE);
          ADaPS_store(&(plist->data),(void *)(mark_list_local),"%s_%s",ADaPS_DEFAULT,mark_name,plist->species[i_species]);
        }
      }
    }
    break;
  default:
    mark_list_buffer=(size_t *)SID_malloc(sizeof(size_t)*MAX_MARK_BUFFER_SIZE);
    for(i_species=0;i_species<header.n_type;i_species++){
      if(header.n_mark_species[i_species]>0){
        n_particles_local=((size_t *)ADaPS_fetch(plist->data,"n_%s",plist->species[i_species]))[0];
        // Initialize arrays
        ids_local=(size_t *)ADaPS_fetch(plist->data,"id_%s",plist->species[i_species]);
        if(ADaPS_exist(plist->data,"%s_%s",mark_name,plist->species[i_species])){
          mark_list=(int *)ADaPS_fetch(plist->data,"%s_%s",mark_name,plist->species[i_species]);
          flag_allocate=FALSE;
        }
        else{
          mark_list=(int *)SID_malloc(sizeof(int)*n_particles_local);
          for(i_particle=0;i_particle<n_particles_local;i_particle++)
            mark_list[i_particle]=FALSE;
          flag_allocate=TRUE;
        }      
        merge_sort(ids_local,n_particles_local,&ids_local_index,SID_SIZE_T,SORT_COMPUTE_INDEX,SORT_COMPUTE_NOT_INPLACE);
        // Use a buffer to increase speed
        for(i_particle=0;i_particle<header.n_mark_species[i_species];){
          n_buffer=MIN(header.n_mark_species[i_species]-i_particle,MAX_MARK_BUFFER_SIZE);
          SID_fread_chunked_all(mark_list_local,
                                n_buffer,
                                &fp_mark_file);
          merge_sort(mark_list_local,n_buffer,NULL,SID_SIZE_T,SORT_INPLACE_ONLY,SORT_COMPUTE_INPLACE);
          for(j_particle=0,k_particle=find_index(ids_local,mark_list_buffer[0],n_particles_local,ids_local_index);
              j_particle<n_buffer;
              j_particle++,i_particle++){
            while(ids_local[ids_local_index[k_particle]]<mark_list_local[j_particle] && k_particle<n_buffer-1) k_particle++;
            if(ids_local[ids_local_index[k_particle]]==mark_list_local[j_particle]){
              switch(flag_op_mode){
              case MARK_INIT:
              case MARK_AND:
              case MARK_OR:
                mark_list[i_particle]=TRUE;
                break;
              }
            }
          }
        }
        SID_free((void **)&ids_local_index);
        ADaPS_store(&(plist->data),(void *)mark_list,"%s_%s",ADaPS_DEFAULT,mark_name,plist->species[i_species]);
      }
    }
    SID_free((void **)&mark_list_buffer);
    break;
  }
  SID_fclose_chunked(&fp_mark_file);

  SID_log("Done.",SID_LOG_CLOSE);
}
Пример #9
0
int main(int argc, char *argv[]) {
    plist_info plist;
    char       filename_PHKs_root[256];
    char       filename_snapshot_root[256];
    char       filename_snapshot[256];
    char       filename_output_properties_dir[256];
    char       filename_output_properties[256];
    char       filename_output_profiles_dir[256];
    char       filename_output_profiles[256];
    char       filename_output_SO_dir[256];
    char       filename_output_SO[256];
    char       filename_output_properties_temp[256];
    char       filename_output_profiles_temp[256];
    char       filename_output_SO_temp[256];
    char       group_text_prefix[4];
    int        n_groups_process;
    int        n_groups;
    int        n_groups_all;
    int        i_rank;
    int        i_group;
    int        i_file_lo_in;
    int        i_file_lo;
    int        i_file_hi_in;
    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;
    size_t     n_particles_cumulative;
    GBPREAL *  x_array;
    GBPREAL *  y_array;
    GBPREAL *  z_array;
    size_t *   ids_particles;
    size_t *   ids_particles_index;
    size_t *   ids_groups;
    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_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     particle_mass;

    int         r_val;
    struct stat file_stats;
    size_t      n_bytes;
    size_t      n_bytes_buffer;
    void *      buffer;

    FILE *               fp_PHKs;
    FILE *               fp_profiles;
    FILE *               fp_PHKs_temp;
    FILE *               fp_profiles_temp;
    cosmo_info *         cosmo;
    halo_properties_info properties;
    halo_profile_info    profile;
    int                  n_temp;
    int                  n_truncated;
    int                  largest_truncated;
    int                  largest_truncated_local;
    char *               filename_number;

    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    char filename_in_root[SID_MAX_FILENAME_LENGTH];
    char filename_out_root[SID_MAX_FILENAME_LENGTH];
    char filename_properties_in[SID_MAX_FILENAME_LENGTH];
    char filename_properties_out[SID_MAX_FILENAME_LENGTH];
    char filename_profiles_in[SID_MAX_FILENAME_LENGTH];
    char filename_profiles_out[SID_MAX_FILENAME_LENGTH];
    char filename_SO_in[SID_MAX_FILENAME_LENGTH];
    char filename_SO_out[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_in_root, argv[1]);
    strcpy(filename_out_root, argv[2]);
    int start_snap     = atoi(argv[3]);
    int stop_snap      = atoi(argv[4]);
    int n_files_out_in = atoi(argv[5]);

    // Process each snapshot in turn
    SID_log("Rewriting catalogs from root {%s} to root {%s} with %d files...", SID_LOG_OPEN, filename_in_root, filename_out_root, n_files_out_in);
    for(int i_snap = start_snap; i_snap <= stop_snap; i_snap++) {
        SID_log("Processing snapshot No. %d...", SID_LOG_OPEN | SID_LOG_TIMER, i_snap);

        // Loop once for subgroups and once for groups
        for(int i_run = 0; i_run < 2; i_run++) {
            char filename_in[SID_MAX_FILENAME_LENGTH];
            char filename_out[SID_MAX_FILENAME_LENGTH];
            char group_prefix_text[8];
            switch(i_run) {
                case 0:
                    sprintf(group_prefix_text, "sub");
                    break;
                case 1:
                    sprintf(group_prefix_text, "");
                    break;
            }
            SID_log("Processing %sgroups...", SID_LOG_OPEN | SID_LOG_TIMER, group_prefix_text);
            sprintf(filename_properties_in, "%s_%03d.catalog_%sgroups_properties", filename_in_root, i_snap, group_prefix_text);
            sprintf(filename_properties_out, "%s_%03d.catalog_%sgroups_properties", filename_out_root, i_snap, group_prefix_text);
            sprintf(filename_profiles_in, "%s_%03d.catalog_%sgroups_profiles", filename_in_root, i_snap, group_prefix_text);
            sprintf(filename_profiles_out, "%s_%03d.catalog_%sgroups_profiles", filename_out_root, i_snap, group_prefix_text);
            sprintf(filename_SO_in, "%s_%03d.catalog_%sgroups_SO", filename_in_root, i_snap, group_prefix_text);
            sprintf(filename_SO_out, "%s_%03d.catalog_%sgroups_SO", filename_out_root, i_snap, group_prefix_text);

            // Create filename bases for each dataset
            char filename_properties_in_base[SID_MAX_FILENAME_LENGTH];
            char filename_properties_out_base[SID_MAX_FILENAME_LENGTH];
            char filename_profiles_in_base[SID_MAX_FILENAME_LENGTH];
            char filename_profiles_out_base[SID_MAX_FILENAME_LENGTH];
            char filename_SO_in_base[SID_MAX_FILENAME_LENGTH];
            char filename_SO_out_base[SID_MAX_FILENAME_LENGTH];
            strcpy(filename_properties_in_base, filename_properties_in);
            strcpy(filename_properties_out_base, filename_properties_out);
            strcpy(filename_profiles_in_base, filename_profiles_in);
            strcpy(filename_profiles_out_base, filename_profiles_out);
            strcpy(filename_SO_in_base, filename_SO_in);
            strcpy(filename_SO_out_base, filename_SO_out);
            strip_path(filename_properties_in_base);
            strip_path(filename_properties_out_base);
            strip_path(filename_profiles_in_base);
            strip_path(filename_profiles_out_base);
            strip_path(filename_SO_in_base);
            strip_path(filename_SO_out_base);

            // Figure out if the properties file(s) are multi-file format
            int   i_file;
            int   n_files_props;
            int   n_props;
            int   n_props_all;
            int   flag_multifile_properties = GBP_FALSE;
            FILE *fp_test;
            char  filename_test[SID_MAX_FILENAME_LENGTH];
            sprintf(filename_test, "%s/%s.0", filename_properties_in, filename_properties_in_base);
            if((fp_test = fopen(filename_test, "r")) != NULL) {
                SID_fread_verify(&i_file, sizeof(int), 1, fp_test);
                SID_fread_verify(&n_files_props, sizeof(int), 1, fp_test);
                SID_fread_verify(&n_props, sizeof(int), 1, fp_test);
                SID_fread_verify(&n_props_all, sizeof(int), 1, fp_test);
                fclose(fp_test);
                flag_multifile_properties = GBP_TRUE;
            } else {
                sprintf(filename_test, "%s", filename_properties_in);
                if((fp_test = fopen(filename_test, "r")) != NULL) {
                    SID_fread_verify(&i_file, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_files_props, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_props, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_props_all, sizeof(int), 1, fp_test);
                    fclose(fp_test);
                    flag_multifile_properties = GBP_FALSE;
                    if(n_props != n_props_all)
                        SID_exit_error("Halo counts don't agree (ie. %d!=%d) in properties file.", SID_ERROR_LOGIC,
                                       n_props, n_props_all);
                    if(n_files_props != 1)
                        SID_exit_error("Invalid file count (%d) in non-multifile properties file {%s}.",
                                       SID_ERROR_LOGIC, n_files_props, filename_test);
                } else
                    SID_exit_error("Could not open properties dataset.", SID_ERROR_IO_OPEN);
            }
            if(i_file != 0)
                SID_exit_error("Invalid starting file index (%d) in properties file.", SID_ERROR_LOGIC, i_file);

            // Figure out if the profiles file(s) are multi-file format
            int n_files_profs;
            int n_profs;
            int n_profs_all;
            int flag_multifile_profiles = GBP_FALSE;
            sprintf(filename_test, "%s/%s.0", filename_profiles_in, filename_profiles_in_base);
            if((fp_test = fopen(filename_test, "r")) != NULL) {
                int i_file;
                SID_fread_verify(&i_file, sizeof(int), 1, fp_test);
                SID_fread_verify(&n_files_profs, sizeof(int), 1, fp_test);
                SID_fread_verify(&n_profs, sizeof(int), 1, fp_test);
                SID_fread_verify(&n_profs_all, sizeof(int), 1, fp_test);
                fclose(fp_test);
                flag_multifile_profiles = GBP_TRUE;
            } else {
                sprintf(filename_test, "%s", filename_profiles_in);
                if((fp_test = fopen(filename_test, "r")) != NULL) {
                    SID_fread_verify(&i_file, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_files_profs, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_profs, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_profs_all, sizeof(int), 1, fp_test);
                    fclose(fp_test);
                    flag_multifile_profiles = GBP_FALSE;
                    if(n_profs != n_profs_all)
                        SID_exit_error("Halo counts don't agree (ie. %d!=%d) in profiles file.", SID_ERROR_LOGIC,
                                       n_profs, n_profs_all);
                    if(n_files_profs != 1)
                        SID_exit_error("Invalid file count (%d) in non-multifile profiles file.", SID_ERROR_LOGIC,
                                       n_files_profs);
                } else
                    SID_exit_error("Could not open profiles dataset.", SID_ERROR_IO_OPEN);
            }
            if(i_file != 0)
                SID_exit_error("Invalid starting file index (%d) in profiles file.", SID_ERROR_LOGIC, i_file);

            // Check that the halo counts in the properties and profiles datasets agree
            if(n_profs_all != n_props_all)
                SID_exit_error("The properties and profiles halo counts don't agree (ie. %d!=%d)", SID_ERROR_LOGIC,
                               n_profs_all, n_props_all);
            int n_halos_all = n_props_all;
            SID_log("(%d halos)...", SID_LOG_CONTINUE, n_halos_all);

            // SO files are defined only for groups
            int n_files_SO;
            int n_SO;
            int n_SO_all;
            int flag_multifile_SO = GBP_FALSE;
            int flag_SO_present   = GBP_TRUE;
            if(i_run == 1) {
                // Figure out if the SO file(s) are multi-file format
                sprintf(filename_test, "%s/%s.0", filename_SO_in, filename_SO_in_base);
                if((fp_test = fopen(filename_test, "r")) != NULL) {
                    int i_file;
                    SID_fread_verify(&i_file, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_files_SO, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_SO, sizeof(int), 1, fp_test);
                    SID_fread_verify(&n_SO_all, sizeof(int), 1, fp_test);
                    fclose(fp_test);
                    flag_multifile_SO = GBP_TRUE;
                } else {
                    sprintf(filename_test, "%s", filename_SO_in);
                    if((fp_test = fopen(filename_test, "r")) != NULL) {
                        SID_fread_verify(&i_file, sizeof(int), 1, fp_test);
                        SID_fread_verify(&n_files_SO, sizeof(int), 1, fp_test);
                        SID_fread_verify(&n_SO, sizeof(int), 1, fp_test);
                        SID_fread_verify(&n_SO_all, sizeof(int), 1, fp_test);
                        fclose(fp_test);
                        flag_multifile_SO = GBP_FALSE;
                        if(n_SO != n_SO_all)
                            SID_exit_error("Halo counts don't agree (ie. %d!=%d) in SO file.", SID_ERROR_LOGIC, n_SO,
                                           n_SO_all);
                        if(n_files_SO != 1)
                            SID_exit_error("Invalid file count (%d) in non-multifile SO file.", SID_ERROR_LOGIC,
                                           n_files_SO);
                    }
                    // Not all snapshots are garanteed to have an SO dataset.  Continue if one isn't found
                    else {
                        flag_SO_present = GBP_FALSE;
                        SID_log("Could not open SO dataset.", SID_LOG_COMMENT);
                    }
                }
                if(i_file != 0)
                    SID_exit_error("Invalid starting file index (%d) in SO file.", SID_ERROR_LOGIC, i_file);

                // Check that the halo counts in the properties and profiles datasets agree
                if(n_SO_all != n_props_all && flag_SO_present)
                    SID_exit_error("The properties and SO halo counts don't agree (ie. %d!=%d)", SID_ERROR_LOGIC,
                                   n_SO_all, n_props_all);
            }

            // If there are fewer than 1000 halos, don't bother
            //    writting to a multi-file
            int n_files_out = n_files_out_in;
            if(n_halos_all < GBP_MAX(n_files_out_in, 1000))
                n_files_out = 1;

            // Perform rewrites
            int n_rewrite = 2;
            if(i_run == 1 && flag_SO_present)
                n_rewrite = 3;
            for(int i_rewrite = 0; i_rewrite < n_rewrite; i_rewrite++) {
                char *buffer        = NULL;
                FILE *fp_read       = NULL;
                FILE *fp_write      = NULL;
                int   i_file_read   = 0;
                int   i_file_write  = 0;
                int   i_halo_read   = 0; // file index
                int   i_halo_write  = 0; // file index
                int   j_halo_read   = 0; // catalog_index
                int   j_halo_write  = 0; // catalog index
                int   n_halos_read  = 0;
                int   n_halos_write = 0;
                int   n_files_rewrite;
                int   n_items_all;
                int   flag_multifile;
                char *filename_items_in;
                char *filename_items_in_base;
                char *filename_items_out;
                char *filename_items_out_base;
                if(i_rewrite == 0) {
                    SID_log("Processing properties...", SID_LOG_OPEN | SID_LOG_TIMER);
                    buffer                  = (char *)SID_malloc(sizeof(halo_properties_info));
                    n_files_rewrite         = n_files_props;
                    n_items_all             = n_props_all;
                    flag_multifile          = flag_multifile_properties;
                    filename_items_in       = filename_properties_in;
                    filename_items_in_base  = filename_properties_in_base;
                    filename_items_out      = filename_properties_out;
                    filename_items_out_base = filename_properties_out_base;
                } else if(i_rewrite == 1) {
                    SID_log("Processing profiles...", SID_LOG_OPEN | SID_LOG_TIMER);
                    buffer                  = (char *)SID_malloc(sizeof(halo_profile_bin_info));
                    n_files_rewrite         = n_files_profs;
                    n_items_all             = n_profs_all;
                    flag_multifile          = flag_multifile_profiles;
                    filename_items_in       = filename_profiles_in;
                    filename_items_in_base  = filename_profiles_in_base;
                    filename_items_out      = filename_profiles_out;
                    filename_items_out_base = filename_profiles_out_base;
                } else if(i_rewrite == 2) {
                    SID_log("Processing SO files...", SID_LOG_OPEN | SID_LOG_TIMER);
                    buffer                  = (char *)SID_malloc(6 * sizeof(float));
                    n_files_rewrite         = n_files_SO;
                    n_items_all             = n_SO_all;
                    flag_multifile          = flag_multifile_SO;
                    filename_items_in       = filename_SO_in;
                    filename_items_in_base  = filename_SO_in_base;
                    filename_items_out      = filename_SO_out;
                    filename_items_out_base = filename_SO_out_base;
                }
                for(int i_halo = 0; i_halo < n_halos_all; i_halo++) {
                    // Open a new input file if need-be
                    while(i_halo_read >= n_halos_read && i_file_read < n_files_rewrite) {
                        if(!flag_multifile && i_file_read > 0)
                            SID_exit_error("Trying to open a second file in a non-multifile dataset.", SID_ERROR_LOGIC);
                        if(flag_multifile)
                            sprintf(filename_in, "%s/%s.%d", filename_items_in, filename_items_in_base, i_file_read);
                        else
                            sprintf(filename_in, "%s", filename_items_in);
                        if(fp_read != NULL)
                            fclose(fp_read);
                        if((fp_read = fopen(filename_in, "r")) != NULL) {
                            int n_files_in;
                            SID_fread_verify(&i_file, sizeof(int), 1, fp_read);
                            SID_fread_verify(&n_files_in, sizeof(int), 1, fp_read);
                            SID_fread_verify(&n_halos_read, sizeof(int), 1, fp_read);
                            SID_fread_verify(&n_items_all, sizeof(int), 1, fp_read);
                            if(n_files_in != n_files_rewrite)
                                SID_exit_error("File counts are not consistant (ie. %d!=%d).", SID_ERROR_LOGIC,
                                               n_files_in, n_files_rewrite);
                        } else
                            SID_exit_error("Could not open {%s}.", SID_ERROR_IO_OPEN, filename_in);
                        if(i_file != i_file_read)
                            SID_exit_error("Invalid file index in (ie. %d!=%d).", SID_ERROR_LOGIC, i_file, i_file_read);
                        if(n_items_all != n_halos_all)
                            SID_exit_error("Invalid total halo count in {%s} (ie. %d!=%d).", SID_ERROR_LOGIC,
                                           filename_in, n_items_all, n_halos_all);
                        i_halo_read = 0;
                        i_file_read++;
                    }
                    // Open a new output file if need-be
                    if(i_halo_write >= n_halos_write) {
                        if(n_files_out == 0 && i_file_write > 0)
                            SID_exit_error("Trying to create a second file in a non-multifile dataset.",
                                           SID_ERROR_LOGIC);
                        if(n_files_out > 1) {
                            if(i_file_write == 0)
                                mkdir(filename_items_out, 02755);
                            sprintf(filename_out, "%s/%s.%d", filename_items_out, filename_items_out_base, i_file_write);
                        } else
                            sprintf(filename_out, "%s", filename_items_out);
                        if(i_file_write == (n_files_out - 1))
                            n_halos_write = n_halos_all - i_halo_write;
                        else
                            n_halos_write = (int)((float)(i_file_write + 1) * (float)n_halos_all / (float)n_files_out) - j_halo_write;
                        int n_halos_left = n_halos_all - j_halo_write;
                        if(n_halos_write > n_halos_left)
                            n_halos_write = n_halos_left;
                        if(fp_write != NULL)
                            fclose(fp_write);
                        // SID_log("Opening {%s} for write.",SID_LOG_COMMENT,filename_out);
                        if((fp_write = fopen(filename_out, "w")) != NULL) {
                            fwrite(&i_file_write, sizeof(int), 1, fp_write);
                            fwrite(&n_files_out, sizeof(int), 1, fp_write);
                            fwrite(&n_halos_write, sizeof(int), 1, fp_write);
                            fwrite(&n_halos_all, sizeof(int), 1, fp_write);
                        } else
                            SID_exit_error("Could not create properties file {%s}.", SID_ERROR_IO_OPEN, filename_out);
                        i_halo_write = 0;
                        i_file_write++;
                    }
                    switch(i_rewrite) {
                        // Rewrite properties
                        case 0:
                            SID_fread_verify(buffer, sizeof(halo_properties_info), 1, fp_read);
                            fwrite(buffer, sizeof(halo_properties_info), 1, fp_write);
                            break;
                        // Rewrite profiles
                        case 1: {
                            int n_bins;
                            SID_fread_verify(&n_bins, sizeof(int), 1, fp_read);
                            fwrite(&n_bins, sizeof(int), 1, fp_write);
                            for(int i_bin = 0; i_bin < n_bins; i_bin++) {
                                SID_fread_verify(buffer, sizeof(halo_profile_bin_info), 1, fp_read);
                                fwrite(buffer, sizeof(halo_profile_bin_info), 1, fp_write);
                            }
                            break;
                        }
                        // Rewrite SOs
                        case 2:
                            SID_fread_verify(buffer, sizeof(float), 6, fp_read);
                            fwrite(buffer, sizeof(float), 6, fp_write);
                            break;
                    }
                    i_halo_read++;
                    j_halo_read++;
                    i_halo_write++;
                    j_halo_write++;
                } // i_halo
                SID_free(SID_FARG buffer);
                if(fp_read != NULL)
                    fclose(fp_read);
                if(fp_write != NULL)
                    fclose(fp_write);
                fp_read  = NULL;
                fp_write = NULL;
                // Sanity check
                if(j_halo_read != n_halos_all)
                    SID_exit_error("The proper number of halos was not read (ie. %d!=%d).", SID_ERROR_IO_OPEN,
                                   j_halo_read, n_halos_all);
                if(j_halo_write != n_halos_all)
                    SID_exit_error("The proper number of halos was not written (ie. %d!=%d).", SID_ERROR_IO_OPEN,
                                   j_halo_write, n_halos_all);
                // If any files need to be zero-filled, do so now
                for(; i_file_write < n_files_out; i_file_write++) {
                    if(n_files_out == 0 && i_file_write > 0)
                        SID_exit_error("Trying to create a second file in a non-multifile dataset.", SID_ERROR_LOGIC);
                    if(n_files_out > 1) {
                        if(i_file_write == 0)
                            mkdir(filename_items_out, 02755);
                        sprintf(filename_out, "%s/%s.%d", filename_items_out, filename_items_out_base, i_file_write);
                    } else
                        sprintf(filename_out, "%s", filename_items_out);
                    if(i_file_write == (n_files_out - 1))
                        n_halos_write = n_halos_all - i_halo_write;
                    else
                        n_halos_write = (int)((float)(i_file_write + 1) * (float)n_halos_all / (float)n_files_out) - j_halo_write;
                    int n_halos_left = n_halos_all - j_halo_write;
                    if(n_halos_write > n_halos_left)
                        n_halos_write = n_halos_left;
                    if(fp_write != NULL)
                        fclose(fp_write);
                    // SID_log("Opening {%s} for write.",SID_LOG_COMMENT,filename_out);
                    if((fp_write = fopen(filename_out, "w")) != NULL) {
                        fwrite(&i_file_write, sizeof(int), 1, fp_write);
                        fwrite(&n_files_out, sizeof(int), 1, fp_write);
                        fwrite(&n_halos_write, sizeof(int), 1, fp_write);
                        fwrite(&n_halos_all, sizeof(int), 1, fp_write);
                    } else
                        SID_exit_error("Could not create file {%s}.", SID_ERROR_IO_OPEN, filename_out);
                }
                if(fp_write != NULL)
                    fclose(fp_write);
                SID_log("Done.", SID_LOG_CLOSE);
            }
            SID_log("Done.", SID_LOG_CLOSE);
        } // i_run
        SID_log("Done.", SID_LOG_CLOSE);
    } // i_snap
    SID_log("Done.", SID_LOG_CLOSE);

    SID_Finalize();
}
Пример #10
0
void init_cfunc(cfunc_info *cfunc,const char *filename_cosmology,int    n_data,  int    n_random,int    n_bits_PHK,
                double redshift,  double box_size,int    n_jack,
                double r_min_l1D, double r_max_1D,double dr_1D,
                double r_min_2D,  double r_max_2D,double dr_2D){
  SID_log("Initializing correlation function...",SID_LOG_OPEN);

  // Initialize flags
  cfunc->initialized    =TRUE;
  cfunc->flag_compute_RR=TRUE;

  // Initialize constants
  cfunc->n_data    =n_data;
  cfunc->n_random  =n_random;
  cfunc->F_random  =(double)n_random/(double)n_data;
  cfunc->redshift  =redshift;
  cfunc->box_size  =box_size;
  cfunc->n_jack    =n_jack;
  cfunc->r_min_l1D =r_min_l1D;
  cfunc->lr_min_l1D=take_log10(r_min_l1D);
  cfunc->r_max_1D  =r_max_1D;
  cfunc->r_min_2D  =r_min_2D;
  cfunc->r_max_2D  =r_max_2D;
  cfunc->r_max     =MAX(cfunc->r_max_1D,cfunc->r_max_2D);
  cfunc->dr_1D     =dr_1D;
  cfunc->dr_2D     =dr_2D;

  // Decide on PHK boundary widths
  cfunc->n_bits_PHK=n_bits_PHK;
  for(cfunc->PHK_width=1;cfunc->PHK_width<20 && (double)cfunc->PHK_width*(cfunc->box_size/pow(2.,(double)(cfunc->n_bits_PHK)))<cfunc->r_max;) 
     cfunc->PHK_width++;

  // Initialize the number of bins
  cfunc->n_1D        =(int)(0.5+(cfunc->r_max_1D)/cfunc->dr_1D);
  cfunc->n_2D        =(int)(0.5+(cfunc->r_max_2D-cfunc->r_min_2D)/cfunc->dr_2D);
  cfunc->n_2D_total  =cfunc->n_2D*cfunc->n_2D;
  cfunc->n_jack_total=cfunc->n_jack*cfunc->n_jack*cfunc->n_jack;
  SID_log("keys        =%d-bit", SID_LOG_COMMENT,cfunc->n_bits_PHK);
  SID_log("boundries   =%d keys",SID_LOG_COMMENT,cfunc->PHK_width);
  SID_log("# of 1D bins=%d",     SID_LOG_COMMENT,cfunc->n_1D);
  SID_log("# of 2D bins=%d",     SID_LOG_COMMENT,cfunc->n_2D);

  // Initialize logarythmic bin sizes
  cfunc->dr_l1D=(take_log10(cfunc->r_max_1D)-cfunc->lr_min_l1D)/(double)cfunc->n_1D;

  // Initialize arrays
  cfunc->CFUNC_l1D =(double **)SID_malloc(sizeof(double *)*4);
  cfunc->dCFUNC_l1D=(double **)SID_malloc(sizeof(double *)*4);
  cfunc->COVMTX_l1D=(double **)SID_malloc(sizeof(double *)*4);
  cfunc->CFUNC_1D  =(double **)SID_malloc(sizeof(double *)*4);
  cfunc->dCFUNC_1D =(double **)SID_malloc(sizeof(double *)*4);
  cfunc->COVMTX_1D =(double **)SID_malloc(sizeof(double *)*4);
  cfunc->CFUNC_2D  =(double **)SID_malloc(sizeof(double *)*4);
  cfunc->dCFUNC_2D =(double **)SID_malloc(sizeof(double *)*4);
  cfunc->COVMTX_2D =(double **)SID_malloc(sizeof(double *)*4);
  int i_run;
  for(i_run=0;i_run<4;i_run++){
     cfunc->CFUNC_l1D[i_run] =(double *)SID_malloc(sizeof(double)*(cfunc->n_1D));
     cfunc->dCFUNC_l1D[i_run]=(double *)SID_malloc(sizeof(double)*(cfunc->n_1D));
     cfunc->COVMTX_l1D[i_run]=(double *)SID_malloc(sizeof(double)*(cfunc->n_1D*cfunc->n_1D));
     cfunc->CFUNC_1D[i_run]  =(double *)SID_malloc(sizeof(double)*(cfunc->n_1D));
     cfunc->dCFUNC_1D[i_run] =(double *)SID_malloc(sizeof(double)*(cfunc->n_1D));
     cfunc->COVMTX_1D[i_run] =(double *)SID_malloc(sizeof(double)*(cfunc->n_1D*cfunc->n_1D));
     cfunc->CFUNC_2D[i_run]  =(double *)SID_malloc(sizeof(double)*(cfunc->n_2D)*(cfunc->n_2D));
     cfunc->dCFUNC_2D[i_run] =(double *)SID_malloc(sizeof(double)*(cfunc->n_2D)*(cfunc->n_2D));
     cfunc->COVMTX_2D[i_run] =(double *)SID_malloc(sizeof(double)*(cfunc->n_2D_total*cfunc->n_2D_total));
  }

  cfunc->DD_l1D    =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->DR_l1D    =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->RR_l1D    =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->DD_1D     =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->DR_1D     =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->RR_1D     =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->DD_2D     =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->DR_2D     =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  cfunc->RR_2D     =(long long **)SID_malloc(sizeof(long long *)*(cfunc->n_jack_total+1));
  int i_jack;
  for(i_jack=0;i_jack<=cfunc->n_jack_total;i_jack++){
    cfunc->DD_l1D[i_jack]=(long long *)SID_calloc(sizeof(long long)*cfunc->n_1D);
    cfunc->DR_l1D[i_jack]=(long long *)SID_calloc(sizeof(long long)*cfunc->n_1D);
    cfunc->RR_l1D[i_jack]=(long long *)SID_calloc(sizeof(long long)*cfunc->n_1D);
    cfunc->DD_1D[i_jack] =(long long *)SID_calloc(sizeof(long long)*cfunc->n_1D);
    cfunc->DR_1D[i_jack] =(long long *)SID_calloc(sizeof(long long)*cfunc->n_1D);
    cfunc->RR_1D[i_jack] =(long long *)SID_calloc(sizeof(long long)*cfunc->n_1D);
    cfunc->DD_2D[i_jack] =(long long *)SID_calloc(sizeof(long long)*cfunc->n_2D*cfunc->n_2D);
    cfunc->DR_2D[i_jack] =(long long *)SID_calloc(sizeof(long long)*cfunc->n_2D*cfunc->n_2D);
    cfunc->RR_2D[i_jack] =(long long *)SID_calloc(sizeof(long long)*cfunc->n_2D*cfunc->n_2D);
  }

  // Initialize the cosmology
  if(filename_cosmology==NULL)
     init_cosmo_default(&(cfunc->cosmo));
  else
     read_gbpCosmo_file(&(cfunc->cosmo),filename_cosmology);

  SID_log("Done.",SID_LOG_CLOSE);
}
Пример #11
0
void read_MCMC_state(MCMC_info *MCMC){
  char      filename_output_dir[MAX_FILENAME_LENGTH];
  char      filename_chain_dir[MAX_FILENAME_LENGTH];
  char      filename_results_dir[MAX_FILENAME_LENGTH];
  char      filename_plots_dir[MAX_FILENAME_LENGTH];
  char      filename_run[MAX_FILENAME_LENGTH];
  char      filename_chain[MAX_FILENAME_LENGTH];
  char      filename_chain_config[MAX_FILENAME_LENGTH];
  char      filename_stats[MAX_FILENAME_LENGTH];
  char      filename_coverage[MAX_FILENAME_LENGTH];
  char      filename_chain_covariance[MAX_FILENAME_LENGTH];
  char      filename_covariance[MAX_FILENAME_LENGTH];
  char      filename_histograms[MAX_FILENAME_LENGTH];
  char      filename_results[MAX_FILENAME_LENGTH];
  char      filename_stop[MAX_FILENAME_LENGTH];
  char      format_string[32];
  int       my_chain;
  int       i_P,i_DS,i_M,i_array;
  double   *V_read;
  FILE     *fp_run;
  FILE     *fp_chain;
  FILE     *fp_chain_config;
  FILE     *fp_stats;
  FILE     *fp_coverage;
  FILE     *fp_chain_covariance;
  FILE     *fp_covariance;
  FILE     *fp_histograms;
  FILE     *fp_results;
  FILE     *fp_stop;
  MCMC_DS_info *current_DS;

  set_MCMC_mode(MCMC,MCMC_MODE_DEFAULT);
  my_chain=MCMC->my_chain;

    SID_log("Reading MCMC state from {%s}...",SID_LOG_OPEN,MCMC->filename_output_dir);

    // Set directories
    sprintf(filename_output_dir, "%s/",        MCMC->filename_output_dir);
    sprintf(filename_chain_dir,  "%s/chains/", MCMC->filename_output_dir);
    sprintf(filename_results_dir,"%s/results/",MCMC->filename_output_dir);
    sprintf(filename_plots_dir,  "%s/plots/",  MCMC->filename_output_dir);
    // Set filenames
    sprintf(filename_run,             "%s/run.dat",                  MCMC->filename_output_dir);
    sprintf(filename_chain,           "%s/chain_trace_%06d.dat",     filename_chain_dir,my_chain);
    sprintf(filename_chain_config,    "%s/chain_config_%06d.dat",    filename_chain_dir,my_chain);
    sprintf(filename_chain_covariance,"%s/chain_covariance_%06d.dat",filename_chain_dir,my_chain);
    sprintf(filename_stats,           "%s/chain_stats_%06d.dat",     filename_chain_dir,my_chain);
    sprintf(filename_coverage,        "%s/coverage.dat",             filename_results_dir);
    sprintf(filename_histograms,      "%s/histograms.dat",           filename_results_dir);
    sprintf(filename_covariance,      "%s/covariance.dat",           filename_results_dir);

    MCMC->map_P_to_M                =NULL;
    MCMC->compute_MCMC_ln_likelihood=compute_MCMC_ln_likelihood_default;
    MCMC->params                    =NULL;
    MCMC->temperature               =1.0;
    MCMC->n_P                       =0;
    MCMC->n_thin                    =1;
    MCMC->n_DS                      =0;
    MCMC->n_M_total                 =0;
    MCMC->n_arrays                  =0;
    MCMC->n_M                       =NULL;
    MCMC->array                     =NULL;
    MCMC->V                         =NULL;
    MCMC->m                         =NULL;
    MCMC->b                         =NULL;
    MCMC->RNG                       =NULL;
    MCMC->flag_integrate_on         =TRUE;
    MCMC->flag_analysis_on          =TRUE;
    MCMC->first_map_call            =TRUE;
    MCMC->mode                      =MCMC_MODE_DEFAULT;
    MCMC->DS                        =NULL;
    MCMC->last                      =NULL;

    // Read/Write Header file
    if((fp_run=fopen(filename_run,"rb"))!=NULL){
      fp_run=fopen(filename_run,"rb");
      fread(MCMC->problem_name,        sizeof(char),MCMC_NAME_SIZE,fp_run);
      fread(&(MCMC->n_chains),         sizeof(int),              1,fp_run);
      fread(&(MCMC->n_avg),            sizeof(int),              1,fp_run);
      fread(&(MCMC->flag_autocor_on),  sizeof(int),              1,fp_run);
      fread(&(MCMC->flag_no_map_write),sizeof(int),              1,fp_run);
      fread(&(MCMC->n_P),              sizeof(int),              1,fp_run);
      SID_log("Problem name    ={%s}",SID_LOG_COMMENT,MCMC->problem_name);
      SID_log("n_avg           ={%d}",SID_LOG_COMMENT,MCMC->n_avg);
      SID_log("flag_autocor_on ={%d}",SID_LOG_COMMENT,MCMC->flag_autocor_on);
      MCMC->P_names    =(char  **)SID_malloc(sizeof(char *)*MCMC->n_P);
      MCMC->P_init     =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
      MCMC->P_new        =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
      MCMC->P_last       =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
      MCMC->P_chain      =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
      MCMC->P_limit_min  =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
      MCMC->P_limit_max  =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
      for(i_P=0;i_P<MCMC->n_P;i_P++)
        MCMC->P_limit_min[i_P]=-DBL_MAX*1e-3;
      for(i_P=0;i_P<MCMC->n_P;i_P++)
        MCMC->P_limit_max[i_P]=DBL_MAX*1e-3;

      SID_log("Parameters (name, initial_value,limit min,limit max):",SID_LOG_OPEN);
      MCMC->P_name_length=0;
      for(i_P=0;i_P<MCMC->n_P;i_P++){
        MCMC->P_names[i_P]=(char *)SID_malloc(sizeof(char)*MCMC_NAME_SIZE);
        fread(MCMC->P_names[i_P],       sizeof(char),  MCMC_NAME_SIZE,fp_run);
        fread(&(MCMC->P_init[i_P]),     sizeof(double),             1,fp_run);
        fread(&(MCMC->P_limit_min[i_P]),sizeof(double),             1,fp_run);
        fread(&(MCMC->P_limit_max[i_P]),sizeof(double),             1,fp_run);
        MCMC->P_name_length=MAX(MCMC->P_name_length,strlen(MCMC->P_names[i_P]));
      }
      sprintf(MCMC->P_name_format,"%%-%ds",            MCMC->P_name_length);
      sprintf(format_string,      "%s %%13.6le %%13.6le %%13.6le",MCMC->P_name_format);
      for(i_P=0;i_P<MCMC->n_P;i_P++)
        SID_log(format_string,SID_LOG_COMMENT,MCMC->P_names[i_P],MCMC->P_init[i_P],MCMC->P_limit_min[i_P],MCMC->P_limit_max[i_P]);
      SID_log(NULL,SID_LOG_CLOSE|SID_LOG_NOPRINT);
      fread(&(MCMC->n_arrays),sizeof(int),1,fp_run);
      SID_log("n_arrays=%d",  SID_LOG_OPEN,MCMC->n_arrays);
      MCMC->array     =(double **)SID_malloc(sizeof(double *)*MCMC->n_arrays);
      MCMC->array_name=(char   **)SID_malloc(sizeof(char   *)*MCMC->n_arrays);
      for(i_array=0;i_array<MCMC->n_arrays;i_array++){
        MCMC->array[i_array]     =(double *)SID_malloc(sizeof(double)*MCMC->n_P);
        MCMC->array_name[i_array]=(char *)SID_malloc(sizeof(char)*MCMC_NAME_SIZE);
        fread(MCMC->array_name[i_array],sizeof(char),  MCMC_NAME_SIZE,fp_run);
        fread(MCMC->array[i_array],     sizeof(double),MCMC->n_P,     fp_run);
        SID_log("array #%03d name ={%s}",SID_LOG_COMMENT,i_array,MCMC->array_name[i_array]);
      }
      SID_log(NULL,SID_LOG_CLOSE|SID_LOG_NOPRINT);
      fread(&(MCMC->n_DS),sizeof(int),1,fp_run);
      SID_log("Reading %d datasets...",SID_LOG_OPEN,MCMC->n_DS);
      for(i_DS=0;i_DS<MCMC->n_DS;i_DS++){
        SID_log("Dataset #%03d:",SID_LOG_OPEN,i_DS);
        current_DS           =(MCMC_DS_info *)SID_malloc(sizeof(MCMC_DS_info));
        fread(current_DS->name,  sizeof(char),MCMC_NAME_SIZE,fp_run);
        fread(&(current_DS->n_M),sizeof(int),              1,fp_run);
        MCMC->n_M_total+=current_DS->n_M;
        SID_log("name    ={%s}",SID_LOG_COMMENT,current_DS->name);
        SID_log("n_M     =%d",  SID_LOG_COMMENT,current_DS->n_M);
        current_DS->M_target =(double *)SID_malloc(sizeof(double)*current_DS->n_M);
        current_DS->dM_target=(double *)SID_malloc(sizeof(double)*current_DS->n_M);
        current_DS->params   =NULL;
        fread(current_DS->M_target,   sizeof(double),current_DS->n_M,fp_run);
        fread(current_DS->dM_target,  sizeof(double),current_DS->n_M,fp_run);
        fread(&(current_DS->n_arrays),sizeof(int),                 1,fp_run);
        SID_log("n_arrays=%d",  SID_LOG_OPEN,current_DS->n_arrays);
        current_DS->array     =(double **)SID_malloc(sizeof(double *)*current_DS->n_arrays);
        current_DS->array_name=(char   **)SID_malloc(sizeof(char   *)*current_DS->n_arrays);
        for(i_array=0;i_array<current_DS->n_arrays;i_array++){
          current_DS->array[i_array]     =(double *)SID_malloc(sizeof(double)*current_DS->n_M);
          current_DS->array_name[i_array]=(char *)SID_malloc(sizeof(char)*MCMC_NAME_SIZE);
          fread(current_DS->array_name[i_array],sizeof(char),  MCMC_NAME_SIZE, fp_run);
          fread(current_DS->array[i_array],     sizeof(double),current_DS->n_M,fp_run);
          SID_log("array #%03d name={%s}",SID_LOG_COMMENT,i_array,current_DS->array_name[i_array]);
        }
        SID_log(NULL,SID_LOG_CLOSE|SID_LOG_NOPRINT);
        current_DS->next=NULL;
        if(MCMC->DS==NULL)
          MCMC->DS=current_DS;
        else
          MCMC->last->next=current_DS;
        MCMC->last=current_DS;
        SID_log(NULL,SID_LOG_CLOSE|SID_LOG_NOPRINT);
      }
      SID_log("Done.",SID_LOG_CLOSE);
      fclose(fp_run);
    }

    // ... fetch the number of intervals that have already been computed ...
    fp_chain_config=fopen(filename_chain_config,"rb");
    V_read=(double *)SID_malloc(sizeof(double)*MCMC->n_P*MCMC->n_P);
    fread(&(MCMC->n_iterations),     sizeof(int),   1,      fp_chain_config);
    fread(&(MCMC->n_iterations_burn),sizeof(int),   1,      fp_chain_config);

    // ... fetch the temperature and covariance matrix that was being used
    fread(&(MCMC->temperature),      sizeof(double),1,                  fp_chain_config);
    fread(V_read,                    sizeof(double),MCMC->n_P*MCMC->n_P,fp_chain_config);
    set_MCMC_covariance(MCMC,V_read);
    SID_free(SID_FARG V_read);

    // Initialize dataset arrays
    init_MCMC_arrays(MCMC);

    SID_log("# burn  iterations = %d", SID_LOG_COMMENT,MCMC->n_iterations_burn);
    SID_log("# total iterations = %d", SID_LOG_COMMENT,MCMC->n_iterations);
    SID_log("Temperature        = %le",SID_LOG_COMMENT,MCMC->temperature);
    fclose(fp_chain_config);

    SID_log("Done.",SID_LOG_CLOSE);
}
Пример #12
0
void init_field(int n_d, int *n, double *L, field_info *FFT) {
    ptrdiff_t  n_x_local;
    ptrdiff_t  i_x_start_local;
    ptrdiff_t  n_y_transpose_local;
    ptrdiff_t  i_y_start_transpose_local;
    ptrdiff_t *n_x_rank;

    int  flag_active;
    int  n_active;
    int  min_size, max_size;

    SID_log("Initializing ", SID_LOG_OPEN);
    for(ptrdiff_t i_d = 0; i_d < n_d; i_d++) {
        if(i_d < (n_d - 1))
            SID_log("%dx", SID_LOG_CONTINUE, n[i_d]);
        else
            SID_log("%d element %d-d FFT ", SID_LOG_CONTINUE, n[i_d], n_d);
    }
    SID_log("(%d byte precision)...", SID_LOG_CONTINUE, (int)sizeof(GBPREAL));

    // Initialize FFT sizes
    FFT->n_d             = n_d;
    FFT->n               = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    FFT->L               = (double *)SID_calloc(sizeof(double) * FFT->n_d);
    FFT->n_k_local       = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    FFT->n_R_local       = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    FFT->i_R_start_local = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    FFT->i_k_start_local = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    FFT->i_R_stop_local  = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    FFT->i_k_stop_local  = (ptrdiff_t *)SID_calloc(sizeof(ptrdiff_t) * FFT->n_d);
    for(ptrdiff_t i_d = 0; i_d < FFT->n_d; i_d++) {
        FFT->n[i_d]               = n[i_d];
        FFT->L[i_d]               = L[i_d];
        FFT->i_R_start_local[i_d] = 0;
        FFT->i_k_start_local[i_d] = 0;
        FFT->n_R_local[i_d]       = FFT->n[i_d];
        FFT->n_k_local[i_d]       = FFT->n[i_d];
    }
    FFT->n_k_local[FFT->n_d - 1] = FFT->n[FFT->n_d - 1] / 2 + 1;

    // Initialize FFTW

    // Create an integer version of FFT->n[] to pass to ..._create_plan
    int *n_int=(int *)SID_malloc(sizeof(int)*FFT->n_d);
    for(int i_d=0;i_d<FFT->n_d;i_d++)
        n_int[i_d]=(int)FFT->n[i_d];
#if FFTW_V2
#if USE_MPI
    int total_local_size_int;
    int n_x_local_int;
    int i_x_start_local_int;
    int n_y_transpose_local_int;
    int i_y_start_transpose_local_int;
    FFT->plan  = rfftwnd_mpi_create_plan(SID.COMM_WORLD->comm, FFT->n_d, n_int, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE);
    FFT->iplan = rfftwnd_mpi_create_plan(SID.COMM_WORLD->comm, FFT->n_d, n_int, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE);
    rfftwnd_mpi_local_sizes(FFT->plan,
                            &(n_x_local_int),
                            &(i_x_start_local_int),
                            &(n_y_transpose_local_int),
                            &(i_y_start_transpose_local_int),
                            &total_local_size_int);
    n_x_local =  (ptrdiff_t)n_x_local_int;
    i_x_start_local = (ptrdiff_t)i_x_start_local_int;
    n_y_transpose_local = (ptrdiff_t)n_y_transpose_local_int;
    i_y_start_transpose_local = (ptrdiff_t)i_y_start_transpose_local_int;
    FFT->total_local_size = (size_t)total_local_size_int;
#else
    FFT->total_local_size = 1;
    for(ptrdiff_t i_d = 0; i_d < FFT->n_d; i_d++) {
        if(i_d < FFT->n_d - 1)
            FFT->total_local_size *= FFT->n[i_d];
        else
            FFT->total_local_size *= 2 * (FFT->n[i_d] / 2 + 1);
    }
#if USE_DOUBLE
    FFT->plan  = fftwnd_create_plan(FFT->n_d, n_int, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE | FFTW_IN_PLACE);
    FFT->iplan = fftwnd_create_plan(FFT->n_d, n_int, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE | FFTW_IN_PLACE);
#else
    FFT->plan  = rfftwnd_create_plan(FFT->n_d, n_int, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE | FFTW_IN_PLACE);
    FFT->iplan = rfftwnd_create_plan(FFT->n_d, n_int, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE | FFTW_IN_PLACE);
#endif
#endif
#else
#if USE_MPI
#if USE_DOUBLE
    fftw_mpi_init();
    FFT->total_local_size = fftw_mpi_local_size_many_transposed(FFT->n_d,
                                                                FFT->n,
                                                                1,
                                                                FFTW_MPI_DEFAULT_BLOCK,
                                                                FFTW_MPI_DEFAULT_BLOCK,
                                                                SID_COMM_WORLD->comm,
                                                                &(n_x_local),
                                                                &(i_x_start_local),
                                                                &(n_y_transpose_local),
                                                                &(i_y_start_transpose_local));
    FFT->plan  = fftw_mpi_plan_dft_r2c(FFT->n_d, FFT->n, FFT->field_local, FFT->cfield_local, SID_COMM_WORLD->comm, FFTW_ESTIMATE);
    FFT->iplan = fftw_mpi_plan_dft_c2r(FFT->n_d, FFT->n, FFT->cfield_local, FFT->field_local, SID_COMM_WORLD->comm, FFTW_ESTIMATE);
#else
    fftwf_mpi_init();
    FFT->total_local_size   = fftwf_mpi_local_size_many_transposed(FFT->n_d,
                                                                 FFT->n,
                                                                 1,
                                                                 FFTW_MPI_DEFAULT_BLOCK,
                                                                 FFTW_MPI_DEFAULT_BLOCK,
                                                                 SID_COMM_WORLD->comm,
                                                                 &(n_x_local),
                                                                 &(i_x_start_local),
                                                                 &(n_y_transpose_local),
                                                                 &(i_y_start_transpose_local));
    FFT->plan  = fftwf_mpi_plan_dft_r2c(FFT->n_d, FFT->n, FFT->field_local, FFT->cfield_local, SID_COMM_WORLD->comm, FFTW_ESTIMATE);
    FFT->iplan = fftwf_mpi_plan_dft_c2r(FFT->n_d, FFT->n, FFT->cfield_local, FFT->field_local, SID_COMM_WORLD->comm, FFTW_ESTIMATE);
#endif
#else
    FFT->total_local_size = 1;
    for(ptrdiff_t i_d=0; i_d < FFT->n_d; i_d++) {
        if(i_d < FFT->n_d - 1)
            FFT->total_local_size *= FFT->n[i_d];
        else
            FFT->total_local_size *= 2 * (FFT->n[i_d] / 2 + 1);
    }
#if USE_DOUBLE
    FFT->plan  = fftw_plan_dft_r2c(FFT->n_d, FFT->n, FFT->field_local, FFT->cfield_local, FFTW_ESTIMATE);
    FFT->iplan = fftw_plan_dft_c2r(FFT->n_d, FFT->n, FFT->cfield_local, FFT->field_local, FFTW_ESTIMATE);
#else
    FFT->plan  = fftwf_plan_dft_r2c(FFT->n_d, FFT->n, FFT->field_local, FFT->cfield_local, FFTW_ESTIMATE);
    FFT->iplan = fftwf_plan_dft_c2r(FFT->n_d, FFT->n, FFT->cfield_local, FFT->field_local, FFTW_ESTIMATE);
#endif
#endif
#endif

    SID_free(SID_FARG n_int);


    // Set empty slabs to start at 0 to make ignoring them simple.
    if(n_x_local == 0)
        i_x_start_local = 0;
    if(n_y_transpose_local == 0)
        i_y_start_transpose_local = 0;

    // Modify the local slab dimensions according to what FFTW chose.
    FFT->i_R_start_local[0] = i_x_start_local;
    FFT->n_R_local[0]       = n_x_local;
    if(FFT->n_d > 1) {
        FFT->i_k_start_local[1] = i_y_start_transpose_local;
        FFT->n_k_local[1]       = n_y_transpose_local;
    }

    // Allocate field
#if USE_FFTW3
    FFT->field_local  = (gbpFFT_real    *)fftwf_alloc_real(FFT->total_local_size);
#else
    FFT->field_local  = (gbpFFT_real    *)SID_malloc(sizeof(gbpFFT_real)*FFT->total_local_size);
#endif
    FFT->cfield_local = (gbpFFT_complex *)FFT->field_local;

    // Upper limits of slab decomposition
    for(ptrdiff_t i_d = 0; i_d < FFT->n_d; i_d++) {
        FFT->i_R_stop_local[i_d] = FFT->i_R_start_local[i_d] + FFT->n_R_local[i_d] - 1;
        FFT->i_k_stop_local[i_d] = FFT->i_k_start_local[i_d] + FFT->n_k_local[i_d] - 1;
    }

    // FFTW padding sizes
    if(FFT->n_d > 1) {
        FFT->pad_size_R = 2 * (FFT->n_R_local[FFT->n_d - 1] / 2 + 1) - FFT->n_R_local[FFT->n_d - 1];
        FFT->pad_size_k = 0;
    } else {
        FFT->pad_size_R = 0;
        FFT->pad_size_k = 0;
    }

    // Number of elements (global and local) in the FFT
    ptrdiff_t i_d = 0;
    for(FFT->n_field = 1, FFT->n_field_R_local = 1, FFT->n_field_k_local = 1; i_d < FFT->n_d; i_d++) {
        FFT->n_field *= (size_t)FFT->n[i_d];
        FFT->n_field_R_local *= (size_t)FFT->n_R_local[i_d];
        FFT->n_field_k_local *= (size_t)FFT->n_k_local[i_d];
    }

    // Clear the field
    clear_field(FFT);

    // Initialize the FFT's real-space grid
    FFT->R_field = (double **)SID_malloc(sizeof(double *) * FFT->n_d);
    FFT->dR      = (double *)SID_malloc(sizeof(double *) * FFT->n_d);
    for(ptrdiff_t i_d = 0; i_d < FFT->n_d; i_d++) {
        FFT->R_field[i_d] = (double *)SID_malloc(sizeof(double) * (FFT->n[i_d] + 1));
        FFT->dR[i_d]      = FFT->L[i_d] / (double)(FFT->n[i_d]);
        for(ptrdiff_t i_i = 0; i_i < FFT->n[i_d]; i_i++)
            FFT->R_field[i_d][i_i] = FFT->L[i_d] * ((double)i_i / (double)(FFT->n[i_d]));
        FFT->R_field[i_d][FFT->n[i_d]] = FFT->L[i_d];
    }

    // Initialize the FFT's k-space grid
    FFT->k_field   = (double **)SID_malloc(sizeof(double *) * FFT->n_d);
    FFT->dk        = (double *)SID_malloc(sizeof(double *) * FFT->n_d);
    FFT->k_Nyquist = (double *)SID_malloc(sizeof(double *) * FFT->n_d);
    for(ptrdiff_t i_d = 0; i_d < FFT->n_d; i_d++) {
        FFT->k_field[i_d]   = (double *)SID_malloc(sizeof(double) * FFT->n[i_d]);
        FFT->dk[i_d]        = TWO_PI / FFT->L[i_d];
        FFT->k_Nyquist[i_d] = TWO_PI * (double)(FFT->n[i_d]) / FFT->L[i_d] / 2.;
        for(ptrdiff_t i_i = 0; i_i < FFT->n[i_d]; i_i++) {
            if(i_i >= FFT->n[i_d] / 2)
                FFT->k_field[i_d][i_i] = TWO_PI * (double)(i_i - FFT->n[i_d]) / FFT->L[i_d];
            else
                FFT->k_field[i_d][i_i] = TWO_PI * (double)(i_i) / FFT->L[i_d];
        }
    }

    // Flags
    FFT->flag_padded = GBP_FALSE;

    // Slab info
    FFT->slab.n_x_local       = FFT->n_R_local[0];
    FFT->slab.i_x_start_local = FFT->i_R_start_local[0];
    FFT->slab.i_x_stop_local  = FFT->i_R_stop_local[0];
    FFT->slab.x_min_local     = FFT->R_field[0][FFT->i_R_start_local[0]];
    if(FFT->slab.n_x_local > 0)
        FFT->slab.x_max_local = FFT->R_field[0][FFT->i_R_stop_local[0] + 1];
    else
        FFT->slab.x_max_local = FFT->slab.x_min_local;
    SID_Allreduce(&(FFT->slab.x_max_local), &(FFT->slab.x_max), 1, SID_DOUBLE, SID_MAX, SID_COMM_WORLD);

#if USE_MPI
    // All ranks are not necessarily assigned any slices, so
    //   we need to figure out what ranks are to the right and the left for
    //   buffer exchanges
    n_x_rank              = (ptrdiff_t *)SID_malloc(sizeof(ptrdiff_t) * SID.n_proc);
    n_x_rank[SID.My_rank] = (ptrdiff_t)FFT->slab.n_x_local;
    if(n_x_rank[SID.My_rank] > 0)
        flag_active = GBP_TRUE;
    else
        flag_active = GBP_FALSE;
    SID_Allreduce(&flag_active, &n_active, 1, SID_INT, SID_SUM, SID_COMM_WORLD);
    SID_Allreduce(&n_x_rank[SID.My_rank], &min_size, 1, SID_INT, SID_MIN, SID_COMM_WORLD);
    SID_Allreduce(&n_x_rank[SID.My_rank], &max_size, 1, SID_INT, SID_MAX, SID_COMM_WORLD);
    for(int i_rank = 0; i_rank < SID.n_proc; i_rank++)
        SID_Bcast(&(n_x_rank[i_rank]), 1, SID_INT, i_rank, SID_COMM_WORLD);
    FFT->slab.rank_to_right = -1;
    for(int i_rank = SID.My_rank + 1; i_rank < SID.My_rank + SID.n_proc && FFT->slab.rank_to_right < 0; i_rank++) {
        int j_rank = i_rank % SID.n_proc;
        if(n_x_rank[j_rank] > 0)
            FFT->slab.rank_to_right = j_rank;
    }
    if(FFT->slab.rank_to_right < 0)
        FFT->slab.rank_to_right = SID.My_rank;
    FFT->slab.rank_to_left = -1;
    for(int i_rank = SID.My_rank - 1; i_rank > SID.My_rank - SID.n_proc && FFT->slab.rank_to_left < 0; i_rank--) {
        int j_rank = i_rank;
        if(i_rank < 0)
            j_rank = i_rank + SID.n_proc;
        if(n_x_rank[j_rank] > 0)
            FFT->slab.rank_to_left = j_rank;
    }
    if(FFT->slab.rank_to_left < 0)
        FFT->slab.rank_to_left = SID.My_rank;
    free(n_x_rank);
    SID_log("(%d cores unused, min/max slab size=%d/%d)...", SID_LOG_CONTINUE, SID.n_proc - n_active, min_size, max_size);
#else
    FFT->slab.rank_to_right = SID.My_rank;
    FFT->slab.rank_to_left  = SID.My_rank;
    if(FFT->slab.n_x_local > 0) {
        flag_active = GBP_TRUE;
        n_active    = 1;
        min_size    = FFT->slab.n_x_local;
        max_size    = FFT->slab.n_x_local;
    } else {
        flag_active = GBP_FALSE;
        n_active    = 0;
        min_size    = 0;
        max_size    = 0;
    }
#endif

    SID_log("Done.", SID_LOG_CLOSE);
}
Пример #13
0
size_t mark_particles(plist_info *plist, int run_mode, double *input_vals, const char *mark_name) {
    size_t   n_particles;
    size_t   n_particles_local;
    size_t   i_particle;
    int      i_species;
    size_t * id;
    GBPREAL *x;
    GBPREAL *y;
    GBPREAL *z;
    GBPREAL  r;
    int *    mark_array;
    int      flag_volume;
    int      flag_volume_sphere;
    size_t   n_marked_local = 0;
    size_t   n_marked       = 0;

    // Interpret run-mode
    flag_volume        =
            SID_CHECK_BITFIELD_SWITCH(run_mode, VOLUME_BOX) || SID_CHECK_BITFIELD_SWITCH(run_mode, VOLUME_SPHERE);
    flag_volume_sphere = SID_CHECK_BITFIELD_SWITCH(run_mode, VOLUME_SPHERE);

    // Loop over all species
    for(i_species = 0; i_species < N_GADGET_TYPE; i_species++) {
        if(ADaPS_exist(plist->data, "n_all_%s", plist->species[i_species])) {
            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];
            // If this species has local particles
            if(n_particles_local > 0) {
                mark_array = (int *)SID_malloc(sizeof(int) * n_particles_local);
                // Mark particles in a volume
                if(flag_volume) {
                    x = (GBPREAL *)ADaPS_fetch(plist->data, "x_%s", plist->species[i_species]);
                    y = (GBPREAL *)ADaPS_fetch(plist->data, "y_%s", plist->species[i_species]);
                    z = (GBPREAL *)ADaPS_fetch(plist->data, "z_%s", plist->species[i_species]);
                    // Loop over all particles
                    for(i_particle = 0; i_particle < n_particles_local; i_particle++) {
                        mark_array[i_particle] = GBP_FALSE;
                        switch(flag_volume_sphere) {
                            case GBP_TRUE:
                                if(add_quad(3,
                                            (double)(x[i_particle]) - input_vals[0],
                                            (double)(y[i_particle]) - input_vals[1],
                                            (double)(z[i_particle]) - input_vals[2]) <= input_vals[3])
                                    mark_array[i_particle] = GBP_TRUE;
                                break;
                            case GBP_FALSE:
                                if(x[i_particle] >= (GBPREAL)input_vals[0] && x[i_particle] <= (GBPREAL)input_vals[1]) {
                                    if(y[i_particle] >= (GBPREAL)input_vals[2] && y[i_particle] <= (GBPREAL)input_vals[3]) {
                                        if(z[i_particle] >= (GBPREAL)input_vals[4] && z[i_particle] <= (GBPREAL)input_vals[5]) {
                                            mark_array[i_particle] = GBP_TRUE;
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
                // Mark particles by property
                else {
                }
                for(i_particle = 0; i_particle < n_particles_local; i_particle++)
                    if(mark_array[i_particle])
                        n_marked_local++;
                ADaPS_store(&(plist->data), (void *)mark_array, "%s_%s", ADaPS_DEFAULT, mark_name, plist->species[i_species]);
            }
        }
    }
    calc_sum_global(&n_marked_local, &n_marked, 1, SID_SIZE_T, CALC_MODE_DEFAULT, SID_COMM_WORLD);
    return (n_marked);
}
Пример #14
0
void compute_trees_horizontal(char        *filename_halo_root_in,
                              char        *filename_cat_root_in,
                              char        *filename_snap_list_in,
                              char        *filename_root_matches,
                              char        *filename_output_dir,
                              cosmo_info **cosmo,
                              int          i_read_start,
                              int          i_read_stop,
                              int          i_read_step,
                              int          n_search,
                              int          flag_fix_bridges,
                              int         *flag_clean){
  char        group_text_prefix[5];
  FILE       *fp;
  char       *line=NULL;
  int         line_length=0;
  int         n_strays;
  int         n_strays_drop;
  int         n_strays_bridge;
  int         i_stray;
  int         n_match;
  int         n_match_halos;
  int         n_back_match;
  int         i_match;
  int         j_match;
  int         k_match;
  int         n_groups_1;
  int         n_groups_2;
  int         n_groups_3;
  int         i_group;
  int         j_group;
  int         k_group;
  int         l_group;
  int         n_subgroups_1;
  int         n_subgroups_2;
  int         i_subgroup;
  int         j_subgroup;
  int         i_drop;
  int         j_drop;
  int         k_drop;
  int         i_bridge;
  int         j_bridge;
  int         n_lines;
  int         i_file;
  int         j_file;
  int         i_write;
  int         j_write;
  int         l_write;
  int         l_read;
  int         j_file_1;
  int         j_file_2;
  int         i_read;
  int         j_read;
  int         j_read_1;
  int         j_read_2;
  int         n_descendant;
  int         n_progenitor;
  int         descendant_index;
  int         progenitor_index;
  int         my_descendant_index,my_descendant_id,my_descendant_list,my_index;
  int         index;
  int         max_id         =0;
  int         max_id_group   =0;
  int         max_id_subgroup=0;
  int        *my_descendant;
  int        *n_particles;
  int        *n_particles_groups;
  int        *n_particles_subgroups;
  int         my_trunk;
  double      expansion_factor;
  int         n_found;
  int         n_found_bridge;
  double      delta_r;
  double      delta_M;
  double      R_vir_p;
  double      R_vir_d;
  int         i_find,n_find;
  int         flag_continue;
  int         flag_drop;
  int        *match_id=NULL;
  int        *search_id=NULL;
  int         n_progenitors_max;
  int         i_search;
  int         flag_dropped;
  int         flag_first;
  int         n_particles_max;
  int         trunk_index;
  int        *n_groups=NULL;
  int        *n_subgroups=NULL;
  int         max_tree_id_group;
  int         max_tree_id_subgroup;
  int         max_tree_id;
  int       **n_subgroups_group=NULL;
  int        *n_subgroups_group_1=NULL;
  size_t    **sort_id=NULL;
  size_t    **sort_group_id=NULL;
  size_t    **sort_subgroup_id=NULL;
  size_t     *match_index=NULL;
  size_t     *bridge_index=NULL;
  size_t     *search_index=NULL;
  float      *match_score=NULL;
  char       *match_flag_two_way=NULL;
  int        *bridge_keep=NULL;
  int         flag_match_subgroups;
  int         flag_keep_strays=FALSE;
  int         n_k_match=2;
  int         n_snap;
  
  tree_horizontal_info **subgroups;
  tree_horizontal_info **groups;
  tree_horizontal_info **halos;
  tree_horizontal_info  *halos_i;
  match_info           **back_matches_groups;
  match_info           **back_matches_subgroups;
  match_info           **back_matches;

  int     n_files;
  int     n_subgroups_max;
  int     n_groups_max;
  int    *n_halos;
  int     n_halos_max;
  int     n_halos_i;
  int     i_halo;
  int     n_halos_1_matches;
  int     n_halos_2_matches;
  int     j_halo;
  int     k_halo;
  int     l_halo;

  int     n_list;
  int     k_file;
  int     l_file;
  int     k_index;
  int     k_file_temp;
  int     k_index_temp;

  int     n_wrap;
  int     i_file_start;
  
  char  filename_output_dir_horizontal[MAX_FILENAME_LENGTH];
  char  filename_output_dir_horizontal_cases[MAX_FILENAME_LENGTH];
  char  filename_output_file_root[MAX_FILENAME_LENGTH];
  char  filename_matching_out[MAX_FILENAME_LENGTH];
  FILE *fp_matching_out;
  int   i_column;

  SID_log("Constructing horizontal merger trees for snapshots #%d->#%d (step=%d)...",SID_LOG_OPEN|SID_LOG_TIMER,i_read_start,i_read_stop,i_read_step);

  if(n_search<1)
    SID_trap_error("n_search=%d but must be at least 1",ERROR_LOGIC,n_search);

  int flag_compute_fragmented=TRUE;
  int flag_compute_ghosts    =FALSE;

  if(!flag_fix_bridges)
    SID_log("Bridge-fixing is turned off.",SID_LOG_COMMENT);
  if(!flag_compute_fragmented)
    SID_log("Fragmented-halo propagation is turned off.",SID_LOG_COMMENT);
  if(!flag_compute_ghosts)
    SID_log("Ghost-populated tree construction is turned off.",SID_LOG_COMMENT);

  // Create the output directory
  mkdir(filename_output_dir,02755);

  // Create snapshot expansion factor list
  double *a_list=NULL;
  int     n_a_list_in;
  write_a_list(filename_snap_list_in,
               filename_output_dir,
               i_read_start,
               i_read_stop,
               i_read_step);
  read_a_list(filename_output_dir,
              &a_list,
              &n_a_list_in);

  write_tree_run_parameters(filename_output_dir,
                            i_read_start,
                            i_read_stop,
                            i_read_step,
                            n_search,
                            flag_fix_bridges,
                            flag_compute_fragmented,
                            flag_compute_ghosts);

  // Validate existing matching files &/or perfrom matching
  //if(!compute_trees_matches(filename_halo_root_in,
  //                          filename_root_matches,
  //                          i_read_start,
  //                          i_read_stop,
  //                          i_read_step,
  //                          n_search,
  //                          WRITE_MATCHES_MODE_TREES|WRITE_MATCHES_PERFORM_CHECK))
  //   SID_trap_error("Matching could not be completed.  Terminating.",ERROR_LOGIC);
  read_matches_header(filename_root_matches,
                      i_read_start,
                      i_read_stop,
                      i_read_step,
                      &n_files,
                      &n_subgroups,
                      &n_groups,
                      &n_subgroups_max,
                      &n_groups_max,
                      &n_halos_max);

  // We need these for allocating arrays
  calc_max(n_subgroups,&n_subgroups_max,n_files,SID_INT,CALC_MODE_DEFAULT);
  calc_max(n_groups,   &n_groups_max,   n_files,SID_INT,CALC_MODE_DEFAULT);
  n_halos_max=MAX(n_subgroups_max,n_groups_max);

  // We need enough indices to allow us to hold-on to descendants until outputing
  //   and for the current and last i_file as well
  n_wrap      =2*n_search+2;
  i_file_start=n_files-1;

  // Initialize arrays
  SID_log("Creating arrays...",SID_LOG_OPEN);
  n_particles_groups    =(int    *)SID_malloc(sizeof(int)   *n_halos_max);
  n_particles_subgroups =(int    *)SID_malloc(sizeof(int)   *n_halos_max);
  match_id              =(int    *)SID_malloc(sizeof(int)   *n_halos_max);
  match_score           =(float  *)SID_malloc(sizeof(float) *n_halos_max);
  match_index           =(size_t *)SID_malloc(sizeof(size_t)*n_halos_max);
  match_flag_two_way    =(char   *)SID_malloc(sizeof(char)  *n_halos_max);
  subgroups             =(tree_horizontal_info **)SID_malloc(sizeof(tree_horizontal_info *)*n_wrap);
  groups                =(tree_horizontal_info **)SID_malloc(sizeof(tree_horizontal_info *)*n_wrap);
  n_subgroups_group     =(int                  **)SID_malloc(sizeof(int                  *)*n_wrap);
  back_matches_subgroups=(match_info           **)SID_malloc(sizeof(match_info *)          *n_wrap);
  back_matches_groups   =(match_info           **)SID_malloc(sizeof(match_info *)          *n_wrap);
  for(i_search=0;i_search<n_wrap;i_search++){
     subgroups[i_search]             =(tree_horizontal_info *)SID_calloc(sizeof(tree_horizontal_info)*n_subgroups_max);
     groups[i_search]                =(tree_horizontal_info *)SID_calloc(sizeof(tree_horizontal_info)*n_groups_max);       
     n_subgroups_group[i_search]     =(int                  *)SID_calloc(sizeof(int)                 *n_groups_max);       
     back_matches_subgroups[i_search]=NULL;
     back_matches_groups[i_search]   =NULL;
  }
  SID_log("Done.",SID_LOG_CLOSE);

  // Process the first file separately
  //   (just give everything ids from a running index.  Also adds MMS flags.) ...
  init_trees_horizontal_roots(groups,
                              subgroups,
                              match_id,
                              match_score,
                              match_index,
                              match_flag_two_way,
                              n_particles_groups,
                              n_particles_subgroups,
                              n_subgroups_group,
                              n_groups_max,
                              n_subgroups_max,
                              filename_root_matches,
                              i_read_start,
                              i_read_stop,
                              i_read_step,
                              i_file_start,
                              n_wrap,
                              n_halos_max,
                              &max_id_group,
                              &max_tree_id_group,
                              &max_id_subgroup,
                              &max_tree_id_subgroup);

  // The first snapshot is done now (set to defaults as the roots of trees) ... now loop over all other snapshots ...
  //   There are a bunch of counters at work here.  Because we aren't necessarily using every 
  //     snapshot (if i_read_step>1), we need counters to keep track of which snapshots we
  //     are working with (i_read_*,j_read_*, etc), counters to keep track of which
  //     files's we're dealing with as far as the trees indices are concerned (i_file_*,j_file_*,etc), and
  //     counters to keep track of which files are being/have been written (i_write_*,j_write_* etc).
  //     We can't write files right away because previously processed snapshots can be changed
  //     when we deal with dropped and bridged halos.
  for(i_read   =i_read_stop-i_read_step,
        i_file =i_file_start-1, 
        j_file =1,             
        i_write=i_file_start,      
        j_write=i_read_stop,
        l_write=0;      
      i_read>=i_read_start;
      i_read-=i_read_step,    
         i_file--, 
         j_file++){
    SID_log("Processing snapshot #%d...",SID_LOG_OPEN|SID_LOG_TIMER,i_read);

    // Loop twice (1st to process subgroups, 2nd to process groups)
    for(k_match=0;k_match<n_k_match;k_match++){

       // Initialize a bunch of stuff which depends on whether
       //   we are processing groups or subgroups.
       // Do the groups first, so that we have access to n_subgroups_group,
       //   which we need for setting MOST_MASSIVE flags, etc
       switch(k_match){
          case 0:
          sprintf(group_text_prefix,"");
          flag_match_subgroups=MATCH_GROUPS;
          halos               =groups;
          back_matches        =back_matches_groups;
          n_halos             =n_groups;
          n_halos_max         =n_groups_max;
          max_id              =max_id_group;
          max_tree_id         =max_tree_id_group;
          n_particles         =n_particles_groups;
          break;
          case 1:
          sprintf(group_text_prefix,"sub");
          flag_match_subgroups=MATCH_SUBGROUPS;
          halos               =subgroups;
          back_matches        =back_matches_subgroups;
          n_halos             =n_subgroups;
          n_halos_max         =n_subgroups_max;
          max_id              =max_id_subgroup;
          max_tree_id         =max_tree_id_subgroup;
          n_particles         =n_particles_subgroups;
          break;
       }
       halos_i  =halos[i_file%n_wrap];
       n_halos_i=n_halos[j_file];

       SID_log("Processing %d %sgroups...",SID_LOG_OPEN|SID_LOG_TIMER,n_halos_i,group_text_prefix);

       // Initialize tree pointer-arrays with dummy values
       init_trees_horizontal_snapshot(halos_i,
                                      &(back_matches[i_file%n_wrap]),
                                      i_read,
                                      i_file,
                                      n_groups[j_file],
                                      n_groups_max,
                                      n_subgroups[j_file],
                                      n_subgroups_max,
                                      flag_match_subgroups);

       // Identify matches that will be used for progenitor building (and read halo sizes)
       if(flag_fix_bridges)
          identify_back_matches(halos,
                                halos_i,
                                &(back_matches[i_file%n_wrap]),
                                n_halos_i,
                                match_id,
                                match_score,
                                match_index,
                                match_flag_two_way,
                                n_particles,
                                i_file,
                                i_read,
                                i_read_start,
                                i_read_stop,
                                i_read_step,
                                n_search,
                                n_wrap,
                                n_halos_max,
                                n_files,
                                filename_root_matches,
                                flag_match_subgroups);

       // Perform forward-matching
       identify_progenitors(halos,
                            halos_i,
                            n_subgroups_group,
                            n_halos_i,
                            match_id,
                            match_score,
                            match_index,
                            match_flag_two_way,
                            n_particles,
                            i_file,
                            i_read,
                            i_read_start,
                            i_read_stop,
                            i_read_step,
                            n_search,
                            n_wrap,
                            n_halos_max,
                            n_files,
                            flag_fix_bridges,
                            &max_id,
                            &n_halos_1_matches,
                            &n_halos_2_matches,
                            filename_root_matches,
                            group_text_prefix,
                            flag_match_subgroups);

       // Add MOST_MASSIVE substructure flags
       if(flag_match_subgroups==MATCH_SUBGROUPS)
          add_substructure_info(subgroups[i_file%n_wrap],
                                n_subgroups_group[i_file%n_wrap],
                                n_particles_groups,
                                n_groups[j_file],
                                n_subgroups[j_file],
                                flag_match_subgroups);
      
       // Finalize matches to unprocessed halos.  In particular,
       //    resolve matches to bridged halos that were not matched
       //    to any emerged candidates.
       finalize_trees_horizontal(n_halos_1_matches,
                                 n_halos_i,
                                 halos,
                                 halos_i,
                                 i_file,
                                 n_search,
                                 n_wrap,
                                 &max_id,
                                 &max_tree_id);
 
       // Now that we know which halos are main progenitors, we
       //    can set the n_partices_largest_descendant values.
       set_largest_descendants(halos_i,n_halos_i);

       // Now that we know which halos are the main progenitors of this
       //    snapshot's bridged halos, we can mark any other back matches
       //    as candidate emerged halos and identify bridges.
       if(flag_fix_bridges)
          identify_bridges(halos_i,n_halos_i,n_search);

       // Report some statistics
       //   n.b.: This is only an estimate in some cases, since subsequent snapshots may alter this snapshot.  
       //         See the final written log.txt file for accurate numbers.
       write_trees_horizontal_report(n_halos_i,n_halos_max,halos_i);

       // Update the max_id variables
       switch(flag_match_subgroups){
          case MATCH_SUBGROUPS:
            max_id_subgroup=max_id;
            max_tree_id_subgroup=max_tree_id;
            break;
          case MATCH_GROUPS:
            max_id_group   =max_id;
            max_tree_id_group=max_tree_id;
            break;
       }
       SID_log("Done.",SID_LOG_CLOSE);
    } // k_match
 
    // Write trees once a few files have been processed
    //   and no more dropped groups etc. need to be given ids
    if(j_file>n_search){
       int mode_write;
       if(flag_compute_ghosts || flag_compute_fragmented)
          mode_write=TREE_HORIZONTAL_WRITE_EXTENDED|TREE_HORIZONTAL_WRITE_ALLCASES|TREE_HORIZONTAL_WRITE_CHECK_FRAGMENTED;
       else
          mode_write=TREE_HORIZONTAL_WRITE_ALLCASES|TREE_HORIZONTAL_WRITE_CHECK_FRAGMENTED;
       write_trees_horizontal((void **)groups, 
                              (void **)subgroups,
                              n_groups[l_write],   n_groups_max,   
                              n_subgroups[l_write],n_subgroups_max,
                              n_subgroups_group,
                              max_tree_id_subgroup,
                              max_tree_id_group,
                              i_write,
                              j_write,
                              l_write,
                              i_read_step,
                              n_search,
                              n_wrap,
                              i_file_start,
                              filename_cat_root_in,
                              filename_output_dir,
                              a_list,
                              cosmo,
                              n_k_match,
                              l_write==0,
                              mode_write);
       i_write--;
       l_write++;
       j_write-=i_read_step;
    }
    SID_log("Done.",SID_LOG_CLOSE);
  } // loop over snaps

  // Write the remaining snapshots
  for(;j_write>=i_read_start;i_write--,j_write-=i_read_step,l_write++){
     int mode_write;
     if(flag_compute_ghosts || flag_compute_fragmented)
        mode_write=TREE_HORIZONTAL_WRITE_EXTENDED|TREE_HORIZONTAL_WRITE_ALLCASES|TREE_HORIZONTAL_WRITE_CHECK_FRAGMENTED;
     else
        mode_write=TREE_HORIZONTAL_WRITE_ALLCASES|TREE_HORIZONTAL_WRITE_CHECK_FRAGMENTED;
     write_trees_horizontal((void **)groups,   
                            (void **)subgroups,
                            n_groups[l_write],   n_groups_max,   
                            n_subgroups[l_write],n_subgroups_max,
                            n_subgroups_group,
                            max_tree_id_subgroup,
                            max_tree_id_group,
                            i_write,
                            j_write,
                            l_write,
                            i_read_step,
                            n_search,
                            n_wrap,
                            i_file_start,
                            filename_cat_root_in,
                            filename_output_dir,
                            a_list,
                            cosmo,
                            n_k_match,
                            l_write==0,
                            mode_write);
  }
  int i_write_last;
  int l_write_last;
  int j_write_last;
  i_write_last=i_write+1;
  j_write_last=j_write+i_read_step;
  l_write_last=l_write-1;

  // Clean-up
  SID_log("Freeing arrays...",SID_LOG_OPEN);
  for(i_search=0;i_search<n_wrap;i_search++){
     // Free subgroup information
     SID_free(SID_FARG subgroups[i_search]);
     SID_free(SID_FARG back_matches_subgroups[i_search]);
     // Free group information
     SID_free(SID_FARG groups[i_search]);
     SID_free(SID_FARG back_matches_groups[i_search]);
  }
  SID_free(SID_FARG subgroups);
  SID_free(SID_FARG groups);
  SID_free(SID_FARG back_matches_subgroups);
  SID_free(SID_FARG back_matches_groups);
  SID_free(SID_FARG match_id);
  SID_free(SID_FARG match_score);
  SID_free(SID_FARG match_index);
  SID_free(SID_FARG match_flag_two_way);
  SID_free(SID_FARG n_particles_groups);
  SID_free(SID_FARG n_particles_subgroups);
  SID_log("Done.",SID_LOG_CLOSE);

  // Any information that needs to be communicated up the trees from the
  //    roots will be done here.  This includes any information needed
  //    for tracking mergers and fragmented halos.
  // Because fragmented halos might persist longer than the search interval, we have to
  //    walk the trees forward in time to propagate the TREE_CASE_FRAGMENTED_* flags.
  // At this point, fragmented halos are only labeled when they appear.
  //    This will propagate the fragmented halo flags forward in time.
  propagate_progenitor_info(n_groups,
                            n_subgroups,
                            n_subgroups_group,
                            i_file_start,
                            i_write_last,
                            j_write_last,
                            l_write_last,
                            i_read_stop,
                            i_read_step,
                            max_tree_id_subgroup,
                            max_tree_id_group,
                            n_subgroups_max,
                            n_groups_max,
                            n_search,
                            n_files,
                            n_wrap,
                            n_k_match,
                            a_list,
                            cosmo,
                            filename_output_dir,
                            flag_compute_fragmented);

  // If extended horizontal tree files were written for fragmented
  //    halo propagation or ghost tree construction, remove them.
  if(flag_compute_ghosts || flag_compute_fragmented){
     SID_log("Removing temporary tree files...",SID_LOG_OPEN);
     for(j_write=i_read_stop;j_write>=i_read_start;j_write-=i_read_step){
        char filename_output_dir_horizontal[MAX_FILENAME_LENGTH];
        char filename_output_dir_horizontal_trees[MAX_FILENAME_LENGTH];
        char filename_remove[MAX_FILENAME_LENGTH];
        sprintf(filename_output_dir_horizontal,      "%s/horizontal",filename_output_dir);
        sprintf(filename_output_dir_horizontal_trees,"%s/trees",     filename_output_dir_horizontal);
        sprintf(filename_remove,"%s/horizontal_trees_tmp_%03d.dat",filename_output_dir_horizontal_trees,j_write);
        remove(filename_remove);
     }
     SID_log("Done.",SID_LOG_CLOSE);
  }

  // Some final clean-up
  SID_log("Cleaning up...",SID_LOG_OPEN);
  SID_free(SID_FARG n_groups);
  SID_free(SID_FARG n_subgroups);
  for(i_search=0;i_search<n_wrap;i_search++)
     SID_free(SID_FARG n_subgroups_group[i_search]);
  SID_free(SID_FARG n_subgroups_group);
  SID_free(SID_FARG a_list);
  SID_log("Done.",SID_LOG_CLOSE);

  // Force the forest construction to use all snapshots
  int n_search_forests=i_read_stop;

  // Construct tree->forest mappings
  compute_forests(filename_output_dir,n_search_forests);

  SID_log("Done.",SID_LOG_CLOSE);
}
void compute_treenode_list_marker_stats(tree_info *trees,treenode_list_info *list,tree_markers_info **markers_all,tree_markers_stats_info *stats,int **n_hist_count,int *n_hist){

   int n_stats=3;
   int n_M    =2;
   (*n_hist)  =n_stats+n_M;

   // Allocate histogram arrays -- stats
   int   i_hist=0;
   int **hist  =(int **)SID_calloc(sizeof(int *)*(*n_hist));
   int  *n_bins=(int  *)SID_calloc(sizeof(int)*(*n_hist));
   for(int i_stat=0;i_stat<n_stats;i_stat++){
      n_bins[i_hist]=trees->n_snaps;
      hist[i_hist++]=(int *)SID_calloc(sizeof(int)*n_bins[i_hist]);
   }

   // Allocate histogram arrays -- mass
   double logM_min=7.;
   int    n_M_bins=90;
   double dlogM   =0.1;
   for(int i_M=0;i_M<n_M;i_M++){
      n_bins[i_hist]=n_M_bins;
      hist[i_hist++]=(int *)SID_calloc(sizeof(int)*n_bins[i_hist]);
   }

   // Allocate histogram counts
   if((*n_hist_count)==NULL)
      (*n_hist_count)=(int *)SID_calloc(sizeof(int)*(*n_hist));
   else{
      for(i_hist=0;i_hist<(*n_hist);i_hist++)
         (*n_hist_count)[i_hist]=0;
   }

   // Work with a precompiled markers array if it's been given
   tree_markers_info *markers;
   if(markers_all==NULL)
      markers=(tree_markers_info *)SID_malloc(sizeof(tree_markers_info));

   // Create histograms
   for(int i_list=0;i_list<list->n_list_local;i_list++){
      markers=fetch_treenode_precomputed_markers(trees,list->list[i_list]);
      // Build histogram
      int i_bin;
      for(int i_hist=0;i_hist<(*n_hist);i_hist++){
         i_bin=-1;
         if(i_hist<n_stats){
            tree_node_info *marker=NULL;
            if(i_hist==0)
               marker=markers->half_peak_mass;
            else if(i_hist==1)
               marker=markers->merger_33pc_remnant;
            else if(i_hist==2)
               marker=markers->merger_10pc_remnant;
            else
               SID_trap_error("Behaviour undefined in compute_treenode_list_marker_stats()",ERROR_LOGIC);
            if(marker!=NULL)
               i_bin=marker->snap_tree;
            else
               i_bin=-1;
         }
         else{
            if(i_hist==(n_stats+0)){
               halo_properties_info *properties=fetch_treenode_properties(trees,list->list[i_list]);
               i_bin=(take_log10(properties->M_vir)-logM_min)/dlogM;
            }
            else if(i_hist==(n_stats+1))
               i_bin=(take_log10(markers->M_peak)-logM_min)/dlogM;
            else
               SID_trap_error("Behaviour undefined in compute_treenode_list_marker_stats()",ERROR_LOGIC);
         }
         if(i_bin>=0 && i_bin<n_bins[i_hist]){
            hist[i_hist][i_bin]++;
            (*n_hist_count)[i_hist]++;
         }
      }
   }
   if(markers_all==NULL)
      SID_free(SID_FARG markers);
   for(int i_hist=0;i_hist<(*n_hist);i_hist++){
      SID_Allreduce(SID_IN_PLACE,hist[i_hist],n_bins[i_hist],SID_INT,SID_SUM,SID.COMM_WORLD);
      SID_Allreduce(SID_IN_PLACE,&((*n_hist_count)[i_hist]),1,SID_INT,SID_SUM,SID.COMM_WORLD);
   }

   // Find 68 and 95 percent confidence ranges
   for(int i_hist=0;i_hist<(*n_hist);i_hist++){
      int sum=0;
      for(int i_bin=0;i_bin<n_bins[i_hist];i_bin++)
         sum+=hist[i_hist][i_bin];
      size_t *hist_index=NULL;
      merge_sort(hist[i_hist],(size_t)(n_bins[i_hist]),&hist_index,SID_INT,SORT_COMPUTE_INDEX,FALSE);
      int i_peak =hist_index[n_bins[i_hist]-1];
      int i_68_lo=hist_index[n_bins[i_hist]-1];
      int i_68_hi=hist_index[n_bins[i_hist]-1];
      int target =(int)(0.68*(double)sum);
      int accum  =0;
      int i_bin  =0;
      while(accum<=target && i_bin<n_bins[i_hist]){
         size_t idx_i=hist_index[n_bins[i_hist]-i_bin-1];
         if(idx_i<i_68_lo) i_68_lo=idx_i;
         if(idx_i>i_68_hi) i_68_hi=idx_i;
         accum+=hist[i_hist][idx_i];
         i_bin++;
      }
      int i_95_lo=i_68_lo;
      int i_95_hi=i_68_hi;
      target=(int)(0.95*(double)sum);
      while(accum<=target && i_bin<n_bins[i_hist]){
         size_t idx_i=hist_index[n_bins[i_hist]-i_bin-1];
         if(idx_i<i_95_lo) i_95_lo=idx_i;
         if(idx_i>i_95_hi) i_95_hi=idx_i;
         accum+=hist[i_hist][idx_i];
         i_bin++;
      }
      SID_free(SID_FARG hist_index);

      if(i_hist==0){
         stats->t_half_peak_mass_ranges[0][0]=trees->t_list[i_68_lo];
         stats->t_half_peak_mass_ranges[0][1]=trees->t_list[i_68_hi];
         stats->t_half_peak_mass_ranges[1][0]=trees->t_list[i_95_lo];
         stats->t_half_peak_mass_ranges[1][1]=trees->t_list[i_95_hi];
         stats->t_half_peak_mass_peak        =trees->t_list[i_peak];
      }
      else if(i_hist==1){
         stats->t_merger_33pc_ranges[0][0]=trees->t_list[i_68_lo];
         stats->t_merger_33pc_ranges[0][1]=trees->t_list[i_68_hi];
         stats->t_merger_33pc_ranges[1][0]=trees->t_list[i_95_lo];
         stats->t_merger_33pc_ranges[1][1]=trees->t_list[i_95_hi];
         stats->t_merger_33pc_peak        =trees->t_list[i_peak];
      }
      else if(i_hist==2){
         stats->t_merger_10pc_ranges[0][0]=trees->t_list[i_68_lo];
         stats->t_merger_10pc_ranges[0][1]=trees->t_list[i_68_hi];
         stats->t_merger_10pc_ranges[1][0]=trees->t_list[i_95_lo];
         stats->t_merger_10pc_ranges[1][1]=trees->t_list[i_95_hi];
         stats->t_merger_10pc_peak        =trees->t_list[i_peak];
      }
      else if(i_hist==3){
         stats->M_peak_ranges[0][0]=logM_min+(double)(i_68_lo)*dlogM;
         stats->M_peak_ranges[0][1]=logM_min+(double)(i_68_hi)*dlogM;
         stats->M_peak_ranges[1][0]=logM_min+(double)(i_95_lo)*dlogM;
         stats->M_peak_ranges[1][1]=logM_min+(double)(i_95_hi)*dlogM;
         stats->M_peak_peak        =logM_min+(double)( i_peak)*dlogM;
      }
      else if(i_hist==4){
         stats->M_vir_ranges[0][0]=logM_min+(double)(i_68_lo)*dlogM;
         stats->M_vir_ranges[0][1]=logM_min+(double)(i_68_hi)*dlogM;
         stats->M_vir_ranges[1][0]=logM_min+(double)(i_95_lo)*dlogM;
         stats->M_vir_ranges[1][1]=logM_min+(double)(i_95_hi)*dlogM;
         stats->M_vir_peak        =logM_min+(double)( i_peak)*dlogM;
      }
      else
         SID_trap_error("Behaviour undefined in compute_treenode_list_marker_stats()",ERROR_LOGIC);
   }

   // Clean-up
   for(int i_hist=0;i_hist<(*n_hist);i_hist++)
      SID_free(SID_FARG hist[i_hist]);
   SID_free(SID_FARG hist);
   SID_free(SID_FARG n_bins);

}
Пример #16
0
void init_MCMC(MCMC_info * MCMC,
               const char *problem_name,
               void *      params,
               int (*f)(double *, MCMC_info *, double **),
               int     n_P,
               double *P_init,
               char ** P_names,
               double *P_limit_min,
               double *P_limit_max,
               int     n_arrays,
               ...) {
    int     i_P;
    int     i_array;
    int     i;
    FILE *  ft, *ft_restart;
    char    test_dir[256], test_restart[256];
    va_list vargs;
    va_start(vargs, n_arrays);

    SID_log("Initializing MCMC structure...", SID_LOG_OPEN);

    // Set defaults to bare minimums
    MCMC->n_avg                 = 100;
    MCMC->n_iterations_burn     = 4;
    MCMC->n_iterations          = 8;
    MCMC->n_thin                = 1;
    MCMC->coverage_size         = 100;
    MCMC->flag_autocor_on       = GBP_FALSE;
    MCMC->flag_integrate_on     = GBP_FALSE;
    MCMC->flag_analysis_on      = GBP_TRUE;
    MCMC->first_map_call        = GBP_TRUE;
    MCMC->first_link_call       = GBP_TRUE;
    MCMC->flag_init_chain       = GBP_TRUE;
    MCMC->first_chain_call      = GBP_TRUE;
    MCMC->first_parameter_call  = GBP_TRUE;
    MCMC->first_likelihood_call = GBP_TRUE;
    MCMC->ln_likelihood_last    = 0.;
    MCMC->ln_likelihood_new     = 0.;
    MCMC->ln_likelihood_chain   = 0.;
    MCMC->P_init                = NULL;
    MCMC->P_new                 = NULL;
    MCMC->P_last                = NULL;
    MCMC->P_chain               = NULL;
    MCMC->P_limit_min           = NULL;
    MCMC->P_limit_max           = NULL;
    MCMC->P_min                 = NULL;
    MCMC->P_max                 = NULL;
    MCMC->P_avg                 = NULL;
    MCMC->dP_avg                = NULL;
    MCMC->P_best                = NULL;
    MCMC->P_peak                = NULL;
    MCMC->P_lo_68               = NULL;
    MCMC->P_hi_68               = NULL;
    MCMC->P_lo_95               = NULL;
    MCMC->P_hi_95               = NULL;
    MCMC->n_M                   = NULL;
    MCMC->M_new                 = NULL;
    MCMC->M_last                = NULL;
    MCMC->DS                    = NULL;
    MCMC->last                  = NULL;
    MCMC->V                     = NULL;
    MCMC->m                     = NULL;
    MCMC->b                     = NULL;
    MCMC->RNG                   = NULL;
    MCMC->params                = NULL;
    MCMC->temperature           = 1.0;
    MCMC->n_DS                  = 0;
    MCMC->n_M_total             = 0;
    MCMC->n_fail                = 0;
    MCMC->n_success             = 0;
    MCMC->n_propositions        = 0;
    MCMC->n_map_calls           = 0;

    // Process the passed arguments
    MCMC->map_P_to_M                 = f;
    MCMC->compute_MCMC_ln_likelihood = compute_MCMC_ln_likelihood_default;
    MCMC->params                     = params;
    MCMC->n_P                        = n_P;
    sprintf(MCMC->problem_name, "%s", problem_name);
    MCMC->P_names       = (char **)SID_malloc(sizeof(char *) * MCMC->n_P);
    MCMC->P_name_length = 0;
    for(i_P = 0; i_P < n_P; i_P++) {
        MCMC->P_names[i_P] = (char *)SID_malloc(sizeof(char) * MCMC_NAME_SIZE);
        sprintf(MCMC->P_names[i_P], "%s", P_names[i_P]);
        MCMC->P_name_length = GBP_MAX((size_t)(MCMC->P_name_length), strlen(MCMC->P_names[i_P]));
    }
    sprintf(MCMC->P_name_format, "%%-%ds", MCMC->P_name_length);

    // Initialize the MCMC mode and set things associated with it
    set_MCMC_mode(MCMC, MCMC_MODE_DEFAULT); // MCMC->my_chain is set here

    // Set parameter arrays and limits
    MCMC->P_init      = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
    MCMC->P_new       = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
    MCMC->P_last      = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
    MCMC->P_chain     = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
    MCMC->P_limit_min = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
    MCMC->P_limit_max = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
    if(P_limit_min == NULL) {
        for(i_P = 0; i_P < n_P; i_P++)
            MCMC->P_limit_min[i_P] = -DBL_MAX * 1e-3;
    } else {
        for(i_P = 0; i_P < n_P; i_P++)
            MCMC->P_limit_min[i_P] = P_limit_min[i_P];
    }
    if(P_limit_max == NULL) {
        for(i_P = 0; i_P < n_P; i_P++)
            MCMC->P_limit_max[i_P] = DBL_MAX * 1e-3;
    } else {
        for(i_P = 0; i_P < n_P; i_P++)
            MCMC->P_limit_max[i_P] = P_limit_max[i_P];
    }

    // Set parameter initial values
    memcpy(MCMC->P_init, P_init, (size_t)MCMC->n_P * sizeof(double));
    memcpy(MCMC->P_new, P_init, (size_t)MCMC->n_P * sizeof(double));
    memcpy(MCMC->P_last, P_init, (size_t)MCMC->n_P * sizeof(double));
    memcpy(MCMC->P_chain, P_init, (size_t)MCMC->n_P * sizeof(double));

    // Set arrays
    MCMC->n_arrays = n_arrays;
    if(n_arrays > 0) {
        MCMC->array      = (double **)SID_malloc(sizeof(double *) * MCMC->n_arrays);
        MCMC->array_name = (char **)SID_malloc(sizeof(char *) * MCMC->n_arrays);
        for(i_array = 0; i_array < n_arrays; i_array++) {
            MCMC->array[i_array]      = (double *)SID_malloc(sizeof(double) * MCMC->n_P);
            MCMC->array_name[i_array] = (char *)SID_malloc(sizeof(char) * MCMC_NAME_SIZE);
            memcpy(MCMC->array[i_array], (double *)va_arg(vargs, double *), (size_t)(MCMC->n_P) * sizeof(double));
            sprintf(MCMC->array_name[i_array], "%s", (char *)va_arg(vargs, char *));
        }
    } else
Пример #17
0
void write_match_results(char *      filename_out_dir,
                         char *      filename_out_root,
                         int         i_read,
                         int         j_read,
                         const char *filename_cat1,
                         const char *filename_cat2,
                         plist_info *plist1,
                         plist_info *plist2,
                         int         k_match,
                         float       match_weight_rank_index,
                         int         mode) {
    char    filename_out[256];
    char    filename_out_dir_snap[256];
    FILE *  fp_out;
    int     k_read, l_read;
    int     flag_go;
    int     i_read_start_file;
    int     i_read_stop_file;
    int     i_read_step_file;
    int     n_search_file;
    int     n_search_total;
    int     n_k_match;
    int     flag_match_subgroups;
    char    group_text_prefix[5];
    int     n_matches;
    int     n_files;
    int     n_groups_1;
    int     n_groups_1_local;
    int     n_groups_2;
    int     n_groups_2_local;
    int     i_group;
    int     buffered_count;
    int     buffered_count_local;
    int     j_group;
    int     index_test;
    int     i_rank;
    int *   n_particles;
    int *   n_sub_group;
    int *   file_index_1;
    int *   match_id    = NULL;
    float * match_score = NULL;
    int *   match_count = NULL;
    char    cat_name_1[20];
    char    cat_name_2[20];
    size_t *match_rank  = NULL;
    size_t *match_index = NULL;
    size_t  offset;
    int *   n_return;
    void *  buffer;
    int *   buffer_int;
    size_t *buffer_size_t;
    float * buffer_float;
    int     n_buffer_max = 131072; // 32*32k=1MB for 8-byte values
    int     n_buffer;
    int     i_buffer;
    int     j_buffer;

    switch(k_match) {
        case 0:
            flag_match_subgroups = MATCH_SUBGROUPS;
            sprintf(group_text_prefix, "sub");
            break;
        case 1:
            flag_match_subgroups = MATCH_GROUPS;
            sprintf(group_text_prefix, "");
            break;
    }

    // Intialize filenames
    if(SID_CHECK_BITFIELD_SWITCH(mode, WRITE_MATCHES_MODE_TREES)) {
        sprintf(filename_out_dir_snap, "%s/%s", filename_out_dir, filename_cat1);
        // Create output directory if need-be
        if(filename_out_dir != NULL)
            mkdir(filename_out_dir, 02755);
    } else if(SID_CHECK_BITFIELD_SWITCH(mode, WRITE_MATCHES_MODE_SINGLE))
        sprintf(filename_out_dir_snap, "%s/", filename_out_dir);
    else
        SID_exit_error("Invalid write mode flag (%d).", SID_ERROR_LOGIC, mode);
    if(filename_out_dir != NULL)
        sprintf(filename_out, "%s/%sgroup_matches_%s_%s.dat", filename_out_dir_snap, group_text_prefix, filename_cat1, filename_cat2);
    else
        sprintf(filename_out, "%s_%sgroup_matches_%s_%s.dat", filename_out_root, group_text_prefix, filename_cat1, filename_cat2);

    SID_log("Writing match results to {%s}...", SID_LOG_OPEN | SID_LOG_TIMER, filename_out);

    // Fetch halo counts ...
    n_groups_1 = ((int *)ADaPS_fetch(plist1->data, "n_%sgroups_all_%s", group_text_prefix, filename_cat1))[0];
    n_groups_2 = ((int *)ADaPS_fetch(plist2->data, "n_%sgroups_all_%s", group_text_prefix, filename_cat2))[0];

    // Write header.
    if(SID.I_am_Master) {
        if(filename_out_dir != NULL)
            mkdir(filename_out_dir_snap, 02755);
        if((fp_out = fopen(filename_out, "w")) == NULL)
            SID_exit_error("Could not open {%s} for writing.", SID_ERROR_IO_OPEN, filename_out);
        fwrite(&i_read, sizeof(int), 1, fp_out);
        fwrite(&j_read, sizeof(int), 1, fp_out);
        fwrite(&n_groups_1, sizeof(int), 1, fp_out);
        fwrite(&n_groups_2, sizeof(int), 1, fp_out);
        fwrite(&match_weight_rank_index, sizeof(float), 1, fp_out);
    }

    // Everything else only needs to be written if there are halos to match with
    if(n_groups_1 > 0) {
        // Fetch catalog and matching info ...
        n_groups_1_local = ((int *)ADaPS_fetch(plist1->data, "n_%sgroups_%s", group_text_prefix, filename_cat1))[0];
        file_index_1     = (int *)ADaPS_fetch(plist1->data, "file_index_%sgroups_%s", group_text_prefix, filename_cat1);
        n_groups_2_local = ((int *)ADaPS_fetch(plist2->data, "n_%sgroups_%s", group_text_prefix, filename_cat2))[0];
        match_id         = (int *)ADaPS_fetch(plist1->data, "match_match");
        match_score      = (float *)ADaPS_fetch(plist1->data, "match_score_match");
        match_count      = (int *)ADaPS_fetch(plist1->data, "match_count_match");

        // Generate ranking of matches
        sort(match_id, (size_t)n_groups_1_local, &match_index, SID_INT, SORT_GLOBAL, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
        sort(match_index, (size_t)n_groups_1_local, &match_rank, SID_SIZE_T, SORT_GLOBAL, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
        SID_free(SID_FARG match_index);

        // Now we write the matching results.  We need to write back to the file in the
        //   order that it was read from the halo catalogs, not necessarily the PH order
        //   that it is stored in RAM.  This requires some buffer trickery.
        buffer        = SID_malloc(n_buffer_max * sizeof(size_t));
        buffer_int    = (int *)buffer;
        buffer_size_t = (size_t *)buffer;
        buffer_float  = (float *)buffer;

        // Write match_ids ...
        //    ... loop over all the groups in buffer-sized batches
        SID_log("Writing match IDs...", SID_LOG_OPEN | SID_LOG_TIMER);
        for(i_group = 0, buffered_count_local = 0; i_group < n_groups_1; i_group += n_buffer) {
            // Decide this buffer iteration's size
            n_buffer = GBP_MIN(n_buffer_max, n_groups_1 - i_group);
            // Set the buffer to a default value smaller than the smallest possible data size
            for(i_buffer = 0; i_buffer < n_buffer; i_buffer++)
                buffer_int[i_buffer] = -2; // Min value of match_id is -1
            // Determine if any of the local data is being used for this buffer
            for(j_group = 0; j_group < n_groups_1_local; j_group++) {
                index_test = file_index_1[j_group] - i_group;
                // ... if so, set the appropriate buffer value
                if(index_test >= 0 && index_test < n_buffer) {
                    buffer_int[index_test] = match_id[j_group];
                    buffered_count_local++;
                }
            }
            // Doing a global max on the buffer yields the needed buffer on all ranks
            SID_Allreduce(SID_IN_PLACE, buffer_int, n_buffer, SID_INT, SID_MAX, SID_COMM_WORLD);

            if(SID.I_am_Master) {
                // Sanity check
                for(i_buffer = 0; i_buffer < n_buffer; i_buffer++) {
                    if(buffer_int[i_buffer] < -1 || buffer_int[i_buffer] >= n_groups_2)
                        SID_exit_error(
                                "Illegal match_id result (%d) for group No. %d.  There are %d groups in the target catalog.",
                                SID_ERROR_LOGIC, buffer_int[i_buffer], i_group + i_buffer, n_groups_2);
                }
                // Write the buffer
                fwrite(buffer_int, sizeof(int), (size_t)n_buffer, fp_out);
            }
        }

        // Sanity check
        calc_sum_global(&buffered_count_local, &buffered_count, 1, SID_INT, CALC_MODE_DEFAULT, SID_COMM_WORLD);
        if(buffered_count != n_groups_1)
            SID_exit_error("Buffer counts don't make sense (ie %d!=%d) after writing match IDs.", SID_ERROR_LOGIC,
                           buffered_count, n_groups_1);
        SID_log("Done.", SID_LOG_CLOSE);

        // Write match_score ...
        //    ... loop over all the groups in buffer-sized batches
        SID_log("Writing match scores...", SID_LOG_OPEN | SID_LOG_TIMER);
        for(i_group = 0, buffered_count_local = 0; i_group < n_groups_1; i_group += n_buffer) {
            // Decide this buffer iteration's size
            n_buffer = GBP_MIN(n_buffer_max, n_groups_1 - i_group);
            // Set the buffer to a default value smaller than the smallest possible data size
            for(i_buffer = 0; i_buffer < n_buffer; i_buffer++)
                buffer_float[i_buffer] = -1.; // Min value of match_score is 0.
            // Determine if any of the local data is being used for this buffer
            for(j_group = 0; j_group < n_groups_1_local; j_group++) {
                index_test = file_index_1[j_group] - i_group;
                // ... if so, set the appropriate buffer value
                if(index_test >= 0 && index_test < n_buffer) {
                    buffer_float[index_test] = match_score[j_group];
                    buffered_count_local++;
                }
            }
            // Doing a global max on the buffer yields the needed buffer on all ranks
            SID_Allreduce(SID_IN_PLACE, buffer_float, n_buffer, SID_FLOAT, SID_MAX, SID_COMM_WORLD);

            if(SID.I_am_Master) {
                // Sanity check
                for(i_buffer = 0; i_buffer < n_buffer; i_buffer++) {
                    if(buffer_float[i_buffer] < 0.)
                        SID_exit_error("Illegal match_score result (%f) for group No. %d.", SID_ERROR_LOGIC,
                                       buffer_float[i_buffer], i_group + i_buffer);
                }
                // Write the buffer
                fwrite(buffer, sizeof(float), (size_t)n_buffer, fp_out);
            }
        }

        // Sanity check
        calc_sum_global(&buffered_count_local, &buffered_count, 1, SID_INT, CALC_MODE_DEFAULT, SID_COMM_WORLD);
        if(buffered_count != n_groups_1)
            SID_exit_error("Buffer counts don't make sense (ie %d!=%d) after writing match scores.", SID_ERROR_LOGIC,
                           buffered_count, n_groups_1);
        SID_log("Done.", SID_LOG_CLOSE);

        // Write match_count ...
        //    ... loop over all the groups in buffer-sized batches
        SID_log("Writing match counts...", SID_LOG_OPEN | SID_LOG_TIMER);
        for(i_group = 0, buffered_count_local = 0; i_group < n_groups_1; i_group += n_buffer) {
            // Decide this buffer iteration's size
            n_buffer = GBP_MIN(n_buffer_max, n_groups_1 - i_group);
            // Set the buffer to a default value smaller than the smallest possible data size
            for(i_buffer = 0; i_buffer < n_buffer; i_buffer++)
                buffer_int[i_buffer] = -1; // Min value of match_count is 0.
            // Determine if any of the local data is being used for this buffer
            for(j_group = 0; j_group < n_groups_1_local; j_group++) {
                index_test = file_index_1[j_group] - i_group;
                // ... if so, set the appropriate buffer value
                if(index_test >= 0 && index_test < n_buffer) {
                    buffer_int[index_test] = match_count[j_group];
                    buffered_count_local++;
                }
            }
            // Doing a global max on the buffer yields the needed buffer on all ranks
            SID_Allreduce(SID_IN_PLACE, buffer_int, n_buffer, SID_INT, SID_MAX, SID_COMM_WORLD);

            if(SID.I_am_Master) {
                // Sanity check
                for(i_buffer = 0; i_buffer < n_buffer; i_buffer++) {
                    if(buffer_int[i_buffer] < 0.)
                        SID_exit_error("Illegal match_count result (%f) for group No. %d.", SID_ERROR_LOGIC,
                                       buffer_int[i_buffer], i_group + i_buffer);
                }
                // Write the buffer
                fwrite(buffer, sizeof(int), (size_t)n_buffer, fp_out);
            }
        }

        // Sanity check
        calc_sum_global(&buffered_count_local, &buffered_count, 1, SID_INT, CALC_MODE_DEFAULT, SID_COMM_WORLD);
        if(buffered_count != n_groups_1)
            SID_exit_error("Buffer counts don't make sense (ie %d!=%d) after writing match scores.", SID_ERROR_LOGIC,
                           buffered_count, n_groups_1);
        SID_log("Done.", SID_LOG_CLOSE);

        // Clean-up
        SID_log("Cleaning-up...", SID_LOG_OPEN);
        SID_free(SID_FARG buffer);
        SID_free(SID_FARG match_rank);

        SID_log("Done.", SID_LOG_CLOSE);
    }

    // Close the file
    if(SID.I_am_Master)
        fclose(fp_out);

    SID_log("Done.", SID_LOG_CLOSE);
}
Пример #18
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();
}
Пример #19
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);
}
Пример #20
0
int main(int argc, char *argv[]) {
    int    n_search;
    int    i_halo;
    char   filename_in[SID_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 *  n_particles_i;
    int *  n_particles_j;
    int    j_read;
    int    mode;
    int    n_groups_i;
    int    n_groups_j;
    int    j_halo;
    int    i_read;
    int    i_read_start;
    int    i_read_stop;
    SID_fp fp_in;

    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    char filename_root_in[SID_MAX_FILENAME_LENGTH];
    char filename_catalog_root[SID_MAX_FILENAME_LENGTH];
    char filename_halo_version[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_root_in, argv[1]);
    strcpy(filename_halo_version, argv[2]);
    if(!strcmp(argv[3], "groups") || !strcmp(argv[3], "group"))
        mode = MATCH_GROUPS;
    else if(!strcmp(argv[3], "subgroups") || !strcmp(argv[3], "subgroup"))
        mode = MATCH_SUBGROUPS;
    else {
        SID_exit_error("Invalid mode selection {%s}.  Should be 'group' or 'subgroup'.", SID_ERROR_SYNTAX, argv[3]);
    }
    i_read               = atoi(argv[4]);
    j_read               = atoi(argv[5]);
    int flag_SSimPL_base = GBP_TRUE;
    if(argc == 7) {
        flag_SSimPL_base = GBP_FALSE;
        strcpy(filename_catalog_root, argv[6]);
        sprintf(filename_catalog_root, "%s/halos/%s", argv[6], filename_halo_version);
    } else
        sprintf(filename_catalog_root, "%s/halos/%s", filename_root_in, filename_halo_version);
    SID_log("Searching match information for halo #%d in file #%d of {%s}...", SID_LOG_OPEN | SID_LOG_TIMER, i_halo, i_read, filename_root_in);

    // 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[SID_MAX_FILENAME_LENGTH];
    if(flag_SSimPL_base)
        sprintf(filename_root, "%s/trees/matches/%03d/", filename_root_in, i_read);
    else
        sprintf(filename_root, "%s_", filename_root_in);

    // Read header information
    int   i_read_in;
    int   j_read_in;
    int   n_groups_1;
    int   n_groups_2;
    float score_rank_index;
    sprintf(filename_in, "%s%sgroup_matches_%03d_%03d.dat", filename_root, group_text_prefix, i_read, j_read);
    SID_fopen(filename_in, "r", &fp_in);
    SID_fread(&i_read_in, sizeof(int), 1, &fp_in);
    SID_log("i_read    =%d", SID_LOG_COMMENT, i_read_in);
    SID_fread(&j_read_in, sizeof(int), 1, &fp_in);
    SID_log("j_read    =%d", SID_LOG_COMMENT, j_read_in);
    SID_fread(&n_groups_i, sizeof(int), 1, &fp_in);
    SID_log("n_groups_i=%d", SID_LOG_COMMENT, n_groups_i);
    SID_fread(&n_groups_j, sizeof(int), 1, &fp_in);
    SID_log("n_groups_j=%d", SID_LOG_COMMENT, n_groups_j);
    SID_fread(&score_rank_index, sizeof(int), 1, &fp_in);
    SID_log("score_idx =%f", SID_LOG_COMMENT, score_rank_index);

    // Allocate RAM
    int *  match = (int *)SID_malloc(sizeof(int) * n_groups_i);
    float *score = (float *)SID_malloc(sizeof(float) * n_groups_i);
    int *  count = (int *)SID_malloc(sizeof(int) * n_groups_i);

    // Read arrays
    SID_fread(match, sizeof(int), n_groups_i, &fp_in);
    SID_fread(score, sizeof(float), n_groups_i, &fp_in);
    SID_fread(count, sizeof(int), n_groups_i, &fp_in);

    // Close file
    SID_fclose(&fp_in);

    // Read halo sizes from header file
    SID_log("Reading halo sizes...", SID_LOG_OPEN);
    int *n_p_i = (int *)SID_malloc(sizeof(int) * n_groups_i);
    sprintf(filename_in, "%s_%03d.catalog_%sgroups", filename_catalog_root, i_read, group_text_prefix);
    SID_fopen(filename_in, "r", &fp_in);
    int n_cat_i;
    int offset_size_i;
    SID_fread(&n_cat_i, sizeof(int), 1, &fp_in);
    SID_fread(&offset_size_i, sizeof(int), 1, &fp_in);
    if(n_cat_i != n_groups_i)
        SID_exit_error("Catalog 'i' halo counts don't match (ie %d!=%d)", SID_ERROR_LOGIC, n_cat_i, n_groups_i);
    SID_fread(n_p_i, sizeof(int), n_cat_i, &fp_in);
    SID_fclose(&fp_in);

    int *n_p_j = (int *)SID_malloc(sizeof(int) * n_groups_j);
    sprintf(filename_in, "%s_%03d.catalog_%sgroups", filename_catalog_root, j_read, group_text_prefix);
    SID_fopen(filename_in, "r", &fp_in);
    int n_cat_j;
    int offset_size_j;
    SID_fread(&n_cat_j, sizeof(int), 1, &fp_in);
    SID_fread(&offset_size_j, sizeof(int), 1, &fp_in);
    if(n_cat_j != n_groups_j)
        SID_exit_error("Catalog 'i' halo counts don't match (ie %d!=%d)", SID_ERROR_LOGIC, n_cat_j, n_groups_j);
    SID_fread(n_p_j, sizeof(int), n_cat_j, &fp_in);
    SID_fclose(&fp_in);
    SID_log("Done.", SID_LOG_CLOSE);

    // Print results
    for(k_read = 0; k_read < n_groups_i; k_read++) {
        if(match[k_read] >= 0)
            printf("%7d %7d %7d %7d %7d %le %le %le\n",
                   k_read,
                   match[k_read],
                   n_p_i[k_read],
                   n_p_j[match[k_read]],
                   count[k_read],
                   score[k_read],
                   maximum_match_score(n_p_i[k_read]),
                   match_score_f_goodness(score[k_read], n_p_i[k_read]));
    }

    // Clean-up
    SID_free(SID_FARG match);
    SID_free(SID_FARG score);
    SID_free(SID_FARG count);
    SID_free(SID_FARG n_p_i);
    SID_free(SID_FARG n_p_j);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
Пример #21
0
void SID_init(int       *argc,
              char     **argv[],
              SID_args   args[],
              void      *mpi_comm_as_void){
  int  status;
  int  i_level;
  int  i_char;
  int  flag_continue;
  int  flag_passed_comm;

  // MPI-specific things
#if USE_MPI
  int      n_keys;
  int      i_key;
  char     key[256];
  char     key_value[256];
  int      key_exists;
  char     nodes_string[256];
  SID_fp   fp_tmp;
  FILE    *fp_hack;
  int      node_name_length;
  MPI_Comm mpi_comm;
#if USE_MPI_IO
  MPI_Info info_disp;
#endif

  if (mpi_comm_as_void == NULL)
  {
    flag_passed_comm = 0;
    MPI_Init(argc,argv);
    MPI_Comm_dup(MPI_COMM_WORLD, &mpi_comm);
  }
  else
  {
    mpi_comm = *((MPI_Comm *) mpi_comm_as_void);
    flag_passed_comm = 1;
  }

  MPI_Comm_size(mpi_comm, &(SID.n_proc));
  MPI_Comm_rank(mpi_comm, &(SID.My_rank));

  SID.My_node =(char *)SID_malloc(SID_MAXLENGTH_PROCESSOR_NAME * sizeof(char));
#if USE_MPI
  MPI_Get_processor_name(SID.My_node, &node_name_length);
#else
  sprintf(SID.My_node,"localhost");
  node_name_length=strlen(SID.My_node);
#endif
  if (node_name_length >= SID_MAXLENGTH_PROCESSOR_NAME-1)
    SID_trap_error("SID_MAXLENGTH_PROCESSOR_NAME needs to be increased",ERROR_LOGIC);

  // Make my_rank=MASTER_RANK the master
  if(SID.My_rank==MASTER_RANK)
    SID.I_am_Master=TRUE;
  else
    SID.I_am_Master=FALSE;

  // Identify the last rank
  if(SID.My_rank==SID.n_proc-1)
    SID.I_am_last_rank=TRUE;
  else
    SID.I_am_last_rank=FALSE;

  #if USE_MPI_IO
  // Fetch collective buffering defaults
  MPI_Info_create(&(SID.file_info));
  if(SID.I_am_Master){
    fp_hack=fopen(".tmp.SID","w+");    
    fclose(fp_hack);
  }
  MPI_Barrier(mpi_comm);
  MPI_File_open(mpi_comm,
                ".tmp.SID",
                MPI_MODE_WRONLY,
                MPI_INFO_NULL,
                &(fp_tmp.fp));
  MPI_File_get_info(fp_tmp.fp,&info_disp);
  MPI_Info_get_nkeys(info_disp,&n_keys);
  for(i_key=0;i_key<n_keys;i_key++){
    MPI_Info_get_nthkey(info_disp,i_key,key);
    MPI_Info_get(info_disp,key,MPI_MAX_INFO_VAL,key_value,&key_exists);
    if(key_exists)
      MPI_Info_set((SID.file_info),key,key_value);
  }
  MPI_File_close(&(fp_tmp.fp));
  if(SID.I_am_Master)
    remove(".tmp.SID");

  // Set user-defined colective buffering optimizations
  sprintf(nodes_string,"%d",MIN(SID.n_proc,N_IO_FILES_MAX));
  MPI_Info_set((SID.file_info),"cb_nodes",            nodes_string);
  MPI_Info_set((SID.file_info),"cb_config_list",      "*:1");
  #endif
#else
  SID.My_rank=MASTER_RANK;
  SID.n_proc =1;
#endif

/*
#if !USE_MPI_IO
    SID.n_groups=SID.n_proc/N_IO_FILES_MAX;
    if(SID.n_proc%N_IO_FILES_MAX) SID.n_groups++;
    SID.My_group=SID.My_rank/N_IO_FILES_MAX;
#endif
*/

  // Set ranks to the left and right
  SID.rank_to_right  =(SID.My_rank+1)%SID.n_proc;
  SID.rank_to_left   = SID.My_rank-1;
  if(SID.rank_to_left<0)
    SID.rank_to_left = SID.n_proc-1;

  // Intitialize log timing information
  SID.time_start_level=(time_t *)SID_malloc(sizeof(time_t)*SID_LOG_MAX_LEVELS);
  SID.time_stop_level =(time_t *)SID_malloc(sizeof(time_t)*SID_LOG_MAX_LEVELS);
  SID.time_total_level=(int    *)SID_malloc(sizeof(int)   *SID_LOG_MAX_LEVELS);
  SID.IO_size         =(double *)SID_malloc(sizeof(double)*SID_LOG_MAX_LEVELS);
  SID.flag_use_timer  =(int    *)SID_malloc(sizeof(int)   *SID_LOG_MAX_LEVELS);
  for(i_level=0;i_level<SID_LOG_MAX_LEVELS;i_level++){
    SID.time_start_level[i_level]=0;
    SID.time_stop_level[i_level] =0;
    SID.time_total_level[i_level]=0;
    SID.IO_size[i_level]         =0.;
    SID.flag_use_timer[i_level]  =FALSE;
  }

  // Initialize other log information
#if USE_MPI
  if(*argc>1)
    SID.fp_in        =fopen((*argv)[1],"r");
  else
    SID.fp_in        =NULL;
#else
  SID.fp_in          =stdin;
#endif
  if (flag_passed_comm)
    SID.fp_log       = NULL;
  else
    SID.fp_log       = stderr;
  SID.level          =0;
  SID.indent         =TRUE;
  SID.awake          =TRUE;
  SID.flag_results_on=FALSE;
  SID.verbosity      =SID_LOG_MAX_LEVELS;

  // Store the name of the binary executable that brought us here
  strcpy(SID.My_binary,(*argv)[0]);
  strip_path(SID.My_binary);

  // Initialize argument information
  if(args!=NULL){
    if((status=SID_parse_args(*argc,*argv,args))>0){
      SID_print_syntax(*argc,*argv,args);
      SID_exit(status);
    }
  }
  else
    SID.args=NULL;

#if USE_MPI_IO
  if(SID.I_am_Master){
    fp_hack=fopen(".tmp.SID","w+");
    fclose(fp_hack);
  }
  MPI_Barrier(mpi_comm);
  SID_fopen(".tmp.SID","w",&fp_tmp);
  MPI_File_get_info(fp_tmp.fp,&info_disp);
  if(SID.I_am_Master){
    fprintf(stdout,"\n");
    fprintf(stdout,"MPI-I/O Configuration:\n");
    fprintf(stdout,"---------------------\n");
    MPI_Info_get_nkeys(info_disp,&n_keys);
    for(i_key=0;i_key<n_keys;i_key++){
      MPI_Info_get_nthkey(info_disp,i_key,key);
      MPI_Info_get(info_disp,key,MPI_MAX_INFO_VAL,key_value,&key_exists);
      if(key_exists)
        fprintf(stdout,"key %2d of %d: {%s}={%s}\n",i_key+1,n_keys,key,key_value);
    }
    fprintf(stdout,"\n");
  }
  SID_fclose(&fp_tmp);
  if(SID.I_am_Master)
    remove(".tmp.SID");
#else
  #if USE_MPI
  if(SID.I_am_Master)
    fprintf(stdout,"MPI-I/O switched off.\n\n");
  #endif
#endif

  // Create private COMM_WORLD
  SID_Comm_init(&(SID.COMM_WORLD));
#if USE_MPI
  MPI_Comm_dup(mpi_comm,                &((SID.COMM_WORLD)->comm));
  MPI_Comm_group((SID.COMM_WORLD)->comm,&((SID.COMM_WORLD)->group));
  MPI_Comm_size(SID.COMM_WORLD->comm,   &((SID.COMM_WORLD)->n_proc));
  MPI_Comm_rank(SID.COMM_WORLD->comm,   &((SID.COMM_WORLD)->My_rank));

  // We have duplicated our duplicate mpi communicator - now we can free the
  // original duplicate
  MPI_Comm_free(&mpi_comm);
#else
  SID.COMM_WORLD->comm   =NULL;
  SID.COMM_WORLD->group  =NULL;
  SID.COMM_WORLD->n_proc =1;
  SID.COMM_WORLD->My_rank=MASTER_RANK;
#endif

  // Start total-run-ime timer
  (void)time(&(SID.time_start));

  // Default max wallclock
  SID.max_wallclock=DEFAULT_MAX_WALLCLOCK_TIME;
}
Пример #22
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);
}
Пример #23
0
void average_tree_branches(const char *catalog_name){
  SID_log("Processing tree tracks in catalog {%s}...",SID_LOG_OPEN,catalog_name);

  // Master Rank does all the work
  FILE *fp_tracks_in=NULL;
  if(SID.I_am_Master){
     // Create and open the output files
     char   filename_tracks_in[MAX_FILENAME_LENGTH];
     sprintf(filename_tracks_in,"%s_tracks.dat",catalog_name);
     SID_log("Processing {%s}...",SID_LOG_OPEN,filename_tracks_in);
     fp_tracks_in=fopen(filename_tracks_in,"r");

     // Write header for tracks file
     int n_list;
     int n_snaps;
     fread_verify(&n_list, sizeof(int),1,fp_tracks_in);
     fread_verify(&n_snaps,sizeof(int),1,fp_tracks_in);
     int    *snap_list=(int    *)SID_malloc(sizeof(int)   *n_snaps);
     double *z_list   =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *t_list   =(double *)SID_malloc(sizeof(double)*n_snaps);
     fread_verify(snap_list,sizeof(int),   n_snaps,fp_tracks_in);
     fread_verify(z_list,   sizeof(double),n_snaps,fp_tracks_in);
     fread_verify(t_list,   sizeof(double),n_snaps,fp_tracks_in);

     // Allocate some temporary arrays for the tracks
     double   M_min   = 6.;
     double   M_max   =16.;
     int      n_M_bins=200;
     double   dM      =(M_max-M_min)/(double)n_M_bins;
     double   inv_dM  =1./dM;
     int    **M_hist  =(int **)SID_malloc(sizeof(int *)*n_snaps);
     for(int i_snap=0;i_snap<n_snaps;i_snap++)
        M_hist[i_snap]=(int *)SID_calloc(sizeof(int)*n_M_bins);
     int    *i_z_track=(int    *)SID_malloc(sizeof(int)*n_snaps);;
     int    *idx_track=(int    *)SID_malloc(sizeof(int)*n_snaps);;
     double *M_track  =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *x_track  =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *y_track  =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *z_track  =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *vx_track =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *vy_track =(double *)SID_malloc(sizeof(double)*n_snaps);
     double *vz_track =(double *)SID_malloc(sizeof(double)*n_snaps);

     // Process each track in turn
     for(int i_list=0;i_list<n_list;i_list++){
        int n_track;
        // Read track
        fread_verify(&n_track, sizeof(int),   1,      fp_tracks_in);
        fread_verify(i_z_track,sizeof(int),   n_track,fp_tracks_in);
        fread_verify(idx_track,sizeof(int),   n_track,fp_tracks_in);
        fread_verify(x_track,  sizeof(double),n_track,fp_tracks_in);
        fread_verify(y_track,  sizeof(double),n_track,fp_tracks_in);
        fread_verify(z_track,  sizeof(double),n_track,fp_tracks_in);
        fread_verify(vx_track, sizeof(double),n_track,fp_tracks_in);
        fread_verify(vy_track, sizeof(double),n_track,fp_tracks_in);
        fread_verify(vz_track, sizeof(double),n_track,fp_tracks_in);
        fread_verify(M_track,  sizeof(double),n_track,fp_tracks_in);
        // Build the M-histograms
        for(int i_track=0;i_track<n_track;i_track++){
           int i_bin=(int)((take_log10(M_track[i_track])-M_min)*inv_dM);
           if(i_bin>=0 && i_bin<n_M_bins)
              M_hist[i_z_track[i_track]][i_bin]++;
        }
     } // for i_list
     fclose(fp_tracks_in);

     // Build confidence intervals for M-track
     int    *n_i    =(int    *)SID_calloc(sizeof(int)*n_snaps);
     double *M_peak =(double *)SID_calloc(sizeof(double)*n_snaps);
     double *M_68_lo=(double *)SID_calloc(sizeof(double)*n_snaps);
     double *M_68_hi=(double *)SID_calloc(sizeof(double)*n_snaps);
     for(int i_snap=0;i_snap<n_snaps;i_snap++){
        size_t *M_hist_index=NULL;
        for(int i_bin=0;i_bin<n_M_bins;i_bin++)
           n_i[i_snap]+=M_hist[i_snap][i_bin];
        if(n_i[i_snap]>0){
           merge_sort(M_hist[i_snap],(size_t)n_M_bins,&M_hist_index,SID_INT,SORT_COMPUTE_INDEX,FALSE);
           int i_peak =M_hist_index[n_M_bins-1];
           int i_68_lo=M_hist_index[n_M_bins-1];
           int i_68_hi=M_hist_index[n_M_bins-1];
           int target =(int)(0.68*(double)n_i[i_snap]);
           int accum  =0;
           int i_bin  =0;
           while(accum<=target && i_bin<n_M_bins){
              size_t idx_i=M_hist_index[n_M_bins-i_bin-1];
              if(idx_i<i_68_lo) i_68_lo=idx_i;
              if(idx_i>i_68_hi) i_68_hi=idx_i;
              accum+=M_hist[i_snap][idx_i];
              i_bin++;
           }
           M_peak[i_snap] =M_min+((double)i_peak +0.5)*dM;
           M_68_lo[i_snap]=M_min+((double)i_68_lo+0.5)*dM;
           M_68_hi[i_snap]=M_min+((double)i_68_hi+0.5)*dM;
           SID_free(SID_FARG M_hist_index);
        }
        else{
           M_peak[i_snap] =-1;
           M_68_lo[i_snap]=-1;
           M_68_hi[i_snap]=-1;
        }
     }

     // Write results
     char  filename_out[MAX_FILENAME_LENGTH];
     FILE *fp_out;
     sprintf(filename_out,"%s_tracks.txt",catalog_name);
     fp_out=fopen(filename_out,"w");
     for(int i_snap=0;i_snap<n_snaps;i_snap++)
        fprintf(fp_out,"%le %le %d %le %le %le\n",
                       z_list[i_snap],
                       t_list[i_snap]/S_PER_YEAR,
                       n_i[i_snap],
                       M_peak[i_snap],
                       M_68_lo[i_snap],
                       M_68_hi[i_snap]);
     fclose(fp_out);

     // Clean-up
     for(int i_snap=0;i_snap<n_snaps;i_snap++)
        SID_free(SID_FARG M_hist[i_snap]);
     SID_free(SID_FARG M_hist);
     SID_free(SID_FARG n_i);
     SID_free(SID_FARG M_peak);
     SID_free(SID_FARG M_68_lo);
     SID_free(SID_FARG M_68_hi);
     SID_free(SID_FARG i_z_track);
     SID_free(SID_FARG idx_track);
     SID_free(SID_FARG M_track);
     SID_free(SID_FARG x_track);
     SID_free(SID_FARG y_track);
     SID_free(SID_FARG z_track);
     SID_free(SID_FARG vx_track);
     SID_free(SID_FARG vy_track);
     SID_free(SID_FARG vz_track);
     SID_free(SID_FARG snap_list);
     SID_free(SID_FARG z_list);
     SID_free(SID_FARG t_list);
     SID_log("Done.",SID_LOG_CLOSE);
  } // if I_am_Master
  SID_Barrier(SID.COMM_WORLD);

  SID_log("Done.",SID_LOG_CLOSE);
}
Пример #24
0
void read_matches(char    *filename_in_dir,
                  int      i_read_in,
                  int      j_read_in,
                  int      n_halos_max,
                  int      mode,
                  int     *n_groups_i,
                  int     *n_groups_j,
                  int     *n_particles_i_in,
                  int     *n_particles_j_in,
                  int     *n_sub_group_i_in,
                  int     *n_sub_group_j_in,
                  int     *match_ids,
                  float   *match_score,
                  size_t  *match_index,
                  char    *match_flag_two_way,
                  int      flag_reject_bad_matches){
   char   group_text_prefix[5];
   char   filename_in[MAX_FILENAME_LENGTH];
   SID_fp fp_in;
   int    k_read;
   int    l_read;
   int    n_search;
   int    n_matches;
   size_t offset;
   int    flag_continue;
   int    i_read_file;
   int    j_read_file;
   int    n_groups_file;
   int    n_groups_file_1;
   int    n_groups_file_2;
   int    n_groups;
   int    n_groups_i_file;
   int    n_groups_j_file;
   int   *n_sub_group_i;
   int   *n_sub_group_j;
   int    flag_alloc_n_sub_i=FALSE;
   int    flag_alloc_n_sub_j=FALSE;
   
   if(i_read_in==j_read_in)
     SID_trap_error("i_read=j_read in read_matches",ERROR_LOGIC);

   switch(mode){
      case MATCH_SUBGROUPS:
      sprintf(group_text_prefix,"sub");
      break;
      case MATCH_GROUPS:
      sprintf(group_text_prefix,"");
      // We need n_subgroups arrays for removal of bad groups
      //    if they have not been passed to us.
      if(n_sub_group_i_in==NULL){
         flag_alloc_n_sub_i=TRUE;
         n_sub_group_i     =(int *)SID_malloc(sizeof(int)*n_halos_max);
      }
      else
         n_sub_group_i=n_sub_group_i_in;
      if(n_sub_group_j_in==NULL){
         flag_alloc_n_sub_j=TRUE;
         n_sub_group_j     =(int *)SID_malloc(sizeof(int)*n_halos_max);
      }
      else
         n_sub_group_j=n_sub_group_j_in;
      break;
   }

   // Since we need the particle counts for the goodness of match criterion,
   //   create temporary arrays in case we weren't passed an array for them.
   int *n_particles_i=n_particles_i_in;
   int *n_particles_j=n_particles_j_in;
   // 1) We always need n_particles_i
   int  flag_alloc_n_particles_i=FALSE;
   int  flag_alloc_n_particles_j=FALSE;
   if(n_particles_i==NULL)
      flag_alloc_n_particles_i=TRUE;
   if(n_particles_j==NULL)
      flag_alloc_n_particles_j=TRUE;
   // 2) We need n_particles_j if doing 2-way checks
   if(match_flag_two_way!=NULL && n_particles_j==NULL)
      flag_alloc_n_particles_j=TRUE;

   // Read the needed info from the header file
   int  i_read;
   int  i_read_start;
   int  i_read_stop;
   int  n_search_total;
   int  n_files;
   int  n_groups_in;
   int  counter=0;
   char filename_in_name[256];
   strcpy(filename_in_name,filename_in_dir);
   strip_path(filename_in_name);
   sprintf(filename_in,"%s/%sgroup_matches_header.dat",filename_in_dir,group_text_prefix);
   SID_fopen(filename_in,"r",&fp_in);
   SID_fread(&i_read_start,  sizeof(int),1,&fp_in);
   SID_fread(&i_read_stop,   sizeof(int),1,&fp_in);
   SID_fread(&n_search_total,sizeof(int),1,&fp_in);
   SID_fread(&n_files,       sizeof(int),1,&fp_in);
   for(i_read=i_read_stop;i_read>=i_read_start && counter<2;i_read--){
      SID_fread(&i_read_file, sizeof(int),1,&fp_in);
      if(i_read_file==i_read_in){
         SID_fread(n_groups_i,sizeof(int),1,&fp_in);
         if((*n_groups_i)>0){
            // Create a temporary array for n_particles_i if we were not passed one
            if(flag_alloc_n_particles_i)
               n_particles_i=(int *)SID_malloc(sizeof(int)*(*n_groups_i));
            if(n_particles_i!=NULL)
               SID_fread_ordered(n_particles_i,sizeof(int),(size_t)(*n_groups_i),&fp_in);
            else
               SID_fskip(sizeof(int),(*n_groups_i),&fp_in);
            if(mode==MATCH_GROUPS){
               if(n_sub_group_i!=NULL)
                  SID_fread_ordered(n_sub_group_i,sizeof(int),(size_t)(*n_groups_i),&fp_in);
               else
                  SID_fskip(sizeof(int),(*n_groups_i),&fp_in);
            }
         }
         counter++;
      }
      else if(i_read_file==j_read_in){
         SID_fread(n_groups_j,sizeof(int),1,&fp_in);
         if((*n_groups_j)>0){
            if(flag_alloc_n_particles_j)
               n_particles_j=(int *)SID_malloc(sizeof(int)*(*n_groups_j));
            if(n_particles_j!=NULL)
               SID_fread_ordered(n_particles_j,sizeof(int),(size_t)(*n_groups_j),&fp_in);
            else
               SID_fskip(sizeof(int),(*n_groups_j),&fp_in);
            if(mode==MATCH_GROUPS){
               if(n_sub_group_j!=NULL)
                  SID_fread_ordered(n_sub_group_j,sizeof(int),(size_t)(*n_groups_j),&fp_in);
               else
                  SID_fskip(sizeof(int),(*n_groups_j),&fp_in);
            }
         }
         counter++;
      }
      else{
         SID_fread(&n_groups_in,sizeof(int),1,&fp_in);
         if(n_groups_in>0){
            SID_fskip(sizeof(int),n_groups_in,&fp_in);
            if(mode==MATCH_GROUPS)
               SID_fskip(sizeof(int),n_groups_in,&fp_in);
         }
      }
   }
   SID_fclose(&fp_in);

   // Read the matching file
   char filename_cat1[256];
   char filename_cat2[256];
   char filename_in_dir_snap[256];
   sprintf(filename_cat1,"%03d",i_read_in);
   sprintf(filename_cat2,"%03d",j_read_in);
   sprintf(filename_in_dir_snap,"%s/%s",filename_in_dir,filename_cat1);
   if(filename_in_dir!=NULL)
      sprintf(filename_in,"%s/%sgroup_matches_%s_%s.dat",filename_in_dir_snap,group_text_prefix,filename_cat1,filename_cat2);
   else
      sprintf(filename_in,"%s_%sgroup_matches_%s_%s.dat",filename_in_name,    group_text_prefix,filename_cat1,filename_cat2);

   SID_fopen(filename_in,"r",&fp_in);
   SID_fread(&i_read_file,sizeof(int),1,&fp_in);
   SID_fread(&j_read_file,sizeof(int),1,&fp_in);
   SID_fread(n_groups_i,  sizeof(int),1,&fp_in);
   SID_fread(n_groups_j,  sizeof(int),1,&fp_in);

   // Read matching data
   SID_fread(match_ids,  sizeof(int),   (*n_groups_i),&fp_in);
   SID_fread(match_index,sizeof(size_t),(*n_groups_i),&fp_in);
   SID_fread(match_score,sizeof(float), (*n_groups_i),&fp_in);
   SID_fclose(&fp_in);

   // If one of the catalogs is empty, set to no-match defaults
   if((*n_groups_i)<=0 || (*n_groups_j)<=0){
      for(int i_halo=0;i_halo<(*n_groups_i);i_halo++){
         match_ids[i_halo]  =-1;
         match_score[i_halo]= 0.;
      }
      if(match_flag_two_way!=NULL){
         for(int i_halo=0;i_halo<(*n_groups_i);i_halo++)
            match_flag_two_way[i_halo]=FALSE;
      }
   }
   else{
      // If we are reading groups, nullify all matches
      //    between halos with no substructures.
      int i_halo;
      if(mode==MATCH_GROUPS){
         size_t *match_index_temp;
         for(i_halo=0;i_halo<(*n_groups_i);i_halo++){
            if(n_sub_group_i[i_halo]<=0){
               match_ids[i_halo]  =-1;
               match_score[i_halo]= 0.;
            }
            else if(match_ids[i_halo]>=0 && (*n_groups_j)>0){
               if(n_sub_group_j[match_ids[i_halo]]<=0){
                  match_ids[i_halo]  =-1;
                  match_score[i_halo]= 0.;
               }
            }
         }
         merge_sort(match_ids,(size_t)(*n_groups_i),&match_index_temp,SID_INT,SORT_COMPUTE_INDEX,SORT_COMPUTE_NOT_INPLACE);
         memcpy(match_index,match_index_temp,(*n_groups_i)*sizeof(size_t));
         SID_free(SID_FARG match_index_temp);
      }

      // Apply a goodness-of-fit criterion and check that the maximum allowed score has not been exceeded
      for(i_halo=0;i_halo<(*n_groups_i);i_halo++){
         if(match_ids[i_halo]>=0){
            if(flag_reject_bad_matches && !check_validity_of_match(n_particles_i[i_halo],match_score[i_halo]))
               match_ids[i_halo]=-1;
         }
      }

      // Since we may have changed some matches with the goodness 
      //    of fit criterion, we need to re-perform the sort
      size_t *match_index_temp=NULL;
      merge_sort(match_ids,(size_t)(*n_groups_i),&match_index_temp,SID_INT,SORT_COMPUTE_INDEX,SORT_COMPUTE_NOT_INPLACE);
      memcpy(match_index,match_index_temp,(*n_groups_i)*sizeof(size_t));
      SID_free(SID_FARG match_index_temp);

      // Determine if the matches are two-way if we have been asked to check this
      if(match_flag_two_way!=NULL && (*n_groups_i)>0){

         // We're going to need the particle counts in the target catalog for checking the goodness of return matches.  Make sure we have them.
         if(n_particles_j==NULL)
            SID_trap_error("Target catalog halo sizes are not defined in read_matches() but are needed for checking two-way match flags.",ERROR_LOGIC);

         // Flip the file names for reading the return matches
         sprintf(filename_in_dir_snap,"%s/%s",filename_in_dir,filename_cat2);
         if(filename_in_dir!=NULL)
            sprintf(filename_in,"%s/%sgroup_matches_%s_%s.dat",filename_in_dir_snap,group_text_prefix,filename_cat2,filename_cat1);
         else
            sprintf(filename_in,"%s_%sgroup_matches_%s_%s.dat",filename_in_name,    group_text_prefix,filename_cat2,filename_cat1);

         // Open two files, one for reading the IDs and one for matching the scores of the matching file
         int i_read_file_check;
         int j_read_file_check;
         int n_groups_i_check;
         int n_groups_j_check;
         SID_fp fp_check_ids;
         SID_fp fp_check_score;
         SID_fopen(filename_in,"r",&fp_check_ids);
         SID_fopen(filename_in,"r",&fp_check_score);
         SID_fread(&j_read_file_check,sizeof(int),1,&fp_check_ids);
         SID_fread(&i_read_file_check,sizeof(int),1,&fp_check_ids);
         SID_fread(&n_groups_j_check, sizeof(int),1,&fp_check_ids);
         SID_fread(&n_groups_i_check, sizeof(int),1,&fp_check_ids);

         // Check that we have the right files
         if(n_groups_i_check!=(*n_groups_i))
            SID_trap_error("Source halo counts don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,n_groups_i_check,(*n_groups_i));
         if(n_groups_j_check!=(*n_groups_j))
            SID_trap_error("Target halo counts don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,n_groups_j_check,(*n_groups_j));
         if(i_read_file_check!=i_read_file)
            SID_trap_error("Source file numbers don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,i_read_file_check,i_read_file);
         if(j_read_file_check!=j_read_file)
            SID_trap_error("Target file numbers don't match (ie. %d!=%d) in two-way check in read_matches().",ERROR_LOGIC,j_read_file_check,j_read_file);

         // Skip to the beginning of the relevant block for the score-reading file pointer
         SID_fskip(sizeof(int),   4,            &fp_check_score); // header
         SID_fskip(sizeof(int),   (*n_groups_j),&fp_check_score); // ids
         SID_fskip(sizeof(size_t),(*n_groups_j),&fp_check_score); // indices

         // Set everything to being a one-way match unless subsequently changed
         for(i_halo=0;i_halo<(*n_groups_i);i_halo++) 
            match_flag_two_way[i_halo]=FALSE;

         // Read matching data in buffered chunks 
         int     n_good      =0;
         int     n_2way      =0;
         int     n_buffer    =1024*1024;
         int     n_chunk     =0;
         int     n_remaining =(*n_groups_j);
         int    *buffer_ids  =(int   *)SID_malloc(sizeof(int)  *n_buffer);
         float  *buffer_score=(float *)SID_malloc(sizeof(float)*n_buffer);
         for(int j_halo=0;n_remaining>0;n_remaining-=n_chunk){
            n_chunk=MIN(n_remaining,n_buffer);
            SID_fread(buffer_ids,  sizeof(int),  n_chunk,&fp_check_ids);
            SID_fread(buffer_score,sizeof(float),n_chunk,&fp_check_score);
            for(int k_halo=0;k_halo<n_chunk;k_halo++,j_halo++){
               int id_i=buffer_ids[k_halo];
               if(id_i>=0){
                  if(id_i>=(*n_groups_i))
                     SID_trap_error("Allowed matching index has been exceeded i(ie %d>=%d) while determining two-way match flags.",
                                    ERROR_LOGIC,id_i,(*n_groups_i));
                  // Do this check first to avoid having to check if both needed n_particles_* references are defined
                  int id_j=match_ids[id_i];
                  if(id_j==j_halo){
                     if(!flag_reject_bad_matches || check_validity_of_match(n_particles_j[j_halo],buffer_score[k_halo])){
                        match_flag_two_way[id_i]=TRUE; 
                        n_2way++;
                     }
                     n_good++;
                  }
               }
            }
         }
         //SID_log("n_good=%d n_2way=%d",SID_LOG_COMMENT,n_good,n_2way);
         SID_fclose(&fp_check_ids);
         SID_fclose(&fp_check_score);
         SID_free(SID_FARG buffer_ids);
         SID_free(SID_FARG buffer_score);
      }
   }

   // If any of these arrays are temporary, free them.
   if(flag_alloc_n_sub_i)
      SID_free(SID_FARG n_sub_group_i);
   if(flag_alloc_n_sub_j)
      SID_free(SID_FARG n_sub_group_j);
   if(flag_alloc_n_particles_i)
      SID_free(SID_FARG n_particles_i);
   if(flag_alloc_n_particles_j)
      SID_free(SID_FARG n_particles_j);
}
Пример #25
0
int main(int argc, char *argv[]) {
    int    n_search;
    int    i_halo;
    char   filename_in[SID_MAX_FILENAME_LENGTH];
    char   group_text_prefix[4];
    int    n_files;
    int    k_read;
    int    l_read;
    int *  n_particles_i;
    int *  n_particles_j;
    int    j_read;
    int    mode;
    int    j_halo;
    int    i_read;
    int    i_read_start;
    int    i_read_stop;
    SID_fp fp_in;

    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    char filename_SSimPL_root[SID_MAX_FILENAME_LENGTH];
    strcpy(filename_SSimPL_root, argv[1]);
    SID_log("Checking the integrity of the match files for {%s}...", SID_LOG_OPEN | SID_LOG_TIMER, filename_SSimPL_root);
    int *n_groups    = NULL;
    int *n_subgroups = NULL;
    for(int i_type = 0; i_type < 2; i_type++) {
        // Convert filename_root to filename
        switch(i_type) {
            case 0:
                mode = MATCH_SUBGROUPS;
                sprintf(group_text_prefix, "sub");
                break;
            case 1:
                mode = MATCH_GROUPS;
                sprintf(group_text_prefix, "");
                break;
        }
        SID_log("Processing %sgroups...", SID_LOG_OPEN | SID_LOG_TIMER, group_text_prefix);

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

        // Read halo sizes from header file
        SID_log("Processing header file...", SID_LOG_OPEN | SID_LOG_TIMER);
        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_fread(&i_read_stop, sizeof(int), 1, &fp_in);
        SID_fread(&n_search, sizeof(int), 1, &fp_in);
        SID_fread(&n_files, sizeof(int), 1, &fp_in);
        int *n_halos = NULL;
        switch(mode) {
            case MATCH_SUBGROUPS:
                n_subgroups = (int *)SID_malloc(sizeof(int) * n_files);
                n_halos     = n_subgroups;
                break;
            case MATCH_GROUPS:
                n_groups = (int *)SID_malloc(sizeof(int) * n_files);
                n_halos  = n_groups;
                break;
        }
        if(mode == MATCH_GROUPS)
            SID_log("Halo counts (snap/No. groups/No. subgroups):", SID_LOG_OPEN);
        for(k_read = 0; k_read < n_files; k_read++) {
            SID_fread(&l_read, sizeof(int), 1, &fp_in);
            SID_fread(&(n_halos[k_read]), sizeof(int), 1, &fp_in);
            SID_fskip(sizeof(int), n_halos[k_read], &fp_in);
            if(mode == MATCH_GROUPS) {
                int *n_subgroups_group = (int *)SID_malloc(sizeof(int) * n_halos[k_read]);
                SID_fread(n_subgroups_group, sizeof(int), n_halos[k_read], &fp_in);
                int n_subgroups_test = 0;
                for(int i_test = 0; i_test < n_halos[k_read]; i_test++)
                    n_subgroups_test += n_subgroups_group[i_test];
                if(n_subgroups[k_read] != n_subgroups_test)
                    SID_log("Error in %s header: l_read=%3d k_read=%3d n_subgroups: %d!=%d\n",
                            SID_LOG_COMMENT,
                            l_read,
                            k_read,
                            n_subgroups[k_read],
                            n_subgroups_test);
                SID_free(SID_FARG n_subgroups_group);
            }
            if(mode == MATCH_GROUPS)
                SID_log("%03d %d %d", SID_LOG_COMMENT, k_read, n_groups[k_read], n_subgroups[k_read]);
        }
        if(mode == MATCH_GROUPS)
            SID_log("", SID_LOG_CLOSE | SID_LOG_NOPRINT);
        SID_fclose(&fp_in);
        SID_log("Done.", SID_LOG_CLOSE);

        SID_log("Processing match files...", SID_LOG_OPEN | SID_LOG_TIMER);
        for(int i_read = i_read_start; i_read < i_read_stop; i_read++) {
            for(int j_read = GBP_MAX(0, i_read - n_search); j_read < GBP_MIN(i_read_stop, i_read + n_search); j_read++) {
                if(i_read != j_read) {
                    sprintf(filename_in, "%s/%03d/%sgroup_matches_%03d_%03d.dat", filename_root_in, i_read, group_text_prefix, i_read, j_read);
                    SID_log("Processing {%s}...", SID_LOG_OPEN, filename_in);

                    // Read header information
                    int i_read_in;
                    int j_read_in;
                    int n_groups_i;
                    int n_groups_j;
                    SID_fopen(filename_in, "r", &fp_in);
                    SID_fread(&i_read_in, sizeof(int), 1, &fp_in);
                    SID_fread(&j_read_in, sizeof(int), 1, &fp_in);
                    SID_fread(&n_groups_i, sizeof(int), 1, &fp_in);
                    SID_fread(&n_groups_j, sizeof(int), 1, &fp_in);

                    if(i_read_in != i_read || j_read_in != j_read || n_groups_i != n_halos[n_files - i_read_in - 1] ||
                       n_groups_j != n_halos[n_files - j_read_in - 1])
                        SID_log("Error in matching file: i_read=%3d j_read=%3d n_i_in=%d n_i=%d n_j_in=%d n_j=%d\n",
                                SID_LOG_COMMENT,
                                i_read,
                                j_read,
                                n_groups_i,
                                n_halos[n_files - i_read_in - 1],
                                n_groups_j,
                                n_halos[n_files - j_read_in - 1]);

                    // Read matches
                    int match;
                    for(k_read = 0; k_read < n_groups_i; k_read++)
                        SID_fread(&match, sizeof(int), 1, &fp_in);

                    // Read indices
                    size_t indices;
                    for(k_read = 0; k_read < n_groups_i; k_read++)
                        SID_fread(&indices, sizeof(size_t), 1, &fp_in);

                    // Read scores
                    float score;
                    for(k_read = 0; k_read < n_groups_i; k_read++)
                        SID_fread(&score, sizeof(float), 1, &fp_in);

                    // Close file
                    SID_fclose(&fp_in);

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

        SID_log("Done.", SID_LOG_CLOSE);
    }
    SID_free(SID_FARG n_groups);
    SID_free(SID_FARG n_subgroups);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
Пример #26
0
int read_matches_header(char *filename_root_in,
                        int   i_read_start,
                        int   i_read_stop,
                        int   i_read_step,
                        int * n_files_return,
                        int **n_subgroups_return,
                        int **n_groups_return,
                        int * n_subgroups_max,
                        int * n_groups_max,
                        int * n_halos_max) {
    char       filename_out[256];
    char       filename_out_dir[256];
    char       filename_out_name[256];
    FILE *     fp_test;
    FILE *     fp_out;
    SID_fp     fp_in;
    int        i_read, k_read, l_read;
    int        i_read_start_file;
    int        i_read_stop_file;
    int        i_read_step_file;
    int        k_match;
    int        n_k_match;
    char       group_text_prefix[5];
    int        n_matches;
    int        j_read;
    char       filename_cat1[256];
    char       filename_cat2[256];
    char       filename_cat1_order[256];
    char       filename_cat2_order[256];
    char       filename_out_dir_snap[256];
    int        n_groups_1;
    int        n_groups_1_local;
    int        n_groups_2;
    int        n_groups_2_local;
    int        i_group;
    int        buffered_count;
    int        buffered_count_local;
    int        j_group;
    int        index_test;
    int        i_rank;
    int *      n_particles;
    int *      n_sub_group;
    int *      match_id    = NULL;
    float *    match_score = NULL;
    char       cat_name_1[20];
    char       cat_name_2[20];
    size_t *   match_rank  = NULL;
    size_t *   match_index = NULL;
    size_t     offset;
    plist_info plist1;
    plist_info plist2;
    void *     buffer;
    int *      buffer_int;
    size_t *   buffer_size_t;
    float *    buffer_float;
    int        n_buffer_max = 1000;
    int        n_buffer;
    int        i_buffer;
    int        j_buffer;
    int        flag_sucessful_completion = GBP_TRUE;

    SID_log("Reading header information...", SID_LOG_OPEN);

    // Count the number of snapshots we are going to use and
    //    initialize the arrays that are to be returned
    for(i_read = i_read_stop, (*n_files_return) = 0; i_read >= i_read_start; i_read -= i_read_step)
        (*n_files_return)++;
    (*n_subgroups_return) = (int *)SID_malloc(sizeof(int) * (*n_files_return));
    (*n_groups_return)    = (int *)SID_malloc(sizeof(int) * (*n_files_return));

    if(SID.I_am_Master) {
        FILE *fp_read_header;
        // Loop for subgroups and then groups
        for(k_match = 0; k_match < 2; k_match++) {
            switch(k_match) {
                case 0:
                    sprintf(group_text_prefix, "sub");
                    break;
                case 1:
                    sprintf(group_text_prefix, "");
                    break;
            }

            // Open file and read header
            int i_read_start_in;
            int i_read_stop_in;
            int n_search_total_in;
            int n_files_in;
            int i_read_in;
            sprintf(filename_out, "%s/%sgroup_matches_header.dat", filename_root_in, group_text_prefix);
            if((fp_read_header = fopen(filename_out, "r")) == NULL)
                SID_exit_error("Could not open file {%s} when reading header information.", SID_ERROR_IO_OPEN,
                               filename_out);
            SID_fread_verify(&i_read_start_in, sizeof(int), 1, fp_read_header);
            SID_fread_verify(&i_read_stop_in, sizeof(int), 1, fp_read_header);
            SID_fread_verify(&n_search_total_in, sizeof(int), 1, fp_read_header);
            SID_fread_verify(&n_files_in, sizeof(int), 1, fp_read_header);
            i_read_in = i_read_stop_in + 1;

            // Loop for each snapshot we want to keep
            int i_read_next;
            for(j_read = 0, i_read_next = i_read_stop; j_read < (*n_files_return); j_read++, i_read_next -= i_read_step) {
                // Read-forward to the desired snapshot
                int flag_continue = GBP_TRUE;
                while(flag_continue && i_read_in > i_read_start_in) {
                    SID_fread_verify(&i_read_in, sizeof(int), 1, fp_read_header);
                    SID_fread_verify(&n_groups_1, sizeof(int), 1, fp_read_header);
                    fseek(fp_read_header, n_groups_1 * sizeof(int), SEEK_CUR); // Skip halo sizes
                    if(k_match == 1)
                        fseek(fp_read_header, n_groups_1 * sizeof(int), SEEK_CUR); // Skip n_sub_per_group
                    if(i_read_in == i_read_next) {
                        switch(k_match) {
                            case 0:
                                (*n_subgroups_return)[j_read] = n_groups_1;
                                break;
                            case 1:
                                (*n_groups_return)[j_read] = n_groups_1;
                                break;
                        }
                        flag_continue = GBP_FALSE;
                    }
                }
            }
            fclose(fp_read_header);
            if(j_read != (*n_files_return))
                SID_exit_error("Was not able to read the appriate number of group/subgroup sizes (i.e. %d!=%d)",
                               SID_ERROR_LOGIC, j_read, (*n_files_return));
        }
    }
    SID_Bcast((*n_subgroups_return), (*n_files_return), SID_INT, SID_MASTER_RANK, SID_COMM_WORLD);
    SID_Bcast((*n_groups_return), (*n_files_return), SID_INT, SID_MASTER_RANK, SID_COMM_WORLD);

    // Compute some maxs (useful for array allocation)
    calc_max((*n_subgroups_return), n_subgroups_max, (*n_files_return), SID_INT, CALC_MODE_DEFAULT);
    calc_max((*n_groups_return), n_groups_max, (*n_files_return), SID_INT, CALC_MODE_DEFAULT);
    (*n_halos_max) = GBP_MAX((*n_subgroups_max), (*n_groups_max));

    SID_log("Done.", SID_LOG_CLOSE);

    return (flag_sucessful_completion);
}
Пример #27
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);
}
Пример #28
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);
}
Пример #29
0
void SID_cat_files(const char *filename_out,
                   int   mode,
                   int   n_files, ...){
  int      i_file;
  char    *filename_in;
  va_list  vargs;
  FILE    *fp_in;
  FILE    *fp_out;
  int      r_val;
  int      flag_clean;
  struct   stat file_stats;
  size_t   n_bytes;
  size_t   n_bytes_buffer;
  void    *buffer;

  va_start(vargs,n_files);

  SID_log("Concatinating %d files to output {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,n_files,filename_out);

  // Interpret mode
  if(check_mode_for_flag(mode,SID_CAT_CLEAN)) flag_clean=TRUE;
  else                                        flag_clean=FALSE;

  // Open output file
  fp_out=fopen(filename_out,"w");
  if(fp_out==NULL)
    SID_trap_error("Could not open file {%s}!",ERROR_IO_OPEN,filename_out);
  buffer=SID_malloc(IO_BUFFER_SIZE);

  // Loop over the files to be concatinated...
  for(i_file=0;i_file<n_files;i_file++){
    // Open next file and get file size
    filename_in=(char *)va_arg(vargs,char *);
    r_val      =stat(filename_in,&file_stats);
    if(r_val!=0)
      SID_trap_error("Could not open file {%s}!",ERROR_IO_OPEN,filename_in);
    else
      SID_log("Processing {%s}...",SID_LOG_OPEN,filename_in);
    n_bytes=file_stats.st_size;
    fp_in  =fopen(filename_in,"r");

    // Copy this input file to the output file in chunks ...
    n_bytes_buffer=MIN(n_bytes,IO_BUFFER_SIZE);
    while(n_bytes_buffer>0){
      // Read
      r_val=fread(buffer,
                  1,
                  n_bytes_buffer,
                  fp_in);
      // Write
      r_val=fwrite(buffer,
                   1,
                   n_bytes_buffer,
                   fp_out);
      // Adjust buffer size
      n_bytes-=n_bytes_buffer;
      n_bytes_buffer=MIN(n_bytes,IO_BUFFER_SIZE);
    }

    // Close input file and remove it if asked to
    fclose(fp_in);
    if(flag_clean)
      remove(filename_in);

    SID_log("Done.",SID_LOG_CLOSE);
  }

  // Clean-up
  fclose(fp_out);
  SID_free(SID_FARG buffer);

  SID_log("Done.",SID_LOG_CLOSE);

  va_end(vargs);
}
Пример #30
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);
}