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.

118 lines
2.7 KiB

  1. /* rk4.cc - Simulation of rocket flight */
  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 <stdlib.h>
  16. #include <stdio.h>
  17. #include <cmath>
  18. #include <iostream>
  19. #include <fstream.h>
  20. #include "rk4.h"
  21. #include "c++rocket.h"
  22. static int ModelNeq[MODEL_LAST] = { 7, 5 };
  23. rk4_solver::rk4_solver(Model_t model)
  24. {
  25. neq = ModelNeq[model];
  26. time = 0.0;
  27. memory = 0;
  28. switch (model)
  29. {
  30. case AERO_MODEL:
  31. md = model_1;
  32. break;
  33. case SIMPLE_MODEL:
  34. md = model_2;
  35. break;
  36. default:
  37. exit(-1);
  38. }
  39. }
  40. rk4_solver::~rk4_solver()
  41. {
  42. if (memory) {
  43. for (int j = 0; j < length; j++)
  44. delete [] ans[j];
  45. delete ans;
  46. }
  47. cout << "Freeing memory...\n" ;
  48. }
  49. int rk4_solver::solve(double *st, double duration, double step)
  50. {
  51. int i;
  52. length = (int)rint(duration/step) + 1;
  53. // memory allocation
  54. ans = new double*[length];
  55. for (int j = 0; j < length; j++)
  56. ans[j] = new double[neq];
  57. memory = 1;
  58. // fill the ans array
  59. rk4 ( (ModelFunc_t)md, neq, step, duration, st, ans, NULL);
  60. return length;
  61. }
  62. // print the data to screen
  63. void rk4_solver::print()
  64. {
  65. for (int i = 0; i < length; i++)
  66. {
  67. for (int j = 0; j < neq; j++)
  68. {
  69. cout << ans[i][j] << "\t";
  70. }
  71. cout << "\n";
  72. }
  73. }
  74. // export the data in filename with the format need by octave
  75. // for the function load. It will maybe work with matlab to
  76. // but i'm not sure
  77. void rk4_solver::export_octave(char *filename)
  78. {
  79. ofstream out(filename);
  80. out << "# Create by c++rocket" << endl;
  81. out << "# name: M" << endl;
  82. out << "# type: matrix" << endl;
  83. out << "# rows: " << length << endl;
  84. out << "# columns: " << neq << endl;
  85. for (int i = 0; i < length; i++)
  86. {
  87. for (int j = 0; j < neq; j++)
  88. {
  89. out << ans[i][j] << " ";
  90. }
  91. out << endl;
  92. }
  93. }