Functional Mock-up Interface - FMI¶
The new standard for model exchange and co-simulation with Functional Mockup Interface (FMI) allows export of pre-compiled models, i.e., C-code or binary code, from a tool for import in another tool, and vice versa. The FMI standard is Modelica independent. Import and export works both between different Modelica tools, or between certain non-Modelica tools. OpenModelica supports FMI 1.0 & 2.0,
- Model Exchange
- Co-Simulation (under development)
FMI Export¶
To export the FMU use the OpenModelica command translateModelFMU(ModelName) from command line interface, OMShell, OMNotebook or MDT. The export FMU command is also integrated with OMEdit. Select FMI > Export FMU the FMU package is generated in the current directory of omc. You can use the cd() command to see the current location. You can set which version of FMI to export through OMEdit settings, see section FMI.
To export the bouncing ball example to an FMU, use the following commands:
>>> loadFile(getInstallationDirectoryPath() + "/share/doc/omc/testmodels/BouncingBall.mo")
true
>>> translateModelFMU(BouncingBall)
"SimCode: The model BouncingBall has been translated to FMU"
>>> system("unzip -l BouncingBall.fmu | egrep -v 'sources|files' | tail -n+3 | grep -o '[A-Za-z._0-9/]*$' > BB.log")
0
Warning
Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions("-d=initialization").
After the command execution is complete you will see that a file BouncingBall.fmu has been created. Its contents varies depending on the current platform. On the machine generating this documentation, the contents in Listing 2 are generated (along with the C source code).
binaries/
binaries/linux64/
binaries/linux64/config.log
binaries/linux64/BouncingBall_FMU.libs
binaries/linux64/BouncingBall.so
modelDescription.xml
A log file for FMU creation is also generated named ModelName_FMU.log. If there are some errors while creating FMU they will be shown in the command line window and logged in this log file as well.
By default an FMU that can be used for both Model Exchange and Co-Simulation is generated. We only support FMI 2.0 for Co-Simulation FMUs.
Currently the Co-Simulation FMU supports only the forward Euler solver with root finding which does an Euler step of communicationStepSize in fmi2DoStep. Events are checked for before and after the call to fmi2GetDerivatives.
FMI Import¶
To import the FMU package use the OpenModelica command importFMU,
>>> list(OpenModelica.Scripting.importFMU, interfaceOnly=true)
function importFMU
input String filename "the fmu file name";
input String workdir = "<default>" "The output directory for imported FMU files. <default> will put the files to current working directory.";
input Integer loglevel = 3 "loglevel_nothing=0;loglevel_fatal=1;loglevel_error=2;loglevel_warning=3;loglevel_info=4;loglevel_verbose=5;loglevel_debug=6";
input Boolean fullPath = false "When true the full output path is returned otherwise only the file name.";
input Boolean debugLogging = false "When true the FMU's debug output is printed.";
input Boolean generateInputConnectors = true "When true creates the input connector pins.";
input Boolean generateOutputConnectors = true "When true creates the output connector pins.";
output String generatedFileName "Returns the full path of the generated file.";
end importFMU;
The command could be used from command line interface, OMShell, OMNotebook or MDT. The importFMU command is also integrated with OMEdit. Select FMI > Import FMU the FMU package is extracted in the directory specified by workdir, since the workdir parameter is optional so if its not specified then the current directory of omc is used. You can use the cd() command to see the current location.
The implementation supports FMI for Model Exchange 1.0 & 2.0 and FMI for Co-Simulation 1.0 stand-alone. The support for FMI Co-Simulation is still under development.
The FMI Import is currently a prototype. The prototype has been tested in OpenModelica with several examples. It has also been tested with example FMUs from FMUSDK and Dymola. A more fullfleged version for FMI Import will be released in the near future.
When importing the model into OMEdit, roughly the following commands will be executed:
>>> imported_fmu_mo_file:=importFMU("BouncingBall.fmu")
"BouncingBall_me_FMU.mo"
>>> loadFile(imported_fmu_mo_file)
true
The imported FMU can then be simulated like any normal model:
>>> simulate(BouncingBall_me_FMU, stopTime=3.0)
record SimulationResult
resultFile = "«DOCHOME»/BouncingBall_me_FMU_res.mat",
simulationOptions = "startTime = 0.0, stopTime = 3.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'BouncingBall_me_FMU', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
messages = "",
timeFrontend = 0.023988871,
timeBackend = 0.007264148000000001,
timeSimCode = 0.05919602600000001,
timeTemplates = 0.04075088800000001,
timeCompile = 0.3988506829999999,
timeSimulation = 0.043200056,
timeTotal = 0.573369926
end SimulationResult;