我正在尝试在Modelica中执行一个字符串。该字符串将保存在变量中,以便在需要时能够更改它。
function Test
input String inComp="resistor.R:=2";
output String outComp;
algorithm
outComp:=inComp;
end Test;
你能不能
我正在使用Dymola。
我需要做的是以下内容。
- 从文本文件中读取组件名称(或在执行函数时输入它们) - 然后更改这些组件的参数。这段代码就是一个例子:
function Test
input String inComp="resistor"; //Entered by the user, or read from a text file
output Real result;
algorithm
inComp.R :=2 ; /*This is incorrect since it wouldn't understand that
I want to enter : resistor.R := 2; */
result := inComp.R ; //In order to view the result
end Test;
答案 0 :(得分:6)
在Modelica中,您通常不会尝试做什么。可能有些工具有一个“反射API”允许这个(或者可能是一个带有命令字符串并执行它的内置函数)但是当然没有适用于各种工具的通用API。
如果你想在Dymola中运行一系列具有不同参数值的模拟,我建议至少有三种不同的想要继续。
for j in {1.0, 1.1, 1.2, 1.5, 1.8} loop J1.J := j; simulateModel("Modelica.Mechanics.Rotational.Examples.CoupledClutches", resultFile="CoupledClutches_"+String(j)); end for;
function RunLoop algorithm for j in {1.0, 1.1, 1.2, 1.5, 1.8} loop simulateModel("Modelica.Mechanics.Rotational.Examples.CoupledClutches(J1(J="+String(j)+"))", resultFile="CoupledClutches_"+String(j)); end for; end RunLoop;
simulateExtendedModel
和simulateMultiExtendedModel
实际上与上面几乎完全相同,但是更简洁(Dymola中的document("simulateExtendedModel")
和document("simulateMultiExtendedModel")
类型命令窗口以获取有关这些的更多信息)。好的,那应该给你一个开始。如果这些都不能用于任何原因,只需用你的其他任何要求更新问题。
答案 1 :(得分:0)
使用Perl等动态编写和执行某些脚本的不同选项。例如,Text::Template
可以用作模板引擎。我经常为LaTeX做这件事。