可以使用参数来设置组件的单位属性吗?

Can a parameter be used to set the unit attribute for a component?

到目前为止,使用 Wolfram System Modeler 4.3 和 5.1 可以无错误地编译以下最小示例:

model UnitErrorModel
    MyComponent c( hasUnit = "myUnit" );

    block MyComponent
        parameter String hasUnit = "1";
        output Real y( unit = hasUnit );
    equation
        y = 10;
    end MyComponent;

end UnitErrorModel;

但是随着 WSM 12.0 的新版本(版本的跳跃是由于与当前版本的 Wolfram 旗舰 Mathematica 保持一致)我收到一条错误消息:

Internal error: Codegen.getValueString: Non-constant expression:c.hasUnit

(注意:错误是由 Mathematica 12.0 中的 WSMLink'WSMSimulate 给出的,它在内部是 运行ning System Modeler 12.0;这里要求 "InternalValues" 属性以上型号,因为我现在还没有安装 WSM 12.0)。

尝试在 OpenModelica [OMEdit v. 1.13.2(64 位)] 中模拟上述模型显示:

SimCodeUtil.mo: 8492:9-8492:218]: Internal error Unexpected expression (should have been handled earlier, probably in the front-end. Unit/displayUnit expression is not a string literal: c.hasUnit

所以似乎要设置单位属性我不能使用具有参数可变性的变量?这是为什么 - 毕竟编译器在编译 运行 时间时可以硬连线单元还不够吗(毕竟给定的模型 运行 在 WSM 4.3 和 5.1 中没有任何错误) ?

编辑:从开始,我相信至少可以使用最终参数来设置单位属性。最终修改(例如 c( final hasUnit = "myUnit" ) 不能解决问题。

Wolfram MathCore 的某个人就此问题向我提供了 feedback on Wolfram Community

You are correct in that it's not in violation with the specification, although making it a constant makes more sense since you would invalidate all your static unit checking if you are allowed to change the unit after building the simulation. We filed an issue on the specification regarding this (Modelica Specification Issue # 2362).

因此,MatheCore 在提出他们已经实施的 Modelica 规范更改方面领先了一点。 ;-)

注意:在 Wolfram System Modeler (12.0) 中使用注释 Evaluate = true 将无法解决问题(参见上面@matth 的评论)。

作为解决方法,用于设置 unit 属性的变量应该具有 常量 可变性,但仍然可以包含在使用 annotation(Dialog(group = "GroupName")).

以交互方式更改用户对话框