On a recent project I came across a Modelica function or two I found very useful. By the same token, I thought they would make an interesting blog post! Many cases exist where you might wish to automatically generate Modelica files. One example would be to generate variants of a model with permutations. Or perhaps generating data records. These functions surround the creation and customization of Modelica models. Of course, there are many ways you can combine these, and plenty of other functions to augment things with. I’m sure you will think of many more functions to deploy with these than I have thought of!
The getClassText() Modelica function
DymolaCommands.Documentation.getClassText(fullName, includeAnnotations, formatted);
As the name implies, this function will return the Modelica text of the full class name (not file path) it is pointed towards, as a single string. You have the option to set the ability to include the annotations present in the text. All of the text of a class can be brought into Dymola to be worked with. Finally, the last argument dictates whether this should be plain or HTML text. Plain text enables the function to return text which can be edited and/or used with the setClassText() function.
Figure: Function calls
The replace() Modelica function
Modelica.Utilities.Strings.replace(string, searchString, replaceString, startIndex=1, replaceAll=true, caseSensitive=true);
Having imported the Modelica text for a model, you’ll likely want to modify and customize it in some way. There are a few methods of doing this using various Modelica Utilities functions. My personal go to is to use the replace() function.
The function will search the supplied string, replacing any specified string with another that you have supplied. Supplying an index location where to start the search enables the replacements to occur in a specific section of the supplied string. You can also dictate whether to replace all instances of the found string, which is similar to the “find and replace” method in word processing. Likewise, you can also differentiate between case sensitive instances of the searched string.
The setClassText() Modelica function
DymolaCommands.SimulatorAPI.setClassText(parentName, fullText);
This function is used to specify the text in a Modelica file, with the target package as well as the text the file can contain required as arguments. What makes it powerful, is that it can set the text for a new Modelica file. The upshot of this is that it enables the automatic creation of Modelica files. In addition, it can be combined with other Modelica functions to customize Modelica files created automatically. This could be from a function, or a script.
Since the function accepts the complete text of a Modelica file, no limits are placed on the class which can be created. Nevertheless if the function path is pointed to an existing Modelica file, then the old file is overwritten.
Final thoughts
Dymola ships with the DymolaCommands and Modelica Standard libraries, which contain a lot of useful functions for Dymola operations. The functions I have described above represent only a few of the Modelica functions available which concern the automated generation of Modelica files and classes. That being said, these functions are the ones I reach to first when tasked with automating the generation of Modelica files.
Written by: Theodor Ensbury – Senior 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.