Expert Insights

Displaying Results in the Simulation Log from a Function in Dymola

Written by Theodor Ensbury | Jan 18, 2018 11:00:00 PM

Functions in Dymola provide a useful way to run multiple models iteratively, in parametric sweeps or in other configurations and studies. A previous blog post, (How to simulate a model multiple times with different parameter values), explores various methods to automate simulation execution, which can be called via a Modelica function. By default, a function called at the top level (e.g. right click on function in package browser > call function) will display the results of executing such a function in the Dymola command window. Whilst this is functional, the results are not displayed to the user in a clear way; units and variables cannot be labelled easily and clearly. A useful enhancement would be for the results to be displayed in the simulation log once the function completes.

The Modelica.Utilities.Streams.print command, when called from within a model achieves just this. However, it does not behave in the same way when called as an individual top level function call. Rather than displaying the string passed to the command in the simulation log, the string is displayed in the command window. Even though this is useful when creating Modelica functions, as values can be displayed at particular points of execution, it doesn’t achieve what we want it to. The solution is a small workaround, whereby the results to be displayed are passed to a dummy model containing only Modelica.Utilities.Streams.print commands and called right at the end of a function.

Declare a function to generate a result

To demonstrate this workaround we shall create a simple function which calculates some form of result. Experimental results could be used, but for simplicity in this example we will use dummy values; mining trajectory result files for specific result values is covered here. The code shown below simply establishes our function and declares some variables which are used to generate a result we would like to display in the simulation log.

The first part of the function to generate a result called resultToPrint which will be displayed in the simulation log.

Create the ‘stream printer’

Now we need to create a dummy model to receive our result and call the Modelica.Utilities.Streams.print command to print the result to the simulation log. This is a simple model consisting only of parameter inputs and the print statements. The Modelica.Utilities.Streams.print command is located within an if statement dependant upon the initial() command so the values are printed only at the initiation of the model. This means they only appear once in the simulation log. Without this, the print command would execute with every time step the model takes. We’ll refer to this dummy model as the ‘stream printer’. As can be seen, spaces can be printed to help separate the strings being printed.

A simple stream printer to display the result in the simulation log above the simulation statistics.

Often, you might want to vary a string message that is displayed in the simulation log depending upon results calculated within your function. Differing print commands can be switched between each other depending upon the value of a Boolean. How this