如何使用 URI 在 Openmodelica 的 combiTable1Ds 中加载文件?
How do I use URIs to load file in combiTable1Ds in Openmodelica?
在 OpenModelica 1.14.0 中,我想将文件中的一些数据加载到 combiTable1Ds 中。我想使用 Modelica URI 以可移植的方式指定路径(即不使用完整的绝对路径)。
但是我指定了 fileName
参数,但是,我总是收到找不到文件的错误消息。
最小示例:
我有以下包布局:
├── TestURI2
│ ├── loadURI2.mo
│ ├── package.mo
│ ├── package.order
│ └── somefile.txt
并在文件中 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;
如果我注释掉与 combiTable1Ds
相关的行,模型会编译并且 x 显示文件路径的正确长度,表明 loadResource
正确识别来自 URI 的路径 modelica://TestURI2/somefile.txt
.
如果我让 combiTable1Ds
处于活动状态,我会得到
/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.
鉴于 loadResource
可以正常工作,我一点也不明白。
这里出了什么问题?
您需要在 combiTable1Ds
中的 fileName
的修饰符中使用 Modelica.Utilities.Files.loadResource
。请注意 Modelica.Utilities.Files.loadResource
是一个 BAD 名称,该函数 不加载 任何东西,它只是从 Modelica URI 转换为绝对文件资源路径。
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;
在 OpenModelica 1.14.0 中,我想将文件中的一些数据加载到 combiTable1Ds 中。我想使用 Modelica URI 以可移植的方式指定路径(即不使用完整的绝对路径)。
但是我指定了 fileName
参数,但是,我总是收到找不到文件的错误消息。
最小示例:
我有以下包布局:
├── TestURI2
│ ├── loadURI2.mo
│ ├── package.mo
│ ├── package.order
│ └── somefile.txt
并在文件中 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;
如果我注释掉与 combiTable1Ds
相关的行,模型会编译并且 x 显示文件路径的正确长度,表明 loadResource
正确识别来自 URI 的路径 modelica://TestURI2/somefile.txt
.
如果我让 combiTable1Ds
处于活动状态,我会得到
/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.
鉴于 loadResource
可以正常工作,我一点也不明白。
这里出了什么问题?
您需要在 combiTable1Ds
中的 fileName
的修饰符中使用 Modelica.Utilities.Files.loadResource
。请注意 Modelica.Utilities.Files.loadResource
是一个 BAD 名称,该函数 不加载 任何东西,它只是从 Modelica URI 转换为绝对文件资源路径。
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;