Modelica scripts (.mos) are very useful. Alongside similarities to the Windows “batch” script of interactive commands, they can be used to store any interactive event. Some common uses of scripts can be to store useful sequences of commands, as is the case of plot scripts, or to set Advanced flags in Dymola. Modelica scripts can also include non-command based functionality, such as setting parameters, start values or containing data. Therefore, other uses include storing data and setting large numbers of parameters, as is the case in the plotTiming.mos script.
Whilst Modelica functions and scripts have similar functionality, they are not the same. Functions can conduct some basic interactive operations, such as setting advanced flags or loading libraries. However, they are unsuitable for tasks such as data storage/collection, or the recording of interactive commands.
Why call a script from a function?
As functions can be called during time integration, then being able to call a script from a function enables it to be called during time integration as well; an extremely useful functionality in certain situations. Data could be loaded into a function from a script file, or it could be used to trigger other interactive functionalities such as setting of advanced flags.
From a Dymola perspective, the latter could be done from within a function. But, as scripts are standalone files, they can be edited easily outside of Dymola using a text editor. Dymola models are deployed to a wide range of environments in many forms. In some of circumstances, the user is utilising a Dymola model without the ability to open, and edit it, from an instance of Dymola itself. Therefore, being able to change the elements of the model, such as parameterisation or Dymola advanced flag settings, from an easy to open script file, is useful for users.
How to call a scripts from a function
Within the DymolaCommands package, the RunScript function can be found. Enabling the calling of a Modelica script, this function makes use of an external function built into Dymola to execute the script from the Dymola command line. As functions can be called from functions, then the RunScript function can be called from any Modelica function.
Figure 1: The DymolaCommands.SimulatorAPI.RunScript function
A simple function, RunScript only requires the user to supply the string path to the script as the first argument. If the script name is solely used, then the function will expect the script to reside in the Dymola working directory. A fuller path, pointing to a non working directory location can also be supplied.
Figure 2: Simple example script calling a script containing data to be run.
The RunScript command in the example above is used to call a script containing step timing data, generated from solver events. If the example Run_A_Script function is called, the script is run as if the user has manually called it interactively or called it from the command line.
Figure 3: A plotTiming.mos script as run from the Run_A_Script function presented above.
Running the Run_A_Script function with the default settings will result in the times matrix being created in the Dymola workspace.
Being able to call and execute the contents of Modelica scripts from within a function is a very handy technique to have as a developer, opening up different options of user interaction with your models. As a bonus, it’s an extremely easy one to implement!
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.