It is good coding practice to store files used by a library in a Resources folder. This post looks at the typical Resources folder structure, the advantages of using the Resources directory and how to reference files in the Resources folder.
What is the “Resources” folder?
When creating a library, there is an option to save the library in a directory structure as described here in the ‘Saving a Modelica Library‘ blog post.
The user adds a Resources folder to this directory structure for storing all the code that is not Modelica code, for example data files, image files and files used by external functions. Figure 1 is an example of a library with a Resources folder.
The Resources folder in Figure 1 contains folders for storing binary files, data files, image files, and Include and Library folders that are used to store code used by external functions. The Resources folder can contain more or less folders.
Why use a Resources folder?
Any folder in the library directory structure could be used for storing files, however there are a number of good reasons to use a Resources folder:
- Using this folder is the convention, so others will expect to find these files here.
- When transferring a model the Save Total zip option can be used. This method stores the Resources folder in a zip file with the model so all the required files are packaged together. See here for more details.
- When using External functions, Modelica will automatically add the library’s Resources/Include folder to the Include directories. This makes referencing these files easy. More details can be found here.
- Dymola supports linking to static libraries compiled in the following forms: Win 32 bit, Win 64 bit, Linux 32 bit and Linux 64 bit. To use this feature save the static libraries in the directory structure in Figure 2. During the translation of the model Dymola detects which build process is being used and links against the Library files in the appropriate folder. See here for more details.
How should files from the “Resources” folder be referenced?
Relative referencing should be used to refer to the location of files. This makes it easy to move the library to other users’ machines.
The Modelica.Utilities.Files.loadResource(uri) function can be used to take a relative URI and return the absolute path of a file. This function should be used to reference files relatively.
The basic syntax, of a URI, to access a file in the Resources folder is:
where <Library name> is the name of the library and <filename> is the path of the the file relative to the Resources folder.
More details about the URI scheme can be found in the Modelica Specifications External resources chapter.
The loadResource() calls the ModelicaServices.ExternalReferences.loadResource() function which implements the loadResource() function. An example of how the loadResource() function is used is shown in Figure 3.
Using a Resources folder should improve library and model reliability.
Written by: Garron Fish – Chief 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