【发布时间】:2019-10-22 19:25:28
【问题描述】:
问题描述
我想在经济建模(例如系统动力学)中使用非国际单位制单位。虽然我当然可以使用 seconds (s) 然后使用 displayUnit,但据我所知没有很好的方法来修改 displayUnit time 在我主要使用的 System Modeler 中。
所以,在编写一个库时,我希望用户选择一个名为 ModelTime 的全局 type,理想情况下,它会在某个顶级类中声明为 inner 和 replaceable。然后模型中的任何组件都可以使用全局 type 来一致地处理任何与时间相关的变量。
小例子
以下示例显示了我想如何实现它。
-
package Units声明了两种非国际单位制单位类型(Time_year、Time_month) -
package Interfaces包含一个部分模型类GenericSimulationModel,它将是使用该库编写的任何模型的顶级范围。它应该提供type ModelTime作为inner和replaceable类 -
package Components定义了一个简单的block类,该类通过outer定义使用ModelTime来定义其output y,该类简单地以全局选择的时间单位显示time -
model Example将所有这些联系在一起,提供一个示例,使用该库的任何模型应该如何工作
代码如下:
model MinimalExample
package Units
type Time_year = Real(final quantity = "Time", final unit = "yr");
type Time_month = Real(final quantity = "Time", final unit = "mo");
end Units;
package Interfaces
partial model GenericSimulationModel "Top-level model scope providing global vars"
inner replaceable type ModelTime = Years "Set to : Months, Years";
protected
type Years = Units.Time_year;
type Months = Units.Time_month;
end GenericSimulationModel;
end Interfaces;
package Components
block ComponentUsingTime
outer type ModelTime = MinimalExample.Units.Time_year;
output ModelTime y;
equation
y = time;
end ComponentUsingTime;
end Components;
model Example
extends Interfaces.GenericSimulationModel(
redeclare replaceable type ModelTime = Months
);
Components.ComponentUsingTime c;
end Example;
equation
end MinimalExample;
虽然在 System Modeler 和 OpenModelica 中所有内容都可以正常编译,但不幸的是,它并没有成功:在上面给出的 Example 模型中的组件 c 中没有使用重新声明的类型。
我可以做些什么来实现我想做的事情?
【问题讨论】:
-
我在 Wolfram 社区上交叉发布了这个问题(参见here)。
-
OpenModelica允许通过在pltting视图中选择“Simulation Time Unit”来设置显示单位。这还不够吗?
标签: modelica units-of-measurement openmodelica systemmodeler