Useful commands and functions for scripting

Beyond the usual assortment of settings and flags, there are a number of useful commands and functions which tell Dymola to do various things. These can be triggered by entering them directly into the Commands window or executed via a script. At times, running models from a script can be useful. This is especially true in cases where you need to pre or post process data. Another case is when you need to prepare the Dymola environment in a specific way. Some of the functions presented in this blog post can help to automate some parts of the running (or pre/post-processing) of your models. As we’ve covered before, scripts can be triggered by functions to be executed during simulation. This opens up numerous possibilities, although some of the commands covered in this post would not be suitable to be called during a simulation.

The DymolaCommands library is one source for useful commands and functions

Useful commands and functions for working with results files

Dymola will write results files in the form of a trajectory file. After each simulation, a results file will be written to the working directory. This can make searching for results files a hassle, especially if the names of the results files were the same and they were overwritten!? Thankfully, there are various useful commands and functions which can help you automatically corral your results files into desired locations, rather than having to do it manually!

  • Modelica.Utilities.Files.copy() – A simple function used to copy a file, potentially to another location. Useful if you want to create copies of results or other files associated with the simulation that you’ve run. Supply the path to the file (only require the file name if the file is in the working directory) as the first argument. Supply the path including the new file name (only the file name is required if you want to create a copy in the working directory) as the second argument. The third argument is a boolean which enables you to control whether or not the incoming file will overwrite existing files in the destination directory.

  • Modelica.Utilities.Files.move() – Similar to Modelica.Utilities.Files.copy (), although the file or directory is moved, rather than copied. Input arguments follow the same format.

  • Modelica.Utilities.Files.createDirectory() – Creates a directory according to the string path supplied. To create a directory on the C drive called testFiles, use the argument “C://testFiles” with this function. Useful for automatically creating file-folder structures outside of the working directory.

  • removeResults() – You can remove a results set from the Variable Browser using this built in Dymola function. It is useful for controlling the results sets loaded in the Variable Browser. Supply the short names for the results sets (for instance, if running the CoupledClutches example model. Then the results file will have the same name as the model, i.e. CoupledClutches) as string. Be sure to enter the argument as a vector (within { } brackets). This enables multiple results sets to be cleared from the Variable Browser at one time. If you run removeResults() without any arguments, all of the existing results are removed from the Variable Browser.

Useful commands and functions for managing models

Automating the execution of large groups of simulations can be handy. Instead of having to manually open and close the required models or load the required versions of models into the workspace, much of this work can be accomplished using various built-in functions. Actions such as benchmarking various versions of a particular model (by loading and unloading from different paths) can be undertaken in this way. Below are some functions that can be helpful to do such things.

  • eraseClasses() – This function unloads the class supplied as the input argument. The first argument should be a string value, entered as a vector (this function supports the unloading of multiple classes at a time). This can be used for packages as well as classes. (Lets not talk about the choice of name for this one… nothing is technically erased when you run this)

  • closeModel() – Despite its name, this function doesn’t actually close a model. Rather, it requires the specified model to be translated again before running. This is helpful if for one reason or another, Dymola believes the model does not actually require retranslation before running again. No input argument is required, it simply acts upon the active model. To use, you can enter the text closeModel() in the script/Commands window.

  • openModel() – Used to load a model into the Package Browser. The path (as a string) to the model should be supplied as the input argument (assuming that the model doesn’t already exist in the working directory). Similar to the GUI load method, if the model is part of a package then the whole package will be loaded into Dymola.

Final thoughts

Dymola features a number of useful commands and functions, with the ones above only representing a small portion. If you’re interested in seeing the full list, I suggest you run the command listfunctions() in the Commands window. The result will be a list of all of the available built-in Dymola functions. These are augmented with the contents of the DymolaCommands library. All this together provides a wide array of options for automating some of the laborious tasks encountered when running multiple Dymola models.

 

Nate Horn – Vice President

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.

 

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2024 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?