main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. ============================================================================
  3. Name : pw_case.c
  4. Author : Joachim Denil
  5. Version :
  6. Copyright : Your copyright notice
  7. ============================================================================
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "fmi2.h"
  12. #include "sim_support.h"
  13. #define START_TIME 0.0
  14. #define STOP_TIME 3.0
  15. #define STEP_SIZE 0.00001
  16. FMU fmu_env, fmu_control_sa, fmu_power_sa, fmu_loop_sa;
  17. int main(void) {
  18. FILE *fp_fmu_env;
  19. fp_fmu_env = fopen("result_ENV.csv", "w");
  20. FILE *fp_fmu_control_sa;
  21. fp_fmu_control_sa = fopen("result_Control_sa.csv", "w");
  22. FILE *fp_fmu_power_sa;
  23. fp_fmu_power_sa = fopen("result_power_sa.csv","w");
  24. FILE *fp_fmu_loop_sa;
  25. fp_fmu_loop_sa = fopen("result_loop_sa.csv", "w");
  26. puts("Loading Dlls\n");
  27. loadDll("libFMI_Environment.dll", &fmu_env, "PW_ENV");
  28. loadDll("libFMI_controller_sa.dll", &fmu_control_sa, "PW_CONTROLLER_SA");
  29. loadDll("libFMI_power_sa.dll", &fmu_power_sa, "POWER_SA");
  30. loadDll("libFMI_loop_sa.dll", &fmu_loop_sa, "LOOP_SA");
  31. puts("instantiating fmus\n");
  32. fmi2Component c_env, c_control_sa, c_power_sa, c_loop_sa;
  33. char *fmuResourceLocation_env = "libFMI_Environment.dll";
  34. char *fmuResourceLocation_control_sa = "libFMI_controller_sa.dll";
  35. char *fmuResourceLocation_power_sa = "libFMI_power_sa.dll";
  36. char *fmuResourceLocation_window_sa = "libFMI_loop_sa.dll";
  37. fmi2CallbackFunctions callbacks_env= {fmuLogger, calloc, free, NULL, &fmu_env};
  38. fmi2CallbackFunctions callbacks_control_sa = {fmuLogger, calloc, free, NULL, &fmu_control_sa};
  39. fmi2CallbackFunctions callbacks_power_sa = {fmuLogger, calloc, free, NULL, &fmu_power_sa};
  40. fmi2CallbackFunctions callbacks_loop_sa = {fmuLogger, calloc, free, NULL, &fmu_loop_sa};
  41. c_env = fmu_env.instantiate("env", fmi2CoSimulation, "1", fmuResourceLocation_env, &callbacks_env, fmi2False, fmi2False);
  42. c_control_sa = fmu_control_sa.instantiate("control_sa", fmi2CoSimulation, "1", fmuResourceLocation_control_sa, &callbacks_control_sa, fmi2False, fmi2False);
  43. c_power_sa = fmu_power_sa.instantiate("power_sa", fmi2CoSimulation, "1", fmuResourceLocation_power_sa, &callbacks_power_sa, fmi2False, fmi2False);
  44. c_loop_sa = fmu_loop_sa.instantiate("loop_sa",fmi2CoSimulation, "1", fmuResourceLocation_window_sa, &callbacks_loop_sa, fmi2False, fmi2False );
  45. fmi2Boolean toleranceDefined = fmi2False; // true if model description define tolerance
  46. fmi2Real tolerance = 0; // used in setting up the experiment
  47. puts("FMU components instantiated, setting up experiments\n");
  48. fmi2Status fmi2Flag[4];
  49. fmi2Flag[0] = fmu_env.setupExperiment(c_env, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
  50. if (fmi2Flag[0] == fmi2Error){
  51. return -1;
  52. }
  53. fmi2Flag[1] = fmu_control_sa.setupExperiment(c_control_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
  54. if (fmi2Flag[1] == fmi2Error){
  55. return -1;
  56. }
  57. fmi2Flag[2] = fmu_power_sa.setupExperiment(c_power_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
  58. if (fmi2Flag[2] == fmi2Error){
  59. return -1;
  60. }
  61. fmi2Flag[3] = fmu_loop_sa.setupExperiment(c_loop_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
  62. if (fmi2Flag[3] == fmi2Error){
  63. return -1;
  64. }
  65. puts("Experiment setup, entering init mode\n");
  66. fmi2Flag[0] = fmu_env.enterInitializationMode(c_env);
  67. if (fmi2Flag[0] == fmi2Error){
  68. return -1;
  69. }
  70. fmi2Flag[1] = fmu_control_sa.enterInitializationMode(c_control_sa);
  71. if (fmi2Flag[1] == fmi2Error){
  72. return -1;
  73. }
  74. fmi2Flag[2] = fmu_power_sa.enterInitializationMode(c_power_sa);
  75. if (fmi2Flag[2] == fmi2Error){
  76. return -1;
  77. }
  78. fmi2Flag[3] = fmu_loop_sa.enterInitializationMode(c_loop_sa);
  79. if (fmi2Flag[3] == fmi2Error){
  80. return -1;
  81. }
  82. puts("Experiment setup, exiting init mode\n");
  83. fmi2Flag[0] = fmu_env.exitInitializationMode(c_env);
  84. if (fmi2Flag[0] == fmi2Error){
  85. return -1;
  86. }
  87. fmi2Flag[1] = fmu_control_sa.exitInitializationMode(c_control_sa);
  88. if (fmi2Flag[1] == fmi2Error){
  89. return -1;
  90. }
  91. fmi2Flag[2] = fmu_power_sa.exitInitializationMode(c_power_sa);
  92. if (fmi2Flag[2] == fmi2Error){
  93. return -1;
  94. }
  95. fmi2Flag[3] = fmu_loop_sa.exitInitializationMode(c_loop_sa);
  96. if (fmi2Flag[3] == fmi2Error){
  97. return -1;
  98. }
  99. fmi2ValueReference vr_out_env[9]={0,1,2,3,4,5,6,7,8};
  100. fmi2Boolean b_out_env[9];
  101. fmi2ValueReference vr_in_control_sa_from_env[8]={0,1,2,3,4,5,6,7};
  102. fmi2ValueReference vr_in_control_sa_from_window[1] = {0};
  103. fmi2ValueReference vr_out_control_sa[2]={9,10};
  104. fmi2ValueReference vr_in_power_sa_u_d[2] = {3,5};
  105. fmi2ValueReference vr_in_power_sa_tau[1] = {4};
  106. fmi2ValueReference vr_out_loop_sa[1] = {0};
  107. fmi2Real r_in_power_from_control[2] = {0,0};
  108. fmi2Real r_in_power_from_loop[1]={0};
  109. fmi2ValueReference vr_out_power_sa[3] = {0,1,2};
  110. fmi2Real r_out_power[3];
  111. fmi2Real r_out_loop_sa[1];
  112. fmi2Boolean b_out_control_sa[2];
  113. double currentTime = START_TIME;
  114. const fmi2StatusKind lst = fmi2LastSuccessfulTime;
  115. while(currentTime <= STOP_TIME){
  116. printf("\n----master new loop, ct:%f, h:%f\n",currentTime,STEP_SIZE);
  117. double next_step_size = STEP_SIZE;
  118. fmi2Component ctemp_env, ctemp_control_sa;
  119. // Make backup
  120. fmu_env.getFMUstate(c_env,&ctemp_env);
  121. fmu_control_sa.getFMUstate(c_control_sa, &ctemp_control_sa);
  122. // do step
  123. fmi2Flag[2] = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_u_d,2, &r_in_power_from_control[0]);
  124. if(fmi2Flag[1] != fmi2OK){
  125. return 1;
  126. }
  127. fmi2Flag[2] = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_tau,1, &r_in_power_from_loop[0]);
  128. if(fmi2Flag[1] != fmi2OK){
  129. return 1;
  130. }
  131. fmi2Flag[2] = fmu_power_sa.doStep(c_power_sa, currentTime, STEP_SIZE, fmi2True);
  132. if (fmu_power_sa.getReal(c_power_sa, vr_out_power_sa, 3, &r_out_power[0]) != fmi2OK){
  133. return 1;
  134. }
  135. fflush(stdout);
  136. fmi2Flag[0] = fmu_env.doStep(c_env, currentTime, STEP_SIZE, fmi2True);
  137. if (fmu_env.getBoolean(c_env, vr_out_env, 9, &b_out_env[0]) != fmi2OK){
  138. return 1;
  139. }
  140. fprintf(fp_fmu_env,"%f,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
  141. currentTime,
  142. b_out_env[0],
  143. b_out_env[1],
  144. b_out_env[2],
  145. b_out_env[3],
  146. b_out_env[4],
  147. b_out_env[5],
  148. b_out_env[6],
  149. b_out_env[7],
  150. b_out_env[8]);
  151. fmi2Flag[1] = fmu_control_sa.setBoolean(c_control_sa,vr_in_control_sa_from_env,8,&b_out_env[0]);
  152. if(fmi2Flag[1] != fmi2OK){
  153. return 1;
  154. }
  155. fmi2Flag[1] = fmu_control_sa.setReal(c_control_sa,vr_in_control_sa_from_window,1, &r_out_power[0]);
  156. if(fmi2Flag[1] != fmi2OK){
  157. return 1;
  158. }
  159. fflush(stdout);
  160. fmi2Flag[1] = fmu_control_sa.doStep(c_control_sa, currentTime, STEP_SIZE, fmi2True);
  161. if (fmu_control_sa.getBoolean(c_control_sa, vr_out_control_sa, 2, &b_out_control_sa[0]) != fmi2OK){
  162. return 1;
  163. }
  164. fprintf(fp_fmu_control_sa,"%f,%d,%d\n",
  165. currentTime,
  166. b_out_control_sa[0],
  167. b_out_control_sa[1]);
  168. r_in_power_from_control[0] = (int)b_out_control_sa[0];
  169. r_in_power_from_control[1] = (int)b_out_control_sa[1];
  170. fflush(stdout);
  171. fprintf(fp_fmu_power_sa,"%f,%f,%f,%f\n",
  172. currentTime,
  173. r_out_power[0],
  174. r_out_power[1],
  175. r_out_power[2]);
  176. fmi2ValueReference vr_toLoop[2] = {1,2};
  177. fmi2Real toLoop[2];
  178. toLoop[0] = r_out_power[1];
  179. toLoop[1] = r_out_power[2];
  180. if (fmu_loop_sa.setReal(c_loop_sa, vr_toLoop, 2, &toLoop[0]) != fmi2OK){
  181. return 1;
  182. }
  183. fmi2Flag[3] = fmu_loop_sa.doStep(c_loop_sa, currentTime, STEP_SIZE, fmi2True);
  184. fflush(stdout);
  185. if (fmu_loop_sa.getReal(c_loop_sa, vr_out_loop_sa, 1, &r_out_loop_sa[0]) != fmi2OK){
  186. return 1;
  187. }
  188. fprintf(fp_fmu_loop_sa,"%f,%f\n",
  189. currentTime,
  190. r_out_loop_sa[0]
  191. );
  192. r_in_power_from_loop[0] = r_out_loop_sa[0];
  193. //r_in_power_from_loop[0] = -7000;
  194. int redoStep = 0;
  195. for(int i=0; i<2; i++)
  196. if (fmi2Flag[i] == fmi2Discard){
  197. redoStep = 1;
  198. fmi2Real newtime;
  199. fmu_control_sa.getRealStatus(c_control_sa, lst, &newtime);
  200. fmi2Real the_FMU_new_step = newtime - currentTime;
  201. if(the_FMU_new_step < next_step_size){
  202. next_step_size = the_FMU_new_step;
  203. }
  204. }
  205. if(redoStep){ // should be a while loop!
  206. // Recover all FMUs and do step again
  207. printf("recover not yet implemented\n");
  208. }else{
  209. currentTime += STEP_SIZE;
  210. }
  211. fflush(stdout);
  212. }
  213. fclose(fp_fmu_env);
  214. fclose(fp_fmu_power_sa);
  215. fclose(fp_fmu_control_sa);
  216. return EXIT_SUCCESS;
  217. }