void rm_MCMC_directory(MCMC_info *MCMC) { char command_text[2 * SID_MAX_FILENAME_LENGTH]; SID_log("Removing MCMC directory {%s}...", SID_LOG_OPEN, MCMC->filename_output_dir); sprintf(command_text, "rm -rf %s > /dev/null", MCMC->filename_output_dir); system(command_text); SID_log("Done.", SID_LOG_CLOSE); }
void compute_FFT(field_info *FFT) { // Add the FFTW padding if we need to. The log message // is only needed if we do add a buffer. int flag_log_message = GBP_FALSE; if(add_buffer_FFT_R(FFT)) { SID_log("Performing FFT...", SID_LOG_OPEN | SID_LOG_TIMER); flag_log_message = GBP_TRUE; } // Perform the FFT #if FFTW_V2 #if USE_MPI rfftwnd_mpi(FFT->plan, 1, FFT->field_local, NULL, FFTW_TRANSPOSED_ORDER); #else rfftwnd_one_real_to_complex(FFT->plan, FFT->field_local, NULL); #endif #else #ifdef USE_DOUBLE fftw_execute((const fftw_plan)FFT->plan); #else fftwf_execute((const fftwf_plan)FFT->plan); #endif #endif if(flag_log_message) SID_log("Done.", SID_LOG_CLOSE); }
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); } }
int main(int argc, char *argv[]){ SID_init(&argc,&argv,NULL,NULL); // Fetch user inputs char filename_SSimPL_dir[MAX_FILENAME_LENGTH]; char filename_halo_version_root[MAX_FILENAME_LENGTH]; char filename_trees_name[MAX_FILENAME_LENGTH]; char filename_output_root[MAX_FILENAME_LENGTH]; strcpy(filename_SSimPL_dir, argv[1]); strcpy(filename_halo_version_root,argv[2]); strcpy(filename_trees_name, argv[3]); strcpy(filename_output_root, argv[4]); // Set the halo and tree filename roots char filename_trees_root[MAX_FILENAME_LENGTH]; char filename_halos_root[MAX_FILENAME_LENGTH]; sprintf(filename_trees_root,"%s/trees/%s",filename_SSimPL_dir,filename_trees_name); sprintf(filename_halos_root,"%s/halos/%s",filename_SSimPL_dir,filename_halo_version_root); SID_log("Generating treenode markers & analysis of merger trees...",SID_LOG_OPEN|SID_LOG_TIMER); // Perform analysis tree_info *trees; read_trees(filename_SSimPL_dir, filename_halo_version_root, filename_trees_name, TREE_MODE_DEFAULT, &trees); // Read catalogs read_trees_catalogs(trees, filename_SSimPL_dir, filename_halo_version_root, READ_TREES_CATALOGS_BOTH); // Loop over the two halo types for(int i_type=0;i_type<2;i_type++){ // Initialize markers (just one-at-a-time to save RAM) int mode; if(i_type==0) mode=PRECOMPUTE_TREENODE_MARKER_GROUPS; else mode=PRECOMPUTE_TREENODE_MARKER_SUBGROUPS; // Compute markers precompute_treenode_markers(trees,mode); // Write markers write_treenode_markers(trees,filename_output_root,mode); // Free markers free_precompute_treenode_markers(trees,mode); } // Clean-up free_trees(&trees); SID_log("Done.",SID_LOG_CLOSE); SID_exit(ERROR_NONE); }
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); }
int main(int argc, char *argv[]) { SID_Init(&argc, &argv, NULL); // Fetch user inputs char filename_SSimPL_dir[SID_MAX_FILENAME_LENGTH]; char filename_halo_version_root[SID_MAX_FILENAME_LENGTH]; char filename_trees_root[SID_MAX_FILENAME_LENGTH]; char filename_trees_reference_root[SID_MAX_FILENAME_LENGTH]; char filename_trees_name[SID_MAX_FILENAME_LENGTH]; char filename_trees_reference_name[SID_MAX_FILENAME_LENGTH]; char filename_halos_root[SID_MAX_FILENAME_LENGTH]; int i_arg = 1; strcpy(filename_SSimPL_dir, argv[i_arg++]); strcpy(filename_halo_version_root, argv[i_arg++]); strcpy(filename_trees_name, argv[i_arg++]); if(argc == 8) strcpy(filename_trees_reference_name, argv[i_arg++]); else sprintf(filename_trees_reference_name, ""); // Set some filenames sprintf(filename_trees_root, "%s/trees/%s", filename_SSimPL_dir, filename_trees_name); sprintf(filename_trees_reference_root, "%s/trees/%s", filename_SSimPL_dir, filename_trees_reference_name); sprintf(filename_halos_root, "%s/halos/%s", filename_SSimPL_dir, filename_halo_version_root); SID_log("Performing analysis of merger trees...", SID_LOG_OPEN | SID_LOG_TIMER); // Read trees tree_info *trees; read_trees(filename_SSimPL_dir, filename_halo_version_root, filename_trees_name, TREE_MODE_DEFAULT | TREE_READ_EXTENDED_POINTERS, &trees); // Read reference trees if(strcmp(filename_trees_reference_name, "")) read_trees(filename_SSimPL_dir, filename_halo_version_root, filename_trees_reference_name, TREE_MODE_DEFAULT | TREE_READ_EXTENDED_POINTERS | TREE_MODE_REFERENCE, &(trees->trees_reference)); // Read catalogs. Use SHORT read to save RAM. read_trees_catalogs(trees, READ_TREES_CATALOGS_BOTH | READ_TREES_CATALOGS_SHORT); if(trees->trees_reference != NULL) read_trees_catalogs(trees->trees_reference, READ_TREES_CATALOGS_BOTH | READ_TREES_CATALOGS_SHORT); // read_trees_match_scores(trees, // filename_SSimPL_dir, // READ_TREES_MATCH_SCORES_ALL); // Perform analysis compute_trees_stats(trees); // Clean-up free_trees(&trees); SID_log("Done.", SID_LOG_CLOSE); SID_Finalize(); }
int main(int argc, char *argv[]) { SID_Init(&argc, &argv, NULL); // Fetch user inputs if(argc != 5) SID_exit_error("Invalid syntax.", SID_ERROR_SYNTAX); char filename_SSimPL_in[SID_MAX_FILENAME_LENGTH]; char filename_halo_type[SID_MAX_FILENAME_LENGTH]; strcpy(filename_SSimPL_in, argv[1]); strcpy(filename_halo_type, argv[2]); int i_snap_lo = atoi(argv[3]); int i_snap_hi = atoi(argv[4]); // Set which files will be processed int flag_process_halos = GBP_TRUE; int flag_process_catalogs = GBP_TRUE; int flag_process_grids = GBP_TRUE; int flag_process_snapshots = GBP_TRUE; // Loop over the given snapshot range SID_log("Processing group/subgroup statistics for files #%d->#%d...", SID_LOG_OPEN | SID_LOG_TIMER, i_snap_lo, i_snap_hi); for(int i_snap = i_snap_lo; i_snap <= i_snap_hi; i_snap++) { char filename_in[SID_MAX_FILENAME_LENGTH]; SID_log("Processing snapshot #%03d...", SID_LOG_OPEN | SID_LOG_TIMER, i_snap); // Process halos if(flag_process_halos) { sprintf(filename_in, "%s/halos/", filename_SSimPL_in); check_integrity_halos(filename_in, filename_halo_type, i_snap); } // Process catalogs if(flag_process_catalogs) { sprintf(filename_in, "%s/catalogs/", filename_SSimPL_in); check_integrity_catalogs(filename_in, filename_halo_type, i_snap); } // Process grids if(flag_process_grids) { sprintf(filename_in, "%s/grids/snapshot_%03d_dark_grid.dat", filename_SSimPL_in, i_snap); check_integrity_grids(filename_in); } // Process snapshots and their smooth files // if(flag_process_snapshots){ // int IDs_byte_size; // int i_region=-1; // if(check_integrity_snapshot(filename_SSimPL_in,i_region,i_snap,&IDs_byte_size)) // check_integrity_smooth(filename_SSimPL_in,i_region,i_snap,IDs_byte_size); // i_region++; // while(check_integrity_snapshot(filename_SSimPL_in,i_region,i_snap,&IDs_byte_size)){ // check_integrity_smooth(filename_SSimPL_in,i_region,i_snap,IDs_byte_size); // i_region++; // } //} SID_log("Done.", SID_LOG_CLOSE); } SID_log("Done.", SID_LOG_CLOSE); SID_Finalize(); }
void remove_buffer_FFT_R(field_info *FFT){ size_t i_FFT,index; if(!(FFT->flag_padded)){ SID_log("Removing FFTW padding...",SID_LOG_OPEN); for(i_FFT=0;i_FFT<FFT->n_field_R_local;i_FFT++){ index=pad_index_FFT_R(FFT,i_FFT); if(i_FFT!=index) FFT->field_local[i_FFT]=FFT->field_local[index]; } SID_log("Done.",SID_LOG_CLOSE); } }
void gbp_fetch_va_arg(gbp_va_list *vargs,size_t size,void *ptr){ size_t next_position=vargs->stream_position+size; if(next_position>=MAX_GBP_VA_ARGS_STREAM_SIZE) SID_log("gbp_va_args stream has been over-run.",ERROR_LOGIC); memcpy(ptr,&(vargs->stream[vargs->stream_position]),size); vargs->stream_position=next_position; }
void write_trend_property_binning_file(trend_property_info *property, const char *filename_output_root) { char filename_out[SID_MAX_FILENAME_LENGTH]; sprintf(filename_out, "%s_%s_bins.txt", filename_output_root, property->name); SID_log("Writing binning description file to {%s}...", SID_LOG_OPEN, filename_out); if(SID.I_am_Master) { FILE *fp_out = fopen(filename_out, "w"); int i_column = 1; fprintf(fp_out, "# Ordinate %s-binning for {%s}\n", property->name, filename_output_root); fprintf(fp_out, "# Column (%03d): Bin index\n", i_column++); fprintf(fp_out, "# (%03d): %s - lo\n", i_column++, property->name); fprintf(fp_out, "# (%03d): %s - hi\n", i_column++, property->name); hist_info *hist = property->hist; for(int i_bin = 0; i_bin < hist->n_bins; i_bin++) fprintf(fp_out, "%03d %le %le\n", i_bin, histogram_bin_x_lo(hist, i_bin), histogram_bin_x_hi(hist, i_bin)); fclose(fp_out); } SID_log("Done.", SID_LOG_CLOSE); }
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); }
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); }
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); } }
int main(int argc, char *argv[]) { SID_Init(&argc, &argv, NULL); // Fetch user inputs char filename_SSimPL_dir[SID_MAX_FILENAME_LENGTH]; char catalog_name[SID_MAX_FILENAME_LENGTH]; char filename_halo_version_root[SID_MAX_FILENAME_LENGTH]; char filename_trees_root[SID_MAX_FILENAME_LENGTH]; char filename_trees_name[SID_MAX_FILENAME_LENGTH]; char filename_halos_root[SID_MAX_FILENAME_LENGTH]; strcpy(filename_SSimPL_dir, argv[1]); strcpy(filename_halo_version_root, argv[2]); strcpy(filename_trees_name, argv[3]); strcpy(catalog_name, argv[4]); double z_obs = atof(argv[5]); double M_cut_lo = atof(argv[6]); double M_cut_hi = atof(argv[7]); int n_subgroups_track_max = atof(argv[8]); sprintf(filename_trees_root, "%s/trees/%s", filename_SSimPL_dir, filename_trees_name); sprintf(filename_halos_root, "%s/halos/%s", filename_SSimPL_dir, filename_halo_version_root); SID_log("Performing analysis of merger trees...", SID_LOG_OPEN | SID_LOG_TIMER); // Perform analysis tree_info *trees; read_trees(filename_SSimPL_dir, filename_halo_version_root, filename_trees_name, TREE_MODE_DEFAULT, &trees); // Read ancillary data read_trees_catalogs(trees, READ_TREES_CATALOGS_GROUPS | READ_TREES_CATALOGS_SUBGROUPS); // Generate catalogs and analyze analyze_halos_and_N_subhalos(trees, "./", catalog_name, z_obs, M_cut_lo, M_cut_hi, n_subgroups_track_max); // Clean-up free_trees(&trees); SID_log("Done.", SID_LOG_CLOSE); SID_Finalize(); }
void set_largest_descendants(tree_horizontal_info *halos_i, int n_halos_i){ SID_log("Setting largest descendants...",SID_LOG_OPEN|SID_LOG_TIMER); for(int i_halo=0;i_halo<n_halos_i;i_halo++){ if(halos_i[i_halo].descendant.halo!=NULL){ if(halos_i[i_halo].id==halos_i[i_halo].descendant.halo->id) // Inherit descendant's largest descendant. if(halos_i[i_halo].descendant.halo->n_particles_largest_descendant>halos_i[i_halo].n_particles) halos_i[i_halo].n_particles_largest_descendant=halos_i[i_halo].descendant.halo->n_particles_largest_descendant; // Current halo size is larger tha largest descendant. Use it. else halos_i[i_halo].n_particles_largest_descendant=halos_i[i_halo].n_particles; // Root of a new branch. Initialize to current halo size. else halos_i[i_halo].n_particles_largest_descendant=halos_i[i_halo].n_particles; } // Root of a strayed halo. Initialize to current halo size. else halos_i[i_halo].n_particles_largest_descendant=halos_i[i_halo].n_particles; } SID_log("Done.",SID_LOG_CLOSE); }
void set_MCMC_covariance(MCMC_info *MCMC, double *V) { int i_P, j_P; if(V == NULL) SID_log("Initializing the covariance matrix...", SID_LOG_OPEN); else SID_log("Updating the covariance matrix...", SID_LOG_OPEN); if(MCMC->V == NULL) MCMC->V = (double *)SID_malloc(sizeof(double) * MCMC->n_P * MCMC->n_P); if(MCMC->m == NULL) MCMC->m = gsl_matrix_calloc(MCMC->n_P, MCMC->n_P); if(MCMC->b == NULL) MCMC->b = gsl_vector_calloc(MCMC->n_P); if(V == NULL) { for(i_P = 0; i_P < MCMC->n_P; i_P++) { for(j_P = 0; j_P < MCMC->n_P; j_P++) { if(i_P == j_P) { // Matrix decomposition fails if we initialize an element to zero here. // Check for this and start with an different value if true. if(MCMC->P_init[i_P] == 0.) MCMC->V[i_P * MCMC->n_P + j_P] = 1e-3 * MCMC->P_limit_max[i_P]; else MCMC->V[i_P * MCMC->n_P + j_P] = pow(MCMC->P_init[i_P], 2.); } else MCMC->V[i_P * MCMC->n_P + j_P] = 0.; } } } else memcpy(MCMC->V, V, (size_t)(MCMC->n_P * MCMC->n_P) * sizeof(double)); for(i_P = 0; i_P < MCMC->n_P; i_P++) for(j_P = 0; j_P < MCMC->n_P; j_P++) gsl_matrix_set(MCMC->m, i_P, j_P, MCMC->V[i_P * MCMC->n_P + j_P]); gsl_linalg_cholesky_decomp(MCMC->m); // Set the upper-diagonal to zero to get L where covariance=L*L^T for(i_P = 0; i_P < MCMC->n_P; i_P++) for(j_P = i_P + 1; j_P < MCMC->n_P; j_P++) gsl_matrix_set(MCMC->m, i_P, j_P, 0.); SID_log("Done.", SID_LOG_CLOSE); }
void restart_MCMC(MCMC_info *MCMC){ SID_log("Resetting MCMC structure for a fresh run...",SID_LOG_OPEN); MCMC->flag_integrate_on =FALSE; MCMC->flag_analysis_on =TRUE; MCMC->first_map_call =TRUE; MCMC->first_link_call =TRUE; MCMC->flag_init_chain =TRUE; MCMC->first_chain_call =TRUE; MCMC->first_parameter_call =TRUE; MCMC->first_likelihood_call =TRUE; MCMC->ln_likelihood_last =0.; MCMC->ln_likelihood_new =0.; MCMC->ln_likelihood_chain =0.; MCMC->n_success =0; MCMC->n_propositions =0; MCMC->n_map_calls =0; free_MCMC_covariance(MCMC); free_MCMC_arrays(MCMC); SID_log("Done.",SID_LOG_CLOSE); }
int main(int argc, char *argv[]){ plist_info plist; int snapshot; char filename_in[256]; char filename_out[256]; SID_init(&argc,&argv,NULL,NULL); /**********************/ /* Parse command line */ /**********************/ if(argc!=2){ fprintf(stderr,"\n syntax: %s gadget_file\n",argv[0]); fprintf(stderr," ------\n\n"); return(ERROR_SYNTAX); } else{ strcpy(filename_in, argv[1]); snapshot=atoi(argv[2]); strcpy(filename_out,argv[3]); strcat(filename_out,".csv"); } SID_log("Converting GADGET file to .csv...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in,filename_out); /****************************************/ /* Read GADGET file into data structure */ /****************************************/ init_plist(&plist,NULL,GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY); SID_log("Reading GADGET file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in); read_gadget_binary(filename_in,snapshot,&plist,READ_GADGET_DEFAULT); SID_log("Done.",SID_LOG_CLOSE); /********************/ /* Write ascii file */ /********************/ SID_log("Writing .csv file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_out); write_gadget_csv(filename_out,&plist); SID_log("Done.",SID_LOG_CLOSE); /************/ /* Clean-up */ /************/ free_plist(&plist); SID_log("Done.",SID_LOG_CLOSE); return(ERROR_NONE); }
void finalize_trees_vertical(tree_info *trees) { SID_log("Finalizing...", SID_LOG_OPEN | SID_LOG_TIMER); // ... correct progenitor ordering ... SID_log("Correcting central halo masses...", SID_LOG_OPEN | SID_LOG_TIMER); for(int i_snap = 0; i_snap < trees->n_snaps; i_snap++) { tree_node_info *current_halo = trees->first_neighbour_subgroups[i_snap]; while(current_halo != NULL) { tree_node_info *current_parent = current_halo->parent_top; tree_node_info *current_central = current_parent->substructure_first; if(current_halo == current_central) { halo_properties_SAGE_info *current_halo_properties = &(trees->subgroup_properties_SAGE[current_halo->snap_tree][current_halo->neighbour_index]); halo_properties_SAGE_info *current_parent_properties = &(trees->group_properties_SAGE[current_parent->snap_tree][current_parent->neighbour_index]); current_halo_properties->M_vir = current_parent_properties->M_vir; } current_halo = current_halo->next_neighbour; } } SID_log("Done.", SID_LOG_CLOSE); // ... assign ids ... SID_log("Assigning IDs...", SID_LOG_OPEN | SID_LOG_TIMER); for(int i_snap = 0; i_snap < trees->n_snaps; i_snap++) { // Process group trees tree_node_info *current_halo = trees->first_neighbour_groups[i_snap]; while(current_halo != NULL) { assign_unique_vertical_tree_ids(trees, current_halo); current_halo = current_halo->next_neighbour; } // Process subgroup trees current_halo = trees->first_neighbour_subgroups[i_snap]; while(current_halo != NULL) { assign_unique_vertical_tree_ids(trees, current_halo); current_halo = current_halo->next_neighbour; } } SID_log("Done.", SID_LOG_CLOSE); SID_log("Done.", SID_LOG_CLOSE); }
void propagate_fragmented_info(tree_horizontal_extended_info **groups, int * n_groups, tree_horizontal_extended_info **subgroups, int * n_subgroups, int ** n_subgroups_group, int i_read, // tree snapshot index int j_read, // actual snapshot index int l_read, int i_read_step, int n_wrap) { SID_log("Propagating fragmented halo information for snapshot #%03d...", SID_LOG_OPEN, j_read); // Process groups int i_group; int i_subgroup; int j_subgroup; int flag_returned; for(i_group = 0, i_subgroup = 0; i_group < n_groups[l_read]; i_group++) { int group_id = groups[i_read % n_wrap][i_group].id; int group_tree_id = groups[i_read % n_wrap][i_group].tree_id; int group_descendant_id = groups[i_read % n_wrap][i_group].descendant_id; int group_type = groups[i_read % n_wrap][i_group].type; int group_file_offset = groups[i_read % n_wrap][i_group].descendant_file_offset; int group_index = groups[i_read % n_wrap][i_group].descendant_index; int group_score_desc = groups[i_read % n_wrap][i_group].score_desc; int group_score_prog = groups[i_read % n_wrap][i_group].score_prog; int group_snap_bridge = groups[i_read % n_wrap][i_group].snap_bridge; int group_file_bridge = groups[i_read % n_wrap][i_group].file_bridge; int group_index_bridge = groups[i_read % n_wrap][i_group].index_bridge; int group_id_bridge = groups[i_read % n_wrap][i_group].id_bridge; // Check if the group's descendant has returned to it's bridge (if fragmented) flag_returned = (group_file_bridge == (i_read + group_file_offset) && group_index_bridge == group_index); // Propagate type. Stop when the fragmented halo // returns to the main progenitor line of it's bridge. if(group_id == group_descendant_id && !flag_returned) { if(group_index >= 0) { // Important for strayed cases // Add the propagated type. if(SID_CHECK_BITFIELD_SWITCH(group_type, TREE_CASE_FRAGMENTED_STRAYED)) groups[(i_read + group_file_offset) % n_wrap][group_index].type |= TREE_CASE_FRAGMENTED_STRAYED; if(SID_CHECK_BITFIELD_SWITCH(group_type, TREE_CASE_FRAGMENTED_NORMAL)) groups[(i_read + group_file_offset) % n_wrap][group_index].type |= TREE_CASE_FRAGMENTED_NORMAL; if(SID_CHECK_BITFIELD_SWITCH(group_type, TREE_CASE_FRAGMENTED_OTHER)) groups[(i_read + group_file_offset) % n_wrap][group_index].type |= TREE_CASE_FRAGMENTED_OTHER; // Count the number of flags the descendant already has switched on int i_count = 0; if(SID_CHECK_BITFIELD_SWITCH(groups[(i_read + group_file_offset) % n_wrap][group_index].type, TREE_CASE_FRAGMENTED_STRAYED)) i_count++; if(SID_CHECK_BITFIELD_SWITCH(groups[(i_read + group_file_offset) % n_wrap][group_index].type, TREE_CASE_FRAGMENTED_NORMAL)) i_count++; if(SID_CHECK_BITFIELD_SWITCH(groups[(i_read + group_file_offset) % n_wrap][group_index].type, TREE_CASE_FRAGMENTED_OTHER)) i_count++; // Check that the affected halo does not have more than one fragmented halo flag turned on if(i_count > 1) SID_exit_error( "Multiple (%d) TREE_CASE_FRAGMENT switches present (type=%d) for i_snap/i_group=%d/%d when a max of one is " "alowed. Progenitor info: i_snap/i_halo/type=%d/%d/%d", SID_ERROR_LOGIC, i_count, groups[(i_read + group_file_offset) % n_wrap][group_index].type, j_read + group_file_offset * i_read_step, group_index, j_read, i_group, group_type); } } // Process subgroups for(j_subgroup = 0; j_subgroup < n_subgroups_group[i_read % n_wrap][i_group]; i_subgroup++, j_subgroup++) { int subgroup_id = subgroups[i_read % n_wrap][i_subgroup].id; int subgroup_tree_id = subgroups[i_read % n_wrap][i_subgroup].tree_id; int subgroup_descendant_id = subgroups[i_read % n_wrap][i_subgroup].descendant_id; int subgroup_type = subgroups[i_read % n_wrap][i_subgroup].type; int subgroup_score_desc = subgroups[i_read % n_wrap][i_subgroup].score_desc; int subgroup_score_prog = subgroups[i_read % n_wrap][i_subgroup].score_prog; int subgroup_snap_bridge = subgroups[i_read % n_wrap][i_subgroup].snap_bridge; int subgroup_file_bridge = subgroups[i_read % n_wrap][i_subgroup].file_bridge; int subgroup_index_bridge = subgroups[i_read % n_wrap][i_subgroup].index_bridge; int subgroup_id_bridge = subgroups[i_read % n_wrap][i_subgroup].id_bridge; int subgroup_file_offset = subgroups[i_read % n_wrap][i_subgroup].descendant_file_offset; int subgroup_index = subgroups[i_read % n_wrap][i_subgroup].descendant_index; // Check if the subgroup's descendant has returned to it's bridge (if fragmented) flag_returned = (subgroup_file_bridge == (i_read + subgroup_file_offset) && subgroup_index_bridge == subgroup_index); // Propagate type. Stop when the fragmented halo // returns to the main progenitor line of it's bridge. if(subgroup_id == subgroup_descendant_id && !flag_returned) { if(subgroup_index >= 0) { // Important for strayed cases // Add the propagated type. if(SID_CHECK_BITFIELD_SWITCH(subgroup_type, TREE_CASE_FRAGMENTED_STRAYED)) subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type |= TREE_CASE_FRAGMENTED_STRAYED; if(SID_CHECK_BITFIELD_SWITCH(subgroup_type, TREE_CASE_FRAGMENTED_NORMAL)) subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type |= TREE_CASE_FRAGMENTED_NORMAL; if(SID_CHECK_BITFIELD_SWITCH(subgroup_type, TREE_CASE_FRAGMENTED_OTHER)) subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type |= TREE_CASE_FRAGMENTED_OTHER; // Count the number of flags the descendant already has switched on int i_count = 0; if(SID_CHECK_BITFIELD_SWITCH( subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type, TREE_CASE_FRAGMENTED_STRAYED)) i_count++; if(SID_CHECK_BITFIELD_SWITCH( subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type, TREE_CASE_FRAGMENTED_NORMAL)) i_count++; if(SID_CHECK_BITFIELD_SWITCH( subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type, TREE_CASE_FRAGMENTED_OTHER)) i_count++; // Check that the affected halo does not have more than one fragmented halo flag turned on if(i_count > 1) SID_exit_error( "Multiple (%d) TREE_CASE_FRAGMENT switches present (type=%d) for i_snap/i_subgroup=%d/%d when a max of one is " "alowed. Progenitor info: i_snap/i_halo/type=%d/%d/%d", SID_ERROR_LOGIC, i_count, subgroups[(i_read + subgroup_file_offset) % n_wrap][subgroup_index].type, j_read + subgroup_file_offset * i_read_step, subgroup_index, j_read, i_subgroup, subgroup_type); } } } } SID_log("Done.", SID_LOG_CLOSE); }
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); }
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(); }
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(); }
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); }
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); }
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); }
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(¶meter_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(¶meter_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); }
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); }
void finalize_trees(tree_info *trees, int group_mode, int subgroup_mode) { SID_log("Finalizing...", SID_LOG_OPEN | SID_LOG_TIMER); // Parse mode int mode_substructure_order = TREE_SUBSTRUCTURE_ORDER_DEFAULT; // ... correct substructure ordering ... if(SID_CHECK_BITFIELD_SWITCH(mode_substructure_order, TREE_SUBSTRUCTURE_ORDER_DEFAULT)) SID_log("Assigning substructure ordering (by particle count)...", SID_LOG_OPEN | SID_LOG_TIMER); else SID_exit_error("Invalid substructure mode (%d).", SID_ERROR_LOGIC, mode_substructure_order); for(int i_snap = 0; i_snap < trees->n_snaps; i_snap++) { tree_node_info *current_group = trees->first_neighbour_groups[i_snap]; while(current_group != NULL) { compute_substructure_order_recursive(current_group, NULL, mode_substructure_order); current_group = current_group->next_neighbour; } } SID_log("Done.", SID_LOG_CLOSE); // ... build peak particle counts ... SID_log("Assigning peak particle counts...", SID_LOG_OPEN | SID_LOG_TIMER); for(int i_snap = 0; i_snap < trees->n_snaps; i_snap++) { // ... groups ... tree_node_info *current_group = trees->first_neighbour_groups[i_snap]; while(current_group != NULL) { if(current_group->descendant == NULL) compute_peak_particle_count_recursive(trees, current_group, NULL, NULL, NULL); current_group = current_group->next_neighbour; } // ... subgroups ... tree_node_info *current_subgroup = trees->first_neighbour_subgroups[i_snap]; while(current_subgroup != NULL) { if(current_subgroup->descendant == NULL) compute_peak_particle_count_recursive(trees, current_subgroup, NULL, NULL, NULL); current_subgroup = current_subgroup->next_neighbour; } } SID_log("Done.", SID_LOG_CLOSE); // ... correct progenitor ordering ... for(int i_type = 0; i_type < 2; i_type++) { char group_prefix_text[5]; tree_node_info **first_neighbours = NULL; int mode_progenitor_order = -1; switch(i_type) { case 0: mode_progenitor_order = group_mode; first_neighbours = trees->first_neighbour_groups; sprintf(group_prefix_text, ""); break; case 1: mode_progenitor_order = subgroup_mode; first_neighbours = trees->first_neighbour_subgroups; sprintf(group_prefix_text, "sub"); break; } if(SID_CHECK_BITFIELD_SWITCH(mode_progenitor_order, TREE_PROGENITOR_ORDER_DELUCIA)) SID_log("Assigning %sgroup progenitor ordering (Delucia)...", SID_LOG_OPEN | SID_LOG_TIMER, group_prefix_text); else if(SID_CHECK_BITFIELD_SWITCH(mode_progenitor_order, TREE_PROGENITOR_ORDER_N_PARTICLES)) SID_log("Assigning %sgroup progenitor ordering (by particle count)...", SID_LOG_OPEN | SID_LOG_TIMER, group_prefix_text); else if(SID_CHECK_BITFIELD_SWITCH(mode_progenitor_order, TREE_PROGENITOR_ORDER_N_PARTICLES_INCLUSIVE)) SID_log("Assigning %sgroup progenitor ordering (by inclusive particle count)...", SID_LOG_OPEN | SID_LOG_TIMER, group_prefix_text); else if(SID_CHECK_BITFIELD_SWITCH(mode_progenitor_order, TREE_PROGENITOR_ORDER_N_PARTICLES_PEAK)) SID_log("Assigning %sgroup progenitor ordering (by peak particle count)...", SID_LOG_OPEN | SID_LOG_TIMER, group_prefix_text); else if(SID_CHECK_BITFIELD_SWITCH(mode_progenitor_order, TREE_PROGENITOR_ORDER_N_PARTICLES_INCLUSIVE_PEAK)) SID_log("Assigning %sgroup progenitor ordering (by peak inclusive particle count)...", SID_LOG_OPEN | SID_LOG_TIMER, group_prefix_text); else SID_exit_error("Invalid progenitor mode (%d).", SID_ERROR_LOGIC, mode_progenitor_order); for(int i_snap = 0; i_snap < trees->n_snaps; i_snap++) { tree_node_info *current_halo = first_neighbours[i_snap]; while(current_halo != NULL) { if(current_halo->descendant == NULL) compute_progenitor_order_recursive(trees, current_halo, NULL, mode_progenitor_order); current_halo = current_halo->next_neighbour; } } SID_log("Done.", SID_LOG_CLOSE); } // ... assign depth-first-indices ... SID_log("Assigning depth first indices...", SID_LOG_OPEN | SID_LOG_TIMER); for(int i_type = 0; i_type < 2; i_type++) { tree_node_info **first_in_forest; int * n_halos_forest_local; char group_prefix_text[5]; switch(i_type) { case 0: first_in_forest = trees->first_in_forest_groups; n_halos_forest_local = trees->n_groups_forest_local; sprintf(group_prefix_text, ""); break; case 1: first_in_forest = trees->first_in_forest_subgroups; n_halos_forest_local = trees->n_subgroups_forest_local; sprintf(group_prefix_text, "sub"); break; } for(int i_forest = 0; i_forest < trees->n_forests_local; i_forest++) { int depth_first_index = 0; tree_node_info *current = first_in_forest[i_forest]; while(current != NULL) { if(current->descendant == NULL) assign_depth_first_index_recursive(current, &depth_first_index); current = current->next_in_forest; } if(depth_first_index != n_halos_forest_local[i_forest]) SID_exit_error("DFI != n_halos (i.e. %d!=%d) while processing %sgroups", SID_ERROR_LOGIC, depth_first_index, n_halos_forest_local[i_forest], group_prefix_text); } } SID_log("Done.", SID_LOG_CLOSE); SID_log("Done.", SID_LOG_CLOSE); }
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); }