void VisualizerFMU::allocateContext(const std::string& modelFile, const std::string& path) { // First we need to define the callbacks and set up the context. mCallbacks.malloc = malloc; mCallbacks.calloc = calloc; mCallbacks.realloc = realloc; mCallbacks.free = free; mCallbacks.logger = jm_default_logger; mCallbacks.log_level = jm_log_level_debug; // jm_log_level_error; mCallbacks.context = 0; #ifdef FMILIB_GENERATE_BUILD_STAMP std::cout << "Library build stamp: \n" << fmilib_get_build_stamp() << std::endl; #endif mpContext = std::shared_ptr<fmi_import_context_t>(fmi_import_allocate_context(&mCallbacks), fmi_import_free_context); //get version std::string fmuFileName = path + modelFile; mVersion = fmi_import_get_fmi_version(mpContext.get(), fmuFileName.c_str(), path.c_str()); }
void* FMI1ModelExchangeConstructor_OMC(int fmi_log_level, char* working_directory, char* instanceName, int debugLogging) { FMI1ModelExchange* FMI1ME = malloc(sizeof(FMI1ModelExchange)); FMI1ME->FMILogLevel = fmi_log_level; /* JM callbacks */ FMI1ME->JMCallbacks.malloc = malloc; FMI1ME->JMCallbacks.calloc = calloc; FMI1ME->JMCallbacks.realloc = realloc; FMI1ME->JMCallbacks.free = free; FMI1ME->JMCallbacks.logger = importlogger; FMI1ME->JMCallbacks.log_level = FMI1ME->FMILogLevel; FMI1ME->JMCallbacks.context = 0; FMI1ME->FMIImportContext = fmi_import_allocate_context(&FMI1ME->JMCallbacks); /* FMI callback functions */ FMI1ME->FMICallbackFunctions.logger = fmi1logger; FMI1ME->FMICallbackFunctions.allocateMemory = calloc; FMI1ME->FMICallbackFunctions.freeMemory = free; /* parse the xml file */ FMI1ME->FMIWorkingDirectory = (char*) malloc(strlen(working_directory)+1); strcpy(FMI1ME->FMIWorkingDirectory, working_directory); FMI1ME->FMIImportInstance = fmi1_import_parse_xml(FMI1ME->FMIImportContext, FMI1ME->FMIWorkingDirectory); if(!FMI1ME->FMIImportInstance) { fprintf(stderr, "Error parsing the XML file contained in %s\n", FMI1ME->FMIWorkingDirectory); return 0; } /* Load the binary (dll/so) */ jm_status_enu_t status; status = fmi1_import_create_dllfmu(FMI1ME->FMIImportInstance, FMI1ME->FMICallbackFunctions, 0); if (status == jm_status_error) { fprintf(stderr, "Could not create the DLL loading mechanism(C-API).\n"); return 0; } FMI1ME->FMIInstanceName = (char*) malloc(strlen(instanceName)+1); strcpy(FMI1ME->FMIInstanceName, instanceName); FMI1ME->FMIDebugLogging = debugLogging; fmi1_import_instantiate_model(FMI1ME->FMIImportInstance, FMI1ME->FMIInstanceName); fmi1_import_set_debug_logging(FMI1ME->FMIImportInstance, FMI1ME->FMIDebugLogging); FMI1ME->FMIToleranceControlled = fmi1_true; FMI1ME->FMIRelativeTolerance = 0.001; FMI1ME->FMIEventInfo = malloc(sizeof(fmi1_event_info_t)); fmi1_import_initialize(FMI1ME->FMIImportInstance, FMI1ME->FMIToleranceControlled, FMI1ME->FMIRelativeTolerance, FMI1ME->FMIEventInfo); return FMI1ME; }
void* FMI2ModelExchangeConstructor_OMC(int fmi_log_level, char* working_directory, char* instanceName, int debugLogging) { FMI2ModelExchange* FMI2ME = malloc(sizeof(FMI2ModelExchange)); jm_status_enu_t status, instantiateModelStatus; FMI2ME->FMILogLevel = fmi_log_level; /* JM callbacks */ FMI2ME->JMCallbacks.malloc = malloc; FMI2ME->JMCallbacks.calloc = calloc; FMI2ME->JMCallbacks.realloc = realloc; FMI2ME->JMCallbacks.free = free; FMI2ME->JMCallbacks.logger = importlogger; FMI2ME->JMCallbacks.log_level = FMI2ME->FMILogLevel; FMI2ME->JMCallbacks.context = 0; FMI2ME->FMIImportContext = fmi_import_allocate_context(&FMI2ME->JMCallbacks); /* parse the xml file */ FMI2ME->FMIWorkingDirectory = (char*) malloc(strlen(working_directory)+1); strcpy(FMI2ME->FMIWorkingDirectory, working_directory); FMI2ME->FMIImportInstance = fmi2_import_parse_xml(FMI2ME->FMIImportContext, FMI2ME->FMIWorkingDirectory, NULL); if(!FMI2ME->FMIImportInstance) { FMI2ME->FMISolvingMode = fmi2_none_mode; ModelicaFormatError("Error parsing the XML file contained in %s\n", FMI2ME->FMIWorkingDirectory); return 0; } /* FMI callback functions */ FMI2ME->FMICallbackFunctions.logger = fmi2logger; FMI2ME->FMICallbackFunctions.allocateMemory = calloc; FMI2ME->FMICallbackFunctions.freeMemory = free; FMI2ME->FMICallbackFunctions.componentEnvironment = FMI2ME->FMIImportInstance; /* Load the binary (dll/so) */ status = fmi2_import_create_dllfmu(FMI2ME->FMIImportInstance, fmi2_import_get_fmu_kind(FMI2ME->FMIImportInstance), &FMI2ME->FMICallbackFunctions); if (status == jm_status_error) { FMI2ME->FMISolvingMode = fmi2_none_mode; ModelicaFormatError("Loading of FMU dynamic link library failed with status : %s\n", jm_log_level_to_string(status)); return 0; } FMI2ME->FMIInstanceName = (char*) malloc(strlen(instanceName)+1); strcpy(FMI2ME->FMIInstanceName, instanceName); FMI2ME->FMIDebugLogging = debugLogging; instantiateModelStatus = fmi2_import_instantiate(FMI2ME->FMIImportInstance, FMI2ME->FMIInstanceName, fmi2_model_exchange, NULL, fmi2_false); if (instantiateModelStatus == jm_status_error) { FMI2ME->FMISolvingMode = fmi2_none_mode; ModelicaFormatError("fmi2InstantiateModel failed with status : %s\n", jm_log_level_to_string(instantiateModelStatus)); return 0; } /* Only call fmi2SetDebugLogging if debugLogging is true */ if (FMI2ME->FMIDebugLogging) { int i; size_t categoriesSize = 0; fmi2_status_t debugLoggingStatus; fmi2_string_t *categories; /* Read the log categories size */ categoriesSize = fmi2_import_get_log_categories_num(FMI2ME->FMIImportInstance); categories = (fmi2_string_t*)malloc(categoriesSize*sizeof(fmi2_string_t)); for (i = 0 ; i < categoriesSize ; i++) { categories[i] = fmi2_import_get_log_category(FMI2ME->FMIImportInstance, i); } debugLoggingStatus = fmi2_import_set_debug_logging(FMI2ME->FMIImportInstance, FMI2ME->FMIDebugLogging, categoriesSize, categories); if (debugLoggingStatus != fmi2_status_ok && debugLoggingStatus != fmi2_status_warning) { ModelicaFormatMessage("fmi2SetDebugLogging failed with status : %s\n", fmi1_status_to_string(debugLoggingStatus)); } } FMI2ME->FMIToleranceControlled = fmi2_true; FMI2ME->FMIRelativeTolerance = 0.001; FMI2ME->FMIEventInfo = malloc(sizeof(fmi2_event_info_t)); FMI2ME->FMISolvingMode = fmi2_instantiated_mode; return FMI2ME; }
int main(int argc, char *argv[]) { fmi1_callback_functions_t callBackFunctions; const char* FMUPath; const char* tmpPath; jm_callbacks callbacks; fmi_import_context_t* context; fmi_version_enu_t version; jm_status_enu_t status; int register_active_fmu; fmi1_import_t* fmu; char* outfile; if(argc < 4) { printf("Usage: %s <fmu_file> <temporary_dir> <output_file>\n", argv[0]); do_exit(CTEST_RETURN_FAIL); } FMUPath = argv[1]; tmpPath = argv[2]; outfile = argv[3]; logFile = fopen(outfile, "wb"); if(!logFile) { printf("Could not open output file %s\n", outfile); do_exit(CTEST_RETURN_FAIL); } callbacks.malloc = malloc; callbacks.calloc = calloc; callbacks.realloc = realloc; callbacks.free = free; callbacks.logger = logger; callbacks.log_level = jm_log_level_info; callbacks.context = 0; callBackFunctions.logger = fmi1_log_forwarding; callBackFunctions.allocateMemory = calloc; callBackFunctions.freeMemory = free; #ifdef FMILIB_GENERATE_BUILD_STAMP printf("Library build stamp:\n%s\n", fmilib_get_build_stamp()); #endif context = fmi_import_allocate_context(&callbacks); version = fmi_import_get_fmi_version(context, FMUPath, tmpPath); if(version != fmi_version_1_enu) { printf("Only version 1.0 is supported so far\n"); do_exit(CTEST_RETURN_FAIL); } fmu = fmi1_import_parse_xml(context, tmpPath); if(!fmu) { printf("Error parsing XML, exiting\n"); do_exit(CTEST_RETURN_FAIL); } register_active_fmu = 1; /* Must be used to use our logger. (jm standard prints to strerr which does not generate out put test file) */ status = fmi1_import_create_dllfmu(fmu, callBackFunctions, register_active_fmu); if (status == jm_status_error) { printf("Could not create the DLL loading mechanism(C-API test).\n"); do_exit(CTEST_RETURN_FAIL); } test_logger(fmu); fmi1_import_destroy_dllfmu(fmu); fmi1_import_free(fmu); fmi_import_free_context(context); fclose(logFile); return 0; }
int main(int argc, char *argv[]) { fmi2_callback_functions_t callBackFunctions; const char* FMUPath; const char* tmpPath; jm_callbacks callbacks; fmi_import_context_t* context; fmi_version_enu_t version; jm_status_enu_t status; fmi2_import_t* fmu; if(argc < 3) { printf("Usage: %s <fmu_file> <temporary_dir>\n", argv[0]); do_exit(CTEST_RETURN_FAIL); } FMUPath = argv[1]; tmpPath = argv[2]; callbacks.malloc = malloc; callbacks.calloc = calloc; callbacks.realloc = realloc; callbacks.free = free; callbacks.logger = jm_default_logger; callbacks.log_level = jm_log_level_debug; callbacks.context = 0; #ifdef FMILIB_GENERATE_BUILD_STAMP printf("Library build stamp:\n%s\n", fmilib_get_build_stamp()); #endif context = fmi_import_allocate_context(&callbacks); version = fmi_import_get_fmi_version(context, FMUPath, tmpPath); if(version != fmi_version_2_0_enu) { printf("Only version 2.0 is supported by this code\n"); do_exit(CTEST_RETURN_FAIL); } fmu = fmi2_import_parse_xml(context, tmpPath,0); if(!fmu) { printf("Error parsing XML, exiting\n"); do_exit(CTEST_RETURN_FAIL); } if(fmi2_import_get_fmu_kind(fmu) == fmi2_fmu_kind_cs) { printf("Only ME 2.0 is supported by this code\n"); do_exit(CTEST_RETURN_FAIL); } callBackFunctions.logger = fmi2_log_forwarding; callBackFunctions.allocateMemory = calloc; callBackFunctions.freeMemory = free; callBackFunctions.componentEnvironment = fmu; status = fmi2_import_create_dllfmu(fmu, fmi2_fmu_kind_me, &callBackFunctions); if (status == jm_status_error) { printf("Could not create the DLL loading mechanism(C-API test).\n"); do_exit(CTEST_RETURN_FAIL); } test_simulate_me(fmu); fmi2_import_destroy_dllfmu(fmu); fmi2_import_free(fmu); fmi_import_free_context(context); printf("Everything seems to be OK since you got this far=)!\n"); do_exit(CTEST_RETURN_SUCCESS); return 0; }