Changeset caa3fcca in OpenModelica
- Timestamp:
- 2022-05-16T13:08:49+02:00 (2 years ago)
- 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)
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
OMCompiler/SimulationRuntime/c/simulation/solver/events.c
r83be5f2b rcaa3fcca 52 52 53 53 int maxBisectionIterations = 0; 54 doublebisection(DATA* data, threadData_t *threadData, double*, double*, double*, double*, LIST*, LIST*);54 void bisection(DATA* data, threadData_t *threadData, double*, double*, double*, double*, LIST*, LIST*); 55 55 int checkZeroCrossings(DATA *data, LIST *list, LIST*); 56 56 void saveZeroCrossingsAfterEvent(DATA *data, threadData_t *threadData); 57 58 int checkForStateEvent(DATA* data, LIST *eventList);59 57 60 58 /*! \fn checkForSampleEvent … … 151 149 TRACE_PUSH 152 150 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); 159 155 } 160 156 … … 289 285 * \param [ref] [threadData] 290 286 * \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 */ 293 double 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 301 297 LIST_NODE* it; 302 298 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)); 315 320 316 321 for(it=listFirstNode(eventList); it; it=listNextNode(it)) … … 319 324 } 320 325 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 325 326 /* 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) 329 331 { 330 332 double value = fabs(data->simulationInfo->zeroCrossings[*((long*) listFirstData(eventList))]); … … 342 344 if(value == fabs(data->simulationInfo->zeroCrossings[*((long*) listNodeData(it))])) 343 345 { 344 listPushBack( tmpEventList, listNodeData(it));346 listPushBack(&tmpEventList, listNodeData(it)); 345 347 infoStreamPrint(LOG_ZEROCROSSINGS, 0, "added tmp event : %ld", *((long*) listNodeData(it))); 346 348 } … … 350 352 listClear(eventList); 351 353 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); 370 360 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); 375 366 376 367 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)); 380 369 381 370 /* determined continuous system */ … … 384 373 /*sim_result_emit(data);*/ 385 374 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; 397 380 } 398 381 … … 406 389 * \param [ref] [eventListTmp] 407 390 * \param [in] [eventList] 408 * \return Founded event time409 391 * 410 392 * Method to find root in interval [oldTime, timeValue] 411 393 */ 412 doublebisection(DATA* data, threadData_t *threadData, double* a, double* b, double* states_a, double* states_b, LIST *tmpEventList, LIST *eventList)394 void bisection(DATA* data, threadData_t *threadData, double* a, double* b, double* states_a, double* states_b, LIST *tmpEventList, LIST *eventList) 413 395 { 414 396 TRACE_PUSH … … 459 441 } 460 442 } 461 c = 0.5*(*a + *b); 462 463 TRACE_POP 464 return c; 443 444 TRACE_POP 465 445 } 466 446 -
OMCompiler/SimulationRuntime/c/simulation/solver/events.h
r83be5f2b rcaa3fcca 50 50 void handleEvents(DATA* data, threadData_t *threadData, LIST* eventLst, double *eventTime, SOLVER_INFO* solverInfo); 51 51 52 double findRoot(DATA *data, threadData_t *threadData, LIST *eventList);52 double findRoot(DATA* data, threadData_t* threadData, LIST* eventList, double time_left, double* states_left, double time_right, double* states_right); 53 53 54 54 #ifdef __cplusplus -
OMCompiler/SimulationRuntime/c/util/list.c
r54883436 rcaa3fcca 41 41 #include <string.h> 42 42 43 struct LIST_NODE44 {45 void *data;46 LIST_NODE *next;47 };48 49 struct LIST50 {51 LIST_NODE *first;52 LIST_NODE *last;53 unsigned int itemSize;54 unsigned int length;55 };56 43 57 44 LIST *allocList(unsigned int itemSize) -
OMCompiler/SimulationRuntime/c/util/list.h
r54883436 rcaa3fcca 43 43 44 44 /* 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; 47 50 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; 50 58 51 59 LIST *allocList(unsigned int itemSize); -
testsuite/simulation/modelica/algorithms_functions/ZeroCross.mos
r221b299 rcaa3fcca 4 4 // teardown_command: rm -rf ZeroCrossAlg* output.log *.conv.cpp 5 5 // cflags: -d=-newInst 6 // 6 // 7 7 // Simulate model containing ZeroCrossing in for loop algorithm 8 8 //
Note: See TracChangeset
for help on using the changeset viewer.