Long ago, I played a little bit with FMU which is like a black box used for simulations in the context of Simulink, Dymola and many others.
An FMU is a zip file with:
In order to create the library, you create a three functions with a particular name. For example, something like:
proc setStartValues(comp:ptr ModelInstance) {.exportc: "$1".} =
comp.i[counter] = 1
proc calculateValues(comp:ptr ModelInstance) {.exportc: "$1".}=
if comp.state == modelInitializationMode:
# set first time event
comp.eventInfo.nextEventTimeDefined = fmi2True
comp.eventInfo.nextEventTime = 1 + comp.time
proc eventUpdate(comp:ptr ModelInstance, eventInfo:ptr fmi2EventInfo, timeEvent:cint, isNewEventIteration:cint) {.exportc: "$1".} =
if timeEvent != 0:
comp.i[counter] += 1;
if comp.i[counter] == 13:
eventInfo.terminateSimulation = fmi2True
eventInfo.nextEventTimeDefined = fmi2False
else:
eventInfo.nextEventTimeDefined = fmi2True
eventInfo.nextEventTime = 1 + comp.time
In order to create the .so, I used FFI with fmusdk which is in C. You create the .so by compiling the inc.nim code like:
nim c --nimcache:.cache --app:lib -o:inc.so inc.nim
For me, the easy way would be having a file with those three functions, and another program to create and package everything up. This approach is what it is done by PythonFMU.
I was wondering if I could do a export_fmu function that operates like this:
proc setStartValues(comp:ptr ModelInstance) {.exportc: "$1".} =
...
proc calculateValues(comp:ptr ModelInstance) {.exportc: "$1".}=
...
proc eventUpdate(comp:ptr ModelInstance, eventInfo:ptr fmi2EventInfo, timeEvent:cint, isNewEventIteration:cint) {.exportc: "$1".} =
...
proc main =
export_fmu(setStartValues, calculateValues, eventUpdate)
Could this be feasible? How could I compile just those functions as library from nim?