Modelica 编码标准 / Modelica When 语句

Modelica Coding Standards / Modelica When Statement

大家好,

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

如何避免这个错误?

这只是一个警告。当您像这样定义一个离散变量时,它取决于它在 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;