Ejemplo n.º 1
0
/* Read particle data for one group into *pdata.  Assumes memory is properly allocated to 
 * contain FULL data.  One must cast the result appropriately to use it */
void
bgc_read_part_into( FILE * fp, const unsigned int npart, const int pdata_format, void *pdata )
{
    size_t size = bgc_sizeof_pdata( pdata_format );

    assert( pdata != NULL );
    ftread( pdata, size, npart, fp );

    return;
}
Ejemplo n.º 2
0
/* skip over group without reading it */
int
bgc_skip_particles( FILE * fp, const unsigned int npart, const int pdata_format )
{
    size_t size = bgc_sizeof_pdata( pdata_format );

    long offset;

    offset = npart * size + 2 * sizeof( int );

    return fseek( fp, offset, SEEK_CUR );
}
Ejemplo n.º 3
0
/* Read particle data for one group. One must cast the result appropriately */
void *
bgc_read_particles( FILE * fp, const unsigned int npart, const int pdata_format )
{
    void *pd;

    size_t size = bgc_sizeof_pdata( pdata_format );

    pd = calloc( npart, size );
    assert( pd != NULL );

    ftread( pd, size, npart, fp );

    return ( void * )pd;
}
Ejemplo n.º 4
0
void bgc_skip_to_haloid(FILE *fp, const OUTPUT_HEADER *hdr,int *nParticlesPerGroup,const unsigned int haloid)
{
  size_t bytes=0,size=0;
  int i;

  size = bgc_sizeof_pdata(hdr->format);
  
  for(i=0;i<haloid;i++) {
    bytes += sizeof(int);
    bytes += nParticlesPerGroup[i]*size;
    bytes += sizeof(int);
  }
  fseek(fp,bytes,SEEK_CUR);//done -> fp is now at the right location. The caller can verify

}
Ejemplo n.º 5
0
/* Read particle data for one group. One must cast the result appropriately */
void * bgc_read_particles(FILE *fp, const unsigned int npart, const int pdata_format)
{
    int pad;
    void *pd;
    size_t res;

    size_t size = bgc_sizeof_pdata(pdata_format);

    pd = malloc(npart * size);
    assert(pd != NULL);

    fread(&pad,sizeof(int),1,fp);
    assert(pad == npart * size);
    res = fread(pd,size,npart,fp);
    assert( res == npart );
    fread(&pad,sizeof(int),1,fp);
    assert(pad == npart * size);

    return (void*)pd;
}
Ejemplo n.º 6
0
/* Read particle data for one group. One must cast the result appropriately */
void bgc_read_part_into(FILE *fp, const unsigned int npart, const int pdata_format, void * pdata)
{
    int pad;
    size_t res;
    size_t size = bgc_sizeof_pdata(pdata_format);

    assert(pdata != NULL);
    
    fread(&pad,sizeof(int),1,fp);
    if( pad != npart * size ) {
      fprintf(stderr,"pad = %d (expected %zd)\n", pad, npart*size);
      fprintf(stderr,"npart = %d  particle_size = %zd\n", npart, size);
    }
    assert(pad == npart * size);
    res = fread(pdata,size,npart,fp);
    assert( res == npart );
    fread(&pad,sizeof(int),1,fp);
    assert(pad == npart * size);

    return;
}
Ejemplo n.º 7
0
int
calc_stats_com( FILE * fp, const OUTPUT_HEADER hdr )
{
    int i, k, n;

    int *nParticlesPerGroup;

    PARTICLE_DATA_PV *pdata;

    double plength_check = hdr.BoxSize / 5.0;

    /* allocate array of structures based on the biggest halo, which means only once per file */
    pdata = calloc( hdr.max_npart, bgc_sizeof_pdata( hdr.format ) );
    assert( pdata != NULL );

    nParticlesPerGroup = bgc_read_grouplist( fp, hdr );

    // read in by group chunks and process
    for( i = 0; i < hdr.ngroups; i++ ) {
        int gid = i + hdr.first_group_id;

        int npart = nParticlesPerGroup[i];

        int flag_periodic = 0;

        PARTICLE_DATA_PV pd;

        double xm[3], vm[3], vd[3], vdisp;

        double gmass = hdr.part_mass * ( double )npart;

        bgc_read_part_into( fp, npart, hdr.format, pdata );

        for( k = 0; k < 3; k++ ) {
            xm[k] = vm[k] = vd[k] = 0;
        }

        /* one pass stable algorithm by Knuth and/or Welford
         * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance */
        for( n = 0; n < npart; n++ ) {
            double del;

            pd = pdata[n];
//             if(gid == 347252)
//                 fprintf(stderr, "%4d", n);
            for( k = 0; k < 3; k++ ) {
                del = pd.pos[k] - xm[k];

//                 if(gid == 347252)
//                     fprintf(stderr, " %17.8f", del);

                /* now we have to check for periodic boundary wraparound */
                if( n > 0 ) {
                    /* given our algorithm, we want to skip the first particle */
                    if( del > plength_check ) {
                        flag_periodic = 1;
                        del = ( pd.pos[k] - hdr.BoxSize ) - xm[k];
                    }
                    if( del < -1.0 * plength_check ) {
                        flag_periodic = 1;
                        del = ( pd.pos[k] + hdr.BoxSize ) - xm[k];
                    }
                }
                xm[k] += del / ( n + 1 );

                del = pd.vel[k] - vm[k];
                vm[k] += del / ( n + 1 );

                vd[k] += del * ( pd.vel[k] - vm[k] );   // this is intended to use the updated mean value
            }
//             if(gid == 347252) 
//                 fprintf(stderr, "\n");
        }

        /* check if our COM is beyond the boundary of the box, and fix it if necessary */
        if( flag_periodic ) {
            for( k = 0; k < 3; k++ ) {
                if( xm[k] < 0 )
                    xm[k] += hdr.BoxSize;

                if( xm[k] > hdr.BoxSize )
                    xm[k] -= hdr.BoxSize;
            }
        }
        {
            double var = 0;

            for( k = 0; k < 3; k++ ) {
                var += ( vd[k] / npart );
            }
            vdisp = sqrt( var );
        }

        fprintf( stdout, "%8d %10d %15.6f", gid, npart, gmass );
        for( k = 0; k < 3; k++ ) {
            fprintf( stdout, " %12.4f", xm[k] );
        }
        for( k = 0; k < 3; k++ ) {
            fprintf( stdout, " %12.4f", vm[k] );
        }
        fprintf( stdout, " %12.4f", vdisp );
        fprintf( stdout, "  %d\n", flag_periodic );
    }

    free( pdata );
    return hdr.ngroups;
}
Ejemplo n.º 8
0
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 );
    }

}
Ejemplo n.º 9
0
int
calc_stats_dpp( FILE * fp, const OUTPUT_HEADER hdr, const double eps )
{
    int i, k, n;

    int *nParticlesPerGroup;

    PARTICLE_DATA_PV *pdata;

    double plength_check = hdr.BoxSize / 5.0;

    double neg_plength_check = -1.0 * plength_check;

    // ensure that the box-wrap around check is sufficiently large (> 10 Mpc)
    assert( plength_check > 10.0 );

    /* allocate array of structures based on the biggest halo, which means only once per file */
    pdata = calloc( hdr.max_npart, bgc_sizeof_pdata( hdr.format ) );
    assert( pdata != NULL );

    nParticlesPerGroup = bgc_read_grouplist( fp, hdr );

    // read in by group chunks and process
    for( i = 0; i < hdr.ngroups; i++ ) {
        int gid = i + hdr.first_group_id;

        int npart = nParticlesPerGroup[i];

        int flag_periodic = 0;

        double xsel[3], vm[3], vd[3];

        double vdisp;

        double gmass = hdr.part_mass * ( double )npart;

        double r2 = 0.0, pot_max = 0.0;

        float *x1, *x2;

        PARTICLE_DATA_PV pd;

        bgc_read_part_into( fp, npart, hdr.format, pdata );

        for( k = 0; k < 3; k++ ) {
            xsel[k] = vm[k] = vd[k] = 0.0;
        }

        for( n = 0; n < npart; n++ ) {
            int m;

            double del = 0.0, pot = 0.0;

            pd = pdata[n];
            x1 = pd.pos;

            // second loop over particles for potential calculation
            for( m = 0; m < npart; m++ ) {
                if( n == m ) {
                    continue;
                }
                x2 = pdata[m].pos;
                r2 = 0.0;
                for( k = 0; k < 3; k++ ) {
                    del = x1[k] - x2[k];

                    /* now we have to check for periodic boundary wraparound */
                    if( del > plength_check ) {
                        // del = (x1[k] - hdr.BoxSize) - x2[k];
                        del -= hdr.BoxSize;
                        flag_periodic = 1;
                    } else if( del < neg_plength_check ) {
                        // del = (x1[k] + hdr.BoxSize) - x2[k];
                        del += hdr.BoxSize;
                        flag_periodic = 1;
                    }
                    // assert( abs(del) < plength_check );
                    r2 += del * del;
                }
                pot += 1.0 / sqrt( r2 + eps * eps );
            }


            {   // check potential energy, update as necessary
                int update_pot = FALSE;

                if( n == 0 ) {
                    update_pot = TRUE;
                } else if( pot > pot_max ) {
                    update_pot = TRUE;
                }

                if( update_pot ) {
                    pot_max = pot;
                    for( k = 0; k < 3; k++ ) {
                        xsel[k] = pd.pos[k];
                    }
                }
            }

            /* one pass stable algorithm by Knuth and/or Welford
             * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance */
            for( k = 0; k < 3; k++ ) {
                del = pd.vel[k] - vm[k];
                vm[k] += del / ( n + 1 );

                vd[k] += del * ( pd.vel[k] - vm[k] );   // this is intended to use the updated mean value
            }
        }

        {   /* calculate velocity dispersion */
            double var = 0;

            for( k = 0; k < 3; k++ ) {
                var += ( vd[k] / npart );
            }
            vdisp = sqrt( var );
        }

        fprintf( stdout, "%8d %10d %15.6f", gid, npart, gmass );
        for( k = 0; k < 3; k++ ) {
            fprintf( stdout, " %12.4f", xsel[k] );
        }
        for( k = 0; k < 3; k++ ) {
            fprintf( stdout, " %12.4f", vm[k] );
        }
        fprintf( stdout, " %12.4f", vdisp );
        fprintf( stdout, "  %d\n", flag_periodic );
    }

    free( pdata );
    return hdr.ngroups;
}