]> gerrit.simantics Code Review - simantics/fmil.git/blobdiff - org.simantics.fmil.core/native/FMILibrary/Test/FMI2/fmi2_import_cs_test.c
Add FMILibrary-2.0.3 to org.simantics.fmil.core\native.
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / Test / FMI2 / fmi2_import_cs_test.c
diff --git a/org.simantics.fmil.core/native/FMILibrary/Test/FMI2/fmi2_import_cs_test.c b/org.simantics.fmil.core/native/FMILibrary/Test/FMI2/fmi2_import_cs_test.c
new file mode 100644 (file)
index 0000000..3da837d
--- /dev/null
@@ -0,0 +1,247 @@
+/*\r
+    Copyright (C) 2012 Modelon AB\r
+\r
+    This program is free software: you can redistribute it and/or modify\r
+    it under the terms of the BSD style license.\r
+\r
+    This program is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+    FMILIB_License.txt file for more details.\r
+\r
+    You should have received a copy of the FMILIB_License.txt file\r
+    along with this program. If not, contact Modelon AB <http://www.modelon.com>.\r
+*/\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <stdarg.h>\r
+\r
+#include "config_test.h"\r
+\r
+#include <fmilib.h>\r
+#include <JM/jm_portability.h>\r
+\r
+\r
+#define BUFFER 1000\r
+\r
+void importlogger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message)\r
+{\r
+        printf("module = %s, log level = %s: %s\n", module, jm_log_level_to_string(log_level), message);\r
+}\r
+\r
+/* Logger function used by the FMU internally */\r
+\r
+void fmilogger(fmi2_component_t c, fmi2_string_t instanceName, fmi2_status_t status, fmi2_string_t category, fmi2_string_t message, ...)\r
+{\r
+    /* int len;\r
+       char msg[BUFFER]; */\r
+       va_list argp;   \r
+       va_start(argp, message);\r
+       /* len = jm_vsnprintf(msg, BUFFER, message, argp); */\r
+       fmi2_log_forwarding_v(c, instanceName, status, category, message, argp);\r
+       va_end(argp);\r
+}\r
+\r
+void do_exit(int code)\r
+{\r
+       printf("Press 'Enter' to exit\n");\r
+       /* getchar(); */\r
+       exit(code);\r
+}\r
+\r
+int test_simulate_cs(fmi2_import_t* fmu)\r
+{\r
+       fmi2_status_t fmistatus;\r
+       jm_status_enu_t jmstatus;\r
+\r
+       fmi2_string_t instanceName = "Test CS model instance";\r
+       fmi2_string_t fmuGUID;\r
+       fmi2_string_t fmuLocation = "";\r
+       fmi2_boolean_t visible = fmi2_false;\r
+       fmi2_real_t relativeTol = 1e-4;\r
+/*     fmi2_boolean_t loggingOn = fmi2_true; */\r
+       \r
+       /* fmi2_real_t simulation_results[] = {-0.001878, -1.722275}; */\r
+       fmi2_real_t simulation_results[] = {0.0143633,   -1.62417};\r
+       fmi2_value_reference_t compare_real_variables_vr[] = {0, 1};\r
+       size_t k;\r
+\r
+       fmi2_real_t tstart = 0.0;\r
+       fmi2_real_t tcur = tstart;\r
+       fmi2_real_t hstep = 0.1;\r
+       fmi2_real_t tend = 2.0;\r
+       fmi2_boolean_t StopTimeDefined = fmi2_false;\r
+\r
+       if (sizeof(compare_real_variables_vr)/sizeof(fmi2_value_reference_t) != sizeof(simulation_results)/sizeof(fmi2_real_t)) {\r
+               printf("Number of simulation values and reference values are different\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       printf("Version returned from FMU:   %s\n", fmi2_import_get_version(fmu));\r
+       printf("Platform type returned:      %s\n", fmi2_import_get_types_platform(fmu));\r
+\r
+       fmuGUID = fmi2_import_get_GUID(fmu);\r
+    printf("GUID:      %s\n", fmuGUID);\r
+\r
+\r
+    jmstatus = fmi2_import_instantiate(fmu, instanceName, fmi2_cosimulation, fmuLocation, visible);\r
+       if (jmstatus == jm_status_error) {\r
+               printf("fmi2_import_instantiate failed\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+        fmistatus = fmi2_import_setup_experiment(fmu, fmi2_true,\r
+            relativeTol, tstart, StopTimeDefined, tend);\r
+    if(fmistatus != fmi2_status_ok) {\r
+        printf("fmi2_import_setup_experiment failed\n");\r
+        do_exit(CTEST_RETURN_FAIL);\r
+    }\r
+\r
+        fmistatus = fmi2_import_enter_initialization_mode(fmu);\r
+    if(fmistatus != fmi2_status_ok) {\r
+        printf("fmi2_import_enter_initialization_mode failed\n");\r
+        do_exit(CTEST_RETURN_FAIL);\r
+    }\r
+\r
+        fmistatus = fmi2_import_exit_initialization_mode(fmu);\r
+    if(fmistatus != fmi2_status_ok) {\r
+        printf("fmi2_import_exit_initialization_mode failed\n");\r
+        do_exit(CTEST_RETURN_FAIL);\r
+    }        \r
+\r
+       tcur = tstart;\r
+       printf("%10s %10s\n", "Ball height", "Ball speed");\r
+       while (tcur < tend) {\r
+               fmi2_boolean_t newStep = fmi2_true;\r
+#if 0 /* Prints a real value.. */\r
+               fmi2_real_t rvalue;\r
+               fmi2_value_reference_t vr = 0;\r
+\r
+               fmistatus = fmi2_import_get_real(fmu, &vr, 1, &rvalue);\r
+               printf("rvalue = %f\n", rvalue);\r
+#endif \r
+               fmistatus = fmi2_import_do_step(fmu, tcur, hstep, newStep);\r
+\r
+               for (k = 0; k < sizeof(compare_real_variables_vr)/sizeof(fmi2_value_reference_t); k++) {\r
+                       fmi2_value_reference_t vr = compare_real_variables_vr[k];\r
+                       fmi2_real_t rvalue;\r
+                       fmistatus = fmi2_import_get_real(fmu, &vr, 1, &rvalue);\r
+               }\r
+               {\r
+                       fmi2_real_t val[2];\r
+                       fmi2_import_get_real(fmu, compare_real_variables_vr, 2, val);\r
+                       printf("%10g %10g\n", val[0],val[1]);\r
+               }\r
+\r
+               tcur += hstep;\r
+       }\r
+\r
+       printf("Simulation finished. Checking results\n");\r
+\r
+       /* Validate result */\r
+       for (k = 0; k < sizeof(compare_real_variables_vr)/sizeof(fmi2_value_reference_t); k++) {\r
+               fmi2_value_reference_t vr = compare_real_variables_vr[k];\r
+               fmi2_real_t rvalue;\r
+               fmi2_real_t res;        \r
+               fmistatus = fmi2_import_get_real(fmu, &vr, 1, &rvalue);\r
+               res = rvalue - simulation_results[k];\r
+               res = res > 0 ? res: -res; /* Take abs */\r
+               if (res > 3e-3) {\r
+                       printf("Simulation results is wrong!\n");\r
+                       printf("State [%u]  %g != %g, |res| = %g\n", (unsigned)k, rvalue, simulation_results[k], res);\r
+                       printf("\n");\r
+                       do_exit(CTEST_RETURN_FAIL);\r
+               }\r
+       }\r
+\r
+       fmistatus = fmi2_import_terminate(fmu);\r
+\r
+       fmi2_import_free_instance(fmu);\r
+\r
+       return 0;\r
+}\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+       fmi2_callback_functions_t callBackFunctions;\r
+       const char* FMUPath;\r
+       const char* tmpPath;\r
+       jm_callbacks callbacks;\r
+       fmi_import_context_t* context;\r
+       fmi_version_enu_t version;\r
+       jm_status_enu_t status;\r
+       int k;\r
+\r
+       fmi2_import_t* fmu;     \r
+\r
+       if(argc < 3) {\r
+               printf("Usage: %s <fmu_file> <temporary_dir>\n", argv[0]);\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       } \r
+       for (k = 0; k < argc; k ++)\r
+               printf("argv[%d] = %s\n", k, argv[k]);\r
+\r
+       FMUPath = argv[1];\r
+       tmpPath = argv[2];\r
+\r
+\r
+       callbacks.malloc = malloc;\r
+    callbacks.calloc = calloc;\r
+    callbacks.realloc = realloc;\r
+    callbacks.free = free;\r
+    callbacks.logger = importlogger;\r
+       callbacks.log_level = jm_log_level_debug;\r
+    callbacks.context = 0;\r
+\r
+#ifdef FMILIB_GENERATE_BUILD_STAMP\r
+       printf("Library build stamp:\n%s\n", fmilib_get_build_stamp());\r
+#endif\r
+\r
+       context = fmi_import_allocate_context(&callbacks);\r
+\r
+       version = fmi_import_get_fmi_version(context, FMUPath, tmpPath);\r
+\r
+       if(version != fmi_version_2_0_enu) {\r
+               printf("The code only supports version 2.0\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       fmu = fmi2_import_parse_xml(context, tmpPath, 0);\r
+\r
+       if(!fmu) {\r
+               printf("Error parsing XML, exiting\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+       \r
+       if(fmi2_import_get_fmu_kind(fmu) == fmi2_fmu_kind_me) {\r
+               printf("Only CS 2.0 is supported by this code\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       callBackFunctions.logger = fmi2_log_forwarding;\r
+       callBackFunctions.allocateMemory = calloc;\r
+       callBackFunctions.freeMemory = free;\r
+       callBackFunctions.componentEnvironment = fmu;\r
+\r
+       status = fmi2_import_create_dllfmu(fmu, fmi2_fmu_kind_cs, &callBackFunctions);\r
+       if (status == jm_status_error) {\r
+               printf("Could not create the DLL loading mechanism(C-API) (error: %s).\n", fmi2_import_get_last_error(fmu));\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       test_simulate_cs(fmu);\r
+\r
+       fmi2_import_destroy_dllfmu(fmu);\r
+\r
+       fmi2_import_free(fmu);\r
+       fmi_import_free_context(context);\r
+       \r
+       printf("Everything seems to be OK since you got this far=)!\n");\r
+\r
+       do_exit(CTEST_RETURN_SUCCESS);\r
+\r
+       return 0;\r
+}\r
+\r
+\r