A functional mock-up unit (FMU) is a portable zip package containing a compiled simulation model. It can be imported into an environment compliant with the Functional Mock-up Interface (FMI), an open, tool-independent standard for model exchange and co-simulation. Packaging a model as an FMU lets it run outside its development GUI as part of a larger system. section 9.3.3
The two FMU types differ in where the numerical solver runs:
- A model exchange FMU relies on the simulation environment to supply the solver. section 9.3.3
- A co-simulation FMU packages the solver; the environment sets inputs and advances time. section 9.3.3
Portability has a platform boundary. By default, a build includes binaries for the platform on which it was created. For example, an FMU built on 64-bit Linux can run on the same platform, but the package should not be assumed to contain binaries for every target. section 9.3.3
Build and run a drainage model
Build the water-tank drainage model as a co-simulation FMU with OpenModelica in Docker. The build produces WaterTankDrainage.fmu. Outside the container, install NumPy and FMPy, then use FMPy to simulate the package. The example sets the initial tank level to 2, runs to 15,000 seconds, and requests the tank level as output. section 9.3.3
# Download the model
curl -L https://github.com/digital-twins-in-action/book/blob/main/code/
ch09/WaterTankDrainage.mo > WaterTankDrainage.mo
# Launch OpenModelica container
docker run -it -v "$(pwd):/app" openmodelica/openmodelica:v1.25.5-gui
# Inside the container, start the shell and build the FMU
OMShell-terminal
>>> installPackage(Modelica) # Install the Modelica Standard Library
>>> loadModel(Modelica) # Load it into the environment
>>> loadFile("/app/WaterTankDrainage.mo") # Load the tank model
>>> buildModelFMU(WaterTankDrainage, modelType="cs") # Build
co-simulation FMU
Shows the OpenModelica container setup and commands for building the co-simulation FMU. section 9.3.3
Listing 9.3 Using PyFMU to simulate a draining water tank FMU
import numpy as np
from fmpy import simulate_fmu
start_values = {
"tank.level_start": 2 #1
}
output_variables = ['tank.level']
result = simulate_fmu(
'WaterTankDrainage.fmu',
stop_time=15000, #2
start_values=start_values,
output=output_variables
)
print(result)
Shows how to set initial values, select an output, and run the FMU with FMPy. section 9.3.3
Combine a simulation with noisy sensors
A room-temperature workflow uses a model in an unscented Kalman filter’s prediction step to estimate temperature from five noisy sensor readings. The simulation uses an HVAC setpoint and external temperature; the same FMU is used both to generate simulated sensor data and in the estimator. section 9.3.4
