Based on the original Rocket Workbench on SourceForge in CVS at: https://sourceforge.net/projects/rocketworkbench
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
6.6 KiB

  1. /* cpropep.c - Calculation of Complex Chemical Equilibrium */
  2. /* Copyright (C) 2000 */
  3. /* Antoine Lefebvre <antoine.lefebvre@polymtl.ca> */
  4. /* This program is free software; you can redistribute it and/or modify*/
  5. /* it under the terms of the GNU General Public License as published by*/
  6. /* the Free Software Foundation; either version 2 of the License, or */
  7. /* (at your option) any later version. */
  8. /* This program is distributed in the hope that it will be useful, */
  9. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  10. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  11. /* GNU General Public License for more details. */
  12. /* You should have received a copy of the GNU General Public License */
  13. /* along with this program; if not, write to the Free Software */
  14. /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <iostream>
  16. #include <stdio.h>
  17. #include <cmath>
  18. #include "c++rocket.h"
  19. #include "rocket.h"
  20. extern FILE* yyin; // file pointer for the file parser
  21. // function generate by the flex lexer generator (motor.l)
  22. extern int motorscan(char *name, double *diameter,
  23. double *length, double *masse_totale,
  24. double *masse_poudre, double **thrust,
  25. int *ndata);
  26. simulation *simptr; // this is the pointer to the active simulation
  27. const double G = 6.67259e-11; // universal gaz constant
  28. const double R = 8.3145; // perfect gaz constant
  29. const double g = 9.80665; // Earth gravitational constant
  30. // default value for the initialisation
  31. static double rocket_default[PARA_ROCKET_LAST] = { 0.204,
  32. 2,
  33. 2.33,
  34. 20.4,
  35. 6.32,
  36. 31.6,
  37. 0,
  38. 0,
  39. 0.1143,
  40. 16,
  41. 1,
  42. 1.1,
  43. 0.229,
  44. 0 };
  45. static double planete_default[PLANETE_DATA_LAST] = { 5.98e24,
  46. 6370000 };
  47. static double atmosphere_default[ATM_DATA_LAST] = { 1.013e5,
  48. 0.0289644,
  49. 273.15 };
  50. propulseur::~propulseur()
  51. {
  52. cout << "Destroying propulseur\n";
  53. if (!(ndata == 0))
  54. {
  55. delete [] thrust[0];
  56. delete [] thrust[1];
  57. delete [] thrust;
  58. }
  59. }
  60. int propulseur::print_data(void)
  61. {
  62. if (!(check()))
  63. return -1;
  64. cout << "Motor:\t\t\t" << name << endl;
  65. cout << "Diameter:\t\t" << diameter << " m" << endl;
  66. cout << "Length:\t\t" << length << " m" << endl;
  67. cout << "Mass de propergol:\t" << masse_poudre << " Kg" << endl;
  68. cout << "Total mass:\t\t" << masse_totale << " Kg" << endl;
  69. //cout << ndata << endl;
  70. cout << endl;
  71. cout << "Time (s)\t" << "Thrust (N)" << endl;
  72. for (int k = 0; k < ndata; k++)
  73. cout << thrust[0][k] << "\t\t" << thrust[1][k] << endl;
  74. return 0;
  75. }
  76. int propulseur::load_data(char file[32])
  77. {
  78. FILE *file_in = NULL;
  79. if ((file_in = fopen(file, "r")) == NULL)
  80. {
  81. cout << "No such file or directory " << file << endl;
  82. return -1;
  83. }
  84. yyin = file_in;
  85. thrust = new double*[2];
  86. motorscan(name, &diameter, &length, &masse_totale,
  87. &masse_poudre, thrust, &ndata);
  88. fclose(file_in);
  89. burntime = thrust[0][ndata-1];
  90. return 0;
  91. }
  92. //#ifdef WITH_GTK
  93. //int propulseur::plot_data(void) {
  94. // cout << "I plot the data...\n";
  95. // return 0;
  96. //}
  97. //#endif
  98. int propulseur::check(void)
  99. {
  100. if (ndata == 0)
  101. {
  102. cout << "No data loaded\n";
  103. return -1;
  104. }
  105. return 1;
  106. }
  107. double propulseur::T(double time)
  108. {
  109. if (!(check()))
  110. return 0; // no thrust
  111. double pousse;
  112. // there is file with no data at time 0
  113. if (time < thrust[0][0])
  114. return (thrust[1][0]/thrust[0][0])*time;
  115. // after the last data, the thrust is 0
  116. if (time > thrust[0][ndata-1])
  117. return 0;
  118. // linear interpolation ( should be interesting
  119. // to add quadratic interpolation
  120. for (int i = 1; i < ndata; i++)
  121. if ((thrust[0][i] > time) && (thrust[0][i-1] <= time))
  122. {
  123. pousse = (thrust[1][i]-thrust[1][i-1])/
  124. (thrust[0][i]-thrust[0][i-1])*(time-thrust[0][i-1]) +
  125. thrust[1][i-1];
  126. }
  127. return pousse;
  128. }
  129. double propulseur::M(double time)
  130. {
  131. if (!(check()))
  132. return 0;
  133. if (time > burntime)
  134. return (masse_totale - masse_poudre);
  135. else
  136. return (masse_totale - time*(masse_totale-masse_poudre)/burntime);
  137. return 0;
  138. }
  139. double propulseur::impulse(void)
  140. {
  141. if (!(check()))
  142. return -1;
  143. double I = 0;
  144. for (int i = 0; i < (ndata - 1); i++)
  145. I += (thrust[1][i]+thrust[1][i+1])*(thrust[0][i+1]-thrust[0][i])/2;
  146. return I;
  147. }
  148. double propulseur::favg(void)
  149. {
  150. if (check())
  151. return (impulse()/burntime);
  152. return -1;
  153. }
  154. double propulseur::vg(void)
  155. {
  156. if (check())
  157. return impulse()/masse_poudre;
  158. return -1;
  159. }
  160. double propulseur::isp(void)
  161. {
  162. if (check())
  163. return vg()/g;
  164. return -1;
  165. }
  166. double propulseur::propellant_mass_fraction(void)
  167. {
  168. if (check())
  169. return masse_poudre/masse_totale;
  170. return -1;
  171. }
  172. double propulseur::impulse_to_weight(void)
  173. {
  174. if (check())
  175. return impulse()/(masse_totale*g);
  176. return -1;
  177. }
  178. rocket::rocket(void)
  179. {
  180. nprop = 0;
  181. r_data = rocket_default;
  182. }
  183. rocket::rocket(double *r)
  184. {
  185. nprop = 0;
  186. for (int i = 0; i < PARA_ROCKET_LAST; i++)
  187. r_data[i] = r[i];
  188. }
  189. int rocket::set_propulseurs(int stage, char file[128])
  190. {
  191. if (stage > MAXPROP)
  192. return -1;
  193. if (!(stage == 1))
  194. if (prop[stage-2].ndata == 0) {
  195. cout << "Stage " << stage-1 << " not loaded.\n";
  196. return -1;
  197. }
  198. prop[stage-1].load_data(file);
  199. nprop = stage;
  200. return 0;
  201. }
  202. planete::planete(void)
  203. {
  204. p_data = planete_default;
  205. }
  206. planete::planete(double *p)
  207. {
  208. for (int i = 0; i < PLANETE_DATA_LAST; i++)
  209. p_data[i] = p[i];
  210. }
  211. planete::planete(double *p, double *a) : atmosphere(a)
  212. {
  213. for (int i = 0; i < PLANETE_DATA_LAST; i++)
  214. p_data[i] = p[i];
  215. }
  216. double planete::densiteatm(double altitude)
  217. {
  218. double d = (((a_data[pression_sol] * a_data[masse_molaire]) / (R*a_data[temperature]))* exp(( (a_data[masse_molaire]*G*p_data[masse])/(R*a_data[temperature]) ) * ( (1/(p_data[rayon] + altitude)) - (1/p_data[rayon]) ) ));
  219. return d;
  220. }
  221. atmosphere::atmosphere(void)
  222. {
  223. a_data = atmosphere_default;
  224. }
  225. atmosphere::atmosphere(double* a)
  226. {
  227. for (int i = 0; i < ATM_DATA_LAST; i++)
  228. a_data[i] = a[i];
  229. }
  230. flight_program::flight_program(void)
  231. {
  232. for (int i = 0; i < MAXPROP; i++)
  233. {
  234. ta[i] = 0;
  235. tl[i] = 0;
  236. }
  237. }
  238. int flight_program::set_prog(int stage, double allumage, double largage)
  239. {
  240. if ( (stage < 1) || (stage > MAXPROP) )
  241. return -1;
  242. ta[stage - 1] = allumage;
  243. tl[stage - 1] = largage;
  244. return 0;
  245. }