参数化Modelica库和将模型用作参数的可能性

参数化Modelica库和将模型用作参数的可能性,modelica,jmodelica,Modelica,Jmodelica,我试图建立一个参数化的库。我可以使用包和连接器作为参数 也可以使用模型作为参数。 但是,据我所知,如果在库中使用该模型来使用extend构建新模型,那么它是不允许的 我想知道库中是否包含具有内部/外部连接器样式的模型,是否允许将内部模型作为库的参数 下面是一个简单的例子来说明这个问题。TEST是库,Fish3b是应用程序。当我在库测试中运行这个示例时,它都可以工作,但是当我有一个单独的应用程序文件时,它就不能工作了 错误文本为:找不到运行JModelica 2.4的类型的类声明 package

我试图建立一个参数化的库。我可以使用包和连接器作为参数

也可以使用模型作为参数。 但是,据我所知,如果在库中使用该模型来使用extend构建新模型,那么它是不允许的

我想知道库中是否包含具有内部/外部连接器样式的模型,是否允许将内部模型作为库的参数

下面是一个简单的例子来说明这个问题。TEST是库,Fish3b是应用程序。当我在库测试中运行这个示例时,它都可以工作,但是当我有一个单独的应用程序文件时,它就不能工作了

错误文本为:找不到运行JModelica 2.4的类型的类声明

package TEST

   model FishType1
       outer Real T;
       Real health;
   equation
       health = 30-T;
   end FishType1;

   model FishType2
       outer Real T;
       Real health;
   equation
       health = 32-T;
   end FishType2;

   package Equipment
       model AquariumType
           replaceable model FishType
           end FishType;       
           FishType fish;
           inner Real T;
       equation
          T = 29;
       end AquariumType;
   end Equipment;

   // Adapt AquariumType model to actual fish
   model Aquarium
       import TEST.Equipment.AquariumType;
       extends AquariumType(redeclare model FishType=FishType2);
   end Aquarium;

   // Example
   model Example
       Aquarium aquarium;
   end Example;

end TEST;
下面是从上面的库导入的应用程序代码示例 -我想这里有一些错误

   encapsulated package Fish3b

       model FishType3
           outer Real T;
           Real health;
       equation
           health = 34-T;
       end FishType3;

       // Adapt package Equipment with AquariumType model to actual fish
       package Equipment3 
           import TEST.Equipment;
           extends Equipment.AquariumType(redeclare model   FishType=FishType3);
       end Equipment3;

       // Example
       model Example
           import Fish3b.Equipment3;
           Equipment3.AquariumType aquarium;
       end Example;

   end Fish3b;

谢谢“tbeu”的评论

我已经修改了代码,这样包装设备就不会从模型扩展。下面更新的代码也更好地代表了我潜在的“真实”问题。这一切都很有效。 谢谢

更新的库文件TEST2.mo:

   package TEST2

       model FishType1
           outer Real T;
           Real health;
       equation
           health = 30-T;
       end FishType1;

       model FishType2
           outer Real T;
           Real health;
       equation
           health = 32-T;
       end FishType2;

       package Equipment
           replaceable model FishType
           end FishType;

           constant Integer dummy = 1;

           model AquariumType
               FishType fish;
               inner Real T;
           equation
               T = 29;
           end AquariumType;
       end Equipment;

       // Adapt package Equipment to the actual fish
       package Equipment1
           import TEST2.Equipment;
           extends Equipment(redeclare model FishType=FishType1);
       end Equipment1;

       // Example
       model Example
           Equipment1.AquariumType aquarium;
       end Example;

    end TEST2;
应用程序代码T2_Fish3现在使用上述库TEST2:

   encapsulated package T2_Fish3

       model FishType3
           outer Real T;
           Real health;
       equation
           health = 34-T;
       end FishType3;

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment(redeclare model FishType=FishType3);
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

   end T2_Fish3;

janpeter的答案是有效的

另一种避免引入称为“FishType1”、“FishType3”等模型的替代方法是使用“重新声明模型扩展”,如下所示(Test2可以保持不变,也可以对设备1进行相同的更改),但它使用更高级的结构

encapsulated package T2_Fish3

       // Adapt package Equipment to the actual fish
       package Equipment3
           import TEST2.Equipment;
           extends Equipment;

           redeclare model extends FishType
            outer Real T;
            Real health;
           equation 
             health = 32-T;
           end FishType;
       end Equipment3;

       // Example
       model Example
           Equipment3.AquariumType aquarium;
       end Example;

end T2_Fish3;
此外,可以将公共“外部真实T”移动到基本模型鱼尾类型,从而导致:

package TEST2

    package Equipment
        replaceable model FishType
          outer Real T;
        end FishType;

        constant Integer dummy = 1;

        model AquariumType
            FishType fish;
            inner Real T;
        equation 
            T = 29;
        end AquariumType;
    end Equipment;

    // Adapt package Equipment to the actual fish
    package Equipment1
      import TEST2.Equipment;

      extends Equipment;
      redeclare model extends FishType
        Real health;
      equation 
        health = 30 - T;
      end FishType;
    end Equipment1;

    // Example
    model Example
        Equipment1.AquariumType aquarium;
    end Example;

end TEST2;


使用Hans Olsson之前的输入更新了代码,但与上面建议的更正略有不同。 我设法避免“从可替换模型扩展”,但我不太确定这有多重要,请参见我之前的评论

此外,我想我要保持不同鱼类的清晰身份,并在包装设备外申报

    package TEST3

        partial model FishBase
            outer Real T;
        end FishBase;

        model FishType1
            extends FishBase;
            Real health;
        equation
            health = 30 - T;
        end FishType1;

        model FishType2
            extends FishBase;
            Real health;
        equation
            health = 32 - T;
        end FishType2;  

        package Equipment
            replaceable model FishType
            end FishType;

            constant Integer dummy = 1;

            model AquariumType
                FishType fish;
                inner Real T;
            equation
                T = 29;
            end AquariumType;
        end Equipment;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment;
            redeclare model FishType=FishType2;
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end TEST3;
以及应用程序代码的示例:

    encapsulated package T3_Fish3

        model FishType3
            import TEST3.FishBase;
            extends FishBase;
            Real health;
        equation
            health = 34-T;
        end FishType3;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment(redeclare model FishType=FishType3);
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end T3_Fish3;

这不是[SSCCE][1],因为无法解析应用程序代码中的命名空间
Library
。[1] :抱歉,我发布代码时,代码中的术语“库”更改为“设备”。我想在文章中更普遍地使用单词库,所以库文件是测试,包设备是库中的一部分,我想使用模型FishType的parametrise。我在文中说,更正后的代码确实存在问题。Fish3b.Equipment3(package)的Modelica类及其基类TEST.Equipment.Oquarium类型(model)不兼容,因为包不能从模型扩展。看到替代解决方案总是很好的!有趣的是,我们可以“重新声明模型扩展”并避免更新实际模型的名称。我认为这将非常有用。我认为在您的包TEST2中,我认为您的基本模型应该具有部分模型FishType,并且不可替换,对吗?在您的应用程序代码T2\u Fish3中,您声明了“外部真实T”不需要,因为在部分基模型中声明了。但可能具有良好的可读性。是的,FishType应该是可替换的和部分的。我同意可能会错过继承的声明,但我通常认为重复声明仍然不是一个好的做法。我认为从可替换模型扩展是更重要的e或更少禁止!?最近从OpenModelica社区得到了这样的评论。在他们新的OM编译器2.0-beta测试中,你会得到这种错误,但在当前的1.13或1.14中没有,JModelica没有什么问题,没有给出错误或警告,模拟似乎还可以。Modelica def 6.2.1对我来说也不是很清楚。Fritzon在在他的书2015年第2版中第4.15节。在这种情况下是合法的。禁止从未知模型扩展的想法是为了避免新的基本模型元素破坏东西。在这种情况下不会发生这种情况,因为基本模型是已知的。有关更多信息,请参阅第7.3.1节。了解您有一个“好的”编写重新声明FishType的应用程序代码,该代码从已知的FishType扩展而来,并且是“安全的”。在您的示例中,可替换的代码可能只是交换为部分代码,而重新声明被忽略了……。我甚至在JModelica中尝试过它(太宽容了?),可能无法通过Dymola编译器?我试图在本示例中找出一个例子,它在哪里““让基类像您一样可替换,但却无法轻松看到这一点,这是值得的吗?”?我理解7.3.1是6.2.1的一个重要例外,并在最后一行提到。将其视为“规则”,以确保在“更高”级别上的正确性。
    encapsulated package T3_Fish3

        model FishType3
            import TEST3.FishBase;
            extends FishBase;
            Real health;
        equation
            health = 34-T;
        end FishType3;

        // Adapt package Equipment to the actual fish
        package Equipment3
            import TEST3.Equipment;
            extends Equipment(redeclare model FishType=FishType3);
        end Equipment3;

        // Example
        model Example
            Equipment3.AquariumType aquarium;
        end Example;

    end T3_Fish3;