simongraffe

Ball Throw Simulation

May 11th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. static const double PI      = 3.141592654;
  6. static const double radDeg  = 57.2957795; // 180/PI Conversion factor rad to degrees
  7.  
  8. typedef struct vectorPolar
  9. {
  10.     double r;
  11.     double theta;
  12.  
  13. } *vectorPolar;
  14.  
  15. void add(vectorPolar v1, vectorPolar v2)
  16. {
  17.     // Addition of two polar vectors r, theta.
  18.     // Result stored in first vector.
  19.     double x =  v1->r * cos(v1->theta) + v2->r * cos(v2->theta);
  20.     double y =  v1->r * sin(v1->theta) + v2->r * sin(v2->theta);
  21.     v1->r = sqrt(x * x + y * y);
  22.     v1->theta = atan2(y, x);
  23. }
  24.  
  25. void printDataPoint(int timeMin, int timeSeg, double height, vectorPolar p, vectorPolar v)
  26. {
  27.     // Copies all results of simulation to file 'coordenadas.txt'
  28.     static FILE *filePointer = NULL;
  29.     if (filePointer == NULL)
  30.     {
  31.         filePointer = fopen("coordenadas.txt", "w");
  32.         fprintf(filePointer, "T(min), T(seg) , Height, Pos(magn), Pos(ang), Veloc(magn), Veloc(ang)\n");
  33.     }
  34.     fprintf(filePointer, "%d, %d, %E, %E, %E, %E, %E\n",
  35.         timeMin, timeSeg, height, p->r, p->theta * radDeg, v->r, v->theta * radDeg);
  36. }
  37.  
  38. int main (void)
  39. {   // Simulation of a ball thrown from Earth surface
  40.     // on a point at equator with given initial speed
  41.     // and taking into account Earth's rotation
  42.     // Simón Graffe simonjgl@gmail.com
  43.     // constants:
  44.     const double G              = 6.674E-11; // m^3.kg^-1.s^-2 Gravitational constant
  45.     const double earthMass      = 5.9723E24; // kg             Earth's mass
  46.     const double earthRadius    = 6378100;   // m              Equatorial radius
  47.     const double earthRotPeriod = 0.99726968 * 3600 * 24; // d -> s  Rotation period
  48.     const double GM = G * earthMass;         // m^3.s^-2       Precalculated factor
  49.     const int    step           = 100;        // ms            Time step for simulation loop
  50.     const double deltaT = (double)step/1000.; // s             Integration step
  51.  
  52.     struct vectorPolar p, v, a;
  53.  
  54.     // Initial position (on equator):
  55.     p.r = earthRadius;
  56.     p.theta = 0;
  57.  
  58.     // Tangent velocity at that point:
  59.     struct vectorPolar vt;
  60.     vt.r = 2 * PI * earthRadius / earthRotPeriod;
  61.     vt.theta = PI / 2;
  62.  
  63.     // Throw speed (vertically)
  64.     struct vectorPolar vi;
  65.     vi.r = 38660E3/3600; // km/h -> m/s
  66.     vi.theta = 0;
  67.  
  68.     // Combine both velocities:
  69.     v.r = 0;
  70.     v.theta = 0;
  71.     add(&v, &vt); // v = v + vt
  72.     add(&v, &vi); // v = v + vi -> v = vt +vi
  73.  
  74.     // Start of simulation:
  75.     // Max time: 24 h in milli seconds
  76.     for (int t = 0; t<=24*3600*1000 ;t += step)
  77.     {
  78.         // gravitational acceleration at that point
  79.         a.r     = - GM / (p.r * p.r);
  80.         a.theta = p.theta;
  81.         struct vectorPolar deltaV;
  82.         deltaV.r     = a.r * deltaT;
  83.         deltaV.theta = a.theta;
  84.         struct vectorPolar deltaP;
  85.         deltaP.r     = v.r * deltaT;
  86.         deltaP.theta = v.theta;
  87.         // Update position and velocity at the new point
  88.         add(&p, &deltaP);
  89.         add(&v, &deltaV);
  90.  
  91.         double height; // Height above Earth surface
  92.         height = (p.r - earthRadius);
  93.         if (((t % 300000) == 0) || (height <0))
  94.             printDataPoint((t/1000)/60, (t/1000) % 60, height, &p,&v);
  95.         // If height is negative, the object crashed, we exit the simulation
  96.         if (height < 0)
  97.             break;
  98.     }
  99.     return 0;
  100. }
Add Comment
Please, Sign In to add comment