Skip to content

Commit

Permalink
Improve the filtering of results files
Browse files Browse the repository at this point in the history
- Read both "time" and "Time" directly in read_matlab4.c.
- Handle filtering of parameters in filterSimulationResults.
  • Loading branch information
sjoelund committed May 7, 2015
1 parent 06ed6bf commit cbc725d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
1 change: 0 additions & 1 deletion Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -2616,7 +2616,6 @@ public function filterSimulationResults
input String outFile;
input String[:] vars = fill("",0);
output Boolean success;
output String[:] resultFiles;
external "builtin";
annotation(Documentation(info="<html>
<p>Takes one simulation result and filters out the selected variables only, producing the output file.</p>
Expand Down
46 changes: 38 additions & 8 deletions Compiler/runtime/SimulationResults.c
Expand Up @@ -401,11 +401,15 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
int numToFilter = listLength(vars);
int i, j;
int numUnique = 0;
int numUniqueParam = 1;
int longestName = 0;
int longestDesc = 0;
ModelicaMatVariable_t **mat_var = GC_malloc(numToFilter*sizeof(ModelicaMatVariable_t*));
int *indexes = (int*) GC_malloc(simresglob.matReader.nvar*sizeof(int)); /* Need it to be zeros; note that the actual number of indexes is smaller */
int *parameter_indexes = (int*) GC_malloc(simresglob.matReader.nparam*sizeof(int)); /* Need it to be zeros; note that the actual number of indexes is smaller */
int *indexesToOutput = NULL;
int *parameter_indexesToOutput = NULL;
parameter_indexes[0] = 1; /* time */
omc_matlab4_read_all_vals(&simresglob.matReader);
for (i=0; i<numToFilter; i++) {
const char *var = MMC_STRINGDATA(MMC_CAR(vars));
Expand All @@ -417,21 +421,29 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Could not read variable %s in file %s."), msg, 2);
return 0;
}
if (mat_var[i]->isParam) {
if (mat_var[i]->index < 0) {
msg[0] = SystemImpl__basename(inFile);
msg[1] = var;
c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("filterSimulationResults not implemented for parameter variable %s in file %s."), msg, 1);
c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Variable has negative index %s in file %s."), msg, 2);
return 0;
}
/* Store the old index in the array */
if (0==indexes[mat_var[i]->index-1]++) {
numUnique++;
if (mat_var[i]->isParam) {
/* Store the old index in the array */
if (0==parameter_indexes[mat_var[i]->index-1]++) {
numUniqueParam++;
}
} else {
/* Store the old index in the array */
if (0==indexes[mat_var[i]->index-1]++) {
numUnique++;
}
}
longestName = intMax(longestName, strlen(mat_var[i]->name));
longestDesc = intMax(longestDesc, strlen(mat_var[i]->descr));
}
/* Create the list of variable indexes to output */
indexesToOutput = GC_malloc_atomic(numUnique * sizeof(int));
parameter_indexesToOutput = GC_malloc_atomic(numUniqueParam * sizeof(int));
j=0;
for (i=0; i<simresglob.matReader.nvar; i++) {
if (indexes[i]) {
Expand All @@ -440,6 +452,14 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
/* indexes becomes the lookup table from old index to new index */
indexes[i] = j;
}
j=0;
for (i=0; i<simresglob.matReader.nparam; i++) {
if (parameter_indexes[i]) {
parameter_indexesToOutput[j++] = i+1;
}
/* indexes becomes the lookup table from old index to new index */
parameter_indexes[i] = j;
}
FILE *fout = fopen(outFile, "wb");
if (fout == NULL) {
return failedToWriteToFile(outFile);
Expand Down Expand Up @@ -485,13 +505,13 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
return failedToWriteToFile(outFile);
}
for (i=0; i<numToFilter; i++) {
int32_t x = 2; /* data_2 */
int32_t x = mat_var[i]->isParam ? 1 : 2; /* data_1 or data_2 */
if (1 != fwrite(&x, sizeof(int32_t), 1, fout)) {
return failedToWriteToFile(outFile);
}
}
for (i=0; i<numToFilter; i++) {
int32_t x = indexes[indexesToOutput[i]-1];
int32_t x = (mat_var[i]->index < 0 ? -1 : 1) * (mat_var[i]->isParam ? parameter_indexes[abs(mat_var[i]->index)-1] : indexes[abs(mat_var[i]->index)-1]);
if (1 != fwrite(&x, sizeof(int32_t), 1, fout)) {
return failedToWriteToFile(outFile);
}
Expand All @@ -509,14 +529,24 @@ int SimulationResults_filterSimulationResults(const char *inFile, const char *ou
}
}

if (writeMatVer4MatrixHeader(fout, "data_1", 2, 1, sizeof(double))) {
if (writeMatVer4MatrixHeader(fout, "data_1", 2, numUniqueParam, sizeof(double))) {
return failedToWriteToFile(outFile);
}
double start_stop[2] = {omc_matlab4_startTime(&simresglob.matReader), omc_matlab4_stopTime(&simresglob.matReader)};

if (1 != fwrite(start_stop, sizeof(double)*2, 1, fout)) {
return failedToWriteToFile(outFile);
}

for (i=1; i<numUniqueParam; i++) {
int paramIndex = parameter_indexesToOutput[i];
double d[2] = {simresglob.matReader.params[abs(paramIndex)-1],0};
d[1] = d[0];
if (1!=fwrite(d, sizeof(double)*2, 1, fout)) {
return failedToWriteToFile(outFile);
}
}

if (writeMatVer4MatrixHeader(fout, "data_2", simresglob.matReader.nrows, numUnique, sizeof(double))) {
return failedToWriteToFile(outFile);
}
Expand Down
10 changes: 9 additions & 1 deletion SimulationRuntime/c/util/read_matlab4.c
Expand Up @@ -441,9 +441,17 @@ const char* omc_new_matlab4_reader(const char *filename, ModelicaMatReader *read
ModelicaMatVariable_t *omc_matlab4_find_var(ModelicaMatReader *reader, const char *varName)
{
ModelicaMatVariable_t key;
ModelicaMatVariable_t *res;
key.name = (char*) varName;

return (ModelicaMatVariable_t*)bsearch(&key,reader->allInfo,reader->nall,sizeof(ModelicaMatVariable_t),omc_matlab4_comp_var);
res = (ModelicaMatVariable_t*)bsearch(&key,reader->allInfo,reader->nall,sizeof(ModelicaMatVariable_t),omc_matlab4_comp_var);
if (res == NULL) { /* Try to convert the name to a Dymola name */
if (0==strcmp(varName, "time")) {
key.name = "Time";
return (ModelicaMatVariable_t*)bsearch(&key,reader->allInfo,reader->nall,sizeof(ModelicaMatVariable_t),omc_matlab4_comp_var);
}
}
return res;
}

/* Writes the number of values in the returned array if nvals is non-NULL */
Expand Down
2 changes: 1 addition & 1 deletion common
Submodule common updated 1 files
+9 −0 m4/omhome.m4

0 comments on commit cbc725d

Please sign in to comment.