{"id":8061,"date":"2019-04-15T16:01:08","date_gmt":"2019-04-15T21:01:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=8061"},"modified":"2019-04-15T16:01:08","modified_gmt":"2019-04-15T21:01:08","slug":"simulink-and-the-functional-mock-up-interface-standard","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2019\/04\/15\/simulink-and-the-functional-mock-up-interface-standard\/","title":{"rendered":"Simulink and the Functional Mock-up Interface Standard"},"content":{"rendered":"<p>Today, I want to cover the current state of support of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Functional_Mock-up_Interface\">Functional Mock-up Units<\/a> (FMU) in Simulink.<\/p>\n<p><strong>Background<\/strong><\/p>\n<p>From the FMI standard website, FMI is:<\/p>\n<p><em>Functional Mock-up Interface (FMI) is a tool independent standard to support both model exchange and co-simulation of dynamic models using a combination of xml-files and compiled C-code.<\/em><\/p>\n<p>This standard has been gaining popularity over the past few years, and we added support in Simulink to import and export FMUs.<\/p>\n<p>Disclaimer: The FMI standard is one of many options available for co-simulation between Simulink and other tools. As with most standards, it comes with a set of constraints. If your requirements do not fit within those constraints and you want more flexibility, I recommend writing an S-Function as I described in this post: <a href=\"https:\/\/blogs.mathworks.com\/simulink\/2018\/05\/01\/communicating-with-an-external-application-for-co-simulation\">Communicating with an External Application for Co-Simulation<\/a>. With an S-Function, you have full control over the co-simulation interface.<\/p>\n<p><strong>FMU Import<\/strong><\/p>\n<p>In R2017b, the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2017b\/simulink\/ref_extras\/fmu.html\">FMU<\/a> block was introduced. This block supports FMI standards 1.0 and 2.0, in both Co-Simulation mode (where the FMU contains its own local solver) and Model Exchange mode (where the FMU states are integrated by the Simulink solver).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/FMU_lib.png\" alt=\"FMU block\" \/><\/p>\n<p>I recommend looking at the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/in-product-solutions.html\">FMU Importing<\/a> section of the documentation to get started with that.<\/p>\n<p><strong>FMU Import Enhancements<\/strong><\/p>\n<p>In R2018a, we added two significant enhancements to the FMU block. The first one is <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/ug\/co-simulation-numerical-compensation.html\">numerical compensation<\/a>.<\/p>\n<p>Co-Simulation FMUs typically represent continuous phenomena, but their interface must be discrete and this can introduce numerical inaccuracies. To reduce those inaccuracies, numerical compensation can help. When a port has numerical compensation enabled, you will see the following icon next to it:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/ug\/fmu_cosim_nummodel_update.png\" alt=\"Numerical compensation\" \/><\/p>\n<p>The second enhancement to the FMU block is that it can now <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/ug\/run-co-simulation-components-on-multiple-cores.html\">run on different cores<\/a>. As you can imagine, this can result in significant performance improvements if componentized properly.<\/p>\n<p>One thing to note is that those two features also work with hand-written C-Mex S-Functions. If you want to write a co-simulation interface for a third-party tool without going through the FMU standard, keep that in mind!<\/p>\n<p><strong>Export a Model as a Tool-Coupling FMU<\/strong><\/p>\n<p>In R2018b, we added support in the other direction. It is now possible to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/ug\/export-model-as-tool-coupling-fmu.html\">export a model as a Tool-Coupling FMU<\/a> and include it in other tools.<\/p>\n<p>To begin, your Simulink model must be in a Simulink Project. In the Share menu, you will notice a new FMU entry:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/shareFMU.png\" alt=\"Share FMU\" \/><\/p>\n<p>Note that the Simulink Project should be configured to load and put on the path everything that the model needs. This is typically done through project <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2018b\/simulink\/ug\/automate-startup-tasks.html\">startup tasks<\/a>.<\/p>\n<p><strong>Using the Exported Tool-Coupling FMU<\/strong><\/p>\n<p>To use the FMU, you need a dedicated MATLAB session. For that, you have two options.<\/p>\n<p>The first option is to manually start MATLAB and execute <strong>shareMATLABForFMUCoSim<\/strong>. When the other tool utilizing the FMU will begin executing, you should see the Simulink project and model open and start.<\/p>\n<p>The second option is to start a dedicated MATLAB session from the operating system to run in the background. On Windows OS, you can execute a batch file from a DOS prompt to start as many dedicated MATLAB session as needed. See <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/simulink\/ug\/export-model-as-tool-coupling-fmu.html\">the documentation<\/a> for the location and syntax of this batch file.<\/p>\n<p><strong>Debugging Tip<\/strong><\/p>\n<p>Before importing an FMU in Simulink, I recommend running it through the <a href=\"https:\/\/github.com\/modelica-tools\/FMUComplianceChecker\/releases\">FMU Compliance Checker<\/a>, to ensure that the FMU is valid.<\/p>\n<p>If you have problems utilizing an FMU exported from Simulink in another tool, I recommend trying to import it first in a seperate MATLAB session using the FMU Import block. In the block, enable debugging to get as much information as possible:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/debugLogging.png\" alt=\"Debugging FMU\" \/><\/p>\n<p>If you select to send the debug log to \"Display\" as in the image above, the info will show up in the Simulink Diagnostic Viewer.<\/p>\n<p><strong>Now it's your turn<\/strong><\/p>\n<p>Are you using the FMU technology to co-simulate with different tools? Let us know which ones in the comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/simulink\/2019Q1\/debugLogging.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Today, I want to cover the current state of support of Functional Mock-up Units (FMU) in Simulink.<br \/>\nBackground<br \/>\nFrom the FMI standard website, FMI is:<br \/>\nFunctional Mock-up Interface (FMI) is a tool... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2019\/04\/15\/simulink-and-the-functional-mock-up-interface-standard\/\">read more >><\/a><\/p>\n","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[87,16],"tags":[568,570],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8061"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=8061"}],"version-history":[{"count":23,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8061\/revisions"}],"predecessor-version":[{"id":8913,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/8061\/revisions\/8913"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=8061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=8061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=8061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}