You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Modelica模型的图层面板中显示变量值?

解决Modelica中Show.RealValue始终显示0.0的问题

问题分析

你通过Modelica.Blocks.Interfaces.RealOutput创建输出端口y传递变量x的值到Show.RealValue模块,但显示始终为0.0,核心原因是Interfaces.RealOutput是模型外部输出接口,设计目的是向外部模型暴露信号,而非用于内部信号传递,部分Modelica工具不会实时更新这类接口的内部连接值。

解决方案

方案一:直接给Show模块输入端口赋值(推荐)

无需额外创建输出端口,直接在方程中将x的值赋值给Show.RealValue的输入端口numberPort,代码修改如下:

model FirstOrderExperiment
  Real x "State variable";
  Modelica.Blocks.Interaction.Show.RealValue realValue annotation(
    Placement(visible = true, transformation(origin = {6, 4}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
initial equation
  x = 2 "Used before simulation to compute initial values";
equation
  der(x) = 1 - x "Drives value of x toward 1.0";
  realValue.numberPort = x; // 直接关联变量x与Show模块输入
end FirstOrderExperiment;

这种方式省去了中间端口和连接线,直接建立变量与显示模块的关联,仿真时能正确展示x的实时数值。

方案二:使用专用信号输出组件

如果需要保留外部输出端口,将Interfaces.RealOutput替换为Modelica.Blocks.Sources.RealOutput(专用信号输出组件),注意给组件的内置输出端口y赋值,再连接到Show模块:

model FirstOrderExperiment
  Real x "State variable";
  Modelica.Blocks.Sources.RealOutput y annotation(
    Placement(visible = true, transformation(origin = {-32, 10}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
  Modelica.Blocks.Interaction.Show.RealValue realValue annotation(
    Placement(visible = true, transformation(origin = {6, 4}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
initial equation
  x = 2 "Used before simulation to compute initial values";
equation
  der(x) = 1 - x "Drives value of x toward 1.0";
  y.y = x; // 给RealOutput组件的输出端口赋值
  connect(y.y, realValue.numberPort) annotation(
    Line(points = {{-32, 10}, {-18.75, 10}, {-18.75, 4}, {-5.5, 4}}, color = {0, 0, 127}));
end FirstOrderExperiment;

说明

Modelica.Blocks.Interfaces.RealOutput属于模型的外部接口,仅用于模型间的信号传递,内部使用时工具可能不会实时更新其值;而直接赋值或使用专用信号组件能确保显示模块获取到变量的实时数值。

内容的提问来源于stack exchange,提问作者gcoopermax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:53:09