@@ -256,7 +256,10 @@ void mat4_writeParameterData(simulation_result *self,DATA *data, threadData_t *t
256256 generateData_1 (data, threadData, doubleMatrix, rows, cols, matData->startTime , matData->stopTime );
257257 /* write `data_1' matrix */
258258 mat_writeMatVer4Matrix (self,data, threadData," data_1" , cols, rows, doubleMatrix, MAT4_DOUBLE_PRECISION_FULL_MATRIX);
259- free (doubleMatrix); doubleMatrix = NULL ;
259+ if (doubleMatrix) {
260+ free (doubleMatrix);
261+ doubleMatrix = NULL ;
262+ }
260263 matData->fp .seekp (remember);
261264 } catch (...) {
262265 matData->fp .close ();
@@ -333,17 +336,22 @@ void mat4_init(simulation_result *self,DATA *data, threadData_t *threadData)
333336 matData->r_indx_map .size () + matData->i_indx_map .size () + matData->b_indx_map .size () + matData->negatedboolaliases + 1 /* add one more for timeValue*/ + self->cpuTime + /* add one more for solverSteps*/ + omc_flag[FLAG_SOLVER_STEPS] + nSensitivities, 0 ,
334337 omc_flag[FLAG_SINGLE_PRECISION] ? MAT4_SINGLE_PRECISION_FULL_MATRIX : MAT4_DOUBLE_PRECISION_FULL_MATRIX);
335338
336- free (doubleMatrix);
339+ if (doubleMatrix) {
340+ free (doubleMatrix);
341+ doubleMatrix = NULL ;
342+ }
337343 free (intMatrix);
338- doubleMatrix = NULL ;
339344 intMatrix = NULL ;
340345 matData->fp .flush ();
341346
342347 } catch (...) {
343348 matData->fp .close ();
344349 free (names); names=NULL ;
345350 free (stringMatrix);
346- free (doubleMatrix);
351+ if (doubleMatrix) {
352+ free (doubleMatrix);
353+ doubleMatrix = NULL ;
354+ }
347355 free (intMatrix);
348356 rt_accumulate (SIM_TIMER_OUTPUT);
349357 throwStreamPrint (threadData, " Error while writing mat file %s" ,self->filename );
@@ -558,7 +566,6 @@ static void generateDataInfo(simulation_result *self, DATA *data, threadData_t *
558566 /* assign rows & cols */
559567 rows = nVars + nParams;
560568 cols = 4 ;
561-
562569 dataInfo = (int *) calloc (rows*cols,sizeof (int ));
563570 assertStreamPrint (threadData, 0 !=dataInfo," Cannot alloc memory" );
564571 /* continuous and discrete variables, including time */
@@ -593,7 +600,7 @@ static void generateDataInfo(simulation_result *self, DATA *data, threadData_t *
593600 if (it != matData->r_indx_parammap .end ())
594601 {
595602 table = 1 ;
596- aliascol = it->second + 1 ;
603+ aliascol = it->second ;
597604 }
598605 } else if (mdl_data->realAlias [i].aliasType == 2 ) /* time */
599606 {
@@ -612,7 +619,7 @@ static void generateDataInfo(simulation_result *self, DATA *data, threadData_t *
612619 /* row 3 - linear interpolation == 0 */
613620 dataInfo[ccol+2 ] = 0 ;
614621 /* row 4 - not defined outside of the defined time range == -1 */
615- dataInfo[ccol+3 ] = -1 ;
622+ dataInfo[ccol+3 ] = mdl_data-> realAlias [i]. aliasType == 1 ? 0 : -1 ;
616623 ccol += 4 ;
617624 }
618625 }
@@ -624,24 +631,28 @@ static void generateDataInfo(simulation_result *self, DATA *data, threadData_t *
624631 if (mdl_data->integerAlias [i].aliasType == 0 ) /* variable */
625632 {
626633 it = matData->i_indx_map .find (mdl_data->integerAlias [i].nameID );
627- if (it != matData->i_indx_map .end ())
634+ if (it != matData->i_indx_map .end ()) {
628635 table = 2 ;
636+ }
637+ aliascol = it->second +1 ;
629638 }
630639 else if (mdl_data->integerAlias [i].aliasType == 1 ) /* parameter */
631640 {
632641 it = matData->i_indx_parammap .find (mdl_data->integerAlias [i].nameID );
633- if (it != matData->i_indx_parammap .end ())
642+ if (it != matData->i_indx_parammap .end ()) {
634643 table = 1 ;
644+ }
645+ aliascol = it->second ;
635646 }
636647 if (table)
637648 {
638649 /* row 1 - which table */
639650 dataInfo[ccol] = table;
640651 /* row 2 - index of var in table */
641652 if (mdl_data->integerAlias [i].negate )
642- dataInfo[ccol+1 ] = -(it-> second + 1 ) ;
653+ dataInfo[ccol+1 ] = -aliascol ;
643654 else
644- dataInfo[ccol+1 ] = it-> second + 1 ;
655+ dataInfo[ccol+1 ] = aliascol ;
645656 /* row 3 - linear interpolation == 0 */
646657 dataInfo[ccol+2 ] = 0 ;
647658 /* row 4 - not defined outside of the defined time range == -1 */
@@ -662,28 +673,31 @@ static void generateDataInfo(simulation_result *self, DATA *data, threadData_t *
662673 if (mdl_data->booleanAlias [i].aliasType == 0 ) /* variable */
663674 {
664675 it = matData->b_indx_map .find (mdl_data->booleanAlias [i].nameID );
665- if (it != matData->b_indx_map .end ())
676+ if (it != matData->b_indx_map .end ()) {
666677 table = 2 ;
678+ }
679+ aliascol = it->second +1 ;
667680 }
668681 else if (mdl_data->booleanAlias [i].aliasType == 1 ) /* parameter */
669682 {
670683 it = matData->b_indx_parammap .find (mdl_data->booleanAlias [i].nameID );
671- if (it != matData->b_indx_parammap .end ())
684+ if (it != matData->b_indx_parammap .end ()) {
672685 table = 1 ;
686+ }
687+ aliascol = it->second ;
673688 }
674689 }
675690 if (table)
676691 {
677692 /* row 1 - which table */
678693 dataInfo[ccol] = table;
679694 /* row 2 - index of var in table */
680- if (mdl_data->booleanAlias [i].negate )
681- {
695+ if (mdl_data->booleanAlias [i].negate ) {
682696 dataInfo[ccol+1 ] = indx;
683697 indx++;
698+ } else {
699+ dataInfo[ccol+1 ] = aliascol;
684700 }
685- else
686- dataInfo[ccol+1 ] = it->second +1 ;
687701 /* row 3 - linear interpolation == 0 */
688702 dataInfo[ccol+2 ] = 0 ;
689703 /* row 4 - not defined outside of the defined time range == -1 */
@@ -696,13 +710,12 @@ static void generateDataInfo(simulation_result *self, DATA *data, threadData_t *
696710 for (size_t i = 0 ; i < (size_t )nParams; ++i) {
697711 /* col 1 - which table */
698712 dataInfo[ccol+4 *i] = 1 ;
699- /* col 2 - index of var in the table (first parameter has index 2 ) */
700- dataInfo[ccol+4 *i+1 ] = i+2 ;
713+ /* col 2 - index of var in the table (first parameter has index 1 ) */
714+ dataInfo[ccol+4 *i+1 ] = i+1 ;
701715 /* col 3 (== 0 <- interpolation doesn't matter here) */
702716 dataInfo[ccol+4 *i+2 ] = 0 ;
703717 /* col 4 - keep first/last value outside of time range */
704718 dataInfo[ccol+4 *i+3 ] = 0 ;
705-
706719 }
707720 /* ccol += mdl_data->nParameters*4; */
708721}
@@ -712,47 +725,47 @@ static void generateData_1(DATA *data, threadData_t *threadData, double* &data_1
712725 const SIMULATION_INFO *sInfo = data->simulationInfo ;
713726 const MODEL_DATA *mData = data->modelData ;
714727
715- int offset = 1 ;
728+ int offset = 0 ;
716729 long i = 0 ;
717730
718731 /* calculate number of rows and columns */
719- rows = 2 ;
720- cols = 1 + mData ->nParametersReal +
721- mData ->nParametersInteger +
722- mData ->nParametersBoolean ;
732+ cols = mData ->nParametersReal +
733+ mData ->nParametersInteger +
734+ mData ->nParametersBoolean ;
735+ if (cols) {
736+ rows = 1 ;
737+ } else {
738+ rows = 0 ;
739+ }
723740
724741 /* allocate data buffer */
725- data_1 = (double *)calloc (rows*cols, sizeof (double ));
726- assertStreamPrint (threadData, 0 !=data_1, " Malloc failed" );
727- data_1[0 ] = tstart; /* start time */
728- data_1[cols] = tstop; /* stop time */
742+ if (rows) {
743+ data_1 = (double *)calloc (rows*cols, sizeof (double ));
744+ assertStreamPrint (threadData, 0 !=data_1, " Malloc failed" );
745+ } else {
746+ data_1 = NULL ;
747+ }
729748
730749 /* double variables */
731- for (i = 0 ; i < mData ->nParametersReal ; ++i)
732- {
750+ for (i = 0 ; i < mData ->nParametersReal ; ++i) {
733751 if (!mData ->realParameterData [i].filterOutput ) {
734752 data_1[offset] = sInfo ->realParameter [i];
735- data_1[offset+cols] = sInfo ->realParameter [i];
736753 offset ++;
737754 }
738755 }
739756
740757 /* integer variables */
741- for (i = 0 ; i < mData ->nParametersInteger ; ++i)
742- {
758+ for (i = 0 ; i < mData ->nParametersInteger ; ++i) {
743759 if (!mData ->integerParameterData [i].filterOutput ) {
744760 data_1[offset] = (double )sInfo ->integerParameter [i];
745- data_1[offset+cols] = (double )sInfo ->integerParameter [i];
746761 offset ++;
747762 }
748763 }
749764
750765 /* bool variables */
751- for (i = 0 ; i < mData ->nParametersBoolean ; ++i)
752- {
766+ for (i = 0 ; i < mData ->nParametersBoolean ; ++i) {
753767 if (!mData ->booleanParameterData [i].filterOutput ) {
754768 data_1[offset] = (double )sInfo ->booleanParameter [i];
755- data_1[offset+cols] = (double )sInfo ->booleanParameter [i];
756769 offset ++;
757770 }
758771 }
0 commit comments