Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/lamont/test/boids/benchmark/boids.diderot
ViewVC logotype

View of /branches/lamont/test/boids/benchmark/boids.diderot

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3282 - (download) (annotate)
Tue Oct 13 19:46:34 2015 UTC (4 years, 3 months ago) by lamonts
File size: 6014 byte(s)
Fixed bug with dead strands appearing in query lists
real{} initVelocities = load("velocities.nrrd");
real{} initPosns = load("posns.nrrd"); 
int numBoids = length(initPosns)/2; 
input real boidRadius = 5; 
input real queryRadius = 30;
input real DESIRED_SEPARTION = 15;
input real NEIGHBOR_SEPARTION = 15;

int MAX_SPEED =1;
int RED = 0; 
int BLUE = 1; 
real MAX_FORCE = 0.05; 
input int width = 800; 
input int height = 600;  
int steps = 0; 
int iterMax = 1000; 
real threshold = 0.30; 
int iterMax = 400;  

function vec2 wrapAround (vec2 pos)
{
    vec2 newPos = [pos[0],pos[1]]; 
    real x = pos[0]; 
    real y = pos[1]; 

    if (newPos[0] < -boidRadius){ 
      x = width + boidRadius;
    }

    if (newPos[1] < -boidRadius) {
      y = height+boidRadius;
    }

    if (newPos[0] > width + boidRadius){
      x = -boidRadius;
    } 

    if (newPos[1] > height + boidRadius){
      y = -boidRadius;
    } 

    return [x,y]; 
}

function vec2 limit (vec2 v, real maxForce)
{
  if (|v| > maxForce) {
      vec2 newV = normalize(v); 
      newV *= maxForce; 
      return newV; 
  }  
  return v; 
}
function vec2 seek (vec2 v, vec2 pos, vec2 vel)
{
    vec2 desired = v - pos; 

    desired = normalize(desired); 
    desired *= MAX_SPEED; 

    vec2 steer = desired - vel; 
    steer = limit(steer,MAX_FORCE); 

    return steer;
}
function vec3 separate (vec3 steer, vec2 neighbor, vec2 pos, vec2 vel, bool shouldAvg) 
{
  if(!shouldAvg){
    real d  =  |pos - neighbor|; 
    vec2 steerPos = [steer[0],steer[1]]; 
    if(d > 0 && d < DESIRED_SEPARTION) { 
      vec2 newSteer = steerPos + (normalize(pos-neighbor)/d); 
      return [newSteer[0],newSteer[1],steer[2] + 1];    
    }
    return steer; 
  }else {
      vec2 avgSteer = [steer[0],steer[1]]; 
      if(steer[2] > 0) 
      {
        avgSteer /= steer[2]; 
      } 
      if(|avgSteer| > 0){
        avgSteer = normalize(avgSteer); 
        avgSteer *= MAX_SPEED; 
        avgSteer -= vel; 
        avgSteer = limit(avgSteer, MAX_FORCE); 
      }
      return [avgSteer[0],avgSteer[1],0]; 
  }
}
function vec3 align(vec3 sum, vec2 neighborPos, vec2 neighborVel, vec2 pos, vec2 vel, bool shouldAvg)
{
  if(!shouldAvg){
    real d  =  |pos - neighborPos|; 
    vec2 sumVal = [sum[0],sum[1]]; 
    if(d > 0 && d < NEIGHBOR_SEPARTION) { 
        vec2 newSum = sumVal + neighborVel;  
        return [newSum[0],newSum[1],sum[2] + 1];    
    }
    return sum;  
  }else {
      vec2 avgSum = [sum[0],sum[1]]; 
      if(sum[2] > 0) 
      {
        avgSum /= sum[2]; 
        avgSum = normalize(avgSum); 
        avgSum *= MAX_SPEED; 
        avgSum = avgSum - vel; 
        avgSum = limit(avgSum, MAX_FORCE); 
        return [avgSum[0],avgSum[1],0]; 
      }else {
        return [0.0,0.0,0.0]; 
      }
  }
}
function vec3 cohension (vec3 sum, vec2 neighbor, vec2 pos, vec2 vel, bool shouldAvg) 
{
  if(!shouldAvg){
    real d  =  |pos - neighbor|; 
    vec2 sumVal = [sum[0],sum[1]]; 
    if(d > 0 && d < NEIGHBOR_SEPARTION) { 
        vec2 newSum = sumVal + neighbor;  
        return [newSum[0],newSum[1],sum[2] + 1];    
    }
    return sum; 
  }else {
      vec2 avgSum = [sum[0],sum[1]]; 
      if(sum[2] > 0) 
      {
        avgSum /= sum[2]; 
        avgSum = normalize(avgSum); 
        avgSum *= MAX_SPEED; 
        avgSum = avgSum - vel; 
        avgSum = seek(avgSum,pos,vel); 
        return [avgSum[0],avgSum[0],0.0]; 
      }else {
        return [0.0,0.0,0.0]; 
      }
  }
} 

strand Boid(int i,real vx, real vy, real px, real py, int c) {
    vec2 pos = [px,py]; 
    vec2 vel = [vx,vy]; 
    int color = c; 
    int id = i; 
    bool isSatisfied = false; 
    output tensor[6] boidInfo = [pos[0],pos[1],vel[0],vel[1], real(color), 0.0]; 

    update {

        vec3 same_aVec = [0.0,0.0,0.0];  
        vec3 same_cVec = [0.0,0.0,0.0];  

        vec3 other_cVec = [0.0,0.0,0.0];
        vec3 other_aVec = [0.0,0.0,0.0];  

        vec3 alignVec = [0.0,0.0,0.0]; 
        vec3 cohereVec = [0.0,0.0,0.0]; 
        vec3 seperateVec = [0.0,0.0,0.0];

        int otherCount = 0; 
        int sameCount = 0; 
        int count = 0; 



        if(steps > iterMax){
             stabilize;
        }else { 
          //Calculate the individual rule vectors 
          foreach(Boid neighbor in sphere(queryRadius)){

              seperateVec = separate(seperateVec,neighbor.pos,pos,vel,false); 

              if(neighbor.color == color){
               same_aVec = align(same_aVec,neighbor.pos,neighbor.vel,pos,vel,false);
               same_cVec = cohension(same_cVec,neighbor.pos,pos,vel,false); 
               sameCount+=1; 
              }else { 
                other_aVec = align(other_aVec,neighbor.pos,neighbor.vel,pos,vel,false);
                other_cVec = cohension(other_cVec,neighbor.pos,pos,vel,false); 
               otherCount +=1;
              }
              count+=1; 
          }

          if(sameCount > 0){
              //Calculate the averages for the vectors 
              seperateVec = separate(seperateVec,pos,pos,vel,true); 
              alignVec = align(same_aVec,vel,vel,pos,vel,true);
              cohereVec = cohension(same_cVec,pos,pos,vel,true);

             isSatisfied = true if steps > iterMax else false; 

          }

          // Arbitrarily weight these forces
          //seperateVec *= 1.5;

          // Add the force vectors to acceleration
          vec3 temp = seperateVec + alignVec + cohereVec; 
          vec2 acceleration = [temp[0],temp[1]]; 

          // update velocity 
          vel += acceleration; 
          vel = limit(vel,MAX_SPEED); 
          pos += vel; 

          //Wrap if needed 
          pos = wrapAround(pos);
        }

        boidInfo = [pos[0],pos[1],vel[0],vel[1], real(color), 0.0];
   }
}
global {
  steps += 1; 
}

initially {Boid(i,initVelocities{i*2},initVelocities{i*2+1},initPosns{i*2}, 
                                                            initPosns{i*2+1},
                                                            RED if numBoids/2 < i else BLUE) | i in 0 .. numBoids-1 };

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0