Engee documentation
Notebook

Importing FMU Co-Simulation component

Showing how to use FMU Import blocks to add external FMU-formatted models to the Engee canvas.

Model description

Co-Simulation mode in FMU (Functional Mock-up Unit) components is designed to combine several models developed in different modelling tools into a single time-synchronised system.

This mode allows you to combine models created in different environments (MATLAB/Simulink, Dymola, OpenModelica, etc.) without having to rewrite their code.

Each FMU works as a "black box" with its own solver.

In this example, our "external" model is in the file fmuVanDerPol.fmu, which we will place in the FMU Import block, and after setting it up, we will get the graphics from the model it contains.

image.png

We propose to study the FMU file. To do this, let's put some libraries and call the command info(fmu).

In [ ]:
Pkg.add( ["FMI"] )
using FMI
fmu = loadFMU( "fmuVanDerPol.fmu" )
info(fmu)
#################### Begin information for FMU ####################
	Model name:			myvdp
	FMI-Version:			2.0
	GUID:				{a50ebd80-9a0f-b492-4bf4-893f9611b1a5}
	Generation tool:		[Unknown generation tool]
	Generation time:		2018-09-26T19:47:29Z
	Var. naming conv.:		structured
	Event indicators:		0
	Inputs:				0
	Outputs:			2
		0 ["Out1"]
		1 ["Out2"]
	States:				0
	Parameters:			1
		2 ["mu"]
	Supports Co-Simulation:		true
		Model identifier:	myvdp
		Get/Set State:		true
		Serialize State:	true
		Dir. Derivatives:	false
		Var. com. steps:	false
		Input interpol.:	false
		Max order out. der.:	nothing
	Supports Model-Exchange:	false
##################### End information for FMU #####################

Setting up the block

In this information, we need to find the input port names, output port names and parameter names in order to properly create the block interface and configure the block.

This model has no inputs, there are two outputs (Out1 and Out2) and there is a parameter mu. Also we don't see the time step settings that the solver of this model is waiting for.

We found out empirically that the computational step size of this FMU model should not be lower than 0.1 с. The global step size of the entire simulation can be anything.

image.png

Let's run the model at runtime and plot the graphs:

In [ ]:
model_name = "fmu_co_simulation";
model_name in [m.name for m in engee.get_all_models()] ? engee.open(model_name) : engee.load( "$(@__DIR__)/$(model_name).engee");
res = engee.run( model_name );

plot( res["x"].time, [res["x"].value res["dx"].value], label=["x" "dx"],
      lw=3, size=(600,300) )
Out[0]:

When choosing Co-Simulation over Model Exchange?

Model Exchange (ME) requires a single solver for the whole system and is suitable if all models can work with a single solver.

Co-Simulation (CS) is chosen when:

  • models use different solvers,
  • legacy code or proprietary simulators are available,
  • distributed execution is required (e.g., HIL testing).

Conclusion

Co-Simulation FMU is a powerful tool for integrating heterogeneous models without having to completely rewrite them, but with the trade-off of controlled synchronisation and potential sampling errors.

Blocks used in example