Skip to content

Commit

Permalink
- Fix for mat-files that contain alias of the time-variable
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8562 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Apr 9, 2011
1 parent ae30379 commit 09e6d31
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 52 deletions.
12 changes: 7 additions & 5 deletions c_runtime/read_matlab4.c
Expand Up @@ -169,7 +169,9 @@ double* omc_matlab4_read_vals(ModelicaMatReader *reader, int varIndex)
for (i=0; i<reader->nrows; i++) {
fseek(reader->file,reader->var_offset + sizeof(double)*(i*reader->nvar + abs(varIndex)-1), SEEK_SET);
if (1 != fread(&tmp[i], sizeof(double), 1, reader->file)) {
free(tmp); tmp=NULL;
/* fprintf(stderr, "Corrupt file at %d of %d? nvar %d\n", i, reader->nrows, reader->nvar); */
free(tmp);
tmp=NULL;
return NULL;
}
/* fprintf(stderr, "tmp[%d]=%g\n", i, tmp[i]); */
Expand Down Expand Up @@ -248,10 +250,10 @@ double omc_matlab4_stopTime(ModelicaMatReader *reader)
int omc_matlab4_val(double *res, ModelicaMatReader *reader, ModelicaMatVariable_t *var, double time)
{
if (var->isParam) {
if (var->index < 0)
*res = -reader->params[abs(var->index)-1];
else
*res = reader->params[var->index-1];
if (var->index < 0)
*res = -reader->params[abs(var->index)-1];
else
*res = reader->params[var->index-1];
} else {
double w1,w2,y1,y2;
int i1,i2;
Expand Down
84 changes: 37 additions & 47 deletions c_runtime/simulation_result_mat.cpp
Expand Up @@ -36,12 +36,14 @@
#include <cstring>
#include <cstdlib>
#include <stdint.h>
#include <assert.h>

static const struct omc_varInfo timeValName = {0,"time","Simulation time [s]",{"",-1,-1,-1,-1}};

static int calcDataSize(map<void*,int> &indx_map)
{
int sz = 1; // time
int sz = 0;
indx_map.insert(simulation_result_mat::indx_type((void*)(&globalData->timeValue),sz++));
for (int i = 0; i < globalData->nStates; i++)
if (!globalData->statesFilterOutput[i]) {
indx_map.insert(simulation_result_mat::indx_type((void*)(&globalData->states[i]),sz));
Expand Down Expand Up @@ -167,7 +169,7 @@ simulation_result_mat::simulation_result_mat(const char* filename,

data2HdrPos = fp.tellp();
// write `data_2' header
writeMatVer4MatrixHeader("data_2", indx_map.size()+1, 0, false);
writeMatVer4MatrixHeader("data_2", indx_map.size(), 0, false);

fp.flush();

Expand All @@ -190,7 +192,7 @@ simulation_result_mat::~simulation_result_mat()
if (fp) {
try {
fp.seekp(data2HdrPos);
writeMatVer4MatrixHeader("data_2", indx_map.size()+1, ntimepoints, false);
writeMatVer4MatrixHeader("data_2", indx_map.size(), ntimepoints, false);
fp.close();
} catch (...) {
// just ignore, we are in destructor
Expand Down Expand Up @@ -336,59 +338,50 @@ void simulation_result_mat::generateDataInfo(double* &dataInfo,

dataInfo = new double[rows*cols];
if (dataInfo == NULL) throw SimulationResultMallocException();
// time variable
dataInfo[0] = 0.0;
dataInfo[1] = 1.0;
dataInfo[2] = 0.0;
dataInfo[3] = -1.0;
ccol += 4;
// continuous and discrete variables
// continuous and discrete variables, including time
for(size_t i = 0; i < (size_t)indx_map.size(); ++i) {
// row 1 - which table
dataInfo[ccol+4*i] = 2.0;
dataInfo[ccol++] = 2.0;
// row 2 - index of var in table (variable 'Time' have index 1)
dataInfo[ccol+4*i+1] = i+2.0;
dataInfo[ccol++] = i+1.0;
// row 3 - linear interpolation == 0
dataInfo[ccol+4*i+2] = 0.0;
dataInfo[ccol++] = 0.0;
// row 4 - not defined outside of the defined time range == -1
dataInfo[ccol+4*i+3] = -1.0;
dataInfo[ccol++] = -1.0;
}
ccol = ccol+4*indx_map.size();
// alias variables
for (int i = 0; i < globalData->nAlias; i++) if (!globalData->aliasFilterOutput[i]) {
double table = 0;
map<void*,int>::iterator it = indx_map.find((void*)globalData->realAlias[i].alias);
if (it == indx_map.end()) {
it = indx_parammap.find((void*)globalData->realAlias[i].alias);
if (it == indx_parammap.end())
continue;
else
for (int i = 0; i < globalData->nAlias; i++) {
if (!globalData->aliasFilterOutput[i]) {
double table = 0;
map<void*,int>::iterator it = indx_map.find((void*)globalData->realAlias[i].alias);
if (it == indx_map.end()) {
it = indx_parammap.find((void*)globalData->realAlias[i].alias);
assert(it != indx_parammap.end());
table = 1.0;
} else {
table = 2.0;
} else {
table = 2.0;
}
// row 1 - which table
dataInfo[ccol] = table;
// row 2 - index of var in table (variable 'Time' have index 1)
if (((globalData->realAlias)[i]).negate)
dataInfo[ccol+1] = -(it->second+1.0);
else
dataInfo[ccol+1] = it->second+1.0;
// row 3 - linear interpolation == 0
dataInfo[ccol+2] = 0.0;
// row 4 - not defined outside of the defined time range == -1
dataInfo[ccol+3] = -1.0;
ccol += 4;
}
// row 1 - which table
dataInfo[ccol] = table;
// row 2 - index of var in table (variable 'Time' have index 1)
if (((globalData->realAlias)[i]).negate)
dataInfo[ccol+1] = -(it->second+1.0);
else
dataInfo[ccol+1] = it->second+1.0;
// row 3 - linear interpolation == 0
dataInfo[ccol+2] = 0.0;
// row 4 - not defined outside of the defined time range == -1
dataInfo[ccol+3] = -1.0;
ccol += 4;
}
for (int i = 0; i < globalData->intVariables.nAlias; i++) if (!globalData->intVariables.aliasFilterOutput[i]) {
double table = 0;
map<void*,int>::iterator it = indx_map.find((void*)globalData->intVariables.alias[i].alias);
if (it == indx_map.end()) {
it = indx_parammap.find((void*)globalData->intVariables.alias[i].alias);
if (it == indx_parammap.end())
continue;
else
table = 1.0;
assert(it != indx_parammap.end());
table = 1.0;
} else {
table = 2.0;
}
Expand All @@ -410,12 +403,9 @@ void simulation_result_mat::generateDataInfo(double* &dataInfo,
map<void*,int>::iterator it = indx_map.find((void*)globalData->boolVariables.alias[i].alias);
if (it == indx_map.end()) {
it = indx_parammap.find((void*)globalData->boolVariables.alias[i].alias);
if (it == indx_parammap.end())
continue;
else
table = 1.0;
}
else {
assert(it == indx_parammap.end());
table = 1.0;
} else {
table = 2.0;
}
// row 1 - which table
Expand Down

0 comments on commit 09e6d31

Please sign in to comment.