Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. // Collision.cc
  2. // This program simulates the interaction of two
  3. // particles colliding.
  4. // Usage:
  5. //  mkdir output
  6. //  ./Collision > output/data1
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. using namespace std;
  11.  
  12. struct particle {
  13.     double x    ; // x coordinate
  14.     double im   ; // inverse mass
  15.     double v    ; // velocity
  16. } ;
  17.  
  18. void collide (particle p1, particle p2)
  19. // two particles collide elastically
  20. {
  21.   // find the relative velocity
  22.   double velocity_along_line =  p1.v - p2.v;
  23.   // find mass fraction for p1
  24.   double p1f = p1.im / ( p1.im + p2.im ) ;
  25.   double p2f = p2.im / ( p1.im + p2.im ) ;
  26.   // reverse the c.o.m. velocity of each along the line of collision
  27.   p1.v -= 2.0 * p1f * velocity_along_line ;
  28.   p2.v += 2.0 * p2f * velocity_along_line ;
  29. }
  30.  
  31. void ShowState(double t, particle &a, particle &b, particle &w1, particle &w2) {
  32.   // Displays data in the order time, Wall 1, Wall 2, Position A, Position B
  33.   cout << t << "\t" << w1.x << "\t" << w2.x << "\t" << a.x << "\t" << b.x << endl;
  34. }
  35.  
  36. void LeapForward(particle &a, particle &b, particle &w1, particle &w2, double &t, double dt){
  37.  
  38.   double X = t/dt; // Number of iterations
  39.  
  40.   for(int i=0; i<X; i++){
  41.  
  42.     a.x += dt * a.v ;
  43.     b.x += dt * b.v ;
  44.  
  45.   }
  46.  
  47.   ShowState(t, a, b, w1, w2) ;
  48. }
  49.  
  50. // Find next collision time
  51. void nct(particle &a, particle &b, particle &w1, particle &w2, double &t, double &Y, double &tab, double &taw, double &tbw, int &collisiontype) {
  52.  
  53.  
  54.  
  55.  
  56.  
  57.   if(a.v > b.v){
  58.     tab = (a.x - b.x)/(b.v - a.v) ; // A to B collision
  59.   }
  60.   if(a.v < 0){
  61.     taw = - a.x/a.v ; // A to Wall 1 collision
  62.   }
  63.   if(b.v > 0){
  64.     tbw = (w2.x - b.x)/b.v ; // B to Wall 2 collision
  65.   }
  66.   if(tab < Y){
  67.     Y = tab;
  68.     collisiontype = 1;
  69.   }
  70.   if(taw < Y){
  71.     Y = taw;
  72.     collisiontype = 2;
  73.   }
  74.   if(tbw < Y){
  75.     Y = tbw;
  76.     collisiontype = 3;
  77.   }
  78.   tab = taw = tbw = 1e100;
  79.  
  80.   t = t + Y ;
  81. }
  82.  
  83. int main(){
  84.  
  85.   double tab = 1e100;
  86.   double taw = 1e100;
  87.   double tbw = 1e100;
  88.  
  89.   particle a;
  90.   particle b;
  91.   particle w1;
  92.   particle w2;
  93.   int L = 10 ; // Length of Box
  94.   double dt = 0.1;
  95.   double t = 0.0;
  96.   double Y = 1000000000000;
  97.   int collisiontype = 0;
  98.  
  99.   // Set wall conditions
  100.   w1.x = 0;
  101.   w2.x = L;
  102.   w1.v = 0;
  103.   w2.v = 0;
  104.   w1.im = 0;
  105.   w2.im = 0;
  106.  
  107.   // Set default conditions
  108.   a.x = 3.0;
  109.   a.v = 0.0;
  110.   b.x = 7.0;
  111.   b.v = 2.0;
  112.   a.im = 1.0;
  113.   b.im = 1.0;
  114.  
  115.   for(int i=0; i<4; i++){
  116.     nct(a, b, w1, w2, t, Y, tab, taw, tbw, collisiontype);
  117.     LeapForward(a, b, w1, w2, t, dt);
  118.  
  119.     if(collisiontype==1){
  120.       collide(a,b);
  121.     }
  122.  
  123.     if(collisiontype==2){
  124.       collide(w1, a);
  125.     }
  126.  
  127.     if(collisiontype==3){
  128.       collide(b, w2);
  129.     }
  130.   }
  131.  
  132.   return 0 ;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement