RLV_example.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /////////////////////////////// RLV动力学积分部分 //////////////////////////////
  2. // 动力学方程积分部分
  3. // 输入: StateVar: 状态变量
  4. // ControlVar:控制变量
  5. // auxdata: 其他参数
  6. // 输出: dState: 状态微分
  7. // integrand: 积分量
  8. // path: 路径约束
  9. // 方法:无
  10. // 编写:李兆亭
  11. // 时间:2020 / 09 / 17
  12. // 其他:由用户自定义
  13. #include <iostream>
  14. #include <armadillo>
  15. #include <time.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "GPM.h"
  19. #include <math.h>
  20. using namespace std;
  21. using namespace arma;
  22. int Dynamics123(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path) {
  23. // 动力学模型
  24. mat State = *StateVar;
  25. mat Control = *ControlVar;
  26. // 常数
  27. double g = auxdata.g;
  28. double Re = auxdata.Re;
  29. // 状态量本地化
  30. vec v = State.col(0);
  31. vec theta = State.col(1);
  32. vec psai = State.col(2);
  33. vec r = State.col(3);
  34. vec lamda = State.col(4);
  35. vec phi = State.col(5);
  36. // 控制量本地化
  37. vec alpha = Control.col(0);
  38. vec sigma = Control.col(1);
  39. // 参量计算
  40. vec H = r*Re - Re;
  41. int n = size(H)(0);
  42. vec Rou = zeros(n, 1);
  43. vec Ma = zeros(n, 1);
  44. vec L = zeros(n, 1);
  45. vec D = zeros(n, 1);
  46. Get_Rou_Mach( H, v*sqrt(Re*g), &Rou, &Ma);
  47. alpha2LD(alpha, Rou, Ma, v*sqrt(Re*g), auxdata, &L, &D);
  48. L = L / g; // 这里是归一化
  49. D = D / g;
  50. // 微分方程
  51. vec dv = -D - sin(theta);
  52. vec dtheta = L%cos(sigma) / v - cos(theta) / v + v%cos(theta)/ r;
  53. vec dpsai = L%sin(sigma) / v / cos(theta) + v%tan(phi)%cos(theta)%sin(psai)/ r;
  54. vec dr = v%sin(theta);
  55. vec dlamda = v%cos(theta)%sin(psai)/ r/ cos(phi);
  56. vec dphi = v%cos(theta)%cos(psai)/ r;
  57. double C1_v = auxdata.C1_v; // 与热流密度相关参数
  58. double Rd_v = auxdata.Rd_v; // 与热流密度相关参数
  59. double n_v = auxdata.n_v; // 与热流密度相关参数
  60. double m_v = auxdata.m_v; // 与热流密度相关参数
  61. vec path1 = sqrt(L%L + D%D);
  62. vec path2 = 0.5*Rou%v%v*Re*g;
  63. #ifdef LUOYC20220701
  64. printf("C1_v = %lf, Rd_v = %lf, n_v = %lf, m_v = %lf\n", C1_v, Rd_v, n_v, m_v);
  65. printf("Rou = \n");
  66. for (int i = 0; i < Rou.n_elem; i++)
  67. {
  68. printf("%lf ", *(Rou.mem + i));
  69. }
  70. printf("\n");
  71. printf("V = \n");
  72. for (int i = 0; i < v.n_elem; i++)
  73. {
  74. printf("%lf ", *(v.mem + i));
  75. }
  76. printf("\n");
  77. #endif
  78. #ifdef LUOYC20220701
  79. double tmp1 = sqrt(Rd_v);
  80. double tmp2 = sqrt(Re * g);
  81. double tmp3 = C1_v / tmp1;
  82. // printf("tmp1 = %lf, tmp2 = %lf, tmp3 = %lf\n", tmp1, tmp2, tmp3);
  83. vec vtmp1 = pow(Rou, n_v);
  84. /*
  85. int len = Rou.n_elem;
  86. double rou[len];
  87. for(int l = 0; l < len; l++)
  88. {
  89. rou[l] = *(Rou.mem + l);
  90. }
  91. printf("pow(Rou, n_v) = \n");
  92. for (int i = 0; i < vtmp1.n_elem; i++)
  93. {
  94. printf("%lf ", *(vtmp1.mem + i));
  95. }
  96. printf("\n");
  97. */
  98. vec vtmp2 = v * tmp2;
  99. printf("vtmp2 = \n");
  100. for (int i = 0; i < vtmp2.n_elem; i++)
  101. {
  102. printf("%lf ", *(vtmp2.mem + i));
  103. }
  104. printf("\n");
  105. vec vtmp3 = pow(vtmp2, m_v);
  106. printf("vtmp3 = \n");
  107. #if 0
  108. for (int i = 0; i < vtmp3.n_elem; i++)
  109. {
  110. if(isnan(*(vtmp2.mem + i)))
  111. {
  112. *(vtmp3.mem + i) *= 0*log((double)0);
  113. }
  114. printf("%lf ", *(vtmp3.mem + i));
  115. }
  116. printf("\n");
  117. #endif
  118. vec path3 = C1_v / tmp1 * vtmp1 % vtmp3;
  119. #else
  120. vec path3 = C1_v / sqrt(Rd_v)*pow(Rou, n_v)%pow((v*sqrt(Re*g)),m_v);
  121. #endif
  122. #ifdef LUOYC20220701
  123. printf("Dynamics123 path3 = :\n");
  124. for (int j = 0; j < 6; j++)
  125. {
  126. printf("%lf ", *(path3.mem + j));
  127. }
  128. printf("\n");
  129. #endif
  130. mat dState0(n, 6);
  131. dState0.col(0) = dv;
  132. dState0.col(1) = dtheta;
  133. dState0.col(2) = dpsai;
  134. dState0.col(3) = dr;
  135. dState0.col(4) = dlamda;
  136. dState0.col(5) = dphi;
  137. mat path0(n, 3);
  138. path0.col(0) = path1;
  139. path0.col(1) = path2;
  140. path0.col(2) = path3;
  141. mat integrand0(n, 1);
  142. integrand0.col(0) = abs(dtheta);
  143. *dState = dState0;
  144. #ifdef LUOYC20220706
  145. printf("RLV_example row = %d, col = %d, n_elem = %d\n", dState->n_rows, dState->n_cols, dState->n_elem);
  146. #endif
  147. *integrand = integrand0;
  148. #ifdef LUOYC20220706
  149. printf("RLV_example1 row = %d, col = %d, n_elem = %d\n", dState->n_rows, dState->n_cols, dState->n_elem);
  150. #endif
  151. *path = path0;
  152. #ifdef LUOYC20220706
  153. printf("RLV_example2 row = %d, col = %d, n_elem = %d\n", dState->n_rows, dState->n_cols, dState->n_elem);
  154. #endif
  155. int aaaa = 1;
  156. return 1;
  157. }
  158. int Dynamics123(cx_mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path) {
  159. // 待修改。
  160. // 2021.08.13
  161. // 动力学模型
  162. cx_mat State = *StateVar;
  163. mat Control = *ControlVar;
  164. // 常数
  165. double g = auxdata.g;
  166. double Re = auxdata.Re;
  167. // 状态量本地化
  168. vec v = abs(State.col(0));
  169. vec theta = abs(State.col(1));
  170. vec psai = abs(State.col(2));
  171. vec r = abs(State.col(3));
  172. vec lamda = abs(State.col(4));
  173. vec phi = abs(State.col(5));
  174. // 控制量本地化
  175. vec alpha = Control.col(0);
  176. vec sigma = Control.col(1);
  177. // 参量计算
  178. vec H = r*Re - Re;
  179. int n = size(H)(0);
  180. vec Rou = zeros(n, 1);
  181. vec Ma = zeros(n, 1);
  182. vec L = zeros(n, 1);
  183. vec D = zeros(n, 1);
  184. Get_Rou_Mach(H, v*sqrt(Re*g), &Rou, &Ma);
  185. alpha2LD(alpha, Rou, Ma, v*sqrt(Re*g), auxdata, &L, &D);
  186. L = L / g; // 这里是归一化
  187. D = D / g;
  188. // 微分方程
  189. vec dv = -D - sin(theta);
  190. vec dtheta = L%cos(sigma) / v - cos(theta) / v + v%cos(theta) / r;
  191. vec dpsai = L%sin(sigma) / v / cos(theta) + v%tan(phi) % cos(theta) % sin(psai) / r;
  192. vec dr = v%sin(theta);
  193. vec dlamda = v%cos(theta) % sin(psai) / r / cos(phi);
  194. vec dphi = v%cos(theta) % cos(psai) / r;
  195. double C1_v = auxdata.C1_v; // 与热流密度相关参数
  196. double Rd_v = auxdata.Rd_v; // 与热流密度相关参数
  197. double n_v = auxdata.n_v; // 与热流密度相关参数
  198. double m_v = auxdata.m_v; // 与热流密度相关参数
  199. vec path1 = sqrt(L%L + D%D);
  200. vec path2 = 0.5*Rou%v%v*Re*g;
  201. vec path3 = C1_v / sqrt(Rd_v)*pow(Rou, n_v) % pow((v*sqrt(Re*g)), m_v);
  202. mat dState0(n, 6);
  203. // dState0.col(0) = cx_vec(dv,zeros(size(dState0.col(0)))) // 复数形式
  204. dState0.col(0) = dv;
  205. dState0.col(1) = dtheta;
  206. dState0.col(2) = dpsai;
  207. dState0.col(3) = dr;
  208. dState0.col(4) = dlamda;
  209. dState0.col(5) = dphi;
  210. mat path0(n, 3);
  211. path0.col(0) = path1;
  212. path0.col(1) = path2;
  213. path0.col(2) = path3;
  214. mat integrand0(n, 1);
  215. integrand0.col(0) = abs(dtheta);
  216. *dState = dState0;
  217. *integrand = integrand0;
  218. *path = path0;
  219. int aaaa = 1;
  220. return 1;
  221. }
  222. int Dynamics123(mat* StateVar, cx_mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path) {
  223. // 待修改。
  224. // 2021.08.13
  225. // 动力学模型
  226. mat State = *StateVar;
  227. cx_mat Control = *ControlVar;
  228. // 常数
  229. double g = auxdata.g;
  230. double Re = auxdata.Re;
  231. // 状态量本地化
  232. vec v = State.col(0);
  233. vec theta = State.col(1);
  234. vec psai = State.col(2);
  235. vec r = State.col(3);
  236. vec lamda = State.col(4);
  237. vec phi = State.col(5);
  238. // 控制量本地化
  239. vec alpha = abs(Control.col(0));
  240. vec sigma = abs(Control.col(1));
  241. // 参量计算
  242. vec H = r*Re - Re;
  243. int n = size(H)(0);
  244. vec Rou = zeros(n, 1);
  245. vec Ma = zeros(n, 1);
  246. vec L = zeros(n, 1);
  247. vec D = zeros(n, 1);
  248. Get_Rou_Mach(H, v*sqrt(Re*g), &Rou, &Ma);
  249. alpha2LD(alpha, Rou, Ma, v*sqrt(Re*g), auxdata, &L, &D);
  250. L = L / g; // 这里是归一化
  251. D = D / g;
  252. // 微分方程
  253. vec dv = -D - sin(theta);
  254. vec dtheta = L%cos(sigma) / v - cos(theta) / v + v%cos(theta) / r;
  255. vec dpsai = L%sin(sigma) / v / cos(theta) + v%tan(phi) % cos(theta) % sin(psai) / r;
  256. vec dr = v%sin(theta);
  257. vec dlamda = v%cos(theta) % sin(psai) / r / cos(phi);
  258. vec dphi = v%cos(theta) % cos(psai) / r;
  259. double C1_v = auxdata.C1_v; // 与热流密度相关参数
  260. double Rd_v = auxdata.Rd_v; // 与热流密度相关参数
  261. double n_v = auxdata.n_v; // 与热流密度相关参数
  262. double m_v = auxdata.m_v; // 与热流密度相关参数
  263. vec path1 = sqrt(L%L + D%D);
  264. vec path2 = 0.5*Rou%v%v*Re*g;
  265. vec path3 = C1_v / sqrt(Rd_v)*pow(Rou, n_v) % pow((v*sqrt(Re*g)), m_v);
  266. mat dState0(n, 6);
  267. dState0.col(0) = dv;
  268. dState0.col(1) = dtheta;
  269. dState0.col(2) = dpsai;
  270. dState0.col(3) = dr;
  271. dState0.col(4) = dlamda;
  272. dState0.col(5) = dphi;
  273. mat path0(n, 3);
  274. path0.col(0) = path1;
  275. path0.col(1) = path2;
  276. path0.col(2) = path3;
  277. mat integrand0(n, 1);
  278. integrand0.col(0) = abs(dtheta);
  279. *dState = dState0;
  280. *integrand = integrand0;
  281. *path = path0;
  282. int aaaa = 1;
  283. return 1;
  284. }
  285. /////////////////////////////////////////// 性能指标部分 ////////////////////////////////
  286. // 性能指标部分
  287. // 输入: input: 输入
  288. // 输出: output: 输出
  289. // event: 事件约束
  290. // 方法:无
  291. // 编写:李兆亭
  292. // 时间:2020 / 09 / 17
  293. // 其他:由用户自定义
  294. int Objective123(input0* input, double* output, mat* event) {
  295. // Inputs
  296. // input.phase(phasenumber).initialstate -- row
  297. // input.phase(phasenumber).finalstate -- row
  298. // input.phase(phasenumber).initialtime -- scalar
  299. // input.phase(phasenumber).finaltime -- scalar
  300. // input.phase(phasenumber).integral -- row
  301. // input.parameter -- row
  302. // input.auxdata = auxiliary information
  303. // Output
  304. // output.objective -- scalar
  305. // output.eventgroup(eventnumber).event -- row
  306. double is1 = (*input).phase.initialstate(0);
  307. double fs1 = (*input).phase.finalstate(0);
  308. double is2 = (*input).phase.initialstate(1);
  309. double fs2 = (*input).phase.finalstate(1);
  310. (*output) = (*input).phase.finaltime;
  311. (*event).reset();
  312. return 1;
  313. }
  314. /*function[output, event] = Objective(input)
  315. % 功能:计算终端点处的有关目标函数。
  316. % 利用Matlab矩阵计算较快的特点
  317. % Inputs
  318. % input.phase(phasenumber).initialstate -- row
  319. % input.phase(phasenumber).finalstate -- row
  320. % input.phase(phasenumber).initialtime -- scalar
  321. % input.phase(phasenumber).finaltime -- scalar
  322. % input.phase(phasenumber).integral -- row
  323. %
  324. % input.parameter -- row
  325. % input.auxdata = auxiliary information
  326. % Output
  327. % output.objective -- scalar
  328. % output.eventgroup(eventnumber).event -- row
  329. output = input.phase.finaltime;
  330. is1 = input.phase.initialstate(1);
  331. fs1 = input.phase.finalstate(1);
  332. is2 = input.phase.initialstate(2);
  333. fs2 = input.phase.finalstate(2);
  334. event = [fs1 + fs2];
  335. end*/