问题描述
我想在经济建模(例如系统动力学)中使用非国际单位制单位。虽然我当然可以花几秒钟(s)然后使用,但据我所知,在我主要使用的 System Modeler 中displayUnit
没有很好的方法来修改displayUnit的时间。
因此,在编写一个库时,我希望用户选择一个全局type
调用ModelTime
,理想情况下它会被声明为inner
某个replaceable
顶级类。然后模型中的任何组件都可以使用全局type
来一致地处理任何与时间相关的变量。
最小的例子
下面的例子展示了我想如何实现它。
package Units
声明了两种非国际单位制单位类型(Time_year
,Time_month
)package Interfaces
包含一个部分模型类GenericSimulationModel
,它将成为使用该库编写的任何模型的顶级范围。它应该提供type ModelTime
asinner
和replaceable
类package Components
定义一个简单的block
类,该类ModelTime
通过outer
定义来定义其在全局选择的时间单位中的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 中没有使用重新声明的类型。
我能做些什么来实现我想做的事情?