【问题标题】:Can a type be set globally using inner/outer and be replaceable?可以使用内部/外部全局设置类型并且可以替换吗?
【发布时间】:2019-10-22 19:25:28
【问题描述】:

问题描述

我想在经济建模(例如系统动力学)中使用非国际单位制单位。虽然我当然可以使用 seconds (s) 然后使用 displayUnit,但据我所知没有很好的方法来修改 displayUnit time 在我主要使用的 System Modeler 中。

所以,在编写一个库时,我希望用户选择一个名为 ModelTime 的全局 type,理想情况下,它会在某个顶级类中声明为 innerreplaceable。然后模型中的任何组件都可以使用全局 type 来一致地处理任何与时间相关的变量。

小例子

以下示例显示了我想如何实现它。

  • package Units 声明了两种非国际单位制单位类型(Time_yearTime_month
  • package Interfaces 包含一个部分模型类 GenericSimulationModel,它将是使用该库编写的任何模型的顶级范围。它应该提供type ModelTime 作为innerreplaceable
  • 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


【解决方案1】:

我从 Wolfram MathCore 的某个人(系统建模器的开发人员)那里收到了一些 feedback on Wolfram Community

您看到的 MinimalExample.example 和 MinimalLibrary.Example 的行为是错误,据我所知,它们应该可以工作,我已将它们转发给从事这些工作的开发人员。

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 2013-05-31
    • 2011-08-11
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    相关资源
    最近更新 更多