void enforceConstraints() { // Perfect vertical reflection. if (fBall.pos.fY < kBounds.fTop || fBall.pos.fY >= kBounds.fBottom) { fBall.spd.fY = -fBall.spd.fY; fBall.pos.fY = box_reflect(fBall.pos.fY, kBounds.fTop, kBounds.fBottom); } // Horizontal bounce - introduces a speed fuzz. if (fBall.pos.fX < kBounds.fLeft || fBall.pos.fX >= kBounds.fRight) { fBall.spd.fX = this->fuzzBallSpeed(-fBall.spd.fX); fBall.spd.fY = this->fuzzBallSpeed(fBall.spd.fY); fBall.pos.fX = box_reflect(fBall.pos.fX, kBounds.fLeft, kBounds.fRight); this->updatePaddleStrategy(); } }
int main(int argc, char** argv) { //printf("%f\n",GRID_NUM); struct timespec diff(struct timespec start, struct timespec end); struct timespec time1, time2; struct timespec time_stamp; int npart,i,j,Num; params param; mols mol; adjacent adj; param.npart = N_BODY_NUM; param.dt = DT; param.eps_lj = EPS; param.sig_lj = SIG; mol.x = malloc(2*param.npart * sizeof(float)); mol.v = malloc(2*param.npart * sizeof(float)); mol.a = malloc(2*param.npart * sizeof(float)); mol.F = malloc(2*param.npart * sizeof(float)); #if (NEAREST) double Blength = BLOCK_LENGTH(GRID_NUM,BOX_SIZE); printf("Blength: %lf\n",Blength); Num = EST_NUM(GRID_NUM,N_BODY_NUM); Num = 4*Num; if(!Num) { Num = 4; } if(N_BODY_NUM < Num) { Num = N_BODY_NUM; } adj.n = malloc(sizeof(int) * GRID_NUM * GRID_NUM * Num); memset(adj.n,-1,sizeof(int) * GRID_NUM * GRID_NUM * Num); printf("Num: %d\n",Num); #endif npart = init_particles(param.npart, mol.x , mol.v, ¶m); if(npart < param.npart) { fprintf(stderr, "Could not generate %d particles, Trying %d particles instead\n",param.npart,npart); param.npart = npart; } else { fprintf(stdout,"Generated %d particles\n",param.npart); } init_particles_va( param.npart, mol.v,mol.a, ¶m); #if(NEAREST) printf("Before gridSort\n"); gridSort(npart, GRID_NUM, Num, adj.n, mol.x); printf("After gridSort\n"); #endif /*for(i=0;i<npart;i++) printf("myBlockNum: %d\n",getMyBlock(param.npart,2*i, adj.n, Num/2)); for(i=0; i < param.npart; i++) { printf("nBody-Num: %d Posx: %f Velx: %f Accx: %f Forcex: %f\n",i, mol.x[2*i],mol.v[2*i],mol.a[2*i],mol.F[2*i]); printf("nBody-Num: %d Posy: %f Vely: %f Accy: %f Forcey: %f\n",i, mol.x[2*i+1],mol.v[2*i+1],mol.a[2*i+1],mol.F[2*i+1]); } */ #if(LINUX) clock_gettime(CLOCK_REALTIME, &time1); #endif #if(NEAREST) compute_forces_nearby(param.npart, adj.n, mol.x, mol.F, GRID_NUM, Num); #elif(OPT) compute_forces(param.npart,mol.x,mol.F); #else compute_forces_naive(param.npart,mol.x,mol.F); #endif printf("After First ComputeForces\n"); for(i=0;i<ITERS;i++) { #if(NEAREST) gridSort(npart, GRID_NUM, Num, adj.n, mol.x); // Added in the gridSort function for each iteration #endif verletInt1(param.npart,param.dt , mol.x, mol.v,mol.a); box_reflect(param.npart,mol.x,mol.v,mol.a ); #if(NEAREST) compute_forces_nearby(param.npart, adj.n, mol.x, mol.F, GRID_NUM, Num); #elif(OPT) compute_forces(param.npart,mol.x,mol.F); #else compute_forces_naive(param.npart,mol.x,mol.F); #endif verletInt2(param.npart,param.dt, mol.x, mol.v, mol.a); memset(mol.F, 0 , 2*param.npart * sizeof(float)); } #if(LINUX) clock_gettime(CLOCK_REALTIME, &time2); #endif /* for(i=0; i < param.npart; i++) { printf("nBody-Num: %d Posx: %f Velx: %f Accx: %f Forcex: %f\n",i, mol.x[2*i],mol.v[2*i],mol.a[2*i],mol.F[2*i]); printf("nBody-Num: %d Posy: %f Vely: %f Accy: %f Forcey: %f\n",i, mol.x[2*i+1],mol.v[2*i+1],mol.a[2*i+1],mol.F[2*i+1]); } */ #if(LINUX) double blength = BLOCK_LENGTH(GRID_NUM,BOX_SIZE); printf("Boxsize: %lf,Blocksize: %lf,MaxBodiesPerBlock: %d\n",BOX_SIZE,blength, maxNumPartPerBlock(blength,SIG)); time_stamp = diff(time1,time2); printf("Execution time: %lf\n",(double)((time_stamp.tv_sec + (time_stamp.tv_nsec/1.0e9)))); #else printf("Boxsize: %lf,BlockNum: %lf,MaxBodiesPerBlock: %d\n",BOX_SIZE,GRID_NUM, maxNumPartPerBlock(GRID_NUM,SIG)); #endif /* // test statements for the grid allocation int count =0; for(i=0;i<GRID_NUM*GRID_NUM*Num/2;i++) { if(adj.n[i]!=-1) { count++; } } printf("%d\n",count); */ #if(NEAREST) free(adj.n); #endif free(mol.x); free(mol.v); free(mol.a); free(mol.F); printf("Done\n"); return 0; }