February 29th, 2016 by Mahdieh Mehrabi
The working directory is the location on the computer in which Dymola runs and writes various files and of course the output file containing relevant trajectories of variables. It’s possible to find out, or alter which directory this is at some point during a simulation by using functions in the base library.
By doing this, we can facilitate co-simulation, or organise files and data written by the software to various ends. Where there are hundreds of sets of initial conditions being run in a design-of-experiment type exercise, then this would be invaluable, as where files are “dropped” could depend on the nature of the results – satisfaction of some criterion, or the load-case applied etc.
A series of functions exist in the Modelica standard library that allow convenient manipulation of files and folders to serve good modelling and bookkeeping.
The function Modelica.Utilities.System.setWorkDirectory is used to set the new working directory. It takes a string as the argument. The string can be constructed within the parentheses, using quotations and/or addition of more than one string.
You can of course predefine the argument and refer to it by name, too.
Figure 1. code examples – setWorkingDirectory
Refer to the example programme to see the lines of code above applied in context.
The code below assigns the full command path to the current working directory as the value of a predefined string. Once held in the string variable wdir, the path can be used to construct other strings.
Figure 2. assignment of the path to the current working directory, to a string
The function, used as a command is useful for creating models that place result files in an appropriate place regardless of where the model is stored. When using Dymola alongside other CAE software, it’s useful to provide the opportunity to scan for the presence of files or trigger activity by dropping a file. Look into Windows batch commands for more on this.
In the code below, the function “system” is used. The function is a basic built in function, and doesn’t require you to put in a path. You can access it from any point, passing a string to the operating system.
Figure 3. system() function
In order to know what the contents of the string to send to the system should be, you will need to look into batch scripting for Windows. The simple string I have included is simply the command mkdir which causes a directory to be created, followed by a name for the directory.
It is much more useful and flexible if the programme can create as many folders as prove to be needed “on the day” which means, for example that results could be grouped into time-windows, or ranges, without knowing a-priori which ranges of results would occur, or how long the simulation would take.
Figure 4. createDirectory() function
The example model includes a long command path which will be created by the function.
Once a new working directory has been assigned, it can be useful to write a file. Simply creating an empty file can already be useful, because even its presence can act as a simple “switch” signalling an external software to commence an activity.
It may also be as simple as the desire to provide results to a client in a particular format, or systematically divide sets of billions of numbers into groups to ease the loading, and use of these by parties in different places using different software, and so forth.
It is notable that the following function is intended to print a line in a file. The file is created, however when a file by the given name does not yet exist.
Figure 5. code providing a string to print and the filename required
The Modelica basic library contains several useful functions used to handle files. Both Modelica.Utilities.Files and Modelica.Utilities.Streams are home to these.
In short, the difference between an equation section and an algorithm section is as follows:
You would use equations to provide descriptions of physics, but you would use algorithm to perform a set sequence of steps in the same way as you might do in any conventional programming language. Each line of code in the equation section is a relation, while lines in algorithm are statements.
This is underlined if you note that the “=” used in any algorithm statement will be transformed to “:=” if the code is checked by the automatic tools provided.
Consulting the manual, the := represents an assignment of value (as in typical programming) whereas the = is being used in its more normal everyday algebra sense.
The example provided uses the “when” clause to make the instructions about writing the directories and files happen only at the end of the forward-marching integration of the model provided. There need not be a model at all, however if the clause isn’t used, the instructions will be executed once at each time-step, as it is effectively appended to the model, as if the physical laws wouldn’t be satisfied until a folder was created.
This is the programme from which the excerpts used previously were extracted, with comments.
Figure 6. code to obtain eigenvectors and write to file
By: Sas Harrison – Systems Engineer
 Dymola can also check for the existence of a file (via a pre-programmed interaction with the operating system)