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

Commit

Permalink
Fix more initialization issues with FMUs
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Jan 10, 2019
1 parent 2f4f72e commit ef2fed9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 56 deletions.
Expand Up @@ -605,7 +605,8 @@ int initialization(DATA *data, threadData_t *threadData, const char* pInitMethod

infoStreamPrint(LOG_INIT, 0, "### START INITIALIZATION ###");

setAllParamsToStart(data);
if (strcmp(pInitMethod, "fmi"))
setAllParamsToStart(data);

#if !defined(OMC_MINIMAL_RUNTIME)
/* import start values from extern mat-file */
Expand All @@ -621,7 +622,8 @@ int initialization(DATA *data, threadData_t *threadData, const char* pInitMethod
}
#endif
/* set up all variables with their start-values */
setAllVarsToStart(data);
if (strcmp(pInitMethod, "fmi"))
setAllVarsToStart(data);

if(!(pInitFile && strcmp(pInitFile, ""))) {
data->callback->updateBoundParameters(data, threadData);
Expand All @@ -633,7 +635,7 @@ int initialization(DATA *data, threadData_t *threadData, const char* pInitMethod
updateStaticDataOfNonlinearSystems(data, threadData);

/* if there are user-specified options, use them! */
if (pInitMethod && strcmp(pInitMethod, "")) {
if (pInitMethod && (strcmp(pInitMethod, "") && strcmp(pInitMethod, "fmi"))) {
initMethod = IIM_UNKNOWN;

for (i=1; i<IIM_MAX; ++i) {
Expand Down
108 changes: 55 additions & 53 deletions SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.c
Expand Up @@ -561,6 +561,22 @@ fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fm
}

fmi2Status fmi2EnterInitializationMode(fmi2Component c)
{
ModelInstance *comp = (ModelInstance *)c;

if (invalidState(comp, "fmi2EnterInitializationMode", modelInstantiated, ~0))
return fmi2Error;
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode...")

setZCtol(comp->tolerance); /* set zero-crossing tolerance */
setStartValues(comp);
copyStartValuestoInitValues(comp->fmuData);
comp->state = modelInitializationMode;

return fmi2OK;
}

fmi2Status fmi2ExitInitializationMode(fmi2Component c)
{
fmi2Status res = fmi2Error;
ModelInstance *comp = (ModelInstance *)c;
Expand All @@ -570,81 +586,67 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c)
int done=0;

threadData->currentErrorStage = ERROR_SIMULATION;
if (invalidState(comp, "fmi2EnterInitializationMode", modelInstantiated, ~0))
if (invalidState(comp, "fmi2ExitInitializationMode", modelInitializationMode, ~0))
return fmi2Error;
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2ExitInitializationMode...")

setThreadData(comp);

FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode...")
/* set zero-crossing tolerance */
setZCtol(comp->tolerance);

setStartValues(comp);
copyStartValuestoInitValues(comp->fmuData);
comp->fmuData->callback->updateBoundParameters(comp->fmuData, comp->threadData);
comp->fmuData->callback->updateBoundVariableAttributes(comp->fmuData, comp->threadData);

/* try */
MMC_TRY_INTERNAL(simulationJumpBuffer)
threadData->mmc_jumper = threadData->simulationJumpBuffer;
threadData->mmc_jumper = threadData->simulationJumpBuffer;

if (initialization(comp->fmuData, comp->threadData, "", "", 0.0)) {
comp->state = modelError;
FILTERED_LOG(comp, fmi2Error, LOG_FMI2_CALL, "fmi2EnterInitializationMode: failed")
}
else
{
/*TODO: Simulation stop time is need to calculate in before hand all sample events
We shouldn't generate them all in beforehand */
initSample(comp->fmuData, comp->threadData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/);
if (initialization(comp->fmuData, comp->threadData, "fmi", "", 0.0))
{
comp->state = modelError;
FILTERED_LOG(comp, fmi2Error, LOG_FMI2_CALL, "fmi2EnterInitializationMode: failed")
}
else
{
/* TODO: Simulation stop time is needed to calculate the sample events beforehand. */
initSample(comp->fmuData, comp->threadData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/);
#if !defined(OMC_NDELAY_EXPRESSIONS) || OMC_NDELAY_EXPRESSIONS>0
initDelay(comp->fmuData, comp->fmuData->localData[0]->timeValue);
initDelay(comp->fmuData, comp->fmuData->localData[0]->timeValue);
#endif
/* due to an event overwrite old values */
overwriteOldSimulationData(comp->fmuData);
/* overwrite old values due to an event */
overwriteOldSimulationData(comp->fmuData);

comp->eventInfo.terminateSimulation = fmi2False;
comp->eventInfo.valuesOfContinuousStatesChanged = fmi2True;

/* Get next event time (sample calls)*/
nextSampleEvent = 0;
nextSampleEvent = getNextSampleTimeFMU(comp->fmuData);
if (nextSampleEvent == -1) {
comp->eventInfo.nextEventTimeDefined = fmi2False;
} else {
comp->eventInfo.nextEventTimeDefined = fmi2True;
comp->eventInfo.nextEventTime = nextSampleEvent;
fmi2EventUpdate(comp, &(comp->eventInfo));
}
comp->state = modelInitializationMode;
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode: succeed")
res = fmi2OK;
comp->eventInfo.terminateSimulation = fmi2False;
comp->eventInfo.valuesOfContinuousStatesChanged = fmi2True;

/* get next event time (sample calls) */
nextSampleEvent = 0;
nextSampleEvent = getNextSampleTimeFMU(comp->fmuData);
if (nextSampleEvent == -1)
{
comp->eventInfo.nextEventTimeDefined = fmi2False;
}
else
{
comp->eventInfo.nextEventTimeDefined = fmi2True;
comp->eventInfo.nextEventTime = nextSampleEvent;
fmi2EventUpdate(comp, &(comp->eventInfo));
}
done = 1;
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode: succeed")
res = fmi2OK;
}
done = 1;
/* catch */
MMC_CATCH_INTERNAL(simulationJumpBuffer)
threadData->mmc_jumper = old_jmp;

if (!done) {
if (!done)
{
FILTERED_LOG(comp, fmi2Error, LOG_FMI2_CALL, "fmi2EnterInitializationMode: terminated by an assertion.")
}
resetThreadData(comp);
return res;
}

fmi2Status fmi2ExitInitializationMode(fmi2Component c)
{
ModelInstance *comp = (ModelInstance *)c;
if (invalidState(comp, "fmi2ExitInitializationMode", modelInitializationMode, ~0))
return fmi2Error;
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2ExitInitializationMode...")

setThreadData(comp);
comp->fmuData->callback->updateBoundParameters(comp->fmuData, comp->threadData);
comp->fmuData->callback->updateBoundVariableAttributes(comp->fmuData, comp->threadData);
comp->state = modelEventMode;
resetThreadData(comp);

FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2ExitInitializationMode: succeed")
return fmi2OK;
return res;
}

/*
Expand Down

0 comments on commit ef2fed9

Please sign in to comment.