Skip to content

Commit

Permalink
filterSimulationResults option to remove descriptions
Browse files Browse the repository at this point in the history
The string comments on variables take up a lot of space in the result-
file. This option allows filtering them all away.

Belonging to [master]:
  - OpenModelica/OMCompiler#2428
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed May 14, 2018
1 parent a3a1750 commit 74e8456
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
7 changes: 7 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -2869,11 +2869,18 @@ public function filterSimulationResults
input String outFile;
input String[:] vars;
input Integer numberOfIntervals = 0 "0=Do not resample";
input Boolean removeDescription = false;
output Boolean success;
external "builtin";
annotation(Documentation(info="<html>
<p>Takes one simulation result and filters out the selected variables only, producing the output file.</p>
<p>If numberOfIntervals<>0, re-sample to that number of intervals, ignoring event points (might be changed in the future).</p>
<p>if removeDescription=true, the description matrix will contain 0-length strings, making the file smaller.</p>
</html>",revisions="<html>
<table>
<tr><th>Revision</th><th>Author</th><th>Comment</th></tr>
<tr><td>1.13.0</td><td>sjoelund.se</td><td>Introduced removeDescription.</td></tr>
</table>
</html>"),preferredView="text");
end filterSimulationResults;

Expand Down
4 changes: 2 additions & 2 deletions Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -2272,10 +2272,10 @@ algorithm
case (cache,_,"compareSimulationResults",_,_)
then (cache,Values.STRING("Error in compareSimulationResults"));

case (cache,_,"filterSimulationResults",{Values.STRING(filename),Values.STRING(filename_1),Values.ARRAY(valueLst=cvars),Values.INTEGER(numberOfIntervals)},_)
case (cache,_,"filterSimulationResults",{Values.STRING(filename),Values.STRING(filename_1),Values.ARRAY(valueLst=cvars),Values.INTEGER(numberOfIntervals),Values.BOOL(b)},_)
equation
vars_1 = List.map(cvars, ValuesUtil.extractValueString);
b = SimulationResults.filterSimulationResults(filename,filename_1,vars_1,numberOfIntervals);
b = SimulationResults.filterSimulationResults(filename,filename_1,vars_1,numberOfIntervals,b);
then
(cache,Values.BOOL(b));

Expand Down
3 changes: 2 additions & 1 deletion Compiler/Util/SimulationResults.mo
Expand Up @@ -149,8 +149,9 @@ public function filterSimulationResults
input String outFile;
input list<String> vars;
input Integer numberOfIntervals=0;
input Boolean removeDescription;
output Boolean result;
external "C" result=SimulationResults_filterSimulationResults(inFile,outFile,vars,numberOfIntervals) annotation(Library = "omcruntime");
external "C" result=SimulationResults_filterSimulationResults(inFile,outFile,vars,numberOfIntervals,removeDescription) annotation(Library = "omcruntime");
end filterSimulationResults;

annotation(__OpenModelica_Interface="frontend");
Expand Down
49 changes: 32 additions & 17 deletions Compiler/runtime/SimulationResults.c
Expand Up @@ -392,12 +392,18 @@ static void* SimulationResultsImpl__readDataset(const char *filename, void *vars
}
}

static inline int failedToWriteToFile(const char *file)
static inline int failedToWriteToFileImpl(const char *file, const char *sourceFile, const char *line)
{
c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Failed to write to file %s."), &file, 1);
const char *msg[3] = {file,line,sourceFile};
c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("%s:%s: Failed to write to file %s."), msg, 3);
return 0;
}

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define failedToWriteToFile(file) failedToWriteToFileImpl(file, __FILE__, STR(__LINE__))


static inline int intMax(int a, int b)
{
return a>b ? a : b;
Expand All @@ -412,7 +418,7 @@ static int endsWith(const char *s, const char *suffix)
return 0;
}
}
int SimulationResults_filterSimulationResults(const char *inFile, const char *outFile, void *vars, int numberOfIntervals)
int SimulationResults_filterSimulationResults(const char *inFile, const char *outFile, void *vars, int numberOfIntervals, int removeDescription)
{
const char *msg[5] = {"","","","",""};
void *tmp;
Expand Down Expand Up @@ -545,21 +551,27 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
}
GC_free(tmp);

if (writeMatVer4MatrixHeader(fout, "description", numToFilter, longestDesc, sizeof(int8_t))) {
return failedToWriteToFile(outFile);
}
if (removeDescription) {
if (writeMatVer4MatrixHeader(fout, "description", numToFilter, 0, sizeof(int8_t))) {
return failedToWriteToFile(outFile);
}
} else {
if (writeMatVer4MatrixHeader(fout, "description", numToFilter, longestDesc, sizeof(int8_t))) {
return failedToWriteToFile(outFile);
}

tmp = omc_alloc_interface.malloc(numToFilter*longestDesc);
for (i=0; i<numToFilter; i++) {
int len = strlen(mat_var[i]->descr);
for (j=0; j<len; j++) {
tmp[numToFilter*j+i] = mat_var[i]->descr[j];
tmp = omc_alloc_interface.malloc(numToFilter*longestDesc);
for (i=0; i<numToFilter; i++) {
int len = strlen(mat_var[i]->descr);
for (j=0; j<len; j++) {
tmp[numToFilter*j+i] = mat_var[i]->descr[j];
}
}
if (1 != fwrite(tmp, numToFilter*longestDesc, 1, fout)) {
return failedToWriteToFile(outFile);
}
GC_free(tmp);
}
if (1 != fwrite(tmp, numToFilter*longestDesc, 1, fout)) {
return failedToWriteToFile(outFile);
}
GC_free(tmp);

if (writeMatVer4MatrixHeader(fout, "dataInfo", numToFilter, 4, sizeof(int32_t))) {
return failedToWriteToFile(outFile);
Expand Down Expand Up @@ -660,8 +672,11 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
vals = omc_matlab4_read_vals(&simresglob.matReader, indexesToOutput[i]);
nrows = simresglob.matReader.nrows;
}
if (1!=fwrite(vals, sizeof(double)*nrows, 1, fout)) {
return failedToWriteToFile(outFile);
if (nrows > 0) {
if (1!=fwrite(vals, sizeof(double)*nrows, 1, fout)) {
fprintf(stderr, "nrows=%d\n", nrows);
return failedToWriteToFile(outFile);
}
}
if (numberOfIntervals) {
GC_free(vals);
Expand Down

0 comments on commit 74e8456

Please sign in to comment.