When developing a model, I often simulate and analyse the same experiments repeatedly. When you are doing this over and over it can become really tedious to create the same plots from scratch every time. Also, when you are sharing your Dymola experiments with colleagues, it can be difficult for others to know what to look at in the results. In both these situations the solution I’d use is to create a plot script to reduce the work needed to analyse simulation results and help guide other users to the important variables to look at. So, in this post I’m going to show you how to easily create and use plot scripts.
To be able to demonstrate plot scripts I need an experiment to analyse; I’m going to make use of an acceleration test example from our VeSyMA library for this. In this experiment the vehicle accelerates from rest, going up through the gears, as shown in the video below.
How to Generate a Plot Script
The easiest route to create your plot script is to generate the plots you need with the plot options in Dymola and then use the inbuilt script generation tool to capture your plots in a script for later reuse. Video 2, below, shows I did this for my acceleration test example.
This is a summary of the steps I performed in the video to generate my plot script:
- Having simulated my experiment, I used clearlog in the command line to clear all the previous actions. This means only the subsequent plots are included in the plot script, not every plot from this session in Dymola.
- I generated my plots using the plot options in the Dymola GUI.
- In the Simulation tab I selected New Script > Generate Script.
- In the Generate Script dialog I checked the Plot setup box.
- This enabled editing of the Include results filenames option, which allows a specific result filename to be included in the script. I didn’t check this option as I wanted to reuse the script with multiple experiments with different names.
- Next I selected where the plot scripted is saved. I recommend storing it in a Scripts folder within the library directory so that it is available for all users with access to the library.
- Once I saved the plot script, the script automatically opened in the Dymola script editor.
The plot script I generated for the acceleration test is shown in figure 1. It contains everything needed for Dymola to recreate my plots from a result file. Section 5.5.19, Built-in functions in Dymola, from the Dymola 2021x Full Users Manual contains a subsection on plot commands which explains what each of the commands means.
Using A Plot Script
There are two ways I reuse a plot script once I have simulated my experiment:
- Run Script in the Simulation tab.
- Add a command to the Commands menu in the Simulations tab.
If you share your libraries with other users, the Commands menu can make the availability of plot scripts more obvious to them; this is why we use the command menu for plot scripts in our commercial libraries.
In video 3, I demonstrate applying both methods to use the plot script created in video 2.
There are a couple of common issues you might come across when using plot scripts.
If you use a plot script with a different experiment than the one it was generated with, the same variable may not be available. In this case, the plots produced will be empty and command log will contain a message like:
Error: Could not plot variable ‘controlBus.engineControlBus.throttleAngle_1’
If you select the option to Include results filenames when generating a plot script, the script will only work with experiments where the result filename matches. So, if you attempt to use that script with an experiment of a different name you’ll see empty plots and a message like this in the command window:
Not possible to open file “C:/Dymola Working Directory/AccelerationTest.mat”: No such file or directory
You can edit the plot script to remove the specific filename, which will look like filename=”C:/Dymola Working Directory/AccelerationTest.mat”.
Missing Plot Script
If you have created a plot command in an experiment but the plot script cannot be found when the command is used, you will receive the error message below.
This may be because the script file has been accidently deleted or wasn’t stored in a shared location available to all the library users. This is why we always store plot scripts in the shared Scripts folder within the library.
Working Directory Change
When using Run Script to execute a script, be aware that the working directory will change to the location where the script is stored. So, if you’re likely to forget to swap the working directory back, you can stop this unwanted behaviour. Open the script in the script editor and deselect the Change to Directory button in the Script Option ribbon before running the script.
Hopefully making use of plots scripts, as described in this post, can reduce the repetitive effort when analysing the results of similar experiments over and over, while also making it easier for other users to see key variables from the results.
Written by: Hannah Hammond-Scott – Modelica Project Leader
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