【问题标题】:Modelica Coding Standards / Modelica When StatementModelica 编码标准/Modelica When 语句
【发布时间】:2020-04-21 01:02:09
【问题描述】:

大家好,

2020 年新年快乐

我在 64 位 windows 7 系统上使用 Openmodelica 1.14 发行版。

我在使用 OMSimulator 中的“when”语句时遇到了一些问题。在寻找解决方案时,我遇到了closed ticket #2664 in Openmodelica。我仍然可以在当前版本的 Openmodelica 中看到报告的问题。

我包含了#2664票的相关文件。

model SimpleTest "just a simple model - Compilation etc."
  Modelica.Blocks.Interfaces.IntegerInput u annotation(Placement(visible = true, transformation(origin = {-100, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-80, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Interfaces.IntegerOutput y annotation(Placement(visible = true, transformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
algorithm
  when change(u) then
    y := y + 2;
  end when;
  annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Rectangle(origin = {-6.15, 2.93}, fillColor = {0, 133, 199}, fillPattern = FillPattern.HorizontalCylinder, extent = {{-77.89, 83.75}, {88.14, -92.53}})}));
end SimpleTest;

SimpleTest.mo 是否符合 Modelica 标准?

在编译 SimpleTest.mo 时,它会引发翻译警告

Assuming fixed start value for the following 1 variables:
         y:DISCRETE(flow=false fixed = false ) SimpleTest type: Integer

如何避免这个错误?

【问题讨论】:

    标签: modelica openmodelica


    【解决方案1】:

    这只是一个警告。当您定义像这样的离散变量时,它取决于它在 when 条件下的先前值,它必须具有固定的起始值。仅仅提供一个起始值是编译器的一个猜测值,当你修复它时,你告诉编译器它必须使用这个值进行初始化。

    如果您不提供起始值,则将其设置为零,如果您不修复它,编译器会自动修复它(导致警告)。

    简单示例:

    Integer y(start=0, fixed=true);
    

    应用于您的模型:

    model SimpleTest "just a simple model - Compilation etc."
      Modelica.Blocks.Interfaces.IntegerInput u annotation(Placement(visible = true, transformation(origin = {-100, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-80, 40}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
      Modelica.Blocks.Interfaces.IntegerOutput y(start=0, fixed=true) annotation(Placement(visible = true, transformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, 20}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
    algorithm
      when change(u) then
        y := y + 2;
      end when;
      annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2}), graphics = {Rectangle(origin = {-6.15, 2.93}, fillColor = {0, 133, 199}, fillPattern = FillPattern.HorizontalCylinder, extent = {{-77.89, 83.75}, {88.14, -92.53}})}));
    end SimpleTest;
    

    【讨论】:

    • y(start=0, fixed=true) 消除了翻译警告。 when 语句 y := y + 2 是有效的模型代码吗?
    • 对不起,我在这里错过了你的实际观点。在实际算法中,如果它在等式中,则需要声明 y = pre(y) + 2
    • 作为一个小说明:在算法内部只允许赋值,这与在常规编程语言中一样工作。另一方面,方程式可以重新排列,因此y = y + 2 将无效,因为它指出0=2 永远不会为真。您可以在方程式中制定whenìf 语句,如果这样做,您需要尊重这一点。 pre 运算符使用左限值,即上一个时间步的值。一般来说,我更喜欢尽可能使用方程式,但尽可能避免使用pre
    猜你喜欢
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2023-03-12
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多