static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, int didEventStep) { if(mt->fmtReal) { int i, flag=1; double tmpdbl; unsigned int tmpint; int total = data->modelData->modelDataXml.nFunctions + data->modelData->modelDataXml.nProfileBlocks; rt_tick(SIM_TIMER_OVERHEAD); rt_accumulate(SIM_TIMER_STEP); /* Disable time measurements if we have trouble writing to the file... */ flag = flag && 1 == fwrite(&mt->stepNo, sizeof(unsigned int), 1, mt->fmtInt); mt->stepNo++; flag = flag && 1 == fwrite(&(data->localData[0]->timeValue), sizeof(double), 1, mt->fmtReal); tmpdbl = rt_accumulated(SIM_TIMER_STEP); flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, mt->fmtReal); flag = flag && total == fwrite(rt_ncall_arr(SIM_TIMER_FIRST_FUNCTION), sizeof(uint32_t), total, mt->fmtInt); for(i=0; i<data->modelData->modelDataXml.nFunctions + data->modelData->modelDataXml.nProfileBlocks; i++) { tmpdbl = rt_accumulated(i + SIM_TIMER_FIRST_FUNCTION); flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, mt->fmtReal); } rt_accumulate(SIM_TIMER_OVERHEAD); if(!flag) { warningStreamPrint(LOG_SOLVER, 0, "Disabled time measurements because the output file could not be generated: %s", strerror(errno)); fclose(mt->fmtInt); fclose(mt->fmtReal); mt->fmtInt = NULL; mt->fmtReal = NULL; } } /* prevent emit if noEventEmit flag is used, if it's an event */ if ((omc_flag[FLAG_NOEVENTEMIT] && didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT]) { sim_result.emit(&sim_result, data, threadData); } #if !defined(OMC_MINIMAL_RUNTIME) embedded_server_update(data->embeddedServerState, data->localData[0]->timeValue); if (data->real_time_sync.enabled) { double time = data->localData[0]->timeValue; int64_t res = rt_ext_tp_sync_nanosec(&data->real_time_sync.clock, (uint64_t) (data->real_time_sync.scaling*(time-data->real_time_sync.time)*1e9)); int64_t maxLateNano = data->simulationInfo->stepSize*1e9*0.1*data->real_time_sync.scaling /* Maximum late time: 10% of step size */; if (res > maxLateNano) { int t=0,tMaxLate=0; const char *unit = prettyPrintNanoSec(res, &t); const char *unit2 = prettyPrintNanoSec(maxLateNano, &tMaxLate); errorStreamPrint(LOG_RT, 0, "Missed deadline at time %g; delta was %d %s (maxLate=%d %s)", time, t, unit, tMaxLate, unit2); } if (res > data->real_time_sync.maxLate) { data->real_time_sync.maxLate = res; } } printAllVarsDebug(data, 0, LOG_DEBUG); /* ??? */ #endif }
void csv_emit(simulation_result *self, DATA *data) { FILE *fout = (FILE*) self->storage; const char* format = "%.16g,"; const char* formatint = "%i,"; const char* formatbool = "%i,"; const char* formatstring = "\"%s\","; int i; modelica_real value = 0; double cpuTimeValue = 0; rt_tick(SIM_TIMER_OUTPUT); rt_accumulate(SIM_TIMER_TOTAL); cpuTimeValue = rt_accumulated(SIM_TIMER_TOTAL); rt_tick(SIM_TIMER_TOTAL); fprintf(fout, format, data->localData[0]->timeValue); if(self->cpuTime) fprintf(fout, format, cpuTimeValue); for(i = 0; i < data->modelData.nVariablesReal; i++) if(!data->modelData.realVarsData[i].filterOutput) fprintf(fout, format, (data->localData[0])->realVars[i]); for(i = 0; i < data->modelData.nVariablesInteger; i++) if(!data->modelData.integerVarsData[i].filterOutput) fprintf(fout, formatint, (data->localData[0])->integerVars[i]); for(i = 0; i < data->modelData.nVariablesBoolean; i++) if(!data->modelData.booleanVarsData[i].filterOutput) fprintf(fout, formatbool, (data->localData[0])->booleanVars[i]); for(i = 0; i < data->modelData.nVariablesString; i++) if(!data->modelData.stringVarsData[i].filterOutput) fprintf(fout, formatstring, (data->localData[0])->stringVars[i]); for(i = 0; i < data->modelData.nAliasReal; i++) if(!data->modelData.realAlias[i].filterOutput) { if(data->modelData.realAlias[i].aliasType == 2) value = (data->localData[0])->timeValue; else value = (data->localData[0])->realVars[data->modelData.realAlias[i].nameID]; if(data->modelData.realAlias[i].negate) fprintf(fout, format, -value); else fprintf(fout, format, value); } for(i = 0; i < data->modelData.nAliasInteger; i++) if(!data->modelData.integerAlias[i].filterOutput) { if(data->modelData.integerAlias[i].negate) fprintf(fout, formatint, -(data->localData[0])->integerVars[data->modelData.integerAlias[i].nameID]); else fprintf(fout, formatint, (data->localData[0])->integerVars[data->modelData.integerAlias[i].nameID]); } for(i = 0; i < data->modelData.nAliasBoolean; i++) if(!data->modelData.booleanAlias[i].filterOutput) { if(data->modelData.booleanAlias[i].negate) fprintf(fout, formatbool, (data->localData[0])->booleanVars[data->modelData.booleanAlias[i].nameID]==1?0:1); else fprintf(fout, formatbool, (data->localData[0])->booleanVars[data->modelData.booleanAlias[i].nameID]); } for(i = 0; i < data->modelData.nAliasString; i++) if(!data->modelData.stringAlias[i].filterOutput) { /* there would no negation of a string happen */ fprintf(fout, formatstring, (data->localData[0])->stringVars[data->modelData.stringAlias[i].nameID]); } fprintf(fout, "\n"); rt_accumulate(SIM_TIMER_OUTPUT); }
static void fmtEmitStep(DATA* data, threadData_t *threadData, MEASURE_TIME* mt, int didEventStep) { if(mt->fmtReal) { int i, flag=1; double tmpdbl; unsigned int tmpint; int total = data->modelData.modelDataXml.nFunctions + data->modelData.modelDataXml.nProfileBlocks; rt_tick(SIM_TIMER_OVERHEAD); rt_accumulate(SIM_TIMER_STEP); /* Disable time measurements if we have trouble writing to the file... */ flag = flag && 1 == fwrite(&mt->stepNo, sizeof(unsigned int), 1, mt->fmtInt); mt->stepNo++; flag = flag && 1 == fwrite(&(data->localData[0]->timeValue), sizeof(double), 1, mt->fmtReal); tmpdbl = rt_accumulated(SIM_TIMER_STEP); flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, mt->fmtReal); flag = flag && total == fwrite(rt_ncall_arr(SIM_TIMER_FIRST_FUNCTION), sizeof(uint32_t), total, mt->fmtInt); for(i=0; i<data->modelData.modelDataXml.nFunctions + data->modelData.modelDataXml.nProfileBlocks; i++) { tmpdbl = rt_accumulated(i + SIM_TIMER_FIRST_FUNCTION); flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, mt->fmtReal); } rt_accumulate(SIM_TIMER_OVERHEAD); if(!flag) { warningStreamPrint(LOG_SOLVER, 0, "Disabled time measurements because the output file could not be generated: %s", strerror(errno)); fclose(mt->fmtInt); fclose(mt->fmtReal); mt->fmtInt = NULL; mt->fmtReal = NULL; } } /* prevent emit if noEventEmit flag is used, if it's an event */ if ((omc_flag[FLAG_NOEVENTEMIT] && didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT]) { sim_result.emit(&sim_result, data, threadData); } printAllVarsDebug(data, 0, LOG_DEBUG); /* ??? */ }
static void printEquations(FILE *fout, int n, MODEL_DATA_XML *xml) { int i,j; for(i=0; i<n; i++) { indent(fout,2);fprintf(fout, "<equation id=\"eq%d\" name=\"", modelInfoXmlGetEquation(xml,i).id);printStrXML(fout,modelInfoXmlGetEquation(xml,i).name);fprintf(fout,"\">\n"); indent(fout,4);fprintf(fout, "<refs>\n"); for(j=0; j<modelInfoXmlGetEquation(xml,i).numVar; j++) { indent(fout,6);fprintf(fout, "<ref refid=\"var%d\" />\n", 0 /* modelInfoXmlGetEquation(xml,i).vars[j]->id */); } indent(fout,4);fprintf(fout, "</refs>\n"); indent(fout,4);fprintf(fout, "<calcinfo time=\"%f\" count=\"%lu\"/>\n", rt_accumulated(SIM_TIMER_FIRST_FUNCTION + xml->nFunctions + xml->nProfileBlocks + modelInfoXmlGetEquation(xml,i).id), rt_ncall(SIM_TIMER_FIRST_FUNCTION + xml->nFunctions + xml->nProfileBlocks + modelInfoXmlGetEquation(xml,i).id)); indent(fout,2);fprintf(fout, "</equation>\n"); } }
void mat4_emit(simulation_result *self,DATA *data) { mat_data *matData = (mat_data*) self->storage; double datPoint=0; rt_tick(SIM_TIMER_OUTPUT); rt_accumulate(SIM_TIMER_TOTAL); double cpuTimeValue = rt_accumulated(SIM_TIMER_TOTAL); rt_tick(SIM_TIMER_TOTAL); /* this is done wrong -- a buffering should be used although ofstream does have some buffering, but it is not enough and not for this purpose */ matData->fp.write((char*)&(data->localData[0]->timeValue), sizeof(double)); if(self->cpuTime) matData->fp.write((char*)&cpuTimeValue, sizeof(double)); for(int i = 0; i < data->modelData.nVariablesReal; i++) if(!data->modelData.realVarsData[i].filterOutput) matData->fp.write((char*)&(data->localData[0]->realVars[i]),sizeof(double)); for(int i = 0; i < data->modelData.nVariablesInteger; i++) if(!data->modelData.integerVarsData[i].filterOutput) { datPoint = (double) data->localData[0]->integerVars[i]; matData->fp.write((char*)&datPoint,sizeof(double)); } for(int i = 0; i < data->modelData.nVariablesBoolean; i++) if(!data->modelData.booleanVarsData[i].filterOutput) { datPoint = (double) data->localData[0]->booleanVars[i]; matData->fp.write((char*)&datPoint,sizeof(double)); } for(int i = 0; i < data->modelData.nAliasBoolean; i++) if(!data->modelData.booleanAlias[i].filterOutput) { if(data->modelData.booleanAlias[i].negate) { datPoint = (double) (data->localData[0]->booleanVars[data->modelData.booleanAlias[i].nameID]==1?0:1); matData->fp.write((char*)&datPoint,sizeof(double)); } } if (!matData->fp) { throwStreamPrint(data->threadData, "Error while writing file %s",self->filename); } ++matData->ntimepoints; rt_accumulate(SIM_TIMER_OUTPUT); }
int printModelInfo(DATA *data, const char *filename, const char *plotfile, const char *plotFormat, const char *method, const char *outputFormat, const char *outputFilename) { static char buf[256]; FILE *fout = fopen(filename, "w"); FILE *plotCommands; time_t t; int i; #if defined(__MINGW32__) || defined(_MSC_VER) || defined(NO_PIPE) plotCommands = fopen(plotfile, "w"); #else plotCommands = popen("gnuplot", "w"); #endif if(!plotCommands) WARNING1(LOG_UTIL, "Plots of profiling data were disabled: %s\n", strerror(errno)); ASSERT2(fout, "Failed to open %s: %s\n", filename, strerror(errno)); if(plotCommands) { fputs("set terminal svg\n", plotCommands); fputs("set nokey\n", plotCommands); fputs("set format y \"%g\"\n", plotCommands); /* The column containing the time spent to calculate each step */ printPlotCommand(plotCommands, plotFormat, "Execution time of global steps", data->modelData.modelFilePrefix, data->modelData.modelDataXml.nFunctions+data->modelData.modelDataXml.nProfileBlocks, -1, 999, ""); } /* The doctype is needed for id() lookup to work properly */ fprintf(fout, "<!DOCTYPE doc [\ <!ELEMENT simulation (modelinfo, variables, functions, equations)>\ <!ATTLIST variable id ID #REQUIRED>\ <!ELEMENT equation (refs)>\ <!ATTLIST equation id ID #REQUIRED>\ <!ELEMENT profileblocks (profileblock*)>\ <!ELEMENT profileblock (refs, ncall, time, maxTime)>\ <!ELEMENT refs (ref*)>\ <!ATTLIST ref refid IDREF #REQUIRED>\ ]>\n"); if(time(&t) < 0) { WARNING1(LOG_UTIL, "time() failed: %s", strerror(errno)); fclose(fout); return 1; } if(!strftime(buf, 250, "%Y-%m-%d %H:%M:%S", localtime(&t))) { WARNING(LOG_UTIL, "strftime() failed"); fclose(fout); return 1; } fprintf(fout, "<simulation>\n"); fprintf(fout, "<modelinfo>\n"); indent(fout, 2); fprintf(fout, "<name>");printStrXML(fout,data->modelData.modelName);fprintf(fout,"</name>\n"); indent(fout, 2); fprintf(fout, "<prefix>");printStrXML(fout,data->modelData.modelFilePrefix);fprintf(fout,"</prefix>\n"); indent(fout, 2); fprintf(fout, "<date>");printStrXML(fout,buf);fprintf(fout,"</date>\n"); indent(fout, 2); fprintf(fout, "<method>");printStrXML(fout,data->simulationInfo.solverMethod);fprintf(fout,"</method>\n"); indent(fout, 2); fprintf(fout, "<outputFormat>");printStrXML(fout,data->simulationInfo.outputFormat);fprintf(fout,"</outputFormat>\n"); indent(fout, 2); fprintf(fout, "<outputFilename>");printStrXML(fout,outputFilename);fprintf(fout,"</outputFilename>\n"); indent(fout, 2); fprintf(fout, "<outputFilesize>%ld</outputFilesize>\n", (long) fileSize(outputFilename)); indent(fout, 2); fprintf(fout, "<overheadTime>%f</overheadTime>\n", rt_accumulated(SIM_TIMER_OVERHEAD)); indent(fout, 2); fprintf(fout, "<preinitTime>%f</preinitTime>\n", rt_accumulated(SIM_TIMER_PREINIT)); indent(fout, 2); fprintf(fout, "<initTime>%f</initTime>\n", rt_accumulated(SIM_TIMER_INIT)); indent(fout, 2); fprintf(fout, "<eventTime>%f</eventTime>\n", rt_accumulated(SIM_TIMER_EVENT)); indent(fout, 2); fprintf(fout, "<outputTime>%f</outputTime>\n", rt_accumulated(SIM_TIMER_OUTPUT)); indent(fout, 2); fprintf(fout, "<linearizeTime>%f</linearizeTime>\n", rt_accumulated(SIM_TIMER_LINEARIZE)); indent(fout, 2); fprintf(fout, "<totalTime>%f</totalTime>\n", rt_accumulated(SIM_TIMER_TOTAL)); indent(fout, 2); fprintf(fout, "<totalStepsTime>%f</totalStepsTime>\n", rt_total(SIM_TIMER_STEP)); indent(fout, 2); fprintf(fout, "<numStep>%d</numStep>\n", (int) rt_ncall_total(SIM_TIMER_STEP)); indent(fout, 2); fprintf(fout, "<maxTime>%.9f</maxTime>\n", rt_max_accumulated(SIM_TIMER_STEP)); fprintf(fout, "</modelinfo>\n"); fprintf(fout, "<modelinfo_ext>\n"); indent(fout, 2); fprintf(fout, "<odeTime>%f</odeTime>\n", rt_accumulated(SIM_TIMER_FUNCTION_ODE)); indent(fout, 2); fprintf(fout, "<odeTimeTicks>%lu</odeTimeTicks>\n", rt_ncall(SIM_TIMER_FUNCTION_ODE)); fprintf(fout, "</modelinfo_ext>\n"); fprintf(fout, "<profilingdataheader>\n"); printProfilingDataHeader(fout, data); fprintf(fout, "</profilingdataheader>\n"); fprintf(fout, "<variables>\n"); for(i=0;i<data->modelData.nVariablesReal;++i){ printVar(fout, 2, &(data->modelData.realVarsData[i].info)); } for(i=0;i<data->modelData.nParametersReal;++i){ printVar(fout, 2, &data->modelData.realParameterData[i].info); } for(i=0;i<data->modelData.nVariablesInteger;++i){ printVar(fout, 2, &data->modelData.integerVarsData[i].info); } for(i=0;i<data->modelData.nParametersInteger;++i){ printVar(fout, 2, &data->modelData.integerParameterData[i].info); } for(i=0;i<data->modelData.nVariablesBoolean;++i){ printVar(fout, 2, &data->modelData.booleanVarsData[i].info); } for(i=0;i<data->modelData.nParametersBoolean;++i){ printVar(fout, 2, &data->modelData.booleanParameterData[i].info); } for(i=0;i<data->modelData.nVariablesString;++i){ printVar(fout, 2, &data->modelData.stringVarsData[i].info); } for(i=0;i<data->modelData.nParametersString;++i){ printVar(fout, 2, &data->modelData.stringParameterData[i].info); } fprintf(fout, "</variables>\n"); fprintf(fout, "<functions>\n"); printFunctions(fout, plotCommands, plotFormat, data->modelData.modelFilePrefix, data); fprintf(fout, "</functions>\n"); fprintf(fout, "<equations>\n"); printEquations(fout, data->modelData.modelDataXml.nEquations, &data->modelData.modelDataXml); fprintf(fout, "</equations>\n"); fprintf(fout, "<profileblocks>\n"); printProfileBlocks(fout, plotCommands, plotFormat, data); fprintf(fout, "</profileblocks>\n"); fprintf(fout, "</simulation>\n"); fclose(fout); if(plotCommands) { const char *omhome = data->simulationInfo.OPENMODELICAHOME; char *buf = NULL; int genHtmlRes; buf = (char*)malloc(230 + 2*strlen(plotfile) + 2*(omhome ? strlen(omhome) : 0)); assert(buf); #if defined(__MINGW32__) || defined(_MSC_VER) || defined(NO_PIPE) if(omhome) { #if defined(__MINGW32__) || defined(_MSC_VER) sprintf(buf, "%s/lib/omc/libexec/gnuplot/binary/gnuplot.exe %s", omhome, plotfile); #else sprintf(buf, "gnuplot %s", plotfile); #endif fclose(plotCommands); if(0 != system(buf)) { WARNING1(LOG_UTIL, "Plot command failed: %s\n", buf); } } #else if(0 != pclose(plotCommands)) { WARNING(LOG_UTIL, "Warning: Plot command failed\n"); } #endif if(omhome) { #if defined(__MINGW32__) || defined(_MSC_VER) char *xsltproc; sprintf(buf, "%s/lib/omc/libexec/xsltproc/xsltproc.exe", omhome); xsltproc = strdup(buf); #else const char *xsltproc = "xsltproc"; #endif sprintf(buf, "%s -o %s_prof.html %s/share/omc/scripts/default_profiling.xsl %s_prof.xml", xsltproc, data->modelData.modelFilePrefix, omhome, data->modelData.modelFilePrefix); #if defined(__MINGW32__) || defined(_MSC_VER) free(xsltproc); #endif genHtmlRes = system(buf); } else { strcpy(buf, "OPENMODELICAHOME missing"); genHtmlRes = 1; } if(genHtmlRes) { WARNING1(LOG_STDOUT, "Failed to generate html version of profiling results: %s\n", buf); } INFO2(LOG_STDOUT, "Time measurements are stored in %s_prof.html (human-readable) and %s_prof.xml (for XSL transforms or more details)", data->modelData.modelFilePrefix, data->modelData.modelFilePrefix); free(buf); } return 0; }
/* * add the values of one step for all variables to the data * array to be able to later store this on file. */ static void add_result(simulation_result *self,DATA *data,double *data_, long *actualPoints) { plt_data *pltData = (plt_data*) self->storage; const DATA *simData = data; int i; double cpuTimeValue = 0; rt_accumulate(SIM_TIMER_TOTAL); cpuTimeValue = rt_accumulated(SIM_TIMER_TOTAL); rt_tick(SIM_TIMER_TOTAL); { data_[pltData->currentPos++] = simData->localData[0]->timeValue; if(self->cpuTime) data_[pltData->currentPos++] = cpuTimeValue; /* .. reals .. */ for(i = 0; i < simData->modelData->nVariablesReal; i++) { if(!simData->modelData->realVarsData[i].filterOutput) { data_[pltData->currentPos++] = simData->localData[0]->realVars[i]; } } /* .. integers .. */ for(i = 0; i < simData->modelData->nVariablesInteger; i++) { if(!simData->modelData->integerVarsData[i].filterOutput) { data_[pltData->currentPos++] = simData->localData[0]->integerVars[i]; } } /* .. booleans .. */ for(i = 0; i < simData->modelData->nVariablesBoolean; i++) { if(!simData->modelData->booleanVarsData[i].filterOutput) { data_[pltData->currentPos++] = simData->localData[0]->booleanVars[i]; } } /* .. alias reals .. */ for(i = 0; i < simData->modelData->nAliasReal; i++) { if(!simData->modelData->realAlias[i].filterOutput) { double value; if(simData->modelData->realAlias[i].aliasType == 2) value = (simData->localData[0])->timeValue; else if(simData->modelData->realAlias[i].aliasType == 1) value = simData->simulationInfo->realParameter[simData->modelData->realAlias[i].nameID]; else value = (simData->localData[0])->realVars[simData->modelData->realAlias[i].nameID]; if(simData->modelData->realAlias[i].negate) data_[pltData->currentPos++] = -value; else data_[pltData->currentPos++] = value; } } /* .. alias integers .. */ for(i = 0; i < simData->modelData->nAliasInteger; i++) { if(!simData->modelData->integerAlias[i].filterOutput) { modelica_integer value; if(simData->modelData->integerAlias[i].aliasType == 1) value = simData->simulationInfo->integerParameter[simData->modelData->realAlias[i].nameID]; else value = (simData->localData[0])->integerVars[simData->modelData->realAlias[i].nameID]; if(simData->modelData->integerAlias[i].negate) data_[pltData->currentPos++] = -value; else data_[pltData->currentPos++] = value; } } /* .. alias booleans .. */ for(i = 0; i < simData->modelData->nAliasBoolean; i++) { if(!simData->modelData->booleanAlias[i].filterOutput) { modelica_boolean value; if(simData->modelData->integerAlias[i].aliasType == 1) value = simData->simulationInfo->booleanParameter[simData->modelData->realAlias[i].nameID]; else value = (simData->localData[0])->booleanVars[simData->modelData->realAlias[i].nameID]; if(simData->modelData->booleanAlias[i].negate) data_[pltData->currentPos++] = value==1?0:1; else data_[pltData->currentPos++] = value; } } } /*cerr << " ... done" << endl; */ (*actualPoints)++; }