Changeset e1dbd879 in OpenModelica


Ignore:
Timestamp:
2022-05-16T11:15:56+02:00 (2 years ago)
Author:
Philip Hannebohm <phannebohm@…>
Children:
14ac561
Parents:
5b747e9
git-author:
Philip Hannebohm <phannebohm@…> (05/16/22 10:46:52)
git-committer:
Philip Hannebohm <phannebohm@…> (05/16/22 11:15:56)
Message:

Fix array management

File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/SimulationRuntime/c/simulation/solver/events.c

    r5b747e9 re1dbd879  
    299299  static LIST tmpEventList = (LIST){NULL, NULL, sizeof(long), 0};
    300300
    301   /* FIXME stop allocating all the time */
    302   double *states_right = (double*) malloc(data->modelData->nStates * sizeof(double));
    303   double *states_left = (double*) malloc(data->modelData->nStates * sizeof(double));
     301  /* static work arrays */
     302  static double *states_left = NULL;
     303  static double *states_right = NULL;
    304304
    305305  double time_left = data->simulationInfo->timeValueOld;
    306306  double time_right = data->localData[0]->timeValue;
    307307
    308   assert(states_right);
    309   assert(states_left);
    310 
    311   for(it=listFirstNode(eventList); it; it=listNextNode(it))
    312   {
    313     infoStreamPrint(LOG_ZEROCROSSINGS, 0, "search for current event. Events in list: %ld", *((long*)listNodeData(it)));
     308  /* allocate memory once at first call, never free */
     309  if(!states_left)
     310  {
     311    states_left = (double*) malloc(data->modelData->nStates * sizeof(double));
     312    assertStreamPrint(NULL, NULL != states_left, "out of memory");
     313  }
     314  if(!states_right)
     315  {
     316    states_right = (double*) malloc(data->modelData->nStates * sizeof(double));
     317    assertStreamPrint(NULL, NULL != states_right, "out of memory");
    314318  }
    315319
     
    317321  memcpy(states_left,  data->simulationInfo->realVarsOld, data->modelData->nStates * sizeof(double));
    318322  memcpy(states_right, data->localData[0]->realVars     , data->modelData->nStates * sizeof(double));
     323
     324  for(it=listFirstNode(eventList); it; it=listNextNode(it))
     325  {
     326    infoStreamPrint(LOG_ZEROCROSSINGS, 0, "search for current event. Events in list: %ld", *((long*)listNodeData(it)));
     327  }
    319328
    320329  /* Search for event time and event_id with bisection method */
     
    349358  while(listLen(&tmpEventList) > 0)
    350359  {
     360    /* TODO do this directly w/o free-malloc */
    351361    long event_id = *((long*)listFirstData(&tmpEventList));
    352362    listPopFront(&tmpEventList);
     
    359369
    360370  data->localData[0]->timeValue = time_left;
    361   for(i=0; i < data->modelData->nStates; i++) {
    362     data->localData[0]->realVars[i] = states_left[i];
    363   }
     371  memcpy(data->localData[0]->realVars, states_left, data->modelData->nStates * sizeof(double));
    364372
    365373  /* determined continuous system */
     
    369377
    370378  data->localData[0]->timeValue = time_right;
    371   for(i=0; i < data->modelData->nStates; i++)
    372   {
    373     data->localData[0]->realVars[i] = states_right[i];
    374   }
    375 
    376   free(states_left);
    377   free(states_right);
     379  memcpy(data->localData[0]->realVars, states_right, data->modelData->nStates * sizeof(double));
    378380
    379381  TRACE_POP
Note: See TracChangeset for help on using the changeset viewer.