2

first ever Stack Overflow Post!

I am working in Dymola 2021x, and I am trying to run a model that a colleague provided me with. The package.mo files of one of the components is loading some files with

```fileName=ModelicaServices.ExternalReferences.loadResource("modelica://Master_Branch/components.../filename.mos")```

Now, as far as I understood, what should happen is that "modelica://" in that path is the working directory, so that the recources can be found no matter where I place the project folder. However, what does happen is the following:

When I try to simulate the component, I get messages during translation that all the files referenced with "modelica://..." can't be found. I get errors like the following:

Not possible to open file "C:/Program Files/Dymola 2021x/Modelica/Master_Branch/modelica:/Master_Branch/.../filename.mos" for reading:
Invalid argument

Now I think what happens is that the program does not recognise that modelica:// is supposed to be the working directory, and instead tries to find the directory modelica:// INSIDE the working directory.

My colleague does not get this error. We are both not super well-versed in Dymola, and so I was wondering if there is something basic that I might be missing? For reference, the libraries that I am using are:

MSL3.2.3
ModelicaServices 3.2.3
AixLib 0.10.7
BuildingSystems 2.0.0-Beta
Modelica_Synchronous 0.92.2
NCDataReader2 2.5
SDF 0.41
Modelica_DeviceDrivers 1.7.0
ExternData 2.5.0

I tried looking at the modelica documentation at https://specification.modelica.org/v3.4/Ch13.html (13.2.3 External resources) but didn't get much out of it, as well as the documentation for the load.Resource() command at https://doc.modelica.org/om/Modelica.Utilities.Files.loadResource.html

Any help or suggestions would be welcome! Cheers and thanks, Thies

marco
  • 5,944
  • 10
  • 22
thiesloose
  • 21
  • 2
  • 1
    The name following "modelica://" should the name of a top-level Modelica package. Do you really have a Modelica package named "Master_Branch"? – Hans Olsson Dec 02 '21 at 11:05
  • This seems related: https://stackoverflow.com/questions/61655422/exchanging-modelica-fmu-models – Markus A. Dec 02 '21 at 13:07

1 Answers1

6

The function ModelicaServices.ExternalReferences.loadResource() does not return the path of the working directory. It serves a different purpose - see below. If your list of loaded libraries is correct, it will not work, as you must reference a loaded class.

The quoted error message seems strange. But without an incomplete example its difficult to help in this specific case. However, I will try to give some general information which might help.

Getting the working directory

To get the current working directory, use this:

Modelica.Utilities.System.getWorkDirectory();
 = "C:/tmp/dymola"

Usage of loadResource()

loadResource() allows you to retrieve the absolute file path of a resource (a non-modelica-file) stored with a modelica library on the hard disk. Typical usage is to reference images, scripts or data sets.

You can e.g. get the location of the Modelica library on the hard disk with this call:

import Modelica.Utilities.Files.loadResource;
loadResource("modelica://Modelica/");
 = "C:/Program Files/Dymola 2022x/Modelica/Library/Modelica 4.0.0/"

This returns the absolute file path to the directory where the currently loaded Modelica library is stored. Now you can extend this call to reference a resource:

loadResource("modelica://Modelica/Resources/Scripts/Dymola/Mechanics/Rotational/CoupledClutches.mos")
= "C:/Program Files/Dymola 2022x/Modelica/Library/Modelica 4.0.0/Resources/Scripts/Dymola/Mechanics/Rotational/CoupledClutches.mos"

So the argument passed to loadResource can be broken down tho this:

[modelica-uri][top-level-package]/[relative-file-path]

with:

  • [modelica-uri]: modelica://
  • [package]: Absolute class path to a currently loaded package
  • /: the slash is vital. From the modelica spec:

    Any Modelica-scheme URI containing a slash after the package-name is interpreted as a reference to a resource

  • [relative-file-path]: path to file, relative to the location of package

Keep this in mind:

  • the referenced class must be loaded
  • loadResource() does not check, if the referenced file exists
  • use forward slashes, also on Windows
  • when you just reference a library without a resource, don't forget the trailing slash.
    • this works: loadResource("modelica://Modelica/")
    • this does not work: loadResource("modelica://Modelica")

ModelicaServices vs. Modelica.Utilities

ModelicaServices.ExternalReferences.loadResource() is equivalent to Modelica.Utilities.Files.loadResource(), but I suggest to use the latter one. The Modelica library is visible to the user. ModelicaSerivces is also loaded automatically by Dymola at startup, but its hidden from the package browser.

marco
  • 5,944
  • 10
  • 22
  • Note that ModelicaServices is visible by default. It's only in Dymola that it is hidden. – sjoelund.se Dec 09 '21 at 09:31
  • Well, tools should adapt ModelicaServices to make it work. Making it hidden was part of how Dymola adapted it. – Hans Olsson Dec 15 '21 at 09:45
  • Hey, sorry for not getting back to you earlier! I just now had the opportunity to get back to the project. First, thanks for the in-depth reply! I tried your example code for `Modelica.Utilities.Files.loadResource()`, but got some different results: For `loadResource("modelica://Modelica/")` I get `"C:/Users/Thies/Documents/Dymola/modelica:/Modelica/"` Also, when just entering `Modelica.Utilities.Files.loadResource("")` I get `= "C:/Users/Thies/Documents/Dymola"` To me, it would seem like the modelica://(URI?) is not needed... or I haven't understood what it does? – thiesloose Dec 16 '21 at 08:41
  • Strange results. Are you using Dymola? Which version? I get `""` for `loadResource("")`. The Modelica URI is needed when you refer to Modelica classes. – marco Dec 17 '21 at 07:14
  • yeah, I am on Dymola. I am using Dymola 2021x with Modellica 3.2.3. Maybe it's noteworthy that I am working on a virtual machine? I don't know if that might change how paths are interpreted, but I feel that shouldn't be it. To me it seems like the loadResource() command must somehow be "faulty". Would you know if there are differences in the way loadResource() works between modelica 3.2.3 and 4.0.0? – thiesloose Dec 21 '21 at 12:08
  • Modelica 3.2.3 is not the default Modelica version in Dymola 2021x, but Modelica 4.0.0. I guess this is related to your issue. It is best to use the default Modelica version provided by Dymola. Did you install Modelica 3.2.3 as described in the user manual? Why don't you upgrade to Modelica 4.0.0? – marco Dec 21 '21 at 17:02