Beispiel #1
0
int
group_stats( char *bgc_file )
{
    FILE *fp;

    OUTPUT_HEADER hdr;

    int ngroups_read = 0;

    fprintf( stderr, "Reading BGC file: %s\n", bgc_file );
//     fprintf(stdout, "# from BGC file: %s\n", bgc_file);
    fflush( stderr );
    fp = fopen( bgc_file, "r" );
    if( fp == NULL ) {
        fprintf( stderr, "ERROR: problem opening file '%s'\n", bgc_file );
        assert( fp != NULL );
    }

    bgc_read_header( fp, &hdr );

    if( PDATA_FORMAT_PV == hdr.format ) {
//         fprintf(stdout, "# 11 columns: gid(0) npart(1) group_mass(2) pos_com(3,4,5) vel_com(6,7,8) vdisp(9) periodic_needed(10)\n");
        ngroups_read = calc_stats_com( fp, hdr );
    } else if( PDATA_FORMAT_PVBE == hdr.format ) {
//         ngroups_read = calc_stats_mbp(fp, hdr);
        fprintf( stderr, "USING BINDING ENERGY IS NOT YET IMPLEMENTED!\n" );
        return ( EXIT_FAILURE );
    } else {
        fprintf( stderr, "ERROR: skipping '%s' -- PDATA_FORMAT not compatible (%d)\n", bgc_file,
                 hdr.format );
        return ( EXIT_FAILURE );
    }

    return ngroups_read;
}
Beispiel #2
0
int
group_stats( char *bgc_file, const double eps )
{
    FILE *fp;

    OUTPUT_HEADER hdr;

    int ngroups_read = 0;

    fprintf( stderr, "Reading BGC file: %s\n", bgc_file );
    fflush( stderr );
    fp = fopen( bgc_file, "r" );
    if( fp == NULL ) {
        fprintf( stderr, "ERROR: problem opening file '%s'\n", bgc_file );
        assert( fp != NULL );
    }

    bgc_read_header( fp, &hdr );

    if( PDATA_FORMAT_PV == hdr.format ) {
        ngroups_read = calc_stats_dpp( fp, hdr, eps );
        return ( EXIT_FAILURE );
    } else {
        fprintf( stderr, "ERROR: skipping '%s' -- PDATA_FORMAT not compatible (%d)\n", bgc_file,
                 hdr.format );
        return ( EXIT_FAILURE );
    }

    return ngroups_read;
}
Beispiel #3
0
/*
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "/home/piscioja/NRC/include/nr.h"
#include "/home/piscioja/halobias/bgc_read_utils.c"

#define sqr(x) ((x)*(x))
#define max(A,B) ((A) > (B) ? (A) : (B))
#define min(A,B) ((A) < (B) ? (A) : (B))
#define mabs(A) ((A) < 0.0 ? -(A) : (A))
#define cnint(x) ((x-floor(x)) < 0.5 ? floor(x) : ceil(x))
#define csign(x) (x < 0.0 ? -1 : 1)
#define PI (3.141592)
#define epsilon 1e-9
#define MAXLEN (5000)


int main()
{

double get_Mmin0(double , double , double , double , double , char* , char*);
double logMmin;
char bgc_file[MAXLEN],mf_file[MAXLEN];

snprintf(bgc_file,MAXLEN,"/net/bender/data0/LasDamas/Esmeralda/3011/fof_b0p2/Esmeralda_3011_z0p000_fof_b0p2.0000.bgc");
//snprintf(halos_file,MAXLEN,"/net/bender/data0/LasDamas/Consuelo/4004/Consuelo_4004_z0p054_fof_b0p2.com.halos");
snprintf(mf_file,MAXLEN,"/hd0/Research/Clustering/Emcee_test/3011_mass_function");

logMmin=get_Mmin0(0.0063,0.1,11.0,12.5,0.1,bgc_file,mf_file);

fprintf(stderr,"get_Mmin0> logMmin = %5.2f\n",logMmin) ;

return 0;


}
*/
double get_Mmin0(double rhogal, double siglogM, double logM0, double logM1, double alpha, char *bgc_file, char *mf_file)

{
	fprintf(stderr,"In get_Min0\n");
  int i,j ;
/*---Arguments-------------------------*/

  double M0,M1 ;
  FILE *fp1,*fp2 ;
/*---Halo-files------------------------*/

  OUTPUT_HEADER hdr ;
  int junki,Nhalos,Npart ;
  double junkf,mp,Lbox,Mhalo,logMhalo ;
  int Nbin,*Nhalosbin ;
  double logMbin1,logMbin2,dlogMbin ;
/*---Bias-parameters-------------------*/
  double logMh,Mh,Ncenavg,Nsatavg,Ngalaxies,Ngaltarget ;
  double logMmin,fNgal ;
  void Printhelp(void) ;

/*---Read-Arguments------------------------------------------------------------*/
  M0=pow(10.,logM0) ;
  M1=pow(10.,logM1) ;



/*---Read-BGC-header-----------------------------------------------------------*/

  fp1=fopen(bgc_file,"r") ;
  
  fprintf(stderr,"%s\n",bgc_file);
  assert(fp1 != NULL);
  bgc_read_header(fp1,&hdr);
  fclose(fp1) ;
  Nhalos=hdr.ngroups_total ;
  mp=hdr.part_mass*1e10 ;
  Lbox=hdr.BoxSize ;

  Ngaltarget = rhogal*Lbox*Lbox*Lbox ;

/*---Read-halo-center-file-and-build-mass-function-----------------------------*/

  logMbin1 = 10 ;
  logMbin2 = 16 ;
  dlogMbin = 0.01 ;
  Nbin = (int)((logMbin2-logMbin1)/dlogMbin) ;
  Nhalosbin=(int *) calloc(Nbin,sizeof(int)) ;

 


  fp2=fopen(mf_file,"r") ;
  fprintf(stderr,"%s\n",mf_file);
  assert(fp2 != NULL);



  for(i=0;i<Nbin;i++)
    {
      fscanf(fp2,"%lf %d",&junkf,&Nhalosbin[i]); 


    }

fclose(fp2);

/*---Loop-over-Mmin-values-------------------------------------------------------*/

  logMmin = 8. ;
  fNgal = 100. ;
  i=0;
  while(fNgal>1.01) 
    {
      i++;
      if(i>5E6)
	{
		fprintf(stderr,"Something has gone wrong with get_Mmin.  Check HOD\n");
		return -1;
	}
      logMmin += 0.01 ;
      Ngalaxies = 0. ;
      for(j=0;j<Nbin;j++)
	{
	  if(Nhalosbin[j]>0)
	    {
	      logMh = logMbin1 + j*dlogMbin + 0.5*dlogMbin ;
	      Mh = pow(10.,logMh) ;
	
/*---Navg(M)--------------------------------------------------------------------*/

	      Ncenavg = 0.5*(1 + erff((logMh-logMmin)/siglogM)) ;
	      
	      if(Mh>M0)
		{
		  Nsatavg = 0.5*(1 + erff((logMh-logMmin)/siglogM))*pow(((Mh-M0)/M1),alpha) ;
		}
	      else
		{
		  Nsatavg = 0. ;
		}
	      
	      Ngalaxies += (Ncenavg + Nsatavg)*(double)Nhalosbin[j] ;
	    }
	}

	fNgal = Ngalaxies/Ngaltarget ;
    }

  fprintf(stderr,"get_Mmin0> logMmin = %5.2f\n",logMmin) ;
//  fprintf(stdout,"%5.2f\n",logMmin) ;

  return logMmin ;
}
int
main( int argc, char **argv )
{
    FILE *fp;

    char *grp_file, *be_file, *bgc_file;

    int i, npart, max_gid;

    int miss_npart, miss_ngroup, miss_be;

    int *grp, *grpcount;

    float *be = NULL;;
    OUTPUT_HEADER hdr;

    int *nParticlesPerGroup;

    PARTICLE_DATA_IDBE *pdata;

    if( argc == 3 ) {
        grp_file = argv[1];
        be_file = NULL;
        bgc_file = argv[2];

    } else if( argc == 4 ) {
        grp_file = argv[1];
        be_file = argv[2];
        bgc_file = argv[3];
    } else {
        printf( "Usage: \n" );
        printf( "  %s GRP_file BGC_file          :: checks particle IDs in groups \n", argv[0] );
        printf( "  %s GRP_file BE_file  BGC_FILE :: checks both IDs and binding energy \n",
                argv[0] );
        exit( EXIT_FAILURE );
    }

    printf( "Reading GRP file: %s\n", grp_file );
    fflush( stdout );
    grp = read_grp( grp_file, &npart );
    printf( "  there are %d particles in this GRP file\n", npart );

    if( be_file ) {
        int be_npart;

        printf( "Reading BE file: %s\n", be_file );
        fflush( stdout );
        be = read_be( be_file, &be_npart );
        printf( "  there are %d particles in this BE file\n", be_npart );
        if( npart != be_npart ) {
            puts( "ERROR: GRP and BE files don't have the same number of particles" );
            exit( EXIT_FAILURE );
        }
    }

    printf( "Opening BGC file: %s\n", bgc_file );
    fflush( stdout );
    fp = fopen( bgc_file, "r" );
    assert( fp != NULL );

    printf( "Reading BGC header...\n" );
    bgc_read_header( fp, &hdr );
    printf( "  there are %d particles in this BGC file\n", hdr.npart );

    /* assume BGC corresponds to GRP file, so constants are consistent - but check it */
    if( npart != hdr.npart_orig ) {
        printf
            ( "ERROR: GRP and BGC files don't seem to match (original particle numbers are mismatched)\n" );
        exit( EXIT_FAILURE );
    }
    max_gid = hdr.ngroups_total;

    grpcount = calloc( max_gid + 1, sizeof( int ) );
    assert( grpcount != NULL );

    for( i = 1; i <= npart; i++ ) {
        int gid = grp[i];

        assert( gid <= max_gid );
        grpcount[gid] += 1;
    }

    printf( "Reading group list for %d groups\n", hdr.ngroups );
    nParticlesPerGroup = bgc_read_grouplist( fp, hdr );

    // read particles, FORMAT IS HARDCODED!
    size_t size = bgc_sizeof_pdata( PDATA_FORMAT_IDBE );

    pdata = malloc( size * hdr.max_npart );
    assert( pdata != NULL );

    miss_npart = miss_ngroup = miss_be = 0;

    printf( "\nVerifying groups from BGC file: GID %d to %d ... \n", hdr.first_group_id,
            hdr.first_group_id + hdr.ngroups - 1 );
    printf( "(this is verifying %d of %d total groups)\n", hdr.ngroups, hdr.ngroups_total );
    if( be_file ) {
        printf( "\nBinding Energy Comparison: difference accurate to less than %e\n",
                FLOAT_EPSILON );
    }

    for( i = 0; i < hdr.ngroups; i++ ) {        // read in group chunks and compare
        int k, grpgid;

        int npart = nParticlesPerGroup[i];

        int gid = i + hdr.first_group_id;

        assert( npart <= hdr.max_npart );
        bgc_read_part_into( fp, npart, PDATA_FORMAT_IDBE, pdata );

        if( grpcount[gid] != npart ) {
            // check to see if GRP and BGC disagree with the number of particles
            // if they weren't the same, the particle check might not be comprehensive, since we use 
            // the BGC catalog to loop over.
            miss_ngroup++;
        }

        for( k = 0; k < npart; k++ ) {
            int id = pdata[k].part_id;

            grpgid = grp[id];
            if( grpgid != gid ) {
                miss_npart++;
            }
            if( be_file ) {
                float part_be = be[id];

                float bgc_be = pdata[k].binding_energy;

                if( !eql_float( part_be, bgc_be ) ) {
                    miss_be++;
                }
            }

        }
    }

    fclose( fp );
    free( grp );
    free( grpcount );
    free( pdata );
    if( be_file ) {
        free( be );
    }

    printf( "\nRESULTS:\n" );
    printf( "  group count mismatches: %d\n", miss_ngroup );
    printf( "  particle ID mismatches: %d\n", miss_npart );
    if( be_file ) {
        printf( "  binding energy misses:  %d\n", miss_be );
    }

    if( miss_npart || miss_ngroup || miss_be ) {
        puts( "ERROR: unsucessful match!" );
        return ( EXIT_FAILURE );
    } else {
        puts( "SUCCESS: groups match!" );
        return ( EXIT_SUCCESS );
    }

}