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.
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.
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 Boolean is determined can be done either in the top level function, being subsequently passed to the stream printer, or within the stream printer itself. Both methods have associated advantages and disadvantages; the former method is useful in functions where cases are switched (i.e. the boolean used to control the stream printer output is utilised in the function elsewhere) and the latter is useful if you want to only maintain the stream printer when modifying what is printed. This example uses the second method to change the message based upon the sign of the variable to be printed.
Call the stream printer to display result in simulation log
To utilise the stream printer, it has to be called as a model and run with the data we want to display. As the stream printer model has no calculations, then it doesn’t need to be simulated for any length of time, so the end time in the call is set to 1s.
When the top level function is run, the result we wish to print into the simulation log is displayed as desired.
Whilst this method is somewhat laborious to create when switching between multiple cases to print, it does provide a useful workaround to display pertinent data to the user at the end of a function. All of the code to create these models can be downloaded in a PDF here.
Written by: Theodor Ensbury – Project Engineer
Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion