Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.37 KB | None | 0 0
  1. import math
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. #initial condition
  6. mass = 5.125 #Oz
  7. circumference = 9.125 #inches
  8. x0 = 0 #ft
  9. y0 = 2.000 #ft
  10. z0 = 3 #ft
  11. exit_speed = 100 #mph
  12. launch_angle = 30 #degree
  13. direction = 0 #degree
  14. sign = 1
  15. backspin= -763+120*launch_angle+21*direction*sign#rpm
  16. sidespin = -sign*849-94*direction #rpm
  17. wg = 0 #rpm
  18. tau = 25 #sec
  19. dt = 0.01
  20. pi = 3.1415
  21.  
  22.  
  23. Temp_F = 78 #F
  24. elev_ft = 0 #ft
  25. vwind = 0 #mph
  26. phiwind = 0 #degree
  27. hwind = 0 #mph
  28. relative_humidity = 50 #%
  29. barometric_pressure = 29.92 #Hg
  30.  
  31. Temp_C = (5/9)*(Temp_F-32)
  32. elev_m = elev_ft / 3.2808
  33.  
  34. vxw = vwind*1.467*math.sin(phiwind*math.pi/180) #ft/s
  35. vyw = vwind*1.467*math.cos(phiwind*math.pi/180) #ft/s
  36.  
  37. beta = 0.0001217 #1/m
  38. SVP = 4.5841*math.exp((18.687-Temp_C /234.5)*Temp_C/(257.14+Temp_C ))
  39. barometric_pressure_mmHg = barometric_pressure*1000/39.37
  40. rho_kg_m3 = 1.2929*(273/(Temp_C+273)*(barometric_pressure_mmHg*math.exp(-beta*elev_ft)-0.3783*relative_humidity*SVP/100)/760) #kg/m^3
  41. rho_lb_ft3 = rho_kg_m3*0.06261 #lb/ft^3
  42. # const
  43. c0 = 0.07182*rho_lb_ft3*(5.125/mass)*(circumference/9.125)**2
  44.  
  45. v0 = 1.467*exit_speed
  46. v0x = v0*math.cos(launch_angle*math.pi/180)*math.sin(direction*math.pi/180)
  47. v0y = v0*math.cos(launch_angle*math.pi/180)*math.cos(direction*math.pi/180)
  48. v0z = v0*math.sin(launch_angle*math.pi/180)
  49. wx = (backspin*math.cos(direction*math.pi/180)-
  50. sidespin*math.sin(launch_angle*math.pi/180)*math.sin(direction*math.pi/180)+wg*v0x/v0)*math.pi/30
  51. wy = (-backspin*math.sin(direction*math.pi/180)-
  52. sidespin*math.sin(launch_angle*math.pi/180)*math.cos(direction*math.pi/180)+wg*v0y/v0)*math.pi/30
  53. wz = (sidespin*math.cos(launch_angle*math.pi/180)+wg*v0z/v0)*math.pi/30
  54. omega = math.sqrt(wx*wx+wy*wy+wz*wz)
  55. romega = (circumference/2/math.pi)*omega/12
  56.  
  57.  
  58.  
  59.  
  60.  
  61. def accelaration(t,vx,vy,vz,z):
  62.  
  63. v = math.sqrt(vx*vx+vy*vy+vz*vz)
  64. if(z > hwind):
  65. vw = math.sqrt((vx-vxw)**2 + (vy-vyw)**2+vz**2)
  66. vyw_sim = vyw
  67. else:
  68. vw = v
  69. vyw_sim = 0
  70. S = (romega/vw)*math.exp(-t/(tau*146.7/v))
  71. Cd = 0.4105*(1+0.2017*S*S)
  72. Cl = 1/(2.32+0.4/S)
  73. vxw_sim = 0
  74. adragx = -c0*Cd*vw*(vx-vxw)
  75. adragy = -c0*Cd*vw*(vy-vyw_sim)
  76. adragz = -c0*Cd*vw*vz
  77. w = omega*math.exp(-t/tau)*30/math.pi
  78. aMagx = c0*(Cl/omega)*vw*(wy*vz-wz*(vy-vyw))
  79. aMagy = c0*(Cl/omega)*vw*(wz*(vx-vw)-wx*vz)
  80. aMagz = c0*(Cl/omega)*vw*(wx*(vy-vyw)-wy*(vx-vxw))
  81. ax = adragx+aMagx
  82. ay = adragy+aMagy
  83. az = adragz+aMagz-32.174
  84.  
  85.  
  86. return ax,ay,az
  87.  
  88. def RK4(x,y,z,vx,vy,vz,t):
  89. k1x,k1y,k1z = tuple([dt*i for i in accelaration(t,vx,vy,vz,z)]) #dt * f(x,t)
  90. k2x,k2y,k2z = tuple([dt*i for i in accelaration(t+dt/2,vx+k1x/2,vy+k1y/2,vz+k1z/2,z)])
  91. k3x,k3y,k3z = tuple([dt*i for i in accelaration(t+dt/2,vx+k2x/2,vy+k2y/2,vz+k2z/2,z)])
  92. k4x,k4y,k4z = tuple([dt*i for i in accelaration(t+dt,vx+k3x,vy+k3y,vz+k3z,z)])
  93. print(' ')
  94. print(k1x)
  95. print(k2x)
  96.  
  97. print(' ')
  98.  
  99. vx += (1/6)*(k1x+2*k2x+2*k3x+k4x)
  100. vy += (1/6)*(k1y+2*k2y+2*k3y+k4y)
  101. vz += (1/6)*(k1z+2*k2z+2*k3z+k4z)
  102.  
  103. x += vx * dt
  104. y += vy * dt
  105. z += vz * dt
  106.  
  107. return x,y,z,vx,vy,vz
  108.  
  109.  
  110. print(v0x)
  111. print(v0y)
  112. print(v0z)
  113. print('bruh')
  114. x_path_RK4 = []
  115. y_path_RK4 = []
  116. z_path_RK4 = []
  117.  
  118. x = 0
  119. y = y0
  120. z = z0
  121.  
  122. vx = v0x
  123. vy = v0y
  124. vz = v0z
  125. for t in range(1000):
  126. x_path_RK4.append(x)
  127. y_path_RK4.append(y)
  128. z_path_RK4.append(z)
  129.  
  130. t = dt*t
  131.  
  132. x,y,z,vx,vy,vz = RK4(x,y,z,vx,vy,vz,t)
  133.  
  134. if(z <0):
  135.  
  136. print('Hang time = %1.2f s Distance = %1.1f feet' % (t, y))
  137.  
  138.  
  139. break
  140.  
  141.  
  142. My Translation Effort Below:
  143.  
  144.  
  145. #include <iostream>
  146. #include <cmath>
  147. #include <iomanip>
  148. #include <vector>
  149. #include <math.h>
  150.  
  151. using namespace std;
  152.  
  153. ///Use structs possibly to not have to declare all these variables globally
  154.  
  155. vector<double> acceleration(double, double, double, double, double);
  156.  
  157. double distance_final(double, double, double, double, double, double, double);
  158.  
  159. ///These are variables that change with the user input. User will be able to change exit speed, launch angle, etc.
  160. ///Because it would take so long to go through every single input from the user, some were put in the "advanced section", that the user can elect to edit or skip over, leaving the results blank.
  161.  
  162.  
  163. //initial condition
  164. double mass = 5.125; //Oz
  165. double circumference = 9.125; //inches
  166. double x0 = 0; //ft
  167. double y0 = 2.000; //ft
  168. double z0 = 3; //ft
  169. double exit_speed = 100; //mph
  170. double launch_angle = 30; //degree
  171. double direction = 0; //degree
  172. double sign = 1;
  173. double backspin = -763 + 120*launch_angle + 21*direction * sign;//rpm
  174. double sidespin = (-1*sign)*849-94*direction; //rpm
  175. double wg = 0;//rpm
  176. double tau = 25; //sec
  177. double dt = 0.01;
  178. double pi = 3.1415;
  179. double e = 2.7182818;
  180.  
  181.  
  182. double Temp_F = 78; //F
  183. double elev_ft = 0; //ft
  184. double vwind = 0; //mph
  185. double phiwind = 0; //degree
  186. double hwind = 0; //mph
  187. double relative_humidity = 50; //%
  188. double barometric_pressure = 29.92; //Hg
  189.  
  190. double Temp_C = (5/9)*(Temp_F-32);
  191. double elev_m = elev_ft / 3.2808;
  192.  
  193. double vxw = vwind*1.467*sin(phiwind*pi/180); //ft/s wind speed in x direction
  194. double vyw = vwind*1.467*cos(phiwind*pi/180); //ft/s wind speed in y direction
  195.  
  196. double beta = 0.0001217; //1/m
  197. double SVP = 4.5841*(pow(e,((18.687-Temp_C /234.5)*Temp_C/(257.14+Temp_C )))); //barometric calculations incorporated into distance and involving temperature
  198. double barometric_pressure_mmHg = barometric_pressure*1000/39.37;
  199. double rho_kg_m3 = 1.2929*(273/(Temp_C+273)*(barometric_pressure_mmHg*(pow(e,(-beta*elev_ft)))-0.3783*relative_humidity*SVP/100)/760); //kg/m^3
  200. double rho_lb_ft3 = rho_kg_m3*0.06261; //lb/ft^3
  201. // constant value, non editable variables
  202. double c0 = 0.07182*rho_lb_ft3*(5.125/mass)*(pow(2, (circumference/9.125)));
  203.  
  204. double v0 = 1.467*exit_speed;
  205. double v0x = v0*cos(launch_angle*pi/180)*sin(direction*pi/180);
  206. double v0y = v0*cos(launch_angle*pi/180)*cos(direction*pi/180);
  207. double v0z = v0*sin(launch_angle*pi/180);
  208. double wx = (backspin*cos(direction*pi/180)-
  209. sidespin*sin(launch_angle*pi/180)*sin(direction*pi/180)+wg*v0x/v0)*pi/30;
  210. double wy = (-backspin*sin(direction*pi/180)-
  211. sidespin*sin(launch_angle*pi/180)*cos(direction*pi/180)+wg*v0y/v0)*pi/30;
  212. double wz = (sidespin*cos(launch_angle*pi/180)+wg*v0z/v0)*pi/30;
  213. double omega = sqrt(wx*wx+wy*wy+wz*wz);
  214. double romega = (circumference/2/pi)*omega/12;
  215.  
  216.  
  217.  
  218. ///All the constants/variables declared above...
  219.  
  220. int main()
  221. {
  222. int userNum, input;
  223. cout << "Welcome to my distance calculator! Would you like to go through all advanced settings(1) or the simplified settings(2)?" << endl;
  224. cin >> userNum;
  225. if (userNum == 1) ///Advanced settings
  226. {
  227. cout << "Enter exit speed in mph: ";
  228. cin >> input;
  229. exit_speed = input;
  230. cout << "Enter launch angle in degrees: ";
  231. cin >> input;
  232. launch_angle = input;
  233. cout << "Enter horizontal degrees, where zero degrees is the first base line and 90 is the third base line: ";
  234. cin >> input;
  235. direction = input;
  236. cout << "Enter temperature in Fahrenheit: ";
  237. cin >> input;
  238. Temp_F = input;
  239. cout << "Enter elevation above sea level: ";
  240. cin >> input;
  241. elev_ft = input;
  242. cout << "Enter wind speed: ";
  243. cin >> input;
  244. vwind = input;
  245. cout << "Enter wind direction in degrees: ";
  246. cin >> input;
  247. phiwind = input;
  248. cout << "Enter humidity: ";
  249. cin >> input;
  250. relative_humidity = input;
  251. cout << "Enter wind velocity coming in: ";
  252. cin >> input;
  253. hwind = input;
  254.  
  255. }
  256. else ///Simplified settings
  257. {
  258. cout << "Enter exit speed in mph: ";
  259. cin >> input;
  260. exit_speed = input;
  261. cout << "Enter launch angle in degrees: ";
  262. cin >> input;
  263. launch_angle = input;
  264. cout << "Enter horizontal degrees, where zero degrees is the first base line and 90 is the third base line: ";
  265. cin >> input;
  266. direction = input;
  267. }
  268.  
  269. exit_speed = 100; //mph
  270. launch_angle = 30; //degree
  271. direction = 0; //degree
  272. sign = 1;
  273. Temp_F = 78; //F
  274. elev_ft = 0; //ft
  275. vwind = 0; //mph
  276. phiwind = 0; //degree
  277. hwind = 0; //mph
  278. relative_humidity = 50; //%
  279.  
  280.  
  281. vector<int> x_path_RK4; ///Making the path in the x, y, and z coordinates.
  282. vector<int> y_path_RK4;
  283. vector<int> z_path_RK4;
  284.  
  285. double x = 0;
  286. double y = y0;
  287. double z = z0;
  288.  
  289. double vx = v0x;
  290. double vy = v0y;
  291. double vz = v0z;
  292.  
  293.  
  294. for (int t = 1; t < 1000; t++)
  295. {
  296.  
  297. x_path_RK4.push_back(x);
  298. y_path_RK4.push_back(y);
  299. z_path_RK4.push_back(z);
  300.  
  301.  
  302.  
  303. distance_final(x,y,z,vx,vy,vz,t);
  304. cout << " & " << z << " & " << endl;
  305. if(z <= 0)
  306. {
  307. t = dt * t;
  308. cout << "Hang time: " << t << endl;
  309. cout << "Distance Traveled: " << y << endl;
  310. break;
  311. }
  312. }
  313.  
  314. ///Sfml code down here...
  315. ///Use vector math using horizontal angle and magnitude(distance) of ball vector find the coordinate points in the x and y direction
  316.  
  317.  
  318. }
  319.  
  320. vector<double> acceleration(double t, double vx, double vy, double vz, double z)
  321. {
  322. double v = sqrt(vx*vx+vy*vy+vz*vz);
  323. double vw, vyw_sim;
  324. if(z > hwind)
  325. {
  326. vw = sqrt((pow(2, (vx-vxw))) + (pow(2, (vy-vyw))) + (pow(2, vz)));
  327. vyw_sim = vyw;
  328. }
  329. else
  330. {
  331. vw = v;
  332. vyw_sim = 0;
  333. }
  334.  
  335. double S = (romega/vw)*(pow(2, (-t/(tau*146.7/v))));
  336. double Cd = 0.4105*(1+0.2017*S*S);
  337. double Cl = 1/(2.32+0.4/S);
  338. double vxw_sim = 0;
  339. double adragx = -c0*Cd*vw*(vx-vxw);
  340. double adragy = -c0*Cd*vw*(vy-vyw_sim);
  341. double adragz = -c0*Cd*vw*vz;
  342. double w = omega*(pow(e, (-t/tau)))*30/pi;
  343. double aMagx = c0*(Cl/omega)*vw*(wy*vz-wz*(vy-vyw));
  344. double aMagy = c0*(Cl/omega)*vw*(wz*(vx-vw)-wx*vz);
  345. double aMagz = c0*(Cl/omega)*vw*(wx*(vy-vyw)-wy*(vx-vxw));
  346. double ax = adragx+aMagx;
  347. double ay = adragy+aMagy;
  348. double az = adragz+aMagz-32.174;
  349.  
  350.  
  351. vector<double> myArray;
  352. myArray.push_back(ax);
  353. myArray.push_back(ay);
  354. myArray.push_back(az);
  355.  
  356. return myArray;
  357.  
  358. }
  359.  
  360.  
  361. double distance_final(double x, double y, double z, double vx, double vy, double vz, double t)
  362. {
  363. ///Final distances are calculated here in this function using all previous variable
  364. vector<double> result = acceleration(t,vx,vy,vz,z); ///Result of the acceleration function, gets three values from the acceleration function and puts them into this vector
  365. double a1x = result[0];
  366. double a1y = result[1];
  367. double a1z = result[2];
  368. double k1x = dt*a1x;
  369. double k1y = dt*a1y;
  370. double k1z = dt*a1z;
  371.  
  372.  
  373. vector<double> result2 = acceleration(t+dt/2,vx+k1x/2,vy+k1y/2,vz+k1z/2,z);
  374. double a2x = result2[0];
  375. double a2y = result2[1];
  376. double a2z = result2[2];
  377. double k2x = dt*a2x;
  378. double k2y = dt*a2y;
  379. double k2z = dt*a2z;
  380.  
  381. vector<double> result3 = acceleration(t+dt/2,vx+k2x/2,vy+k2y/2,vz+k2z/2,z);
  382. double a3x = result3[0];
  383. double a3y = result3[1];
  384. double a3z = result3[2];
  385. double k3x = dt*a3x;
  386. double k3y = dt*a3y;
  387. double k3z = dt*a3z;
  388.  
  389. vector<double> result4 = acceleration(t+dt,vx+k3x,vy+k3y,vz+k3z,z);
  390. double a4x = result4[0];
  391. double a4y = result4[1];
  392. double a4z = result4[2];
  393. double k4x = dt*a4x;
  394. double k4y = dt*a4y;
  395. double k4z = dt*a4z;
  396.  
  397.  
  398.  
  399.  
  400. vx += (1/6)*(k1x+2*k2x+2*k3x+k4x);
  401. vy += (1/6)*(k1y+2*k2y+2*k3y+k4y);
  402. vz += (1/6)*(k1z+2*k2z+2*k3z+k4z);
  403.  
  404. x += vx * dt;
  405. y += vy * dt;///total distance calculated
  406. z += vz * dt;
  407.  
  408. return x,y,z,vx,vy,vz;
  409.  
  410. }
  411. ````
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement