如何在OpenModelica的Modeling视图中显示静态模型计算结果
问题描述
我是Modelica新手,希望在OpenModelica中实现两个静态数值相加,并在Modeling视图中显示计算结果。
我创建了两个Modelica.Blocks.Sources.Constant源(source1=10、source2=20),并使用Modelica.Blocks.Math.Add模块实现相加(result=source1+source2),完整模型代码如下:
model demo Modelica.Blocks.Sources.Constant source1(k = 10) annotation( Placement(visible = true, transformation(origin = {-70, 52}, extent = {{-10, 10}, {10, -10}}, rotation = 0))); Modelica.Blocks.Sources.Constant source2(k = 20) annotation( Placement(visible = true, transformation(origin = {-70, -12}, extent = {{-10, 10}, {10, -10}}, rotation = 0))); Modelica.Blocks.Math.Add add annotation( Placement(visible = true, transformation(origin = {-12, 10}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); equation connect(source1.y, add.u1) annotation( Line(points = {{-58, 52}, {-24, 52}, {-24, 16}}, color = {0, 0, 127})); connect(source2.y, add.u2) annotation( Line(points = {{-58, -12}, {-24, -12}, {-24, 4}}, color = {0, 0, 127})); annotation( uses(Modelica(version = "4.0.0")), Diagram); end demo;
目前我可以运行仿真,在Plotting视图中选择add模块的结果生成图表,但我希望留在Modeling视图中,通过某个显示元件直接展示结果值30。请问:
- OpenModelica是否提供这类显示元件?
- 是否必须切换到Plotting视图(即使是无需时间变量的静态模型)?
- 如果没有现成元件,能否自定义(需访问仿真结果)?
解决方案
- 有现成的显示元件:可直接使用
Modelica.Blocks.Sinks.Display模块,它专门用于在Modeling视图的Diagram界面实时显示输入数值。 - 无需切换到Plotting视图:静态模型完全可以通过
Display元件在Modeling视图直接查看结果,不需要跳转至Plotting视图生成图表。 - 自定义元件可行,但无需额外操作:虽然可以自定义显示元件(比如编写简单Modelica模型结合仿真结果更新显示逻辑),但现成的
Display元件已满足需求,无需额外自定义。
修改后的示例模型
在原有模型中添加Display元件并连接到add模块的输出,代码如下:
model demo Modelica.Blocks.Sources.Constant source1(k = 10) annotation( Placement(visible = true, transformation(origin = {-70, 52}, extent = {{-10, 10}, {10, -10}}, rotation = 0))); Modelica.Blocks.Sources.Constant source2(k = 20) annotation( Placement(visible = true, transformation(origin = {-70, -12}, extent = {{-10, 10}, {10, -10}}, rotation = 0))); Modelica.Blocks.Math.Add add annotation( Placement(visible = true, transformation(origin = {-12, 10}, extent = {{-10, -10}, {10, 10}}, rotation = 0))); Modelica.Blocks.Sinks.Display resultDisplay annotation( Placement(visible = true, transformation(origin = {50, 10}, extent = {{-20, -15}, {20, 15}}, rotation = 0))); equation connect(source1.y, add.u1) annotation( Line(points = {{-58, 52}, {-24, 52}, {-24, 16}}, color = {0, 0, 127})); connect(source2.y, add.u2) annotation( Line(points = {{-58, -12}, {-24, -12}, {-24, 4}}, color = {0, 0, 127})); connect(add.y, resultDisplay.u) annotation( Line(points = {{2, 10}, {38, 10}}, color = {0, 0, 127})); annotation( uses(Modelica(version = "4.0.0")), Diagram); end demo;
运行仿真后,你会在Modeling视图的Diagram界面看到resultDisplay元件直接显示计算结果30。
内容的提问来源于stack exchange,提问作者Stefan
相关产品推荐
相关产品推荐

