Changeset 472a887 in OpenModelica
- Timestamp:
- 2010-12-01T12:24:24+01:00 (14 years ago)
- Branches:
- Added-citation-metadata, maintenance/v1.14, maintenance/v1.15, maintenance/v1.16, maintenance/v1.17, maintenance/v1.18, maintenance/v1.19, maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, maintenance/v1.23, master, omlib-staging
- Children:
- 46c01a5
- Parents:
- d5045e79
- Location:
- c_runtime
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
c_runtime/simulation_result_mat.cpp
r95e688a3 r472a887 46 46 const char Aclass[] = "A1 bt. ir1 na Tj re ac nt so r y "; 47 47 48 const int rank = 5;48 const int rank = 9; 49 49 int dims[rank] = { 1, globalData->nStates, globalData->nStates, 50 globalData->nAlgebraic, globalData->nParameters }; 50 globalData->nAlgebraic, globalData->intVariables.nAlgebraic, 51 globalData->boolVariables.nAlgebraic, 52 globalData->nParameters, globalData->intVariables.nParameters, 53 globalData->boolVariables.nParameters }; 51 54 const char **names[rank] = { &timeValName, 52 globalData->statesNames, 53 globalData->stateDerivativesNames, 54 globalData->algebraicsNames, 55 globalData->parametersNames }; 55 globalData->statesNames, 56 globalData->stateDerivativesNames, 57 globalData->algebraicsNames, 58 globalData->int_alg_names, 59 globalData->bool_alg_names, 60 globalData->parametersNames, 61 globalData->int_param_names, 62 globalData->bool_param_names, 63 }; 56 64 const char **descriptions[rank] = { &timeValDesc, 57 globalData->statesComments, 58 globalData->stateDerivativesComments, 59 globalData->algebraicsComments, 60 globalData->parametersComments }; 61 const int nVars = 1+globalData->nStates*2+globalData->nAlgebraic; 65 globalData->statesComments, 66 globalData->stateDerivativesComments, 67 globalData->algebraicsComments, 68 globalData->int_alg_comments, 69 globalData->bool_alg_comments, 70 globalData->parametersComments, 71 globalData->int_param_comments, 72 globalData->bool_param_comments 73 }; 74 const int nVars = globalData->nStates*2+globalData->nAlgebraic 75 +globalData->intVariables.nAlgebraic +globalData->boolVariables.nAlgebraic; 76 const int nParams = globalData->nParameters+globalData->intVariables.nParameters 77 +globalData->boolVariables.nParameters; 62 78 63 79 char *stringMatrix = NULL; … … 74 90 75 91 // flatten variables' names 76 flattenStrBuf(rank, dims, names, stringMatrix, rows, cols );92 flattenStrBuf(rank, dims, names, stringMatrix, rows, cols, true); 77 93 // write `name' matrix 78 94 writeMatVer4Matrix("name", rows, cols, stringMatrix, true); … … 80 96 81 97 // flatten variables' comments 82 flattenStrBuf(rank, dims, descriptions, stringMatrix, rows, cols );98 flattenStrBuf(rank, dims, descriptions, stringMatrix, rows, cols, false); 83 99 // write `description' matrix 84 100 writeMatVer4Matrix("description", rows, cols, stringMatrix, true); … … 86 102 87 103 // generate dataInfo table 88 generateDataInfo(doubleMatrix, rows, cols, globalData );104 generateDataInfo(doubleMatrix, rows, cols, globalData, nVars, nParams); 89 105 // write `dataInfo' matrix 90 106 writeMatVer4Matrix("dataInfo", cols, rows, doubleMatrix, false); … … 93 109 94 110 // generate `data_1' matrix (with parameter data) 95 rows = 2; 96 cols = 1+globalData->nParameters; 97 doubleMatrix = new double[rows*cols]; 98 if (doubleMatrix == NULL) 99 throw SimulationResultMallocException(); 100 doubleMatrix[0] = tstart; // start time 101 doubleMatrix[cols] = tstop; // stop time 102 for(fortran_integer i = 0; i < globalData->nParameters; ++i) { 103 doubleMatrix[i+1] = globalData->parameters[i]; 104 doubleMatrix[i+1+cols] = globalData->parameters[i]; 105 } 111 generateData_1(doubleMatrix, rows, cols, globalData, tstart, tstop); 106 112 // write `data_1' matrix 107 113 writeMatVer4Matrix("data_1", cols, rows, doubleMatrix, false); … … 110 116 data2HdrPos = fp.tellp(); 111 117 // write `data_2' header 112 writeMatVer4MatrixHeader("data_2", nVars , 0, false);118 writeMatVer4MatrixHeader("data_2", nVars+1, 0, false); 113 119 114 120 fp.flush(); … … 123 129 simulation_result_mat::~simulation_result_mat() 124 130 { 125 int nVars = 1+globalData->nStates*2+globalData->nAlgebraic; 131 int nVars = 1+globalData->nStates*2+globalData->nAlgebraic 132 +globalData->intVariables.nAlgebraic +globalData->boolVariables.nAlgebraic; 126 133 // this is a bad programming practice - closing file in destructor, 127 134 // where a proper error reporting can't be done … … 139 146 void simulation_result_mat::emit() 140 147 { 148 double datPoint; 149 141 150 // that does not belong here 142 151 storeExtrapolationData(); … … 149 158 fp.write((char*)globalData->statesDerivatives,sizeof(double)*globalData->nStates); 150 159 fp.write((char*)globalData->algebraics,sizeof(double)*globalData->nAlgebraic); 160 for(fortran_integer i = 0; i < globalData->intVariables.nAlgebraic; ++i) { 161 datPoint = (double)globalData->intVariables.algebraics[i]; 162 fp.write((char*)&datPoint,sizeof(double)); 163 } 164 for(fortran_integer i = 0; i < globalData->boolVariables.nAlgebraic; ++i) { 165 datPoint = (double)globalData->boolVariables.algebraics[i]; 166 fp.write((char*)&datPoint,sizeof(double)); 167 } 168 151 169 if (!fp) throw SimulationResultBaseException(); 152 170 ++ntimepoints; … … 155 173 // from an array of string creates flatten 'char*'-array suitable to be 156 174 // stored as MAT-file matrix 175 static inline void fixDerInName(char *str, size_t len) 176 { 177 char* dot; 178 if (len < 6) return; 179 180 // check if name start with "der(" and includes at least one dot 181 while (strncmp(str,"der(",4) == 0 && (dot = strrchr(str,'.')) != NULL) { 182 size_t pos = (size_t)(dot-str)+1; 183 // move prefix to the begining of string :"der(a.b.c.d)" -> "a.b.c.b.c.d)" 184 for(size_t i = 4; i < pos; ++i) 185 str[i-4] = str[i]; 186 // move "der(" to the end of prefix 187 // "a.b.c.b.c.d)" -> "a.b.c.der(d)" 188 strncpy(&str[pos-4],"der(",4); 189 } 190 } 191 157 192 long simulation_result_mat::flattenStrBuf(int rank, const int *dims, 158 193 const char ** const src[], 159 char* &dest, int& longest, int& nstrings) 194 char* &dest, int& longest, int& nstrings, 195 bool fixNames) 160 196 { 161 197 int i,j; … … 183 219 for (j = 0; j < dims[i]; ++j) { 184 220 strncpy(ptr,src[i][j],longest); 221 if (fixNames) fixDerInName(ptr,strlen(src[i][j])); 185 222 ptr += longest; 186 223 } … … 231 268 void simulation_result_mat::generateDataInfo(double* &dataInfo, 232 269 int& rows, int& cols, 233 const sim_DATA *mdl_data) 234 { 235 size_t nVars = mdl_data->nStates*2+mdl_data->nAlgebraic; 270 const sim_DATA *mdl_data, 271 int nVars, int nParams) 272 { 273 //size_t nVars = mdl_data->nStates*2+mdl_data->nAlgebraic; 236 274 //rows = 1+nVars+mdl_data->nParameters+mdl_data->nVarsAliases; 237 275 size_t ccol = 0; // current column - index offset 238 276 239 277 // assign rows & cols 240 rows = 1+ nVars + mdl_data->nParameters;278 rows = 1+ nVars + nParams; 241 279 cols = 4; 242 280 … … 250 288 ccol += 4; 251 289 // continuous and discrete variables 252 for(size_t i = 0; i < nVars; ++i) {290 for(size_t i = 0; i < (size_t)nVars; ++i) { 253 291 // row 1 - which table 254 292 dataInfo[ccol+4*i] = 2.0; … … 262 300 ccol += nVars*4; 263 301 // parameters and constants 264 for( fortran_integer i = 0; i < mdl_data->nParameters; ++i) {302 for(size_t i = 0; i < (size_t)nParams; ++i) { 265 303 // col 1 - which table 266 304 dataInfo[ccol+4*i] = 1.0; … … 275 313 //ccol += mdl_data->nParameters*4; 276 314 } 315 316 void simulation_result_mat::generateData_1(double* &data_1, 317 int& rows, int& cols, 318 const sim_DATA *mdl_data, 319 double tstart, double tstop) 320 { 321 int offset; 322 // calculate number of rows and columns 323 rows = 2; 324 cols = 1+mdl_data->nParameters+mdl_data->intVariables.nParameters 325 +mdl_data->boolVariables.nParameters; 326 // allocate data buffer 327 data_1 = new double[rows*cols]; 328 if (data_1 == NULL) 329 throw SimulationResultMallocException(); 330 data_1[0] = tstart; // start time 331 data_1[cols] = tstop; // stop time 332 offset = 1; 333 // double variables 334 for(fortran_integer i = 0; i < mdl_data->nParameters; ++i) { 335 data_1[offset+i] = mdl_data->parameters[i]; 336 data_1[offset+i+cols] = mdl_data->parameters[i]; 337 } 338 offset += mdl_data->nParameters; 339 // integer variables 340 for(fortran_integer i = 0; i < mdl_data->intVariables.nParameters; ++i) { 341 data_1[offset+i] = (double)mdl_data->intVariables.parameters[i]; 342 data_1[offset+i+cols] = (double)mdl_data->intVariables.parameters[i]; 343 } 344 offset += mdl_data->intVariables.nParameters; 345 // bool variables 346 for(fortran_integer i = 0; i < mdl_data->boolVariables.nParameters; ++i) { 347 data_1[offset+i] = (double)mdl_data->boolVariables.parameters[i]; 348 data_1[offset+i+cols] = (double)mdl_data->boolVariables.parameters[i]; 349 } 350 351 } -
c_runtime/simulation_result_mat.h
r95e688a3 r472a887 64 64 // helper functions 65 65 static long flattenStrBuf(int rank, const int *dims, const char ** const src[], 66 char* &dest, int& longest, int& nstrings); 66 char* &dest, int& longest, int& nstrings, 67 bool fixNames); 67 68 void writeMatVer4MatrixHeader(const char *name, int rows, int cols, 68 69 bool is_text); … … 70 71 const void *data, bool is_text); 71 72 static void generateDataInfo(double* &dataInfo, int& rows, int& cols, 72 const sim_DATA *mdl_data); 73 const sim_DATA *mdl_data, int nVars, int nParams); 74 static void generateData_1(double* &data_1, int& rows, int& cols, 75 const sim_DATA *mdl_data, double tstart, double tstop); 73 76 }; 74 77
Note: See TracChangeset
for help on using the changeset viewer.