参数化的Modelica库以及使用模型作为参数的可能性

时间:2019-03-12 11:52:12

标签: modelica jmodelica

我尝试制作一个参数化的库。使用包和连接器作为参数,我可以很好地工作。

也可以使用模型作为参数。 但是,如果在库中使用该模型通过扩展来构建新模型,那么我所理解的是不允许的。

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

下面是一个简单的示例来说明问题。 TEST是库,Fish3b是应用程序。当我在库TEST中运行示例时,所有方法都可以正常运行,但是当我有单独的应用程序文件时,则不能。

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

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;

2 个答案:

答案 0 :(得分:1)

janpeter的答案有效。

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

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”移动到基本模型FishType上,从而导致:

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;

encapsulated package T2_Fish3

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

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

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

end T2_Fish3;

答案 1 :(得分:0)

之前使用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;