GPM.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #ifndef GPM_H
  2. #define GPM_H
  3. //代码
  4. //////////////////////*头文件包含部分*/////////////////////////////////////
  5. #include "program.hpp"
  6. #include <cassert>
  7. #include <iostream>
  8. #include <math.h>
  9. #include <armadillo>
  10. using namespace arma;
  11. using namespace std;
  12. using namespace Ipopt;
  13. ////////////////////////*类声明部分*////////////////////////////////////////
  14. //////*input结构体部分*//////
  15. /* 过程中的梯度信息结构体 */
  16. struct Grad_struct_info0 {
  17. public:
  18. /*成员变量*/
  19. arma::rowvec dJdI;
  20. int dJdend;
  21. arma::mat dIdz;
  22. arma::mat dedS;
  23. arma::mat dedt;
  24. arma::mat dedp;
  25. };
  26. /* 过程中的雅可比结构信息 */
  27. struct Jacb_struct_info0 {
  28. public:
  29. /*成员变量*/
  30. arma::mat dD_dS_L;
  31. arma::mat dD_dS_R;
  32. arma::mat dD_dU;
  33. arma::urowvec dD_dt;
  34. arma::umat dD_dp;
  35. arma::mat dP_dS;
  36. arma::mat dP_dU;
  37. arma::mat dP_dt;
  38. arma::umat dP_dp;
  39. arma::mat dI_dS;
  40. arma::mat dI_dU;
  41. arma::mat dI_dt;
  42. arma::sp_mat dI_dp;
  43. arma::mat dE_dS;
  44. arma::mat dE_dU;
  45. arma::mat dE_dt;
  46. arma::mat dE_dp;
  47. };
  48. /* struct结构体 input 部分 */
  49. struct phase0 {
  50. public:
  51. /*成员变量*/
  52. arma::vec time;
  53. arma::mat state;
  54. arma::mat control;
  55. arma::rowvec integral;
  56. arma::vec interval_points;
  57. arma::mat path;
  58. };
  59. struct guess0 {
  60. public:
  61. /*成员变量*/
  62. struct phase0 phase;
  63. };
  64. struct ipoptoptions1 {
  65. public:
  66. /*成员变量*/
  67. int print_level;
  68. char* hessian_approximation;
  69. char* mu_strategy;
  70. char* linear_solver;
  71. double tolerance;
  72. int maxiterration;
  73. };
  74. struct NLP1 {
  75. public:
  76. /*成员变量*/
  77. char *solver;
  78. struct ipoptoptions1 ipoptoptions;
  79. };
  80. struct phase1 {
  81. public:
  82. /*成员变量*/
  83. int inter_num;
  84. arma::vec colpoints;
  85. arma::vec i_proportion;
  86. };
  87. struct refinement1 {
  88. public:
  89. /*成员变量*/
  90. double Conservative_coefficient;
  91. };
  92. struct mesh1 {
  93. public:
  94. /*成员变量*/
  95. char* method;
  96. double tolerance;
  97. int maxiterration;
  98. int colpointsmin;
  99. int colpointsmax;
  100. double splitmult;
  101. double curveratio;
  102. struct phase1 phase;
  103. struct refinement1 refinement;
  104. };
  105. struct derivatives1 {
  106. public:
  107. /*成员变量*/
  108. char* supplier;
  109. int derivativelevel;
  110. char* dependencies;
  111. int number;
  112. };
  113. struct auxdata1 {
  114. public:
  115. /*成员变量*/
  116. double GMe;
  117. double Sref;
  118. double M0;
  119. double C1_v;
  120. double Rd_v;
  121. double n_v;
  122. double m_v;
  123. double Re;
  124. double g;
  125. //新增
  126. double J2;
  127. double omige;
  128. double Ac;
  129. double ae;
  130. double re;
  131. double phif;
  132. double lamf;
  133. arma::vec parameter;
  134. };
  135. struct initialtime1 {
  136. public:
  137. /*成员变量*/
  138. double lower;
  139. double upper;
  140. };
  141. struct finaltime1 {
  142. public:
  143. /*成员变量*/
  144. double lower;
  145. double upper;
  146. };
  147. struct state1 {
  148. public:
  149. /*成员变量*/
  150. arma::vec lower;
  151. arma::vec upper;
  152. };
  153. struct initialstate1 {
  154. public:
  155. /*成员变量*/
  156. arma::vec lower;
  157. arma::vec upper;
  158. };
  159. struct finalstate1 {
  160. public:
  161. /*成员变量*/
  162. arma::vec lower;
  163. arma::vec upper;
  164. };
  165. struct control1 {
  166. public:
  167. /*成员变量*/
  168. arma::vec lower;
  169. arma::vec upper;
  170. };
  171. struct path1 {
  172. public:
  173. /*成员变量*/
  174. arma::vec lower;
  175. arma::vec upper;
  176. };
  177. struct integral1 {
  178. public:
  179. /*成员变量*/
  180. arma::rowvec lower;
  181. arma::rowvec upper;
  182. };
  183. struct eventgroup1 {
  184. public:
  185. /*成员变量*/
  186. arma::vec lower;
  187. arma::vec upper;
  188. };
  189. struct event1 {
  190. public:
  191. /*成员变量*/
  192. arma::vec lower;
  193. arma::vec upper;
  194. };
  195. struct parameter1 {
  196. public:
  197. /*成员变量*/
  198. arma::vec lower;
  199. arma::vec upper;
  200. };
  201. struct phase2 {
  202. public:
  203. /*成员变量*/
  204. struct initialtime1 initialtime;
  205. struct finaltime1 finaltime;
  206. struct state1 state;
  207. struct initialstate1 initialstate;
  208. struct finalstate1 finalstate;
  209. struct control1 control;
  210. struct path1 path;
  211. struct integral1 integral;
  212. struct eventgroup1 eventgroup;
  213. struct event1 event;
  214. struct parameter1 parameter;
  215. };
  216. struct bounds1 {
  217. public:
  218. /*成员变量*/
  219. struct phase2 phase;
  220. };
  221. struct interpX{
  222. public:
  223. /*成员变量*/
  224. char* method;
  225. };
  226. struct feature1 {
  227. public:
  228. /*成员变量*/
  229. int FSE;
  230. };
  231. struct setup0 {
  232. public:
  233. /*成员变量*/
  234. int testmode;
  235. double warmstart;
  236. struct feature1 feature;
  237. struct interpX interp;
  238. char *method;
  239. struct guess0 guess;
  240. struct NLP1 NLP;
  241. struct mesh1 mesh;
  242. struct derivatives1 derivatives;
  243. struct auxdata1 auxdata;
  244. struct bounds1 bounds;
  245. int control_length;
  246. int state_length;
  247. int parameter_length;
  248. int scales;
  249. char* name;
  250. int Scal;
  251. };
  252. struct phase_result {
  253. public:
  254. /*成员变量*/
  255. arma::vec initialstate;
  256. arma::vec finalstate;
  257. double initialtime;
  258. double finaltime;
  259. arma::rowvec integral;
  260. };
  261. struct input0 {
  262. public:
  263. /*成员变量*/
  264. char *name;
  265. struct setup0 setup;
  266. struct phase_result phase;
  267. };
  268. /////////////////////////////
  269. struct functions1 {
  270. public:
  271. /*成员变量*/
  272. int(*continous1)(mat*, mat*, auxdata1, mat*, mat*, mat*);
  273. int(*continous2)(cx_mat*, mat*, auxdata1, mat*, mat*, mat*);
  274. int(*continous3)(mat*, cx_mat*, auxdata1, mat*, mat*, mat*);
  275. int(*endpoint)(input0*, double*, mat*);
  276. };
  277. ////////*output结构体*///////
  278. /* struct结构体 output 部分 */
  279. struct result0 {
  280. public:
  281. /*成员变量*/
  282. arma::vec time;
  283. arma::mat state;
  284. arma::mat control;
  285. double objective;
  286. arma::rowvec mesh_N;
  287. arma::mat mesh_time;
  288. auxdata1 auxdata;
  289. double max_re_error;
  290. };
  291. struct error0 {
  292. public:
  293. /*成员变量*/
  294. double max_re_error_max;
  295. arma::vec re_error_max;
  296. };
  297. struct output0 {
  298. public:
  299. /*成员变量*/
  300. struct result0 result;
  301. arma::vec re_error_max;
  302. arma::mat Final_State_Estimation;
  303. };
  304. ///////////////////////////*函数声明部分*////////////////////////////////////////
  305. int Lagrange_base(mat* point, vec* x, vec* f);
  306. int DLagrange_point(mat* point1, int *k0, int *j, double *f);
  307. double D3Lagrange_point(mat point1, int k0, int j);
  308. int D3L_interval_LGR(mat point1, int Nk, mat* f);
  309. int DL_interval_LGR(mat* point1, int* Nk, mat* f);
  310. int Legendre(int* n, double* x0, double* f);
  311. int Legendre_vector(int* n, vec* x0, vec* f);
  312. int Sum_ZeroPoints(vec* L0, vec* x0, int* Num, mat* Pos);
  313. int Dichotomy_LGR(int* n, vec* Pos, double* tn);
  314. int LGR_points_pro(vec* tn, int* n);
  315. int Weight_LGR_pro(int* N, vec* x, vec* w);
  316. int Intput_Initial(input0* input);
  317. output0* PM_Process(input0* input, functions1 functions, int *iteration_final);
  318. vec Normal(vec b, double bmax, double bmin);
  319. mat Points_Choose(char* method);
  320. int Mesh_initial(int colpointsmax, int interval_num, vec Npoints, mat Col_Points, vec interval_points_ratio,
  321. mat* MeshGrid, mat* Timestart_Timeend);
  322. int Dismission_check(input0* input, int* Sign_start);
  323. int Method2Nk(const char* method, uvec N, uvec* Nk);
  324. int N_Col2interp(const char* method, uvec N_Col, uvec* N_interp);
  325. int D_pro(mat* MeshGrid, int interval_num, uvec* N_interp, uvec* N_Col, mat* D_phase);
  326. int D_M2Cell(mat* D_phase, int interval_num, uvec* N_interp, field<mat>* D_phasei);
  327. vec Re_Normal(vec b, double bmax, double bmin);
  328. int UY_pro(mat MeshGrid, mat U_old1, mat Y_old1, vec T_old1, mat Ts1_Te1, uvec N_interp, int inter_num, char* method,
  329. mat* U_phase, mat* Y_phase);
  330. int W_pro(char* method, uvec* N, uvec* N_Col, int interval_num, mat* W_phase);
  331. int N_initial(vec N1, uvec* N, int interval_num);
  332. int Z_pro(mat Y_phase1, mat U_phase1, vec parameter, uvec last_interp, int Col_total, int length_state, int length_control, double tf, double t0, vec* Z);
  333. int Integral_ipopt(vec Z1, mat deta_t_in_col, int Col_total, auxdata1 auxdata,mat W_phase, int length_state, int length_control, vec* integral, int length_parameter,
  334. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path));
  335. int Grad_struct_solve(vec Z0, mat deta_t_in_col, int Col_total, auxdata1 auxdata, mat W_phase, int length_state,
  336. int length_control, int length_integral, Grad_struct_info0* Grad_struct_info, int length_parameter,
  337. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  338. int(*Objective_ptr)(input0* input, double* output, mat* event));
  339. int End_Objective_ipopt(vec Z1, int Col_total, int length_state, int length_integral, int length_parameter, int length_control,
  340. int(*Objective_ptr)(input0* input, double* output, mat* event),
  341. double* J_end);
  342. int Istruct(vec Z1, int Col_total, int length_state, int length_control, int length_integral, mat W_phase, auxdata1 auxdata,
  343. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  344. vec* I);
  345. int IZstruct(vec Z0, int Col_total, int length_state, int length_control, int length_integral,mat W_phase, auxdata1 auxdata, sp_mat*I_Z_struct0,
  346. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path));
  347. int Fmin_pro(input0 input, int Col_total, int length_state, int length_path, int length_integral, int length_event, vec* Fmin);
  348. int Fmax_pro(input0 input, int Col_total, int length_state, int length_path, int length_integral, int length_event, vec* Fmax);
  349. int Zmin_pro(input0 input, int Col_total, vec* Zmin);
  350. int Zmax_pro(input0 input, int Col_total, vec* Zmax);
  351. int F_pro_struct(vec Z1, mat D_phase, uvec first_col, mat deta_t_in_col, int length_path, int length_parameter, int Col_total, auxdata1 auxdata,
  352. mat W_phase, int z_len, int length_event, int length_state, int length_control, int length_integral, int interval_num, uvec last_interp,
  353. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  354. int(*Objective_ptr)(input0* input, double* output, mat* event),
  355. vec* F1);
  356. int Jacb_struct_pro0(mat Z0, mat D_phase, uvec first_col, mat deta_t_in_col, int length_event, int length_path, int length_parameter,
  357. int Col_total, auxdata1 auxdata, mat W_phase, int length_state, int length_control, int length_integral, int length_F0,
  358. int interval_num, uvec last_interp,
  359. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  360. int(*Objective_ptr)(input0* input, double* output, mat* event),
  361. sp_mat *Jacb_struct0);
  362. int Jacb_solve(sp_mat Jacb_struct01, int Col_total, int length_event, int length_path, int length_state, int length_control, int length_integral, int length_parameter, Jacb_struct_info0* Jacb_struct_info);
  363. int vector_ARM2NLP(vec vector_ARM, Number* vector_NLP, int n);
  364. int vector_NLP2ARM(const Number* vector_NLP, vec* vector_ARM, int n);
  365. int Objective_ipopt(vec Z1, mat deta_t_in_col, int Col_total, auxdata1 auxdata, mat W_phase, int length_state, int length_control, int length_parameter,
  366. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  367. int(*Objective_ptr)(input0* input, double* output, mat* event),
  368. double* J);
  369. int Grad_pro(vec Z0, Grad_struct_info0 Grad_struct_info, mat deta_t_in_col, int Col_total, auxdata1 auxdata, mat W_phase, int length_state,
  370. int length_control, int length_integral, int length_parameter, double eps1, char *deri_supplier,
  371. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  372. int(*Objective_ptr)(input0* input, double* output, mat* event),
  373. sp_mat *Grad);
  374. int F_pro(vec Z1, mat D_phase, uvec first_col, mat deta_t_in_col, int length_path, int Col_total, auxdata1 auxdata, mat W_phase,
  375. int z_len, int length_event, int length_state, int length_control, int length_integral, int length_parameter,
  376. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  377. int(*Objective_ptr)(input0* input, double* output, mat* event),
  378. vec* F1);
  379. int Jacb_pro(vec Z0, field<mat> D_phasei, uvec N_Col, mat deta_t_in_col, sp_mat I_Z_struct0, Jacb_struct_info0 Jacb_struct_info, int length_F0, int length_path, int Col_total, auxdata1 auxdata,
  380. mat W_phase, int length_event, int length_state, int length_control, int length_integral, int length_parameter, int interval_num, double eps1, char *deri_supplier, int length_Z0,
  381. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  382. int(*Objective_ptr)(input0* input, double* output, mat* event),
  383. sp_mat* Jacb);
  384. int sp_ARM2NLP(sp_mat X, Index* iRow, Index *jCol, int nnz_jac);
  385. int values_initial(Number* values, Index nnz_jac);
  386. int values_set(sp_mat Jacb, sp_mat Jacb_struct0, Number* values, Index nnz_jac);
  387. void get_var_num(int varnum0);
  388. void get_constraint_num(int constraint_num0);
  389. void get_nnz_jac(int nnz_jac0);
  390. void get_nnz_h(int nnz_h0);
  391. void get_x0(vec x00);
  392. void get_bounds(vec Fmin0, vec Fmax0, vec Zmin0, vec Zmax0);
  393. void get_data(auxdata1 auxdata00, mat W_phase00, mat deta_t_in_col00, int Col_total00, int length_state00, int length_control00,
  394. Grad_struct_info0 Grad_struct_info00, int length_integral00, double eps00, mat D_phase00, uvec first_col00, int length_path00,
  395. int length_event00, sp_mat Jacb_struct00, field<mat> D_phasei0, uvec N_Col00, sp_mat I_Z_struct00, int interval_num00,
  396. int length_Z00, int length_parameter00, Jacb_struct_info0 Jacb_struct_info00, char *deri_supplier00, int length_F000,
  397. int(*Dynamics_ptr00)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  398. int(*Objective_ptr)(input0* input, double* output, mat* event));
  399. int ipopt_apply(int var_num, int constraint_num, int nnz_jac, int nnz_h, vec x00, vec Fmin0, vec Fmax0, vec Zmin0, vec Zmax0,
  400. auxdata1 auxdata00, mat W_phase00, mat deta_t_in_col00, int Col_total00, int length_state00, int length_control00,
  401. Grad_struct_info0 Grad_struct_info00, int length_integral00, double eps00, mat D_phase00, uvec first_col00, int length_path00,
  402. int length_event00, sp_mat Jacb_struct00, field<mat> D_phasei0, uvec N_Col00, sp_mat I_Z_struct00, int interval_num00,
  403. int length_Z00, int length_parameter00, Jacb_struct_info0 Jacb_struct_info00, char *deri_supplier00, int length_F000, NLP1 NLP,
  404. int(*Dynamics_ptr00)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  405. int(*Objective_ptr)(input0* input, double* output, mat* event),
  406. vec* z_ans, vec* zl_temp, vec* zu_temp, vec* lambda_temp, int* iter_num, double *cpu_time);
  407. int Status_Display(int status);
  408. int Z2YU_interval(vec x_ans, int Col_total, int length_state, int length_control, mat *Y_phase_ans, mat *U_phase_ans);
  409. mat Re_Normal(mat b, double bmax, double bmin);
  410. int Time_ID(vec* Ts1_Te1, double t0, double tf, double t0_ans, double tf_ans);
  411. int Time2Newtime(mat Ts1_Te1, mat MeshGrid, double max_time, double min_time, double t0_ans, double tf_ans, int interval_num, int Col_total, uvec last_col, vec* Time_interval_ans);
  412. mat Normal(mat b, double bmax, double bmin);
  413. int Jacb_struct_Re(Jacb_struct_info0 Jacb_struct_info, mat MeshGrid_nextstep, int length_state, int length_control, int length_path, int length_event, int length_integral, int length_parameter, sp_mat* Jacb_struct1);
  414. // 终端误差状态估计
  415. int FinalStateEstimate(mat U_ans, mat Y_ans, mat MeshGrid, double t0_ans, double tf_ans, uvec N, char *method, vec time_ans,
  416. int interval_num, mat Ts1_Te1_ans, auxdata1 auxdata, int length_state, int length_control, char *interp_method,
  417. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path),
  418. mat* FSE);
  419. // 结果评价与网格细化策略
  420. int result_assess(arma::mat* MeshGrid_nextstep, arma::mat* Ts1_Te1_nextstep, error0* error, arma::mat* inum_interval_increase,
  421. arma::mat U_ans, arma::mat Y_ans, arma::mat MeshGrid, arma::mat time1_ans, double t0_ans, double tf_ans,
  422. arma::uvec N, mesh1 mesh, arma::mat Col_Points, const char* method,
  423. unsigned int interval_num, arma::mat Ts1_Te1_ans, auxdata1 auxdata,
  424. unsigned int length_state, unsigned length_control, int niu, int print_level, int testmode, double CC,
  425. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path));
  426. mat Mesh_choose(mesh1 mesh, arma::mat Col_Points, arma::uvec N); // 根据阶数进行网格信息的选择
  427. arma::field<arma::mat> D_pro1(arma::mat MeshGrid, arma::uword interval_num);// 更新阶段内的左动力学微分矩阵D
  428. arma::field<arma::mat> U_pro(arma::mat MeshGridnew, arma::mat MeshGrid, arma::mat U_old, arma::uvec Nk, arma::uword inter_num, unsigned length_control, int deta_N); // 更新控制量矩阵U
  429. arma::field<arma::mat> Y_pro(arma::mat MeshGridnew, arma::mat MeshGrid, arma::mat Y_old, arma::uvec Nk, arma::uword inter_num, unsigned length_state, int deta_N); // 更新状态量矩阵Y
  430. arma::field<arma::mat> Integral_Matrix(arma::field<arma::mat> D_phase_ans, arma::uword interval_num); // 计算拉格朗日隐式积分矩阵
  431. field<mat> First_deriv_approximation(field<mat> U_phase_ans, field<mat> Y_phase_ans, uword interval_num, auxdata1 auxdata,
  432. int(*Dynamics_ptr)(mat* StateVar, mat* ControlVar, auxdata1 auxdata, mat* dState, mat* integrand, mat* path));
  433. // 新增函数部分
  434. int Get_Rou_Mach(vec H, vec V, vec* Rou, vec* Ma);
  435. int alpha2LD(vec alpha_rad, vec Rou, vec Ma, vec V, auxdata1 auxdata, vec* L, vec* D);
  436. #endif
  437. #pragma once