*** empty log message ***
authorEsteban Meneses <emenese2@illinois.edu>
Tue, 22 Apr 2008 16:58:09 +0000 (16:58 +0000)
committerEsteban Meneses <emenese2@illinois.edu>
Tue, 22 Apr 2008 16:58:09 +0000 (16:58 +0000)
examples/charm++/Molecular/main.C
examples/charm++/Molecular/main.h

index d45af8f275573395bd19947f02cad46bc2923e94..b2f65bed3e598bc53f661424dfe55e257e5edac7 100644 (file)
@@ -14,9 +14,9 @@
 #define A 2.0
 #define B 1.0
 
-#define DEFAULT_PARTICLES 1000
-#define DEFAULT_M 4
-#define DEFAULT_N 3
+#define DEFAULT_PARTICLES 350
+#define DEFAULT_M 5
+#define DEFAULT_N 7
 #define DEFAULT_L 10
 #define DEFAULT_RADIUS 10
 #define DEFAULT_FINALSTEPCOUNT 2
@@ -40,6 +40,12 @@ class Cell : public CBase_Cell {
     int forceCount;                                                                                                                                    // to count the returns from interactions
     int stepCount;                                                                                                                                     // to count the number of steps, and decide when to stop
                int updateCount;
+               bool updateFlag;
+               bool incomingFlag;
+
+               void updateProperties();                                                                                                        // updates properties after receiving forces from interactions
+               void checkNextStep();                                                                                                                   // checks whether to continue with next step
+               void print();                                                                                                                                                   // prints all its particles
 
   public:
     Cell();
@@ -123,7 +129,7 @@ Main::Main(CkArgMsg* msg) {
       //Processor Round Robin needed /* FIXME */
  
       #ifdef DEBUG
-        CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", x,y,x,y);
+        //CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", x,y,x,y);
         interactionCount++;
       #endif
 
@@ -133,7 +139,7 @@ Main::Main(CkArgMsg* msg) {
       // (x,y) and (x+1,y) pair
       (x == m-1) ? (i=(x+1)%m, k=x) : (i=x, k=x+1);
       #ifdef DEBUG
-        CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", i,y,k,y);
+        //CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", i,y,k,y);
         interactionCount++;
       #endif
       interactionArray( i, y, k, y ).insert( /* processor number */0 );
@@ -141,7 +147,7 @@ Main::Main(CkArgMsg* msg) {
       // (x,y) and (x,y+1) pair
       (y == n-1) ? (j=(y+1)%n, l=y) : (j=y, l=y+1);
       #ifdef DEBUG
-        CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", x,j,x,l);
+        //CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", x,j,x,l);
         interactionCount++;
       #endif
 
@@ -151,7 +157,7 @@ Main::Main(CkArgMsg* msg) {
       // (x,y) and (x+1,y+1) pair, Irrespective of y /* UNDERSTAND */
       (x == m-1) ? ( i=(x+1)%m, k=x, j=(y+1)%n, l=y ) : (i=x, k=x+1, j=y, l=(y+1)%n );
       #ifdef DEBUG
-        CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", i,j,k,l);
+        //CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", i,j,k,l);
         interactionCount++;
       #endif
       interactionArray( i, j, k, l ).insert( /* processor number */0 );
@@ -159,7 +165,7 @@ Main::Main(CkArgMsg* msg) {
       // (x,y) and (x-1,y+1) pair /* UNDERSTAND */
       (x == 0) ? ( i=x, k=(x-1+m)%m, j=y, l=(y+1)%n ) : (i=x-1, k=x, j=(y+1)%n, l=y );
       #ifdef DEBUG
-        CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", i,j,k,l);
+        //CkPrintf("INITIAL:( %d, %d) ( %d , %d )\n", i,j,k,l);
         interactionCount++;
       #endif
       interactionArray( i, j, k, l ).insert( /* processor number */0 );
@@ -204,6 +210,9 @@ Cell::Cell() {
 
   forceCount = 0;
   stepCount = 0;
+       updateFlag = false;
+       incomingFlag = false;
+
 }
 
 // Constructor needed for chare object migration (ignore for now)
@@ -212,7 +221,7 @@ Cell::~Cell() {
   /* FIXME */ // Deallocate particle lists
 }
 
-
+// Function to start interaction among particles in neighboring cells as well as its own particles
 void Cell::start() {
 
   int x = thisIndex.x;
@@ -221,7 +230,7 @@ void Cell::start() {
   int i, j, k, l;
 
   #ifdef DEBUG
-    CkPrintf("START:( %d, %d) ( %d , %d )\n", x,y,x,y);
+    //CkPrintf("START:( %d, %d) ( %d , %d )\n", x,y,x,y);
   #endif
   
   // self interaction
@@ -263,56 +272,226 @@ void Cell::start() {
 
 }
 
-void Cell::updateForces(CkVec<Particle> &particles) {
+// Function to update forces coming from a neighbor interaction chare
+void Cell::updateForces(CkVec<Particle> &updates) {
+       int i, x ,y;
+       CkVec<Particle> outgoing;
+
+       // incrementing the counter for receiving updates
   forceCount++;
+
+       // updating force information
+       for(i = 0; i < updates.length(); i++){
+               particles[i].fx += updates[i].fx;
+               particles[i].fy += updates[i].fy;
+       }
+
+       // if all forces are received, then it must recompute particles location
   if( forceCount >= 9) {
     
+               CkPrintf("Forces done!\n");
+
     // Received all it's forces from the interactions.
     stepCount++;
     forceCount = 0;
     
-    /* FIX ME*/ // Update forces on own particles and determine which of them are being displaced to neighbors
-    /* FIX ME*/ // Calls to updateParticles on neighbours.
-      
+    // Update properties on own particles
+               updateProperties();
+               updateFlag = true;
+
+               // Sending particles to neighboring cells
+               x = thisIndex.x;
+               y = thisIndex.y;
+               // particles sent to (x-1,y-1)          
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].x < x*L && particles[i].y < y*L){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray((x-1+m)%m,(y-1+n)%n).updateParticles(outgoing);
+                     
+               // particles sent to (x-1,y)            
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].x < x*L && particles[i].y <= y*(L+1)){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray((x-1+m)%m,y).updateParticles(outgoing);
+
+               // particles sent to (x-1,y+1)
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].x < x*L && particles[i].y > y*(L+1)){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray((x-1+m)%m,(y+1)%n).updateParticles(outgoing);
+
+               // particles sent to (x+1,y-1)
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].x > x*(L+1) && particles[i].y < y*L){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray((x+1)%m,(y-1+n)%n).updateParticles(outgoing);
+
+               // particles sent to (x+1,y)
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].x > x*(L+1) && particles[i].y <= y*(L+1)){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray((x+1)%m,y).updateParticles(outgoing);
+
+               // particles sent to (x+1,y+1)
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].x > x*(L+1) && particles[i].y > y*(L+1)){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray((x+1)%m,(y+1)%n).updateParticles(outgoing);
+
+               // particles sent to (x,y-1)
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].y < y*L){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray(x,(y-1+n)%n).updateParticles(outgoing);
+
+               // particles sent to (x,y+1)
+               outgoing.removeAll();
+               i = 0;
+               while(i < particles.length()){
+                       if(particles[i].y > (y+1)*L){
+                               outgoing.push_back(particles[i]);
+                               particles.remove(i);
+                       }else
+                               i++;
+               }
+               cellArray(x,(y+1)%n).updateParticles(outgoing);
+
     #ifdef DEBUG
       CkPrintf("STEP: %d DONE:( %d , %d )\n", stepCount, thisIndex.x, thisIndex.y);
     #endif
 
-    if(stepCount >= finalStepCount) {
-      mainProxy.checkIn();
-    } else {
-      thisProxy( thisIndex.x, thisIndex.y ).start();
-    }
+               // checking whether to proceed with next step
+               checkNextStep();
+
   }
     
 }
 
+// Prints all particle set
+void Cell::print(){
+       int i;
+       CkPrintf("*******************************************************************\n");
+       CkPrintf("Cell (%d,%d)\n",thisIndex.x,thisIndex.y);
+       CkPrintf("Part     x     y\n");
+       for(i = 0; i < particles.length(); i++){
+               CkPrintf("%-5d %7.4f %7.4f \n",i,particles[i].x,particles[i].y);
+       }
+       CkPrintf("*******************************************************************\n");
+}
+
+// Function that checks whether it must start the following step or wait until other messages are received
+void Cell::checkNextStep(){
+       int i;
+
+       if(updateFlag && incomingFlag){
+
+               // resetting flags
+               updateFlag = false;
+               incomingFlag = false;
+
+               // adding new elements
+               for(i = 0; i < incomingParticles.length(); i++){
+                       particles.push_back(incomingParticles[i]);
+               }
+               incomingParticles.removeAll();
+
+               // checking for next step
+               if(stepCount >= finalStepCount) {
+                       print();
+                       mainProxy.checkIn();
+         } else {
+           thisProxy( thisIndex.x, thisIndex.y ).start();
+         }
+       }
+
+}
+
 // Function that receives a set of particles and updates the forces of them into the local set
 void Cell::updateParticles(CkVec<Particle> &updates) {
-
   updateCount++;
 
   for( int i=0; i < updates.length(); i++) {
     incomingParticles.push_back(updates[i]);
   }
 
+       // if all the incoming particle updates have been received, we must check whether to proceed with next step
+       if(updateCount >= 8 ) {
+               updateCount = 0;
+               incomingFlag = true;
+               checkNextStep();
+       }
 
-  
-       //CHECKincomingParticles.append(updates);
-
-  
+}
 
-       if(updateCount >= 8 ) {
-               
-               /* FIXME Synchronisation?? */
+// Function to update properties (i.e. acceleration, velocity and position) in particles
+void Cell::updateProperties(){
+       int i;
        
-  
-    updateCount = 0;
-    /* FIXME Empty incomingParticles vector after appending it with particles */
+       for(i = 0; i < particles.length(); i++){
+
+               // applying kinetic equations
+               particles[i].ax = particles[i].fx / DEFAULT_MASS;
+               particles[i].ay = particles[i].fy / DEFAULT_MASS;
+               particles[i].vx = particles[i].vx + particles[i].ax * DEFAULT_DELTA;
+               particles[i].vy = particles[i].vy + particles[i].ay * DEFAULT_DELTA;
+               particles[i].x = particles[i].x + particles[i].vx * DEFAULT_DELTA;
+               particles[i].y = particles[i].y + particles[i].vy * DEFAULT_DELTA;
+               particles[i].fx = 0.0;
+               particles[i].fy = 0.0;
+
+               // checking boundary conditions
+               if(particles[i].x < 0.0) particles[i].x = 0.0;
+               if(particles[i].y < 0.0) particles[i].y = 0.0;
+               if(particles[i].x > L*m) particles[i].x = L*m;
+               if(particles[i].y > L*n) particles[i].y = L*n;
+
        }
 
 }
 
+
 // Default constructor
 Interaction::Interaction() {
   cellCount = 0;
@@ -332,7 +511,7 @@ void Interaction::interact(CkVec<Particle> particles, int x, int y ) {
 
     // self interaction check
     if( thisIndex.x == thisIndex.z && thisIndex.y == thisIndex.w ) {
-      CkPrintf("SELF: ( %d , %d )\n", thisIndex.x, thisIndex.y );
+      //CkPrintf("SELF: ( %d , %d )\n", thisIndex.x, thisIndex.y );
       cellCount = 0;
                        interact(particles,particles);
       cellArray( x, y).updateForces(particles);
@@ -349,7 +528,7 @@ void Interaction::interact(CkVec<Particle> particles, int x, int y ) {
        // if both particle sets are received, compute interaction
   if(cellCount >= 2) {
 
-    CkPrintf("PAIR:( %d , %d )  ( %d , %d ) \n", bufferedX, bufferedY, x, y );
+    //CkPrintf("PAIR:( %d , %d )  ( %d , %d ) \n", bufferedX, bufferedY, x, y );
     cellCount = 0;
 
                interact(bufferedParticles,particles);
index 395c8ad412271c6ffbe03a20371797d8a9c18241..3487e421e78681bb5885ed69e8c8f62d2d86dc76 100644 (file)
@@ -10,6 +10,7 @@
 #include "time.h"
 
 #define DEFAULT_MASS 1
+#define DEFAULT_DELTA 0.1
 
 // Class for keeping track of the properties for a particle
 class Particle{