Search code examples
modelicaopenmodelica

How do I use URIs to load file in combiTable1Ds in Openmodelica?


In OpenModelica 1.14.0, I want to load some data from a file into a combiTable1Ds. I want to use a Modelica URI to specify the path in a portable fashion (i.e. not with a full absolute path).

However I specify the fileName argument, though, I always get an error that the file cannot be found.

Minimal example:

I have the following package layout:

├── TestURI2
│   ├── loadURI2.mo
│   ├── package.mo
│   ├── package.order
│   └── somefile.txt

and in the file loadURI2.mo

within TestURI2;

model loadURI2
  Integer x;
  String filepath = Modelica.Utilities.Files.loadResource("modelica://TestURI2/somefile.txt");
  Modelica.Blocks.Tables.CombiTable1Ds combiTable1Ds(fileName = "modelica://TestURI2/somefile.txt", tableName = "bla", tableOnFile = true)  annotation(Placement(visible = true, transformation(origin = {-60, 68}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
combiTable1Ds.u = time;
x  = Modelica.Utilities.Strings.length(filepath);
end loadURI2;

If I comment out the lines related to combiTable1Ds, the model compiles and x shows the correct length of the path to the file, indicating that loadResource correctly identifies the path from the URI modelica://TestURI2/somefile.txt.

If I leave combiTable1Ds active, I get

/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2/TestURI2.loadURI2 -port=34607 -logFormat=xmltcp -override=startTime=0,stopTime=1,stepSize=0.002,tolerance=1e-6,solver=dassl,outputFormat=mat,variableFilter=.* -r=/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2/TestURI2.loadURI2_res.mat -w -lv=LOG_STATS -inputPath=/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2 -outputPath=/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2
... loading "bla" from "modelica://TestURI2/somefile.txt"

Not possible to open file "modelica://TestURI2/somefile.txt": No such file or directory

simulation terminated by an assertion at initialization
Simulation process failed. Exited with code 255.

which I don't understand at all, given that loadResource works correctly.

What is going wrong here?


Solution

  • You need to use Modelica.Utilities.Files.loadResource in the modifier for the fileName in combiTable1Ds. Note that Modelica.Utilities.Files.loadResource is a BAD name, the function does NOT load anything, it just translates from a Modelica URI to an absolute file path to the resource.

    within TestURI2;
    model loadURI2
      Integer x;
      parameter String filepath = Modelica.Utilities.Files.loadResource("modelica://TestURI2/somefile.txt");
      Modelica.Blocks.Tables.CombiTable1Ds combiTable1Ds(fileName = filepath, tableName = "bla", tableOnFile = true)  annotation(Placement(visible = true, transformation(origin = {-60, 68}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    equation
    combiTable1Ds.u = time;
    x  = Modelica.Utilities.Strings.length(filepath);
    end loadURI2;