void _flush_wcache(struct cached_io *cio, char *extra, int64_t extra_size) {
  FILE *io;
  struct stat out_stats;
  if (cio->wcache_pos || extra_size) {
    if (cio->append) io = check_fopen(cio->filename, "a");
    else {
      io = fopen(cio->filename, "r+");
      if (!io) io = check_fopen(cio->filename, "w");
      fseeko(io, cio->wpos, SEEK_SET);
    }
    //if (cio->do_locks) flock(fileno(io), LOCK_EX);
    
    if (cio->append) fseek(io, 0, SEEK_END);
    if (cio->header) {
      fstat(fileno(io), &out_stats);
      if (!out_stats.st_size)
	fwrite(cio->header, 1, strlen(cio->header), io);
    }
    if (cio->wcache_pos) fwrite(cio->wcache, 1, cio->wcache_pos, io);
    if (extra) fwrite(extra, 1, extra_size, io);
    fflush(io);
    cio->wpos += cio->wcache_pos + extra_size;
    //if (cio->do_locks) flock(fileno(io), LOCK_UN);
    fclose(io);
    cio->wcache_pos = 0;
  }
}
int main(int argc, char **argv) {
  int64_t i, j, p_next, count=0, p_start;
  struct bgc2_header hdr;
  struct binstats *bs_self, *bs_all;
  float bulkvel[3];
  char buffer[1024];
  FILE *output;
  char dirname[100];

  bs_self = init_binstats(BIN_START, BIN_END, BPDEX, 1, 1);
  bs_all = init_binstats(BIN_START, BIN_END, BPDEX, 1, 1);
  if (argc < 2) {
    printf("Usage: %s min_mass max_mass file1.bgc2 ...\n", argv[0]);
    exit(1);
  }
  MIN_MASS = atof(argv[1]);
  MAX_MASS = atof(argv[2]);
  assert(MIN_MASS>0 && MAX_MASS>MIN_MASS);

  for (i=3; i<argc; i++) {
    num_g = num_p = 0;
    load_bgc2(argv[i], &hdr, &grps, &num_g, &parts, &num_p);
    if (i==3) {
      snprintf(dirname, 100, "veldif_z%.2f_m%.2f_m%.2f", hdr.redshift, 
	       log10(MIN_MASS), log10(MAX_MASS));
      mkdir(dirname, 0755);
    }
    p_next = 0;
    for (j=0; j<num_g; j++) {
      p_next += grps[j].npart;
      if (grps[j].mass < MIN_MASS*hdr.Hubble0 || 
	  grps[j].mass > MAX_MASS*hdr.Hubble0 ||
	  grps[j].parent_id >= 0) continue;
      count++;
      p_start = p_next - grps[j].npart;
      calc_bulkvel(bulkvel, parts + p_start, grps[j].npart);
      sprintf(buffer, "%s/veldif_self_%05"PRId64".dat", dirname, count);
      output = check_fopen(buffer, "w");
      bin_velocities(parts + p_start, grps[j].npart_self, grps[j].pos,
		     bulkvel, bs_self, output);
      fclose(output);
      sprintf(buffer, "%s/veldif_all_%05"PRId64".dat", dirname, count);
      output = check_fopen(buffer, "w");
      bin_velocities(parts + p_start, grps[j].npart, grps[j].pos, bulkvel,
		     bs_all, output);
      fclose(output);
    }
  }

  sprintf(buffer, "%s/veldif_self.dat", dirname);
  output = check_fopen(buffer, "w");
  print_data(output, bs_self, count);
  fclose(output);
  sprintf(buffer, "%s/veldif_all.dat", dirname);
  output = check_fopen(buffer, "w");
  print_data(output, bs_all, count);
  fclose(output);
  return 0;
}
Exemple #3
0
void output_merger_catalog(int64_t snap, int64_t chunk, struct halo *halos, int64_t num_halos) {
  char buffer[1024];
  FILE *output;
  int64_t i, j;
  double m;
  struct halo *th;
  struct flock fl = {0};

  snprintf(buffer, 1024, "%s/out_%"PRId64".list", OUTBASE, snap);
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = 0;
  fl.l_len = 0;

  if (chunk == 0) {
    output = check_fopen(buffer, "w");
    fcntl(fileno(output), F_SETLKW, &fl);
    fprintf(output, "#ID DescID M%s Vmax Vrms R%s Rs Np X Y Z VX VY VZ JX JY JZ Spin\n",
	    MASS_DEFINITION, MASS_DEFINITION);
    fprintf(output, "#a = %f\n", SCALE_NOW);
    fprintf(output, "#Om = %f; Ol = %f; h = %f\n", Om, Ol, h0);
    fprintf(output, "#Unbound Threshold: %f; FOF Refinement Threshold: %f\n",
	    UNBOUND_THRESHOLD, FOF_FRACTION);
    fprintf(output, "#Particle mass: %.5e Msun/h\n", PARTICLE_MASS);
    fprintf(output, "#Box size: %f Mpc/h\n", BOX_SIZE);
    fprintf(output, "#Units: Masses in Msun / h\n"
	    "#Units: Positions in Mpc / h (comoving)\n"
	    "#Units: Velocities in km / s (physical)\n"
	    "#Units: Angular Momenta in (Msun/h) * (Mpc/h) * km/s (physical)\n"
	    "#Units: Radii in kpc / h (comoving)\n");
    fprintf(output, "#Rockstar Version: %s\n", ROCKSTAR_VERSION);
  }
  else {
    output = check_fopen(buffer, "a");
    fcntl(fileno(output), F_SETLKW, &fl);
  }
  
  for (i=0; i<num_halos; i++) {
    th = halos+i;
    if (LIGHTCONE) for (j=0; j<3; j++) th->pos[j] -= LIGHTCONE_ORIGIN[j];
    m = (BOUND_PROPS) ? th->mgrav : th->m;
    fprintf(output, "%"PRId64" %"PRId64" %.4e %.2f %.2f %.3f %.3f %"PRId64" %.5f %.5f %.5f %.2f %.2f %.2f %.3e %.3e %.3e %.5f\n",
	    th->id, th->desc, m, th->vmax, th->vrms, th->r, th->rs,
	    th->num_p, th->pos[0], th->pos[1], th->pos[2], th->pos[3],
	    th->pos[4], th->pos[5], th->J[0], th->J[1], th->J[2], th->spin);
  }
  fflush(output);
  fclose(output); //Also unlocks the file
}
Exemple #4
0
void read_input_names(char *filename, char ***stringnames, int64_t *num_names) {
  int64_t i=0, j;
  char buffer[1024];
  FILE *input;
  char **names = NULL;

  if (!strlen(filename)) return;
  input = check_fopen(filename, "r");
  while (fgets(buffer, 1024, input)) {
    while (strlen(buffer) && buffer[strlen(buffer)-1]=='\n')
      buffer[strlen(buffer)-1] = 0;
    if (!strlen(buffer)) continue;
    if (!(i%10)) names = check_realloc(names, sizeof(char *)*(i+10), 
					   "Allocating snapshot names.");
    names[i] = strdup(buffer);
    i++;
  }
  fclose(input);

  if (*stringnames) {
    for (j=0; j<*num_names; j++) free((*stringnames)[j]);
    free(*stringnames);
  }
  *num_names = i;
  *stringnames = names;
}
void print_stats(int64_t out_num) {
  FILE *output;
  char buffer[1024];
  int64_t i,j;
  snprintf(buffer, 1024, "%s/statistics_%"PRId64".list", OUTBASE, out_num);
  output = check_fopen(buffer, "w");
  fprintf(output, "#T: Total number of halos.\n");
  fprintf(output, "#G: Good halos (with consistent descendants).\n");
  fprintf(output, "#D: Dead halos (without consistent descendants).\n");
  fprintf(output, "#ND: Halos initially without descendants in particle merger tree.\n");
  fprintf(output, "#DNF: Halos whose descendants were deleted.\n");
  fprintf(output, "#PGI: Halos whose descendants are gravitationally inconsistent (binned by progenitor mass).\n");
  fprintf(output, "#DGI: Halos whose descendants are gravitationally inconsistent (binned by descendant mass).\n");
  fprintf(output, "#IT: Halos whose descendants are inconsistent with applicable tidal forces.\n");
  fprintf(output, "#PGR: Halos whose descendants were found by gravitational consistency (binned by progenitor mass).\n");
  fprintf(output, "#DGR: Halos whose descendants were found by gravitational consistency (binned by descendant mass).\n");
  fprintf(output, "#TR: Halos whose descendants were identified by tidal forces.\n");
  fprintf(output, "#P: Phantom halos.\n");
  fprintf(output, "#NP: Phantom halos created at this timestep.\n");
  fprintf(output, "#RND: Halos remaining without descendants (not including phantoms).\n");
  fprintf(output, "#MT: Halos which were likely too massive to tidally merge.\n");
  fprintf(output, "#SMMP: MMP links where the mass ratio was greater than %f.\n", MIN_MMP_MASS_RATIO);
  fprintf(output, "#TMP: Tracks which were truncated due to the phantom fraction being higher than %f.\n", MAX_PHANTOM_FRACTION);
  fprintf(output, "#Mass_bin T G D ND DNF PGI DGI IT PGR DGR TR P NP RND MT SMMP TMP\n");

  for (i=0; i<counts_mass_bins; i++) {
    fprintf(output, "%-2"PRId64" ", counts_min_mass_bin+i);
    for (j=0; j<NUM_STATS; j++)
      fprintf(output, "%7"PRId64" ", counts[j][i]);
    fprintf(output, "\n");
  }
  fclose(output);
}
void load_bgc2(char *filename, struct bgc2_header *hdr,
	       GROUP_DATA_RMPVMAX **groups, int64_t *num_groups,
	       PARTICLE_DATA_PV **pdata, int64_t *num_parts)
{
  FILE *input;
  int64_t new_group_size, new_part_size;
  int64_t i, p_start;

  assert(sizeof(struct bgc2_header) == BGC2_HEADER_SIZE);
  input = check_fopen(filename, "rb");

  fread_fortran(hdr, BGC2_HEADER_SIZE, 1, input, 0);
  assert(hdr->magic == BGC_MAGIC);
  assert(hdr->version == 2);
  assert(hdr->format_group_data == GDATA_FORMAT_RMPVMAX);

  new_group_size = sizeof(GROUP_DATA_RMPVMAX)*((*num_groups)+hdr->ngroups);
  *groups = check_realloc(*groups, new_group_size, "Allocating groups.");
  fread_fortran((*groups) + (*num_groups), sizeof(GROUP_DATA_RMPVMAX), 
		hdr->ngroups, input, 0);
  *num_groups += hdr->ngroups;
  
  new_part_size = sizeof(PARTICLE_DATA_PV)*((*num_parts)+hdr->npart);
  *pdata = check_realloc(*pdata, new_part_size, "Allocating particles");
  p_start = 0;
  for (i=0; i<hdr->ngroups; i++) {
    fread_fortran((*pdata) + p_start, sizeof(PARTICLE_DATA_PV), 
		  groups[0][i].npart, input, 0);
    p_start += groups[0][i].npart;
  }
  *num_parts += hdr->npart;
  fclose(input);
}
void turn_on_full_metric_output(int64_t output_num, float a1, float a2) {
  float dt = fabs(scale_to_years(a1)-scale_to_years(a2)) / 1.0e6;
  char buffer[1024];
  sprintf(buffer, "%s/full_metric_data_%"PRId64".list", OUTBASE, output_num);
  metric_output = check_fopen(buffer, "w");
  fprintf(metric_output, "#Dt: %f Myr\n", dt);
  fprintf(metric_output, "#From scale %f to %f\n", a1, a2);
  fprintf(metric_output, "#ProgId ProgMass ProgVmax DescId DescMass DescVmax DeltaX DeltaV DeltaVmax Vrms/Vmax DynFricCoeff\n");
}
Exemple #8
0
int main()
{
#define TEST_CNT 2
    FILE *ifps[TEST_CNT];
    FILE *ofps[TEST_CNT];

    char in[128], out[128];

    for (int i = 0; i < TEST_CNT; i++) {
        sprintf(in, "/tmp/byyl/test4_%d.in", i + 1);
        ifps[i] = fopen(in, "r");
        check_fopen(ifps[i], in);
    }
    for (int i = 0; i < TEST_CNT; i++) {
        sprintf(out, "/tmp/byyl/test4_%d.out", i + 1);
        ofps[i] = fopen(out, "w");
        check_fopen(ofps[i], out);
    }
    for (int i = 0; i < Vn_COUNT; i++) {
        Vs[i].V = Vn_table[i];
    }
    for (int i = 0; i < Vt_COUNT; i++) {
        OPG_matrix_head[i] = Vt_table[i];
    }
    OPG_matrix_head[Vt_COUNT] = EOI;
    firstVT();
    print_firstVT();
    lastVT();
    print_lastVT();
    make_OPG_matrix();
    print_OPM();
    make_prec_func();
    print_pf();
    for (int i = 0; i < TEST_CNT; i++) {
        if (OPG_parser(ifps[i])) {
            fprintf(ofps[i], "true");
        } else {
            fprintf(ofps[i], "false");
        }
        fclose(ifps[i]);
        fclose(ofps[i]);
    }
    return 0;
}
Exemple #9
0
void print_tidal_forces(int64_t output_num, float a, struct halo_stash *h, struct halo_stash *evolved) {
  char buffer[1024];
  FILE *mmp_out, *nmmp_out, *output;
  int64_t mmp, j, eindex, tidal_desc_id, nindex, bfnmmp;
  float tidal_mvir;
  snprintf(buffer, 1024, "%s/extended_tidal_forces_mmp_%"PRId64".list", OUTBASE, output_num);
  mmp_out = check_fopen(buffer, "w");
  fprintf(mmp_out, "#ID DescID TidalId TidalDescId TidalForce Rvir Mvir TidalMvir Vmax\n");
  fprintf(mmp_out, "#Scale factor: %f\n", a);
  snprintf(buffer, 1024, "%s/extended_tidal_forces_nmmp_%"PRId64".list", OUTBASE, output_num);
  nmmp_out = check_fopen(buffer, "w");
  fprintf(nmmp_out, "#ID DescID TidalId TidalDescId TidalForce Rvir Mvir TidalMvir Vmax Confirmed_Nmmp\n");
  fprintf(nmmp_out, "#Scale factor: %f\n", a);
  for (j=0; j<h->num_halos; j++) {
    if (h->halos[j].flags & DEAD_HALO_FLAG)
      continue;
 
    eindex = id_to_index(*evolved, h->halos[j].descid);
    if (eindex < 0) //Descendant halo no longer exists
      continue;
    bfnmmp = 1;
    mmp = (evolved->halos[eindex].mmp_id == h->halos[j].id) ? 1 : 0;
    if (mmp && really_beyond_mmp_ratio(h->halos[j], evolved->halos[eindex])) {
      mmp = 0;
      bfnmmp = 0;
    }
    output = (mmp) ? mmp_out : nmmp_out;

    nindex = id_to_index(*h, h->halos[j].tidal_id);
    if (nindex>-1) {
      tidal_desc_id = h->halos[nindex].descid;
      tidal_mvir = h->halos[nindex].mvir;
    }
    else { tidal_desc_id = -1; tidal_mvir = 0; }

    fprintf(output, "%"PRId64" %"PRId64" %"PRId64" %"PRId64" %.3e %f %.3e %.3e %f %"PRId64"\n",
            h->halos[j].id, h->halos[j].descid, h->halos[j].tidal_id,
            tidal_desc_id, h->halos[j].tidal_force, h->halos[j].rvir, 
            h->halos[j].mvir, tidal_mvir, h->halos[j].vmax, bfnmmp);
  }
  fclose(mmp_out);
  fclose(nmmp_out);
}
void _fill_rcache(struct cached_io *cio) {
  FILE *io;
  io = check_fopen(cio->filename, "r");
  fseek(io, cio->rpos, SEEK_SET);
  cio->rcache_size = fread(cio->rcache, 1, cio->cachesize, io);
  cio->rcache[cio->rcache_size] = 0;
  cio->rpos = ftello(io);
  cio->rcache_pos = 0;
  fclose(io);
}
void _print_metric_stats(int64_t output_num, float a1, float a2, char *title, struct metric_struct *x, struct metric_struct *v, struct metric_struct *vmax)
{
  int64_t i;
  float m, nd;
  char buffer[1024];
  float dt = fabs(scale_to_years(a1)-scale_to_years(a2)) / 1.0e6;
  float vol = pow(box_size/h0, 3);
  FILE *output, *output2;
  snprintf(buffer, 1024, "%s/%s_statistics_%"PRId64".list", OUTBASE, title, output_num);
  output = check_fopen(buffer, "w");
  fprintf(output, "#Dt: %f Myr\n", dt);
  fprintf(output, "#From scale %f to %f\n", a1, a2);
  fprintf(output, "#Mass DX_avg DX_sd DX_tol DV_avg DV_sd DV_tol XV_corr DF_corr DVmax_avg DVmax_sd DVmax_tol Count Number_density\n");
  snprintf(buffer, 1024, "%s/%s_maxmin_%"PRId64".list", OUTBASE, title, output_num);
  output2 = check_fopen(buffer, "w");
  fprintf(output2, "#Dt: %f Myr\n", dt);
  fprintf(output2, "#From scale %f to %f\n", a1, a2);
  fprintf(output2, "#Mass DX_min DX_max DV_min DV_max DVmax_min DVmax_max\n");

  for (i=0; i<metric_mass_bins; i++) {
    if (!x[i].count) continue;
    m = ((float)metric_min_mass_bin + i) / ((float)METRIC_BPDEX);
    nd = log10f(((float)(x[i].count*METRIC_BPDEX))/vol);
    fprintf(output, "%.2f %.3e %.3e %.3e %f %f %f %.2f %.2f %.3f %.3f %.3f %"PRId64" %f\n",
	    m, x[i].avg, x[i].sd, x[i].tol,
	    v[i].avg, v[i].sd, v[i].tol,
	    x[i].corr, v[i].corr, vmax[i].avg,
	    vmax[i].sd, vmax[i].tol, x[i].count, nd);
    fprintf(output2, "%.2f %.3e %.3e %.3e %.3e %.3e %.3e\n",
    	    m, x[i].min, x[i].max, v[i].min,
	    v[i].max, vmax[i].min, vmax[i].max);
  }

  fclose(output);
  fclose(output2);
}
Exemple #12
0
int main(int argc,char *argv[]){

  char f_out_name[BUFSIZ];
  double *A, *gh, *r, *c, *refl;
  int lgh,lr,lc,lA,lh;
  
  

  param_file_type param[Nparam] = 
    {{"gh:" ,"DATA_FILE" ,&gh  ,&lgh ,2*sizeof(double)},
     {"r:"  ,"DATA_FILE" ,&r   ,&lr  ,2*sizeof(double)},
     {"c:"  ,"DATA_FILE" ,&c   ,&lc  ,sizeof(double)},
     {"Al:" ,"DATA_FILE" ,&A   ,&lA  ,sizeof(double)},
     {"refl:","DATA_FILE",&refl,&lh  ,sizeof(double)},
     {"res1:","%s ",&f_out_name,NULL,0}};
    
  FILE *f_param;

  printf("\n ------ MTEX -- read paramater test -------- \n");

  if (argc<2) {
    printf("Error! Missing parameter - parameter_file.\n");
    printf("%s\n",argv[0]);
    abort();
  }
  
  /* read parameter file */
  f_param = check_fopen(argv[1],"r");
  if (read_param_file(f_param,param,Nparam,argc==2)+1<Nparam){
    printf("Some parameters not found!");
    abort();
  }
  fclose(f_param);

  printf("\n print A: \n");

  print_double(stdout,A,lA);

  printf("\n");
  
  /* free memory */
  free(gh);
  free(r);
  free(refl);
  free(c);
  
  return(0);
}
Exemple #13
0
void output_particles(char *filename)
{
  char buffer[1024];
  int64_t i, j, id=0;
  FILE *output;
  struct particle *p2;
  sprintf(buffer, "%s/%s.list", OUTBASE, filename);
  output = check_fopen(buffer, "w");
  for (i=0; i<num_halos; i++) {
    if (!_should_print(halos+i, NULL)) continue;
    for (j=0; j<halos[i].num_p; j++) {
      p2 = p + halos[i].p_start + j;
      fprintf(output, "%f %f %f %f %f %f %"PRId64" %"PRId64"\n", p2->pos[0], p2->pos[1], p2->pos[2], p2->pos[3], p2->pos[4], p2->pos[5], p2->id, id);
    }
    id++;
  }
  fclose(output);
}
Exemple #14
0
void output_hmad(char *filename) {
  char buffer[1024];
  int64_t i, id=0;
  FILE *output;
  int64_t num_hp;

  qsort(halos, num_halos, sizeof(struct halo), compare_masses);
  for (i=0; i<num_halos; i++) {
    if (!_should_print(halos+i, NULL)) continue;
    num_hp = populate_copies(halos+i,halos[i].pos,0);
    qsort(output_p, num_hp, sizeof(struct particle), sort_radii);

    sprintf(buffer, "%s/%s_%"PRId64".list", OUTBASE, filename, id);
    output = check_fopen(buffer, "w");

    if (!FULL_PARTICLE_CHUNKS)
      fprintf(output, "%f %f %f\n", halos[i].pos[0], halos[i].pos[1],
	      halos[i].pos[2]);
    print_bound_particles(halos+i, output, num_hp);
    id++;
    fclose(output);
  }
}
int
main( int argc, char *argv[] )
{
    int iarg = 0, nfiles = 0, ncols = 0;
    int j;
    size_t pair_count_total = 0;
    char *out_file, *w1_file, *w2_file, *pair_file;
    int njack;
    WEIGHT_SET ws1, ws2;
    BINS *bins;
    PAIR_FILE pf;

    if( argc < 5 ) {
        printf( "Usage:  %s OUTPUT_BASE  WEIGHT1  WEIGHT2  *PAIR_FILES\n", argv[0] );
        return ( 0 );
    }

    out_file = argv[1];
    w1_file = argv[2];
    w2_file = argv[3];
    iarg = 4;

    /* read in weights */
    fprintf( stderr, "Reading weight1 file: %s\n", w1_file );
    ncols = ws_read_ascii( &ws1, w1_file, 3 );
    fprintf( stderr, "  found %d columns\n", ncols );
    fprintf( stderr, "Reading weight2 file: %s\n", w2_file );
    ncols = ws_read_ascii( &ws2, w2_file, 3 );
    fprintf( stderr, "  found %d columns\n", ncols );

    njack = ws_get_njack( &ws1 );
    if( njack != ws_get_njack( &ws2 ) ) {
        fprintf( stderr, "ERROR: different number of jackknife samples! (%d != %d)\n",
                 njack, ws_get_njack( &ws2 ) );
        exit( EXIT_FAILURE );
    }

    if( njack < 2 ) {
        fprintf( stderr, "ERROR: need at least 2 jackknife samples! (njack = %d)\n", njack );
        exit( EXIT_FAILURE );
    }

    fprintf( stderr, "Initializing bins...\n" );
    bins = ( BINS * ) check_alloc( njack, sizeof( BINS ) );

    for( j = 0; j < njack; j++ ) {
//         bins[j] = bins_alloc( 2, 10, 10 ); /* two dimensions: rp, pi */
//         bins_init_dim( &bins[j], 0, 0,  10.0, BINS_LINEAR ); /* rp */
//         bins_init_dim( &bins[j], 1, 0,  10.0, BINS_LINEAR ); /* pi */

//         bins[j] = bins_alloc( 2, 21, 7 );       /* two dimensions: rp, pi */
//         bins_init_dim( &bins[j], 0, 0.1, 42.17, BINS_LOG );     /* rp */
//         bins_init_dim( &bins[j], 1, 0.0, 70.00, BINS_LINEAR );  /* pi */

        bins[j] = bins_alloc( 2, 22, 20 );
        bins_init_dim( &bins[j], 0, 0.1, 56.234133, BINS_LOG );
        bins_init_dim( &bins[j], 1, 0.0, 100.0, BINS_LINEAR );

//         bins[j] = bins_alloc( 2, 5, 20 );
//         bins_init_dim( &bins[j], 0, 0.0,  20.0, BINS_LINEAR);
//         bins_init_dim( &bins[j], 1, 0.0, 100.0, BINS_LINEAR);
    }

    for( nfiles = 0; iarg < argc; iarg++ ) {
        int i;
        size_t nread = 10000;
        size_t pair_count = 0;

        PAIR_PROJ ps[nread];

        pair_file = argv[iarg];
        fprintf( stderr, "Processing pairs in %s\n", pair_file );

        pf = pf_open_read( pair_file );
        nfiles += 1;

        while( pair_count < pf_nrows( &pf ) ) {
            nread = pf_read_proj( &pf, ps, nread );
            pair_count += nread;

            for( i = 0; i < nread; i++ ) {
                int i1 = ps[i].id1;
                int i2 = ps[i].id2;

                double w = ws_get_weight( &ws1, i1 ) * ws_get_weight( &ws2, i2 );

                if( w == 0 )
                    continue;
                for( j = 0; j < njack; j++ ) {
                    if( j != ws_get_jackid( &ws1, i1 ) )
                        if( j != ws_get_jackid( &ws2, i2 ) )
                            bins_add_pair_weight( &bins[j], w, ps[i].rp, ps[i].pi );
                }
            }
        }

        pf_cleanup( &pf );
        pair_count_total += pair_count;
    }
    fprintf( stderr, "Completed %zu pairs over %d file%s\n",
             pair_count_total, nfiles, nfiles > 1 ? "s" : "" );
    {
        double *wtj1, *wtj2;
        wtj1 = ws_weight_total_jack( &ws1 );
        wtj2 = ws_weight_total_jack( &ws2 );
        for( j = 0; j < njack; j++ ) {
            /* post processing and output */
            FILE *fp;
            {
                double norm;

                norm = ( wtj1[j] * wtj2[j] );
                bins_normalize( &bins[j], norm );
            }
            /* munge output file to include jack_id */
            {
                char *fn;
                size_t len;

                len = strlen( out_file ) + 40;
                fn = ( char * )check_alloc( len, sizeof( char ) );
                snprintf( fn, len, "%s.%dj%03d.dat", out_file, njack, j );

                fprintf( stderr, "Writing output: %s \n", fn );
                fp = check_fopen( fn, "w" );
                free( fn );
            }
            /* write output to ASCII file, with header info */
            fprintf( fp, "# pair_file: %s\n", pair_file );
            fprintf( fp, "# w1_file:   %s\n", w1_file );
            fprintf( fp, "# w1_total:  %.10g (full %.10g over %d objects)\n", wtj1[j], ws1.wt,
                     ws1.ct );
            fprintf( fp, "# w2_file:   %s\n", w2_file );
            fprintf( fp, "# w2_total:  %.10g (full %.10g over %d objects)\n", wtj2[j], ws2.wt,
                     ws2.ct );
            bins_fprint( &bins[j], fp );
            fclose( fp );
            bins_cleanup( &bins[j] );
        }
        free( wtj1 );
        free( wtj2 );
    }
    fprintf( stderr, "DONE!\n" );

    ws_clean( &ws1 );
    ws_clean( &ws2 );

    return ( 0 );
}
void read_hlist(char *filename) {
  int64_t n, c=0;
  FILE *input;
  struct halo h = {0};
  char buffer[1024];

  SHORT_PARSETYPE;
  #define NUM_INPUTS 33
  enum short_parsetype stypes[NUM_INPUTS] = 
    { D64, D64, F, F, F,  //  #id desc_id mvir vmax vrms
      F, F, D64, F,       //  Rvir Rs Np x 
      F, F, F, F, F,      // y z vx vy vz 
      F, F, F, F, F,      // JX JY JZ Spin rs_klypin
      F, F, F, F, F,      // M_all M1 M2 M3 M4
      F, F, F, F, F,      // Xoff Voff spin_bullock b_to_a c_to_a 
      F, F, F, F,         // A[x] A[y] A[z] T/|U|
    };
  enum parsetype types[NUM_INPUTS];
  void *data[NUM_INPUTS] = {&(h.id),
                            &(h.descid),
			    &(h.mvir), &(h.vmax), &(h.vrms), &(h.rvir), &(h.rs), 
			    &(h.np), 
			    &(h.pos[0]), &(h.pos[1]), &(h.pos[2]), 
			    &(h.vel[0]), &(h.vel[1]), &(h.vel[2]),
			    &(h.J[0]), &(h.J[1]), &(h.J[2]), &(h.spin),
			    &(h.klypin_rs), &(h.m_all), &(h.alt_m[0]), 
			    &(h.alt_m[1]), &(h.alt_m[2]), &(h.alt_m[3]),
			    &(h.Xoff), &(h.Voff), &(h.bullock_spin), 
			    &(h.b_to_a), &(h.c_to_a), &(h.A[0]), 
			    &(h.A[1]), &(h.A[2]), &(h.kin_to_pot)};
  

  for (n=0; n<NUM_INPUTS; n++) types[n] = stypes[n];
  input = check_fopen(filename, "r");
  while (fgets(buffer, 1024, input)) {
    if (buffer[0] == '#') {
      if (c==0) {
	c=1;
	buffer[strlen(buffer)-1] = 0;
	printf("%s PID\n", buffer);
      } else {
	printf("%s", buffer);
      }
    }
    n = stringparse(buffer, data, (enum parsetype *)types, NUM_INPUTS);
    if (n<NUM_INPUTS) continue;
    if (!(all_halos.num_halos%3000))
      all_halos.halos = check_realloc(all_halos.halos, sizeof(struct halo)*(all_halos.num_halos+3000), "Allocating Halos.");
   
    all_halos.halos[all_halos.num_halos] = h;
    all_halos.num_halos++;
  }
  fclose(input);
  
  all_halos.halos = check_realloc(all_halos.halos, sizeof(struct halo)*all_halos.num_halos, "Allocating Halos.");

  for (n=0; n<all_halos.num_halos; n++) {
    all_halos.halos[n].vmax_r = all_halos.halos[n].rvir;
  }

  find_parents(all_halos.num_halos);

  for (n=0; n<all_halos.num_halos; n++) {
    struct halo *th = all_halos.halos + n;
    printf("%"PRId64" %"PRId64" %.3e %.2f %.2f %.3f %.3f %"PRId64" %.5f %.5f %.5f %.2f %.2f %.2f %.3e %.3e %.3e %.5f %.5f %.4e %.4e %.4e %.4e %.4e %.5f %.2f %.5f %.5f %.5f %.5f %.5f %.5f %.4f %"PRId64"\n",
	   th->id, th->descid, th->mvir, th->vmax, th->vrms, th->rvir, th->rs,
	   th->np, th->pos[0], th->pos[1], th->pos[2], th->vel[0], th->vel[1],
	   th->vel[2], th->J[0], th->J[1], th->J[2], th->spin,
	   th->klypin_rs, th->m_all, th->alt_m[0], th->alt_m[1], th->alt_m[2],
	   th->alt_m[3], th->Xoff, th->Voff, th->bullock_spin, th->b_to_a,
	   th->c_to_a, th->A[0], th->A[1], th->A[2], th->kin_to_pot, th->pid);
  }
}
Exemple #17
0
int main (int argc, char *argv[]){

	mpfr_t *lambda, *kappa;
	
	char f_out_name[BUFSIZ];
	int d,iters,n, k, nk,nf,na,nb;
	double *lambdas, *kappas, *f,*a,*b;
		
	param_file_type param[Nparam] = 
    {{"d:"     , "%d", &d,       NULL, sizeof(int)},
     {"iter:"  , "%d", &iters,   NULL, sizeof(int)},
	 {"lambda:", "DATA_FILE", &lambdas, &n , sizeof(double)},
	 {"kappa:", "DATA_FILE", &kappas, &nk , sizeof(double)},  /*psi*/
	 {"h:", "DATA_FILE", &f, &nf , sizeof(double)},           /*odf*/
	 {"a:", "DATA_FILE", &a, &na , sizeof(double)},          /*pdf a*/
	 {"bc:", "DATA_FILE", &b, &nb , sizeof(double)},          /*pdf sqrt(b^2 + c^2)*/
	 {"res1:","%s ", &f_out_name,NULL, 0}};
    
	FILE *f_param;
	FILE *f_out;

	if (argc<2) {
		printf("Error! Missing parameter - parameter_file.\n");
		printf("%s\n",argv[0]);
		abort();
	}
	  
	/* read parameter file */
	f_param = check_fopen(argv[1],"r");
	if (read_param_file(f_param,param,Nparam,argc==2)<Nparam){	
		/*printf("Some parameters not found!");*/
		/*abort();*/
	}
	fclose(f_param);
	
	/* precission & delta */
	init_prec(d);
	
	long int prec = d;
	
	
	if (nk>0) {   /* kappas given*/
		mpfr_t C;
		
		kappa = (mpfr_t*) malloc (nk*sizeof(mpfr_t));
		
		for(k=0;k<nk;k++){ 
			mpfr_init2(kappa[k],prec);
			mpfr_init_set_d(kappa[k], kappas[k],prec);
		}
		
		mhyper(C, kappa, nk);
		
		/* gmp_printf("C: %.*Fe \n ", 20, C);	*/
		
		if (nf>0) /* eval odf values*/
		{
			eval_exp_Ah(C,f,nf);
			
			f_out = check_fopen(f_out_name,"wb");
			fwrite(f,sizeof(double),nf,f_out);
			fclose(f_out);
		} 
		if ( na > 0) {/* eval pdf values*/
		
		/* testing BesselI[0,a]
			mpfr_t in, out;					
						
			for(k=0;k<na;k++){
			
				mpfr_init2(in, prec);
				mpfr_set_d(in,	a[k],prec);
				mpfr_init2(out, prec);
				
				mpfr_i0(out, in, prec);
				
				
				mpfr_printf ("%.1028RNf\ndd", out);
				
				a[k] = mpfr_get_d(out,prec);
				
			}
				
			f_out = check_fopen(f_out_name,"wb");
			fwrite(a,sizeof(double),na,f_out);
			fclose(f_out);
		*/
		
			
			eval_exp_besseli(a,b,C,na);
				
			f_out = check_fopen(f_out_name,"wb");
			fwrite(a,sizeof(double),na,f_out);
			fclose(f_out);
		}
		
		if (nf == 0 && na == 0) { /* only return constant */
	
			double CC = mpfr_get_d(C,prec);
			
			f_out = check_fopen(f_out_name,"wb");
			fwrite(&CC,sizeof(double),1,f_out);
			fclose(f_out);
		
		}
	
	} else {	/* solve kappas */
		/* copy input variables */	
		lambda = (mpfr_t*) malloc (n*sizeof(mpfr_t));
		kappa = (mpfr_t*) malloc (n*sizeof(mpfr_t));	
		
		for(k=0;k<n;k++){ 
				mpfr_init_set_ui(kappa[k],0,prec);
				mpfr_init_set_d(lambda[k],lambdas[k],prec);
			}
			
				
		if(iters>0){		
			/* check input */
			mpfr_t tmp;
			mpfr_init(tmp);
			mpfr_set_d(tmp,0,prec);
			
			for(k=0;k<n;k++){
				mpfr_add(tmp,tmp,lambda[k],prec);
			}
			
			mpfr_ui_sub(tmp,1,tmp,prec);
			mpfr_div_ui(tmp,tmp,n,prec);
			
			for(k=0;k<n;k++){ 		
				mpfr_add(lambda[k],lambda[k],tmp,prec);
			}
			
			mpfr_init2(tmp,prec);
			for(k=0;k<n;k++){
				mpfr_add(tmp,tmp,lambda[k],prec);
			}
			
			mpfr_init2(tmp,prec);
			if( mpfr_cmp(lambda[min_N(lambda,n)],tmp) < 0 ){
				printf("not well formed! sum should be exactly 1 and no lambda negativ");
				exit(0);
			}
			
			
			/* solve the problem */	
			newton(iters,kappa, lambda, n);

		} else {
			guessinitial(kappa, lambda, n);
		}
		
				
		for(k=0;k<n;k++){
			lambdas[k] = mpfr_get_d(kappa[k],GMP_RNDN);	/* something wents wront in matlab for 473.66316276431799;*/
								/* % bug:   lambda= [0.97 0.01 0.001];*/
		}
			
		f_out = check_fopen(f_out_name,"wb");
		fwrite(lambdas,sizeof(double),n,f_out);
		fclose(f_out);
		
		free_N(lambda,n);
		free_N(kappa,n);
		
	}
	
	
	

	
  
	return EXIT_SUCCESS;
}
Exemple #18
-4
void output_ascii(int64_t id_offset, int64_t snap, int64_t chunk, float *bounds) {
  char buffer[1024];
  int64_t i, id=0;
  FILE *output;
  struct halo *th;
  get_output_filename(buffer, 1024, snap, chunk, "ascii");
  //if (PARALLEL_IO) 
  output = check_fopen(buffer, "w");
  //else output = stdout;

  fprintf(output, "#id num_p m%s mbound_%s r%s vmax rvmax vrms x y z vx vy vz Jx Jy Jz E Spin PosUncertainty VelUncertainty bulk_vx bulk_vy bulk_vz BulkVelUnc n_core\n", MASS_DEFINITION, MASS_DEFINITION, MASS_DEFINITION);
  print_ascii_header_info(output, bounds);

  for (i=0; i<num_halos; i++) {
    if (!_should_print(halos+i, bounds)) continue;
    th = halos+i;
    fprintf(output, "%"PRId64" %"PRId64" %.3e %.3e"
	    " %f %f %f %f %f %f %f %f %f %f %g %g %g %g %g %f %f %f %f %f %f %"PRId64"\n", id+id_offset,
	    th->num_p, th->m, th->mgrav, th->r,	th->vmax, th->rvmax, th->vrms,
	    th->pos[0], th->pos[1], th->pos[2], th->pos[3], th->pos[4],
	    th->pos[5], th->J[0], th->J[1], th->J[2], th->energy, th->spin,
	    sqrt(th->min_pos_err), sqrt(th->min_vel_err), th->bulkvel[0],
	    th->bulkvel[1], th->bulkvel[2], sqrt(th->min_bulkvel_err),
	    th->n_core);
    id++;
  }
  //if (PARALLEL_IO) 
  fclose(output);
}
Exemple #19
-7
void output_full_particles(int64_t id_offset, int64_t snap, int64_t chunk, float *bounds) {
  char buffer[1024];
  FILE *output;
  int64_t i, id=0;
  struct halo *th;

  if (chunk>=FULL_PARTICLE_CHUNKS) return;
  get_output_filename(buffer, 1024, snap, chunk, "particles");
  output = check_fopen(buffer, "w");

  fprintf(output, "#Halo table:\n");
  fprintf(output, "#id internal_id num_p m%s mbound_%s r%s vmax rvmax vrms x y z vx vy vz Jx Jy Jz energy spin\n", MASS_DEFINITION, MASS_DEFINITION, MASS_DEFINITION);
  fprintf(output, "#Particle table:\n");
  fprintf(output, "#x y z vx vy vz particle_id assigned_internal_haloid internal_haloid external_haloid\n");

  fprintf(output, "#Notes: As not all halos are printed, some halos may not have external halo ids.  (Hence the need to print internal halo ids).  Each particle is assigned to a unique halo; however, some properties (such as halo bound mass) are calculated including all substructure.  As such, particles belonging to subhalos are included in outputs; to exclude substructure, verify that the internal halo id is the same as the assigned internal halo id.\n");
  fprintf(output, "#Note also that halos with centers in particle ghost zones (outside the nominal boundaries) will not be printed, as their particle lists may be incomplete.\n");

  print_ascii_header_info(output, bounds);
  fprintf(output, "#Halo table begins here:\n");
  
  for (i=0; i<num_halos; i++) {
    th = halos+i;
    if (_should_print(th, bounds)) {
      th->id = id+id_offset;
      id++;
    } else { th->id = -1; }

    fprintf(output, "#%"PRId64" %"PRId64" %"PRId64" %.3e %.3e"
	    " %f %f %f %f %f %f %f %f %f %f %g %g %g %g %g\n", th->id, i,
	    th->num_p, th->m, th->mgrav, th->r, th->vmax, th->rvmax, th->vrms,
	    th->pos[0], th->pos[1], th->pos[2], th->pos[3], th->pos[4],
	    th->pos[5], th->J[0], th->J[1], th->J[2], th->energy, th->spin);
  }

  fprintf(output, "#Particle table begins here:\n");
  for (i=0; i<num_halos; i++) print_child_particles(output, i, i, halos[i].id);
  fclose(output);
  get_output_filename(buffer, 1024, snap, chunk, "particles");
  //gzip_file(buffer);
}
Exemple #20
-10
float find_previous_mass(struct halo *h, struct particle *hp, int64_t *best_num_p) {
  int64_t i, j, last_chunk = -1, max_particles, best_particles = 0;
  int64_t cur_part, remaining;
  char buffer[1024];
  struct previous_halo *tph, *best_ph=NULL;
  struct litehash *lh = NULL;
  FILE *input = NULL;

  *best_num_p = 0;
  if (h->num_p < 100 || !num_prev_halos) return 0;
  fast3tree_find_sphere(phtree, phtree_results, h->pos, h->r);
  if (!phtree_results->num_points) return 0;
  for (i=0; i<phtree_results->num_points; i++) {
    if (!prev_halo_acceptable(h, phtree_results->points[i])) {
      phtree_results->num_points--;
      phtree_results->points[i] = phtree_results->points[phtree_results->num_points];
      i--;
    }
  }
  if (!phtree_results->num_points) return 0;

  qsort(phtree_results->points, phtree_results->num_points, sizeof(struct previous_halo *),
	sort_previous_halos);

  max_particles = MAX_CORE_PARTICLES;
  if (max_particles >= h->num_p) max_particles = h->num_p;
  else convert_and_sort_core_particles(h, hp);
  
  lh = new_litehash(8);
  for (i=0; i<max_particles; i++)
    lh_setval(lh, &(p[hp[i].id].id), (void *)1);

  for (i=0; i<phtree_results->num_points; i++) {
    tph = phtree_results->points[i];
    remaining = tph->num_p;
    cur_part = 0;
    if (remaining > MAX_CORE_PARTICLES) remaining = MAX_CORE_PARTICLES;

    if (tph->file_offset >= 0) {
      if (tph->chunk != last_chunk) {
	if (last_chunk > -1) fclose(input);
	get_output_filename(buffer, 1024, prev_snap, tph->chunk, "bin");
	input = check_fopen(buffer, "rb");
	last_chunk = tph->chunk;
      }
      check_fseeko(input, tph->file_offset, SEEK_SET);
      check_fread(prev_pids, sizeof(int64_t), remaining, input);
    } else {
      memcpy(prev_pids, hid_cache+tph->p_offset, sizeof(int64_t)*remaining);
    }

    for (j=0; j<remaining; j++)
      if (lh_getval(lh, prev_pids + j)) cur_part++;

    if (cur_part > best_particles) {
      best_particles = cur_part;
      best_ph = tph;
    }
  }

  if (last_chunk > -1) fclose(input);
  free_litehash(lh);
  if (best_ph && best_ph->m > 1e13 && best_particles > max_particles*0.1) {
    fprintf(stderr, "Hnow: %f %f %f (%"PRId64"; %e); Phalo: %f %f %f (%e); %"PRId64"\n",
	    h->pos[0], h->pos[1], h->pos[2], h->num_p, h->num_p*PARTICLE_MASS,
	    best_ph->pos[0], best_ph->pos[1], best_ph->pos[2], best_ph->m,
	    best_particles);
  }
  *best_num_p = best_particles;
  if (best_ph && (best_particles > max_particles*0.1)) return best_ph->m;
  return 0;
}
Exemple #21
-29
void load_prev_binary_halos(int64_t snap, int64_t chunk, float *bounds, int64_t our_chunk) {
  FILE *input;
  char buffer[1024];
  struct binary_output_header bh;
  struct halo h;
  int64_t remaining = 0, to_read, i,j, h_start = num_prev_halos;
  double v_to_dx;

  get_output_filename(buffer, 1024, snap, chunk, "bin");
  input = check_fopen(buffer, "rb");
  check_fread(&bh, sizeof(struct binary_output_header), 1, input);
  assert(bh.magic == ROCKSTAR_MAGIC);
  assert(bh.num_halos >= 0);
  assert(bh.num_particles >= 0);

  //Conversion in Comoving Mpc/h / (km/s)
  //Note that the time units are in 1/H = 1/(h*100 km/s/Mpc)
  v_to_dx = 0.01*(scale_to_time(SCALE_NOW) - scale_to_time(bh.scale)) / 
    (0.5*(SCALE_NOW + bh.scale));

  remaining = bh.num_halos;
  while (remaining > 0) {
    to_read = PREV_HALO_BUFFER_SIZE;
    if (to_read > remaining) to_read = remaining;
    check_fread(prev_halo_buffer, sizeof(struct halo), to_read, input);
    remaining -= to_read;
    for (i=0; i<to_read; i++) {
      for (j=0; j<3; j++) 
	prev_halo_buffer[i].pos[j] += v_to_dx*prev_halo_buffer[i].pos[j+3];
      h = prev_halo_buffer[i];
      if (!bounds || _check_bounds(prev_halo_buffer[i].pos, h.pos, bounds) ||
	  our_chunk == chunk)
	add_to_previous_halos(&h, &bh);
    }
  }
  if (chunk == our_chunk) {
    hid_cache = check_realloc(hid_cache, sizeof(int64_t)*bh.num_particles,
			      "Allocating halo id cache");
    check_fread(hid_cache, sizeof(int64_t), bh.num_particles, input);
    for (i=h_start; i<num_prev_halos; i++)
      ph[i].file_offset = -1;
  }
  fclose(input);
}