Example #1
0
void ADaPS_deallocate(ADaPS **remove) {
    if((*remove)->free_function != NULL)
        ((*remove)->free_function)(SID_FARG(*remove)->data, (*remove)->free_function_params);
    else
        SID_free(SID_FARG(*remove)->data);
    if((*remove)->free_function_params != NULL)
        SID_free(SID_FARG(*remove)->free_function_params);
    SID_free(SID_FARG(*remove));
}
Example #2
0
void free_mark_arguments(mark_arg_info **argument) {
    while((*argument) != NULL) {
        mark_arg_info *next = (*argument)->next;
        SID_free(SID_FARG(*argument));
        (*argument) = next;
    }
}
Example #3
0
void free_trees_vertical(tree_vertical_info **tree) {
    tree_vertical_node_info *next_node;
    tree_vertical_node_info *last_node;
    // Free nodes
    next_node = (*tree)->root;
    while(next_node != NULL) {
        last_node = next_node;
        next_node = last_node->next;
        SID_free(SID_FARG last_node);
    }
    // Free neighbour list arrays
    SID_free(SID_FARG(*tree)->n_neighbours);
    SID_free(SID_FARG(*tree)->neighbour_halos);
    SID_free(SID_FARG(*tree)->neighbour_halo_last);
    SID_free(SID_FARG(*tree));
}
Example #4
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);
  }
}
Example #5
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);
}
Example #6
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));
}
int free_precompute_treenode_markers(tree_info *trees,int mode){
   // Set-up to work with groups or subgroups
   tree_markers_info **markers;
   if(check_mode_for_flag(mode,PRECOMPUTE_TREENODE_MARKER_GROUPS))
      markers=trees->group_markers;
   else if(check_mode_for_flag(mode,PRECOMPUTE_TREENODE_MARKER_SUBGROUPS))
      markers=trees->subgroup_markers;
   else
      SID_trap_error("Neither group nor subgroup mode is set in init_treenode_markers_all() when one is needed.",ERROR_LOGIC);

   // Perform deallocation
   if(markers!=NULL){
      for(int i_snap=0;i_snap<trees->n_snaps;i_snap++)
         SID_free(SID_FARG markers[i_snap]);
   }
   SID_free(SID_FARG markers);

   // Process reference trees if present
   if(trees->trees_reference!=NULL)
      free_precompute_treenode_markers(trees->trees_reference,mode);
}
Example #8
0
int count_lines_parameters(FILE *fp) {
    int    n_lines = 0;
    char * line    = NULL;
    size_t n       = 0;
    int    r;
    while(!feof(fp)) {
        r = getline(&line, &n, fp);
        if(r > 0 && !check_parameter(line))
            n_lines++;
    }
    SID_free(SID_FARG line);
    rewind(fp);
    return (n_lines);
}
Example #9
0
void free_MCMC_covariance(MCMC_info *MCMC){
  int           i_DS;
  MCMC_DS_info *current_DS;
  MCMC_DS_info *next_DS;

  if(MCMC->n_M!=NULL){
    SID_log("Freeing MCMC covariance matrix...",SID_LOG_OPEN);

    SID_free(SID_FARG MCMC->V);
    if(MCMC->m!=NULL){
      gsl_matrix_free(MCMC->m);
      MCMC->m=NULL;
    }
    if(MCMC->b!=NULL){
      gsl_vector_free(MCMC->b);
      MCMC->b=NULL;
    }

    SID_log("Done.",SID_LOG_CLOSE);
  }
}
Example #10
0
void free_trees(tree_info **trees){

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

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

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

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

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

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

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

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

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

  SID_log("Done.",SID_LOG_CLOSE);
}
Example #11
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();
}
Example #12
0
void free_treenode_hist(treenode_hist_info **hist){
  SID_free  (SID_FARG (*hist)->array);
  SID_free  (SID_FARG (*hist));
}
Example #13
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);
}
Example #14
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);
}
Example #15
0
void free_pspec(pspec_info *pspec){
  SID_log("Freeing power spectrum...",SID_LOG_OPEN);
  free_cosmo(&(pspec->cosmo));
  free_field(&(pspec->FFT));
  SID_free(SID_FARG (pspec->k_1D));
  SID_free(SID_FARG (pspec->n_modes_1D));
  SID_free(SID_FARG (pspec->n_modes_2D));
  int i_run;
  for(i_run=0;i_run<4;i_run++){
      SID_free(SID_FARG (pspec->P_k_1D[i_run]));
      SID_free(SID_FARG (pspec->dP_k_1D[i_run]));
      SID_free(SID_FARG (pspec->P_k_2D[i_run]));
      SID_free(SID_FARG (pspec->dP_k_2D[i_run]));
  }
  SID_free(SID_FARG pspec->P_k_1D);
  SID_free(SID_FARG pspec->dP_k_1D);
  SID_free(SID_FARG pspec->P_k_2D);
  SID_free(SID_FARG pspec->dP_k_2D);
  SID_log("Done.",SID_LOG_CLOSE);
}
Example #16
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();
}
Example #17
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);
}
Example #18
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);
}
Example #19
0
void free_treenode_list(treenode_list_info **list) {
    SID_free(SID_FARG(*list)->list);
    ADaPS_free(SID_FARG(*list)->data);
    SID_free(SID_FARG(*list));
}
Example #20
0
void free_camera(camera_info **camera) {
    SID_log("Freeing camera...", SID_LOG_OPEN);
    free_perspective(&((*camera)->perspective));
    SID_free(SID_FARG(*camera)->mask_RGB);
    SID_free(SID_FARG(*camera)->mask_Y);
    SID_free(SID_FARG(*camera)->mask_RGBY);
    SID_free(SID_FARG(*camera)->mask_RGBY_MARKED);
    for(int i_depth = 0; i_depth < (*camera)->n_depth_alloc; i_depth++) {
        if((*camera)->image_RGB != NULL)
            free_image(&((*camera)->image_RGB[i_depth]));
        if((*camera)->image_RGB_left != NULL)
            free_image(&((*camera)->image_RGB_left[i_depth]));
        if((*camera)->image_RGB_right != NULL)
            free_image(&((*camera)->image_RGB_right[i_depth]));
        if((*camera)->image_Y != NULL)
            free_image(&((*camera)->image_Y[i_depth]));
        if((*camera)->image_Y_left != NULL)
            free_image(&((*camera)->image_Y_left[i_depth]));
        if((*camera)->image_Y_right != NULL)
            free_image(&((*camera)->image_Y_right[i_depth]));
        if((*camera)->image_RGBY != NULL)
            free_image(&((*camera)->image_RGBY[i_depth]));
        if((*camera)->image_RGBY_left != NULL)
            free_image(&((*camera)->image_RGBY_left[i_depth]));
        if((*camera)->image_RGBY_right != NULL)
            free_image(&((*camera)->image_RGBY_right[i_depth]));
        if((*camera)->image_RY != NULL)
            free_image(&((*camera)->image_RY[i_depth]));
        if((*camera)->image_RY_left != NULL)
            free_image(&((*camera)->image_RY_left[i_depth]));
        if((*camera)->image_RY_right != NULL)
            free_image(&((*camera)->image_RY_right[i_depth]));
        if((*camera)->image_GY != NULL)
            free_image(&((*camera)->image_GY[i_depth]));
        if((*camera)->image_GY_left != NULL)
            free_image(&((*camera)->image_GY_left[i_depth]));
        if((*camera)->image_GY_right != NULL)
            free_image(&((*camera)->image_GY_right[i_depth]));
        if((*camera)->image_BY != NULL)
            free_image(&((*camera)->image_BY[i_depth]));
        if((*camera)->image_BY_left != NULL)
            free_image(&((*camera)->image_BY_left[i_depth]));
        if((*camera)->image_BY_right != NULL)
            free_image(&((*camera)->image_BY_right[i_depth]));
        if((*camera)->image_RGBY_MARKED != NULL)
            free_image(&((*camera)->image_RGBY_MARKED[i_depth]));
        if((*camera)->image_RGBY_MARKED_left != NULL)
            free_image(&((*camera)->image_RGBY_MARKED_left[i_depth]));
        if((*camera)->image_RGBY_MARKED_right != NULL)
            free_image(&((*camera)->image_RGBY_MARKED_right[i_depth]));
    }
    SID_free(SID_FARG(*camera)->image_RGB);
    SID_free(SID_FARG(*camera)->image_RGB_left);
    SID_free(SID_FARG(*camera)->image_RGB_right);
    SID_free(SID_FARG(*camera)->image_Y);
    SID_free(SID_FARG(*camera)->image_Y_left);
    SID_free(SID_FARG(*camera)->image_Y_right);
    SID_free(SID_FARG(*camera)->image_RGBY);
    SID_free(SID_FARG(*camera)->image_RGBY_left);
    SID_free(SID_FARG(*camera)->image_RGBY_right);
    SID_free(SID_FARG(*camera)->image_RY);
    SID_free(SID_FARG(*camera)->image_RY_left);
    SID_free(SID_FARG(*camera)->image_RY_right);
    SID_free(SID_FARG(*camera)->image_GY);
    SID_free(SID_FARG(*camera)->image_GY_left);
    SID_free(SID_FARG(*camera)->image_GY_right);
    SID_free(SID_FARG(*camera)->image_BY);
    SID_free(SID_FARG(*camera)->image_BY_left);
    SID_free(SID_FARG(*camera)->image_BY_right);
    SID_free(SID_FARG(*camera)->image_RGBY_MARKED);
    SID_free(SID_FARG(*camera)->image_RGBY_MARKED_left);
    SID_free(SID_FARG(*camera)->image_RGBY_MARKED_right);
    SID_free(SID_FARG(*camera)->mask_RGB_left);
    SID_free(SID_FARG(*camera)->mask_Y_left);
    SID_free(SID_FARG(*camera)->mask_RGBY_left);
    SID_free(SID_FARG(*camera)->mask_RGBY_MARKED_left);
    SID_free(SID_FARG(*camera)->mask_RGB_right);
    SID_free(SID_FARG(*camera)->mask_Y_right);
    SID_free(SID_FARG(*camera)->mask_RGBY_right);
    SID_free(SID_FARG(*camera)->mask_RGBY_MARKED_right);
    if((*camera)->RGB_gamma != NULL)
        free_interpolate(SID_FARG(*camera)->RGB_gamma, NULL);
    if((*camera)->transfer_list != NULL)
        ADaPS_free(SID_FARG(*camera)->transfer_list);
    if((*camera)->Y_gamma != NULL)
        free_interpolate(SID_FARG(*camera)->Y_gamma, NULL);

    // Free camera depth information
    free_camera_depths(*camera);

    SID_free((void **)camera);
    SID_log("Done.", SID_LOG_CLOSE);
}
Example #21
0
void free_MCMC(MCMC_info *MCMC) {
    int           i_P, i_DS;
    int           i_array;
    MCMC_DS_info *current_DS;
    MCMC_DS_info *next_DS;

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

    // Parameter arrays
    for(i_P = 0; i_P < MCMC->n_P; i_P++)
        SID_free(SID_FARG MCMC->P_names[i_P]);
    SID_free(SID_FARG MCMC->P_names);
    SID_free(SID_FARG MCMC->P_init);
    SID_free(SID_FARG MCMC->P_new);
    SID_free(SID_FARG MCMC->P_last);
    SID_free(SID_FARG MCMC->P_chain);
    SID_free(SID_FARG MCMC->P_limit_min);
    SID_free(SID_FARG MCMC->P_limit_max);
    if(MCMC->n_arrays > 0) {
        for(i_array = 0; i_array < MCMC->n_arrays; i_array++) {
            SID_free(SID_FARG MCMC->array[i_array]);
            SID_free(SID_FARG MCMC->array_name[i_array]);
        }
        SID_free(SID_FARG MCMC->array);
        SID_free(SID_FARG MCMC->array_name);
    }

    // Covariance and displacement vector
    free_MCMC_covariance(MCMC);

    // Random number generator
    if(MCMC->RNG != NULL)
        free_RNG(MCMC->RNG);

    // Dataset arrays
    free_MCMC_arrays(MCMC);
    free_MCMC_DS(MCMC);

    // Communicators
    SID_Comm_free(&(MCMC->comm));

    SID_log("Done.", SID_LOG_CLOSE);
}
Example #22
0
void map_to_grid(size_t      n_particles_local,
                 GBPREAL *   x_particles_local,
                 GBPREAL *   y_particles_local,
                 GBPREAL *   z_particles_local,
                 GBPREAL *   v_particles_local,
                 GBPREAL *   w_particles_local,
                 cosmo_info *cosmo,
                 double      redshift,
                 int         distribution_scheme,
                 double      normalization_constant,
                 field_info *field,
                 field_info *field_norm,
                 int         mode) {
    size_t       i_p;
    int          i_k;
    size_t       i_b;
    size_t       i_grid;
    int          i_coord;
    int          i_i[3];
    int          j_i[3];
    int          k_i[3];
    size_t       n_particles;
    double       v_p;
    double       w_p;
    int          flag_valued_particles;
    int          flag_weight_particles;
    int          flag_weight;
    int          flag_active;
    int          flag_viable;
    double       k_mag;
    double       dk;
    int          n_powspec;
    int          mode_powspec;
    size_t *     n_mode_powspec;
    double *     k_powspec;
    double *     kmin_powspec;
    double *     kmax_powspec;
    double *     k_powspec_bin;
    double *     P_powspec;
    double *     dP_powspec;
    double       k_min;
    double       k_max;
    double       norm_local;
    double       normalization;
    GBPREAL      x_i;
    GBPREAL      x_particle_i;
    GBPREAL      y_particle_i;
    GBPREAL      z_particle_i;
    double       kernal_offset;
    int          W_search_lo;
    int          W_search_hi;
    size_t       receive_left_size  = 0;
    size_t       receive_right_size = 0;
    size_t       index_best;
    int          n_buffer[3];
    size_t       n_send_left;
    size_t       n_send_right;
    size_t       send_size_left;
    size_t       send_size_right;
    GBPREAL *    send_left          = NULL;
    GBPREAL *    send_right         = NULL;
    GBPREAL *    receive_left       = NULL;
    GBPREAL *    receive_right      = NULL;
    GBPREAL *    send_left_norm     = NULL;
    GBPREAL *    send_right_norm    = NULL;
    GBPREAL *    receive_left_norm  = NULL;
    GBPREAL *    receive_right_norm = NULL;
    double       r_i, r_min, r_i_max = 0;
    double       W_i;
    int          index_i;
    interp_info *P_k_interp;
    double *     r_Daub;
    double *     W_Daub;
    double       h_Hubble;
    int          n_Daub;
    interp_info *W_r_Daub_interp = NULL;
    int          i_rank;
    size_t       buffer_index;
    int          i_test;
    double       accumulator;

    // Compute the total poulation size and print a status message
    calc_sum_global(&n_particles_local, &n_particles, 1, SID_SIZE_T, CALC_MODE_DEFAULT, SID_COMM_WORLD);
    SID_log("Distributing %zu items onto a %dx%dx%d grid...", SID_LOG_OPEN, n_particles, field->n[0], field->n[1], field->n[2]);

    // If we've been given a normalization field, make sure it's got the same geometry as the results field
    if(field_norm != NULL) {
        if(field->n_d != field_norm->n_d)
            SID_exit_error("grid dimension counts don't match (ie. %d!=%d)", SID_ERROR_LOGIC, field->n_d,
                           field_norm->n_d);
        int i_d;
        for(i_d = 0; i_d < field->n_d; i_d++) {
            if(field->n[i_d] != field_norm->n[i_d])
                SID_exit_error("grid dimension No. %d's sizes don't match (ie. %d!=%d)", SID_ERROR_LOGIC, i_d,
                               field->n[i_d], field_norm->n[i_d]);
            if(field->n_R_local[i_d] != field_norm->n_R_local[i_d])
                SID_exit_error("grid dimension No. %d's slab sizes don't match (ie. %d!=%d)", SID_ERROR_LOGIC, i_d,
                               field->n_R_local[i_d], field_norm->n_R_local[i_d]);
            if(field->i_R_start_local[i_d] != field_norm->i_R_start_local[i_d])
                SID_exit_error("grid dimension No. %d's start positions don't match (ie. %le!=%le)", SID_ERROR_LOGIC,
                               i_d, field->i_R_start_local[i_d], field_norm->i_R_start_local[i_d]);
            if(field->i_R_stop_local[i_d] != field_norm->i_R_stop_local[i_d])
                SID_exit_error("grid dimension No. %d's stop positions don't match (ie. %le!=%le)", SID_ERROR_LOGIC,
                               i_d, field->i_R_stop_local[i_d], field_norm->i_R_stop_local[i_d]);
        }
        if(field->n_field != field_norm->n_field)
            SID_exit_error("grid field sizes don't match (ie. %d!=%d)", SID_ERROR_LOGIC, field->n_field,
                           field_norm->n_field);
        if(field->n_field_R_local != field_norm->n_field_R_local)
            SID_exit_error("grid local field sizes don't match (ie. %d!=%d)", SID_ERROR_LOGIC, field->n_field_R_local,
                           field_norm->n_field_R_local);
        if(field->total_local_size != field_norm->total_local_size)
            SID_exit_error("grid total local sizes don't match (ie. %d!=%d)", SID_ERROR_LOGIC, field->total_local_size,
                           field_norm->total_local_size);
    }

    // Set some variables
    if(v_particles_local != NULL)
        flag_valued_particles = GBP_TRUE;
    else {
        flag_valued_particles = GBP_FALSE;
        v_p                   = 1.;
    }
    if(w_particles_local != NULL)
        flag_weight_particles = GBP_TRUE;
    else {
        flag_weight_particles = GBP_FALSE;
        w_p                   = 1.;
    }
    h_Hubble = ((double *)ADaPS_fetch(cosmo, "h_Hubble"))[0];

    // Initializing the mass assignment scheme
    switch(distribution_scheme) {
        case MAP2GRID_DIST_DWT20:
            W_search_lo   = 2;
            W_search_hi   = 7;
            kernal_offset = 2.5;
            compute_Daubechies_scaling_fctns(20, 5, &r_Daub, &W_Daub, &n_Daub);
            init_interpolate(r_Daub, W_Daub, n_Daub, gsl_interp_cspline, &W_r_Daub_interp);
            SID_free(SID_FARG r_Daub);
            SID_free(SID_FARG W_Daub);
            SID_log("(using D20 scale function kernal)...", SID_LOG_CONTINUE);
            break;
        case MAP2GRID_DIST_DWT12:
            W_search_lo   = 1;
            W_search_hi   = 6;
            kernal_offset = 1.75;
            compute_Daubechies_scaling_fctns(12, 5, &r_Daub, &W_Daub, &n_Daub);
            init_interpolate(r_Daub, W_Daub, (size_t)n_Daub, gsl_interp_cspline, &W_r_Daub_interp);
            SID_free(SID_FARG r_Daub);
            SID_free(SID_FARG W_Daub);
            SID_log("(using D12 scale function kernal)...", SID_LOG_CONTINUE);
            break;
        case MAP2GRID_DIST_TSC:
            W_search_lo = 2;
            W_search_hi = 2;
            SID_log("(using triangular shaped function kernal)...", SID_LOG_CONTINUE);
            break;
        case MAP2GRID_DIST_CIC:
            SID_log("(using cloud-in-cell kernal)...", SID_LOG_CONTINUE);
        case MAP2GRID_DIST_NGP:
        default:
            W_search_lo = 1;
            W_search_hi = 1;
            SID_log("(using nearest grid point kernal)...", SID_LOG_CONTINUE);
            break;
    }

    // Initializing slab buffers
    n_send_left     = (size_t)(field->n[0] * field->n[1] * W_search_lo);
    n_send_right    = (size_t)(field->n[0] * field->n[1] * W_search_hi);
    send_size_left  = n_send_left * sizeof(GBPREAL);
    send_size_right = n_send_right * sizeof(GBPREAL);
    send_left       = (GBPREAL *)SID_calloc(send_size_left);
    send_right      = (GBPREAL *)SID_calloc(send_size_right);
    receive_left    = (GBPREAL *)SID_calloc(send_size_right);
    receive_right   = (GBPREAL *)SID_calloc(send_size_left);
    if(field_norm != NULL) {
        send_left_norm     = (GBPREAL *)SID_calloc(send_size_left);
        send_right_norm    = (GBPREAL *)SID_calloc(send_size_right);
        receive_left_norm  = (GBPREAL *)SID_calloc(send_size_right);
        receive_right_norm = (GBPREAL *)SID_calloc(send_size_left);
    }

    // Clear the field
    if(!SID_CHECK_BITFIELD_SWITCH(mode, MAP2GRID_MODE_NOCLEAN)) {
        SID_log("Clearing fields...", SID_LOG_OPEN);
        clear_field(field);
        if(field_norm != NULL)
            clear_field(field);
        SID_log("Done.", SID_LOG_CLOSE);
    }

    // It is essential that we not pad the field for the simple way that we add-in the boundary buffers below
    set_FFT_padding_state(field, GBP_FALSE);
    if(field_norm != NULL)
        set_FFT_padding_state(field_norm, GBP_FALSE);

    // Create the mass distribution
    SID_log("Performing grid assignment...", SID_LOG_OPEN | SID_LOG_TIMER);

    // Loop over all the objects
    pcounter_info pcounter;
    SID_Init_pcounter(&pcounter, n_particles_local, 10);
    for(i_p = 0, norm_local = 0.; i_p < n_particles_local; i_p++) {
        double norm_i;
        double value_i;
        if(flag_valued_particles)
            v_p = (double)(v_particles_local[i_p]);
        if(flag_weight_particles)
            w_p = (double)(w_particles_local[i_p]);
        norm_i  = w_p;
        value_i = v_p * norm_i;

        // Particle's position
        x_particle_i = (GBPREAL)x_particles_local[i_p];
        y_particle_i = (GBPREAL)y_particles_local[i_p];
        z_particle_i = (GBPREAL)z_particles_local[i_p];

        // Quantize it onto the grid
        x_particle_i /= (GBPREAL)field->dR[0];
        y_particle_i /= (GBPREAL)field->dR[1];
        z_particle_i /= (GBPREAL)field->dR[2];
        i_i[0] = (int)x_particle_i; // position in grid-coordinates
        i_i[1] = (int)y_particle_i; // position in grid-coordinates
        i_i[2] = (int)z_particle_i; // position in grid-coordinates

        // Apply the kernel
        flag_viable = GBP_TRUE;
        double x_i_effective;
        for(j_i[0] = -W_search_lo; j_i[0] <= W_search_hi; j_i[0]++) {
            for(j_i[1] = -W_search_lo; j_i[1] <= W_search_hi; j_i[1]++) {
                for(j_i[2] = -W_search_lo; j_i[2] <= W_search_hi; j_i[2]++) {
                    // Compute distance to each grid point being searched against ...
                    flag_active = GBP_TRUE;
                    for(i_coord = 0, W_i = 1.; i_coord < 3; i_coord++) {
                        switch(i_coord) {
                            case 0:
                                x_i = (GBPREAL)(i_i[0] + j_i[0]) - x_particle_i;
                                break;
                            case 1:
                                x_i = (GBPREAL)(i_i[1] + j_i[1]) - y_particle_i;
                                break;
                            case 2:
                                x_i = (GBPREAL)(i_i[2] + j_i[2]) - z_particle_i;
                                break;
                        }
                        switch(distribution_scheme) {
                                // Distribute with a Daubechies wavelet transform of 12th or 20th order a la Cui et al '08
                            case MAP2GRID_DIST_DWT12:
                            case MAP2GRID_DIST_DWT20:
                                x_i_effective = x_i + kernal_offset;
                                if(x_i_effective > 0.)
                                    W_i *= interpolate(W_r_Daub_interp, x_i_effective);
                                else
                                    flag_active = GBP_FALSE;
                                break;
                                // Distribute using the triangular shaped cloud (TSC) method
                            case MAP2GRID_DIST_TSC:
                                if(x_i < 0.5)
                                    W_i *= (0.75 - x_i * x_i);
                                else if(x_i < 1.5)
                                    W_i *= 0.5 * (1.5 - fabs(x_i)) * (1.5 - fabs(x_i));
                                else
                                    flag_active = GBP_FALSE;
                                break;
                                // Distribute using the cloud-in-cell (CIC) method
                            case MAP2GRID_DIST_CIC:
                                if(fabs(x_i) < 1.)
                                    W_i *= (1. - fabs(x_i));
                                else
                                    flag_active = GBP_FALSE;
                                break;
                                // Distribute using "nearest grid point" (NGP; ie. the simplest and default) method
                            case MAP2GRID_DIST_NGP:
                            default:
                                if(fabs(x_i) <= 0.5 && flag_viable)
                                    W_i *= 1.;
                                else
                                    flag_active = GBP_FALSE;
                                break;
                        }
                    }
                    if(flag_active) { // This flags-out regions of the kernal with no support to save some time
                        // Set the grid indices (enforce periodic BCs; do x-coordinate last) ...
                        //   ... y-coordinate ...
                        k_i[1] = (i_i[1] + j_i[1]);
                        if(k_i[1] < 0)
                            k_i[1] += field->n[1];
                        else
                            k_i[1] = k_i[1] % field->n[1];
                        //   ... z-coordinate ...
                        k_i[2] = i_i[2] + j_i[2];
                        if(k_i[2] < 0)
                            k_i[2] += field->n[2];
                        else
                            k_i[2] = k_i[2] % field->n[2];
                        //   ... x-coordinate ...
                        //     Depending on x-index, add contribution to the
                        //     local array or to the slab buffers.
                        k_i[0] = (i_i[0] + j_i[0]);
                        if(k_i[0] < field->i_R_start_local[0]) {
                            k_i[0] -= (field->i_R_start_local[0] - W_search_lo);
                            if(k_i[0] < 0)
                                SID_exit_error("Left slab buffer limit exceeded by %d element(s).", SID_ERROR_LOGIC,
                                               -k_i[0]);
                            send_left[index_FFT_R(field, k_i)] += W_i * value_i;
                            if(field_norm != NULL)
                                send_left_norm[index_FFT_R(field_norm, k_i)] += W_i * norm_i;
                        } else if(k_i[0] > field->i_R_stop_local[0]) {
                            k_i[0] -= (field->i_R_stop_local[0] + 1);
                            if(k_i[0] >= W_search_hi)
                                SID_exit_error("Right slab buffer limit exceeded by %d element(s).", SID_ERROR_LOGIC,
                                               k_i[0] - W_search_hi + 1);
                            else {
                                send_right[index_FFT_R(field, k_i)] += W_i * value_i;
                                if(field_norm != NULL)
                                    send_right_norm[index_FFT_R(field_norm, k_i)] += W_i * norm_i;
                            }
                        } else {
                            field->field_local[index_local_FFT_R(field, k_i)] += W_i * value_i;
                            if(field_norm != NULL)
                                field_norm->field_local[index_local_FFT_R(field_norm, k_i)] += W_i * norm_i;
                        }
                        flag_viable = GBP_FALSE;
                    }
                }
            }
        }
        // Report the calculation's progress
        SID_check_pcounter(&pcounter, i_p);
    }
    SID_log("Done.", SID_LOG_CLOSE);

    // Perform exchange of slab buffers and add them to the local mass distribution.
    //    Note: it's important that the FFT field not be padded (see above, where
    //          this is set) for this to work the way it's done.
    SID_log("Adding-in the slab buffers...", SID_LOG_OPEN | SID_LOG_TIMER);
    // Numerator first ...
    exchange_slab_buffer_left(send_left, send_size_left, receive_right, &receive_right_size, &(field->slab));
    exchange_slab_buffer_right(send_right, send_size_right, receive_left, &receive_left_size, &(field->slab));
    for(i_b = 0; i_b < n_send_right; i_b++)
        field->field_local[i_b] += receive_left[i_b];
    for(i_b = 0; i_b < n_send_left; i_b++)
        field->field_local[field->n_field_R_local - n_send_left + i_b] += receive_right[i_b];
    // ... then denominator (if it's being used)
    if(field_norm != NULL) {
        exchange_slab_buffer_left(send_left_norm, send_size_left, receive_right_norm, &receive_right_size, &(field_norm->slab));
        exchange_slab_buffer_right(send_right_norm, send_size_right, receive_left_norm, &receive_left_size, &(field_norm->slab));
        for(i_b = 0; i_b < n_send_right; i_b++)
            field_norm->field_local[i_b] += receive_left_norm[i_b];
        for(i_b = 0; i_b < n_send_left; i_b++)
            field_norm->field_local[field_norm->n_field_R_local - n_send_left + i_b] += receive_right[i_b];
    }
    SID_free(SID_FARG send_left);
    SID_free(SID_FARG send_right);
    SID_free(SID_FARG receive_left);
    SID_free(SID_FARG receive_right);
    if(field_norm != NULL) {
        SID_free(SID_FARG send_left_norm);
        SID_free(SID_FARG send_right_norm);
        SID_free(SID_FARG receive_left_norm);
        SID_free(SID_FARG receive_right_norm);
    }
    SID_log("Done.", SID_LOG_CLOSE);

    // Recompute local normalization (more accurate for large sample sizes)
    if(!SID_CHECK_BITFIELD_SWITCH(mode, MAP2GRID_MODE_NONORM)) {
        SID_log("Applying normalization...", SID_LOG_OPEN);
        if(field_norm != NULL) {
            for(i_grid = 0; i_grid < field->n_field_R_local; i_grid++) {
                if(field_norm->field_local[i_grid] != 0)
                    field->field_local[i_grid] /= field_norm->field_local[i_grid];
            }
        }
        if(SID_CHECK_BITFIELD_SWITCH(mode, MAP2GRID_MODE_APPLYFACTOR)) {
            for(i_grid = 0; i_grid < field->n_field_R_local; i_grid++)
                field->field_local[i_grid] *= normalization_constant;
        }
        if(SID_CHECK_BITFIELD_SWITCH(mode, MAP2GRID_MODE_FORCENORM)) {
            norm_local = 0;
            for(i_grid = 0; i_grid < field->n_field_R_local; i_grid++)
                norm_local += (double)field->field_local[i_grid];
            calc_sum_global(&norm_local, &normalization, 1, SID_DOUBLE, CALC_MODE_DEFAULT, SID_COMM_WORLD);
            double normalization_factor;
            normalization_factor = normalization_constant / normalization;
            for(i_grid = 0; i_grid < field->n_field_R_local; i_grid++)
                field->field_local[i_grid] *= normalization_factor;
        }
        SID_log("Done.", SID_LOG_CLOSE, normalization);
    }

    if(W_r_Daub_interp != NULL)
        free_interpolate(SID_FARG W_r_Daub_interp, NULL);

    SID_log("Done.", SID_LOG_CLOSE);
}
Example #23
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);
}
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);
}
Example #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();
}
Example #26
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);
}
Example #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);
}
Example #28
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);
}
Example #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);
}
Example #30
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);
}