MATLAB 2017b introduced support for the Functional Mock-up Standard by providing FMU-import blocks in Simulink. This open standard enables mixing of simulation models created with different tools, like prototype controllers before coding them. This is an extremely powerful feature that I will write more about in soon. This memo, however, is about an error that occurred to me when importing an FMU in Simulink and how to fix it.
The error is something like:
Error parsing FMU
. Caused by: Error in supplied FMU: Dynamic library file does not exist in FMU .
Fixing this error is generally a matter of modifying the structure of the FMU (Functional mock-up Unit) file. I assume an unit named dq.fmu. There are the following steps to this:
1. Inspect the FMU file
An FMU file is merely a ZIP archive containing the model description, optional code and optional binaries (dynamic libraries) which allow invoking a simulation step or the model equations of a unit. Let’s unzip our FMU.
mkdir dq_fmu unzip dq.fmu -d dq_fmu
This will leave you with the folder dq_fmu with contents comparable to these
dq_fmu ├── binaries │ └── darwin64 │ └── dq.dylib ├── documentation │ ├── _main.html │ ├── model.png │ └── plot_x.png ├── model.png ├── modelDescription.xml └── sources └── dq.c 4 directories, 7 files
Note the description XML file that contains information about the structure of the unit, the binaries folder that contains compiled code for one or more platforms and the additional files and folders for documentation.
Correcting binary’s path
The example above shows a binary ‘dq.dylib’ in folder ‘darwin64’, indicating that dq.dylib is a compiled shared library for FMU dq.fmu and platform Mac OS 64 bit.
MATLAB however expects the following conventions, with dq replaced by the name of your unit.
So, in this case, the folder name of the binary is incorrect.
Let’s rename it:
mv dq_fmu/binaries/darwin64 dq_fmu/binaries/maci64
Repacking the FMU
Re-packing the unit is a matter of nothing more than zipping the folder structure and outputting the archive with the .fmu extension.
cd dq_fmu zip -r ../dq.fmu ./* cd ..
This changes to the FMU extraction directory, zips the content recursively (including folder’s contents) and changes the directory back.
Try to import the resulting FMU in MATLAB, the binary should be found now and MATLAB might either import the unit or provide another warning.