StaticGenerators.xtend 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package be.uantwerpen.ansymo.semanticadaptation.testframework
  2. class StaticGenerators {
  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. def static String generateCMakeLists(String projectName, String frameworkPath)
  118. {
  119. '''
  120. #
  121. # #%~
  122. # The Overture Abstract Syntax Tree
  123. # %%
  124. # Copyright (C) 2017 - 2014 Aarhus University
  125. # %%
  126. # This program is free software: you can redistribute it and/or modify
  127. # it under the terms of the GNU General Public License as
  128. # published by the Free Software Foundation, either version 3 of the
  129. # License, or (at your option) any later version.
  130. #
  131. # This program is distributed in the hope that it will be useful,
  132. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  133. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  134. # GNU General Public License for more details.
  135. #
  136. # You should have received a copy of the GNU General Public
  137. # License along with this program. If not, see
  138. # <http://www.gnu.org/licenses/gpl-3.0.html>.
  139. # #~%
  140. #
  141. cmake_minimum_required (VERSION 3.5.2)
  142. # this (3.5.2) is the first version where External Project uses --recursive
  143. project («projectName» C CXX)
  144. # HOW to
  145. #
  146. # cmake -DCMAKE_TOOLCHAIN_FILE=msys-toolchain.cmake -DCMAKE_BUILD_TYPE=Debug .
  147. # make
  148. #
  149. # to list dependencies use:
  150. # objdump -p binaries/win64/window-sa.dll | grep 'DLL Name:'
  151. #
  152. if (WIN32)
  153. #windows, becuase windows just cannot figure out to do it correct.
  154. # must be a bug in msys mingw gcc 6.3
  155. # it doesnt read the rsp files.
  156. set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 0)
  157. set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 0)
  158. set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
  159. set(CMAKE_C_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
  160. endif()
  161. # Checkout the hcf sources
  162. set(HCF_DIR hcf)
  163. if(EXISTS ${HCF_DIR})
  164. #execute_process(COMMAND git pull WORKING_DIRECTORY ${HCF_DIR})
  165. else()
  166. execute_process(COMMAND git clone --depth 1 git@github.com:into-cps/hybridCosimulation-framework.git ${HCF_DIR} )
  167. execute_process(COMMAND git submodule update --init --recursive WORKING_DIRECTORY ${HCF_DIR})
  168. endif()
  169. add_subdirectory(${HCF_DIR}/semantic-adaptation)
  170. include(CheckCXXCompilerFlag)
  171. CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
  172. CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
  173. if(COMPILER_SUPPORTS_CXX11)
  174. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  175. elseif(COMPILER_SUPPORTS_CXX0X)
  176. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
  177. else()
  178. message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
  179. endif()
  180. add_definitions(-DFMI_COSIMULATION)
  181. include_directories(sources)
  182. file(GLOB CPP_FILES sources/*.cpp)
  183. file(GLOB CPP_MAIN_FILES sources/main*.cpp)
  184. list(REMOVE_ITEM CPP_FILES ${CPP_MAIN_FILES})
  185. ## library ##
  186. #determine the library path
  187. set(FMI_BIN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/binaries")
  188. if(WIN32 OR MINGW)
  189. set(FMI_BIN_DIR "${FMI_BIN_DIR}/win")
  190. # not sure why this doesnt work on MSYS so we just reset it
  191. SET (CMAKE_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
  192. endif()
  193. if(APPLE)
  194. set(FMI_BIN_DIR "${FMI_BIN_DIR}/darwin")
  195. endif()
  196. if(UNIX AND NOT APPLE)
  197. # for Linux, BSD, Solaris, Minix
  198. set(FMI_BIN_DIR "${FMI_BIN_DIR}/linux")
  199. endif()
  200. if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
  201. set(FMI_BIN_DIR "${FMI_BIN_DIR}64")
  202. else ()
  203. set(FMI_BIN_DIR "${FMI_BIN_DIR}32")
  204. endif ()
  205. message("FMI output is ${FMI_BIN_DIR}")
  206. #SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
  207. file(MAKE_DIRECTORY ${FMI_BIN_DIR})
  208. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${FMI_BIN_DIR})
  209. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${FMI_BIN_DIR})
  210. #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY bb)
  211. #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${FMI_BIN_DIR})
  212. add_library(${PROJECT_NAME} SHARED ${CPP_FILES} ${CPP_FILES})
  213. target_link_libraries(${PROJECT_NAME} hcf)
  214. set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
  215. SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
  216. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
  217. ## Main executable ##
  218. add_executable(${PROJECT_NAME}_main ${CPP_MAIN_FILES} ${CPP_FILES})
  219. target_link_libraries(${PROJECT_NAME}_main hcf)
  220. # I need this: -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic
  221. if (WIN32)
  222. #windows
  223. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
  224. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
  225. endif()
  226. '''
  227. }
  228. }