1. Overview
Working with the FMI Code Generator follows three steps:
-
Generate a fully working FMU source code project (wizard or command line).
-
Implement the model-specific functionality in the user source file.
-
Build and deploy the FMU with the provided build system files and scripts.
Whenever variables change, re-run the generator on the existing project (update): the generated files are rewritten, your own code is kept.
2. Creating a new FMU project
2.1. With the graphical wizard
python scripts/main.py
On the start page choose Create a new FMU project. Then enter
-
the model name (also used as FMU identifier and directory name),
-
the target directory and an optional description,
-
the list of variables (inputs, outputs, parameters, locals) with causality, variability, initial value and type.
Confirming the last page generates the project directory <target dir>/<model name> and runs a test build.
2.2. With the command line generator
python scripts/commandLineGenerator.py <modelName> --description "..." \
[--target-dir DIR] [--input vars.input] [--no-build] [--overwrite]
3. Updating an existing project
When you need to add, remove or modify variables, open the existing project — either with Open an existing FMU project on the wizard start page (recently used projects are listed there), or on the command line:
python scripts/commandLineGenerator.py --update <projectDir> [--no-build]
The generator reads <projectDir>/<modelName>.input, rewrites the generated files and keeps the value references of existing variables stable. The previous versions of the generated files are saved to <projectDir>/.backup/<timestamp>/.
Projects created with generator version 1.3 or older (with #define FMI_… in the user .cpp file) are legacy projects. The update writes the new generated files and prints manual migration instructions.
|
4. The generated project
4.1. Directory structure
4.2. Generated files and user files
| File | Owner | Content |
|---|---|---|
|
generator |
FMI model description |
|
generator |
value reference defines ( |
|
generator |
GUID, |
|
generator |
project file (JSON) with model name, description and variables |
|
you |
model implementation |
|
you |
build system files and scripts |
Generator-owned files are overwritten on every update. User files are created once and never touched again.
The places where your own code goes are marked with TODO comments in src/<model>.cpp.
|
4.3. The project file (.input)
5. Implementing the FMU
5.1. FMU state serialization
The generated code serializes all FMI variables. Model-specific state is added in three hooks of the user code:
-
userStateSize() -
serializeUserState(char *&) -
deserializeUserState(const char *&)
The user data is written last; the generated code checks that the byte count matches.
6. Building and deploying
The generated directory contains build system files for CMake and Qt qmake. With CMake you can generate makefiles for various compilers and development environments; with the .pro file you can directly start developing in Qt Creator (the FMU code itself is plain C/C++ without Qt dependencies).
Linux/macOS:
cd <model>/build
./build.sh release # build the FMU in release mode
./deploy.sh # package the FMU (zipped directory structure)
Windows: build_VC_x64.bat and deploy.bat.
You may want to adjust the deploy script to copy your own resource files into the FMU.
7. Python API
To use the code generator functionality from other software, use the Python API: create an instance of class FMIGenerator, specify its attributes and call generate() (or update(projectDir) for an existing project).
# import FMIGenerator class and data types
from FMIGenerator import *
# create instance of FMIGenerator class
fmiGen = FMIGenerator()
# specify attributes
fmiGen.modelName = "MyFirstFMU"
fmiGen.description = "My first auto-generated FMU. Awesome, right?"
fmiGen.targetDir = "../fmus"
# 'targetDir' can be relative path to current working directory
# or an absolute file path
# add variables to export
v = VarDef("InputVar1", "continuous", "input", "exact", "Real")
# valueRef will be given automatically unless specified explicitly
v.startValue = 15
fmiGen.variables.append(v)
# now generate the FMU
fmiGen.generate()