Skip to content

Commit

Permalink
- implement read csv for SimulationResults
Browse files Browse the repository at this point in the history
- add test for msl31/mechanics runtime cpp (not yet activated)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12692 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Aug 27, 2012
1 parent afbc5f6 commit 80118b5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 3 deletions.
24 changes: 24 additions & 0 deletions Compiler/runtime/SimulationResults.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ static int SimulationResultsImpl__readSimulationResultSize(const char *filename,
if (size == -1) c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("Failed to read readSimulationResultSize from file: %s\n"), msg, 1);
break;
}
case CSV: {
size = read_csv_dataset_size(filename);
msg[0] = filename;
if (size == -1) c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("Failed to read readSimulationResultSize from file: %s\n"), msg, 1);
break;
}
default:
msg[0] = PlotFormatStr[simresglob->curFormat];
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("readSimulationResultSize() not implemented for plot format: %s\n"), msg, 1);
Expand Down Expand Up @@ -287,6 +293,24 @@ static void* SimulationResultsImpl__readDataset(const char *filename, void *vars
res = read_ptolemy_dataset(filename,vars,dimsize);
break;
}
case CSV: {
while (RML_NILHDR != RML_GETHDR(vars)) {
var = RML_STRINGDATA(RML_CAR(vars));
vars = RML_CDR(vars);
vals = read_csv_dataset(filename,var,dimsize);
if (vals == NULL) {
msg[1] = var;
msg[0] = filename;
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("Could not read variable %s in file %s."), msg, 2);
return NULL;
} else {
col=mk_nil();
for (i=0;i<dimsize;i++) col=mk_cons(mk_rcon(vals[i]),col);
res = mk_cons(col,res);
}
}
break;
}
default:
msg[0] = PlotFormatStr[simresglob->curFormat];
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("readDataSet() not implemented for plot format: %s\n"), msg, 1);
Expand Down
6 changes: 3 additions & 3 deletions Compiler/runtime/SimulationResultsCmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ DataField getData(const char *varname,const char *filename, unsigned int size, S
res.n = 0;
res.data = NULL;

/* fprintf(stderr, "getData of Var: %s from file %s\n", varname,filename); */
/* fprintf(stderr, "getData of Var: %s from file %s\n", varname,filename); */
cmpvar = mk_nil();
cmpvar = mk_cons(mk_scon(varname),cmpvar);
dataset = SimulationResultsImpl__readDataset(filename,cmpvar,size,srg);
Expand All @@ -171,8 +171,8 @@ DataField getData(const char *varname,const char *filename, unsigned int size, S
return res;
}

/* fprintf(stderr, "Data of Var: %s\n", varname);
First calculate the length of the matrix */
/* fprintf(stderr, "Data of Var: %s\n", varname); */
/* First calculate the length of the matrix */
datasetBackup = dataset;
while (RML_NILHDR != RML_GETHDR(dataset)) {
lst = RML_CAR(dataset);
Expand Down
4 changes: 4 additions & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ runtimeCPP:
runtimeCPPinstall:
$(MAKE) -C SimulationRuntime/cpp/ -f $(defaultMakefileTarget) install
time $(MAKE) -C testsuite/cppruntime -f Makefile

testlibrariemsl31cpp:
(cd testsuite/cppruntime/libraries/msl31 && time $(MAKE) -f Makefile > testsuite-msl31-cpp-trace.txt 2>&1)
echo "log is in testsuite/cppruntime/libraries/msl31/testsuite-msl31-cpp-trace.txt"

runtimeCPPclean:
$(MAKE) -C SimulationRuntime/cpp/ -f $(defaultMakefileTarget) clean
Expand Down
101 changes: 101 additions & 0 deletions SimulationRuntime/c/simulation/results/read_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ extern "C"
#include <stdio.h>
#include "read_csv.h"

int read_csv_dataset_size(const char* filename)
{
string header;
int length;
int size = -1;

ifstream stream(filename);

/* first line start with " */
getline(stream,header);
length = strlen(header.c_str());

/* second line has to be a number */
while (length > 0) {
if (length != 0)
size+=1;
getline(stream,header);
length = strlen(header.c_str());
}
return size;
}

char** read_csv_variables(const char* filename)
{
string header;
Expand Down Expand Up @@ -83,4 +105,83 @@ char** read_csv_variables(const char* filename)
return res;
}

double* read_csv_dataset(const char *filename, const char *var, int dimsize)
{
string header;
string variable;
string value;
unsigned int varpos=0;
unsigned int datapos=0;
bool startReading = false;
int length=0;
unsigned int stringlen=0;
vector<double> data;
char found=0;

/* get Position of var */
ifstream stream(filename);

getline(stream,header);
length = strlen(header.c_str());

for (int i = 0 ; i < length ; i++)
{
if (startReading) {
if (header[i] != '"') {
variable.append(1,header[i]);
stringlen+=1;
}
}

if (header[i] == '"') {
if (startReading) {
if (header[i+1] == ',') {
startReading = false;
if (strncmp(variable.c_str(),var,stringlen)==0) {
found = 1;
break;
}
varpos+=1;
stringlen = 0;
variable.clear();
}
} else {
startReading = true;
}
}
}
if (found==0)
return NULL;
/* collect data */
getline(stream,header);
length = strlen(header.c_str());
while (length > 0) {
for (int i = 0 ; i < length ; i++)
{
if (header[i] == ',') {
if (datapos == varpos) {
data.push_back(atof(value.c_str()));
break;
}
value.clear();
datapos +=1;
}
else
value.append(1,header[i]);
}
getline(stream,header);
length = strlen(header.c_str());
datapos = 0;
value.clear();
}

double *res = (double*)malloc((data.size())*sizeof(double));
for (unsigned int k = 0 ; k < data.size(); k++)
{
res[k] = data.at(k);
}

return res;
}

}
6 changes: 6 additions & 0 deletions SimulationRuntime/c/simulation/results/read_csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
*
*/

#include <stdio.h>

int read_csv_dataset_size(const char* filename);

char** read_csv_variables(const char *filename);

double* read_csv_dataset(const char *filename, const char *var, int dimsize);

0 comments on commit 80118b5

Please sign in to comment.