Skip to content

Commit

Permalink
- handle negated boolean alias in mat result file
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13008 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Sep 21, 2012
1 parent ac90ad2 commit 347c042
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
55 changes: 40 additions & 15 deletions SimulationRuntime/c/simulation/results/simulation_result_mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ int simulation_result_mat::calcDataSize(MODEL_DATA *modelData)
if (!modelData->realAlias[i].filterOutput) sz++;
for (int i = 0; i < modelData->nAliasInteger; i++)
if (!modelData->integerAlias[i].filterOutput) sz++;
negatedboolaliases = 0;
for (int i = 0; i < modelData->nAliasBoolean; i++)
if (!modelData->booleanAlias[i].filterOutput) sz++;
if (!modelData->booleanAlias[i].filterOutput)
{
if (modelData->booleanAlias[i].negate)
negatedboolaliases++;
sz++;
}
return sz;
}

Expand Down Expand Up @@ -189,7 +195,7 @@ simulation_result_mat::simulation_result_mat(const char* filename,
/* remember data2HdrPos */
data2HdrPos = fp.tellp();
/* write `data_2' header */
writeMatVer4MatrixHeader("data_2", r_indx_map.size() + i_indx_map.size() + b_indx_map.size() + 1 /* add one more for timeValue*/, 0, sizeof(double));
writeMatVer4MatrixHeader("data_2", r_indx_map.size() + i_indx_map.size() + b_indx_map.size() + negatedboolaliases + 1 /* add one more for timeValue*/, 0, sizeof(double));

free(doubleMatrix);
free(intMatrix);
Expand Down Expand Up @@ -217,7 +223,7 @@ simulation_result_mat::~simulation_result_mat()
if (fp) {
try {
fp.seekp(data2HdrPos);
writeMatVer4MatrixHeader("data_2", r_indx_map.size() + i_indx_map.size() + b_indx_map.size() + 1 /* add one more for timeValue*/, ntimepoints, sizeof(double));
writeMatVer4MatrixHeader("data_2", r_indx_map.size() + i_indx_map.size() + b_indx_map.size() + negatedboolaliases + 1 /* add one more for timeValue*/, ntimepoints, sizeof(double));
fp.close();
} catch (...) {
/* just ignore, we are in destructor */
Expand Down Expand Up @@ -248,6 +254,14 @@ void simulation_result_mat::emit(DATA *data)
datPoint = (double) data->localData[0]->booleanVars[i];
fp.write((char*)&datPoint,sizeof(double));
}
for (int i = 0; i < data->modelData.nAliasBoolean; i++) if (!data->modelData.booleanAlias[i].filterOutput)
{
if (data->modelData.booleanAlias[i].negate)
{
datPoint = (double) (data->localData[0]->booleanVars[data->modelData.booleanAlias[i].nameID]==1?0:1);
fp.write((char*)&datPoint,sizeof(double));
}
}
if (!fp)
THROW1("Error while writing file %s",filename);
++ntimepoints;
Expand Down Expand Up @@ -365,6 +379,7 @@ void simulation_result_mat::generateDataInfo(int32_t* &dataInfo,
/* size_t nVars = mdl_data->nStates*2+mdl_data->nAlgebraic;
rows = 1+nVars+mdl_data->nParameters+mdl_data->nVarsAliases; */
size_t ccol = 0; /* current column - index offset */
size_t indx = 1;
INTMAP::iterator it;
/* assign rows & cols */
rows = nVars + nParams;
Expand All @@ -377,11 +392,12 @@ void simulation_result_mat::generateDataInfo(int32_t* &dataInfo,
/* row 1 - which table */
dataInfo[ccol++] = 2;
/* row 2 - index of var in table (variable 'Time' have index 1) */
dataInfo[ccol++] = i+1;
dataInfo[ccol++] = indx;
/* row 3 - linear interpolation == 0 */
dataInfo[ccol++] = 0;
/* row 4 - not defined outside of the defined time range == -1 */
dataInfo[ccol++] = -1;
indx++;
}
/* alias variables */
for (int i = 0; i < mdl_data->nAliasReal; i++) {
Expand Down Expand Up @@ -457,25 +473,34 @@ void simulation_result_mat::generateDataInfo(int32_t* &dataInfo,
if (!mdl_data->booleanAlias[i].filterOutput)
{
int table = 0;
if (mdl_data->booleanAlias[i].aliasType == 0) /* variable */
{
it = b_indx_map.find(mdl_data->booleanAlias[i].nameID);
if (it != b_indx_map.end())
table = 2;
}
else if (mdl_data->booleanAlias[i].aliasType == 1) /* parameter */

if (mdl_data->booleanAlias[i].negate)
table = 2;
else
{
it = b_indx_parammap.find(mdl_data->booleanAlias[i].nameID);
if (it != b_indx_map.end())
table = 1;
if (mdl_data->booleanAlias[i].aliasType == 0) /* variable */
{
it = b_indx_map.find(mdl_data->booleanAlias[i].nameID);
if (it != b_indx_map.end())
table = 2;
}
else if (mdl_data->booleanAlias[i].aliasType == 1) /* parameter */
{
it = b_indx_parammap.find(mdl_data->booleanAlias[i].nameID);
if (it != b_indx_map.end())
table = 1;
}
}
if(table)
{
/* row 1 - which table */
dataInfo[ccol] = table;
/* row 2 - index of var in table (variable 'Time' have index 1) */
if (mdl_data->booleanAlias[i].negate)
dataInfo[ccol+1] = -(it->second+1);
{
dataInfo[ccol+1] = indx;
indx++;
}
else
dataInfo[ccol+1] = it->second+1;
/* row 3 - linear interpolation == 0 */
Expand Down
21 changes: 11 additions & 10 deletions SimulationRuntime/c/simulation/results/simulation_result_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
Expand Down Expand Up @@ -59,7 +59,7 @@ class simulation_result_mat : public simulation_result {
virtual ~simulation_result_mat();
virtual void emit(DATA *data);
void writeParameterData(MODEL_DATA *modelData);
virtual const char* result_type() {
virtual const char* result_type() {
/* return "Dymosim's compatible MAT-file"; */
return "mat";
}
Expand All @@ -82,16 +82,17 @@ class simulation_result_mat : public simulation_result {
INTMAP i_indx_parammap;
INTMAP b_indx_map;
INTMAP b_indx_parammap;


unsigned int negatedboolaliases;
int numVars;

/* helper functions */
/* helper functions */
long flattenStrBuf(int dims, const struct VAR_INFO** src,
char* &dest, int& longest, int& nstrings,
char* &dest, int& longest, int& nstrings,
bool fixNames, bool useComment);
void writeMatVer4MatrixHeader(const char *name, int rows, int cols,
unsigned int size);
void writeMatVer4Matrix(const char *name, int rows, int cols,
void writeMatVer4Matrix(const char *name, int rows, int cols,
const void *data, unsigned int size);
void generateDataInfo(int* &dataInfo, int& rows, int& cols,
const MODEL_DATA *mdl_data, int nVars, int nParams);
Expand Down

0 comments on commit 347c042

Please sign in to comment.