Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Handle errors during FMI initialization #1110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SimulationRuntime/c/util/omc_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ void debugStreamPrintWithEquationIndexes(int stream, int indentNext, const int *

static inline jmp_buf* getBestJumpBuffer(threadData_t *threadData)
{
if (!threadData) {
fprintf(stderr, "OpenModelica threadData is NULL; this should not be possible");
abort();
}
switch (threadData->currentErrorStage) {
case ERROR_EVENTSEARCH:
case ERROR_SIMULATION:
Expand Down
15 changes: 12 additions & 3 deletions SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
fmi2Boolean visible, fmi2Boolean loggingOn) {
// ignoring arguments: fmuResourceLocation, visible
ModelInstance *comp;
int fmi2callbacksOK = 0;
threadData_t *threadData = NULL;
if (!functions->logger) {
return NULL;
}
Expand All @@ -336,9 +338,8 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
comp = (ModelInstance *)functions->allocateMemory(1, sizeof(ModelInstance));
if (comp) {
DATA* fmudata = NULL;
MODEL_DATA* modelData = NULL;
SIMULATION_INFO* simInfo = NULL;
threadData_t *threadData = NULL;
MODEL_DATA* modelData = NULL;
SIMULATION_INFO* simInfo = NULL;
int i;

comp->instanceName = (fmi2String)functions->allocateMemory(1 + strlen(instanceName), sizeof(char));
Expand Down Expand Up @@ -393,6 +394,9 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
#endif
/* read input vars */
/* input_function(comp->fmuData); */
MMC_INIT(X);
pthread_setspecific(mmc_thread_data_key, threadData);
MMC_TRY_INTERNAL(mmc_jumper)
#if !defined(OMC_NUM_NONLINEAR_SYSTEMS) || OMC_NUM_NONLINEAR_SYSTEMS>0
/* allocate memory for non-linear system solvers */
initializeNonlinearSystems(comp->fmuData, comp->threadData);
Expand All @@ -409,6 +413,11 @@ fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2Str
/* allocate memory for state selection */
initializeStateSetJacobians(comp->fmuData, comp->threadData);
#endif
fmi2callbacksOK = 1;
MMC_CATCH_INTERNAL(mmc_jumper)
if (!fmi2callbacksOK) {
return NULL;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit strange. The if condition will always fail since fmi2callbacksOK is set to 1 unless you are doing something in MMC_CATCH_INTERNAL which is of course not the case since fmi2callbacksOK is local.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not weird. It's setjmp/longjmp. The assignment to fmi2callbacksOK doesn't occur if an error is triggered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it's a bit wrong since it returns before restoring the jump buffer. But a bigger problem is that when OM imports the FMU, we use thread-local storage for the threadData and we need to have separate threadData for the importing model... This seems to be what causes the crashes. Maybe we need to make the simulation check for existing threadData and re-use that...

#ifdef FMU_EXPERIMENTAL
/* allocate memory for Jacobian */
comp->_has_jacobian = !comp->fmuData->callback->initialAnalyticJacobianA(comp->fmuData, comp->threadData);
Expand Down