Quick-fix: MATLAB FMU import error

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:

The FMU error dialog, shown by MATLAB
Error parsing FMU ‘dq.fmu’. Caused by: – Error in supplied FMU: Dynamic library file (‘dq.dylib’) does not exist in FMU ‘dq.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.

This will leave you with the folder dq_fmu with contents comparable to these

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.

Windows: binaries/win64/dq.dll
Linux: binaries/linux64/dq.so
Mac: binaries/maci64/dq.dylib

So, in this case, the folder name of the binary is incorrect.

Let’s rename it:

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.

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.