首页 > 解决方案 > 参数化 Modelica 库和使用模型作为参数的可能性 - 第 2 部分

问题描述

我的工作是了解库中的参数化包如何适应外部应用程序代码,即我喜欢将库代码和应用程​​序代码保持分离。

在我的示例中,我的设备包有两个参数,一个连接器 LCon 和一个模型 CType。连接器影响设备包中的所有模型。模型 CType 仅影响设备中的一个模型 RType(并且具有更密切的内外关系)到该模型。

当我使 Package Equipment 适应 LCon2 和 CTyp2 的应用要求时,我可以如下所示一次性完成。

   code1

   package Equipment2
          import BR5i.Equipment;
          extends Equipment(redeclare connector LCon=LCon2,
                      redeclare model CType=CType2);
   end Equipment2;    

但是,如果我将这两种改编分成两个不同的部分,我认为代码(从长远来看)更具可读性。我尝试下面的代码,但不起作用。错误文本:找不到 RType 的类声明 - 当我在 JModelica 中运行它时。

   code2

   package Equipment2
       import BR5i.Equipment;
       extends Equipment(redeclare connector LCon=LCon2);
   end Equipment2;

   model BRType2        
       import BR5i.Equipment2.RType;
       extends RType(redeclare model CType=CType2);
   end BRType2;

(对于代码 2,库进行了修改,因此将参数 CType 移动到了单个模型 RType,其中 CType 应作为参数。最后,我希望代码 2 的 BRType2 对应于代码 1 中的 Equipment2.RType) .

我想知道是否有可能在这样的几个步骤中进行更改,即首先 RType 获得一个新的连接器 LCon2,然后在下一步中,现在从 Equipment2 导入的 RType 将 CType 替换为 CType2?

我知道代码不应被视为一系列“赋值语句”,而应视为并行。在我看来,代码 2 中“方程式”的逻辑应该可以得到正确的 BRType2。

标签: modelicaopenmodelicajmodelica

解决方案


您的“code2”将导致 BRType2没有修改 CType。重新声明并不意味着“将包 A 更改为这样”,而是“这应该像包 A,但有这些更改”。因此,要获得您想要的结果,您应该执行以下操作:

package Equipment2
    import BR5i.Equipment;
    extends Equipment(redeclare connector LCon=LCon2);
end Equipment2;

model BRType2
    // Equipment2 has the change in LCon, so extend RType from there instead
    import Equipment2.RType;
    extends RType(redeclare model CType=CType2);
end BRType2;

另请注意,如果 Equipment 包含 Rtype 的任何实例或其他引用,则此方法不会给出预期结果,因为它们将引用未更改的 RType,而不是 BRType2。

为什么你会收到关于找不到 RType 的错误,我不能说。这可能是一个错误,但我会首先检查您是否正确编写了它的路径。


推荐阅读