FmuMainTestGenerator.xtend 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package be.uantwerpen.ansymo.semanticadaptation.testframework
  2. class FmuMainTestGenerator {
  3. def static String generateMainCppFile(String folderPath)
  4. {
  5. '''
  6. /*
  7. * main.cpp
  8. *
  9. * Created on: Mar 14, 2017
  10. * Author: kel
  11. */
  12. #include <iostream>
  13. #include "fmi2TypesPlatform.h"
  14. #include "fmi2Functions.h"
  15. #include <string>
  16. #include <memory>
  17. #include <iostream>
  18. #include <cstdio>
  19. #include <stdarg.h> /* va_list, va_start, va_arg, va_end */
  20. #include <fstream>
  21. using namespace std;
  22. void fmuLoggerCache(void *componentEnvironment, fmi2String instanceName,
  23. fmi2Status status, fmi2String category, fmi2String message, ...) {
  24. va_list argp;
  25. // replace C format strings
  26. va_start(argp, message);
  27. size_t size = vsnprintf(nullptr, 0, message, argp) + 1; // Extra space for '\0'
  28. va_end(argp);
  29. va_start(argp, message);
  30. unique_ptr<char[]> buf(new char[size]);
  31. vsnprintf(buf.get(), size, message, argp);
  32. va_end(argp);
  33. string completeMessage = string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
  34. cout << "FROM MAIN: Name: " << instanceName << " Status: " << status << " Category: "
  35. << category << " Msg: " << completeMessage << endl;
  36. }
  37. fmi2Status setReal(fmi2Component comp, fmi2ValueReference id, fmi2Real val) {
  38. const fmi2ValueReference vr[] { id };
  39. size_t nvr = 1;
  40. fmi2Real value[] { val };
  41. return fmi2SetReal(comp, vr, nvr, value);
  42. }
  43. fmi2Real getReal(fmi2Component comp, fmi2ValueReference id) {
  44. const fmi2ValueReference vr[] { id };
  45. size_t nvr = 1;
  46. fmi2Real value[1];
  47. fmi2GetReal(comp, vr, nvr, value);
  48. return value[0];
  49. }
  50. int main(int argc, char *argv[]) {
  51. // setvbuf(stdout, NULL, _IONBF, 0);
  52. // setvbuf(stderr, NULL, _IONBF, 0);
  53. cout << "hello" << endl;
  54. fmi2CallbackFunctions callback = { &fmuLoggerCache, NULL, NULL, NULL, NULL };
  55. fmi2Component comp =
  56. fmi2Instantiate("this system", fmi2CoSimulation, "{1234}",
  57. "«folderPath»",
  58. &callback, fmi2True,
  59. fmi2True);
  60. if (comp == NULL) {
  61. cout << "init failed" << endl;
  62. return -1;
  63. }
  64. if (fmi2SetupExperiment(comp, false, 0.0, 0, true, 100) != fmi2OK) {
  65. printf("Error fmi2SetupExperiment");
  66. return 1;
  67. }
  68. if (fmi2EnterInitializationMode(comp) != fmi2OK) {
  69. printf("Error fmi2SetupExperiment");
  70. return 1;
  71. }
  72. if (fmi2ExitInitializationMode(comp) != fmi2OK) {
  73. printf("Error fmi2SetupExperiment");
  74. return 1;
  75. }
  76. #define ID_Window_SA_IN_reaction_force 0
  77. #define ID_Window_SA_IN_displacement 1
  78. #define ID_Window_SA_IN_speed 2
  79. #define ID_Window_SA_OUT_disp 3
  80. #define ID_Window_SA_OUT_tau 4
  81. std::fstream fs;
  82. fs.open("result.csv",
  83. std::fstream::in | std::fstream::out | std::fstream::trunc);
  84. //cout << "------------ Header ---------" << endl;
  85. fs << "\"time\",\"ID_Window_SA_OUT_disp\",\"ID_Window_SA_OUT_tau\"" << endl;
  86. double time = 0.0;
  87. double stepSize = 0.01;
  88. for (double time = 0.0; time < 10;) {
  89. if (setReal(comp, ID_Window_SA_IN_reaction_force, -1.0) != fmi2OK) {
  90. printf("Error setting real");
  91. return 1;
  92. }
  93. if (setReal(comp, ID_Window_SA_IN_displacement, time) != fmi2OK) {
  94. printf("Error setting real");
  95. return 1;
  96. }
  97. if (setReal(comp, ID_Window_SA_IN_speed, 1.0) != fmi2OK) {
  98. printf("Error setting real");
  99. return 1;
  100. }
  101. if (fmi2DoStep(comp, time, stepSize, false) != fmi2OK) {
  102. printf("Errorin do step");
  103. return 1;
  104. }
  105. time += stepSize;
  106. auto disp = getReal(comp, ID_Window_SA_OUT_disp);
  107. auto tau = getReal(comp, ID_Window_SA_OUT_tau);
  108. cout << "time: " << time << " disp: " << disp << " tau: " << tau
  109. << endl;
  110. fs << time << "," << disp << "," << tau << endl;
  111. }
  112. fs.close();
  113. return 0;
  114. }
  115. '''
  116. }
  117. }