int fmi2_test(fmi_import_context_t* context, const char* dirPath) { fmi2_callback_functions_t callBackFunctions; const char* modelIdentifier; const char* modelName; const char* GUID; jm_status_enu_t status; fmi2_import_t* fmu; fmi2_fmu_kind_enu_t fmukind; callBackFunctions.logger = fmi2logger; callBackFunctions.allocateMemory = calloc; callBackFunctions.freeMemory = free; callBackFunctions.stepFinished = stepFinished; callBackFunctions.componentEnvironment = 0; fmu = fmi2_import_parse_xml(context, dirPath, 0); if(!fmu) { printf("Error parsing XML, exiting\n"); return (CTEST_RETURN_FAIL); } modelName = fmi2_import_get_model_name(fmu); GUID = fmi2_import_get_GUID(fmu); printf("Model name: %s\n", modelName); if(fmi2_import_get_fmu_kind(fmu) != fmi2_fmu_kind_cs) { modelIdentifier = fmi2_import_get_model_identifier_ME(fmu); printf("Model identifier for ME: %s\n", modelIdentifier); fmukind = fmi2_fmu_kind_me; } else if(fmi2_import_get_fmu_kind(fmu) != fmi2_fmu_kind_me) { modelIdentifier = fmi2_import_get_model_identifier_CS(fmu); printf("Model identifier for CS: %s\n", modelIdentifier); fmukind = fmi2_fmu_kind_cs; } else { printf("Unxepected FMU kind, exiting\n"); return (CTEST_RETURN_FAIL); } printf("Model GUID: %s\n", GUID); status = fmi2_import_create_dllfmu(fmu, fmukind, &callBackFunctions); if (status == jm_status_error) { printf("Could not create the DLL loading mechanism(C-API).\n"); return(CTEST_RETURN_FAIL); } printf("Version returned from FMU: %s\n", fmi2_import_get_version(fmu)); fmi2_import_destroy_dllfmu(fmu); fmi2_import_free(fmu); return (CTEST_RETURN_SUCCESS); }
void* FMI2ModelExchangeConstructor_OMC(int fmi_log_level, char* working_directory, char* instanceName, int debugLogging) { FMI2ModelExchange* FMI2ME = malloc(sizeof(FMI2ModelExchange)); 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); /* FMI callback functions */ FMI2ME->FMICallbackFunctions.logger = fmi2logger; FMI2ME->FMICallbackFunctions.allocateMemory = calloc; FMI2ME->FMICallbackFunctions.freeMemory = free; /* 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) { fprintf(stderr, "Error parsing the XML file contained in %s\n", FMI2ME->FMIWorkingDirectory); return 0; } /* Load the binary (dll/so) */ jm_status_enu_t status; status = fmi2_import_create_dllfmu(FMI2ME->FMIImportInstance, fmi2_import_get_fmu_kind(FMI2ME->FMIImportInstance), &FMI2ME->FMICallbackFunctions); if (status == jm_status_error) { fprintf(stderr, "Could not create the DLL loading mechanism(C-API).\n"); return 0; } FMI2ME->FMIInstanceName = (char*) malloc(strlen(instanceName)+1); strcpy(FMI2ME->FMIInstanceName, instanceName); FMI2ME->FMIDebugLogging = debugLogging; fmi2_import_instantiate_model(FMI2ME->FMIImportInstance, FMI2ME->FMIInstanceName, NULL, fmi2_false); fmi2_import_set_debug_logging(FMI2ME->FMIImportInstance, FMI2ME->FMIDebugLogging, 0, NULL); FMI2ME->FMIToleranceControlled = fmi2_true; FMI2ME->FMIRelativeTolerance = 0.001; FMI2ME->FMIEventInfo = malloc(sizeof(fmi2_event_info_t)); fmi2_import_initialize_model(FMI2ME->FMIImportInstance, FMI2ME->FMIToleranceControlled, FMI2ME->FMIRelativeTolerance, FMI2ME->FMIEventInfo); return FMI2ME; }
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[]) { 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; }