int fread_multifile(fp_multifile_info *fp_in,void *data_out,int item_index){ int n_skip; int r_val=0; if(item_index<0||item_index>=fp_in->n_items_total) SID_trap_error("item_index (%d) is out of range (0->%d) in fread_multifile_file().",ERROR_LOGIC,item_index,fp_in->n_items_total-1); // Skip to the right place (if need-be) if(item_index!=fp_in->i_item || item_index>fp_in->i_item_stop || item_index<fp_in->i_item_start){ // We always have to scan forward, so if we're going backwards, we have to start from scratch if(item_index<fp_in->i_item) fopen_multifile_nth_file(fp_in,0); while(item_index>fp_in->i_item_stop) fopen_multifile_nth_file(fp_in,fp_in->i_file+1); n_skip=item_index-fp_in->i_item; if(n_skip>0) fseeko(fp_in->fp_multifile,(off_t)(fp_in->data_size*n_skip),SEEK_CUR); else if(n_skip<0) SID_trap_error("Negative skips (%d) not supported in fread_multifile_file().",ERROR_LOGIC,n_skip); fp_in->i_item+=n_skip; } // Read data fread_verify(data_out,fp_in->data_size,1,fp_in->fp_multifile); // Set counter fp_in->i_item++; return(r_val); }
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); } }
void force_periodic_double(double *coord,double min,double box_size){ int n=0; while((*coord)<min){ if(n>N_MAX_LOCAL) SID_trap_error("N_MAX reached in force_periodic_double. (1)",ERROR_LOGIC); (*coord)+=box_size; n++; } if(n==0){ while((*coord)>=(min+box_size)){ if(n>N_MAX_LOCAL) SID_trap_error("N_MAX reached in force_periodic_double. (2)",ERROR_LOGIC); (*coord)-=box_size; n++; } } }
void compute_progenitor_score_recursive(tree_node_info *node,int *M_i,int mode){ tree_node_info *current; int i_progenitor; int M_iN,N_i,max_M_iN; // Defined in Section 2 of De Lucia and Blaizot (2006) N_i =node->n_particles; max_M_iN=0; if(node->n_progenitors>=1){ current=node->progenitor_first; i_progenitor=0; while(current!=NULL){ M_iN=0; compute_progenitor_score_recursive(current,&M_iN,mode); if(check_mode_for_flag(mode,TREE_PROGENITOR_ORDER_DELUCIA)) max_M_iN=MAX(max_M_iN,M_iN); i_progenitor++; current=current->progenitor_next; } if(i_progenitor!=node->n_progenitors) SID_trap_error("There is a progenitor problem in compute_progenitor_score_recursive! (%d!=%d)",ERROR_LOGIC,i_progenitor!=node->n_progenitors); } // Add this progenitor's score to the descendant's sum (see De Lucia and Blaizot (2006)) (*M_i)=N_i+max_M_iN; }
void set_NFW_params(double M, double z, int mode, cosmo_info **cosmo, double *c_vir, double *R_vir){ double M_o; double Delta; double h_Hubble; double Omega_M; if(mode!=NFW_MODE_DEFAULT) SID_trap_error("Unknown mode (%d) in set_NFW_params()",ERROR_LOGIC,mode); switch(ADaPS_exist(*cosmo,"M_WDM")){ case FALSE: { Omega_M =((double *)ADaPS_fetch(*cosmo,"Omega_M"))[0]; h_Hubble=((double *)ADaPS_fetch(*cosmo,"h_Hubble"))[0]; M_o =M_sc(z,cosmo,PSPEC_LINEAR_TF,PSPEC_ALL_MATTER); // Mass-concentration from Munoz-Cuartas et al 2010 //(*c_vir)=(11./(1.+z))*pow(M/M_o,-0.13); // Bullock et al '01 & Zehavi et al '04 double w = 0.029; double m = 0.097; double alpha=-110.001; double beta =2469.720; double gamma= 16.885; double a_z =w*z-m; double b_z =alpha/(z+gamma)+beta/pow(z+gamma,2.); (*c_vir) =take_alog10(a_z*take_log10(M/(M_SOL/h_Hubble))+b_z); Delta =Delta_vir(z,*cosmo); Delta=200.; (*R_vir)=R_Delta_z(M,Delta,z,*cosmo); // Bullock et al '01 } break; case TRUE: SID_trap_error("ENS not working.",ERROR_LOGIC); //(*c_vir)=c_ENS(M,z,*cosmo); // Eke, Navarro and Steinmetz //(*R_vir)=R_Delta_z(M,200.,z,*cosmo); // R_200 break; } }
double fetch_treenode_y(tree_info *trees,tree_node_info *halo){ if(halo!=NULL){ halo_properties_info *properties; if(halo->parent==NULL){ if(trees->group_properties!=NULL) properties=&(trees->group_properties[halo->snap_tree][halo->neighbour_index]); else SID_trap_error("Group properties are not defined. They probably have not been read.",ERROR_LOGIC); } else{ if(trees->subgroup_properties!=NULL) properties=&(trees->subgroup_properties[halo->snap_tree][halo->neighbour_index]); else SID_trap_error("Subgroup properties are not defined. They probably have not been read.",ERROR_LOGIC); } return(properties->position_MBP[1]); } return(-1.); }
void init_trees_data(tree_info *trees, void ***rval, size_t data_size, int mode, const char *name, ...){ va_list vargs; va_start(vargs,name); // Create the new item (and apply some defaults) ADaPS *new_item; store_tree_data_free_parms_info *params; params =(store_tree_data_free_parms_info *)SID_malloc(sizeof(store_tree_data_free_parms_info)); params->n_snaps =trees->n_snaps; new_item =(ADaPS *)SID_malloc(sizeof(ADaPS)); new_item->mode =ADaPS_CUSTOM; new_item->free_function =free_trees_data; new_item->free_function_params=params; // Determine the size of the allocation new_item->data_size=trees->n_snaps*sizeof(void *); // Allocate arrays new_item->data=SID_malloc(sizeof(void *)*trees->n_snaps); void **data=(void **)new_item->data; for(int i_snap=0;i_snap<trees->n_snaps;i_snap++){ size_t n_items; size_t alloc_size; if(mode==INIT_TREE_DATA_GROUPS) n_items=trees->n_groups_snap_local[i_snap]; else if(mode==INIT_TREE_DATA_SUBGROUPS) n_items=trees->n_subgroups_snap_local[i_snap]; else SID_trap_error("Invalid init_tree_data() mode (%d).",ERROR_LOGIC,mode); alloc_size =data_size*n_items; data[i_snap] =SID_malloc(alloc_size); // Returns NULL if alloc_size==0 new_item->data_size+=alloc_size; } // Give the new item its name vsprintf(new_item->name,name,vargs); // Remove any previous entries with this name ADaPS_remove(&(trees->data),new_item->name); // Place new item at the start of the list new_item->next=trees->data; trees->data =new_item; (*rval)=data; va_end(vargs); }
double fetch_treenode_M_vir(tree_info *trees,tree_node_info *halo){ if(halo!=NULL){ double M_vir; if(halo->parent_top==NULL){ if(trees->group_properties!=NULL){ halo_properties_info *properties=&(trees->group_properties[halo->snap_tree][halo->neighbour_index]); M_vir=properties->M_vir; } else if(trees->group_properties_SHORT!=NULL){ halo_properties_SHORT_info *properties=&(trees->group_properties_SHORT[halo->snap_tree][halo->neighbour_index]); M_vir=properties->M_vir; } else if(trees->group_properties_SAGE!=NULL){ halo_properties_SAGE_info *properties=&(trees->group_properties_SAGE[halo->snap_tree][halo->neighbour_index]); M_vir=1e10*(double)(properties->M_vir); } else SID_trap_error("Group properties are not defined in fetch_treenode_M_vir(). They probably have not been read.",ERROR_LOGIC); } else{ if(trees->subgroup_properties!=NULL){ halo_properties_info *properties=&(trees->subgroup_properties[halo->snap_tree][halo->neighbour_index]); M_vir=properties->M_vir; } else if(trees->subgroup_properties_SHORT!=NULL){ halo_properties_SHORT_info *properties=&(trees->subgroup_properties_SHORT[halo->snap_tree][halo->neighbour_index]); M_vir=properties->M_vir; } else if(trees->subgroup_properties_SAGE!=NULL){ halo_properties_SAGE_info *properties=&(trees->subgroup_properties_SAGE[halo->snap_tree][halo->neighbour_index]); M_vir=1e10*(double)(properties->M_vir); } else SID_trap_error("Subgroup properties are not defined in fetch_treenode_M_vir(). They probably have not been read.",ERROR_LOGIC); } return(M_vir); } return(-1.); }
double fetch_treenode_x_off(tree_info *trees,tree_node_info *halo){ if(halo!=NULL){ halo_properties_info *properties; if(halo->parent==NULL){ if(trees->group_properties!=NULL) properties=&(trees->group_properties[halo->snap_tree][halo->neighbour_index]); else SID_trap_error("Group properties are not defined. They probably have not been read.",ERROR_LOGIC); } else{ if(trees->subgroup_properties!=NULL) properties=&(trees->subgroup_properties[halo->snap_tree][halo->neighbour_index]); else SID_trap_error("Subgroup properties are not defined. They probably have not been read.",ERROR_LOGIC); } double expansion_factor=a_of_z(trees->z_list[halo->snap_tree]); return(sqrt(pow(properties->position_COM[0]-properties->position_MBP[0],2.)+ pow(properties->position_COM[1]-properties->position_MBP[1],2.)+ pow(properties->position_COM[2]-properties->position_MBP[2],2.))/(properties->R_vir/expansion_factor)); } return(1.); }
int fetch_treenode_n_particles(tree_info *trees,tree_node_info *halo){ if(halo!=NULL){ int n_particles; if(halo->parent_top==NULL){ if(trees->group_properties!=NULL){ halo_properties_info *properties=&(trees->group_properties[halo->snap_tree][halo->neighbour_index]); n_particles=properties->n_particles; } else if(trees->group_properties_SHORT!=NULL){ halo_properties_SHORT_info *properties=&(trees->group_properties_SHORT[halo->snap_tree][halo->neighbour_index]); n_particles=properties->n_particles; } else if(trees->group_properties_SAGE!=NULL){ halo_properties_SAGE_info *properties=&(trees->group_properties_SAGE[halo->snap_tree][halo->neighbour_index]); n_particles=properties->n_particles; } else SID_trap_error("Group properties are not defined in fetch_treenode_n_particles(). They probably have not been read.",ERROR_LOGIC); } else{ if(trees->subgroup_properties!=NULL){ halo_properties_info *properties=&(trees->subgroup_properties[halo->snap_tree][halo->neighbour_index]); n_particles=properties->n_particles; } else if(trees->subgroup_properties_SHORT!=NULL){ halo_properties_SHORT_info *properties=&(trees->subgroup_properties_SHORT[halo->snap_tree][halo->neighbour_index]); n_particles=properties->n_particles; } else if(trees->subgroup_properties_SAGE!=NULL){ halo_properties_SAGE_info *properties=&(trees->subgroup_properties_SAGE[halo->snap_tree][halo->neighbour_index]); n_particles=properties->n_particles; } else SID_trap_error("Subgroup properties are not defined in fetch_treenode_n_particles(). They probably have not been read.",ERROR_LOGIC); } return(n_particles); } return(-1.); }
double fetch_treenode_z(tree_info *trees,tree_node_info *halo){ if(halo!=NULL){ double z; if(halo->parent_top==NULL){ if(trees->group_properties!=NULL){ halo_properties_info *properties=&(trees->group_properties[halo->snap_tree][halo->neighbour_index]); z=properties->position_COM[2]; } else if(trees->group_properties_SHORT!=NULL){ halo_properties_SHORT_info *properties=&(trees->group_properties_SHORT[halo->snap_tree][halo->neighbour_index]); z=properties->pos[2]; } else if(trees->group_properties_SAGE!=NULL){ halo_properties_SAGE_info *properties=&(trees->group_properties_SAGE[halo->snap_tree][halo->neighbour_index]); z=properties->pos[2]; } else SID_trap_error("Group properties are not defined in fetch_treenode_z(). They probably have not been read.",ERROR_LOGIC); } else{ if(trees->subgroup_properties!=NULL){ halo_properties_info *properties=&(trees->subgroup_properties[halo->snap_tree][halo->neighbour_index]); z=properties->position_COM[2]; } else if(trees->subgroup_properties_SHORT!=NULL){ halo_properties_SHORT_info *properties=&(trees->subgroup_properties_SHORT[halo->snap_tree][halo->neighbour_index]); z=properties->pos[2]; } else if(trees->subgroup_properties_SAGE!=NULL){ halo_properties_SAGE_info *properties=&(trees->subgroup_properties_SAGE[halo->snap_tree][halo->neighbour_index]); z=properties->pos[2]; } else SID_trap_error("Subgroup properties are not defined in fetch_treenode_z(). They probably have not been read.",ERROR_LOGIC); } return(z); } return(-1.); }
tree_horizontal_extended_info *set_extended_first_progenitor(tree_horizontal_extended_info **halos,tree_horizontal_extended_info *halo,int n_wrap){ if(halo!=NULL){ int file =halo->first_progenitor_file; int index=halo->first_progenitor_index; if(file>=0 && index>=0){ tree_horizontal_extended_info *node_return=&(halos[file%n_wrap][index]); if(check_mode_for_flag(node_return->type,TREE_CASE_INVALID)) SID_trap_error("A first_progenitor pointer points to an invalid halo (->%d;%d;%d).",ERROR_LOGIC,file,index,node_return->type); return(node_return); } } return(NULL); }
double histogram_bin_x_lo(hist_info *hist,int bin){ double r_val; // Sanity check if(!is_histogram_index_in_range(hist,bin)) SID_trap_error("Invalid bin requested (%d) in histogram_bin_x_hi().",ERROR_LOGIC,bin); if(check_mode_for_flag(hist->mode,GBP_HISTOGRAM_FIXED)){ switch(hist->flag_bin_order_inverted){ case FALSE: r_val=hist->x_min+((double)(bin))*hist->dx; break; case TRUE: r_val=hist->x_min+((double)(hist->n_bins-bin-1))*hist->dx; break; } } else if(check_mode_for_flag(hist->mode,GBP_HISTOGRAM_IRREGULAR_XLO_DEFINED)) r_val=hist->x_lo[bin]; else SID_trap_error("Invalid mode (%d) specified in histogram_bin_x_lo().",ERROR_LOGIC,hist->mode); return(r_val); }
void calc_mean_global(void *data_local, void *result, size_t n_data_local, SID_Datatype type, int mode, SID_Comm *comm){ double temp; size_t n_data; int flag_abs; if(check_mode_for_flag(mode,CALC_MODE_ABS)) flag_abs=CALC_MODE_ABS; else flag_abs=FALSE; calc_sum_global(&n_data_local,&n_data,1, SID_SIZE_T,CALC_MODE_DEFAULT, comm); if(n_data<1){ if(type==SID_DOUBLE || check_mode_for_flag(mode,CALC_MODE_RETURN_DOUBLE)) ((double *)result)[0]=0.; else if(type==SID_FLOAT) ((float *)result)[0]=0.; else if(type==SID_INT) ((int *)result)[0]=0; else if(type==SID_UNSIGNED) ((unsigned int *)result)[0]=0; else if(type==SID_SIZE_T) ((size_t *)result)[0]=0; else SID_trap_error("Unknown variable type in calc_min",ERROR_LOGIC); } else{ calc_sum_global(data_local, &temp, n_data_local,type, CALC_MODE_RETURN_DOUBLE|flag_abs,comm); temp/=(double)n_data; if(type==SID_DOUBLE) ((double *)result)[0]=(double)temp; else if(type==SID_FLOAT) ((float *)result)[0]=(float)temp; else if(type==SID_INT) ((int *)result)[0]=(int)temp; else if(type==SID_UNSIGNED) ((unsigned int *)result)[0]=(unsigned int)temp; else if(type==SID_SIZE_T) ((size_t *)result)[0]=(size_t)temp; } }
void read_atable(const char *filename_in,plist_info *plist,int x_column,int y_column,int z_column,int vx_column,int vy_column,int vz_column,const char *species_name,int mode,...){ // Interpret variable arguments double box_size; double redshift; cosmo_info *cosmo; double h_Hubble; va_list vargs; va_start(vargs,mode); // Interpret mode ... // ... domain decomposition ... int n_bits_PHK; int PHK_width; slab_info *slab; if(check_mode_for_flag(mode,READ_GROUPING_SLAB) && check_mode_for_flag(mode,READ_GROUPING_PHK)) SID_trap_error("Multiple domain decompositions have been set in read_atable().",ERROR_LOGIC); if(check_mode_for_flag(mode,READ_GROUPING_SLAB)) slab =(slab_info *)va_arg(vargs,slab_info *); else if(check_mode_for_flag(mode,READ_GROUPING_PHK)){
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); }
void init_treenode_trend_coordinate(tree_info *trees,trend_info *trend,const char *name){ if(!strcmp(name,"z")) init_trend_coordinate(trend,name,trees,init_tree_property_z,free_tree_property_z,calc_tree_property_index_z); else if(!strcmp(name,"logM_course")) init_trend_coordinate(trend,name,trees,init_tree_property_logM_course,free_tree_property_logM,calc_tree_property_index_logM); else if(!strcmp(name,"logM")) init_trend_coordinate(trend,name,trees,init_tree_property_logM,free_tree_property_logM,calc_tree_property_index_logM); else if(!strcmp(name,"xoff")) init_trend_coordinate(trend,name,trees,init_tree_property_xoff,free_tree_property_xoff,calc_tree_property_index_xoff); else if(!strcmp(name,"SSFctn")) init_trend_coordinate(trend,name,trees,init_tree_property_SSFctn,free_tree_property_SSFctn,calc_tree_property_index_SSFctn); else if(!strcmp(name,"log_sigma_vx")) init_trend_coordinate(trend,name,trees,init_tree_property_log_sigma_vx,free_tree_property_log_sigma_vx,calc_tree_property_index_log_sigma_vx); else if(!strcmp(name,"tau_form")) init_trend_coordinate(trend,name,trees,init_tree_property_tau,free_tree_property_tau,calc_tree_property_index_tau_form); else if(!strcmp(name,"tau_3to1")) init_trend_coordinate(trend,name,trees,init_tree_property_tau,free_tree_property_tau,calc_tree_property_index_tau_3to1); else if(!strcmp(name,"tau_10to1")) init_trend_coordinate(trend,name,trees,init_tree_property_tau,free_tree_property_tau,calc_tree_property_index_tau_10to1); else SID_trap_error("Invalid property {%s} specified in init_treenode_trend().",ERROR_LOGIC,name); }
void *ADaPS_fetch(ADaPS *list, const char *name_in,...){ ADaPS *current; int flag; void *r_val; va_list vargs; char name[ADaPS_NAME_LENGTH]; va_start(vargs,name_in); vsprintf(name,name_in,vargs); current=list; r_val =NULL; flag =TRUE; while(current!=NULL && flag){ if(!strcmp(name,current->name)){ r_val=current->data; flag =FALSE; } current=current->next; } if(flag) SID_trap_error("Variable {%s} was not found in ADaPS structure.",ERROR_LOGIC,name); va_end(vargs); return(r_val); }
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); }
void SID_init(int *argc, char **argv[], SID_args args[], void *mpi_comm_as_void){ int status; int i_level; int i_char; int flag_continue; int flag_passed_comm; // MPI-specific things #if USE_MPI int n_keys; int i_key; char key[256]; char key_value[256]; int key_exists; char nodes_string[256]; SID_fp fp_tmp; FILE *fp_hack; int node_name_length; MPI_Comm mpi_comm; #if USE_MPI_IO MPI_Info info_disp; #endif if (mpi_comm_as_void == NULL) { flag_passed_comm = 0; MPI_Init(argc,argv); MPI_Comm_dup(MPI_COMM_WORLD, &mpi_comm); } else { mpi_comm = *((MPI_Comm *) mpi_comm_as_void); flag_passed_comm = 1; } MPI_Comm_size(mpi_comm, &(SID.n_proc)); MPI_Comm_rank(mpi_comm, &(SID.My_rank)); SID.My_node =(char *)SID_malloc(SID_MAXLENGTH_PROCESSOR_NAME * sizeof(char)); #if USE_MPI MPI_Get_processor_name(SID.My_node, &node_name_length); #else sprintf(SID.My_node,"localhost"); node_name_length=strlen(SID.My_node); #endif if (node_name_length >= SID_MAXLENGTH_PROCESSOR_NAME-1) SID_trap_error("SID_MAXLENGTH_PROCESSOR_NAME needs to be increased",ERROR_LOGIC); // Make my_rank=MASTER_RANK the master if(SID.My_rank==MASTER_RANK) SID.I_am_Master=TRUE; else SID.I_am_Master=FALSE; // Identify the last rank if(SID.My_rank==SID.n_proc-1) SID.I_am_last_rank=TRUE; else SID.I_am_last_rank=FALSE; #if USE_MPI_IO // Fetch collective buffering defaults MPI_Info_create(&(SID.file_info)); if(SID.I_am_Master){ fp_hack=fopen(".tmp.SID","w+"); fclose(fp_hack); } MPI_Barrier(mpi_comm); MPI_File_open(mpi_comm, ".tmp.SID", MPI_MODE_WRONLY, MPI_INFO_NULL, &(fp_tmp.fp)); MPI_File_get_info(fp_tmp.fp,&info_disp); MPI_Info_get_nkeys(info_disp,&n_keys); for(i_key=0;i_key<n_keys;i_key++){ MPI_Info_get_nthkey(info_disp,i_key,key); MPI_Info_get(info_disp,key,MPI_MAX_INFO_VAL,key_value,&key_exists); if(key_exists) MPI_Info_set((SID.file_info),key,key_value); } MPI_File_close(&(fp_tmp.fp)); if(SID.I_am_Master) remove(".tmp.SID"); // Set user-defined colective buffering optimizations sprintf(nodes_string,"%d",MIN(SID.n_proc,N_IO_FILES_MAX)); MPI_Info_set((SID.file_info),"cb_nodes", nodes_string); MPI_Info_set((SID.file_info),"cb_config_list", "*:1"); #endif #else SID.My_rank=MASTER_RANK; SID.n_proc =1; #endif /* #if !USE_MPI_IO SID.n_groups=SID.n_proc/N_IO_FILES_MAX; if(SID.n_proc%N_IO_FILES_MAX) SID.n_groups++; SID.My_group=SID.My_rank/N_IO_FILES_MAX; #endif */ // Set ranks to the left and right SID.rank_to_right =(SID.My_rank+1)%SID.n_proc; SID.rank_to_left = SID.My_rank-1; if(SID.rank_to_left<0) SID.rank_to_left = SID.n_proc-1; // Intitialize log timing information SID.time_start_level=(time_t *)SID_malloc(sizeof(time_t)*SID_LOG_MAX_LEVELS); SID.time_stop_level =(time_t *)SID_malloc(sizeof(time_t)*SID_LOG_MAX_LEVELS); SID.time_total_level=(int *)SID_malloc(sizeof(int) *SID_LOG_MAX_LEVELS); SID.IO_size =(double *)SID_malloc(sizeof(double)*SID_LOG_MAX_LEVELS); SID.flag_use_timer =(int *)SID_malloc(sizeof(int) *SID_LOG_MAX_LEVELS); for(i_level=0;i_level<SID_LOG_MAX_LEVELS;i_level++){ SID.time_start_level[i_level]=0; SID.time_stop_level[i_level] =0; SID.time_total_level[i_level]=0; SID.IO_size[i_level] =0.; SID.flag_use_timer[i_level] =FALSE; } // Initialize other log information #if USE_MPI if(*argc>1) SID.fp_in =fopen((*argv)[1],"r"); else SID.fp_in =NULL; #else SID.fp_in =stdin; #endif if (flag_passed_comm) SID.fp_log = NULL; else SID.fp_log = stderr; SID.level =0; SID.indent =TRUE; SID.awake =TRUE; SID.flag_results_on=FALSE; SID.verbosity =SID_LOG_MAX_LEVELS; // Store the name of the binary executable that brought us here strcpy(SID.My_binary,(*argv)[0]); strip_path(SID.My_binary); // Initialize argument information if(args!=NULL){ if((status=SID_parse_args(*argc,*argv,args))>0){ SID_print_syntax(*argc,*argv,args); SID_exit(status); } } else SID.args=NULL; #if USE_MPI_IO if(SID.I_am_Master){ fp_hack=fopen(".tmp.SID","w+"); fclose(fp_hack); } MPI_Barrier(mpi_comm); SID_fopen(".tmp.SID","w",&fp_tmp); MPI_File_get_info(fp_tmp.fp,&info_disp); if(SID.I_am_Master){ fprintf(stdout,"\n"); fprintf(stdout,"MPI-I/O Configuration:\n"); fprintf(stdout,"---------------------\n"); MPI_Info_get_nkeys(info_disp,&n_keys); for(i_key=0;i_key<n_keys;i_key++){ MPI_Info_get_nthkey(info_disp,i_key,key); MPI_Info_get(info_disp,key,MPI_MAX_INFO_VAL,key_value,&key_exists); if(key_exists) fprintf(stdout,"key %2d of %d: {%s}={%s}\n",i_key+1,n_keys,key,key_value); } fprintf(stdout,"\n"); } SID_fclose(&fp_tmp); if(SID.I_am_Master) remove(".tmp.SID"); #else #if USE_MPI if(SID.I_am_Master) fprintf(stdout,"MPI-I/O switched off.\n\n"); #endif #endif // Create private COMM_WORLD SID_Comm_init(&(SID.COMM_WORLD)); #if USE_MPI MPI_Comm_dup(mpi_comm, &((SID.COMM_WORLD)->comm)); MPI_Comm_group((SID.COMM_WORLD)->comm,&((SID.COMM_WORLD)->group)); MPI_Comm_size(SID.COMM_WORLD->comm, &((SID.COMM_WORLD)->n_proc)); MPI_Comm_rank(SID.COMM_WORLD->comm, &((SID.COMM_WORLD)->My_rank)); // We have duplicated our duplicate mpi communicator - now we can free the // original duplicate MPI_Comm_free(&mpi_comm); #else SID.COMM_WORLD->comm =NULL; SID.COMM_WORLD->group =NULL; SID.COMM_WORLD->n_proc =1; SID.COMM_WORLD->My_rank=MASTER_RANK; #endif // Start total-run-ime timer (void)time(&(SID.time_start)); // Default max wallclock SID.max_wallclock=DEFAULT_MAX_WALLCLOCK_TIME; }
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); }
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); }
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); }
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); }
int fopen_multifile(const char *filename_root, size_t data_size, fp_multifile_info *fp_out, ...){ va_list vargs; va_start(vargs,fp_out); int r_val =TRUE; int flag_filefound=FALSE; // Sort out what file format we're working with fp_out->fp_multifile=NULL; if(SID.I_am_Master){ int i_file; char filename_multifile[MAX_FILENAME_LENGTH]; // Set some filename information vsprintf(fp_out->filename_root,filename_root,vargs); strcpy(fp_out->filename_base,fp_out->filename_root); strip_path(fp_out->filename_base); // Try reading a multifile first ... sprintf(filename_multifile,"%s/%s.%d",fp_out->filename_root,fp_out->filename_base,0); fp_out->fp_multifile=fopen(filename_multifile,"r"); if(fp_out->fp_multifile==NULL){ sprintf(filename_multifile,"%s",fp_out->filename_root); // ... if we didn't find a multi-file, try reading a single file ... fp_out->fp_multifile=fopen(filename_multifile,"r"); if(fp_out->fp_multifile==NULL){ r_val=FALSE; // Don't report error here so that we can use this routine to check if datasets exist without termination //SID_trap_error("Could not open multifile {%s}",ERROR_LOGIC,filename_root); } // ... we found a single file. Set flags. else fp_out->flag_multifile=FALSE; } // ... we found a multi-file. Set flags. else fp_out->flag_multifile=TRUE; // Load/set header information if(fp_out->fp_multifile!=NULL){ fread_verify(&(fp_out->i_file), sizeof(int),1,fp_out->fp_multifile); fread_verify(&(fp_out->n_files), sizeof(int),1,fp_out->fp_multifile); fread_verify(&(fp_out->n_items_file), sizeof(int),1,fp_out->fp_multifile); fread_verify(&(fp_out->n_items_total),sizeof(int),1,fp_out->fp_multifile); fclose(fp_out->fp_multifile); fp_out->fp_multifile=NULL; } } SID_Bcast(fp_out,sizeof(fp_multifile_info),MASTER_RANK,SID.COMM_WORLD); // Set the data size fp_out->data_size=data_size; if(r_val){ // Initialize things by opening the first file. Even if we want a item // that's deep in the list, we have to scan all the headers (starting // with the first) to find where it is. fp_out->fp_multifile=NULL; fp_out->i_file =0; fp_out->i_item =0; if(!(r_val=fopen_multifile_nth_file(fp_out,0))) SID_trap_error("Error opening multifile file.",ERROR_IO_OPEN); } va_end(vargs); return(r_val); }
size_t fread_verify(void *ptr, size_t size, size_t count, FILE *stream){ size_t n_return; if((n_return=fread(ptr,size,count,stream))!=count) SID_trap_error("Failed to read %lld %lld-byte sized items (only %lld returned).",ERROR_IO_READ,count,size,n_return); return(n_return); }
// Scaled mass functions double scaled_mass_function(double sigma,int mode,double *P){ double rval; if(check_mode_for_flag(mode,MF_WATSON)){ // Watson et al. (2013) universal FoF mass function double A = 0.282; double alpha = 2.163; double beta = 1.406; double gamma = 1.210; if(check_mode_for_flag(mode,MF_PASS_PARAMS)){ A = P[0]; alpha = P[1]; beta = P[2]; gamma = P[3]; } rval=A*(pow(beta/sigma,alpha)+1.)*exp(-gamma/(sigma*sigma)); } else if(check_mode_for_flag(mode,MF_TIAMAT)){ // Poole et al. (2013) universal FoF mass function for Tiamat double A = 0.03331; double alpha = 1.153; double beta = 12.33; double gamma = 1.009; if(check_mode_for_flag(mode,MF_PASS_PARAMS)){ A = P[0]; alpha = P[1]; beta = P[2]; gamma = P[3]; } rval=A*(pow(beta/sigma,alpha)+1.)*exp(-gamma/(sigma*sigma)); } else if(check_mode_for_flag(mode,MF_JENKINS)){ // Jenkins et al. (2001) double A=0.315; double B=0.61; double C=3.8; if(check_mode_for_flag(mode,MF_PASS_PARAMS)){ A=P[0]; B=P[1]; C=P[2]; } rval =A*exp(-pow((double)fabs((float)(take_ln(1./sigma)+B)),C)); } else if(check_mode_for_flag(mode,MF_ST)){ // Sheth-Torman (1999) double delta_k=1.686; double A =0.3222; double B =0.707; double C =0.3; if(check_mode_for_flag(mode,MF_PASS_PARAMS)){ A =P[0]; B =P[1]; C =P[2]; } rval =A*sqrt(2.*B/PI)*(delta_k/sigma)*exp(-B*delta_k*delta_k/(2.*sigma*sigma))*(1.+pow(sigma*sigma/(B*delta_k*delta_k),C)); } else if(check_mode_for_flag(mode,MF_PS)){ // Press-Schechter (1974) double delta_k=1.686; if(check_mode_for_flag(mode,MF_PASS_PARAMS)){ delta_k=P[0]; } rval =sqrt(2./PI)*(delta_k/sigma)*exp(-delta_k*delta_k/(2.*sigma*sigma)); } else SID_trap_error("A valid mass function was not specified with mode (%d) in scaled_mass_function().\n",ERROR_LOGIC,mode); return(rval); }
int main(int argc, char *argv[]){ char filename_root[256]; char filename_properties[256]; char filename_indices[256]; char filename_out[256]; char prefix_text[5]; FILE *fp_properties=NULL; FILE *fp_profiles =NULL; FILE *fp_out =NULL; int i_file; int n_files; int n_groups_all; int i_group; int i_group_selected; int i_profile; int flag_process_group; int snap_number; int n_groups_properties; int n_groups_profiles; halo_properties_info properties; halo_profile_info profile; float lambda,v_c; float offset_COM; float r_min,r_max; SID_init(&argc,&argv,NULL,NULL); strcpy(filename_root, argv[1]); snap_number =atoi(argv[2]); i_group_selected=atoi(argv[3]); if(i_group_selected<0){ flag_process_group=TRUE; i_group_selected*=-1; sprintf(prefix_text,""); } else{ flag_process_group=FALSE; sprintf(prefix_text,"sub"); } if(SID.I_am_Master){ sprintf(filename_indices,"%s_%03d.catalog_%sgroups_indices",filename_root,snap_number,prefix_text); FILE *fp_in; if((fp_in=fopen(filename_indices,"r"))==NULL) SID_trap_error("Could not open file {%s}.",ERROR_IO_OPEN,filename_indices); int i_file,n_files,n_groups,n_groups_all; fread(&i_file, sizeof(int),1,fp_in); fread(&n_files, sizeof(int),1,fp_in); fread(&n_groups, sizeof(int),1,fp_in); fread(&n_groups_all,sizeof(int),1,fp_in); int n_particles_i; for(i_group=0;i_group<i_group_selected;i_group++){ fread(&n_particles_i,sizeof(int),1,fp_in); fseeko(fp_in,(off_t)(sizeof(size_t)*n_particles_i),SEEK_CUR); } fread(&n_particles_i,sizeof(int),1,fp_in); fprintf(stderr,"n_particles=%d\n",n_particles_i); int i_particle; for(i_particle=0;i_particle<n_particles_i;i_particle++){ size_t index_i; fread(&index_i,sizeof(size_t),1,fp_in); fprintf(stderr,"%4d %lld\n",i_particle,index_i); } fclose(fp_in); } SID_exit(ERROR_NONE); }
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); }
int main(int argc, char *argv[]){ SID_init(&argc,&argv,NULL,NULL); char filename_cosmology[MAX_FILENAME_LENGTH]; char paramterization[MAX_FILENAME_LENGTH]; double log_M_min =atof(argv[1]); double log_M_max =atof(argv[2]); int n_M_bins =atoi(argv[3]); double redshift =atof(argv[4]); strcpy(filename_cosmology,argv[5]); strcpy(paramterization, argv[6]); SID_log("Constructing mass function between log(M)=%5.3lf->%5.3lf at z=%5.3lf...",SID_LOG_OPEN,log_M_min,log_M_max,redshift); // Initialize cosmology cosmo_info *cosmo=NULL; read_gbpCosmo_file(&cosmo,filename_cosmology); // Decide which parameterization we are going to use int select_flag; char mfn_text[32]; if(!strcmp(paramterization,"JENKINS")){ sprintf(mfn_text,"Jenkins"); select_flag=MF_JENKINS; } else if(!strcmp(paramterization,"PS")){ sprintf(mfn_text,"Press & Schechter"); select_flag=MF_PS; } else if(!strcmp(paramterization,"ST")){ sprintf(mfn_text,"Sheth & Tormen"); select_flag=MF_ST; } else SID_trap_error("Invalid parameterization selected {%s}. Should be {JENKINS,PS or ST}.",ERROR_SYNTAX,paramterization); // Create output filename char filename_out[MAX_FILENAME_LENGTH]; char redshift_text[64]; char *cosmology_name=(char *)ADaPS_fetch(cosmo,"name"); float_to_text(redshift,3,redshift_text); sprintf(filename_out,"mass_function_z%s_%s_%s.txt",redshift_text,cosmology_name,paramterization); // Open file and write header FILE *fp_out=NULL; fp_out=fopen(filename_out,"w"); int i_column=1; fprintf(fp_out,"# Mass function (%s) for %s cosmology at z=%lf\n",mfn_text,filename_cosmology,redshift); fprintf(fp_out,"# \n"); fprintf(fp_out,"# Column (%02d): log M [h^-1 M_sol]\n", i_column++); fprintf(fp_out,"# (%02d): Mass function [(h^{-1} Mpc]^{-3} per dlogM]\n", i_column++); fprintf(fp_out,"# (%02d): Cumulative Mass function(>M) [(h^{-1} Mpc]^{-3}]\n",i_column++); // Create the mass function SID_log("Writing results to {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_out); pcounter_info pcounter; SID_init_pcounter(&pcounter,n_M_bins,10); double h_Hubble =((double *)ADaPS_fetch(cosmo,"h_Hubble"))[0]; double mass_factor=M_SOL/h_Hubble; double MFct_factor=pow(M_PER_MPC,3.0); for(int i_bin=0;i_bin<n_M_bins;i_bin++){ double log_M; if(i_bin==0) log_M=log_M_min; else if(i_bin==(n_M_bins-1)) log_M=log_M_max; else log_M=log_M_min+(((double)(i_bin))/((double)(n_M_bins-1)))*(log_M_max-log_M_min); fprintf(fp_out,"%le %le %le\n",log_M, MFct_factor*mass_function (mass_factor*take_alog10(log_M),redshift,&cosmo,select_flag), MFct_factor*mass_function_cumulative(mass_factor*take_alog10(log_M),redshift,&cosmo,select_flag)); SID_check_pcounter(&pcounter,i_bin); } fclose(fp_out); SID_log("Done.",SID_LOG_CLOSE); // Clean-up free_cosmo(&cosmo); SID_log("Done.",SID_LOG_CLOSE); SID_exit(ERROR_NONE); }