Skip to content

Commit 9a3c780

Browse files
Fix eventhandling for gbode (#14102)
1 parent cb3254b commit 9a3c780

File tree

1 file changed

+43
-70
lines changed
  • OMCompiler/SimulationRuntime/c/simulation/solver

1 file changed

+43
-70
lines changed

OMCompiler/SimulationRuntime/c/simulation/solver/gbode_main.c

Lines changed: 43 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,82 +1508,24 @@ int gbode_main(DATA *data, threadData_t *threadData, SOLVER_INFO *solverInfo)
15081508
messageClose(OMC_LOG_GBODE);
15091509
}
15101510

1511-
if (gbData->multi_rate) {
1512-
if (gbData->gbfData->time < gbData->time) {
1513-
eventTime = checkForEvents(data, threadData, solverInfo, gbData->time, gbData->yOld, gbData->time + gbData->lastStepSize, gbData->y, FALSE, &foundEvent);
1514-
if (foundEvent) {
1515-
solverInfo->currentTime = eventTime;
1516-
sData->timeValue = solverInfo->currentTime;
1517-
1518-
// sData->realVars are the "numerical" values on the right hand side of the event
1519-
gbData->time = eventTime;
1520-
memcpy(gbData->yOld, sData->realVars, nStates * sizeof(double));
1521-
1522-
gbData->gbfData->time = eventTime;
1523-
memcpy(gbData->gbfData->yOld, sData->realVars, nStates * sizeof(double));
1524-
1525-
/* write statistics to the solverInfo data structure */
1526-
memcpy(&solverInfo->solverStatsTmp, &gbData->stats, sizeof(SOLVERSTATS));
1527-
1528-
// log the emitted result
1529-
if (OMC_ACTIVE_STREAM(OMC_LOG_GBODE)){
1530-
infoStreamPrint(OMC_LOG_GBODE, 1, "Emit result (birate integration):");
1531-
printVector_gb(OMC_LOG_GBODE, " y", sData->realVars, nStates, sData->timeValue);
1532-
messageClose(OMC_LOG_GBODE);
1533-
}
1534-
1535-
if (OMC_ACTIVE_STREAM(OMC_LOG_GBODE_STATES)) {
1536-
// dump fast states in file
1537-
dumpFastStates_gb(gbData, TRUE, eventTime, 0);
1538-
}
1539-
1540-
// return to solver main routine for proper event handling (iteration)
1541-
messageClose(OMC_LOG_SOLVER);
1542-
return 0;
1543-
}
1544-
}
1545-
}
1546-
1547-
/* update time with performed stepSize */
1548-
gbData->time += gbData->lastStepSize;
1549-
gbData->timeDense = gbData->time;
1550-
1551-
/* step is accepted and yOld needs to be updated */
1552-
memcpy(gbData->yOld, gbData->y, nStates * sizeof(double));
1553-
1554-
// Rotate ring buffer
1555-
for (i = (gbData->ringBufferSize - 1); i > 0 ; i--) {
1556-
gbData->tv[i] = gbData->tv[i - 1];
1557-
memcpy(gbData->yv + i * nStates, gbData->yv + (i - 1) * nStates, nStates * sizeof(double));
1558-
memcpy(gbData->kv + i * nStates, gbData->kv + (i - 1) * nStates, nStates * sizeof(double));
1559-
}
1560-
1561-
// update new values
1562-
gbData->tv[0] = gbData->timeRight;
1563-
memcpy(gbData->yv, gbData->yRight, nStates * sizeof(double));
1564-
memcpy(gbData->kv, gbData->kRight, nStates * sizeof(double));
1565-
1566-
debugRingBufferSteps_gb(OMC_LOG_GBODE, gbData->yv, gbData->kv, gbData->tv, nStates, gbData->ringBufferSize);
1567-
1568-
if (gbData->multi_rate) {
1569-
infoStreamPrint(OMC_LOG_SOLVER, 0, "Accept step from %10g to %10g, error slow states %10g, error interpolation %10g, new stepsize %10g",
1570-
gbData->time - gbData->lastStepSize, gbData->time, err, gbData->err_int, gbData->stepSize);
1571-
if (OMC_ACTIVE_STREAM(OMC_LOG_GBODE_STATES)) {
1572-
// dump fast states in file
1573-
dumpFastStates_gb(gbData, FALSE, gbData->time, 0);
1574-
}
1575-
} else {
1511+
if (!gbData->multi_rate || (gbData->multi_rate && gbData->gbfData->time < gbData->time)) {
15761512
// check for events, if event is detected stop integrator and trigger event iteration
15771513
eventTime = checkForEvents(data, threadData, solverInfo, gbData->timeLeft, gbData->yLeft, gbData->timeRight, gbData->yRight, FALSE, &foundEvent);
15781514
if (foundEvent) {
1579-
if (eventTime < targetTime + solverInfo->currentStepSize/2) {
1515+
if (eventTime < targetTime + 10*GB_MINIMAL_STEP_SIZE) {
1516+
listClear(solverInfo->eventLst);
15801517
solverInfo->currentTime = eventTime;
15811518
sData->timeValue = eventTime;
15821519

15831520
// sData->realVars are the "numerical" values on the right hand side of the event (hopefully)
15841521
if (!gbData->noRestart) {
15851522
gbData->time = eventTime;
1523+
gbData->timeDense = eventTime;
1524+
gbData->timeRight = eventTime;
15861525
memcpy(gbData->yOld, sData->realVars, gbData->nStates * sizeof(double));
1526+
memcpy(gbData->yRight, sData->realVars, gbData->nStates * sizeof(double));
1527+
gbode_fODE(data, threadData, &(gbData->stats.nCallsODE));
1528+
memcpy(gbData->kRight, fODE, nStates * sizeof(double));
15871529
}
15881530

15891531
/* write statistics to the solverInfo data structure */
@@ -1604,26 +1546,57 @@ int gbode_main(DATA *data, threadData_t *threadData, SOLVER_INFO *solverInfo)
16041546
// Current solution: Step back to the communication interval before the event and event detection
16051547
// needs to be repeated
16061548
listClear(solverInfo->eventLst);
1607-
gbData->lastStepSize = (eventTime - solverInfo->currentStepSize/2) - gbData->timeLeft;
1608-
sData->timeValue = (eventTime - solverInfo->currentStepSize/2);
1549+
gbData->lastStepSize = (eventTime - 10*GB_MINIMAL_STEP_SIZE) - gbData->timeLeft;
1550+
sData->timeValue = (eventTime - 10*GB_MINIMAL_STEP_SIZE);
16091551
gb_interpolation(gbData->interpolation,
16101552
gbData->timeLeft, gbData->yLeft, gbData->kLeft,
16111553
gbData->timeRight, gbData->yRight, gbData->kRight,
16121554
sData->timeValue, sData->realVars,
16131555
nStates, NULL, nStates, gbData->tableau, gbData->x, gbData->k);
1614-
memcpy(gbData->yOld, sData->realVars, gbData->nStates * sizeof(double));
1556+
memcpy(gbData->y, sData->realVars, gbData->nStates * sizeof(double));
16151557
gbData->timeRight = sData->timeValue;
1616-
gbData->time = gbData->timeRight;
1558+
gbData->time = gbData->timeLeft;
16171559
memcpy(gbData->yRight, sData->realVars, gbData->nStates * sizeof(double));
16181560
gbode_fODE(data, threadData, &(gbData->stats.nCallsODE));
16191561
memcpy(gbData->kRight, fODE, nStates * sizeof(double));
16201562
}
16211563
}
1564+
}
16221565

1566+
if (gbData->multi_rate) {
1567+
infoStreamPrint(OMC_LOG_SOLVER, 0, "Accept step from %10g to %10g, error slow states %10g, error interpolation %10g, new stepsize %10g",
1568+
gbData->time - gbData->lastStepSize, gbData->time, err, gbData->err_int, gbData->stepSize);
1569+
if (OMC_ACTIVE_STREAM(OMC_LOG_GBODE_STATES)) {
1570+
// dump fast states in file
1571+
dumpFastStates_gb(gbData, FALSE, gbData->time, 0);
1572+
}
1573+
} else {
16231574
infoStreamPrint(OMC_LOG_SOLVER, 0, "Accept step from %10g to %10g, error %10g interpolation error %10g, new stepsize %10g",
16241575
gbData->timeLeft, gbData->timeRight, err, gbData->err_int, gbData->stepSize);
16251576

16261577
}
1578+
/* update time with performed stepSize */
1579+
1580+
gbData->time += gbData->lastStepSize;
1581+
gbData->timeDense = gbData->time;
1582+
1583+
/* step is accepted and yOld needs to be updated */
1584+
memcpy(gbData->yOld, gbData->y, nStates * sizeof(double));
1585+
1586+
// Rotate ring buffer
1587+
for (i = (gbData->ringBufferSize - 1); i > 0 ; i--) {
1588+
gbData->tv[i] = gbData->tv[i - 1];
1589+
memcpy(gbData->yv + i * nStates, gbData->yv + (i - 1) * nStates, nStates * sizeof(double));
1590+
memcpy(gbData->kv + i * nStates, gbData->kv + (i - 1) * nStates, nStates * sizeof(double));
1591+
}
1592+
1593+
// update new values
1594+
gbData->tv[0] = gbData->timeRight;
1595+
memcpy(gbData->yv, gbData->yRight, nStates * sizeof(double));
1596+
memcpy(gbData->kv, gbData->kRight, nStates * sizeof(double));
1597+
1598+
debugRingBufferSteps_gb(OMC_LOG_GBODE_V, gbData->yv, gbData->kv, gbData->tv, nStates, gbData->ringBufferSize);
1599+
16271600
/* emit step, if solverNoEquidistantGrid is selected */
16281601
if (solverInfo->solverNoEquidistantGrid && (gbData->multi_rate && gbData->gbfData->time<gbData->time)) {
16291602
sData->timeValue = gbData->time;

0 commit comments

Comments
 (0)