@@ -371,7 +371,14 @@ function generateFmi3FunctionsC(
371371 L . push ( " fmi3Float64 startTime;" ) ;
372372 L . push ( " fmi3Float64 stopTime;" ) ;
373373 L . push ( " fmi3Float64 stepSize;" ) ;
374+ L . push ( " fmi3Boolean eventModeUsed;" ) ;
375+ L . push ( " fmi3Boolean earlyReturnAllowed;" ) ;
376+ L . push ( " int state; /* 0=INIT, 1=STEP, 2=EVENT, 3=TERMINATED */" ) ;
374377 L . push ( "} FMU3Instance;" ) ;
378+ L . push ( "#define FMU3_STATE_INIT 0" ) ;
379+ L . push ( "#define FMU3_STATE_STEP 1" ) ;
380+ L . push ( "#define FMU3_STATE_EVENT 2" ) ;
381+ L . push ( "#define FMU3_STATE_TERM 3" ) ;
375382 L . push ( "" ) ;
376383 L . push ( "/* Variable size lookup table for array batching (FMI 3.0 §2.2.7) */" ) ;
377384 L . push ( "static const size_t varSizes[N_VARS + 1] = {" ) ;
@@ -402,7 +409,6 @@ function generateFmi3FunctionsC(
402409 L . push ( " fmi3InstanceEnvironment instanceEnvironment, fmi3LogMessageCallback logMessage," ) ;
403410 L . push ( " fmi3IntermediateUpdateCallback intermediateUpdate) {" ) ;
404411 L . push ( " (void)instantiationToken; (void)resourcePath; (void)visible;" ) ;
405- L . push ( " (void)eventModeUsed; (void)earlyReturnAllowed;" ) ;
406412 L . push ( " (void)requiredIntermediateVariables; (void)nRequiredIntermediateVariables;" ) ;
407413 L . push ( " FMU3Instance* inst = (FMU3Instance*)calloc(1, sizeof(FMU3Instance));" ) ;
408414 L . push ( " if (!inst) return NULL;" ) ;
@@ -412,6 +418,9 @@ function generateFmi3FunctionsC(
412418 L . push ( " inst->instanceEnvironment = instanceEnvironment;" ) ;
413419 L . push ( " inst->loggingOn = loggingOn;" ) ;
414420 L . push ( " inst->stepSize = 0.001;" ) ;
421+ L . push ( " inst->eventModeUsed = eventModeUsed;" ) ;
422+ L . push ( " inst->earlyReturnAllowed = earlyReturnAllowed;" ) ;
423+ L . push ( " inst->state = FMU3_STATE_INIT;" ) ;
415424 L . push ( ` ${ id } _initialize(&inst->model);` ) ;
416425 L . push ( " return (fmi3Instance)inst;" ) ;
417426 L . push ( "}" ) ;
@@ -444,10 +453,16 @@ function generateFmi3FunctionsC(
444453 L . push ( " if (stopTimeDefined) inst->stopTime = stopTime;" ) ;
445454 L . push ( " return fmi3OK;" ) ;
446455 L . push ( "}" ) ;
447- L . push ( "fmi3Status fmi3ExitInitializationMode(fmi3Instance instance) { (void)instance; return fmi3OK; }" ) ;
448- L . push ( "fmi3Status fmi3Terminate(fmi3Instance instance) { (void)instance; return fmi3OK; }" ) ;
456+ L . push (
457+ "fmi3Status fmi3ExitInitializationMode(fmi3Instance instance) { ((FMU3Instance*)instance)->state = FMU3_STATE_STEP; return fmi3OK; }" ,
458+ ) ;
459+ L . push (
460+ "fmi3Status fmi3Terminate(fmi3Instance instance) { ((FMU3Instance*)instance)->state = FMU3_STATE_TERM; return fmi3OK; }" ,
461+ ) ;
449462 L . push ( "void fmi3FreeInstance(fmi3Instance instance) { if (instance) free(instance); }" ) ;
450- L . push ( "fmi3Status fmi3Reset(fmi3Instance instance) { (void)instance; return fmi3OK; }" ) ;
463+ L . push (
464+ "fmi3Status fmi3Reset(fmi3Instance instance) { ((FMU3Instance*)instance)->state = FMU3_STATE_INIT; return fmi3OK; }" ,
465+ ) ;
451466 L . push ( "" ) ;
452467
453468 const numericTypes = [ "Float32" , "Float64" , "Int8" , "UInt8" , "Int16" , "UInt16" , "Int32" , "UInt32" , "Int64" , "UInt64" ] ;
@@ -710,6 +725,20 @@ function generateFmi3FunctionsC(
710725 ) ;
711726 L . push ( " if (canReturnEarly) { *earlyReturn = fmi3True; *lastSuccessfulTime = t; return fmi3OK; }" ) ;
712727 L . push ( " }" ) ;
728+ L . push ( " /* FMI 3.0: CS Event Mode — check event indicators for zero crossings */" ) ;
729+ L . push ( " if (inst->eventModeUsed) {" ) ;
730+ L . push ( " double ei[N_EVENT_INDICATORS + 1];" ) ;
731+ L . push ( ` ${ id } _getEventIndicators(&inst->model, ei);` ) ;
732+ L . push ( " for (int k = 0; k < N_EVENT_INDICATORS; k++) {" ) ;
733+ L . push ( " if ((inst->model.eventPrev[k] <= 0 && ei[k] > 0) || (inst->model.eventPrev[k] >= 0 && ei[k] < 0)) {" ) ;
734+ L . push ( " *eventHandlingNeeded = fmi3True;" ) ;
735+ L . push ( " if (inst->earlyReturnAllowed) { *earlyReturn = fmi3True; *lastSuccessfulTime = t; }" ) ;
736+ L . push ( " break;" ) ;
737+ L . push ( " }" ) ;
738+ L . push ( " }" ) ;
739+ L . push ( " for (int k = 0; k < N_EVENT_INDICATORS; k++) inst->model.eventPrev[k] = ei[k];" ) ;
740+ L . push ( " if (*earlyReturn) return fmi3OK;" ) ;
741+ L . push ( " }" ) ;
713742 L . push ( " }" ) ;
714743 L . push ( " inst->model.time = tEnd; *lastSuccessfulTime = tEnd;" ) ;
715744 L . push ( " return fmi3OK;" ) ;
@@ -718,9 +747,13 @@ function generateFmi3FunctionsC(
718747
719748 // Remaining stubs
720749 L . push ( "/* --- Stubs --- */" ) ;
721- L . push ( "fmi3Status fmi3EnterEventMode(fmi3Instance instance) { (void)instance; return fmi3OK; }" ) ;
750+ L . push (
751+ "fmi3Status fmi3EnterEventMode(fmi3Instance instance) { ((FMU3Instance*)instance)->state = FMU3_STATE_EVENT; return fmi3OK; }" ,
752+ ) ;
722753 L . push ( "fmi3Status fmi3EnterContinuousTimeMode(fmi3Instance instance) { (void)instance; return fmi3OK; }" ) ;
723- L . push ( "fmi3Status fmi3EnterStepMode(fmi3Instance instance) { (void)instance; return fmi3OK; }" ) ;
754+ L . push (
755+ "fmi3Status fmi3EnterStepMode(fmi3Instance instance) { ((FMU3Instance*)instance)->state = FMU3_STATE_STEP; return fmi3OK; }" ,
756+ ) ;
724757 L . push (
725758 "fmi3Status fmi3CompletedIntegratorStep(fmi3Instance instance, fmi3Boolean noSetFMUStatePriorToCurrentPoint, fmi3Boolean* enterEventMode, fmi3Boolean* terminateSimulation) { (void)instance; (void)noSetFMUStatePriorToCurrentPoint; *enterEventMode = fmi3False; *terminateSimulation = fmi3False; return fmi3OK; }" ,
726759 ) ;
0 commit comments