proj home

Files   Classes   Functions   Hierarchy  

integration.h

Go to the documentation of this file.
00001 #ifndef INTEGRATION_H
00002 #define INTEGRATION_H
00003 
00004 #include <particle.h>
00005 #include <particledisp.h>
00006 #include <particlesampler.h>
00007 #include <typedefs.h>
00008 #include <zpr.h>
00009 
00010 // TODO rename integration to particleintbox
00011 //   remove template F as d2toindex is used.
00012 //   remove template D as it is part of the initial
00013 //   setup and hence no point optimizing. 
00014 //   The code becomes fixed, 
00015 //   The distributions code should use inheritance 
00016 
00017 
00018 // D is the distribution.
00019 // F is the index function.
00020 template< typename D, typename F >
00021 class integration
00022 {
00023   particledisp pd;
00024   D distribution;
00025   F fn; 
00026   cell C;
00027 public:
00028 
00029   long stepcount;
00030   gltextmsg stepcountmsg;
00031 
00032   uint numParticles;
00033   bool uniform;
00034   bool printstate;
00035   float h;
00036 
00037   bool displayEk;
00038 
00040   bool LJ; 
00041   double LJpsi;
00042   double LJsigma;
00043   void LJcalc();
00044 
00045   // Histogram parameters
00046   double histlow;
00047   double histhigh;
00048   uint histn;
00049 
00050   particlesampler sampler;
00051   uint samplerstate;
00052 
00053   particle * pi;
00054 
00055   uint * pu;
00056 
00057   framerate<> fr;
00058 
00059   integration();
00060 
00061   void reset(int argc, char**argv);
00062 
00063   void draw();
00064 
00065   void bruteforce();
00066 
00067   void bruteforce
00068   (
00069     uintc * p,
00070     uintc sz
00071   );
00072 
00073   bool run;
00074   void togglestartstop();
00075 
00076   void step()
00077   {
00078     if (!uniform)
00079       bruteforce();
00080     else
00081       uniformgridtest();
00082 
00083     ++stepcount;
00084 
00085     if (samplerstate)
00086       ++sampler;
00087   }
00088   void steplarge();
00089 
00090 
00091   void uniformgridtest();
00092 
00093 //  void steppersecupdate();
00094 
00095 };
00096 
00097 
00098 //---------------------------------------------------------
00099 // Implementation
00100 
00101 /*
00102 template< typename D, typename F >
00103 void integration<D,F>::steppersecupdate()
00104 {
00105   static int t0;
00106   static uint stepcount0;
00107 
00108   int t1 = glutGet(GLUT_ELAPSED_TIME);
00109   double t = 1000.0/(t1-t0);
00110   t0=t1;
00111 
00112   uint steppersec = t*(stepcount-stepcount0);
00113   stepcount0 = stepcount;
00114 
00115   steppersecmsg.updatevalue(steppersec,"step/s");
00116 }
00117 */
00118 
00119 
00120 
00121 
00122 
00123 template< typename D, typename F >
00124 void integration<D,F>::uniformgridtest()
00125 {
00126   boxcollision const & box(distribution.box);
00127   for (uint i=0; i<numParticles; ++i)
00128   {
00129     pi[i].step(h);
00130     box.walls(pi[i],h);
00131   }
00132 
00133   C.eval(fn);
00134 
00135 if (true&&printstate)
00136 {
00137 uint W(fn.W);
00138 uint count;
00139 int idx=0;
00140 cout << endl;
00141 for (uint i=0; i<W; ++i)
00142 {
00143   for (uint j=0; j<W; ++j)
00144   {
00145    
00146     count=0;
00147     for (uint k=0; k<numParticles; ++k)
00148     {
00149       if (idx==fn.index(k))
00150       {
00151         cout << k << ',';
00152         ++count;
00153       }
00154     }
00155     if (count==0)
00156       cout << "*";
00157     cout << " ";
00158     ++idx;
00159   }
00160   cout << endl;
00161 }
00162 }
00163 
00164   uint sz;
00165   for (uint i=0; i<numParticles; ++i)
00166   {
00167     C.getsurroundingparticles(pu,sz,fn,i);
00168 
00169 //cout << SHOW(i) << "   " << SHOW(sz) << "  ";
00170 //print(pu,pu+sz);
00171 
00172     bruteforce(pu,sz);
00173   }
00174 
00175 
00176 if (true&&printstate)
00177 {
00178 cout << endl;
00179 for (uint i=0; i<numParticles; ++i)
00180 {
00181   cout << fn.index(i) << " pi[" << i << "] ";
00182   C.getsurroundingparticles(pu,sz,fn,i);
00183   cout << "  ";
00184   for (uint k=0; k<sz; ++k)
00185     cout << pu[k] << " ";
00186   cout << "        ";
00187   cout << pi[i];
00188 cout << endl;
00189 }
00190 }
00191 
00192 
00193 if (true&&printstate)
00194 {
00195 uint W(fn.W);
00196 uint count;
00197 int idx=0;
00198 cout << endl;
00199 for (uint i=0; i<W; ++i)
00200 {
00201   for (uint j=0; j<W; ++j)
00202   {
00203    
00204     count=0;
00205     for (uint k=0; k<numParticles; ++k)
00206     {
00207       if (idx==fn.index(k))
00208       {
00209         cout << k << ',';
00210         ++count;
00211       }
00212     }
00213     if (count==0)
00214       cout << "*";
00215     cout << " ";
00216     ++idx;
00217   }
00218   cout << endl;
00219 }
00220 }
00221 
00222 
00223 }
00224 
00225 
00226 template< typename D, typename F >
00227 void integration<D,F>::steplarge()
00228 {
00229   for (uint i=0; i<100; ++i)
00230     step();
00231 }
00232   
00233 
00234 template< typename D, typename F >
00235 void integration<D,F>::togglestartstop()
00236 {
00237   if (run)
00238     run=false;
00239   else
00240     run=true;
00241 }
00242 
00243 template< typename D, typename F >
00244 void integration<D,F>::LJcalc()
00245 {
00246   double rx;
00247   double ry;
00248 
00249   double r;
00250   double z;
00251   double f;
00252   double fx;
00253   double fy;
00254 
00255   for (uint i=0; i<numParticles; ++i)
00256     pi[i].acczero();
00257 
00258 //cout << "Before" << endl;
00259 //  for (uint i=0; i<numParticles; ++i)
00260 //    cout << SHOW(pi[i]) << endl;
00261 //cout << endl;
00262 
00263   for (uint i=0; i+1<numParticles; ++i)
00264   {
00265     for (uint k=i+1; k<numParticles; ++k)
00266     {
00267       rx = pi[i].pos[0] - pi[k].pos[0];
00268       ry = pi[i].pos[1] - pi[k].pos[1];
00269     
00270       r = sqrt(rx*rx+ry*ry);
00271 //cout << SHOW(r) << " ";
00272       z = LJsigma/r;
00273 //cout << SHOW(z) << " ";
00274       z *= z;
00275       z *= z*z;
00276 
00277       // Force divided by r.
00278       f = LJpsi*24.0/(r*r)*z*(2.0*z-1.0);
00279 
00280 if (abs(f)>10)
00281 cout << SHOW(f) << endl;
00282 
00283 //cout << SHOW(f) << " ";
00284 
00285       fx = f*rx/r;
00286       fy = f*ry/r;
00287 
00288 //cout << SHOW(fx) << " ";
00289 //cout << SHOW(fy) << " ";
00290 //cout << endl;
00291 
00292       pi[i].acc[0] -= fx;
00293       pi[i].acc[1] -= fy;
00294 
00295       pi[k].acc[0] += fx;
00296       pi[k].acc[1] += fy;
00297     }
00298 
00299   }
00300 
00301 //  for (uint i=0; i<numParticles; ++i)
00302 //    cout << SHOW(pi[i]) << endl;
00303 
00304 }
00305 
00306 template< typename D, typename F >
00307 void integration<D,F>::bruteforce
00308 (
00309   uintc * x,
00310   uintc sz
00311 )
00312 {
00313   boxcollision const & box(distribution.box);
00314 
00315 //cout << SHOW(sz) "  ";
00316 //print(p,p+sz);
00317 
00318 
00319   uint a;
00320   uint b;
00321   for (uint i=0; i+1<sz; ++i)
00322   {
00323     for (uint k=i+1; k<sz; ++k)
00324     {
00325       a = x[i];
00326       b = x[k];
00327 
00328       if (pi[a].isIntersecting(pi[b]))
00329       {
00330         pi[a].step(-h);
00331         pi[b].step(-h);
00332 
00333         pi[a].collisionDynamics(pi[b]);
00334 
00335         pi[a].step(h);
00336         pi[b].step(h);
00337 
00338         box.walls(pi[a],h);
00339         box.walls(pi[b],h);
00340       }
00341     }
00342   }
00343 }
00344 
00345 
00346 template< typename D, typename F >
00347 void integration<D,F>::bruteforce()
00348 {
00349   boxcollision const & box(distribution.box);
00350   for (uint i=0; i<numParticles; ++i)
00351   {
00352     pi[i].step(h);
00353     box.walls(pi[i],h);
00354   }
00355 
00356   if (LJ)
00357     LJcalc();
00358   
00359   for (uint i=0; i+1<numParticles; ++i)
00360   {
00361     for (uint k=i+1; k<numParticles; ++k)
00362     {
00363 
00364       if (pi[i].isIntersecting(pi[k]))
00365       {
00366         pi[i].step(-h);
00367         pi[k].step(-h);
00368 
00369         pi[i].collisionDynamics(pi[k]);
00370 
00371         pi[i].step(h);
00372         pi[k].step(h);
00373 
00374 if (!LJ)
00375 {
00376         box.walls(pi[i],h);
00377         box.walls(pi[k],h);
00378 }
00379       }
00380     }
00381   }
00382 
00383     
00384 
00385 #ifdef DEBUG_PART
00386 if (false)
00387 {
00388 cout << endl;
00389 for (uint i=0; i<numParticles; ++i)
00390   cout << pi[i] << endl;
00391 }
00392 #endif
00393 
00394 }
00395 
00396 
00397 template< typename D, typename F >
00398 void integration<D,F>::draw()
00399 {
00400   // Display the particles
00401 
00402   glPushAttrib(GL_CURRENT_BIT);
00403   glPushAttrib(GL_LIGHTING_BIT);
00404   glColor3f(1.0,0.0,0.0);
00405   pd.draw(pi[0]);
00406   glPopAttrib();
00407   glPopAttrib();
00408 
00409   for (uint i=1; i+1<numParticles; ++i)
00410   {
00411     if ((i%10)!=0)
00412       pd.draw(pi[i]);
00413     else
00414     {
00415       glPushAttrib(GL_CURRENT_BIT);
00416       glPushAttrib(GL_LIGHTING_BIT);
00417       glColor3f(0.1,0.2,1.0);
00418       pd.draw(pi[i]);
00419       glPopAttrib();
00420       glPopAttrib();
00421     }
00422   }
00423 
00424   glPushAttrib(GL_CURRENT_BIT);
00425   glPushAttrib(GL_LIGHTING_BIT);
00426   glColor3f(0.0,1.0,0.0);
00427   pd.draw(pi[numParticles-1]);
00428   glPopAttrib();
00429   glPopAttrib();
00430 
00431 
00432   // Verify energy in system.
00433   if (displayEk)
00434   {
00435 
00436     static int ekcounter;
00437     ++ekcounter;
00438     if ((ekcounter%10)==0)
00439     {
00440 
00441       double sum(0.0);
00442       double energy;
00443       for (uint i=0; i<numParticles; ++i)
00444       {
00445         particle const & p(pi[i]);
00446         energy = p.vel[0] * p.vel[0] + p.vel[1] * p.vel[1];
00447      
00448         sum += energy;
00449       }
00450       cout << "Ek*2/m=" << sum << endl;
00451     }
00452   }
00453 
00454   // Draw Box
00455   glPushAttrib(GL_CURRENT_BIT);
00456   glPushAttrib(GL_LIGHTING_BIT);
00457   glDisable(GL_LIGHTING);
00458   glColor3f(.6,.03,.03);
00459   glBegin(GL_LINES);
00460 
00461   doublec x0 = distribution.box.x0;
00462   doublec x1 = distribution.box.x1;
00463   doublec y0 = distribution.box.y0;
00464   doublec y1 = distribution.box.y1;
00465 
00466   glVertex3f(x0,y0,0.0);
00467   glVertex3f(x1,y0,0.0);
00468   glVertex3f(x1,y0,0.0);
00469   glVertex3f(x1,y1,0.0);
00470   glVertex3f(x1,y1,0.0);
00471   glVertex3f(x0,y1,0.0);
00472   glVertex3f(x0,y1,0.0);
00473   glVertex3f(x0,y0,0.0);
00474 
00475   glEnd();
00476   glPopAttrib();
00477   glPopAttrib();
00478 
00479   // Update Counters
00480 
00481   fr.display();
00482   stepcountmsg.display();
00483 }
00484 
00485 
00486 
00487 
00488 template< typename D, typename F >
00489 integration<D,F>::integration()
00490   : pd(gluNewQuadric(),10,3),
00491   distribution 
00492   (
00493     0.02,  //radius
00494     5.0,  //vmax
00495     boxcollision(0.0,10.0,0.0,10.0)  //box dimensions
00496   ),
00497   fn
00498   (
00499     distribution.box
00500   ),
00501   stepcount(0),
00502   stepcountmsg("0",0.5,-0.8,0.0,0.0,1.0),
00503   histlow(0.0),
00504   histhigh(7.0),
00505   histn(81),
00506   sampler
00507   (
00508     (uintc &)samplerstate,
00509     (uintc)20,     // Sample every x steps all particle speeds
00510     (uintc)5,      // Inner loop: sample every x2 steps
00511     histogram<double>
00512     (
00513       histlow,   // Lowest speed
00514       histhigh, // Highest speed
00515       histn      // Intervals
00516     ),
00517     string("data.txt"),
00518     (particle const * &)pi,
00519     (uintc & )numParticles
00520   )
00521 /*
00522   (
00523     samplerstate,
00524     (uintc)20,     // Sample every x steps all particle speeds
00525     (uintc)5,      // Inner loop: sample every x2 steps
00526     histogram<double>
00527     (
00528       histlow,   // Lowest speed
00529       histhigh, // Highest speed
00530       histn      // Intervals
00531     ),
00532     string("data.txt"),
00533     (particle const *)pi,
00534     numParticles
00535   )
00536 */
00537 {
00538   uniform=false;
00539   printstate=false;
00540   pi=0;
00541   numParticles=10;
00542   h=0.01;
00543   //zpr=true;
00544   run=false;
00545   displayEk=false;
00546   LJ=false;
00547   LJsigma=1.0;
00548   LJpsi=1.0;
00549 }
00550 
00551 
00552 template< typename D, typename F >
00553 void integration<D,F>::reset(int argc, char**argv)
00554 {
00555   commandline c(argc,argv);
00556   c.mapvar(numParticles,"numParticles");
00557   if(pi!=0)
00558     delete[] pi;
00559   fn.pi = pi = new particle[numParticles];
00560 
00561   C.reset(numParticles);
00562 
00563 
00564 // Test Code
00565 pu = new uint [numParticles];
00566 for (uint i=0; i<numParticles; ++i)
00567   pu[i]=i;
00568 
00569   c.mapvar(uniform,"uniform");
00570   c.mapvar(distribution.vmax,"vmax");
00571   c.mapvar(distribution.radius,"radius");
00572   c.mapvar(h,"h");
00573   c.mapvar(printstate,"printstate");
00574 
00575 // <TODO> put this option back in.
00576 //  c.mapvar(zpr,"zpr");
00577 //  if (zpr)
00578 //    zprInit();
00579 
00580   c.mapvar(distribution.box.x0,"x0");
00581   c.mapvar(distribution.box.x1,"x1");
00582   c.mapvar(distribution.box.y0,"y0");
00583   c.mapvar(distribution.box.y1,"y1");
00584 
00585   c.mapvar(displayEk,"displayEk");
00586 
00587   c.mapvar(samplerstate,"samplerstate");
00588 
00589   c.mapvar(LJpsi,"LJpsi");
00590   c.mapvar(LJsigma,"LJsigma");
00591   c.mapvar(LJ,"LJ");
00592   if (LJ)
00593     uniform=false;
00594 
00595   c.mapvar(histlow,"histlow");
00596   c.mapvar(histhigh,"histhigh");
00597   c.mapvar(histn,"histn");
00598   
00599   sampler.set(histogram<double>(histlow,histhigh,histn));
00600 
00601   c.enablehelp(cout);
00602 
00603   fn.reset();
00604 
00605   // Initial conditions: no intersecting particles.
00606   for (uint i=0; i<numParticles; ++i)
00607   {
00608     distribution.eval(pi[i]);
00609     bool flagnoint(true);
00610     for (uint k=0; (k<i)&&flagnoint; ++k)
00611     {
00612       if( pi[i].isIntersecting( pi[k] ) )
00613       {
00614         flagnoint=false;
00615         --i;
00616       }
00617     }
00618   } 
00619 
00620 }
00621 
00622 
00623 
00624 
00625 
00626 #endif
00627 
00628 

Generated on Fri Mar 4 00:49:29 2011 for Chelton Evans Source by  doxygen 1.5.8