Skip to content

Commit

Permalink
- use structure for simulationresults functions, make them global in …
Browse files Browse the repository at this point in the history
…the files including SimulationResults.c, this is usefull to load several result files at the same time

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8595 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Apr 12, 2011
1 parent 3220594 commit 52b9f5a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 64 deletions.
112 changes: 57 additions & 55 deletions Compiler/runtime/SimulationResults.c
Expand Up @@ -21,32 +21,34 @@ typedef enum {
} PlotFormat;
const char *PlotFormatStr[] = {"Unknown","MATLAB4","PLT","CSV"};

static PlotFormat curFormat = UNKNOWN_PLOT;
static char *curFileName = NULL;
static ModelicaMatReader matReader;
static FILE *pltReader;
static FILE *csvReader;
typedef struct {
PlotFormat curFormat;
char *curFileName;
ModelicaMatReader matReader;
FILE *pltReader;
FILE *csvReader;
} SimulationResult_Globals;

void SimulationResultsImpl__close()
void SimulationResultsImpl__close(SimulationResult_Globals* simresglob)
{
switch (curFormat) {
case MATLAB4: omc_free_matlab4_reader(&matReader); break;
case PLT: fclose(pltReader); break;
case CSV: fclose(csvReader); break;
switch (simresglob->curFormat) {
case MATLAB4: omc_free_matlab4_reader(&simresglob->matReader); break;
case PLT: fclose(simresglob->pltReader); break;
case CSV: fclose(simresglob->csvReader); break;
}
curFormat = UNKNOWN_PLOT;
if (curFileName) free(curFileName);
curFileName = NULL;
simresglob->curFormat = UNKNOWN_PLOT;
if (simresglob->curFileName) free(simresglob->curFileName);
simresglob->curFileName = NULL;
}

static PlotFormat SimulationResultsImpl__openFile(const char *filename)
static PlotFormat SimulationResultsImpl__openFile(const char *filename, SimulationResult_Globals* simresglob)
{
PlotFormat format;
int len = strlen(filename);
const char *msg[] = {"",""};
if (curFileName && 0==strcmp(filename,curFileName)) return curFormat; // Super cache :)
if (simresglob->curFileName && 0==strcmp(filename,simresglob->curFileName)) return simresglob->curFormat; // Super cache :)
// Start by closing the old file...
SimulationResultsImpl__close();
SimulationResultsImpl__close(simresglob);

if (len < 5) format = UNKNOWN_PLOT;
else if (0 == strcmp(filename+len-4, ".mat")) format = MATLAB4;
Expand All @@ -55,23 +57,23 @@ static PlotFormat SimulationResultsImpl__openFile(const char *filename)
else format = UNKNOWN_PLOT;
switch (format) {
case MATLAB4:
if (0!=(msg[0]=omc_new_matlab4_reader(filename,&matReader))) {
if (0!=(msg[0]=omc_new_matlab4_reader(filename,&simresglob->matReader))) {
msg[1] = filename;
c_add_message(-1, "SCRIPT", "Error", "Failed to open simulation result %s: %s\n", msg, 2);
return UNKNOWN_PLOT;
}
break;
case PLT:
pltReader = fopen(filename, "r");
if (pltReader==NULL) {
simresglob->pltReader = fopen(filename, "r");
if (simresglob->pltReader==NULL) {
msg[1] = filename;
c_add_message(-1, "SCRIPT", "Error", "Failed to open simulation result %s: %s\n", msg, 2);
return UNKNOWN_PLOT;
}
break;
case CSV:
csvReader = fopen(filename, "r");
if (csvReader==NULL) {
simresglob->csvReader = fopen(filename, "r");
if (simresglob->csvReader==NULL) {
msg[1] = filename;
c_add_message(-1, "SCRIPT", "Error", "Failed to open simulation result %s: %s\n", msg, 2);
return UNKNOWN_PLOT;
Expand All @@ -83,33 +85,33 @@ static PlotFormat SimulationResultsImpl__openFile(const char *filename)
return UNKNOWN_PLOT;
}

curFormat = format;
curFileName = strdup(filename);
simresglob->curFormat = format;
simresglob->curFileName = strdup(filename);
// fprintf(stderr, "SimulationResultsImpl__openFile(%s) => %s\n", filename, PlotFormatStr[curFormat]);
return curFormat;
return simresglob->curFormat;
}

static double SimulationResultsImpl__val(const char *filename, const char *varname, double timeStamp)
static double SimulationResultsImpl__val(const char *filename, const char *varname, double timeStamp, SimulationResult_Globals* simresglob)
{
double res;
const char *msg[4] = {"","","",""};
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename)) {
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename,simresglob)) {
return NAN;
}
switch (curFormat) {
switch (simresglob->curFormat) {
case MATLAB4: {
ModelicaMatVariable_t *var;
if (0 == (var=omc_matlab4_find_var(&matReader,varname))) {
if (0 == (var=omc_matlab4_find_var(&simresglob->matReader,varname))) {
msg[1] = varname;
msg[0] = filename;
c_add_message(-1, "SCRIPT", "Error", "%s not found in %s\n", msg, 2);
return NAN;
}
if (omc_matlab4_val(&res,&matReader,var,timeStamp)) {
if (omc_matlab4_val(&res,&simresglob->matReader,var,timeStamp)) {
char buf[64],buf2[64],buf3[64];
snprintf(buf,60,"%g",timeStamp);
snprintf(buf2,60,"%g",omc_matlab4_startTime(&matReader));
snprintf(buf3,60,"%g",omc_matlab4_stopTime(&matReader));
snprintf(buf2,60,"%g",omc_matlab4_startTime(&simresglob->matReader));
snprintf(buf3,60,"%g",omc_matlab4_stopTime(&simresglob->matReader));
msg[3] = varname;
msg[2] = buf;
msg[1] = buf2;
Expand All @@ -125,16 +127,16 @@ static double SimulationResultsImpl__val(const char *filename, const char *varna
double pt,t,pv,v,w1,w2;
int nread=0;
sprintf(strToFind,"DataSet: %s\n",varname);
fseek(pltReader,0,SEEK_SET);
fseek(simresglob->pltReader,0,SEEK_SET);
do {
if (NULL==fgets(line,255,pltReader)) {
if (NULL==fgets(line,255,simresglob->pltReader)) {
msg[1] = varname;
msg[0] = filename;
c_add_message(-1, "SCRIPT", "Error", "%s not found in %s\n", msg, 2);
return NAN;
}
} while (strcmp(strToFind,line));
while (fscanf(pltReader,"%lg, %lg\n",&t,&v) == 2) {
while (fscanf(simresglob->pltReader,"%lg, %lg\n",&t,&v) == 2) {
nread++;
if (t > timeStamp) break;
pt = t;
Expand All @@ -156,22 +158,22 @@ static double SimulationResultsImpl__val(const char *filename, const char *varna
}
}
default:
msg[0] = PlotFormatStr[curFormat];
msg[0] = PlotFormatStr[simresglob->curFormat];
c_add_message(-1, "SCRIPT", "Error", "val() not implemented for plot format: %s\n", msg, 1);
return NAN;
}
}

static int SimulationResultsImpl__readSimulationResultSize(const char *filename)
static int SimulationResultsImpl__readSimulationResultSize(const char *filename, SimulationResult_Globals* simresglob)
{
const char *msg[2] = {"",""};
int size;
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename)) {
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename,simresglob)) {
return -1;
}
switch (curFormat) {
switch (simresglob->curFormat) {
case MATLAB4: {
return matReader.nrows;
return simresglob->matReader.nrows;
}
case PLT: {
size = read_ptolemy_dataset_size(filename);
Expand All @@ -180,24 +182,24 @@ static int SimulationResultsImpl__readSimulationResultSize(const char *filename)
return size;
}
default:
msg[0] = PlotFormatStr[curFormat];
msg[0] = PlotFormatStr[simresglob->curFormat];
c_add_message(-1, "SCRIPT", "Error", "readSimulationResultSize() not implemented for plot format: %s\n", msg, 1);
return -1;
}
}

static void* SimulationResultsImpl__readVars(const char *filename)
static void* SimulationResultsImpl__readVars(const char *filename, SimulationResult_Globals* simresglob)
{
const char *msg[2] = {"",""};
void *res;
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename)) {
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename,simresglob)) {
return mk_nil();
}
res = mk_nil();
switch (curFormat) {
switch (simresglob->curFormat) {
case MATLAB4: {
int i;
for (i=matReader.nall-1; i>=0; i--) res = mk_cons(mk_scon(matReader.allInfo[i].name),res);
for (i=simresglob->matReader.nall-1; i>=0; i--) res = mk_cons(mk_scon(simresglob->matReader.allInfo[i].name),res);
return res;
}
case PLT: {
Expand All @@ -213,48 +215,48 @@ static void* SimulationResultsImpl__readVars(const char *filename)
return res;
}
default:
msg[0] = PlotFormatStr[curFormat];
msg[0] = PlotFormatStr[simresglob->curFormat];
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)
static void* SimulationResultsImpl__readDataset(const char *filename, void *vars, int dimsize, SimulationResult_Globals* simresglob)
{
const char *msg[2] = {"",""};
void *res,*col;
char *var;
double *vals;
int i;
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename)) {
if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(filename,simresglob)) {
return NULL;
}
res = mk_nil();
switch (curFormat) {
switch (simresglob->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);
dimsize = simresglob->matReader.nrows;
} else if (simresglob->matReader.nrows != dimsize) {
fprintf(stderr, "dimsize: %d, rows %d\n", dimsize, simresglob->matReader.nrows);
c_add_message(-1, "SCRIPT", "Error", "readDataset(...): Expected and actual dimension sizes do not match.", NULL, 0);
return NULL;
}
while (RML_NILHDR != RML_GETHDR(vars)) {
var = RML_STRINGDATA(RML_CAR(vars));
vars = RML_CDR(vars);
mat_var = omc_matlab4_find_var(&matReader,var);
mat_var = omc_matlab4_find_var(&simresglob->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((mat_var->index<0)?-matReader.params[abs(mat_var->index)-1]:matReader.params[abs(mat_var->index)-1]),col);
for (i=0;i<dimsize;i++) col=mk_cons(mk_rcon((mat_var->index<0)?-simresglob->matReader.params[abs(mat_var->index)-1]:simresglob->matReader.params[abs(mat_var->index)-1]),col);
res = mk_cons(col,res);
} else {
vals = omc_matlab4_read_vals(&matReader,mat_var->index);
vals = omc_matlab4_read_vals(&simresglob->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);
Expand All @@ -267,7 +269,7 @@ static void* SimulationResultsImpl__readDataset(const char *filename, void *vars
// return NULL;
}
default:
msg[0] = PlotFormatStr[curFormat];
msg[0] = PlotFormatStr[simresglob->curFormat];
c_add_message(-1, "SCRIPT", "Error", "readDataSet() not implemented for plot format: %s\n", msg, 1);
return NULL;
}
Expand Down
10 changes: 6 additions & 4 deletions Compiler/runtime/SimulationResults_omc.cpp
Expand Up @@ -40,27 +40,29 @@ extern "C" {

#include "SimulationResults.c"

static SimulationResult_Globals simresglob = {UNKNOWN_PLOT,NULL,{NULL,NULL,0,NULL,0,NULL,0,0,0,NULL},NULL,NULL};

void* SimulationResults_readVariables(const char *filename, const char *visvars)
{
return SimulationResultsImpl__readVars(filename);
return SimulationResultsImpl__readVars(filename,&simresglob);
}

extern void* _ValuesUtil_reverseMatrix(void*);
void* SimulationResults_readDataset(const char *filename, void *vars, int datasize)
{
void *res = SimulationResultsImpl__readDataset(filename,vars,datasize);
void *res = SimulationResultsImpl__readDataset(filename,vars,datasize,&simresglob);
if (res == NULL) MMC_THROW();
return res;
}

int SimulationResults_readSimulationResultSize(const char *filename)
{
return SimulationResultsImpl__readSimulationResultSize(filename);
return SimulationResultsImpl__readSimulationResultSize(filename,&simresglob);
}

double SimulationResults_val(const char *filename, const char *varname, double timeStamp)
{
return SimulationResultsImpl__val(filename,varname,timeStamp);
return SimulationResultsImpl__val(filename,varname,timeStamp,&simresglob);
}

}
12 changes: 7 additions & 5 deletions Compiler/runtime/SimulationResults_rml.c
Expand Up @@ -35,6 +35,8 @@

#include "SimulationResults.c"

static SimulationResult_Globals simresglob = {UNKNOWN_PLOT,NULL,{NULL,NULL,0,NULL,0,NULL,0,0,0,NULL},NULL,NULL};

void SimulationResults_5finit(void)
{
}
Expand All @@ -44,14 +46,14 @@ RML_BEGIN_LABEL(SimulationResults__readVariables)
rml_sint_t i,size;
char* filename = RML_STRINGDATA(rmlA0);

rmlA0 = SimulationResultsImpl__readVars(filename);
rmlA0 = SimulationResultsImpl__readVars(filename,&simresglob);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(SimulationResults__readDataset)
{
rmlA0 = (void*)SimulationResultsImpl__readDataset(RML_STRINGDATA(rmlA0),rmlA1,RML_UNTAGFIXNUM(rmlA2));
rmlA0 = (void*)SimulationResultsImpl__readDataset(RML_STRINGDATA(rmlA0),rmlA1,RML_UNTAGFIXNUM(rmlA2),&simresglob);

if (rmlA0 == NULL) {
RML_TAILCALLK(rmlFC);
Expand All @@ -64,21 +66,21 @@ RML_END_LABEL
RML_BEGIN_LABEL(SimulationResults__readSimulationResultSize)
{
char* filename = RML_STRINGDATA(rmlA0);
rmlA0 = mk_icon(SimulationResultsImpl__readSimulationResultSize(filename));
rmlA0 = mk_icon(SimulationResultsImpl__readSimulationResultSize(filename,&simresglob));
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(SimulationResults__val)
{
rmlA0 = mk_rcon(SimulationResultsImpl__val(RML_STRINGDATA(rmlA0),RML_STRINGDATA(rmlA1),rml_prim_get_real(rmlA2)));
rmlA0 = mk_rcon(SimulationResultsImpl__val(RML_STRINGDATA(rmlA0),RML_STRINGDATA(rmlA1),rml_prim_get_real(rmlA2),&simresglob));
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(SimulationResults__close)
{
SimulationResultsImpl__close();
SimulationResultsImpl__close(&simresglob);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
Expand Down

0 comments on commit 52b9f5a

Please sign in to comment.