简体   繁体   English

如何通过 Dymola 中的脚本定义 Combitimetable?

[英]How to define a Combitimetable through a script in Dymola?

I am trying to perform several simulations in a sequence using a for loop in a script.我正在尝试在脚本中使用 for 循环按顺序执行多个模拟。 From simulation to simulation, the only variable to change is the file path of a Combitimetable.从模拟到模拟,唯一要改变的变量是 Combitimetable 的文件路径。

I propagated the variable fileName in order to assign a new path in each iteration.我传播了变量 fileName 以便在每次迭代中分配一个新路径。 However, when the model reads the extension, changes the timeScale and the resolution is lower than needed.但是,当 model 读取扩展名时,更改了 timeScale 并且分辨率低于需要。 I tried to propagate timeScale too, but without changes.我也尝试传播 timeScale,但没有改变。 Is there a function to define the Combitimetable variables?是否有 function 来定义 Combitimetable 变量? Is my only alternative to merge all tables and split the results manually?我唯一的选择是合并所有表并手动拆分结果吗?

Example of the script on a single run (without the for loop):单次运行的脚本示例(没有 for 循环):

filePath="RL_30_200g";
dymolaPath = "modelica://customTILComponents/Combitables/Combitimetable_"+filePath+".txt";
fileName= ModelicaServices.ExternalReferences.loadResource(dymolaPath);
result ="Full_Year_Simulation_"+filePath;
timeScale = 1/3600;
translateModel ("customTILComponents.MA_Santoro.FullModels.OptiHorst_FullModel_New_Year_Simulation_Batch");
simulateModel(startTime=0,stopTime=8860,numberOfIntervals=300,method="Dassl",tolerance=0.000001,resultFile=result);

I am not sure where your problem is and how you change fileName .我不确定您的问题出在哪里以及您如何更改fileName In your question timeScale is also not used anywhere.在您的问题timeScale也没有在任何地方使用。 Anyway, here is how I would do it: add a parameter to your model for fileName .无论如何,我会这样做:为fileName添加一个参数到您的 model 。 Since it is a string, the only way to change it is via a modifier which can be included in the model name of the simulateModel command.由于它是一个字符串,因此更改它的唯一方法是通过一个修饰符,该修饰符可以包含在simulateModel命令的 model 名称中。

Here is an example: In your model with the time table, propagate the parameter fileName :这是一个示例:在带有时间表的 model 中,传播参数fileName

model MyModel
  parameter String fileName="NoName" "File where matrix is stored";
  Modelica.Blocks.Tables.CombiTable1Ds combiTable1Ds(
    tableOnFile=true,
    tableName="x",
    fileName=fileName) annotation (Placement(transformation(extent={{-10,-10},{10,10}})));
  Modelica.Blocks.Sources.Ramp ramp(duration=1) annotation (Placement(transformation(extent={{-60,-10},{-40,10}})));
equation 
  connect(ramp.y, combiTable1Ds.u) annotation (Line(points={{-39,0},{-12,0}}, color={0,0,127}));
  annotation (uses(Modelica(version="4.0.0")));
end MyModel;

Then change the value of fileName in every loop.然后在每个循环中更改fileName的值。 Here we assume that there are three.mat files available in the workspace, named First.mat , Second.mat and Third.mat .这里我们假设工作区中有三个 .mat 文件,分别命名为First.matSecond.matThird.mat

function batchSim
  input String fileNames[:] = {"First", "Second", "Third"};
algorithm 
  for f in fileNames loop
    simulateModel("MyModel(fileName=\""+f+".mat\")", stopTime=1, resultFile="Full_Year_Simulation_"+f);
  end for;
  annotation(__Dymola_interactive=true);
end batchSim;

This works quite well, but the downside is that the model will be recompiled in every iteration of the for loop, as the modifier has changed.这工作得很好,但缺点是 model 将在 for 循环的每次迭代中重新编译,因为修饰符已更改。 If this is a big problem, define all file paths in a string vector in the model and add an integer parameter for the index.如果这是一个大问题,请在 model 中的字符串向量中定义所有文件路径,并为索引添加一个 integer 参数。 Then use the command simulateExtendedModel and change only the index via the parameters initialNames and initialValues .然后使用命令simulateExtendedModel并通过参数initialNamesinitialValues仅更改索引。

Building on the answer by marco (so same model and same external files) an alternative is to make a script such as:基于 marco 的答案(相同的 model 和相同的外部文件),另一种方法是制作一个脚本,例如:

fileNames := {"First", "Second", "Third"};
Advanced.AllowStringParameters:=true;
translateModel("MyModel");
for f in fileNames loop
  fileName:=f;
  simulateModel("MyModel", stopTime=1, resultFile="Full_Year_Simulation_"+f);
end for;

Unfortunately it seems you cannot turn that into a function.不幸的是,您似乎无法将其转换为 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM