int main(int argc, char *argv[]) { const char* fmuFileName = 0; char* fmuPath = 0; char* tmpPath = 0; char* xmlPath = 0; char* dllPath = 0; char* cmd = 0; // define default argument values double tEnd = 1.0; double h=0.1; int loggingOn = 0; char csv_separator = ';'; const char* resultFileName = "-"; // parse command line arguments if (argc>1) { fmuFileName = argv[1]; } else { fprintf(stderr,"error: no fmu file\n"); printHelp(argv[0]); exit(EXIT_FAILURE); } if (argc>2) { if (sscanf(argv[2],"%lf", &tEnd) != 1) { fprintf(stderr,"error: The given end time (%s) is not a number\n", argv[2]); exit(EXIT_FAILURE); } } if (argc>3) { if (sscanf(argv[3],"%lf", &h) != 1) { fprintf(stderr,"error: The given stepsize (%s) is not a number\n", argv[3]); exit(EXIT_FAILURE); } } if (argc>4) { if (sscanf(argv[4],"%d", &loggingOn) != 1 || loggingOn<0 || loggingOn>1) { fprintf(stderr,"error: The given logging option (%s) must be 0 or 1\n", argv[4]); exit(EXIT_FAILURE); } } if (argc>5) { if (strlen(argv[5]) != 1) { fprintf(stderr,"error: The given CSV separator char (%s) is not valid\n", argv[5]); exit(EXIT_FAILURE); } csv_separator = argv[5][0]; } if (argc>6) { resultFileName = argv[6]; if(strlen(resultFileName)==0) resultFileName = 0; // no output } if (argc>7) { fprintf(stderr,"warning: Ignoring %d additional arguments: %s ...\n", argc-6, argv[6]); printHelp(argv[0]); } // get absolute path to FMU, NULL if not found fmuPath = getFmuPath(fmuFileName); if (!fmuPath) exit(EXIT_FAILURE); // unzip the FMU to the tmpPath directory tmpPath = getTmpPath(); if (!fmuUnzip(fmuPath, tmpPath)) exit(EXIT_FAILURE); // parse tmpPath\modelDescription.xml xmlPath = calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1); sprintf(xmlPath, "%s%s", tmpPath, XML_FILE); fmu.modelDescription = parse(xmlPath); free(xmlPath); if (!fmu.modelDescription) exit(EXIT_FAILURE); // load the FMU dll dllPath = calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR) + strlen( getModelIdentifier(fmu.modelDescription)) + strlen(DLL_SUFFIX) + 1); sprintf(dllPath,"%s%s%s%s", tmpPath, DLL_DIR, getModelIdentifier(fmu.modelDescription), DLL_SUFFIX); if (!fmuLoadDll(dllPath, &fmu)) exit(EXIT_FAILURE); free(dllPath); free(fmuPath); // run the simulation fprintf(stderr,"FMU Simulator: run '%s' from t=0..%g with step size h=%g, loggingOn=%d, csv separator='%c'\n", fmuFileName, tEnd, h, loggingOn, csv_separator); if(resultFileName) if(strlen(resultFileName)==1 && resultFileName[0]=='-') fprintf(stderr, "Output will be written to standard output\n"); else fprintf(stderr, "Output will be written to file %s\n", resultFileName); else fprintf(stderr, "No output file will be produced\n"); fmuSimulate(&fmu, tEnd, h, loggingOn, csv_separator, resultFileName); #if WINDOWS /* Remove temp file directory? Not tested*/ cmd = calloc(sizeof(char), strlen(tmpPath)+20); sprintf(cmd, "rmdir %s /s /q", tmpPath); fprintf(stderr,"Removing %s\n", tmpPath); system(cmd); free(cmd); #else cmd = calloc(sizeof(char), strlen(tmpPath)+8); sprintf(cmd, "rm -rf %s", tmpPath); fprintf(stderr,"Removing %s\n", tmpPath); system(cmd); free(cmd); #endif free(tmpPath); // release FMU fmuFree(&fmu); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { const char* fmuFileName; char* fmuPath; char* tmpPath; char* xmlPath; char* dllPath; // define default argument values double tEnd = 1.0; double h=0.1; int loggingOn = 0; char csv_separator = ';'; // parse command line arguments if (argc>1) { fmuFileName = argv[1]; } else { printf("error: no fmu file\n"); printHelp(argv[0]); exit(EXIT_FAILURE); } if (argc>2) { if (sscanf(argv[2],"%lf", &tEnd) != 1) { printf("error: The given end time (%s) is not a number\n", argv[2]); exit(EXIT_FAILURE); } } if (argc>3) { if (sscanf(argv[3],"%lf", &h) != 1) { printf("error: The given stepsize (%s) is not a number\n", argv[3]); exit(EXIT_FAILURE); } } if (argc>4) { if (sscanf(argv[4],"%d", &loggingOn) != 1 || loggingOn<0 || loggingOn>1) { printf("error: The given logging flag (%s) is not boolean\n", argv[4]); exit(EXIT_FAILURE); } } if (argc>5) { if (strlen(argv[5]) != 1) { printf("error: The given CSV separator char (%s) is not valid\n", argv[5]); exit(EXIT_FAILURE); } csv_separator = argv[5][0]; } if (argc>6) { printf("warning: Ignoring %d additional arguments: %s ...\n", argc-6, argv[6]); printHelp(argv[0]); } // get absolute path to FMU, NULL if not found fmuPath = getFmuPath(fmuFileName); if (!fmuPath) exit(EXIT_FAILURE); // unzip the FMU to the tmpPath directory tmpPath = getTmpPath(); if (!unzip(fmuPath, tmpPath)) exit(EXIT_FAILURE); // parse tmpPath\modelDescription.xml xmlPath = calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1); sprintf(xmlPath, "%s%s", tmpPath, XML_FILE); fmu.modelDescription = parse(xmlPath); free(xmlPath); if (!fmu.modelDescription) exit(EXIT_FAILURE); // load the FMU dll dllPath = calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR) + strlen( getModelIdentifier(fmu.modelDescription)) + strlen(".dll") + 1); sprintf(dllPath,"%s%s%s.dll", tmpPath, DLL_DIR, getModelIdentifier(fmu.modelDescription)); if (!loadDll(dllPath, &fmu)) exit(EXIT_FAILURE); free(dllPath); free(fmuPath); free(tmpPath); // run the simulation printf("FMU Simulator: run '%s' from t=0..%g with step size h=%g, loggingOn=%d, csv separator='%c'\n", fmuFileName, tEnd, h, loggingOn, csv_separator); simulate(&fmu, tEnd, h, loggingOn, csv_separator); printf("CSV file '%s' written", RESULT_FILE); // release FMU FreeLibrary(fmu.dllHandle); freeElement(fmu.modelDescription); return EXIT_SUCCESS; }