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

Parametrised Modelica library and possibility to use models as parameters

我尝试制作一个参数化库。我使用包和连接器作为参数工作正常。

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

我想知道库中是否包含带有inner/outer样式连接器的模型,是否允许让内部模型成为库的参数?

下面用一个简单的例子来说明问题。 TEST 是库,Fish3b 是应用程序。当我 运行 库 TEST 中的示例时,它一切正常,但是当我有一个单独的应用程序文件时,它没有。

错误文本是:找不到 class AquariumType 声明 运行ning 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;

以及现在使用上述库 TEST2 的应用程序代码 T2_Fish3:

   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" 等模型的替代方法是使用 "redeclare model extends" 如下(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;

此外,可以将常见的 "outer Real 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;

根据 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;