Skip to content

Commit

Permalink
- Added support for readSimulationResult using .mat-files
Browse files Browse the repository at this point in the history
  - This is ~5x faster than reading .plt even for files with few variables


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7996 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 23, 2011
1 parent 5733402 commit ab52ae8
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 110 deletions.
17 changes: 17 additions & 0 deletions Compiler/FrontEnd/ValuesUtil.mo
Expand Up @@ -57,6 +57,7 @@ protected import Util;
protected import RTOpts;
protected import ClassInf;
protected import Types;
protected import SimulationResults;

public function typeConvert "function: typeConvert
Apply type conversion on a list of Values"
Expand Down Expand Up @@ -2434,4 +2435,20 @@ algorithm
Values.CODE(code) := val;
end getCode;

public function readDataset
input String filename;
input list<String> vars;
input Integer dimsize;
output Values.Value val;
protected
list<list<Real>> rvals;
list<list<Values.Value>> vals;
list<Values.Value> rows;
algorithm
rvals := SimulationResults.readDataset(filename,vars,dimsize);
vals := Util.listListMap_reversed(rvals,makeReal);
rows := Util.listMap_reversed(vals,makeArray);
val := makeArray(rows);
end readDataset;

end ValuesUtil;
40 changes: 20 additions & 20 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -1465,7 +1465,7 @@ algorithm
pwd = System.pwd();
pd = System.pathDelimiter();
filename_1 = stringAppendList({pwd,pd,filename});
value = SimulationResults.readPtolemyplotDataset(filename_1, vars_1, size);
value = ValuesUtil.readDataset(filename_1, vars_1, size);
then
(cache,value,st);

Expand Down Expand Up @@ -1529,7 +1529,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "plot2" ;
vars_2 = Util.listUnionElt("time", vars_1);
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
value = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0);
value = ValuesUtil.readDataset(filename, vars_2, 0);
pwd = System.pwd();
cit = winCitation();
omhome = Settings.getInstallationDirectoryPath();
Expand Down Expand Up @@ -1558,7 +1558,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "Catch error reading simulation file." ;
vars_2 = Util.listUnionElt("time", vars_1);
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0));
failure(_ = ValuesUtil.readDataset(filename, vars_2, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand Down Expand Up @@ -1615,10 +1615,10 @@ algorithm
// vars_2 =


// value = SimulationResults.readPtolemyplotDataset(filename2, vars_2, 0);
// value = ValuesUtil.readDataset(filename2, vars_2, 0);


// failure(_ = SimulationResults.readPtolemyplotDataset(filename2, vars_2, 0));
// failure(_ = ValuesUtil.readDataset(filename2, vars_2, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);
// resI = ValuesUtil.sendPtolemyplotDataset(value, vars_2, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, ExpressionDump.printExpStr(xRange), ExpressionDump.printExpStr(yRange));
Expand Down Expand Up @@ -1651,7 +1651,7 @@ algorithm
// vars_2 =


value = SimulationResults.readPtolemyplotDataset(filename2, vars_2, 0);
value = ValuesUtil.readDataset(filename2, vars_2, 0);

resI = ValuesUtil.sendPtolemyplotDataset(value, vars_2, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, ExpressionDump.printExpStr(xRange), ExpressionDump.printExpStr(yRange));
then
Expand All @@ -1673,7 +1673,7 @@ algorithm
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
failure(_ = System.getVariableNames(filename));
// vars_2 = Util.stringSplitAtChar(str, " ");
// failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0));
// failure(_ = ValuesUtil.readDataset(filename, vars_2, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand All @@ -1693,7 +1693,7 @@ algorithm
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
str = System.getVariableNames(filename);
vars_2 = Util.stringSplitAtChar(str, " ");
value = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0);
value = ValuesUtil.readDataset(filename, vars_2, 0);

resI = ValuesUtil.sendPtolemyplotDataset(value, vars_2, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, ExpressionDump.printExpStr(xRange), ExpressionDump.printExpStr(yRange));
then
Expand Down Expand Up @@ -1725,7 +1725,7 @@ algorithm
filename = Absyn.pathString(className);
filename = stringAppendList({filename, "_res.plt"});

value = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0);
value = ValuesUtil.readDataset(filename, vars_2, 0);

resI = ValuesUtil.sendPtolemyplotDataset(value, vars_2, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, ExpressionDump.printExpStr(xRange), ExpressionDump.printExpStr(yRange));
then
Expand All @@ -1750,7 +1750,7 @@ algorithm
filename = Absyn.pathString(className);
filename = stringAppendList({filename, "_res.plt"});

failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0));
failure(_ = ValuesUtil.readDataset(filename, vars_2, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand All @@ -1769,7 +1769,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "plot" ;
vars_2 = Util.listUnionElt("time", vars_1);
(cache,Values.STRING(filename),_) = Ceval.ceval(cache, env, buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
value = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0);
value = ValuesUtil.readDataset(filename, vars_2, 0);
resI = ValuesUtil.sendPtolemyplotDataset(value, vars_2, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, ExpressionDump.printExpStr(xRange), ExpressionDump.printExpStr(yRange));
then
(cache,Values.BOOL(true),st);
Expand All @@ -1789,7 +1789,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "Catch error reading simulation file." ;
vars_2 = Util.listUnionElt("time", vars_1);
(cache,Values.STRING(filename),_) = Ceval.ceval(cache, env, buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0));
failure(_ = ValuesUtil.readDataset(filename, vars_2, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand Down Expand Up @@ -1847,7 +1847,7 @@ algorithm
strVars = SimulationResults.readVariables(filename);
strVars = Util.listFilter1(strVars, visualizationVarShouldBeAdded, visvars);
vars_2 = Util.listUnionElt("time", strVars);
value = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0);
value = ValuesUtil.readDataset(filename, vars_2, 0);
resI = ValuesUtil.sendPtolemyplotDataset2(value, vars_2, visvar_str, "Plot by OpenModelica");
then
(cache,Values.BOOL(true),st);
Expand All @@ -1867,7 +1867,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr);
vars_2 = Util.listUnionElt("time", vars_1);
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env, buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
value = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0);
value = ValuesUtil.readDataset(filename, vars_2, 0);
resI = ValuesUtil.sendPtolemyplotDataset(value, vars_2, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, title, title);
then
(cache,Values.BOOL(true),st);
Expand All @@ -1887,7 +1887,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "Catch error reading simulation file." ;
vars_2 = Util.listUnionElt("time", vars_1);
(cache,Values.STRING(filename),_) = Ceval.ceval(cache, env, buildCurrentSimulationResultExp(), true, SOME(st), NONE(), msg);
failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_2, 0));
failure(_ = ValuesUtil.readDataset(filename, vars_2, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand Down Expand Up @@ -1955,7 +1955,7 @@ algorithm
(length > 1) = true;
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,
buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
value = SimulationResults.readPtolemyplotDataset(filename, vars_1, 0);
value = ValuesUtil.readDataset(filename, vars_1, 0);
pwd = System.pwd();
cit = winCitation();
omhome = Settings.getInstallationDirectoryPath();
Expand Down Expand Up @@ -1996,7 +1996,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "Catch error reading simulation file." ;
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,
buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg) "Util.list_union_elt(\"time\",vars\') => vars\'\' &" ;
failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_1, 0));
failure(_ = ValuesUtil.readDataset(filename, vars_1, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand Down Expand Up @@ -2044,7 +2044,7 @@ algorithm
filename = Absyn.pathString(className);
filename = stringAppendList({filename, "_res.plt"});

value = SimulationResults.readPtolemyplotDataset(filename, vars_1, 0);
value = ValuesUtil.readDataset(filename, vars_1, 0);
pwd = System.pwd();
cit = winCitation();
omhome = Settings.getInstallationDirectoryPath();
Expand Down Expand Up @@ -2074,7 +2074,7 @@ algorithm
(length > 1) = true;
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,
buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg);
value = SimulationResults.readPtolemyplotDataset(filename, vars_1, 0);
value = ValuesUtil.readDataset(filename, vars_1, 0);
resI = ValuesUtil.sendPtolemyplotDataset(value, vars_1, "Plot by OpenModelica", interpolation, title, legend, grid, logX, logY, xLabel, yLabel, points, ExpressionDump.printExpStr(xRange), ExpressionDump.printExpStr(yRange));
then
(cache,Values.BOOL(true),st);
Expand Down Expand Up @@ -2119,7 +2119,7 @@ algorithm
vars_1 = Util.listMap(expVars, ExpressionDump.printExpStr) "Catch error reading simulation file." ;
(cache,Values.STRING(filename),_) = Ceval.ceval(cache,env,
buildCurrentSimulationResultExp(), true, SOME(st),NONE(), msg) "Util.list_union_elt(\"time\",vars\') => vars\'\' &" ;
failure(_ = SimulationResults.readPtolemyplotDataset(filename, vars_1, 0));
failure(_ = ValuesUtil.readDataset(filename, vars_1, 0));
then
(cache,Values.STRING("Error reading the simulation result."),st);

Expand Down
26 changes: 8 additions & 18 deletions Compiler/Util/SimulationResults.mo
Expand Up @@ -39,7 +39,6 @@ encapsulated package SimulationResults
"

public import Values;
protected import ValuesUtil;

public function val
input String filename;
Expand All @@ -56,29 +55,20 @@ public function readVariables
external "C" vars=SimulationResults_readVariables(filename) annotation(Library = "omcruntime");
end readVariables;

protected function readPtolemyplotDatasetWork
input String inString;
input list<String> inStringLst;
input Integer inInteger;
output Values.Value outValue;

external "C" outValue=SimulationResults_readPtolemyplotDataset(inString,inStringLst,inInteger) annotation(Library = "omcruntime");
end readPtolemyplotDatasetWork;
public function readDataset
input String filename;
input list<String> vars;
input Integer dimsize;
output list<list<Real>> outMatrix;

public function readPtolemyplotDataset
input String inString;
input list<String> inStringLst;
input Integer inInteger;
output Values.Value outValue;
algorithm
outValue := ValuesUtil.reverseMatrix(readPtolemyplotDatasetWork(inString,inStringLst,inInteger));
end readPtolemyplotDataset;
external "C" outMatrix=SimulationResults_readDataset(filename,vars,dimsize) annotation(Library = "omcruntime");
end readDataset;

public function readSimulationResultSize
input String filename;
output Integer size;

external "C" size=SimulationResults_readPtolemyplotDatasetSize(filename) annotation(Library = "omcruntime");
external "C" size=SimulationResults_readSimulationResultSize(filename) annotation(Library = "omcruntime");
end readSimulationResultSize;

end SimulationResults;
Expand Down
37 changes: 21 additions & 16 deletions Compiler/Util/Util.mo
Expand Up @@ -2638,24 +2638,29 @@ public function listListMap "function: listListMap
end FuncTypeType_aToType_b;
replaceable type Type_b subtypeof Any;
algorithm
outTypeBLstLst:=
match (inTypeALstLst,inFuncTypeTypeAToTypeB)
local
list<Type_b> f_1;
list<list<Type_b>> r_1;
list<Type_a> f;
list<list<Type_a>> r;
FuncTypeType_aToType_b fn;
case ({},_) then {};
case ((f :: r),fn)
equation
f_1 = listMap(f, fn);
r_1 = listListMap(r, fn);
then
(f_1 :: r_1);
end match;
outTypeBLstLst := listMap1(inTypeALstLst,listMap,inFuncTypeTypeAToTypeB);
end listListMap;

public function listListMap_reversed "
Takes a list of lists and a function producing one value.
The function is applied to each element of the lists resulting
in a new list of lists.
This function reverses the inner list.
Example: listListMapReverse({ {1,2},{3},{4}},intString) => { {\"4\"},{\"3\"},{\"2\",\"1\"} }"
input list<list<Type_a>> inTypeALstLst;
input FuncTypeType_aToType_b inFuncTypeTypeAToTypeB;
output list<list<Type_b>> outTypeBLstLst;
replaceable type Type_a subtypeof Any;
partial function FuncTypeType_aToType_b
input Type_a inTypeA;
output Type_b outTypeB;
replaceable type Type_b subtypeof Any;
end FuncTypeType_aToType_b;
replaceable type Type_b subtypeof Any;
algorithm
outTypeBLstLst := listMap1(inTypeALstLst,listMap_reversed,inFuncTypeTypeAToTypeB);
end listListMap_reversed;

public function listListMap1 "function listListMap1
author: PA
similar to listListMap but for functions taking two arguments.
Expand Down
55 changes: 54 additions & 1 deletion Compiler/runtime/SimulationResults.c
Expand Up @@ -184,7 +184,60 @@ static void* SimulationResultsImpl__readVars(const char *filename)
}
default:
msg[0] = PlotFormatStr[curFormat];
c_add_message(-1, "SCRIPT", "Error", "readSimulationResultSize() not implemented for plot format: %s\n", msg, 1);
c_add_message(-1, "SCRIPT", "Error", "readSimulationResultSize() not implemented for plot format: %s", msg, 1);
return mk_nil();
}
}

static void* SimulationResultsImpl__readDataset(const char *filename, void *vars, int dimsize)
{
const char *msg[2] = {"",""};
void *res,*col;
char *var;
double *vals;
int i;
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename)) {
return NULL;
}
res = mk_nil();
switch (curFormat) {
case MATLAB4: {
ModelicaMatVariable_t *mat_var;
if (dimsize == 0) {
dimsize = matReader.nrows;
} else if (matReader.nrows != dimsize) {
fprintf(stderr, "dimsize: %d, rows %d\n", dimsize, matReader.nrows);
c_add_message(-1, "SCRIPT", "Error", "readDataset(...): Expected and actual dimension sizes do not match.", NULL, 0);
}
while (RML_NILHDR != RML_GETHDR(vars)) {
var = RML_STRINGDATA(RML_CAR(vars));
vars = RML_CDR(vars);
mat_var = omc_matlab4_find_var(&matReader,var);
if (mat_var == NULL) {
msg[1] = var;
msg[0] = filename;
c_add_message(-1, "SCRIPT", "Error", "Could not read variable %s in file %s.", msg, 2);
return NULL;
} else if (mat_var->isParam) {
col=mk_nil();
for (i=0;i<dimsize;i++) col=mk_cons(mk_rcon(matReader.params[mat_var->index]),col);
res = mk_cons(col,res);
} else {
vals = omc_matlab4_read_vals(&matReader,mat_var->index);
col=mk_nil();
for (i=0;i<dimsize;i++) col=mk_cons(mk_rcon(vals[i]),col);
res = mk_cons(col,res);
}
}
return res;
}
case PLT: {
return read_ptolemy_dataset(filename,vars,dimsize);
// return NULL;
}
default:
msg[0] = PlotFormatStr[curFormat];
c_add_message(-1, "SCRIPT", "Error", "readDataSet() not implemented for plot format: %s\n", msg, 1);
return NULL;
}
}
11 changes: 2 additions & 9 deletions Compiler/runtime/SimulationResults_omc.cpp
Expand Up @@ -45,16 +45,9 @@ void* SimulationResults_readVariables(const char *filename, const char *visvars)
}

extern void* _ValuesUtil_reverseMatrix(void*);
void* SimulationResults_readPtolemyplotDataset(const char *filename, void *lst, int datasize)
void* SimulationResults_readDataset(const char *filename, void *vars, int datasize)
{
int i, size = listLength(lst);
void *p,*res;
const char** vars = (const char**) malloc(sizeof(const char*)*size);
for (i=0, p=lst; i<size; i++) {
vars[i] = MMC_STRINGDATA(MMC_CAR(p));
p = MMC_CDR(p);
}
res = read_ptolemy_dataset(filename,size,vars,datasize);
void *res = SimulationResultsImpl__readDataset(filename,vars,datasize);
if (res == NULL) MMC_THROW();
return res;
}
Expand Down

0 comments on commit ab52ae8

Please sign in to comment.