如何使字符串可执行?

发布于 2024-11-17 20:50:05 字数 673 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-11-24 20:50:05

您想要做的事情在 Modelica 中通常是不可能的。某些工具可能具有允许执行此操作的“反射 API”(或者可能是采用命令字符串并执行它的内置函数),但肯定不存在跨工具工作的通用 API。

如果您想在 Dymola 中使用不同的参数值运行一堆模拟,我可以建议至少进行三种不同的操作。

  • 使用 DDE 接口向 Dymola 发送命令。通过这种方式,您可以“以某种方式”制定参数值(从 Dymola 外部),然后只需请求 Dymola 运行模拟。我不确定 DDE 接口有多丰富,所以我不确定它是否能满足您的需要(例如收获结果)。
  • 编写一个脚本文件。这与编写函数有点不同,但语法几乎相同。例如,要运行具有多个不同惯性值的“CoupledClutches”示例,您可以执行以下操作(在命令窗口中):
    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;
  • 使用函数(像以前一样)但使用修饰符调用simulateModel,例如
    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;
  • 使用内置函数simulateExtendedModelsimulateMultiExtendedModel 实际上与上面的功能几乎相同,但以更简洁的方式(输入 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.

  • Use the DDE interface to send commands to Dymola. This way you can formulate the parameter values "somehow" (externally from Dymola) and then just request Dymola to run simulations. I'm not sure how rich the DDE interface is, so I'm not sure if it will do what you need (e.g. reaping results).
  • Write a script file. This is a bit different from writing a function, but nearly the same in syntax. For example, to run the "CoupledClutches" example with several different inertia values, you can do this (in the command window):
    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;
  • Use a function (as you were) but call simulateModel with modifiers, e.g.
    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;
  • Use the built-in functions simulateExtendedModel and simulateMultiExtendedModel which actually do pretty much the same as above but in a cleaner way (type document("simulateExtendedModel") and document("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.

深府石板幽径 2024-11-24 20:50:05

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文