Changeset caa3fcca in OpenModelica


Ignore:
Timestamp:
2022-05-16T13:08:49+02:00 (2 years ago)
Author:
GitHub <noreply@…>
Branches:
maintenance/v1.20, maintenance/v1.21, maintenance/v1.22, maintenance/v1.23, master
Children:
2ad1387, 58677f97
Parents:
627bacf
git-author:
phannebohm <phannebohm@…> (05/16/22 13:08:49)
git-committer:
GitHub <noreply@…> (05/16/22 13:08:49)
Message:

[janitor] Cleanup events.c (#8956)

  • [janitor] Cleanup events.c
  • make tmpEventList truly static (no malloc)
  • stop returning confusing double from bisection
  • Fix array management
  • Modularize findRoot
  • suggested by bernhardbachmann
Files:
5 edited

Legend:

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

    r83be5f2b rcaa3fcca  
    5252
    5353int maxBisectionIterations = 0;
    54 double bisection(DATA* data, threadData_t *threadData, double*, double*, double*, double*, LIST*, LIST*);
     54void bisection(DATA* data, threadData_t *threadData, double*, double*, double*, double*, LIST*, LIST*);
    5555int checkZeroCrossings(DATA *data, LIST *list, LIST*);
    5656void saveZeroCrossingsAfterEvent(DATA *data, threadData_t *threadData);
    57 
    58 int checkForStateEvent(DATA* data, LIST *eventList);
    5957
    6058/*! \fn checkForSampleEvent
     
    151149  TRACE_PUSH
    152150
    153   if (checkForStateEvent(data, eventLst))
    154   {
    155     if (useRootFinding)
    156     {
    157       *eventTime = findRoot(data, threadData, eventLst);
    158     }
     151  int found = checkForStateEvent(data, eventLst);
     152  if(found && useRootFinding)
     153  {
     154    *eventTime = findRoot(data, threadData, eventLst, data->simulationInfo->timeValueOld, data->simulationInfo->realVarsOld, data->localData[0]->timeValue, data->localData[0]->realVars);
    159155  }
    160156
     
    289285 *  \param [ref] [threadData]
    290286 *  \param [ref] [eventList]
    291  *  \return: first event of interval [oldTime, timeValue]
    292  *
    293  *  This function perform a root finding for interval = [oldTime, timeValue]
    294  */
    295 double findRoot(DATA* data, threadData_t *threadData, LIST *eventList)
    296 {
    297   TRACE_PUSH
    298 
    299   double eventTime;
    300   long event_id;
     287 *  \param [in]  [time_left]
     288 *  \param [in]  [values_left]
     289 *  \param [in]  [time_right]
     290 *  \param [in]  [values_right]
     291 *  \return: first event of interval [time_left, time_right]
     292 */
     293double findRoot(DATA* data, threadData_t* threadData, LIST* eventList, double time_left, double* values_left, double time_right, double* values_right)
     294{
     295  TRACE_PUSH
     296
    301297  LIST_NODE* it;
    302298  fortran_integer i=0;
    303   static LIST *tmpEventList = NULL;
    304 
    305   double *states_right = (double*) malloc(data->modelData->nStates * sizeof(double));
    306   double *states_left = (double*) malloc(data->modelData->nStates * sizeof(double));
    307 
    308   double time_left = data->simulationInfo->timeValueOld;
    309   double time_right = data->localData[0]->timeValue;
    310 
    311   tmpEventList = allocList(sizeof(long));
    312 
    313   assert(states_right);
    314   assert(states_left);
     299  static LIST tmpEventList = (LIST){NULL, NULL, sizeof(long), 0};
     300
     301  /* static work arrays */
     302  static double *states_left = NULL;
     303  static double *states_right = NULL;
     304
     305  /* allocate memory once at first call, never free */
     306  if(!states_left)
     307  {
     308    states_left = (double*) malloc(data->modelData->nStates * sizeof(double));
     309    assertStreamPrint(NULL, NULL != states_left, "out of memory");
     310  }
     311  if(!states_right)
     312  {
     313    states_right = (double*) malloc(data->modelData->nStates * sizeof(double));
     314    assertStreamPrint(NULL, NULL != states_right, "out of memory");
     315  }
     316
     317  /* write states to work arrays */
     318  memcpy(states_left,  values_left,  data->modelData->nStates * sizeof(double));
     319  memcpy(states_right, values_right, data->modelData->nStates * sizeof(double));
    315320
    316321  for(it=listFirstNode(eventList); it; it=listNextNode(it))
     
    319324  }
    320325
    321   /* write states to work arrays */
    322   memcpy(states_left,  data->simulationInfo->realVarsOld, data->modelData->nStates * sizeof(double));
    323   memcpy(states_right, data->localData[0]->realVars    , data->modelData->nStates * sizeof(double));
    324 
    325326  /* Search for event time and event_id with bisection method */
    326   eventTime = bisection(data, threadData, &time_left, &time_right, states_left, states_right, tmpEventList, eventList);
    327 
    328   if(listLen(tmpEventList) == 0)
     327  bisection(data, threadData, &time_left, &time_right, states_left, states_right, &tmpEventList, eventList);
     328
     329  /* what happens here? */
     330  if(listLen(&tmpEventList) == 0)
    329331  {
    330332    double value = fabs(data->simulationInfo->zeroCrossings[*((long*) listFirstData(eventList))]);
     
    342344      if(value == fabs(data->simulationInfo->zeroCrossings[*((long*) listNodeData(it))]))
    343345      {
    344         listPushBack(tmpEventList, listNodeData(it));
     346        listPushBack(&tmpEventList, listNodeData(it));
    345347        infoStreamPrint(LOG_ZEROCROSSINGS, 0, "added tmp event : %ld", *((long*) listNodeData(it)));
    346348      }
     
    350352  listClear(eventList);
    351353
    352   if(ACTIVE_STREAM(LOG_EVENTS))
    353   {
    354     if(listLen(tmpEventList) > 0)
    355     {
    356       debugStreamPrint(LOG_EVENTS, 0, "found events: ");
    357     }
    358     else
    359     {
    360       debugStreamPrint(LOG_EVENTS, 0, "found event: ");
    361     }
    362   }
    363   while(listLen(tmpEventList) > 0)
    364   {
    365     event_id = *((long*)listFirstData(tmpEventList));
    366     listPopFront(tmpEventList);
    367 
    368     infoStreamPrint(LOG_ZEROCROSSINGS, 0, "Event id: %ld ", event_id);
    369 
     354  debugStreamPrint(LOG_EVENTS, 0, (listLen(&tmpEventList) == 1) ? "found event: " : "found events: ");
     355  while(listLen(&tmpEventList) > 0)
     356  {
     357    /* TODO do this directly w/o free-malloc */
     358    long event_id = *((long*)listFirstData(&tmpEventList));
     359    listPopFront(&tmpEventList);
    370360    listPushFront(eventList, &event_id);
    371   }
    372 
    373   eventTime = time_right;
    374   debugStreamPrint(LOG_EVENTS, 0, "time: %.10e", eventTime);
     361
     362    infoStreamPrint(LOG_ZEROCROSSINGS, 0, "Event id: %ld", event_id);
     363  }
     364
     365  debugStreamPrint(LOG_EVENTS, 0, "time: %.10e", time_right);
    375366
    376367  data->localData[0]->timeValue = time_left;
    377   for(i=0; i < data->modelData->nStates; i++) {
    378     data->localData[0]->realVars[i] = states_left[i];
    379   }
     368  memcpy(data->localData[0]->realVars, states_left, data->modelData->nStates * sizeof(double));
    380369
    381370  /* determined continuous system */
     
    384373  /*sim_result_emit(data);*/
    385374
    386   data->localData[0]->timeValue = eventTime;
    387   for(i=0; i < data->modelData->nStates; i++)
    388   {
    389     data->localData[0]->realVars[i] = states_right[i];
    390   }
    391 
    392   free(states_left);
    393   free(states_right);
    394 
    395   TRACE_POP
    396   return eventTime;
     375  data->localData[0]->timeValue = time_right;
     376  memcpy(data->localData[0]->realVars, states_right, data->modelData->nStates * sizeof(double));
     377
     378  TRACE_POP
     379  return time_right;
    397380}
    398381
     
    406389 *  \param [ref] [eventListTmp]
    407390 *  \param [in]  [eventList]
    408  *  \return Founded event time
    409391 *
    410392 *  Method to find root in interval [oldTime, timeValue]
    411393 */
    412 double bisection(DATA* data, threadData_t *threadData, double* a, double* b, double* states_a, double* states_b, LIST *tmpEventList, LIST *eventList)
     394void bisection(DATA* data, threadData_t *threadData, double* a, double* b, double* states_a, double* states_b, LIST *tmpEventList, LIST *eventList)
    413395{
    414396  TRACE_PUSH
     
    459441    }
    460442  }
    461   c = 0.5*(*a + *b);
    462 
    463   TRACE_POP
    464   return c;
     443
     444  TRACE_POP
    465445}
    466446
  • OMCompiler/SimulationRuntime/c/simulation/solver/events.h

    r83be5f2b rcaa3fcca  
    5050void handleEvents(DATA* data, threadData_t *threadData, LIST* eventLst, double *eventTime, SOLVER_INFO* solverInfo);
    5151
    52 double findRoot(DATA *data, threadData_t *threadData, LIST *eventList);
     52double findRoot(DATA* data, threadData_t* threadData, LIST* eventList, double time_left, double* states_left, double time_right, double* states_right);
    5353
    5454#ifdef __cplusplus
  • OMCompiler/SimulationRuntime/c/util/list.c

    r54883436 rcaa3fcca  
    4141#include <string.h>
    4242
    43 struct LIST_NODE
    44 {
    45   void *data;
    46   LIST_NODE *next;
    47 };
    48 
    49 struct LIST
    50 {
    51   LIST_NODE *first;
    52   LIST_NODE *last;
    53   unsigned int itemSize;
    54   unsigned int length;
    55 };
    5643
    5744LIST *allocList(unsigned int itemSize)
  • OMCompiler/SimulationRuntime/c/util/list.h

    r54883436 rcaa3fcca  
    4343
    4444  /* type-free list */
    45   struct LIST_NODE;
    46   typedef struct LIST_NODE LIST_NODE;
     45  typedef struct LIST_NODE
     46  {
     47    void *data;
     48    struct LIST_NODE *next;
     49  } LIST_NODE;
    4750
    48   struct LIST;
    49   typedef struct LIST LIST;
     51  typedef struct LIST
     52  {
     53    LIST_NODE *first;
     54    LIST_NODE *last;
     55    unsigned int itemSize;
     56    unsigned int length;
     57  } LIST;
    5058
    5159  LIST *allocList(unsigned int itemSize);
  • testsuite/simulation/modelica/algorithms_functions/ZeroCross.mos

    r221b299 rcaa3fcca  
    44// teardown_command: rm -rf ZeroCrossAlg* output.log *.conv.cpp
    55// cflags: -d=-newInst
    6 // 
     6//
    77//  Simulate model containing ZeroCrossing in for loop algorithm
    88//
Note: See TracChangeset for help on using the changeset viewer.