Skip to content

Instantly share code, notes, and snippets.

@t-sommer
Last active July 7, 2023 02:41
Show Gist options
  • Save t-sommer/108262d6d1b5bcc921edb48d81a62fc2 to your computer and use it in GitHub Desktop.
Save t-sommer/108262d6d1b5bcc921edb48d81a62fc2 to your computer and use it in GitHub Desktop.
Hacking FMI

Tutorial "Hacking FMI" @ Modelica Conference 2019

Install Prerequisites

  • Anaconda — Python distribution

  • FMPy — Python library to simulate FMUs

  • C/C++ compiler — Xcode (macOS), Visual Studio (Windows) or GCC (Linux) to build source code FMUs and the FMIKit S-function

  • CMake — project generator to build source code FMUs and FMIKit S-function

  • Maven — Java build tool to build the FMIKit block dialog (requires Java 1.7+)

  • PyCharm — Python IDE to code, run and debug Python programs

  • IntelliJ IDEA — Java IDE to view FMIKit Java Dialog, (optional)

  • MATLAB/Simulink R2016B — to import and export FMUs

  • Dependency Walker - display exported symbols in DLLs (optional)

Tip
  • let the installer add Anaconda to the system PATH if this is your only Python

  • don’t install into a folder where you have no write access (e.g. C:\Program Files\Anaconda3)

  • Anaconda comes with almost all the dependencies, so we can save time by using the --no-deps (no dependencies) option:

    conda install -c anaconda pyqtgraph --no-deps
    conda install -c conda-forge fmpy --no-deps

FMPy

Command Line Interface

Get Get the Rectfier.fmu to play with from the FMI Cross-Check repository and try

  • fmpy --help — display available options

  • fmpy info — get info about an FMU

  • fmpy simulate — simulate an FMU

  • fmpy compile — compile the shared library from a source code FMU

Graphical User Interface

Start the FMPy GUI with python -m fmpy.gui

  • create a desktop shortcut

  • set as default program for *.fmu

  • load and simulate an FMU

  • start values

  • input

  • reference signals

  • parameter variation

  • add desktop shortcut

Scripting & advanced usage

  • demo: read the model description, simulate, plot results

  • create a CMake project & debug an FMU

  • performance tweaks

How FMPy works

  • model description parser

  • ctypes interface to the shared library

  • simulation loop

  • solver interface

Challenges

easy Simulate an FMU on the command line and plot the results

medium Write a Python script that variates a parameter and plots the results

hard Write a custom simulation loop that generates an input signal and terminates when a variable reaches a threshold

Special Guest

Test-FMUs

Challenges

easy Build and simulate an FMU in different FMI versions

medium Change a start value in an FMU and compare the results in FMPy

hard add an additional function to an FMU and access it with FMPy

Break

Installation & build from source

  • MATLAB App Installer

  • Build from source

Import an FMU

Run mex -setup an select Visual Studio 2015

  • binary FMU (with generic S-function "Normal" mode)

  • binary FMU (with generic S-function "Rapid Accelerator" mode)

  • binary FMU (with source code S-function)

  • source code FMU

Export an FMU

  • export a Simulink Model as an FMU

Scripting Interface

How it works

FMU Import

  • dialog & XML parser

  • FMU block & user data struct

  • generic S-function

  • generated S-function

FMU Export

  • TLC & TMF files

Challenges

easy Import and simulate an FMU with different solvers

medium Import and simulate an FMU using the scripting interface

hard Build and debug the generic S-function using the CMake project

Unity FMI Addon

  • FMU import, PInvoke interface, using the FMU class

  • run the BouncingBall example scene

  • import an FMU

  • demo

Challenges

easy Import the BouncingBall FMU from the Test-FMUs

medium Use the velocity of the BouncingBall to animate a box shape

hard Add bindings for additional FMI functions

Discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment