如何使字符串可执行?
我正在尝试在 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;
I'm trying to execute a string in Modelica. This string would be saved in a variable in order to be able to change it when I need to.
function Test
input String inComp="resistor.R:=2";
output String outComp;
algorithm
outComp:=inComp;
end Test;
Could you please
I am using Dymola.
What I need to do is the following.
-Read component names from a text file (or input them while executing the function)
-Then change parameters of these components. This code is an example:
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要做的事情在 Modelica 中通常是不可能的。某些工具可能具有允许执行此操作的“反射 API”(或者可能是采用命令字符串并执行它的内置函数),但肯定不存在跨工具工作的通用 API。
如果您想在 Dymola 中使用不同的参数值运行一堆模拟,我可以建议至少进行三种不同的操作。
simulateExtendedModel
和simulateMultiExtendedModel
实际上与上面的功能几乎相同,但以更简洁的方式(输入document("simulateExtendedModel")
和Dymola 命令窗口中的document("simulateMultiExtendedModel")
以获取有关这些的更多信息)。好的,这应该给你一个开始。如果由于某种原因这些都不起作用,只需根据您的任何附加要求更新问题即可。
What you are trying to do is not generally possible in Modelica. It may be that some tools have a "reflective API" that allows this (or perhaps a built-in function that takes a command string and executes it) but there is certainly no universal API that works across tools.
If you want to run a bunch of simulations in Dymola with different parameter values, I can suggest at least three different wants to proceed.
simulateExtendedModel
andsimulateMultiExtendedModel
which actually do pretty much the same as above but in a cleaner way (typedocument("simulateExtendedModel")
anddocument("simulateMultiExtendedModel")
in the Dymola command window to get more info on these).OK, that should give you a start. If none of those work for whatever reason, just update the question with whatever additional requirements you have.
使用 Perl 等动态编写和执行某些脚本的不同选项。例如
Text::Template
可以用作模板引擎。我经常对 LaTeX 这样做。A different option to use Perl etc. to dynamically write and execute some script. For example
Text::Template
could be used as a templating engine. I do this for LaTeX regularly.