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

Commit ef2fed9

Browse files
lochelOpenModelica-Hudson
authored andcommitted
Fix more initialization issues with FMUs
Belonging to [master]: - #2859 - OpenModelica/OpenModelica-testsuite#1099
1 parent 2f4f72e commit ef2fed9

2 files changed

Lines changed: 60 additions & 56 deletions

File tree

SimulationRuntime/c/simulation/solver/initialization/initialization.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ int initialization(DATA *data, threadData_t *threadData, const char* pInitMethod
605605

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

608-
setAllParamsToStart(data);
608+
if (strcmp(pInitMethod, "fmi"))
609+
setAllParamsToStart(data);
609610

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

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

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

639641
for (i=1; i<IIM_MAX; ++i) {

SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.c

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,22 @@ fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fm
561561
}
562562

563563
fmi2Status fmi2EnterInitializationMode(fmi2Component c)
564+
{
565+
ModelInstance *comp = (ModelInstance *)c;
566+
567+
if (invalidState(comp, "fmi2EnterInitializationMode", modelInstantiated, ~0))
568+
return fmi2Error;
569+
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode...")
570+
571+
setZCtol(comp->tolerance); /* set zero-crossing tolerance */
572+
setStartValues(comp);
573+
copyStartValuestoInitValues(comp->fmuData);
574+
comp->state = modelInitializationMode;
575+
576+
return fmi2OK;
577+
}
578+
579+
fmi2Status fmi2ExitInitializationMode(fmi2Component c)
564580
{
565581
fmi2Status res = fmi2Error;
566582
ModelInstance *comp = (ModelInstance *)c;
@@ -570,81 +586,67 @@ fmi2Status fmi2EnterInitializationMode(fmi2Component c)
570586
int done=0;
571587

572588
threadData->currentErrorStage = ERROR_SIMULATION;
573-
if (invalidState(comp, "fmi2EnterInitializationMode", modelInstantiated, ~0))
589+
if (invalidState(comp, "fmi2ExitInitializationMode", modelInitializationMode, ~0))
574590
return fmi2Error;
591+
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2ExitInitializationMode...")
575592

576593
setThreadData(comp);
577-
578-
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode...")
579-
/* set zero-crossing tolerance */
580-
setZCtol(comp->tolerance);
581-
582-
setStartValues(comp);
583-
copyStartValuestoInitValues(comp->fmuData);
594+
comp->fmuData->callback->updateBoundParameters(comp->fmuData, comp->threadData);
595+
comp->fmuData->callback->updateBoundVariableAttributes(comp->fmuData, comp->threadData);
584596

585597
/* try */
586598
MMC_TRY_INTERNAL(simulationJumpBuffer)
587-
threadData->mmc_jumper = threadData->simulationJumpBuffer;
599+
threadData->mmc_jumper = threadData->simulationJumpBuffer;
588600

589-
if (initialization(comp->fmuData, comp->threadData, "", "", 0.0)) {
590-
comp->state = modelError;
591-
FILTERED_LOG(comp, fmi2Error, LOG_FMI2_CALL, "fmi2EnterInitializationMode: failed")
592-
}
593-
else
594-
{
595-
/*TODO: Simulation stop time is need to calculate in before hand all sample events
596-
We shouldn't generate them all in beforehand */
597-
initSample(comp->fmuData, comp->threadData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/);
601+
if (initialization(comp->fmuData, comp->threadData, "fmi", "", 0.0))
602+
{
603+
comp->state = modelError;
604+
FILTERED_LOG(comp, fmi2Error, LOG_FMI2_CALL, "fmi2EnterInitializationMode: failed")
605+
}
606+
else
607+
{
608+
/* TODO: Simulation stop time is needed to calculate the sample events beforehand. */
609+
initSample(comp->fmuData, comp->threadData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/);
598610
#if !defined(OMC_NDELAY_EXPRESSIONS) || OMC_NDELAY_EXPRESSIONS>0
599-
initDelay(comp->fmuData, comp->fmuData->localData[0]->timeValue);
611+
initDelay(comp->fmuData, comp->fmuData->localData[0]->timeValue);
600612
#endif
601-
/* due to an event overwrite old values */
602-
overwriteOldSimulationData(comp->fmuData);
613+
/* overwrite old values due to an event */
614+
overwriteOldSimulationData(comp->fmuData);
603615

604-
comp->eventInfo.terminateSimulation = fmi2False;
605-
comp->eventInfo.valuesOfContinuousStatesChanged = fmi2True;
606-
607-
/* Get next event time (sample calls)*/
608-
nextSampleEvent = 0;
609-
nextSampleEvent = getNextSampleTimeFMU(comp->fmuData);
610-
if (nextSampleEvent == -1) {
611-
comp->eventInfo.nextEventTimeDefined = fmi2False;
612-
} else {
613-
comp->eventInfo.nextEventTimeDefined = fmi2True;
614-
comp->eventInfo.nextEventTime = nextSampleEvent;
615-
fmi2EventUpdate(comp, &(comp->eventInfo));
616-
}
617-
comp->state = modelInitializationMode;
618-
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode: succeed")
619-
res = fmi2OK;
616+
comp->eventInfo.terminateSimulation = fmi2False;
617+
comp->eventInfo.valuesOfContinuousStatesChanged = fmi2True;
618+
619+
/* get next event time (sample calls) */
620+
nextSampleEvent = 0;
621+
nextSampleEvent = getNextSampleTimeFMU(comp->fmuData);
622+
if (nextSampleEvent == -1)
623+
{
624+
comp->eventInfo.nextEventTimeDefined = fmi2False;
625+
}
626+
else
627+
{
628+
comp->eventInfo.nextEventTimeDefined = fmi2True;
629+
comp->eventInfo.nextEventTime = nextSampleEvent;
630+
fmi2EventUpdate(comp, &(comp->eventInfo));
620631
}
621-
done = 1;
632+
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2EnterInitializationMode: succeed")
633+
res = fmi2OK;
634+
}
635+
done = 1;
622636
/* catch */
623637
MMC_CATCH_INTERNAL(simulationJumpBuffer)
624638
threadData->mmc_jumper = old_jmp;
625639

626-
if (!done) {
640+
if (!done)
641+
{
627642
FILTERED_LOG(comp, fmi2Error, LOG_FMI2_CALL, "fmi2EnterInitializationMode: terminated by an assertion.")
628643
}
629-
resetThreadData(comp);
630-
return res;
631-
}
632-
633-
fmi2Status fmi2ExitInitializationMode(fmi2Component c)
634-
{
635-
ModelInstance *comp = (ModelInstance *)c;
636-
if (invalidState(comp, "fmi2ExitInitializationMode", modelInitializationMode, ~0))
637-
return fmi2Error;
638-
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2ExitInitializationMode...")
639644

640-
setThreadData(comp);
641-
comp->fmuData->callback->updateBoundParameters(comp->fmuData, comp->threadData);
642-
comp->fmuData->callback->updateBoundVariableAttributes(comp->fmuData, comp->threadData);
643645
comp->state = modelEventMode;
644646
resetThreadData(comp);
645647

646648
FILTERED_LOG(comp, fmi2OK, LOG_FMI2_CALL, "fmi2ExitInitializationMode: succeed")
647-
return fmi2OK;
649+
return res;
648650
}
649651

650652
/*

0 commit comments

Comments
 (0)